added the logic to clean receive buffer when disconnected
authorjihwanseo <jihwan.seo@samsung.com>
Mon, 29 Feb 2016 06:26:35 +0000 (15:26 +0900)
committerJon A. Cruz <jonc@osg.samsung.com>
Wed, 2 Mar 2016 04:20:35 +0000 (04:20 +0000)
when connection state is disconnected,
received data has to be removed.

Change-Id: I05c5a0142549e3c3fac2fb83d6fd038487ec70c2
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Signed-off-by: Jaewook Jung <jw0213.jung@samsung.com>
Signed-off-by: Jaehong Jo <jaehong.jo@samsung.com>
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5209
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
14 files changed:
android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java
resource/csdk/connectivity/inc/caleinterface.h
resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c
resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c
resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.h
resource/csdk/connectivity/src/bt_le_adapter/android/org_iotivity_ca_CaLeClientInterface.h
resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp
resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c
resource/csdk/connectivity/src/bt_le_adapter/linux/caleinterface.c
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.h
resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c
resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.h

index 2ec4471..3b43b57 100644 (file)
@@ -102,7 +102,7 @@ public class CaLeClientInterface {
     private native static void caLeStateChangedCallback(int state);
 
     // bond state
-    private native static void caLeBondStateChangedCallback(String address);
+    private native static void caLeBondStateChangedCallback(String address, boolean connected);
 
     // Callback
     private static BluetoothAdapter.LeScanCallback mLeScanCallback =
@@ -266,7 +266,7 @@ public class CaLeClientInterface {
                             BluetoothDevice device = intent
                                 .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 
-                        caLeBondStateChangedCallback(device.getAddress());
+                        caLeBondStateChangedCallback(device.getAddress(), false);
                     }
                 }
             }
index a027a60..0ac0028 100644 (file)
@@ -78,6 +78,15 @@ typedef struct
 typedef void (*CALEDeviceStateChangedCallback)(CAAdapterState_t adapter_state);
 
 /**
+ * This will be used to notify device connection state changes to the LE adapter layer.
+ * @param[in]   adapter        Transport type information.
+ * @param[in]   remoteAddress  Endpoint object from which the connection status is changed.
+ * @param[in]   connected      State of connection.
+ */
+typedef void (*CALEConnectionStateChangedCallback)(CATransportAdapter_t adapter,
+                                                   const char *remoteAddress, bool connected);
+
+/**
  * Notify the adapter layer that a packet was received from the GATT
  * peer.
  *
@@ -177,6 +186,19 @@ void CATerminateLENetworkMonitor();
 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback);
 
 /**
+ * Set the callback for the device connection state changes.
+ *
+ * @param[in] callback Callback to notify the Device connection state change to
+ *            the CA Layer
+ *
+ * @return ::CA_STATUS_OK or Appropriate error code
+ * @retval ::CA_STATUS_OK  Successful
+ * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments
+ * @retval ::CA_STATUS_FAILED Operation failed
+ */
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback);
+
+/**
  * Provides the MAC address of the local Bluetooth adapter.
  *
  * @param[out] local_address Pointer to the location where bd address
@@ -374,7 +396,7 @@ void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle);
 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle);
 
 /**
- * Unset the callback of adapter connection state change.
+ * Unset the callback of adapter state change.
  *
  * @return ::CA_STATUS_OK or Appropriate error code.
  * @retval ::CA_STATUS_OK  Successful.
@@ -384,6 +406,16 @@ void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle);
 CAResult_t CAUnSetLEAdapterStateChangedCb();
 
 /**
+ * Unset the callback of adapter connection state change.
+ *
+ * @return ::CA_STATUS_OK or Appropriate error code.
+ * @retval ::CA_STATUS_OK  Successful.
+ * @retval ::CA_STATUS_INVALID_PARAM  Invalid input arguments.
+ * @retval ::CA_STATUS_FAILED Operation failed.
+ */
+CAResult_t CAUnSetLENWConnectionStateChangedCb();
+
+/**
  * This will be used to notify errors in BLE adapter.
  *
  * @param[in] remoteAddress Remote endpoint Address
index df86e67..8939cdc 100644 (file)
@@ -4252,11 +4252,6 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
     VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
 
-    jboolean isCopy;
-    char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
-
-    OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
-
     // send success & signal
     jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
     if (!jni_address)
index 3b4779b..1b77c60 100644 (file)
 #include "camutex.h"
 
 #include "org_iotivity_ca_CaLeClientInterface.h"
+#include "org_iotivity_ca_CaLeServerInterface.h"
 
 #define TAG PCF("OIC_CA_LE_MONITOR")
 
 static JavaVM *g_jvm;
 
 /**
- * @var gCALEDeviceStateChangedCallback
+ * @var g_bleDeviceStateChangedCallback
  * @brief Maintains the callback to be notified on device state changed.
  */
