#include "caleinterface.h"
-#include<stdio.h>
-#include<stdlib.h>
-#include<string.h>
-#include<glib.h>
-#include<arpa/inet.h>
-#include<sys/types.h>
-#include<sys/socket.h>
-#include<netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#include <bluetooth.h>
#include <bluetooth_internal.h>
#include <bluetooth_type.h>
-
-#include "camutex.h"
+#include "octhread.h"
#include "caleadapter.h"
#include "caadapterutils.h"
+#include "oic_string.h"
+#include "oic_malloc.h"
/**
* Logging tag for module name
static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
/**
+ * Maintains the callback to be notified on device state changed.
+ */
+static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
+
+/**
* 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 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 *user_data);
-void *GMainLoopThread (void *param)
+void CALEMainLoopThread(void *param)
{
g_main_loop_run(g_mainloop);
- return NULL;
}
+/**
+* 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] 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);
+
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 = oc_mutex_new();
+ if (NULL == g_bleConnectionStateChangedCbMutex)
+ {
+ 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;
{
OIC_LOG(DEBUG, TAG, "IN");
- ca_mutex_free(g_bleDeviceStateChangedCbMutex);
+ oc_mutex_free(g_bleDeviceStateChangedCbMutex);
g_bleDeviceStateChangedCbMutex = NULL;
+ 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;
}
CAResult_t CAStartLEAdapter()
{
OIC_LOG(DEBUG, TAG, "IN");
+
g_mainloop = g_main_loop_new(NULL, 0);
if(!g_mainloop)
{
return CA_STATUS_FAILED;
}
- if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, GMainLoopThread, (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;
}
- ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
- if (0 != ret)
+ ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
+ if (BT_ERROR_NONE != ret)
{
- OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
+ OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
return CA_STATUS_FAILED;
}
- ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
+ ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
if (BT_ERROR_NONE != ret)
{
- OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
+ OIC_LOG_V(ERROR, TAG,
+ "bt_gatt_set_connection_state_changed_cb has failed");
return CA_STATUS_FAILED;
}
}
ret = bt_deinitialize();
- if (0 != ret)
+ if (BT_ERROR_NONE != ret)
{
OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
return CA_STATUS_FAILED;
{
g_main_loop_quit(g_mainloop);
}
+
+ ca_thread_pool_free(g_threadPoolHandle);
+ g_threadPoolHandle = NULL;
return CA_STATUS_OK;
}
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;
}
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;
+}
+
+CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+ oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
+ g_bleConnectionStateChangedCallback = callback;
+ oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+ OIC_LOG(DEBUG, TAG, "OUT");
+ return CA_STATUS_OK;
+}
+
+CAResult_t CAUnSetLENWConnectionStateChangedCb()
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+ oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
+ g_bleConnectionStateChangedCallback = NULL;
+ oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
{
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;
}
{
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");
+}
+
+void CALENWConnectionStateChangedCb(int result, bool connected,
+ const char *remoteAddress, void *userData)
+{
+ OIC_LOG(DEBUG, TAG, "IN");
+
+ VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
+
+ oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
+ char *addr = OICStrdup(remoteAddress);
+ if (NULL == addr)
+ {
+ OIC_LOG(ERROR, TAG, "addr is NULL");
+ oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
+ return;
+ }
+ g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
+ OICFree(addr);
+ oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
OIC_LOG(DEBUG, TAG, "OUT");
}