|
| 1 | +/**************************************************************************** |
| 2 | + * apps/netutils/netlib/netlib_dhcp_ntp.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#ifdef CONFIG_NETUTILS_DHCPC |
| 30 | + |
| 31 | +#include <errno.h> |
| 32 | +#include <pthread.h> |
| 33 | +#include <stdlib.h> |
| 34 | +#include <string.h> |
| 35 | + |
| 36 | +#include "netutils/netlib.h" |
| 37 | + |
| 38 | +/**************************************************************************** |
| 39 | + * Private Data |
| 40 | + ****************************************************************************/ |
| 41 | + |
| 42 | +static pthread_mutex_t g_dhcp_ntp_lock = PTHREAD_MUTEX_INITIALIZER; |
| 43 | +static FAR char *g_dhcp_ntp_servers; |
| 44 | +static netlib_dhcp_ntp_callback_t g_dhcp_ntp_callback; |
| 45 | +static FAR void *g_dhcp_ntp_callback_arg; |
| 46 | + |
| 47 | +/**************************************************************************** |
| 48 | + * Private Functions |
| 49 | + ****************************************************************************/ |
| 50 | + |
| 51 | +static FAR char *netlib_dhcp_ntp_dup(FAR const char *ntp_server_list) |
| 52 | +{ |
| 53 | + if (ntp_server_list == NULL || ntp_server_list[0] == '\0') |
| 54 | + { |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + |
| 58 | + return strdup(ntp_server_list); |
| 59 | +} |
| 60 | + |
| 61 | +/**************************************************************************** |
| 62 | + * Public Functions |
| 63 | + ****************************************************************************/ |
| 64 | + |
| 65 | +int netlib_set_ntp_servers_from_dhcp(FAR const char *ntp_server_list) |
| 66 | +{ |
| 67 | + FAR char *new_servers; |
| 68 | + FAR char *notify_servers = NULL; |
| 69 | + FAR char *old_servers; |
| 70 | + netlib_dhcp_ntp_callback_t callback; |
| 71 | + FAR void *arg; |
| 72 | + int ret = OK; |
| 73 | + |
| 74 | + new_servers = netlib_dhcp_ntp_dup(ntp_server_list); |
| 75 | + if (ntp_server_list != NULL && ntp_server_list[0] != '\0' && |
| 76 | + new_servers == NULL) |
| 77 | + { |
| 78 | + return -ENOMEM; |
| 79 | + } |
| 80 | + |
| 81 | + pthread_mutex_lock(&g_dhcp_ntp_lock); |
| 82 | + |
| 83 | + if ((g_dhcp_ntp_servers == NULL && new_servers == NULL) || |
| 84 | + (g_dhcp_ntp_servers != NULL && new_servers != NULL && |
| 85 | + strcmp(g_dhcp_ntp_servers, new_servers) == 0)) |
| 86 | + { |
| 87 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 88 | + free(new_servers); |
| 89 | + return OK; |
| 90 | + } |
| 91 | + |
| 92 | + callback = g_dhcp_ntp_callback; |
| 93 | + arg = g_dhcp_ntp_callback_arg; |
| 94 | + |
| 95 | + if (callback != NULL && new_servers != NULL) |
| 96 | + { |
| 97 | + notify_servers = strdup(new_servers); |
| 98 | + if (notify_servers == NULL) |
| 99 | + { |
| 100 | + ret = -ENOMEM; |
| 101 | + goto errout_with_lock; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + old_servers = g_dhcp_ntp_servers; |
| 106 | + g_dhcp_ntp_servers = new_servers; |
| 107 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 108 | + |
| 109 | + free(old_servers); |
| 110 | + |
| 111 | + if (callback != NULL) |
| 112 | + { |
| 113 | + callback(notify_servers, arg); |
| 114 | + } |
| 115 | + |
| 116 | + free(notify_servers); |
| 117 | + return OK; |
| 118 | + |
| 119 | +errout_with_lock: |
| 120 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 121 | + free(new_servers); |
| 122 | + return ret; |
| 123 | +} |
| 124 | + |
| 125 | +int netlib_register_dhcp_ntp_callback(netlib_dhcp_ntp_callback_t callback, |
| 126 | + FAR void *arg) |
| 127 | +{ |
| 128 | + FAR char *notify_servers = NULL; |
| 129 | + |
| 130 | + if (callback == NULL) |
| 131 | + { |
| 132 | + return -EINVAL; |
| 133 | + } |
| 134 | + |
| 135 | + pthread_mutex_lock(&g_dhcp_ntp_lock); |
| 136 | + |
| 137 | + if (g_dhcp_ntp_callback != NULL && |
| 138 | + (g_dhcp_ntp_callback != callback || g_dhcp_ntp_callback_arg != arg)) |
| 139 | + { |
| 140 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 141 | + return -EBUSY; |
| 142 | + } |
| 143 | + |
| 144 | + if (g_dhcp_ntp_servers != NULL) |
| 145 | + { |
| 146 | + notify_servers = strdup(g_dhcp_ntp_servers); |
| 147 | + if (notify_servers == NULL) |
| 148 | + { |
| 149 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 150 | + return -ENOMEM; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + g_dhcp_ntp_callback = callback; |
| 155 | + g_dhcp_ntp_callback_arg = arg; |
| 156 | + |
| 157 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 158 | + |
| 159 | + callback(notify_servers, arg); |
| 160 | + free(notify_servers); |
| 161 | + return OK; |
| 162 | +} |
| 163 | + |
| 164 | +int netlib_unregister_dhcp_ntp_callback(netlib_dhcp_ntp_callback_t callback, |
| 165 | + FAR void *arg) |
| 166 | +{ |
| 167 | + int ret = -ENOENT; |
| 168 | + |
| 169 | + pthread_mutex_lock(&g_dhcp_ntp_lock); |
| 170 | + |
| 171 | + if (g_dhcp_ntp_callback == callback && g_dhcp_ntp_callback_arg == arg) |
| 172 | + { |
| 173 | + g_dhcp_ntp_callback = NULL; |
| 174 | + g_dhcp_ntp_callback_arg = NULL; |
| 175 | + ret = OK; |
| 176 | + } |
| 177 | + |
| 178 | + pthread_mutex_unlock(&g_dhcp_ntp_lock); |
| 179 | + return ret; |
| 180 | +} |
| 181 | + |
| 182 | +#endif /* CONFIG_NETUTILS_DHCPC */ |
0 commit comments