-static CALEDeviceStateChangedCallback gCALEDeviceStateChangedCallback = NULL;
+static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
 
 /**
- * @var gCALEDeviceStateChangedCbMutex
+ * @var g_bleConnectionStateChangedCallback
+ * @brief Maintains the callback to be notified on device state changed.
+ */
+static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
+
+/**
+ * @var g_bleDeviceStateChangedCbMutex
  * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
  *           of the LE adapter gets change.
  */
-static ca_mutex gCALEDeviceStateChangedCbMutex = NULL;
+static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
+
+/**
+ * @var g_bleConnectionStateChangedCbMutex
+ * @brief Mutex to synchronize access to the LE ConnectionStateChanged Callback when the state
+ *           of the LE adapter gets change.
+ */
+static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
 
 //getting context
 void CALENetworkMonitorJNISetContext()
@@ -63,10 +77,10 @@ void CALENetworkMonitorJniInit()
     g_jvm = CANativeJNIGetJavaVM();
 }
 
-void CALESetNetStateCallback(CALEDeviceStateChangedCallback callback)
+void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
 {
-    OIC_LOG(DEBUG, TAG, "CALESetNetStateCallback");
-    gCALEDeviceStateChangedCallback = callback;
+    OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
+    g_bleDeviceStateChangedCallback = callback;
 }
 
 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
@@ -94,27 +108,40 @@ CAResult_t CAStopLEAdapter()
 CAResult_t CAInitLENwkMonitorMutexVaraibles()
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    if (NULL == gCALEDeviceStateChangedCbMutex)
+    if (NULL == g_bleDeviceStateChangedCbMutex)
     {
-        gCALEDeviceStateChangedCbMutex = ca_mutex_new();
-        if (NULL == gCALEDeviceStateChangedCbMutex)
+        g_bleDeviceStateChangedCbMutex = ca_mutex_new();
+        if (NULL == g_bleDeviceStateChangedCbMutex)
         {
             OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
             return CA_STATUS_FAILED;
         }
     }
 
+    if (NULL == g_bleConnectionStateChangedCbMutex)
+    {
+       g_bleConnectionStateChangedCbMutex = ca_mutex_new();
+        if (NULL == g_bleConnectionStateChangedCbMutex)
+        {
+            OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
+            ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+            return CA_STATUS_FAILED;
+        }
+    }
+
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
-
 }
 
 void CATerminateLENwkMonitorMutexVaraibles()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_free(gCALEDeviceStateChangedCbMutex);
-    gCALEDeviceStateChangedCbMutex = NULL;
+    ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+    g_bleDeviceStateChangedCbMutex = NULL;
+
+    ca_mutex_free(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCbMutex = NULL;
 
     OIC_LOG(DEBUG, TAG, "OUT");
 }
@@ -199,9 +226,9 @@ CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
 
     OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
 
-    ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
-    CALESetNetStateCallback(callback);
-    ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
+    ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
+    CALESetAdapterStateCallback(callback);
+    ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
@@ -213,6 +240,26 @@ CAResult_t CAUnSetLEAdapterStateChangedCb()
     return CA_STATUS_OK;
 }
 
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCallback = callback;
+    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}
+
+CAResult_t CAUnsetLENWConnectionStateChangedCb()
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCallback = NULL;
+    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}
+
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
                                                                    jint status)
