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_type.h>
36 #include "caleadapter.h"
37 #include "caadapterutils.h"
38 #include "oic_string.h"
39 #include "oic_malloc.h"
42 * Logging tag for module name
44 #define TAG "OIC_CA_LE_MONITOR"
46 static GMainLoop *g_mainloop = NULL;
47 static ca_thread_pool_t g_threadPoolHandle = NULL;
50 * Maintains the callback to be notified on device state changed.
52 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
55 * Maintains the callback to be notified on device state changed.
57 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
60 * Mutex to synchronize access to the deviceStateChanged Callback when the state
61 * of the LE adapter gets change.
63 static oc_mutex g_bleDeviceStateChangedCbMutex = NULL;
66 * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
67 * of the LE adapter gets change.
69 static oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
72 * This is the callback which will be called when the adapter state gets changed.
74 * @param result [IN] Result of the query done to the platform.
75 * @param adapter_state [IN] State of the LE adapter.
76 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
80 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
83 void CALEMainLoopThread(void *param)
85 g_main_loop_run(g_mainloop);
89 * This is the callback which will be called when the connection state gets changed.
91 * @param result [IN] Result of the query done to the platform.
92 * @param connected [IN] State of connection.
93 * @param remoteAddress [IN] LE address of the device to be notified.
94 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
98 void CALENWConnectionStateChangedCb(int result, bool connected,
99 const char *remoteAddress, void *userData);
101 CAResult_t CAInitializeLENetworkMonitor()
103 OIC_LOG(DEBUG, TAG, "IN");
105 if (NULL == g_bleDeviceStateChangedCbMutex)
107 g_bleDeviceStateChangedCbMutex = oc_mutex_new();
108 if (NULL == g_bleDeviceStateChangedCbMutex)
110 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
111 return CA_STATUS_FAILED;
115 if (NULL == g_bleConnectionStateChangedCbMutex)
117 g_bleConnectionStateChangedCbMutex = oc_mutex_new();
118 if (NULL == g_bleConnectionStateChangedCbMutex)
120 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
121 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
122 return CA_STATUS_FAILED;
126 OIC_LOG(DEBUG, TAG, "OUT");
131 void CATerminateLENetworkMonitor()
133 OIC_LOG(DEBUG, TAG, "IN");
135 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
136 g_bleDeviceStateChangedCbMutex = NULL;
138 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
139 g_bleConnectionStateChangedCbMutex = NULL;
141 OIC_LOG(DEBUG, TAG, "OUT");
144 CAResult_t CAInitializeLEAdapter()
146 OIC_LOG(DEBUG, TAG, "IN");
147 CAResult_t res = ca_thread_pool_init(2, &g_threadPoolHandle);
148 if (CA_STATUS_OK != res)
150 OIC_LOG(ERROR, TAG, "thread pool initialize error.");
154 OIC_LOG(DEBUG, TAG, "OUT");
158 CAResult_t CAStartLEAdapter()
160 OIC_LOG(DEBUG, TAG, "IN");
162 g_mainloop = g_main_loop_new(NULL, 0);
165 OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
166 return CA_STATUS_FAILED;
169 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread,
170 (void *) NULL, NULL))
172 OIC_LOG(ERROR, TAG, "Failed to create thread!");
173 return CA_STATUS_FAILED;
176 int ret = bt_initialize();
177 if (BT_ERROR_NONE != ret)
179 OIC_LOG(ERROR, TAG, "bt_initialize failed");
180 return CA_STATUS_FAILED;
183 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
184 if (BT_ERROR_NONE != ret)
186 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
187 return CA_STATUS_FAILED;
190 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
191 if (BT_ERROR_NONE != ret)
193 OIC_LOG_V(ERROR, TAG,
194 "bt_gatt_set_connection_state_changed_cb has failed");
195 return CA_STATUS_FAILED;
198 OIC_LOG(DEBUG, TAG, "OUT");
202 CAResult_t CAStopLEAdapter()
204 int ret = bt_gatt_unset_connection_state_changed_cb();
205 if (BT_ERROR_NONE != ret)
207 OIC_LOG(DEBUG, TAG, "bt_gatt_unset_connection_state_changed_cb failed");
208 return CA_STATUS_FAILED;
211 ret = bt_adapter_unset_state_changed_cb();
212 if (BT_ERROR_NONE != ret)
214 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
215 return CA_STATUS_FAILED;
218 ret = bt_deinitialize();
219 if (BT_ERROR_NONE != ret)
221 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
222 return CA_STATUS_FAILED;
227 g_main_loop_quit(g_mainloop);
230 ca_thread_pool_free(g_threadPoolHandle);
231 g_threadPoolHandle = NULL;
235 CAResult_t CAGetLEAdapterState()
237 OIC_LOG(DEBUG, TAG, "IN");
239 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
241 //Get Bluetooth adapter state
242 int ret = bt_adapter_get_state(&adapterState);
243 if (BT_ERROR_NONE != ret)
245 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
247 return CA_STATUS_FAILED;
250 if (BT_ADAPTER_ENABLED != adapterState)
252 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
253 return CA_ADAPTER_NOT_ENABLED;
256 OIC_LOG(DEBUG, TAG, "OUT");
260 CAResult_t CAGetLEAddress(char **local_address)
262 OIC_LOG(DEBUG, TAG, "IN");
264 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
266 char *address = NULL;
268 int ret = bt_adapter_get_address(&address);
269 if (BT_ERROR_NONE != ret || !address)
271 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
273 return CA_STATUS_FAILED;
276 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
278 *local_address = address;
280 OIC_LOG(DEBUG, TAG, "OUT");
285 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
287 OIC_LOG(DEBUG, TAG, "IN");
288 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
289 g_bleDeviceStateChangedCallback = callback;
290 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
291 OIC_LOG(DEBUG, TAG, "OUT");
295 CAResult_t CAUnSetLEAdapterStateChangedCb()
297 OIC_LOG(DEBUG, TAG, "IN");
298 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
299 g_bleDeviceStateChangedCallback = NULL;
300 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
301 OIC_LOG(DEBUG, TAG, "OUT");
305 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
307 OIC_LOG(DEBUG, TAG, "IN");
308 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
309 g_bleConnectionStateChangedCallback = callback;
310 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
311 OIC_LOG(DEBUG, TAG, "OUT");
315 CAResult_t CAUnSetLENWConnectionStateChangedCb()
317 OIC_LOG(DEBUG, TAG, "IN");
318 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
319 g_bleConnectionStateChangedCallback = NULL;
320 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
321 OIC_LOG(DEBUG, TAG, "OUT");
325 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
328 OIC_LOG(DEBUG, TAG, "IN");
330 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
332 if (NULL == g_bleDeviceStateChangedCallback)
334 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
335 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
339 if (BT_ADAPTER_DISABLED == adapter_state)
341 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
342 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
343 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
347 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
349 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
350 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
352 OIC_LOG(DEBUG, TAG, "OUT");
355 void CALENWConnectionStateChangedCb(int result, bool connected,
356 const char *remoteAddress, void *userData)
358 OIC_LOG(DEBUG, TAG, "IN");
360 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
362 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
363 char *addr = OICStrdup(remoteAddress);
366 OIC_LOG(ERROR, TAG, "addr is NULL");
367 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
370 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
372 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
374 OIC_LOG(DEBUG, TAG, "OUT");