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"
29 #include<sys/socket.h>
30 #include<netinet/in.h>
32 #include <bluetooth.h>
33 #include <bluetooth_internal.h>
34 #include <bluetooth_type.h>
38 #include "caleadapter.h"
39 #include "caadapterutils.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 ca_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 ca_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] Any user_data passed by the caller when querying for the state changed cb.
80 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
84 * This is the callback which will be called when the connection state gets changed.
86 * @param result [IN] Result of the query done to the platform.
87 * @param connected [IN] State of connection.
88 * @param remoteAddress [IN] LE address of the device to be notified.
89 * @param user_data [IN] Any user_data passed by the caller when querying for the state changed cb.
93 void CALENWConnectionStateChangedCb(int result, bool connected,
94 const char *remoteAddress, void *userData);
96 void CALEMainLoopThread(void *param)
98 g_main_loop_run(g_mainloop);
101 CAResult_t CAInitializeLENetworkMonitor()
103 OIC_LOG(DEBUG, TAG, "IN");
105 if (NULL == g_bleDeviceStateChangedCbMutex)
107 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
108 if (NULL == g_bleDeviceStateChangedCbMutex)
110 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
111 return CA_STATUS_FAILED;
115 if (NULL == g_bleConnectionStateChangedCbMutex)
117 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
118 if (NULL == g_bleConnectionStateChangedCbMutex)
120 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
121 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
122 return CA_STATUS_FAILED;
125 OIC_LOG(DEBUG, TAG, "OUT");
130 void CATerminateLENetworkMonitor()
132 OIC_LOG(DEBUG, TAG, "IN");
134 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
135 g_bleDeviceStateChangedCbMutex = NULL;
137 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
138 g_bleConnectionStateChangedCbMutex = NULL;
139 OIC_LOG(DEBUG, TAG, "OUT");
142 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
144 OIC_LOG(DEBUG, TAG, "IN");
145 g_threadPoolHandle = threadPool;
146 OIC_LOG(DEBUG, TAG, "OUT");
150 CAResult_t CAStartLEAdapter()
152 OIC_LOG(DEBUG, TAG, "IN");
153 g_mainloop = g_main_loop_new(NULL, 0);
156 OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
157 return CA_STATUS_FAILED;
160 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
162 OIC_LOG(ERROR, TAG, "Failed to create thread!");
163 return CA_STATUS_FAILED;
166 int ret = bt_initialize();
169 OIC_LOG(ERROR, TAG, "bt_initialize failed");
170 return CA_STATUS_FAILED;
173 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
176 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
177 return CA_STATUS_FAILED;
180 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
181 if (BT_ERROR_NONE != ret)
183 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
184 return CA_STATUS_FAILED;
187 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
188 if (BT_ERROR_NONE != ret)
190 OIC_LOG_V(ERROR, TAG,
191 "bt_gatt_set_connection_state_changed_cb has failed");
192 return CA_STATUS_FAILED;
195 OIC_LOG(DEBUG, TAG, "OUT");
199 CAResult_t CAStopLEAdapter()
202 int ret = bt_adapter_unset_state_changed_cb();
203 if (BT_ERROR_NONE != ret)
205 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
206 return CA_STATUS_FAILED;
209 ret = bt_deinitialize();
212 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
213 return CA_STATUS_FAILED;
218 g_main_loop_quit(g_mainloop);
223 CAResult_t CAGetLEAdapterState()
225 OIC_LOG(DEBUG, TAG, "IN");
227 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
229 //Get Bluetooth adapter state
230 int ret = bt_adapter_get_state(&adapterState);
231 if (BT_ERROR_NONE != ret)
233 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
235 return CA_STATUS_FAILED;
238 if (BT_ADAPTER_ENABLED != adapterState)
240 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
241 return CA_ADAPTER_NOT_ENABLED;
244 OIC_LOG(DEBUG, TAG, "OUT");
248 CAResult_t CAGetLEAddress(char **local_address)
250 OIC_LOG(DEBUG, TAG, "IN");
252 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
254 char *address = NULL;
256 int ret = bt_adapter_get_address(&address);
257 if (BT_ERROR_NONE != ret || !address)
259 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
261 return CA_STATUS_FAILED;
264 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
266 *local_address = address;
268 OIC_LOG(DEBUG, TAG, "OUT");
273 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
275 OIC_LOG(DEBUG, TAG, "IN");
276 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
277 g_bleDeviceStateChangedCallback = callback;
278 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
279 OIC_LOG(DEBUG, TAG, "OUT");
283 CAResult_t CAUnSetLEAdapterStateChangedCb()
285 OIC_LOG(DEBUG, TAG, "IN");
286 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
287 g_bleDeviceStateChangedCallback = NULL;
288 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
289 OIC_LOG(DEBUG, TAG, "OUT");
293 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
295 OIC_LOG(DEBUG, TAG, "IN");
296 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
297 g_bleConnectionStateChangedCallback = callback;
298 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
299 OIC_LOG(DEBUG, TAG, "OUT");
303 CAResult_t CAUnsetLENWConnectionStateChangedCb()
305 OIC_LOG(DEBUG, TAG, "IN");
306 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
307 g_bleConnectionStateChangedCallback = NULL;
308 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
309 OIC_LOG(DEBUG, TAG, "OUT");
313 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
316 OIC_LOG(DEBUG, TAG, "IN");
318 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
320 if (NULL == g_bleDeviceStateChangedCallback)
322 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
323 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
327 if (BT_ADAPTER_DISABLED == adapter_state)
329 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
330 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
331 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
335 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
336 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
337 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
339 OIC_LOG(DEBUG, TAG, "OUT");
342 void CALENWConnectionStateChangedCb(int result, bool connected,
343 const char *remoteAddress, void *userData)
345 OIC_LOG(DEBUG, TAG, "IN ");
347 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
351 OIC_LOG_V(DEBUG, TAG, "disconnected [%s] ", remoteAddress);
352 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
353 const char *addr = OICStrdup(remoteAddress);
354 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
355 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
359 OIC_LOG_V(DEBUG, TAG, "connected [%s] ", remoteAddress);
362 OIC_LOG(DEBUG, TAG, "OUT");