@@ -222,7 +269,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, j
 
     OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
 
-    if (!gCALEDeviceStateChangedCallback)
+    if (!g_bleDeviceStateChangedCallback)
     {
         OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
         return;
@@ -238,7 +285,7 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, j
         CALEClientCreateDeviceList();
         CALEServerCreateCachedDeviceList();
 
-        gCALEDeviceStateChangedCallback(newStatus);
+        g_bleDeviceStateChangedCallback(newStatus);
     }
     else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
     {
@@ -273,46 +320,54 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, j
         CALEClientSetScanFlag(false);
 
         CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
-        gCALEDeviceStateChangedCallback(newStatus);
+        g_bleDeviceStateChangedCallback(newStatus);
     }
 }
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
-                                                                       jstring addr)
+                                                                       jstring jaddr, jboolean jconnected)
 {
     OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
     VERIFY_NON_NULL_VOID(env, TAG, "env is null");
     VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
-    VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
+    VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
 
     // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
     // below logics will be needed when ble pairing is set.
 
-    CAResult_t res = CALEClientDisconnectforAddress(env, addr);
+    CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
     if (CA_STATUS_OK != res)
     {
         OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
     }
 
     // remove obj for client
-    res = CALEClientRemoveGattObjForAddr(env, addr);
+    res = CALEClientRemoveGattObjForAddr(env, jaddr);
     if (CA_STATUS_OK != res)
     {
         OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
     }
 
-    res = CALEClientRemoveDeviceInScanDeviceList(env, addr);
+    res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
     if (CA_STATUS_OK != res)
     {
         OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
     }
 
     // remove obej for server
-    res = CALEServerRemoveDevice(env, addr);
+    res = CALEServerRemoveDevice(env, jaddr);
     if (CA_STATUS_OK != res)
     {
         OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
     }
 
+    const char* address = (*env)->GetStringUTFChars(env, jaddr, NULL);
+    if (!address)
+    {
+        OIC_LOG(ERROR, TAG, "address is null");
+        return;
+    }
+
+    g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, (bool)jconnected);
 }
index 25f20d9..50fc1a0 100644 (file)
@@ -51,7 +51,7 @@ void CALENetworkMonitorJniInit();
  * @param[in]  callback    Callback to be notified on reception of BT state information.
 
  */
-void CALESetNetStateCallback(CALEDeviceStateChangedCallback callback);
+void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback);
 
 #ifdef __cplusplus
 } /* extern "C" */
index a210c6d..5fc2136 100644 (file)
@@ -112,11 +112,11 @@ Java_org_iotivity_ca_caLeClientInterface_caLeStateChangedCallback
 /*
  * Class:     org_iotivity_ca_caLeClientInterface
  * Method:    caLeBondStateChangedCallback
- * Signature: (I)V
+ * Signature: (Ljava/lang/String;Z)V
  */
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_caLeClientInterface_caLeBondStateChangedCallback
-(JNIEnv *, jobject, jstring);
+(JNIEnv *, jobject, jstring, jboolean);
 
 #ifdef __cplusplus
 }
