X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fcsdk%2Fconnectivity%2Fsrc%2Fbt_le_adapter%2Ftizen%2Fcalenwmonitor.c;h=577587731864a8560bec0b0b2568d6ccf67c4970;hb=refs%2Ftags%2Faccepted%2Ftizen%2Funified%2F20171010.063815;hp=dd4e3f3ef3d0bbbcbeca15c172259763dec66ad5;hpb=b9f543a2d95dc0db88c2aa82a8ac880c2ab6296d;p=platform%2Fupstream%2Fiotivity.git 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 dd4e3f3..5775877 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/calenwmonitor.c @@ -33,7 +33,7 @@ #include #include -#include "camutex.h" +#include "octhread.h" #include "caleadapter.h" #include "caadapterutils.h" #include "oic_string.h" @@ -44,10 +44,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; @@ -65,28 +61,13 @@ 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; - -/** - * 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; +static oc_mutex g_bleConnectionStateChangedCbMutex = NULL; /** * This is the callback which will be called when the adapter state gets changed. @@ -100,6 +81,11 @@ static bool g_isBTStackInitialised = false; 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. * @@ -113,99 +99,31 @@ 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; - } - 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"); 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); - return CA_STATUS_FAILED; - } - } - - 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); + OIC_LOG(ERROR, TAG, "oc_mutex_new failed"); + oc_mutex_free(g_bleDeviceStateChangedCbMutex); 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; @@ -215,24 +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; - 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; + 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; } @@ -241,12 +160,42 @@ CAResult_t CAStartLEAdapter() { OIC_LOG(DEBUG, TAG, "IN"); - if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL)) + g_mainloop = g_main_loop_new(NULL, 0); + if(!g_mainloop) + { + OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n"); + return CA_STATUS_FAILED; + } + + 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 (BT_ERROR_NONE != ret) + { + OIC_LOG(ERROR, TAG, "bt_initialize failed"); + return CA_STATUS_FAILED; + } + + 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 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; } @@ -272,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; } @@ -279,21 +231,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 @@ -343,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; } @@ -353,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; } @@ -363,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; } @@ -385,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; } @@ -398,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"); } @@ -416,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"); }