* @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.
* This file provides the APIs for EDR Network Monitor.
*/
-#include <glib.h>
#include <string.h>
#include <bluetooth.h>
#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.
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");
}
{
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");
return CA_STATUS_FAILED;
}
- if (g_mainloop)
- {
- g_main_loop_quit(g_mainloop);
- }
OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
return CA_STATUS_OK;
}
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
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;
}
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");
OIC_LOG(ERROR, CALEADAPTER_TAG, "CAInitializeLENetworkMonitor() failed");
return CA_STATUS_FAILED;
}
- CAInitializeLEAdapter(handle);
+ CAInitializeLEAdapter();
CASetLEClientThreadPoolHandle(handle);
// -----------------------------------------------------------------------
-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.
{
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();
OIC_LOG(ERROR, TAG, "CABleGattSetCallbacks Failed");
ca_mutex_unlock(g_LEClientStateMutex);
CATerminateLEGattClient();
- return;
+ return CA_STATUS_FAILED;
}
g_isLEGattClientStarted = true;
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,
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)
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)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <glib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
*/
#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.
*/
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.
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");
}
}
- 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;
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;
}
{
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;
}
return CA_STATUS_FAILED;
}
- if (g_mainloop)
- {
- g_main_loop_quit(g_mainloop);
- }
return CA_STATUS_OK;
}
{
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
{
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;
}
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");
#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.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <stdio.h>
+#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
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";
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)
}
}
- 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)
*
******************************************************************/
-
+#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#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)
}
}
+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)
{
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)
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.
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)
cout << "\nOCStack init error";
return 0;
}
+
#ifdef WITH_PRESENCE
if (OCStartPresence(0) != OC_STACK_OK)
{
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
*/