index 6f1732c..f543bb4 100644 (file)
@@ -132,3 +132,8 @@ CAResult_t CAUnSetLEAdapterStateChangedCb()
 }
 
 
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
+{
+    //TODO
+    return CA_STATUS_OK;
+}
index 1e10f5f..5a61a40 100644 (file)
@@ -163,7 +163,7 @@ static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCa
 static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
 
 /**
- * Call the callback to the upper layer when the device state gets
+ * Call the callback to the upper layer when the adapter state gets
  * changed.
  *
  * @param[in] adapter_state New state of the adapter to be notified to
@@ -172,6 +172,16 @@ static void CASetLEAdapterThreadPoolHandle(ca_thread_pool_t handle);
 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state);
 
 /**
+ * Call the callback to the upper layer when the device connection state gets
+ * changed.
+ *
+ * @param[in] address      LE address of the device to be notified to the upper layer.
+ * @param[in] isConnected  whether connection state is connected or not.
+ */
+static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
+                                         bool isConnected);
+
+/**
  * Used to initialize all required mutex variable for LE Adapter
  * implementation.
  *
@@ -1500,7 +1510,7 @@ static void CATerminateLE();
  *
  * @param[in] remoteAddress Remote address of the device from where
  *                          data is received.
- * @param[in] data          Actual data recevied from the remote
+ * @param[in] data          Actual data received from the remote
  *                          device.
  * @param[in] dataLength    Length of the data received from the
  *                          remote device.
@@ -2131,6 +2141,12 @@ static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCa
         {
             OIC_LOG(ERROR, CALEADAPTER_TAG, "CASetLEAdapterStateChangedCb failed!");
         }
+
+        res = CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCb);
+        if (CA_STATUS_OK != res)
+        {
+            OIC_LOG(ERROR, CALEADAPTER_TAG, "CALEConnectionStateChangedCb failed!");
+        }
     }
     else
     {
@@ -2145,6 +2161,83 @@ static CAResult_t CALERegisterNetworkNotifications(CANetworkChangeCallback netCa
     return res;
 }
 
+static void CALEConnectionStateChangedCb(CATransportAdapter_t adapter, const char* address,
+                                         bool isConnected)
+{
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEConnectionStateChangedCb");
+
+    VERIFY_NON_NULL_VOID(address, CALEADAPTER_TAG, "address");
+
+#ifdef __TIZEN__
+    ca_mutex_lock(g_bleIsServerMutex);
+    switch (g_adapterType)
+    {
+        case ADAPTER_SERVER:
+            CALEGattServerConnectionStateChanged(isConnected, address);
+            break;
+        case ADAPTER_CLIENT:
+            CALEGattConnectionStateChanged(isConnected, address);
+            break;
+        case ADAPTER_BOTH_CLIENT_SERVER:
+            CALEGattConnectionStateChanged(isConnected, address);
+            CALEGattServerConnectionStateChanged(isConnected, address);
+            break;
+        default:
+            break;
+    }
+    ca_mutex_unlock(g_bleIsServerMutex);
+#endif
+
+    if(!isConnected)
+    {
+#ifndef SINGLE_THREAD
+        if(g_bleClientSenderInfo)
+        {
+            CABLESenderInfo_t *senderInfo = NULL;
+            uint32_t senderIndex = 0;
+
+            if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleClientSenderInfo, &senderInfo,
+                                                   &senderIndex))
+            {
+                u_arraylist_remove(g_bleClientSenderInfo, senderIndex);
+                OICFree(senderInfo->defragData);
+                OICFree(senderInfo->remoteEndpoint);
+                OICFree(senderInfo);
+
+                OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
+            }
+            else
+            {
+                OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
+            }
+        }
+
+        if(g_bleServerSenderInfo)
+        {
+            CABLESenderInfo_t *senderInfo = NULL;
+            uint32_t senderIndex = 0;
+
+            if(CA_STATUS_OK == CALEGetSenderInfo(address, g_bleServerSenderInfo, &senderInfo,
+                                                   &senderIndex))
+            {
+                u_arraylist_remove(g_bleServerSenderInfo, senderIndex);
+                OICFree(senderInfo->defragData);
+                OICFree(senderInfo->remoteEndpoint);
+                OICFree(senderInfo);
+
+                OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo is removed for disconnection");
+            }
+            else
+            {
+                OIC_LOG(DEBUG, CALEADAPTER_TAG, "SenderInfo doesn't exist");
+            }
+        }
+#endif
+    }
+
+    OIC_LOG(DEBUG, CALEADAPTER_TAG, "OUT");
+}
+
 static void CALEDeviceStateChangedCb(CAAdapterState_t adapter_state)
 {
     OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CALEDeviceStateChangedCb");
index 3073ff5..cb527e9 100644 (file)
@@ -997,6 +997,12 @@ CAResult_t CASetLEAdapterStateChangedCb(
     return CA_STATUS_OK;
 }
 
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
+{
+    //TODO
+    return CA_STATUS_OK;
+}
+
 CAResult_t CAGetLEAddress(char **local_address)
 {
     OIC_LOG(DEBUG, TAG, "Get Linux BLE local device information.");
index 795150b..c30ad82 100644 (file)
@@ -230,8 +230,7 @@ void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userDa
     OIC_LOG(DEBUG, TAG, "OUT ");
 }
 
-void CALEGattConnectionStateChangedCb(int result, bool connected,
-                                      const char *remoteAddress, void *userData)
+void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress)
 {
     OIC_LOG(DEBUG, TAG, "IN ");
 
@@ -754,15 +753,6 @@ CAResult_t CALEGattSetCallbacks()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    int ret = bt_gatt_set_connection_state_changed_cb(CALEGattConnectionStateChangedCb, NULL);
-    if (BT_ERROR_NONE != ret)
-    {
-        OIC_LOG_V(ERROR, TAG,
-                  "bt_gatt_set_connection_state_changed_cb Failed with return as [%s ]",
-                  CALEGetErrorMsg(ret));
-        return CA_STATUS_FAILED;
-    }
-
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
index 32b4485..e11072f 100644 (file)
@@ -69,13 +69,10 @@ void CALEGattCharacteristicWriteCb(int result, bt_gatt_h reqHandle, void *userDa
  * This is the callback which will be called whenever there is change in gatt connection
  * with server(Connected/Disconnected)
  *
- * @param[in]  result        The result of connection state change.
  * @param[in]  connected     State of connection
  * @param[in]  remoteAddress Mac address of the remote device in which we made connection.
- * @param[in]  userData      The user data passed from the request function
  */
