Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / caedrnwmonitor.c
index 3cafd34..dd3e11d 100644 (file)
@@ -24,6 +24,7 @@
  * This file provides the APIs for EDR Network Monitor.
  */
 
+#include <glib.h>
 #include <string.h>
 #include <bluetooth.h>
 
@@ -34,6 +35,9 @@
 #include "caqueueingthread.h"
 #include "caremotehandler.h"
 
+static GMainLoop *g_mainloop = NULL;
+static ca_thread_pool_t g_threadPoolHandle = NULL;
+
 /**
  * @var g_edrNetworkChangeCallback
  * @brief Maintains the callback to be notified on local bluetooth adapter status change.
@@ -47,19 +51,16 @@ static CAEDRNetworkStatusCallback g_edrNetworkChangeCallback = NULL;
 static void CAEDRAdapterStateChangeCallback(int result, bt_adapter_state_e adapterState,
                                             void *userData);
 
+void *GMainLoopThread (void *param)
+{
+    g_main_loop_run(g_mainloop);
+    return NULL;
+}
+
 CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
 {
     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
-
-    // Initialize Bluetooth service
-    int err = bt_initialize();
-    if (BT_ERROR_NONE != err)
-    {
-        OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth initialization failed!, error num [%x]",
-                  err);
-        return CA_STATUS_FAILED;
-    }
-
+    g_threadPoolHandle = threadPool;
     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
     return CA_STATUS_OK;
 }
@@ -80,6 +81,28 @@ CAResult_t CAEDRStartNetworkMonitor()
 {
     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
 
+    g_mainloop = g_main_loop_new(NULL, 0);
+    if(!g_mainloop)
+    {
+        OIC_LOG(ERROR, EDR_ADAPTER_TAG, "g_main_loop_new failed\n");
+        return CA_STATUS_FAILED;
+    }
+
+    if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, GMainLoopThread, (void *) NULL))
+    {
+        OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create thread!");
+        return CA_STATUS_FAILED;
+    }
+
+    // Initialize Bluetooth service
+    int err = bt_initialize();
+    if (BT_ERROR_NONE != err)
+    {
+        OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth initialization failed!, error num [%x]",
+                  err);
+        return CA_STATUS_FAILED;
+    }
+
     int ret = bt_adapter_set_state_changed_cb(CAEDRAdapterStateChangeCallback, NULL);
     if(BT_ERROR_NONE != ret)
     {
@@ -101,6 +124,11 @@ CAResult_t CAEDRStopNetworkMonitor()
         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "bt_adapter_set_state_changed_cb failed");
         return CA_STATUS_FAILED;
     }
+
+    if (g_mainloop)
+    {
+        g_main_loop_quit(g_mainloop);
+    }
     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
     return CA_STATUS_OK;
 }