From: vimala.v Date: Fri, 6 May 2016 11:39:38 +0000 (+0530) Subject: [TIZEN]Moved GMainloop to CSDK sample from network monitor[LE/EDR]. X-Git-Tag: 1.2.0+RC1~347 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f586bf987dabc3daac99752d8e4f575bf940feb4;p=platform%2Fupstream%2Fiotivity.git [TIZEN]Moved GMainloop to CSDK sample from network monitor[LE/EDR]. Change-Id: I6b4f19ba07142e8f6e8aa4ca015ae6cc9384a42a Signed-off-by: vimala.v Reviewed-on: https://gerrit.iotivity.org/gerrit/8055 Tested-by: jenkins-iotivity Reviewed-by: Jon A. Cruz --- diff --git a/resource/csdk/connectivity/inc/caleinterface.h b/resource/csdk/connectivity/inc/caleinterface.h index 0ac0028..084c536 100644 --- a/resource/csdk/connectivity/inc/caleinterface.h +++ b/resource/csdk/connectivity/inc/caleinterface.h @@ -115,7 +115,7 @@ typedef CAResult_t (*CABLEDataReceivedCallback)(const char *remoteAddress, * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments * @retval ::CA_STATUS_FAILED Operation failed */ -CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool); +CAResult_t CAInitializeLEAdapter(); /** * Start the LE adapter layer. diff --git a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrnwmonitor.c b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrnwmonitor.c index 49b521b..7f36db3 100644 --- a/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrnwmonitor.c +++ b/resource/csdk/connectivity/src/bt_edr_adapter/tizen/caedrnwmonitor.c @@ -24,7 +24,6 @@ * This file provides the APIs for EDR Network Monitor. */ -#include #include #include @@ -35,13 +34,6 @@ #include "caqueueingthread.h" #include "caremotehandler.h" -#define MICROSECS_PER_SEC 1000000 - -static uint64_t const INITIALIZE_TIMEOUT = 1 * MICROSECS_PER_SEC; - -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. @@ -49,101 +41,24 @@ static ca_thread_pool_t g_threadPoolHandle = NULL; static CAEDRNetworkStatusCallback g_edrNetworkChangeCallback = NULL; /** - * Mutex to synchronize access to - */ -static ca_mutex g_btinitializeMutex = NULL; - -/** - * Condition for gmainloop to run. - */ -static ca_cond g_initializeCond = NULL; - -/** - * Flag to check if BT stack is initialised. - */ -static bool g_isBTStackInitialised = false; - -/** * @fn CAEDRAdapterStateChangeCallback * @brief This callback is registered to receive bluetooth adapter state changes. */ static void CAEDRAdapterStateChangeCallback(int result, bt_adapter_state_e adapterState, void *userData); -void CAEDRMainLoopThread(void *param) -{ - OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); - // Initialize Bluetooth service - int ret = bt_initialize(); - if (BT_ERROR_NONE != ret) - { - OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth initialization failed!, error num [%x]", - ret); - return; - } - - ret = bt_adapter_set_state_changed_cb(CAEDRAdapterStateChangeCallback, NULL); - if(BT_ERROR_NONE != ret) - { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "bt_adapter_set_state_changed_cb failed"); - return; - } - - g_mainloop = g_main_loop_new(g_main_context_default(), FALSE); - - ca_mutex_lock(g_btinitializeMutex); - g_isBTStackInitialised = true; - ca_mutex_unlock(g_btinitializeMutex); - ca_cond_signal(g_initializeCond); - - // Run gmainloop to handle the events from bt stack - g_main_loop_run(g_mainloop); - - OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); -} - CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool) { - OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); - g_threadPoolHandle = threadPool; - - if (NULL == g_btinitializeMutex) - { - g_btinitializeMutex = ca_mutex_new(); - if (NULL == g_btinitializeMutex) - { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "ca_mutex_new failed"); - return CA_STATUS_FAILED; - } - } - - if (NULL == g_initializeCond) - { - g_initializeCond = ca_cond_new(); - if (NULL == g_initializeCond) - { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "ca_cond_new failed"); - ca_mutex_free(g_btinitializeMutex); - return CA_STATUS_FAILED; - } - } - - OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); + (void)threadPool; return CA_STATUS_OK; } -void CAEDRTerminateNetworkMonitor(void) +void CAEDRTerminateNetworkMonitor() { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); g_edrNetworkChangeCallback = NULL; - ca_mutex_free(g_btinitializeMutex); - g_btinitializeMutex = NULL; - - ca_cond_free(g_initializeCond); - g_initializeCond = NULL; - OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); } @@ -151,11 +66,20 @@ CAResult_t CAEDRStartNetworkMonitor() { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); - if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAEDRMainLoopThread, - (void *) NULL)) + // Initialize Bluetooth service + int ret = bt_initialize(); + if (BT_ERROR_NONE != ret) + { + OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Bluetooth initialization failed!, error num [%x]", + ret); + return; + } + + ret = bt_adapter_set_state_changed_cb(CAEDRAdapterStateChangeCallback, NULL); + if(BT_ERROR_NONE != ret) { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create thread!"); - return CA_STATUS_FAILED; + OIC_LOG(ERROR, EDR_ADAPTER_TAG, "bt_adapter_set_state_changed_cb failed"); + return; } OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); @@ -180,10 +104,6 @@ CAResult_t CAEDRStopNetworkMonitor() return CA_STATUS_FAILED; } - if (g_mainloop) - { - g_main_loop_quit(g_mainloop); - } OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT"); return CA_STATUS_OK; } @@ -233,21 +153,6 @@ CAResult_t CAEDRGetAdapterEnableState(bool *state) OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN"); VERIFY_NON_NULL(state, EDR_ADAPTER_TAG, "state holder is NULL!"); - ca_mutex_lock(g_btinitializeMutex); - if (!g_isBTStackInitialised) - { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Wait till BT is initialised"); - CAWaitResult_t ret = ca_cond_wait_for(g_initializeCond, g_btinitializeMutex, - INITIALIZE_TIMEOUT); - if (CA_WAIT_TIMEDOUT == ret) - { - OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Timeout before BT initialize"); - ca_mutex_unlock(g_btinitializeMutex); - return CA_STATUS_FAILED; - } - } - ca_mutex_unlock(g_btinitializeMutex); - bt_adapter_state_e adapterState; int err = bt_adapter_get_state(&adapterState); // Get Bluetooth adapter state diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c index cf75d87..04f21e85 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/calenwmonitor.c @@ -85,11 +85,10 @@ void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback) g_bleDeviceStateChangedCallback = callback; } -CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool) +CAResult_t CAInitializeLEAdapter() { - OIC_LOG(DEBUG, TAG, "IN"); - (void)threadPool; - OIC_LOG(DEBUG, TAG, "OUT"); + // Nothing to do. + return CA_STATUS_OK; } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp index f543bb4..1063d34 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp +++ b/resource/csdk/connectivity/src/bt_le_adapter/arduino/cablenwmonitor.cpp @@ -63,7 +63,7 @@ void CATerminateLENetworkMonitor() OIC_LOG(DEBUG, TAG, "OUT"); } -CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool) +CAResult_t CAInitializeLEAdapter() { OIC_LOG(DEBUG, TAG, "IN"); OIC_LOG(DEBUG, TAG, "OUT"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 7b9c0ce..302a2e8 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -1756,7 +1756,7 @@ CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed"); return CA_STATUS_FAILED; } - CAInitializeLEAdapter(handle); + CAInitializeLEAdapter(); CASetLEClientThreadPoolHandle(handle); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/linux/caleinterface.c b/resource/csdk/connectivity/src/bt_le_adapter/linux/caleinterface.c index 0b3e946..57aa4eb 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/linux/caleinterface.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/linux/caleinterface.c @@ -772,9 +772,8 @@ static bool CALEWaitForNonEmptyList(GList * const * list, // ----------------------------------------------------------------------- -CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool) +CAResult_t CAInitializeLEAdapter() { - (void)threadPool; #if !GLIB_CHECK_VERSION(2,36,0) /* Initialize the GLib type system. diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c index 3d4fd24..d158914 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c @@ -373,40 +373,12 @@ CAResult_t CAStartLEGattClient() { OIC_LOG(DEBUG, TAG, "IN"); - ca_mutex_lock(g_LEClientThreadPoolMutex); - if (NULL == g_LEClientThreadPool) - { - OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL"); - CATerminateGattClientMutexVariables(); - ca_mutex_unlock(g_LEClientThreadPoolMutex); - return CA_STATUS_FAILED; - } - - CAResult_t result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartLEGattClientThread, - NULL); - if (CA_STATUS_OK != result) - { - OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed"); - CATerminateGattClientMutexVariables(); - ca_mutex_unlock(g_LEClientThreadPoolMutex); - return CA_STATUS_FAILED; - } - ca_mutex_unlock(g_LEClientThreadPoolMutex); - - OIC_LOG(DEBUG, TAG, "OUT"); - return CA_STATUS_OK; -} - -void CAStartLEGattClientThread(void *data) -{ - OIC_LOG(DEBUG, TAG, "IN"); - ca_mutex_lock(g_LEClientStateMutex); if (true == g_isLEGattClientStarted) { OIC_LOG(ERROR, TAG, "Gatt Client is already running!!"); ca_mutex_unlock(g_LEClientStateMutex); - return; + return CA_STATUS_FAILED; } CAResult_t result = CALEGattSetCallbacks(); @@ -415,7 +387,7 @@ void CAStartLEGattClientThread(void *data) OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed"); ca_mutex_unlock(g_LEClientStateMutex); CATerminateLEGattClient(); - return; + return CA_STATUS_FAILED; } g_isLEGattClientStarted = true; @@ -427,7 +399,7 @@ void CAStartLEGattClientThread(void *data) OIC_LOG(ERROR, TAG, "gBleServerThreadPool is NULL"); CATerminateGattClientMutexVariables(); ca_mutex_unlock(g_LEClientThreadPoolMutex); - return; + return CA_STATUS_FAILED; } result = ca_thread_pool_add_task(g_LEClientThreadPool, CAStartTimerThread, @@ -435,22 +407,14 @@ void CAStartLEGattClientThread(void *data) if (CA_STATUS_OK != result) { OIC_LOG(ERROR, TAG, "ca_thread_pool_add_task failed"); + CATerminateGattClientMutexVariables(); ca_mutex_unlock(g_LEClientThreadPoolMutex); - return; + return CA_STATUS_FAILED; } ca_mutex_unlock(g_LEClientThreadPoolMutex); - OIC_LOG(DEBUG, TAG, "Giving the control to threadPool"); - - GMainContext *thread_context = g_main_context_new(); - - g_eventLoop = g_main_loop_new(thread_context, FALSE); - - g_main_context_push_thread_default(thread_context); - - g_main_loop_run(g_eventLoop); - OIC_LOG(DEBUG, TAG, "OUT"); + return CA_STATUS_OK; } void CAStartTimerThread(void *data) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.h b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.h index e11072f..afa6171 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.h @@ -85,15 +85,6 @@ void CALEAdapterScanResultCb(int result, bt_adapter_le_device_scan_result_info_s void *userData); /** - * This thread will be used to initialize the Gatt Client and start device discovery. - * 1. Setting neccessary callbacks for connection, characteristics changed and discovery. - * 2. Start device discovery - * - * @param[in] data Currently it will be NULL(no parameter) - */ -void CAStartLEGattClientThread(void *data); - -/** * This thread will be used to Start the timer for scanning. * * @param[in] data Currently it will be NULL(no parameter) diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c index 669ed11..ec92acb 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -44,13 +43,6 @@ */ #define TAG "OIC_CA_LE_MONITOR" -#define MICROSECS_PER_SEC 1000000 - -static uint64_t const INITIALIZE_TIMEOUT = 1 * MICROSECS_PER_SEC; - -static GMainLoop *g_mainloop = NULL; -static ca_thread_pool_t g_threadPoolHandle = NULL; - /** * Maintains the callback to be notified on device state changed. */ @@ -74,21 +66,6 @@ static ca_mutex g_bleDeviceStateChangedCbMutex = NULL; static ca_mutex g_bleConnectionStateChangedCbMutex = NULL; /** - * Mutex to synchronize access to - */ -static ca_mutex g_btinitializeMutex = NULL; - -/** - * Condition for gmainloop to run. - */ -static ca_cond g_initializeCond = NULL; - -/** - * Flag to check if BT stack is initialised. - */ -static bool g_isBTStackInitialised = false; - -/** * 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. @@ -113,58 +90,6 @@ void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state, void CALENWConnectionStateChangedCb(int result, bool connected, const char *remoteAddress, void *userData); -void CALEMainLoopThread(void *param) -{ - OIC_LOG(DEBUG, TAG, "IN"); - - int ret = bt_initialize(); - if (BT_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "bt_initialize failed"); - return; - } - - bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED; - //Get Bluetooth adapter state - ret = bt_adapter_get_state(&adapterState); - - if (BT_ERROR_NONE != ret && BT_ADAPTER_ENABLED == adapterState) - { - ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0); - if (BT_ERROR_NONE != ret) - { - OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed"); - return; - } - } - - ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL); - if (BT_ERROR_NONE != ret) - { - OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed"); - return; - } - - 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; - } - - g_mainloop = g_main_loop_new(g_main_context_default(), FALSE); - - ca_mutex_lock(g_btinitializeMutex); - g_isBTStackInitialised = true; - ca_mutex_unlock(g_btinitializeMutex); - ca_cond_signal(g_initializeCond); - - // Run gmainloop to handle the events from bt stack - g_main_loop_run(g_mainloop); - OIC_LOG(DEBUG, TAG, "OUT"); -} - CAResult_t CAInitializeLENetworkMonitor() { OIC_LOG(DEBUG, TAG, "IN"); @@ -190,30 +115,6 @@ CAResult_t CAInitializeLENetworkMonitor() } } - if (NULL == g_btinitializeMutex) - { - g_btinitializeMutex = ca_mutex_new(); - if (NULL == g_btinitializeMutex) - { - OIC_LOG(ERROR, TAG, "ca_mutex_new failed"); - ca_mutex_free(g_bleDeviceStateChangedCbMutex); - ca_mutex_free(g_bleConnectionStateChangedCbMutex); - return CA_STATUS_FAILED; - } - } - - if (NULL == g_initializeCond) - { - g_initializeCond = ca_cond_new(); - if (NULL == g_initializeCond) - { - OIC_LOG(ERROR, TAG, "ca_cond_new failed"); - ca_mutex_free(g_bleDeviceStateChangedCbMutex); - ca_mutex_free(g_bleConnectionStateChangedCbMutex); - ca_mutex_free(g_btinitializeMutex); - return CA_STATUS_FAILED; - } - } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; @@ -229,18 +130,12 @@ void CATerminateLENetworkMonitor() ca_mutex_free(g_bleConnectionStateChangedCbMutex); g_bleConnectionStateChangedCbMutex = NULL; - ca_mutex_free(g_btinitializeMutex); - g_btinitializeMutex = NULL; - - ca_cond_free(g_initializeCond); - g_initializeCond = 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; OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } @@ -249,9 +144,38 @@ CAResult_t CAStartLEAdapter() { OIC_LOG(DEBUG, TAG, "IN"); - if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL)) + int ret = bt_initialize(); + if (BT_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "bt_initialize failed"); + return CA_STATUS_FAILED; + } + bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED; + //Get Bluetooth adapter state + ret = bt_adapter_get_state(&adapterState); + + if (BT_ERROR_NONE != ret && BT_ADAPTER_ENABLED == adapterState) + { + ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0); + if (BT_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed"); + return CA_STATUS_FAILED; + } + } + + ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL); + if (BT_ERROR_NONE != ret) { - OIC_LOG(ERROR, TAG, "Failed to create thread!"); + OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed"); + 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; } @@ -276,10 +200,6 @@ CAResult_t CAStopLEAdapter() return CA_STATUS_FAILED; } - if (g_mainloop) - { - g_main_loop_quit(g_mainloop); - } return CA_STATUS_OK; } @@ -287,21 +207,6 @@ CAResult_t CAGetLEAdapterState() { OIC_LOG(DEBUG, TAG, "IN"); - ca_mutex_lock(g_btinitializeMutex); - if (!g_isBTStackInitialised) - { - OIC_LOG(INFO, TAG, "Wait for BT initialization"); - CAWaitResult_t ret = ca_cond_wait_for(g_initializeCond, g_btinitializeMutex, - INITIALIZE_TIMEOUT); - if (CA_WAIT_TIMEDOUT == ret) - { - OIC_LOG(ERROR, TAG, "Timeout before BT initialize"); - ca_mutex_unlock(g_btinitializeMutex); - return CA_STATUS_FAILED; - } - } - ca_mutex_unlock(g_btinitializeMutex); - bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED; //Get Bluetooth adapter state diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c index 8ce25bc..16c0956 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.c @@ -138,24 +138,84 @@ CAResult_t CAStartLEGattServer() { OIC_LOG(DEBUG, TAG, "IN"); - ca_mutex_lock(g_leServerThreadPoolMutex); - if (NULL == g_leServerThreadPool) + ca_mutex_lock(g_leServerStateMutex); + if (true == g_isLEGattServerStarted) + { + OIC_LOG(ERROR, TAG, "Gatt Server is already running"); + ca_mutex_unlock(g_leServerStateMutex); + return CA_STATUS_OK; + } + + CAResult_t ret = CAInitLEGattServer(); + if (CA_STATUS_OK != ret) { - OIC_LOG(ERROR, TAG, "g_leServerThreadPool is NULL"); - ca_mutex_unlock(g_leServerThreadPoolMutex); + OIC_LOG_V(ERROR, TAG, "CAInitLEGattServer failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); return CA_STATUS_FAILED; } - CAResult_t ret = ca_thread_pool_add_task(g_leServerThreadPool, CAStartLEGattServerThread, - NULL); + char *serviceUUID = CA_GATT_SERVICE_UUID; + + ret = CAAddNewLEServiceInGattServer(serviceUUID); if (CA_STATUS_OK != ret) { - OIC_LOG_V(ERROR, TAG, "ca_thread_pool_add_task failed with ret [%d]", ret); - ca_mutex_unlock(g_leServerThreadPoolMutex); + OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); return CA_STATUS_FAILED; } - ca_mutex_unlock(g_leServerThreadPoolMutex); + static const char charReadUUID[] = CA_GATT_RESPONSE_CHRC_UUID; + char charReadValue[] = {33, 44, 55, 66}; // These are initial random values + + // For Read Characteristics. + ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue, + CA_LE_INITIAL_BUF_SIZE, true); + if (CA_STATUS_OK != ret) + { + OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); + return CA_STATUS_FAILED; + } + + static const char charWriteUUID[] = CA_GATT_REQUEST_CHRC_UUID; + char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values + + + ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue, + CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics. + if (CA_STATUS_OK != ret ) + { + OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); + return CA_STATUS_FAILED; + } + + ret = CARegisterLEServicewithGattServer(g_gattSvcPath); + if (CA_STATUS_OK != ret ) + { + OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); + return CA_STATUS_FAILED; + } + + ret = CALEStartAdvertise(serviceUUID); + if (CA_STATUS_OK != ret) + { + OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret); + ca_mutex_unlock(g_leServerStateMutex); + CATerminateLEGattServer(); + return CA_STATUS_FAILED; + } + + g_isLEGattServerStarted = true; + + ca_mutex_unlock(g_leServerStateMutex); + OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; } @@ -243,103 +303,6 @@ CAResult_t CALEStopAdvertise() return CA_STATUS_OK; } -void CAStartLEGattServerThread(void *data) -{ - OIC_LOG(DEBUG, TAG, "IN"); - ca_mutex_lock(g_leServerStateMutex); - if (true == g_isLEGattServerStarted) - { - OIC_LOG(ERROR, TAG, "Gatt Server is already running"); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - CAResult_t ret = CAInitLEGattServer(); - if (CA_STATUS_OK != ret) - { - OIC_LOG_V(ERROR, TAG, "CAInitLEGattService failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - char *serviceUUID = CA_GATT_SERVICE_UUID; - - ret = CAAddNewLEServiceInGattServer(serviceUUID); - if (CA_STATUS_OK != ret) - { - OIC_LOG_V(ERROR, TAG, "CAAddNewLEServiceInGattServer failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - static const char charReadUUID[] = CA_GATT_RESPONSE_CHRC_UUID; - char charReadValue[] = {33, 44, 55, 66}; // These are initial random values - - // For Read Characteristics. - ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charReadUUID, charReadValue, - CA_LE_INITIAL_BUF_SIZE, true); - if (CA_STATUS_OK != ret) - { - OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - static const char charWriteUUID[] = CA_GATT_REQUEST_CHRC_UUID; - char charWriteValue[] = {33, 44, 55, 66}; // These are initial random values - - - ret = CAAddNewCharacteristicsToGattServer(g_gattSvcPath, charWriteUUID, charWriteValue, - CA_LE_INITIAL_BUF_SIZE, false); // For Write Characteristics. - if (CA_STATUS_OK != ret ) - { - OIC_LOG_V(ERROR, TAG, "CAAddNewCharacteristicsToGattServer failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - ret = CARegisterLEServicewithGattServer(g_gattSvcPath); - if (CA_STATUS_OK != ret ) - { - OIC_LOG_V(ERROR, TAG, "CARegisterLEServicewithGattServer failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - ret = CALEStartAdvertise(serviceUUID); - if (CA_STATUS_OK != ret) - { - OIC_LOG_V(ERROR, TAG, "CALEStartAdvertise failed[%d]", ret); - ca_mutex_unlock(g_leServerStateMutex); - CATerminateLEGattServer(); - return; - } - - g_isLEGattServerStarted = true; - - ca_mutex_unlock(g_leServerStateMutex); - - OIC_LOG(DEBUG, TAG, "LE Server initialization complete."); - - GMainContext *thread_context = NULL; - - thread_context = g_main_context_new(); - - g_eventLoop = g_main_loop_new(thread_context, FALSE); - - g_main_context_push_thread_default(thread_context); - - g_main_loop_run(g_eventLoop); - - OIC_LOG(DEBUG, TAG, "OUT"); -} - CAResult_t CAStopLEGattServer() { OIC_LOG(DEBUG, TAG, "IN"); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.h b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.h index 100ea9b..f3c73d6 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleserver.h @@ -41,15 +41,6 @@ #include "caleinterface.h" /** - * This is thread which will be used for creating ble service and advertise ble service. - * 1. Create New OIC Service 2. Add read & write characteristics to service. - * 3. Register Service 4. Advertise service. - * - * @param[in] data Currently it will be NULL. - */ -void CAStartLEGattServerThread(void *data); - -/** * Used to initialize gatt server. * * @return ::CA_STATUS_OK or Appropriate error code. diff --git a/resource/csdk/stack/samples/tizen/SimpleClientServer/occlient.cpp b/resource/csdk/stack/samples/tizen/SimpleClientServer/occlient.cpp index a128a73..a66a5fa 100644 --- a/resource/csdk/stack/samples/tizen/SimpleClientServer/occlient.cpp +++ b/resource/csdk/stack/samples/tizen/SimpleClientServer/occlient.cpp @@ -20,6 +20,7 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include +#include #include #include #include @@ -51,6 +52,9 @@ static int g_unicastDiscovery = 0; static int g_testCase = 0; static int g_connectivity = 0; +static GMainLoop *g_mainloop = NULL; +pthread_t g_thread; + static const char *DEVICE_DISCOVERY_QUERY = "%s/oic/d"; static const char *PLATFORM_DISCOVERY_QUERY = "%s/oic/p"; static const char *RESOURCE_DISCOVERY_QUERY = "%s/oic/res"; @@ -781,10 +785,52 @@ int InitDiscovery(OCQualityOfService qos) return ret; } +void *GMainLoopThread(void *param) +{ + + if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ) + { + InitDeviceDiscovery(OC_LOW_QOS); + } + else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ) + { + InitPlatformDiscovery(OC_LOW_QOS); + } + else + { + InitDiscovery(OC_LOW_QOS); + } + + while (!gQuitFlag) + { + if (OCProcess() != OC_STACK_OK) + { + cout << "\nOCStack process error"; + return NULL; + } +#ifndef ROUTING_GATEWAY + sleep(1); +#endif + } + + if (g_mainloop) + { + g_main_loop_quit(g_mainloop); + } + return NULL; +} + int main(int argc, char* argv[]) { int opt; + g_mainloop = g_main_loop_new(NULL, FALSE); + if(!g_mainloop) + { + printf("g_main_loop_new failed\n"); + return 0; + } + while ((opt = getopt(argc, argv, "u:t:c:")) != -1) { switch(opt) @@ -872,35 +918,20 @@ int main(int argc, char* argv[]) } } - if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_DEV_REQ) - { - InitDeviceDiscovery(OC_LOW_QOS); - } - else if (g_unicastDiscovery == 0 && g_testCase == TEST_DISCOVER_PLATFORM_REQ) - { - InitPlatformDiscovery(OC_LOW_QOS); - } - else - { - InitDiscovery(OC_LOW_QOS); - } // Break from loop with Ctrl+C OIC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); - while (!gQuitFlag) - { - if (OCProcess() != OC_STACK_OK) - { - cout << "\nOCStack process error\n"; - return 0; - } -#ifndef ROUTING_GATEWAY - sleep(1); -#endif + int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL); + if (result < 0) + { + printf("pthread_create failed in initialize\n"); + return 0; } + g_main_loop_run(g_mainloop); + cout << "\nExiting occlient main loop...\n"; if (OCStop() != OC_STACK_OK) diff --git a/resource/csdk/stack/samples/tizen/SimpleClientServer/ocrouting.cpp b/resource/csdk/stack/samples/tizen/SimpleClientServer/ocrouting.cpp index 7039f93..bbe304c 100644 --- a/resource/csdk/stack/samples/tizen/SimpleClientServer/ocrouting.cpp +++ b/resource/csdk/stack/samples/tizen/SimpleClientServer/ocrouting.cpp @@ -18,7 +18,7 @@ * ******************************************************************/ - +#include #include #include #include @@ -37,6 +37,8 @@ using namespace std; #define TAG "ocrouting" int gQuitFlag = 0; +static GMainLoop *g_mainloop = NULL; +pthread_t g_thread; /* SIGINT handler: set gQuitFlag to 1 for graceful termination */ void handleSigInt(int signum) @@ -47,9 +49,37 @@ void handleSigInt(int signum) } } +void *GMainLoopThread(void *param) +{ + + while (!gQuitFlag) + { + if (OCProcess() != OC_STACK_OK) + { + cout << "\nOCStack process error"; + return NULL; + } +#ifndef ROUTING_GATEWAY + sleep(1); +#endif + } + + if (g_mainloop) + { + g_main_loop_quit(g_mainloop); + } + return NULL; +} + int main() { cout << "\nOCRouting sample is starting..."; + g_mainloop = g_main_loop_new(NULL, FALSE); + if(!g_mainloop) + { + printf("g_main_loop_new failed\n"); + return 0; + } if (OCInit(NULL, 0, OC_GATEWAY) != OC_STACK_OK) { @@ -62,15 +92,16 @@ int main() signal(SIGINT, handleSigInt); - while (!gQuitFlag) + + int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL); + if (result < 0) { - if (OCProcess() != OC_STACK_OK) - { - cout << "\nOCStack process error"; - return 0; - } + printf("pthread_create failed in initialize\n"); + return 0; } + g_main_loop_run(g_mainloop); + cout << "\nExiting ocrouting main loop..."; if (OCStop() != OC_STACK_OK) diff --git a/resource/csdk/stack/samples/tizen/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/tizen/SimpleClientServer/ocserver.cpp index faf5108..31f26fe 100644 --- a/resource/csdk/stack/samples/tizen/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/tizen/SimpleClientServer/ocserver.cpp @@ -44,6 +44,9 @@ static int gObserveNotifyType = 3; int gQuitFlag = 0; int gLightUnderObservation = 0; +static GMainLoop *g_mainloop = NULL; +pthread_t g_thread; + static LightResource Light; // This variable determines instance number of the Light resource. // Used by POST method to create a new instance of Light resource. @@ -923,12 +926,41 @@ static void PrintUsage() cout << "\n-o 1 : Notify list of observers"; } +void *GMainLoopThread(void *param) +{ + + while (!gQuitFlag) + { + if (OCProcess() != OC_STACK_OK) + { + cout << "\nOCStack process error"; + return NULL; + } +#ifndef ROUTING_GATEWAY + sleep(1); +#endif + } + + if (g_mainloop) + { + g_main_loop_quit(g_mainloop); + } + return NULL; +} + int main(int argc, char* argv[]) { pthread_t threadId; pthread_t threadId_presence; int opt; + g_mainloop = g_main_loop_new(NULL, FALSE); + if(!g_mainloop) + { + printf("g_main_loop_new failed\n"); + return 0; + } + while ((opt = getopt(argc, argv, "o:")) != -1) { switch(opt) @@ -955,6 +987,7 @@ int main(int argc, char* argv[]) cout << "\nOCStack init error"; return 0; } + #ifdef WITH_PRESENCE if (OCStartPresence(0) != OC_STACK_OK) { @@ -1035,18 +1068,15 @@ int main(int argc, char* argv[]) signal(SIGINT, handleSigInt); - while (!gQuitFlag) + int result = pthread_create(&g_thread, NULL, GMainLoopThread, (void *)NULL); + if (result < 0) { - if (OCProcess() != OC_STACK_OK) - { - cout << "\nOCStack process error"; - return 0; - } -#ifndef ROUTING_GATEWAY - sleep(1); -#endif + printf("pthread_create failed in initialize\n"); + return 0; } + g_main_loop_run(g_mainloop); + /* * Cancel the Light thread and wait for it to terminate */