-void CALEGattConnectionStateChangedCb(int result, bool connected,
-                                      const char *remoteAddress,void *userData);
+void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress);
 
 /**
  * This is the callback which will be called when LE advertisement is found.
index 45096aa..18cd152 100644 (file)
@@ -52,12 +52,23 @@ static ca_thread_pool_t g_threadPoolHandle = NULL;
 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
 
 /**
+ * Maintains the callback to be notified on device state changed.
+ */
+static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
+
+/**
  * Mutex to synchronize access to the deviceStateChanged Callback when the state
  *           of the LE adapter gets change.
  */
 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
 
 /**
+ * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
+ * of the LE adapter gets change.
+ */
+static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
+
+/**
 * This is the callback which will be called when the adapter state gets changed.
 *
 * @param result         [IN] Result of the query done to the platform.
@@ -67,7 +78,20 @@ static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
 * @return  None.
 */
 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
-                        void *user_data);
+                               void *user_data);
+
+/**
+* This is the callback which will be called when the connection state gets changed.
+*
+* @param result         [IN] Result of the query done to the platform.
+* @param connected      [IN] State of connection.
+* @param remoteAddress  [IN] LE address of the device to be notified.
+* @param user_data      [IN] Any user_data passed by the caller when querying for the state changed cb.
+*
+* @return  None.
+*/
+void CALENWConnectionStateChangedCb(int result, bool connected,
+                                    const char *remoteAddress, void *userData);
 
 void CALEMainLoopThread(void *param)
 {
@@ -87,6 +111,17 @@ CAResult_t CAInitializeLENetworkMonitor()
             return CA_STATUS_FAILED;
         }
     }
+
+    if (NULL == g_bleConnectionStateChangedCbMutex)
+    {
+        g_bleConnectionStateChangedCbMutex = ca_mutex_new();
+        if (NULL == g_bleConnectionStateChangedCbMutex)
+        {
+            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+            return CA_STATUS_FAILED;
+        }
+    }
     OIC_LOG(DEBUG, TAG, "OUT");
 
     return CA_STATUS_OK;
@@ -99,6 +134,8 @@ void CATerminateLENetworkMonitor()
     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
     g_bleDeviceStateChangedCbMutex = NULL;
 
+    ca_mutex_free(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCbMutex = NULL;
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
@@ -147,6 +184,14 @@ CAResult_t CAStartLEAdapter()
         return CA_STATUS_FAILED;
     }
 
