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 "caipinterface.h"
24 #include <sys/ioctl.h>
25 #include <arpa/inet.h>
31 #include "caadapterutils.h"
34 #include "oic_malloc.h"
35 #include "oic_string.h"
36 #include "org_iotivity_ca_CaIpInterface.h"
38 #define IP_MONITOR_TAG "IP_MONITOR"
39 #define MAX_INTERFACE_INFO_LENGTH (1024)
42 * @var g_stopNetworkMonitor
43 * @brief Used to stop the network monitor thread.
45 static bool g_stopNetworkMonitor = false;
48 * @var g_stopNetworkMonitorMutex
49 * @brief Mutex for synchronizing access to g_stopNetworkMonitor flag.
51 static ca_mutex g_stopNetworkMonitorMutex = NULL;
54 * @struct CAIPNwMonitorContext
55 * @brief Used for storing network monitor context information.
59 u_arraylist_t *netInterfaceList;
60 ca_thread_pool_t threadPool;
61 CANetworkStatus_t nwConnectivityStatus;
62 CAIPConnectionStateChangeCallback networkChangeCb;
63 } CAIPNetworkMonitorContext;
66 * @var g_networkMonitorContext
67 * @brief network monitor context.
69 static CAIPNetworkMonitorContext *g_networkMonitorContext = NULL;
72 * @var g_networkMonitorContextMutex
73 * @brief Mutex for synchronizing access to cached interface and IP address information.
75 static ca_mutex g_networkMonitorContextMutex = NULL;
79 * @brief pointer to store JavaVM
81 static JavaVM *g_jvm = NULL;
85 * @brief pointer to store Application Context
87 static jobject g_context = NULL;
90 * @fn CAIPUpdateInterfaceInformation
91 * @brief This methods gets local interface name and IP address information.
93 static CAResult_t CAIPUpdateInterfaceInformation(u_arraylist_t **netInterfaceList);
95 * @fn CACreateIPJNIInterfaceObject
96 * @brief creates new instance of caipinterface through JNI
98 static CAResult_t CACreateIPJNIInterfaceObject(jobject context);
101 * @fn CAIPSendNetworkChangeCallback
102 * @brief updates network status to IP adapter
104 static void CAIPSendNetworkChangeCallback(CANetworkStatus_t currNetworkStatus);
106 CAResult_t CAIPJniInit()
108 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
110 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPJniInit");
111 g_jvm = CANativeJNIGetJavaVM();
115 OIC_LOG(ERROR, IP_MONITOR_TAG, "JNI initialize error");
116 return CA_STATUS_FAILED;
119 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
123 CAResult_t CAIPJniSetContext()
125 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPJniSetContext");
126 g_context = (jobject) CANativeJNIGetContext();
130 OIC_LOG(ERROR, IP_MONITOR_TAG, "unable to get application context");
131 return CA_STATUS_FAILED;
137 CAResult_t CACreateIPJNIInterfaceObject(jobject context)
139 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
141 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CACreateIPJNIInterfaceObject");
143 VERIFY_NON_NULL(context, IP_MONITOR_TAG, "context");
147 if ((*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
149 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get JNIEnv pointer");
150 return CA_STATUS_FAILED;
153 //getApplicationContext
154 jclass contextClass = (*env)->FindClass(env, "android/content/Context");
157 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get context object class");
158 return CA_STATUS_FAILED;
161 jmethodID getApplicationContextMethod = (*env)->GetMethodID(env, contextClass,
162 "getApplicationContext",
163 "()Landroid/content/Context;");
164 if (!getApplicationContextMethod)
166 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get getApplicationContext method");
167 return CA_STATUS_FAILED;
170 jobject gApplicationContext = (*env)->CallObjectMethod(env, context,
171 getApplicationContextMethod);
172 if (!getApplicationContextMethod)
174 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get getApplicationContext");
175 return CA_STATUS_FAILED;
178 //Create caipinterface jni instance
179 jclass IPJniInterface = (*env)->FindClass(env, "org/iotivity/ca/CaIpInterface");
182 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get caipinterface class");
183 return CA_STATUS_FAILED;
186 jmethodID IPInterfaceConstructorMethod = (*env)->GetMethodID(env, IPJniInterface, "<init>",
187 "(Landroid/content/Context;)V");
188 if (!IPInterfaceConstructorMethod)
190 OIC_LOG(ERROR, IP_MONITOR_TAG, "Could not get caipinterface constructor method");
191 return CA_STATUS_FAILED;
194 (*env)->NewObject(env, IPJniInterface, IPInterfaceConstructorMethod, gApplicationContext);
195 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Create caipinterface instance, success");
197 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
201 static CAResult_t CAIPUpdateInterfaceInformation(u_arraylist_t **netInterfaceList)
203 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
205 VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null");
207 /* Get a socket handle. */
210 sck = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
215 sck=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
220 OIC_LOG(ERROR, IP_MONITOR_TAG, "Error in socket creation");
221 return CA_STATUS_FAILED;
224 char buf[MAX_INTERFACE_INFO_LENGTH] = { 0 };
227 /* Query available interfaces. */
228 ifc.ifc_len = MAX_INTERFACE_INFO_LENGTH;
231 if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
234 OIC_LOG(ERROR, IP_MONITOR_TAG, "Failed to get interface info");
235 return CA_STATUS_FAILED;
238 /* Iterate through the list of interfaces. */
239 struct ifreq* ifr = ifc.ifc_req;
240 int32_t interfaces = ifc.ifc_len / sizeof(struct ifreq);
242 OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPUpdateInterfaceInformation : %d", interfaces);
244 for (int32_t i = 0; i < interfaces; i++)
246 struct ifreq temp_ifr = { 0 };
247 struct ifreq* item = &ifr[i];
249 char interfaceAddress[CA_IPADDR_SIZE] = { 0 };
250 char interfaceSubnetMask[CA_IPADDR_SIZE] = { 0 };
251 socklen_t len = sizeof(struct sockaddr_in);
253 strcpy(temp_ifr.ifr_name, item->ifr_name);
255 if (ioctl(sck, SIOCGIFFLAGS, &temp_ifr))
257 OIC_LOG(ERROR, IP_MONITOR_TAG,
258 "CAIPUpdateInterfaceInformation, SIOCGIFFLAGS Failed");
260 return CA_STATUS_FAILED;
263 if ((temp_ifr.ifr_flags & IFF_LOOPBACK)
264 || !(temp_ifr.ifr_flags & IFF_UP) || !(temp_ifr.ifr_flags & IFF_RUNNING))
269 if (AF_INET != ((struct sockaddr_in*) &item->ifr_addr)->sin_family)
274 //get the interface ip address
275 if (0 != getnameinfo(&item->ifr_addr, len, interfaceAddress, sizeof(interfaceAddress),
276 NULL, 0, NI_NUMERICHOST))
278 OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get IPAddress, Error code: %s",
281 return CA_STATUS_FAILED;
284 if (ioctl((int) sck, SIOCGIFNETMASK, item) < 0)
286 OIC_LOG(ERROR, IP_MONITOR_TAG,
287 "CAIPUpdateInterfaceInformation, SIOCGIFNETMASK Failed");
289 return CA_STATUS_FAILED;
292 // get the interface subnet mask
293 if (0 != getnameinfo(&item->ifr_netmask, len, interfaceSubnetMask,
294 sizeof(interfaceSubnetMask), NULL, 0, NI_NUMERICHOST))
296 OIC_LOG_V(ERROR, IP_MONITOR_TAG, "Failed to get subnet mask, Error code: %s",
299 return CA_STATUS_FAILED;
302 CANetInfo_t *netInfo = (CANetInfo_t *) OICCalloc(1, sizeof(CANetInfo_t));
305 OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed");
307 return CA_MEMORY_ALLOC_FAILED;
310 // set interface name
311 strncpy(netInfo->interfaceName, item->ifr_name, strlen(item->ifr_name));
313 // set local ip address
314 strncpy(netInfo->ipAddress, interfaceAddress, strlen(interfaceAddress));
317 strncpy(netInfo->subnetMask, interfaceSubnetMask, strlen(interfaceSubnetMask));
319 CAResult_t result = u_arraylist_add(*netInterfaceList, (void *) netInfo);
320 if (CA_STATUS_OK != result)
322 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!Thread exiting.");
324 return CA_STATUS_FAILED;
327 OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "ipAddress : %s, interfaceName : %s, subnetmask : %s",
328 netInfo->ipAddress, netInfo->interfaceName, netInfo->subnetMask);
336 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
337 return CA_STATUS_FAILED;
340 static bool CACheckIsAnyInterfaceDown(const u_arraylist_t *netInterfaceList,
341 const CANetInfo_t *info)
343 VERIFY_NON_NULL_RET(netInterfaceList, IP_MONITOR_TAG, "netInterfaceList is null", false);
344 VERIFY_NON_NULL_RET(info, IP_MONITOR_TAG, "info is null", false);
346 uint32_t list_length = u_arraylist_length(netInterfaceList);
347 for (uint32_t list_index = 0; list_index < list_length; list_index++)
349 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(netInterfaceList, list_index);
354 if (strncmp(netInfo->interfaceName, info->interfaceName, strlen(info->interfaceName)) == 0)
359 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Interface is down");
363 static bool CACheckIsInterfaceInfoChanged(const CANetInfo_t *info)
365 VERIFY_NON_NULL_RET(info, IP_MONITOR_TAG, "info is null", false);
367 ca_mutex_lock(g_networkMonitorContextMutex);
369 uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
370 for (uint32_t list_index = 0; list_index < list_length; list_index++)
372 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(
373 g_networkMonitorContext->netInterfaceList, list_index);
378 if (strncmp(netInfo->interfaceName, info->interfaceName, strlen(info->interfaceName)) == 0)
380 if (strncmp(netInfo->ipAddress, info->ipAddress, strlen(info->ipAddress)) == 0)
382 ca_mutex_unlock(g_networkMonitorContextMutex);
387 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network interface info changed");
388 if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, list_index))
390 if (g_networkMonitorContext->networkChangeCb)
392 g_networkMonitorContext->networkChangeCb(netInfo->ipAddress,
399 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
406 CANetInfo_t *newNetInfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
409 OIC_LOG(ERROR, IP_MONITOR_TAG, "newNetInfo malloc failed");
410 ca_mutex_unlock(g_networkMonitorContextMutex);
413 memcpy(newNetInfo, info, sizeof(*newNetInfo));
415 OIC_LOG(DEBUG, IP_MONITOR_TAG, "New Interface found");
417 CAResult_t result = u_arraylist_add(g_networkMonitorContext->netInterfaceList,
418 (void *) newNetInfo);
419 if (CA_STATUS_OK != result)
421 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
423 ca_mutex_unlock(g_networkMonitorContextMutex);
426 ca_mutex_unlock(g_networkMonitorContextMutex);
428 /*Callback will be unset only at the time of termination. By that time, all the threads will be
429 stopped gracefully. This callback is properly protected*/
430 if (g_networkMonitorContext->networkChangeCb)
432 g_networkMonitorContext->networkChangeCb(newNetInfo->ipAddress, CA_INTERFACE_UP);
438 static CAResult_t CAInitializeNetworkMonitorMutexes()
440 if (!g_networkMonitorContextMutex)
442 g_networkMonitorContextMutex = ca_mutex_new();
443 if (!g_networkMonitorContextMutex)
445 OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContextMutex Malloc failed");
446 return CA_MEMORY_ALLOC_FAILED;
450 if (!g_stopNetworkMonitorMutex)
452 g_stopNetworkMonitorMutex = ca_mutex_new();
453 if (!g_stopNetworkMonitorMutex)
455 OIC_LOG(ERROR, IP_MONITOR_TAG, "g_stopNetworkMonitorMutex Malloc failed");
456 ca_mutex_free(g_networkMonitorContextMutex);
457 return CA_MEMORY_ALLOC_FAILED;
463 static void CADestroyNetworkMonitorMutexes()
465 ca_mutex_free(g_networkMonitorContextMutex);
466 g_networkMonitorContextMutex = NULL;
468 ca_mutex_free(g_stopNetworkMonitorMutex);
469 g_stopNetworkMonitorMutex = NULL;
472 CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
474 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPInitializeNetworkMonitor IN");
476 VERIFY_NON_NULL(threadPool, IP_MONITOR_TAG, "threadPool is null");
478 CAResult_t ret = CAIPJniInit();
479 if (CA_STATUS_OK != ret)
481 OIC_LOG(ERROR, IP_MONITOR_TAG, "Initialization failed");
485 ret = CAIPJniSetContext();
486 if (CA_STATUS_OK != ret)
488 OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPJniSetContext failed");
492 ret = CACreateIPJNIInterfaceObject(g_context);
493 if (CA_STATUS_OK != ret)
495 OIC_LOG(ERROR, IP_MONITOR_TAG, "unable to create caipinterface instance");
499 ret = CAInitializeNetworkMonitorMutexes();
501 if (CA_STATUS_OK != ret)
503 OIC_LOG(ERROR, IP_MONITOR_TAG, "CAInitializeNetworkMonitorMutexes failed");
504 return CA_STATUS_FAILED;
507 ca_mutex_lock(g_networkMonitorContextMutex);
509 g_networkMonitorContext = (CAIPNetworkMonitorContext *) OICCalloc(
510 1, sizeof(*g_networkMonitorContext));
511 if (!g_networkMonitorContext)
513 OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext Malloc failed");
514 ca_mutex_unlock(g_networkMonitorContextMutex);
515 CADestroyNetworkMonitorMutexes();
516 return CA_MEMORY_ALLOC_FAILED;
518 g_networkMonitorContext->threadPool = threadPool;
520 g_networkMonitorContext->netInterfaceList = u_arraylist_create();
521 if (!g_networkMonitorContext->netInterfaceList)
523 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_create failed");
524 OICFree(g_networkMonitorContext);
525 ca_mutex_unlock(g_networkMonitorContextMutex);
526 CADestroyNetworkMonitorMutexes();
527 return CA_MEMORY_ALLOC_FAILED;
530 CAIPUpdateInterfaceInformation(&g_networkMonitorContext->netInterfaceList);
532 if (u_arraylist_length(g_networkMonitorContext->netInterfaceList))
534 g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_UP;
538 g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
541 ca_mutex_unlock(g_networkMonitorContextMutex);
543 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
547 void CAIPTerminateNetworkMonitor()
549 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
551 ca_mutex_lock(g_networkMonitorContextMutex);
552 g_networkMonitorContext->threadPool = NULL;
554 CAClearNetInterfaceInfoList(g_networkMonitorContext->netInterfaceList);
556 g_networkMonitorContext->netInterfaceList = NULL;
557 g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
558 g_networkMonitorContext->networkChangeCb = NULL;
559 g_networkMonitorContext->threadPool = NULL;
561 OICFree(g_networkMonitorContext);
562 g_networkMonitorContext = NULL;
564 ca_mutex_unlock(g_networkMonitorContextMutex);
566 ca_mutex_lock(g_stopNetworkMonitorMutex);
567 g_stopNetworkMonitor = true;
568 ca_mutex_unlock(g_stopNetworkMonitorMutex);
570 CADestroyNetworkMonitorMutexes();
572 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
575 CAResult_t CAIPStartNetworkMonitor()
577 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
579 ca_mutex_lock(g_stopNetworkMonitorMutex);
580 g_stopNetworkMonitor = false;
581 ca_mutex_unlock(g_stopNetworkMonitorMutex);
583 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
587 CAResult_t CAIPStopNetworkMonitor()
589 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
591 ca_mutex_lock(g_networkMonitorContextMutex);
592 if (!g_networkMonitorContext)
594 OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext is null");
595 ca_mutex_unlock(g_networkMonitorContextMutex);
596 return CA_STATUS_FAILED;
599 ca_mutex_unlock(g_networkMonitorContextMutex);
601 ca_mutex_lock(g_stopNetworkMonitorMutex);
602 if (!g_stopNetworkMonitor)
604 g_stopNetworkMonitor = true;
608 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStopNetworkMonitor, already stopped!");
610 ca_mutex_unlock(g_stopNetworkMonitorMutex);
612 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
616 CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList)
618 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
620 VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "u_array_list is null");
621 VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null");
622 VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
623 "g_networkMonitorContextMutex is null");
625 // Get the interface and ipaddress information from cache
626 ca_mutex_lock(g_networkMonitorContextMutex);
627 if (!g_networkMonitorContext->netInterfaceList
628 || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
630 OIC_LOG(ERROR, IP_MONITOR_TAG, "Network not enabled");
631 ca_mutex_unlock(g_networkMonitorContextMutex);
632 return CA_ADAPTER_NOT_ENABLED;
635 uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
636 OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo list length [%d]",
638 for (uint32_t list_index = 0; list_index < list_length; list_index++)
640 CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
641 g_networkMonitorContext->netInterfaceList, list_index);
646 OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo ip [%s]",
648 CANetInfo_t *newNetinfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
651 OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed!");
652 ca_mutex_unlock(g_networkMonitorContextMutex);
653 return CA_MEMORY_ALLOC_FAILED;
656 memcpy(newNetinfo, info, sizeof(*info));
658 CAResult_t result = u_arraylist_add(*netInterfaceList, (void *) newNetinfo);
659 if (CA_STATUS_OK != result)
661 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
662 ca_mutex_unlock(g_networkMonitorContextMutex);
663 return CA_STATUS_FAILED;
667 ca_mutex_unlock(g_networkMonitorContextMutex);
669 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
673 CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask)
675 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
677 VERIFY_NON_NULL(subnetMask, IP_MONITOR_TAG, "subnet mask");
678 VERIFY_NON_NULL(ipAddress, IP_MONITOR_TAG, "ipAddress is null");
679 VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null");
680 VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
681 "g_networkMonitorContextMutex is null");
683 // Get the interface and ipaddress information from cache
684 ca_mutex_lock(g_networkMonitorContextMutex);
685 if (!g_networkMonitorContext->netInterfaceList
686 || (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
688 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network not enabled");
689 ca_mutex_unlock(g_networkMonitorContextMutex);
690 return CA_ADAPTER_NOT_ENABLED;
693 uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
694 OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "list lenght [%d]", list_length);
695 for (uint32_t list_index = 0; list_index < list_length; list_index++)
697 CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
698 g_networkMonitorContext->netInterfaceList, list_index);
704 if (strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0)
706 OIC_LOG_V(DEBUG, IP_MONITOR_TAG,
707 "CAIPGetInterfaceSubnetMask subnetmask is %s", info->subnetMask);
708 *subnetMask = OICStrdup(info->subnetMask);
712 ca_mutex_unlock(g_networkMonitorContextMutex);
714 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
718 bool CAIPIsConnected()
720 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
721 if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
723 OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
727 ca_mutex_lock(g_networkMonitorContextMutex);
728 if (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList))
730 OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
731 ca_mutex_unlock(g_networkMonitorContextMutex);
734 ca_mutex_unlock(g_networkMonitorContextMutex);
736 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
740 void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
742 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
743 if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
745 OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPSetConnectionStateChangeCallback failed");
748 ca_mutex_lock(g_networkMonitorContextMutex);
750 g_networkMonitorContext->networkChangeCb = callback;
752 ca_mutex_unlock(g_networkMonitorContextMutex);
754 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
757 void CAIPSendNetworkChangeCallback(CANetworkStatus_t currNetworkStatus)
759 OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
760 ca_mutex_lock(g_stopNetworkMonitorMutex);
762 if (g_stopNetworkMonitor)
764 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Stop Network Monitor Thread is called");
765 ca_mutex_unlock(g_stopNetworkMonitorMutex);
768 ca_mutex_unlock(g_stopNetworkMonitorMutex);
769 ca_mutex_lock(g_networkMonitorContextMutex);
770 if(!g_networkMonitorContext)
772 OIC_LOG(DEBUG, IP_MONITOR_TAG, "g_networkChangeCb is NULL");
773 ca_mutex_unlock(g_networkMonitorContextMutex);
776 if (!g_networkMonitorContext->networkChangeCb)
778 OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkChangeCb->networkChangeCb is NULL");
779 ca_mutex_unlock(g_networkMonitorContextMutex);
783 ca_mutex_unlock(g_networkMonitorContextMutex);
785 u_arraylist_t *netInterfaceList = u_arraylist_create();
787 VERIFY_NON_NULL_VOID(netInterfaceList, IP_MONITOR_TAG,
788 "memory allocation failed for netInterfaceList");
790 // if network status is changed
791 CAResult_t ret = CAIPUpdateInterfaceInformation(&netInterfaceList);
792 if (CA_STATUS_OK != ret)
794 OIC_LOG(ERROR, IP_MONITOR_TAG, "could not update interface information");
797 ca_mutex_lock(g_networkMonitorContextMutex);
798 if (!g_networkMonitorContext->netInterfaceList)
800 OIC_LOG(ERROR, IP_MONITOR_TAG,
801 "u_arraylist_create failed. Network Monitor thread stopped");
802 CAClearNetInterfaceInfoList(netInterfaceList);
803 ca_mutex_unlock(g_networkMonitorContextMutex);
807 uint32_t listLength = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
808 for (uint32_t listIndex = 0; listIndex < listLength;)
810 CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
811 g_networkMonitorContext->netInterfaceList, listIndex);
818 bool ret = CACheckIsAnyInterfaceDown(netInterfaceList, info);
821 OIC_LOG(DEBUG, IP_MONITOR_TAG, "Interface is down");
822 if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, listIndex))
824 OIC_LOG(DEBUG, IP_MONITOR_TAG, "u_arraylist_remove success");
825 if (g_networkMonitorContext->networkChangeCb)
827 g_networkMonitorContext->networkChangeCb(info->ipAddress, CA_INTERFACE_DOWN);
834 OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
844 ca_mutex_unlock(g_networkMonitorContextMutex);
846 listLength = u_arraylist_length(netInterfaceList);
847 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
849 CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(netInterfaceList, listIndex);
854 bool ret = CACheckIsInterfaceInfoChanged(info);
857 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CACheckIsInterfaceInfoChanged true");
861 CAClearNetInterfaceInfoList(netInterfaceList);
862 OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
865 JNIEXPORT void JNICALL Java_org_iotivity_ca_CaIpInterface_stateEnabled
866 (JNIEnv *env, jclass clazz)
868 CANetworkStatus_t currNetworkStatus = CA_INTERFACE_UP;
869 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStateEnabled");
871 CAIPSendNetworkChangeCallback(currNetworkStatus);
874 JNIEXPORT void JNICALL Java_org_iotivity_ca_CaIpInterface_stateDisabled
875 (JNIEnv *env, jclass clazz)
877 CANetworkStatus_t currNetworkStatus = CA_INTERFACE_DOWN;
878 OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStateDisabled");
880 CAIPSendNetworkChangeCallback(currNetworkStatus);