1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "caleinterface.h"
27 #include <arpa/inet.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <bluetooth.h>
33 #include <bluetooth_internal.h>
34 #include <bluetooth_type.h>
37 #include "caleadapter.h"
38 #include "caadapterutils.h"
39 #include "oic_string.h"
40 #include "oic_malloc.h"
43 * Logging tag for module name
45 #define TAG "OIC_CA_LE_MONITOR"
47 static GMainLoop *g_mainloop = NULL;
48 static ca_thread_pool_t g_threadPoolHandle = NULL;
51 * Maintains the callback to be notified on device state changed.
53 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
56 * Maintains the callback to be notified on device state changed.
58 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
61 * Mutex to synchronize access to the deviceStateChanged Callback when the state
62 * of the LE adapter gets change.
64 static oc_mutex g_bleDeviceStateChangedCbMutex = NULL;
67 * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
68 * of the LE adapter gets change.
70 static oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
73 * This is the callback which will be called when the adapter state gets changed.
75 * @param result [IN] Result of the query done to the platform.
76 * @param adapter_state [IN] State of the LE adapter.
77 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
81 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
84 void CALEMainLoopThread(void *param)
86 g_main_loop_run(g_mainloop);
90 * This is the callback which will be called when the connection state gets changed.
92 * @param result [IN] Result of the query done to the platform.
93 * @param connected [IN] State of connection.
94 * @param remoteAddress [IN] LE address of the device to be notified.
95 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
99 void CALENWConnectionStateChangedCb(int result, bool connected,
100 const char *remoteAddress, void *userData);
102 CAResult_t CAInitializeLENetworkMonitor()
104 OIC_LOG(DEBUG, TAG, "IN");
106 if (NULL == g_bleDeviceStateChangedCbMutex)
108 g_bleDeviceStateChangedCbMutex = oc_mutex_new();
109 if (NULL == g_bleDeviceStateChangedCbMutex)
111 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
112 return CA_STATUS_FAILED;
116 if (NULL == g_bleConnectionStateChangedCbMutex)
118 g_bleConnectionStateChangedCbMutex = oc_mutex_new();
119 if (NULL == g_bleConnectionStateChangedCbMutex)
121 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
122 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
123 return CA_STATUS_FAILED;
127 OIC_LOG(DEBUG, TAG, "OUT");
132 void CATerminateLENetworkMonitor()
134 OIC_LOG(DEBUG, TAG, "IN");
136 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
137 g_bleDeviceStateChangedCbMutex = NULL;
139 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
140 g_bleConnectionStateChangedCbMutex = NULL;
142 OIC_LOG(DEBUG, TAG, "OUT");
145 CAResult_t CAInitializeLEAdapter()
147 OIC_LOG(DEBUG, TAG, "IN");
148 CAResult_t res = ca_thread_pool_init(2, &g_threadPoolHandle);
149 if (CA_STATUS_OK != res)
151 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
155 OIC_LOG(DEBUG, TAG, "OUT");
159 CAResult_t CAStartLEAdapter()
161 OIC_LOG(DEBUG, TAG, "IN");
163 g_mainloop = g_main_loop_new(NULL, 0);
166 OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
167 return CA_STATUS_FAILED;
170 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread,
171 (void *) NULL, NULL))
173 OIC_LOG(ERROR, TAG, "Failed to create thread!");
174 return CA_STATUS_FAILED;
177 int ret = bt_initialize();
178 if (BT_ERROR_NONE != ret)
180 OIC_LOG(ERROR, TAG, "bt_initialize failed");
181 return CA_STATUS_FAILED;
184 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
185 if (BT_ERROR_NONE != ret)
187 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
188 return CA_STATUS_FAILED;
191 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
192 if (BT_ERROR_NONE != ret)
194 OIC_LOG_V(ERROR, TAG,
195 "bt_gatt_set_connection_state_changed_cb has failed");
196 return CA_STATUS_FAILED;
199 OIC_LOG(DEBUG, TAG, "OUT");
203 CAResult_t CAStopLEAdapter()
206 int ret = bt_adapter_unset_state_changed_cb();
207 if (BT_ERROR_NONE != ret)
209 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
210 return CA_STATUS_FAILED;
213 ret = bt_deinitialize();
214 if (BT_ERROR_NONE != ret)
216 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
217 return CA_STATUS_FAILED;
222 g_main_loop_quit(g_mainloop);
225 ca_thread_pool_free(g_threadPoolHandle);
226 g_threadPoolHandle = NULL;
230 CAResult_t CAGetLEAdapterState()
232 OIC_LOG(DEBUG, TAG, "IN");
234 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
236 //Get Bluetooth adapter state
237 int ret = bt_adapter_get_state(&adapterState);
238 if (BT_ERROR_NONE != ret)
240 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
242 return CA_STATUS_FAILED;
245 if (BT_ADAPTER_ENABLED != adapterState)
247 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
248 return CA_ADAPTER_NOT_ENABLED;
251 OIC_LOG(DEBUG, TAG, "OUT");
255 CAResult_t CAGetLEAddress(char **local_address)
257 OIC_LOG(DEBUG, TAG, "IN");
259 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
261 char *address = NULL;
263 int ret = bt_adapter_get_address(&address);
264 if (BT_ERROR_NONE != ret || !address)
266 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
268 return CA_STATUS_FAILED;
271 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
273 *local_address = address;
275 OIC_LOG(DEBUG, TAG, "OUT");
280 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
282 OIC_LOG(DEBUG, TAG, "IN");
283 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
284 g_bleDeviceStateChangedCallback = callback;
285 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
286 OIC_LOG(DEBUG, TAG, "OUT");
290 CAResult_t CAUnSetLEAdapterStateChangedCb()
292 OIC_LOG(DEBUG, TAG, "IN");
293 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
294 g_bleDeviceStateChangedCallback = NULL;
295 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
296 OIC_LOG(DEBUG, TAG, "OUT");
300 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
302 OIC_LOG(DEBUG, TAG, "IN");
303 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
304 g_bleConnectionStateChangedCallback = callback;
305 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
306 OIC_LOG(DEBUG, TAG, "OUT");
310 CAResult_t CAUnSetLENWConnectionStateChangedCb()
312 OIC_LOG(DEBUG, TAG, "IN");
313 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
314 g_bleConnectionStateChangedCallback = NULL;
315 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
316 OIC_LOG(DEBUG, TAG, "OUT");
320 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
323 OIC_LOG(DEBUG, TAG, "IN");
325 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
327 if (NULL == g_bleDeviceStateChangedCallback)
329 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
330 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
334 if (BT_ADAPTER_DISABLED == adapter_state)
336 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
337 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
338 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
342 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
344 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
345 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
347 OIC_LOG(DEBUG, TAG, "OUT");
350 void CALENWConnectionStateChangedCb(int result, bool connected,
351 const char *remoteAddress, void *userData)
353 OIC_LOG(DEBUG, TAG, "IN");
355 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
357 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
358 char *addr = OICStrdup(remoteAddress);
361 OIC_LOG(ERROR, TAG, "addr is NULL");
362 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
365 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
367 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
369 OIC_LOG(DEBUG, TAG, "OUT");