+    ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
+    if (BT_ERROR_NONE != ret)
+    {
+        OIC_LOG_V(ERROR, TAG,
+                  "bt_gatt_set_connection_state_changed_cb has failed");
+        return CA_STATUS_FAILED;
+    }
+
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -245,6 +290,26 @@ CAResult_t CAUnSetLEAdapterStateChangedCb()
     return CA_STATUS_OK;
 }
 
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCallback = callback;
+    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}
+
+CAResult_t CAUnsetLENWConnectionStateChangedCb()
+{
+    OIC_LOG(DEBUG, TAG, "IN");
+    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    g_bleConnectionStateChangedCallback = NULL;
+    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    OIC_LOG(DEBUG, TAG, "OUT");
+    return CA_STATUS_OK;
+}
+
 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
                                           void *user_data)
 {
@@ -273,3 +338,26 @@ void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
 
     OIC_LOG(DEBUG, TAG, "OUT");
 }
+
+void CALENWConnectionStateChangedCb(int result, bool connected,
+                                    const char *remoteAddress, void *userData)
+{
+    OIC_LOG(DEBUG, TAG, "IN ");
+
+    VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
+
+    if (!connected)
+    {
+        OIC_LOG_V(DEBUG, TAG, "disconnected [%s] ", remoteAddress);
+        ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+        const char *addr = OICStrdup(remoteAddress);
+        g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
+        ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    }
+    else
+    {
+        OIC_LOG_V(DEBUG, TAG, "connected [%s] ", remoteAddress);
+    }
+
+    OIC_LOG(DEBUG, TAG, "OUT");
+}
index 5e73070..a46a558 100644 (file)
@@ -125,13 +125,10 @@ void CASetLEConnectionStateChangedCallback(CALEConnectionStateChangedCallback co
     g_connStateCb = connStateCb;
 }
 
-void CALEGattServerConnectionStateChangedCb(int result, bool connected,
-                                            const char *remoteAddress, void *userData)
+void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress)
 {
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address");
 
-    OIC_LOG_V(DEBUG, TAG, "CABleGattConnectionStateChangedCb result[%d]", result);
-
     if (connected)
     {
         OIC_LOG_V(DEBUG, TAG, "Connected to [%s]", remoteAddress);
@@ -325,16 +322,6 @@ void CAStartLEGattServerThread(void *data)
         return;
     }
 
-    int res = bt_gatt_set_connection_state_changed_cb(CALEGattServerConnectionStateChangedCb,
-                                                      NULL);
-    if (BT_ERROR_NONE != res)
-    {
-        OIC_LOG_V(ERROR, TAG,
-                  "bt_gatt_set_connection_state_changed_cb Failed with return as [%s]",
-                  CALEGetErrorMsg(res));
-        return;
-    }
-
     ret = CALEStartAdvertise(serviceUUID);
     if (CA_STATUS_OK != ret)
     {
index c638531..0d934b0 100644 (file)
@@ -158,22 +158,10 @@ void CALEGattRemoteCharacteristicWriteCb(char *remoteAddress, bt_gatt_server_h s
  * This is the callback which will be called whenever there is change in gatt connection
  * with Client(Connected/Disconnected).
  *
- * @param[in]  result         The result of discovering.
  * @param[in]  connected      State of connection.
  * @param[in]  remoteAddress  Mac address of the remote device in which we made connection.
- * @param[in]  userData       The user data passed from the request function.
  */
-void CALEGattServerConnectionStateChangedCb(int result, bool connected,
-                                            const char *remoteAddress, void *userData);
-
-/**
- * Callback function type for network status changes delivery from CA common logic.
- * @param[in]   adapter        Transport type information.
- * @param[in]   remoteAddress  Endpoint object from which the connection status is changed.
- * @param[in]   connected      State of connection.
- */
-typedef void (*CALEConnectionStateChangedCallback)(CATransportAdapter_t adapter,
-                                                   const char *remoteAddress, bool connected);
+void CALEGattServerConnectionStateChanged(bool connected, const char *remoteAddress);
 
 /**
  * Setting the connection state changed callback.