-static void CADestroyNetworkMonitorMutexes()
-{
- ca_mutex_free(g_networkMonitorContextMutex);
- g_networkMonitorContextMutex = NULL;
-
- ca_mutex_free(g_stopNetworkMonitorMutex);
- g_stopNetworkMonitorMutex = NULL;
-}
-
-CAResult_t CAIPInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPInitializeNetworkMonitor IN");
-
- VERIFY_NON_NULL(threadPool, IP_MONITOR_TAG, "threadPool is null");
-
- CAResult_t ret = CAIPJniInit();
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Initialization failed");
- return ret;
- }
-
- ret = CAIPJniSetContext();
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPJniSetContext failed");
- return ret;
- }
-
- ret = CACreateIPJNIInterfaceObject(g_context);
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "unable to create caipinterface instance");
- return ret;
- }
-
- ret = CAInitializeNetworkMonitorMutexes();
-
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "CAInitializeNetworkMonitorMutexes failed");
- return CA_STATUS_FAILED;
- }
-
- ca_mutex_lock(g_networkMonitorContextMutex);
-
- g_networkMonitorContext = (CAIPNetworkMonitorContext *) OICCalloc(
- 1, sizeof(*g_networkMonitorContext));
- if (!g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext Malloc failed");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- CADestroyNetworkMonitorMutexes();
- return CA_MEMORY_ALLOC_FAILED;
- }
- g_networkMonitorContext->threadPool = threadPool;
-
- g_networkMonitorContext->netInterfaceList = u_arraylist_create();
- if (!g_networkMonitorContext->netInterfaceList)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_create failed");
- OICFree(g_networkMonitorContext);
- ca_mutex_unlock(g_networkMonitorContextMutex);
- CADestroyNetworkMonitorMutexes();
- return CA_MEMORY_ALLOC_FAILED;
- }
-
- CAIPUpdateInterfaceInformation(&g_networkMonitorContext->netInterfaceList);
-
- if (u_arraylist_length(g_networkMonitorContext->netInterfaceList))
- {
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_UP;
- }
- else
- {
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
- }
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-void CAIPTerminateNetworkMonitor()
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- g_networkMonitorContext->threadPool = NULL;
-
- CAClearNetInterfaceInfoList(g_networkMonitorContext->netInterfaceList);
-
- g_networkMonitorContext->netInterfaceList = NULL;
- g_networkMonitorContext->nwConnectivityStatus = CA_INTERFACE_DOWN;
- g_networkMonitorContext->networkChangeCb = NULL;
- g_networkMonitorContext->threadPool = NULL;
-
- OICFree(g_networkMonitorContext);
- g_networkMonitorContext = NULL;
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
- g_stopNetworkMonitor = true;
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
-
- CADestroyNetworkMonitorMutexes();
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
-}
-
-CAResult_t CAIPStartNetworkMonitor()
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
- g_stopNetworkMonitor = false;
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAIPStopNetworkMonitor()
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkMonitorContext is null");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
- }
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- ca_mutex_lock(g_stopNetworkMonitorMutex);
- if (!g_stopNetworkMonitor)
- {
- g_stopNetworkMonitor = true;
- }
- else
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStopNetworkMonitor, already stopped!");
- }
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAIPGetInterfaceInfo(u_arraylist_t **netInterfaceList)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- VERIFY_NON_NULL(netInterfaceList, IP_MONITOR_TAG, "u_array_list is null");
- VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null");
- VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
- "g_networkMonitorContextMutex is null");
-
- // Get the interface and ipaddress information from cache
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList
- || !(u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Network not enabled");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_ADAPTER_NOT_ENABLED;
- }
-
- uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo list length [%d]",
- list_length);
- for (uint32_t list_index = 0; list_index < list_length; list_index++)
- {
- CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, list_index);
- if (!info)
- {
- continue;
- }
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceInfo ip [%s]",
- info->ipAddress);
- CANetInfo_t *newNetinfo = (CANetInfo_t *) OICMalloc(sizeof(CANetInfo_t));
- if (!newNetinfo)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "Malloc failed!");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_MEMORY_ALLOC_FAILED;
- }
-
- memcpy(newNetinfo, info, sizeof(*info));
-
- CAResult_t result = u_arraylist_add(*netInterfaceList, (void *) newNetinfo);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_add failed!");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_STATUS_FAILED;
- }
- }
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
-
- VERIFY_NON_NULL(subnetMask, IP_MONITOR_TAG, "subnet mask");
- VERIFY_NON_NULL(ipAddress, IP_MONITOR_TAG, "ipAddress is null");
- VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null");
- VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG,
- "g_networkMonitorContextMutex is null");
-
- // Get the interface and ipaddress information from cache
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList
- || (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList)))
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network not enabled");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return CA_ADAPTER_NOT_ENABLED;
- }
-
- uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "list lenght [%d]", list_length);
- for (uint32_t list_index = 0; list_index < list_length; list_index++)
- {
- CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, list_index);
- if (!info)
- {
- continue;
- }
-
- if (strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0)
- {
- OIC_LOG_V(DEBUG, IP_MONITOR_TAG,
- "CAIPGetInterfaceSubnetMask subnetmask is %s", info->subnetMask);
- *subnetMask = OICStrdup(info->subnetMask);
- break;
- }
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-bool CAIPIsConnected()
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
- if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
- return false;
- }
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList))
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "IP is not connected");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return false;
- }
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
- return true;
-}
-
-void CAIPSetConnectionStateChangeCallback(CAIPConnectionStateChangeCallback callback)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
- if (!g_networkMonitorContextMutex || !g_networkMonitorContext)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "CAIPSetConnectionStateChangeCallback failed");
- return;
- }
- ca_mutex_lock(g_networkMonitorContextMutex);
-
- g_networkMonitorContext->networkChangeCb = callback;
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
-}
-
-void CAIPSendNetworkChangeCallback(CANetworkStatus_t currNetworkStatus)
-{
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN");
- ca_mutex_lock(g_stopNetworkMonitorMutex);
-
- if (g_stopNetworkMonitor)
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Stop Network Monitor Thread is called");
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
- return;
- }
- ca_mutex_unlock(g_stopNetworkMonitorMutex);
- ca_mutex_lock(g_networkMonitorContextMutex);
- if(!g_networkMonitorContext)
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "g_networkChangeCb is NULL");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return;
- }
- if (!g_networkMonitorContext->networkChangeCb)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "g_networkChangeCb->networkChangeCb is NULL");
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return;
- }
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- u_arraylist_t *netInterfaceList = u_arraylist_create();
-
- VERIFY_NON_NULL_VOID(netInterfaceList, IP_MONITOR_TAG,
- "memory allocation failed for netInterfaceList");
-
- // if network status is changed
- CAResult_t ret = CAIPUpdateInterfaceInformation(&netInterfaceList);
- if (CA_STATUS_OK != ret)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "could not update interface information");
- }
-
- ca_mutex_lock(g_networkMonitorContextMutex);
- if (!g_networkMonitorContext->netInterfaceList)
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG,
- "u_arraylist_create failed. Network Monitor thread stopped");
- CAClearNetInterfaceInfoList(netInterfaceList);
- ca_mutex_unlock(g_networkMonitorContextMutex);
- return;
- }
-
- uint32_t listLength = u_arraylist_length(g_networkMonitorContext->netInterfaceList);
- for (uint32_t listIndex = 0; listIndex < listLength;)
- {
- CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(
- g_networkMonitorContext->netInterfaceList, listIndex);
- if (!info)
- {
- listIndex++;
- continue;
- }
-
- bool ret = CACheckIsAnyInterfaceDown(netInterfaceList, info);
- if (ret)
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "Interface is down");
- if (u_arraylist_remove(g_networkMonitorContext->netInterfaceList, listIndex))
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "u_arraylist_remove success");
- if (g_networkMonitorContext->networkChangeCb)
- {
- g_networkMonitorContext->networkChangeCb(info->ipAddress, CA_INTERFACE_DOWN);
- }
- OICFree(info);
- listLength--;
- }
- else
- {
- OIC_LOG(ERROR, IP_MONITOR_TAG, "u_arraylist_remove failed");
- break;
- }
- }
- else
- {
- listIndex++;
- }
- }
-
- ca_mutex_unlock(g_networkMonitorContextMutex);
-
- listLength = u_arraylist_length(netInterfaceList);
- for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
- {
- CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get(netInterfaceList, listIndex);
- if (!info)
- {
- continue;
- }
- bool ret = CACheckIsInterfaceInfoChanged(info);
- if (ret)
- {
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CACheckIsInterfaceInfoChanged true");
- }
- }
-
- CAClearNetInterfaceInfoList(netInterfaceList);
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT");
-}
-
-JNIEXPORT void JNICALL Java_org_iotivity_ca_CaIpInterface_stateEnabled
- (JNIEnv *env, jclass clazz)
-{
- CANetworkStatus_t currNetworkStatus = CA_INTERFACE_UP;
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStateEnabled");
-
- CAIPSendNetworkChangeCallback(currNetworkStatus);
-}
-
-JNIEXPORT void JNICALL Java_org_iotivity_ca_CaIpInterface_stateDisabled
- (JNIEnv *env, jclass clazz)
-{
- CANetworkStatus_t currNetworkStatus = CA_INTERFACE_DOWN;
- OIC_LOG(DEBUG, IP_MONITOR_TAG, "CAIPStateDisabled");