replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / calenwmonitor.c
index 97de397..5775877 100644 (file)
@@ -33,7 +33,7 @@
 #include <bluetooth_internal.h>
 #include <bluetooth_type.h>
 
-#include "camutex.h"
+#include "octhread.h"
 #include "caleadapter.h"
 #include "caadapterutils.h"
 #include "oic_string.h"
@@ -61,68 +61,69 @@ static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback =
  * Mutex to synchronize access to the deviceStateChanged Callback when the state
  *           of the LE adapter gets change.
  */
-static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
+static oc_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;
+static oc_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.
 * @param adapter_state  [IN] State of the LE adapter.
-* @param user_data      [IN] Any user_data passed by the caller when querying for the state changed cb.
+* @param user_data      [IN] User data passed by the caller when querying for the state changed cb.
 *
 * @return  None.
 */
 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
                                void *user_data);
 
+void CALEMainLoopThread(void *param)
+{
+    g_main_loop_run(g_mainloop);
+}
+
 /**
 * 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.
+* @param user_data      [IN] 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)
-{
-    g_main_loop_run(g_mainloop);
-}
-
 CAResult_t CAInitializeLENetworkMonitor()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
     if (NULL == g_bleDeviceStateChangedCbMutex)
     {
-        g_bleDeviceStateChangedCbMutex = ca_mutex_new();
+        g_bleDeviceStateChangedCbMutex = oc_mutex_new();
         if (NULL == g_bleDeviceStateChangedCbMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
             return CA_STATUS_FAILED;
         }
     }
 
     if (NULL == g_bleConnectionStateChangedCbMutex)
     {
-        g_bleConnectionStateChangedCbMutex = ca_mutex_new();
+        g_bleConnectionStateChangedCbMutex = oc_mutex_new();
         if (NULL == g_bleConnectionStateChangedCbMutex)
         {
-            OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
-            ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+            OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
+            oc_mutex_free(g_bleDeviceStateChangedCbMutex);
             return CA_STATUS_FAILED;
         }
     }
+
     OIC_LOG(DEBUG, TAG, "OUT");
 
     return CA_STATUS_OK;
@@ -132,18 +133,25 @@ void CATerminateLENetworkMonitor()
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_free(g_bleDeviceStateChangedCbMutex);
     g_bleDeviceStateChangedCbMutex = NULL;
 
-    ca_mutex_free(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_free(g_bleConnectionStateChangedCbMutex);
     g_bleConnectionStateChangedCbMutex = NULL;
+
     OIC_LOG(DEBUG, TAG, "OUT");
 }
 
-CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
+CAResult_t CAInitializeLEAdapter()
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    g_threadPoolHandle = threadPool;
+    CAResult_t res = ca_thread_pool_init(2, &g_threadPoolHandle);
+    if (CA_STATUS_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "thread pool initialize error.");
+        return res;
+    }
+
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -151,6 +159,7 @@ CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
 CAResult_t CAStartLEAdapter()
 {
     OIC_LOG(DEBUG, TAG, "IN");
+
     g_mainloop = g_main_loop_new(NULL, 0);
     if(!g_mainloop)
     {
@@ -158,14 +167,15 @@ CAResult_t CAStartLEAdapter()
         return CA_STATUS_FAILED;
     }
 
-    if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
+    if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread,
+                                                (void *) NULL, NULL))
     {
         OIC_LOG(ERROR, TAG, "Failed to create thread!");
         return CA_STATUS_FAILED;
     }
 
     int ret = bt_initialize();
-    if (0 != ret)
+    if (BT_ERROR_NONE != ret)
     {
         OIC_LOG(ERROR, TAG, "bt_initialize failed");
         return CA_STATUS_FAILED;
@@ -201,7 +211,7 @@ CAResult_t CAStopLEAdapter()
     }
 
     ret = bt_deinitialize();
-    if (0 != ret)
+    if (BT_ERROR_NONE != ret)
     {
         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
         return CA_STATUS_FAILED;
@@ -211,6 +221,9 @@ CAResult_t CAStopLEAdapter()
     {
         g_main_loop_quit(g_mainloop);
     }
+
+    ca_thread_pool_free(g_threadPoolHandle);
+    g_threadPoolHandle = NULL;
     return CA_STATUS_OK;
 }
 
@@ -267,9 +280,9 @@ CAResult_t CAGetLEAddress(char **local_address)
 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
     g_bleDeviceStateChangedCallback = callback;
-    ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -277,9 +290,9 @@ CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
 CAResult_t CAUnSetLEAdapterStateChangedCb()
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
     g_bleDeviceStateChangedCallback = NULL;
-    ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -287,19 +300,19 @@ CAResult_t CAUnSetLEAdapterStateChangedCb()
 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
     g_bleConnectionStateChangedCallback = callback;
-    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
 
-CAResult_t CAUnsetLENWConnectionStateChangedCb()
+CAResult_t CAUnSetLENWConnectionStateChangedCb()
 {
     OIC_LOG(DEBUG, TAG, "IN");
-    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
     g_bleConnectionStateChangedCallback = NULL;
-    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
     OIC_LOG(DEBUG, TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -309,12 +322,12 @@ void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
 {
     OIC_LOG(DEBUG, TAG, "IN");
 
-    ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
 
     if (NULL == g_bleDeviceStateChangedCallback)
     {
         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
-        ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
+        oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
         return;
     }
 
@@ -322,13 +335,14 @@ void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
     {
         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
-        ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
+        oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
         return;
     }
 
     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
+
     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
-    ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
+    oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
 }
@@ -340,17 +354,17 @@ void CALENWConnectionStateChangedCb(int result, bool connected,
 
     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
 
-    ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
     char *addr = OICStrdup(remoteAddress);
     if (NULL == addr)
     {
         OIC_LOG(ERROR, TAG, "addr is NULL");
-        ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+        oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
         return;
     }
     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
     OICFree(addr);
-    ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+    oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
 
     OIC_LOG(DEBUG, TAG, "OUT");
 }