replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / android / caedrnwmonitor.c
index 0a1055f..c2e17e9 100644 (file)
@@ -27,7 +27,7 @@
 #include "logger.h"
 #include "oic_malloc.h"
 #include "cathreadpool.h" /* for thread pool */
-#include "camutex.h"
+#include "octhread.h"
 #include "uarraylist.h"
 #include "caadapterutils.h"
 #include "caedrserver.h"
@@ -36,7 +36,7 @@
 #include "org_iotivity_ca_CaEdrInterface.h"
 
 //#define DEBUG_MODE
-#define TAG PCF("CA_EDR_MONITOR")
+#define TAG PCF("OIC_CA_EDR_MONITOR")
 
 static JavaVM *g_jvm;
 static jobject g_context;
@@ -59,8 +59,6 @@ void CAEDRNetworkMonitorJniInit()
 
 CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     if (!threadPool)
     {
         return CA_STATUS_FAILED;
@@ -71,7 +69,6 @@ CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
         CANativeJNIGetJavaVM();
     }
 
-    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
@@ -83,32 +80,20 @@ void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallb
 
 void CAEDRTerminateNetworkMonitor(void)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }
 
 CAResult_t CAEDRStartNetworkMonitor()
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
-    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
 CAResult_t CAEDRStopNetworkMonitor()
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
-    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
 CAResult_t CAEDRClientSetCallbacks(void)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
-    OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
@@ -127,27 +112,28 @@ Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobje
 
     if (NULL == g_networkChangeCb)
     {
-        OIC_LOG_V(DEBUG, TAG, "gNetworkChangeCb is null", status);
+        OIC_LOG(DEBUG, TAG, "g_networkChangeCb is null");
+        return;
     }
 
     jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
     if (!jni_cid_BTAdapter)
     {
-        OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
+        OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
         return;
     }
 
     jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
     if (!id_state_on)
     {
-        OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
+        OIC_LOG(ERROR, TAG, "id_state_on is null");
         return;
     }
 
     jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
     if (!id_state_off)
     {
-        OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
+        OIC_LOG(ERROR, TAG, "id_state_off is null");
         return;
     }
 
@@ -157,14 +143,23 @@ Java_org_iotivity_ca_CaEdrInterface_caEdrStateChangedCallback(JNIEnv *env, jobje
     if (state_on == status)
     {
         CANetworkStatus_t newStatus = CA_INTERFACE_UP;
+
+        CAResult_t res = CAEDRStartReceiveThread(false);
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG(ERROR, TAG, "Failed to CAEDRStartReceiveThread");
+            return;
+        }
         CAEDRServerStartAcceptThread();
         g_networkChangeCb(newStatus);
     }
     else if (state_off == status)
     {
         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
+        CAEDRServerStop();
+        CAEDRNativeSocketCloseToAll(env);
+        CAEDRNativeRemoveAllDeviceState();
         CAEDRNativeRemoveAllDeviceSocket(env);
-        CAEDRNativeRemoveAllDeviceState(env);
         g_networkChangeCb(newStatus);
     }
 }
@@ -187,3 +182,32 @@ Java_org_iotivity_ca_CaEdrInterface_caEdrBondStateChangedCallback(JNIEnv *env, j
         CAEDRNativeRemoveDevice(addr);
     }
 }
+
+JNIEXPORT void JNICALL
+Java_org_iotivity_ca_CaEdrInterface_caEdrConnectionStateChangedCallback(JNIEnv *env,
+                                                                        jobject obj,
+                                                                        jstring addr,
+                                                                        jint isConnected)
+{
+    if (!env || !obj || !addr)
+    {
+        OIC_LOG(ERROR, TAG, "parameter is null");
+        return;
+    }
+
+    OIC_LOG_V(DEBUG, TAG, "CaEdrInterface - Connection State Changed : %d", isConnected);
+
+    if (!isConnected)
+    {
+        const char *address = (*env)->GetStringUTFChars(env, addr, NULL);
+        if (!address)
+        {
+            OIC_LOG(ERROR, TAG, "address is null");
+            return;
+        }
+
+        CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
+        CAEDRNativeRemoveDevice(address);
+        (*env)->ReleaseStringUTFChars(env, addr, address);
+    }
+}