Replace/examine usages of mem* and strcat/strcpy
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / tizen / caipnwmonitor.c
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include "caipinterface.h"
22
23 #include <wifi.h>
24
25 #include "caadapterutils.h"
26 #include "camutex.h"
27 #include "logger.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
30
31 #define IP_MONITOR_TAG "IP_MONITOR"
32
33 /**
34  * @var g_networkMonitorContextMutex
35  * @brief  Mutex for synchronizing access to cached interface and IP address information.
36  */
37 static ca_mutex g_networkMonitorContextMutex = NULL;
38
39 /**
40  * @struct CAIPNwMonitorContext
41  * @brief  Used for storing network monitor context information.
42  */
43 typedef struct
44 {
45     u_arraylist_t  *netInterfaceList;
46     ca_thread_pool_t threadPool;
47     CANetworkStatus_t nwConnectivityStatus;
48     CAIPConnectionStateChangeCallback networkChangeCb;
49 } CAIPNetworkMonitorContext;
50
51 /**
52  * @var g_networkMonitorContext
53  * @brief  network monitor context.
54  */
55 static CAIPNetworkMonitorContext *g_networkMonitorContext = NULL;
56
57 static void CARemoveInterfaceInfo()
58 {
59     ca_mutex_lock(g_networkMonitorContextMutex);
60     if (!g_networkMonitorContext->netInterfaceList)
61     {
62         OIC_LOG(ERROR, IP_MONITOR_TAG,
63                 "netInterfaceList is empty");
64         ca_mutex_unlock(g_networkMonitorContextMutex);
65         return;
66     }
67
68     uint32_t list_index = 0;
69     uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
70     for (list_index = 0; list_index < list_length; list_index++)
71     {
72         CANetInfo_t *netInfo = (CANetInfo_t *)u_arraylist_get(
73                                g_networkMonitorContext->netInterfaceList, list_index);
74         if (!netInfo)
75         {
76             continue;
77         }
78
79         if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, list_index))
80         {
81             if (g_networkMonitorContext->networkChangeCb)
82             {
83                 g_networkMonitorContext->networkChangeCb(netInfo->ipAddress,
84                                                          CA_INTERFACE_DOWN);
85             }
86         }
87         else
88         {
89             OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
90         }
91
92         OICFree(netInfo);
93     }
94
95     u_arraylist_free(&g_networkMonitorContext->netInterfaceList);
96
97     ca_mutex_unlock(g_networkMonitorContextMutex);
98 }
99
100 static bool CACheckIsInterfaceInfoChanged(const CANetInfo_t *info)
101 {
102     VERIFY_NON_NULL_RET(info, IP_MONITOR_TAG, "info is null", false);
103
104     ca_mutex_lock(g_networkMonitorContextMutex);
105     uint32_t list_index = 0;
106     uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
107     for (list_index = 0; list_index < list_length; list_index++)
108     {
109         CANetInfo_t *netInfo = (CANetInfo_t *)u_arraylist_get(
110                                g_networkMonitorContext->netInterfaceList, list_index);
111         if (!netInfo)
112         {
113             continue;
114         }
115
116         if (strncmp(netInfo->interfaceName, info->interfaceName, strlen(info->interfaceName)) == 0)
117         {
118             if (strncmp(netInfo->ipAddress, info->ipAddress, strlen(info->ipAddress)) == 0)
119             {
120                 ca_mutex_unlock(g_networkMonitorContextMutex);
121                 return false;
122             }
123             else
124             {
125                 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network interface info changed");
126                 if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, list_index))
127                 {
128                     if (g_networkMonitorContext->networkChangeCb)
129                     {
130                         g_networkMonitorContext->networkChangeCb(netInfo->ipAddress,
131                                                                  CA_INTERFACE_DOWN);
132                     }
133                     OICFree(netInfo);
134                 }
135                 else
136                 {
137                     OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
138                 }
139                 break;
140             }
141         }
142     }
143
144     CANetInfo_t *newNetInfo = (CANetInfo_t *)OICMalloc(sizeof(CANetInfo_t));
145     if (!newNetInfo)
146     {
147         OIC_LOG(ERROR, IP_MONITOR_TAG, "newNetInfo malloc failed");
148         ca_mutex_unlock(g_networkMonitorContextMutex);
149         return false;
150     }
151     *newNetInfo = *info;
152
153     OIC_LOG(DEBUG, IP_MONITOR_TAG, "New Interface found");
154
155     CAResult_t result = u_arraylist_add(g_networkMonitorContext->netInterfaceList,
156                                         (void *)newNetInfo);
157     if (CA_STATUS_OK != result)
158     {
159         OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
160         OICFree(newNetInfo);
161         ca_mutex_unlock(g_networkMonitorContextMutex);
162         return false;
163     }
164     ca_mutex_unlock(g_networkMonitorContextMutex);
165
166     /*Callback will be unset only at the time of termination. By that time, all the threads will be
167       stopped gracefully. This callback is properly protected*/
168     if (g_networkMonitorContext->networkChangeCb)
169     {
170         g_networkMonitorContext->networkChangeCb(newNetInfo->ipAddress, CA_INTERFACE_UP);
171     }
172
173     return true;
174 }
175
176 void CAWiFiGetInterfaceInformation(char **interfaceName, char **ipAddress, char **subnetMask)
177 {
178     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
179
180     int ret = WIFI_ERROR_NONE;
181
182     if (!interfaceName || !ipAddress || !subnetMask)
183     {
184         OIC_LOG(ERROR, IP_MONITOR_TAG, "Invalid input: interface/ipaddress holder is NULL!");
185         return;
186     }
187
188     // Get wifi interface name
189     if (WIFI_ERROR_NONE != (ret = wifi_get_network_interface_name(interfaceName)))
190     {
191         OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get interface name! error num [%d]", ret);
192         return;
193     }
194
195     // Get wifi connected IP address
196     wifi_ap_h accessPoint = NULL;
197     if (WIFI_ERROR_NONE != (ret = wifi_get_connected_ap(&accessPoint)))
198     {
199         OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get access point! error num [%d]",
200                   ret);
201
202         OICFree(*interfaceName);
203         *interfaceName = NULL;
204         return;
205     }
206
207     if (WIFI_ERROR_NONE != (ret = wifi_ap_get_ip_address(accessPoint, WIFI_ADDRESS_FAMILY_IPV4,
208                                   ipAddress)))
209     {
210         OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get interface address! error num [%d]",
211                   ret);
212         OICFree(*interfaceName);
213         *interfaceName = NULL;
214         return;
215     }
216
217     if (WIFI_ERROR_NONE != (ret = wifi_ap_get_subnet_mask(accessPoint, WIFI_ADDRESS_FAMILY_IPV4,
218                                   subnetMask)))
219     {
220         OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get interface address! error num [%d]",
221                   ret);
222         OICFree(*ipAddress);
223         OICFree(*interfaceName);
224         *ipAddress = NULL;
225         *interfaceName = NULL;
226         return;
227     }
228
229     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
230 }
231
232 static void CAIPGetInterfaceInformation(u_arraylist_t **netInterfaceList)
233 {
234     VERIFY_NON_NULL_VOID(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null");
235
236     // Get wifi network information
237     char *interfaceName = NULL;
238     char *ipAddress = NULL;
239     char *subnetMask = NULL;
240     ///TODO: currently we are filling single interface. Once found the proper tizen apis for
241     // getting multiple interfaces, then this function will be updated.
242     CAWiFiGetInterfaceInformation(&interfaceName, &ipAddress, &subnetMask);
243
244     if (!interfaceName || !ipAddress || !subnetMask)
245     {
246         OIC_LOG(ERROR, IP_MONITOR_TAG, "interface/ipaddress/subnetmask is NULL!");
247         return;
248     }
249
250     CANetInfo_t *netInfo = (CANetInfo_t *)OICCalloc(1, sizeof(CANetInfo_t));
251     if (!netInfo)
252     {
253         OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed");
254         OICFree(interfaceName);
255         OICFree(ipAddress);
256         OICFree(subnetMask);
257         return;
258     }
259
260     // set interface name
261     OICStrcpy(netInfo->interfaceName, sizeof(netInfo->interfaceName), interfaceName);
262
263     // set local ip address
264     OICStrcpy(netInfo->ipAddress, sizeof(netInfo->ipAddress), ipAddress);
265
266     // set subnet mask
267     OICStrcpy(netInfo->subnetMask, sizeof(netInfo->subnetMask), subnetMask);
268
269     CAResult_t result = u_arraylist_add(*netInterfaceList, (void *)netInfo);
270     if (CA_STATUS_OK != result)
271     {
272         OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
273     }
274     OICFree(interfaceName);
275     OICFree(ipAddress);
276     OICFree(subnetMask);
277 }
278
279
280 void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
281                                     void *userData)
282 {
283     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
284
285     if (WIFI_CONNECTION_STATE_ASSOCIATION == state
286         || WIFI_CONNECTION_STATE_CONFIGURATION == state)
287     {
288         OIC_LOG(DEBUG, IP_MONITOR_TAG, "Connection is in Association State");
289         return;
290     }
291
292     // If Wifi is connected, then get the latest IP from the WIFI Interface
293     if (WIFI_CONNECTION_STATE_CONNECTED == state)
294     {
295         // Get network information
296         char *interfaceName = NULL;
297         char *ipAddress = NULL;
298         char *subnetMask = NULL;
299         CAWiFiGetInterfaceInformation(&interfaceName, &ipAddress, &subnetMask);
300
301         CANetInfo_t *netInfo = (CANetInfo_t *)OICCalloc(1, sizeof(CANetInfo_t));
302         if (!netInfo)
303         {
304             OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed");
305             OICFree(interfaceName);
306             OICFree(ipAddress);
307             OICFree(subnetMask);
308             return;
309         }
310
311         // set interface name
312         OICStrcpy(netInfo->interfaceName, sizeof(netInfo->interfaceName), interfaceName);
313
314         // set local ip address
315         OICStrcpy(netInfo->ipAddress, sizeof(netInfo->ipAddress), ipAddress);
316
317         // set subnet mask
318         OICStrcpy(netInfo->subnetMask, sizeof(netInfo->subnetMask), subnetMask);
319
320         bool ret = CACheckIsInterfaceInfoChanged(netInfo);
321         if (ret)
322         {
323             OIC_LOG(DEBUG, IP_MONITOR_TAG, "CACheckIsInterfaceInfoChanged true");
324         }
325
326         OICFree(interfaceName);
327         OICFree(ipAddress);
328         OICFree(subnetMask);
329     }
330     else
331     {
332         CARemoveInterfaceInfo();
333     }
334
335     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
336 }
337
338 void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData)
339 {
340     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
341
342     if (WIFI_DEVICE_STATE_ACTIVATED == state)
343     {
344         OIC_LOG(DEBUG, IP_MONITOR_TAG, "Wifi is in Activated State");
345     }
346     else
347     {
348         CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL);
349         OIC_LOG(DEBUG, IP_MONITOR_TAG, "Wifi is in Deactivated State");
350     }
351
352     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
353 }
354
355 static CAResult_t CAInitializeNetworkMonitorMutexes()
356 {
357     if (!g_networkMonitorContextMutex)
358     {
359         g_networkMonitorContextMutex = ca_mutex_new();
360         if (!g_networkMonitorContextMutex)
361         {
362             OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContextMutex Malloc  failed");
363             return CA_MEMORY_ALLOC_FAILED;
364         }
365     }
366
367     return CA_STATUS_OK;
368 }
369
370 static void CADestroyNetworkMonitorMutexes()
371 {
372     ca_mutex_free(g_networkMonitorContextMutex);
373     g_networkMonitorContextMutex = NULL;
374 }
375
376 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
377 {
378     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
379
380     VERIFY_NON_NULL(threadPool, IP_MONITOR_TAG, "threadPool is null");
381
382     CAResult_t ret = CAInitializeNetworkMonitorMutexes();
383
384     if (CA_STATUS_OK != ret)
385     {
386         OIC_LOG(ERROR, IP_MONITOR_TAG, "CAInitializeNetworkMonitorMutexes failed");
387         return CA_STATUS_FAILED;
388     }
389     ca_mutex_lock(g_networkMonitorContextMutex);
390
391      // Initialize Wifi service
392     wifi_error_e retValue = wifi_initialize();
393     if (WIFI_ERROR_NONE != retValue)
394     {
395         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_initialize failed");
396         return CA_STATUS_FAILED;
397     }
398
399     g_networkMonitorContext = (CAIPNetworkMonitorContext *)OICCalloc(1,
400                               sizeof(*g_networkMonitorContext));
401     if (!g_networkMonitorContext)
402     {
403         OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext Malloc  failed");
404         ca_mutex_unlock(g_networkMonitorContextMutex);
405         CADestroyNetworkMonitorMutexes();
406         return CA_MEMORY_ALLOC_FAILED;
407     }
408
409     g_networkMonitorContext->netInterfaceList = u_arraylist_create();
410     if (!g_networkMonitorContext->netInterfaceList)
411     {
412         OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_create failed");
413         OICFree(g_networkMonitorContext);
414         ca_mutex_unlock(g_networkMonitorContextMutex);
415         CADestroyNetworkMonitorMutexes();
416         return CA_MEMORY_ALLOC_FAILED;
417     }
418
419     CAIPGetInterfaceInformation(&g_networkMonitorContext->netInterfaceList);
420
421     ca_mutex_unlock(g_networkMonitorContextMutex);
422
423     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
424     return CA_STATUS_OK;
425 }
426
427 void CAIPTerminateNetworkMonitor()
428 {
429     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
430
431     ca_mutex_lock(g_networkMonitorContextMutex);
432
433      // Deinitialize Wifi service
434     wifi_error_e ret = wifi_deinitialize();
435     if (WIFI_ERROR_NONE != ret)
436     {
437         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_deinitialize failed");
438     }
439
440     CAClearNetInterfaceInfoList(g_networkMonitorContext->netInterfaceList);
441
442     g_networkMonitorContext->netInterfaceList = NULL;
443     g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
444     g_networkMonitorContext->networkChangeCb = NULL;
445
446     OICFree(g_networkMonitorContext);
447     g_networkMonitorContext = NULL;
448
449     ca_mutex_unlock(g_networkMonitorContextMutex);
450
451     CADestroyNetworkMonitorMutexes();
452
453     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
454 }
455
456 CAResult_t CAIPStartNetworkMonitor()
457 {
458     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
459
460      // Set callback for receiving state changes
461     wifi_error_e ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL);
462     if (WIFI_ERROR_NONE != ret)
463     {
464         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_set_device_state_changed_cb failed");
465         return CA_STATUS_FAILED;
466     }
467
468     // Set callback for receiving connection state changes
469     ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL);
470     if (WIFI_ERROR_NONE != ret)
471     {
472         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_set_connection_state_changed_cb failed");
473         return CA_STATUS_FAILED;
474     }
475
476     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
477     return CA_STATUS_OK;
478 }
479
480 CAResult_t CAIPStopNetworkMonitor()
481 {
482     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
483
484      // Reset callback for receiving state changes
485     wifi_error_e ret = wifi_unset_device_state_changed_cb();
486     if (WIFI_ERROR_NONE != ret)
487     {
488         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_unset_device_state_changed_cb failed");
489     }
490
491     // Reset callback for receiving connection state changes
492     ret = wifi_unset_connection_state_changed_cb();
493     if (WIFI_ERROR_NONE != ret)
494     {
495         OIC_LOG(ERROR, IP_MONITOR_TAG, "wifi_unset_connection_state_changed_cb failed");
496     }
497
498     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
499     return CA_STATUS_OK;
500 }
501
502 CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList)
503 {
504     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
505
506     VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "u_array_list is null");
507     VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG,
508                     "g_networkMonitorContext is null");
509     VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
510                     "g_networkMonitorContextMutex is null");
511
512     // Get the interface and ipaddress information from cache
513     ca_mutex_lock(g_networkMonitorContextMutex);
514     if (!g_networkMonitorContext->netInterfaceList
515         || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
516     {
517         OIC_LOG(ERROR, IP_MONITOR_TAG, "Network not enabled");
518         ca_mutex_unlock(g_networkMonitorContextMutex);
519         return CA_ADAPTER_NOT_ENABLED;
520     }
521
522     uint32_t list_index = 0;
523     uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
524     OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo list length [%d]",
525               list_length);
526     for (list_index = 0; list_index < list_length; list_index++)
527     {
528         CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(
529                             g_networkMonitorContext->netInterfaceList, list_index);
530         if (!info)
531         {
532             continue;
533         }
534         OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo ip [%s]",
535                   info->ipAddress);
536         CANetInfo_t *newNetinfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
537         if (!newNetinfo)
538         {
539             OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed!");
540             ca_mutex_unlock(g_networkMonitorContextMutex);
541             return CA_MEMORY_ALLOC_FAILED;
542         }
543
544         *newNetinfo = *info;
545
546         CAResult_t result = u_arraylist_add(*netInterfaceList, (void *)newNetinfo);
547         if (CA_STATUS_OK != result)
548         {
549             OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
550             ca_mutex_unlock(g_networkMonitorContextMutex);
551             return CA_STATUS_FAILED;
552         }
553     }
554
555     ca_mutex_unlock(g_networkMonitorContextMutex);
556
557     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
558     return CA_STATUS_OK;
559 }
560
561 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask)
562 {
563     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
564
565     VERIFY_NON_NULL(subnetMask, IP_MONITOR_TAG, "subnet mask");
566     VERIFY_NON_NULL(ipAddress, IP_MONITOR_TAG, "ipAddress is null");
567     VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG,
568                     "g_networkMonitorContext is null");
569     VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
570                     "g_networkMonitorContextMutex is null");
571
572     // Get the interface and ipaddress information from cache
573     ca_mutex_lock(g_networkMonitorContextMutex);
574     if (!g_networkMonitorContext->netInterfaceList
575         || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
576     {
577         OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network not enabled");
578         ca_mutex_unlock(g_networkMonitorContextMutex);
579         return CA_ADAPTER_NOT_ENABLED;
580     }
581
582     uint32_t list_index = 0;
583     uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
584     OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "list lenght [%d]", list_length);
585     for (list_index = 0; list_index < list_length; list_index++)
586     {
587         CANetInfo_t *info = (CANetInfo_t *)u_arraylist_get(
588                             g_networkMonitorContext->netInterfaceList, list_index);
589         if (!info)
590         {
591             continue;
592         }
593
594         if (strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0)
595         {
596             OIC_LOG_V(DEBUG, IP_MONITOR_TAG,
597                       "CAIPGetInterfaceSubnetMask subnetmask is %s", info->subnetMask);
598             *subnetMask = OICStrdup(info->subnetMask);
599             break;
600         }
601     }
602     ca_mutex_unlock(g_networkMonitorContextMutex);
603
604     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
605     return CA_STATUS_OK;
606 }
607
608 bool CAIPIsConnected()
609 {
610     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
611
612     wifi_connection_state_e connection_state;
613     wifi_error_e ret = wifi_get_connection_state(&connection_state);
614     if (WIFI_ERROR_NONE != ret)
615     {
616         OIC_LOG(ERROR, IP_MONITOR_TAG, "Failed to get the Connection State");
617         return false;
618     }
619
620     if (WIFI_CONNECTION_STATE_DISCONNECTED == connection_state)
621     {
622         OIC_LOG(DEBUG, IP_MONITOR_TAG, "WIFI is not Connected");
623         return false;
624     }
625
626     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
627     return true;
628 }
629
630 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
631 {
632     OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
633     if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
634     {
635         OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPSetConnectionStateChangeCallback failed");
636         return;
637     }
638     ca_mutex_lock(g_networkMonitorContextMutex);
639
640     g_networkMonitorContext->networkChangeCb = callback;
641
642     ca_mutex_unlock(g_networkMonitorContextMutex);
643
644     OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
645 }