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 ca_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 ca_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] Any user_data passed by the caller when querying for the state changed cb.
81 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
85 * This is the callback which will be called when the connection state gets changed.
87 * @param result [IN] Result of the query done to the platform.
88 * @param connected [IN] State of connection.
89 * @param remoteAddress [IN] LE address of the device to be notified.
90 * @param user_data [IN] Any user_data passed by the caller when querying for the state changed cb.
94 void CALENWConnectionStateChangedCb(int result, bool connected,
95 const char *remoteAddress, void *userData);
97 void CALEMainLoopThread(void *param)
99 g_main_loop_run(g_mainloop);
102 CAResult_t CAInitializeLENetworkMonitor()
104 OIC_LOG(DEBUG, TAG, "IN");
106 if (NULL == g_bleDeviceStateChangedCbMutex)
108 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
109 if (NULL == g_bleDeviceStateChangedCbMutex)
111 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
112 return CA_STATUS_FAILED;
116 if (NULL == g_bleConnectionStateChangedCbMutex)
118 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
119 if (NULL == g_bleConnectionStateChangedCbMutex)
121 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
122 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
123 return CA_STATUS_FAILED;
126 OIC_LOG(DEBUG, TAG, "OUT");
131 void CATerminateLENetworkMonitor()
133 OIC_LOG(DEBUG, TAG, "IN");
135 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
136 g_bleDeviceStateChangedCbMutex = NULL;
138 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
139 g_bleConnectionStateChangedCbMutex = NULL;
140 OIC_LOG(DEBUG, TAG, "OUT");
143 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
145 OIC_LOG(DEBUG, TAG, "IN");
146 g_threadPoolHandle = threadPool;
147 OIC_LOG(DEBUG, TAG, "OUT");
151 CAResult_t CAStartLEAdapter()
153 OIC_LOG(DEBUG, TAG, "IN");
154 g_mainloop = g_main_loop_new(NULL, 0);
157 OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
158 return CA_STATUS_FAILED;
161 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
163 OIC_LOG(ERROR, TAG, "Failed to create thread!");
164 return CA_STATUS_FAILED;
167 int ret = bt_initialize();
170 OIC_LOG(ERROR, TAG, "bt_initialize failed");
171 return CA_STATUS_FAILED;
174 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
177 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
178 return CA_STATUS_FAILED;
181 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
182 if (BT_ERROR_NONE != ret)
184 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
185 return CA_STATUS_FAILED;
188 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
189 if (BT_ERROR_NONE != ret)
191 OIC_LOG_V(ERROR, TAG,
192 "bt_gatt_set_connection_state_changed_cb has failed");
193 return CA_STATUS_FAILED;
196 OIC_LOG(DEBUG, TAG, "OUT");
200 CAResult_t CAStopLEAdapter()
203 int ret = bt_adapter_unset_state_changed_cb();
204 if (BT_ERROR_NONE != ret)
206 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
207 return CA_STATUS_FAILED;
210 ret = bt_deinitialize();
213 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
214 return CA_STATUS_FAILED;
219 g_main_loop_quit(g_mainloop);
224 CAResult_t CAGetLEAdapterState()
226 OIC_LOG(DEBUG, TAG, "IN");
228 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
230 //Get Bluetooth adapter state
231 int ret = bt_adapter_get_state(&adapterState);
232 if (BT_ERROR_NONE != ret)
234 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
236 return CA_STATUS_FAILED;
239 if (BT_ADAPTER_ENABLED != adapterState)
241 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
242 return CA_ADAPTER_NOT_ENABLED;
245 OIC_LOG(DEBUG, TAG, "OUT");
249 CAResult_t CAGetLEAddress(char **local_address)
251 OIC_LOG(DEBUG, TAG, "IN");
253 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
255 char *address = NULL;
257 int ret = bt_adapter_get_address(&address);
258 if (BT_ERROR_NONE != ret || !address)
260 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
262 return CA_STATUS_FAILED;
265 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
267 *local_address = address;
269 OIC_LOG(DEBUG, TAG, "OUT");
274 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
276 OIC_LOG(DEBUG, TAG, "IN");
277 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
278 g_bleDeviceStateChangedCallback = callback;
279 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
280 OIC_LOG(DEBUG, TAG, "OUT");
284 CAResult_t CAUnSetLEAdapterStateChangedCb()
286 OIC_LOG(DEBUG, TAG, "IN");
287 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
288 g_bleDeviceStateChangedCallback = NULL;
289 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
290 OIC_LOG(DEBUG, TAG, "OUT");
294 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
296 OIC_LOG(DEBUG, TAG, "IN");
297 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
298 g_bleConnectionStateChangedCallback = callback;
299 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
300 OIC_LOG(DEBUG, TAG, "OUT");
304 CAResult_t CAUnsetLENWConnectionStateChangedCb()
306 OIC_LOG(DEBUG, TAG, "IN");
307 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
308 g_bleConnectionStateChangedCallback = NULL;
309 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
310 OIC_LOG(DEBUG, TAG, "OUT");
314 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
317 OIC_LOG(DEBUG, TAG, "IN");
319 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
321 if (NULL == g_bleDeviceStateChangedCallback)
323 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
324 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
328 if (BT_ADAPTER_DISABLED == adapter_state)
330 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
331 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
332 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
336 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
337 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
338 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
340 OIC_LOG(DEBUG, TAG, "OUT");
343 void CALENWConnectionStateChangedCb(int result, bool connected,
344 const char *remoteAddress, void *userData)
346 OIC_LOG(DEBUG, TAG, "IN");
348 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
350 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
351 char *addr = OICStrdup(remoteAddress);
354 OIC_LOG(ERROR, TAG, "addr is NULL");
355 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
358 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
360 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
362 OIC_LOG(DEBUG, TAG, "OUT");