Fix issuse related unregisterReceiver
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / android / caipnwmonitor.c
old mode 100755 (executable)
new mode 100644 (file)
index 7896334..2685880
@@ -21,7 +21,6 @@
 #include "caipinterface.h"
 
 #include <sys/types.h>
-#include <ifaddrs.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <string.h>
@@ -37,7 +36,7 @@
 #include "oic_string.h"
 #include "org_iotivity_ca_CaIpInterface.h"
 
-#define TAG "IP_MONITOR"
+#define TAG "OIC_CA_IP_MONITOR"
 
 static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family,
                                          uint32_t addr, int flags);
@@ -47,16 +46,22 @@ static CAResult_t CAAddInterfaceItem(u_arraylist_t *iflist, int index,
 
 CAResult_t CAIPJniInit();
 
+/**
+ * destroy JNI interface.
+ * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
+ */
+static CAResult_t CAIPDestroyJniInterface();
+
 #define MAX_INTERFACE_INFO_LENGTH 1024 // allows 32 interfaces from SIOCGIFCONF
 
-CAResult_t CAIPInitializeNetworkMonitor()
+CAResult_t CAIPStartNetworkMonitor()
 {
     return CAIPJniInit();
 }
 
-CAResult_t CAIPTerminateNetworkMonitor()
+CAResult_t CAIPStopNetworkMonitor()
 {
-    return CA_STATUS_OK;
+    return CAIPDestroyJniInterface();
 }
 
 int CAGetPollingInterval(int interval)
@@ -316,16 +321,10 @@ CAResult_t CAIPJniInit()
         return CA_STATUS_FAILED;
     }
 
-    jclass cls_Context = (*env)->FindClass(env, "android/content/Context");
-    if (!cls_Context)
-    {
-        OIC_LOG(ERROR, TAG, "Could not get context object class");
-        return CA_STATUS_FAILED;
-    }
+    jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context",
+                                                           "getApplicationContext",
+                                                           "()Landroid/content/Context;");
 
-    jmethodID mid_getApplicationContext = (*env)->GetMethodID(env, cls_Context,
-                                                                "getApplicationContext",
-                                                                "()Landroid/content/Context;");
     if (!mid_getApplicationContext)
     {
         OIC_LOG(ERROR, TAG, "Could not get getApplicationContext method");
@@ -362,6 +361,78 @@ CAResult_t CAIPJniInit()
     return CA_STATUS_OK;
 }
 
+static CAResult_t CAIPDestroyJniInterface()
+{
+    OIC_LOG(DEBUG, TAG, "CAIPDestroyJniInterface");
+
+    JavaVM *jvm = CANativeJNIGetJavaVM();
+    if (!jvm)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get JavaVM pointer");
+        return CA_STATUS_FAILED;
+    }
+
+    bool isAttached = false;
+    JNIEnv* env;
+    jint res = (*jvm)->GetEnv(jvm, (void**) &env, JNI_VERSION_1_6);
+    if (JNI_OK != res)
+    {
+        OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
+        res = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
+
+        if (JNI_OK != res)
+        {
+            OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
+            return CA_STATUS_FAILED;
+        }
+        isAttached = true;
+    }
+
+    jclass jni_IpInterface = (*env)->FindClass(env, "org/iotivity/ca/CaIpInterface");
+    if (!jni_IpInterface)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get CaIpInterface class");
+        goto error_exit;
+    }
+
+    jmethodID jni_InterfaceDestroyMethod = (*env)->GetStaticMethodID(env, jni_IpInterface,
+                                                                     "destroyIpInterface",
+                                                                     "()V");
+    if (!jni_InterfaceDestroyMethod)
+    {
+        OIC_LOG(ERROR, TAG, "Could not get CaIpInterface destroy method");
+        goto error_exit;
+    }
+
+    (*env)->CallStaticVoidMethod(env, jni_IpInterface, jni_InterfaceDestroyMethod);
+
+    if ((*env)->ExceptionCheck(env))
+    {
+        OIC_LOG(ERROR, TAG, "destroyIpInterface has failed");
+        (*env)->ExceptionDescribe(env);
+        (*env)->ExceptionClear(env);
+        goto error_exit;
+    }
+
+    OIC_LOG(DEBUG, TAG, "Destroy instance for CaIpInterface");
+
+    if (isAttached)
+    {
+        (*jvm)->DetachCurrentThread(jvm);
+    }
+
+    return CA_STATUS_OK;
+
+error_exit:
+
+    if (isAttached)
+    {
+        (*jvm)->DetachCurrentThread(jvm);
+    }
+
+    return CA_STATUS_FAILED;
+}
+
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_CaIpInterface_caIpStateEnabled(JNIEnv *env, jclass class)
 {
@@ -379,5 +450,11 @@ Java_org_iotivity_ca_CaIpInterface_caIpStateDisabled(JNIEnv *env, jclass class)
     (void)class;
     OIC_LOG(DEBUG, TAG, "caIpStateDisabled");
 
-    CAIPGetInterfaceInformation(0);
+    u_arraylist_t *iflist = CAIPGetInterfaceInformation(0);
+    if (!iflist)
+    {
+        OIC_LOG_V(ERROR, TAG, "get interface info failed");
+        return;
+    }
+    u_arraylist_destroy(iflist);
 }