1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "cawifiinterface.h"
25 #include "caadapterutils.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
30 #define WIFI_MONITOR_TAG "WIFI_MONITOR"
34 * @var g_wifiNetInfoMutex
35 * @brief Mutex for synchronizing access to cached interface and IP address information.
37 static ca_mutex g_wifiNetInfoMutex = NULL;
40 * @var g_wifiInterfaceName
41 * @brief Maintains interface name.
43 static char *g_wifiInterfaceName = NULL;
46 * @var g_wifiIPAddress
47 * @brief Maintains interface IP address.
49 static char *g_wifiIPAddress = NULL;
52 * @var g_wifiSubnetMask
53 * @brief Maintains interface subnetmask.
55 static char *g_wifiSubnetMask = NULL;
58 * @var g_networkChangeCb
59 * @brief Maintains network connection state change callback.
61 static CAWiFiConnectionStateChangeCallback g_networkChangeCb = NULL;
64 * @fn CAWIFIConnectionStateChangedCb
65 * @brief This callback is registered to receive wifi network connection state changes.
67 static void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
71 * @fn CAWIFIDeviceStateChangedCb
72 * @brief This callback is registered to receive wifi device state changes.
74 static void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData);
77 * @fn CAWiFiGetInterfaceInformation
78 * @brief This methods gets local interface name and IP address information.
80 static void CAWiFiGetInterfaceInformation(char **interfaceName, char **ipAddress,
83 CAResult_t CAWiFiInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
85 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
87 // Initialize Wifi service
88 wifi_error_e ret = wifi_initialize();
89 if (WIFI_ERROR_NONE != ret)
91 OIC_LOG(ERROR, WIFI_MONITOR_TAG, "wifi_initialize failed");
92 return CA_STATUS_FAILED;
95 if (!g_wifiNetInfoMutex)
97 g_wifiNetInfoMutex = ca_mutex_new();
100 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
104 void CAWiFiTerminateNetworkMonitor(void)
106 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
108 // Deinitialize Wifi service
109 wifi_error_e ret = wifi_deinitialize();
110 if (WIFI_ERROR_NONE != ret)
112 OIC_LOG(INFO, WIFI_MONITOR_TAG, "wifi_deinitialize failed");
115 if (g_wifiInterfaceName)
117 OICFree(g_wifiInterfaceName);
118 g_wifiInterfaceName = NULL;
123 OICFree(g_wifiIPAddress);
124 g_wifiIPAddress = NULL;
127 if (g_wifiSubnetMask)
129 OICFree(g_wifiSubnetMask);
130 g_wifiSubnetMask = NULL;
133 if (g_wifiNetInfoMutex)
135 ca_mutex_free(g_wifiNetInfoMutex);
136 g_wifiNetInfoMutex = NULL;
139 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
142 CAResult_t CAWiFiStartNetworkMonitor(void)
144 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
146 // Set callback for receiving state changes
147 wifi_error_e ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL);
148 if (WIFI_ERROR_NONE != ret)
150 OIC_LOG(ERROR, WIFI_MONITOR_TAG, "wifi_set_device_state_changed_cb failed");
151 return CA_STATUS_FAILED;
154 // Set callback for receiving connection state changes
155 ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL);
156 if (WIFI_ERROR_NONE != ret)
158 OIC_LOG(ERROR, WIFI_MONITOR_TAG, "wifi_set_connection_state_changed_cb failed");
159 return CA_STATUS_FAILED;
162 CAWiFiGetInterfaceInformation(&g_wifiInterfaceName, &g_wifiIPAddress, &g_wifiSubnetMask);
164 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
168 CAResult_t CAWiFiStopNetworkMonitor(void)
170 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
172 // Reset callback for receiving state changes
173 wifi_error_e ret = wifi_unset_device_state_changed_cb();
174 if (WIFI_ERROR_NONE != ret)
176 OIC_LOG(INFO, WIFI_MONITOR_TAG, "wifi_unset_device_state_changed_cb failed");
179 // Reset callback for receiving connection state changes
180 ret = wifi_unset_connection_state_changed_cb();
181 if (WIFI_ERROR_NONE != ret)
183 OIC_LOG(INFO, WIFI_MONITOR_TAG, "wifi_unset_connection_state_changed_cb failed");
186 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
190 CAResult_t CAWiFiGetInterfaceInfo(char **interfaceName, char **ipAddress)
192 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
194 VERIFY_NON_NULL(interfaceName, WIFI_MONITOR_TAG, "interface name holder is NULL");
195 VERIFY_NON_NULL(ipAddress, WIFI_MONITOR_TAG, "IP address holder is NULL");
197 ca_mutex_lock(g_wifiNetInfoMutex);
199 if (NULL == g_wifiInterfaceName || NULL == g_wifiIPAddress)
201 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "Network not enabled");
202 ca_mutex_unlock(g_wifiNetInfoMutex);
203 return CA_ADAPTER_NOT_ENABLED;
206 if (g_wifiInterfaceName && strlen(g_wifiInterfaceName))
208 *interfaceName = OICStrdup((const char *)g_wifiInterfaceName);
211 if (g_wifiIPAddress && strlen(g_wifiIPAddress))
213 *ipAddress = OICStrdup((const char *)g_wifiIPAddress);
216 ca_mutex_unlock(g_wifiNetInfoMutex);
218 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
222 CAResult_t CAWiFiGetInterfaceSubnetMask(char **subnetMask)
224 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
226 VERIFY_NON_NULL(subnetMask, WIFI_MONITOR_TAG, "subnet mask");
228 ca_mutex_lock(g_wifiNetInfoMutex);
229 if (NULL == g_wifiSubnetMask)
231 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "There is no subnet mask information!");
232 ca_mutex_unlock(g_wifiNetInfoMutex);
233 return CA_STATUS_FAILED;
236 *subnetMask = (g_wifiSubnetMask) ? OICStrdup((const char *)g_wifiSubnetMask): NULL;
237 ca_mutex_unlock(g_wifiNetInfoMutex);
239 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
243 bool CAWiFiIsConnected(void)
245 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
247 wifi_connection_state_e connection_state;
248 wifi_error_e ret = wifi_get_connection_state(&connection_state);
249 if (WIFI_ERROR_NONE != ret)
251 OIC_LOG(ERROR, WIFI_MONITOR_TAG, "Failed to get the Connection State");
255 if (WIFI_CONNECTION_STATE_DISCONNECTED == connection_state)
257 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "WIFI is not Connected");
261 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
265 void CAWiFiSetConnectionStateChangeCallback(
266 CAWiFiConnectionStateChangeCallback callback)
268 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
270 g_networkChangeCb = callback;
273 void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
276 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
278 if (WIFI_CONNECTION_STATE_ASSOCIATION == state
279 || WIFI_CONNECTION_STATE_CONFIGURATION == state)
281 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "Connection is in Association State");
285 CANetworkStatus_t nwStatus = CA_INTERFACE_DOWN;
287 // If Wifi is connected, then get the latest IP from the WIFI Interface
288 if (WIFI_CONNECTION_STATE_CONNECTED == state)
290 nwStatus = CA_INTERFACE_UP;
292 // Get network information
293 char *interfaceName = NULL;
294 char *ipAddress = NULL;
295 char *subnetMask = NULL;
296 CAWiFiGetInterfaceInformation(&interfaceName, &ipAddress, &subnetMask);
298 // Update the cached network information
299 ca_mutex_lock(g_wifiNetInfoMutex);
301 OICFree(g_wifiInterfaceName);
302 OICFree(g_wifiIPAddress);
303 OICFree(g_wifiSubnetMask);
304 g_wifiInterfaceName = interfaceName;
305 g_wifiIPAddress = ipAddress;
306 g_wifiSubnetMask = subnetMask;
308 ca_mutex_unlock(g_wifiNetInfoMutex);
312 nwStatus = CA_INTERFACE_DOWN;
315 if (g_networkChangeCb)
317 g_networkChangeCb(g_wifiIPAddress, nwStatus);
320 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
324 void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData)
326 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
328 if (WIFI_DEVICE_STATE_ACTIVATED == state)
330 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "Wifi is in Activated State");
334 CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL);
335 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "Wifi is in Deactivated State");
338 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");
342 void CAWiFiGetInterfaceInformation(char **interfaceName, char **ipAddress, char **subnetMask)
344 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "IN");
346 int ret = WIFI_ERROR_NONE;
348 if (!interfaceName || !ipAddress || !subnetMask)
350 OIC_LOG(ERROR, WIFI_MONITOR_TAG, "Invalid input: interface/ipaddress holder is NULL!");
354 ca_mutex_lock(g_wifiNetInfoMutex);
355 // Get wifi interface name
356 if (WIFI_ERROR_NONE != (ret = wifi_get_network_interface_name(interfaceName)))
358 OIC_LOG_V(ERROR, WIFI_MONITOR_TAG, "Failed to get interface name! error num [%d]", ret);
360 ca_mutex_unlock(g_wifiNetInfoMutex);
364 // Get wifi connected IP address
365 wifi_ap_h accessPoint = NULL;
366 if (WIFI_ERROR_NONE != (ret = wifi_get_connected_ap(&accessPoint)))
368 OIC_LOG_V(ERROR, WIFI_MONITOR_TAG, "Failed to get access point! error num [%d]",
371 OICFree(*interfaceName);
372 *interfaceName = NULL;
373 ca_mutex_unlock(g_wifiNetInfoMutex);
377 if (WIFI_ERROR_NONE != (ret = wifi_ap_get_ip_address(accessPoint, WIFI_ADDRESS_FAMILY_IPV4,
380 OIC_LOG_V(ERROR, WIFI_MONITOR_TAG, "Failed to get interface address! error num [%d]",
382 OICFree(*interfaceName);
383 *interfaceName = NULL;
384 ca_mutex_unlock(g_wifiNetInfoMutex);
388 if (WIFI_ERROR_NONE != (ret = wifi_ap_get_subnet_mask(accessPoint, WIFI_ADDRESS_FAMILY_IPV4,
391 OIC_LOG_V(ERROR, WIFI_MONITOR_TAG, "Failed to get interface address! error num [%d]",
394 OICFree(*interfaceName);
396 *interfaceName = NULL;
397 ca_mutex_unlock(g_wifiNetInfoMutex);
401 ca_mutex_unlock(g_wifiNetInfoMutex);
403 OIC_LOG(DEBUG, WIFI_MONITOR_TAG, "OUT");