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"
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
31 #include <bluetooth.h>
32 #include <bluetooth_internal.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"
47 * Maintains the callback to be notified on device state changed.
49 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
52 * Maintains the callback to be notified on device state changed.
54 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
57 * Mutex to synchronize access to the deviceStateChanged Callback when the state
58 * of the LE adapter gets change.
60 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
63 * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
64 * of the LE adapter gets change.
66 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
69 * This is the callback which will be called when the adapter state gets changed.
71 * @param result [IN] Result of the query done to the platform.
72 * @param adapter_state [IN] State of the LE adapter.
73 * @param user_data [IN] Any user_data passed by the caller when querying for the state changed cb.
77 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
81 * This is the callback which will be called when the connection state gets changed.
83 * @param result [IN] Result of the query done to the platform.
84 * @param connected [IN] State of connection.
85 * @param remoteAddress [IN] LE address of the device to be notified.
86 * @param user_data [IN] Any user_data passed by the caller when querying for the state changed cb.
90 void CALENWConnectionStateChangedCb(int result, bool connected,
91 const char *remoteAddress, void *userData);
93 CAResult_t CAInitializeLENetworkMonitor()
95 OIC_LOG(DEBUG, TAG, "IN");
97 if (NULL == g_bleDeviceStateChangedCbMutex)
99 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
100 if (NULL == g_bleDeviceStateChangedCbMutex)
102 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
103 return CA_STATUS_FAILED;
107 if (NULL == g_bleConnectionStateChangedCbMutex)
109 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
110 if (NULL == g_bleConnectionStateChangedCbMutex)
112 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
113 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
114 return CA_STATUS_FAILED;
117 OIC_LOG(DEBUG, TAG, "OUT");
122 void CATerminateLENetworkMonitor()
124 OIC_LOG(DEBUG, TAG, "IN");
126 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
127 g_bleDeviceStateChangedCbMutex = NULL;
129 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
130 g_bleConnectionStateChangedCbMutex = NULL;
131 OIC_LOG(DEBUG, TAG, "OUT");
134 CAResult_t CAInitializeLEAdapter()
136 OIC_LOG(DEBUG, TAG, "IN");
137 OIC_LOG(DEBUG, TAG, "OUT");
141 CAResult_t CAStartLEAdapter()
143 OIC_LOG(DEBUG, TAG, "IN");
145 int ret = bt_initialize();
146 if (BT_ERROR_NONE != ret)
148 OIC_LOG(ERROR, TAG, "bt_initialize failed");
149 return CA_STATUS_FAILED;
151 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
152 //Get Bluetooth adapter state
153 ret = bt_adapter_get_state(&adapterState);
155 if (BT_ERROR_NONE != ret && BT_ADAPTER_ENABLED == adapterState)
157 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
158 if (BT_ERROR_NONE != ret)
160 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
161 return CA_STATUS_FAILED;
165 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
166 if (BT_ERROR_NONE != ret)
168 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
169 return CA_STATUS_FAILED;
172 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
173 if (BT_ERROR_NONE != ret)
175 OIC_LOG_V(ERROR, TAG,
176 "bt_gatt_set_connection_state_changed_cb has failed");
177 return CA_STATUS_FAILED;
180 OIC_LOG(DEBUG, TAG, "OUT");
184 CAResult_t CAStopLEAdapter()
187 int ret = bt_adapter_unset_state_changed_cb();
188 if (BT_ERROR_NONE != ret)
190 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
191 return CA_STATUS_FAILED;
194 ret = bt_deinitialize();
195 if (BT_ERROR_NONE != ret)
197 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
198 return CA_STATUS_FAILED;
204 CAResult_t CAGetLEAdapterState()
206 OIC_LOG(DEBUG, TAG, "IN");
208 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
210 //Get Bluetooth adapter state
211 int ret = bt_adapter_get_state(&adapterState);
212 if (BT_ERROR_NONE != ret)
214 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
216 return CA_STATUS_FAILED;
219 if (BT_ADAPTER_ENABLED != adapterState)
221 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
222 return CA_ADAPTER_NOT_ENABLED;
225 OIC_LOG(DEBUG, TAG, "OUT");
229 CAResult_t CAGetLEAddress(char **local_address)
231 OIC_LOG(DEBUG, TAG, "IN");
233 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
235 char *address = NULL;
237 int ret = bt_adapter_get_address(&address);
238 if (BT_ERROR_NONE != ret || !address)
240 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
242 return CA_STATUS_FAILED;
245 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
247 *local_address = address;
249 OIC_LOG(DEBUG, TAG, "OUT");
254 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
256 OIC_LOG(DEBUG, TAG, "IN");
257 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
258 g_bleDeviceStateChangedCallback = callback;
259 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
260 OIC_LOG(DEBUG, TAG, "OUT");
264 CAResult_t CAUnSetLEAdapterStateChangedCb()
266 OIC_LOG(DEBUG, TAG, "IN");
267 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
268 g_bleDeviceStateChangedCallback = NULL;
269 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
270 OIC_LOG(DEBUG, TAG, "OUT");
274 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
276 OIC_LOG(DEBUG, TAG, "IN");
277 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
278 g_bleConnectionStateChangedCallback = callback;
279 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
280 OIC_LOG(DEBUG, TAG, "OUT");
284 CAResult_t CAUnsetLENWConnectionStateChangedCb()
286 OIC_LOG(DEBUG, TAG, "IN");
287 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
288 g_bleConnectionStateChangedCallback = NULL;
289 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
290 OIC_LOG(DEBUG, TAG, "OUT");
294 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
297 OIC_LOG(DEBUG, TAG, "IN");
299 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
301 if (NULL == g_bleDeviceStateChangedCallback)
303 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
304 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
308 if (BT_ADAPTER_DISABLED == adapter_state)
310 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
311 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
312 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
316 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
318 int ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
319 if (BT_ERROR_NONE != ret)
321 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
325 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
326 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
328 OIC_LOG(DEBUG, TAG, "OUT");
331 void CALENWConnectionStateChangedCb(int result, bool connected,
332 const char *remoteAddress, void *userData)
334 OIC_LOG(DEBUG, TAG, "IN");
336 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
338 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
339 char *addr = OICStrdup(remoteAddress);
342 OIC_LOG(ERROR, TAG, "addr is NULL");
343 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
346 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
348 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
350 OIC_LOG(DEBUG, TAG, "OUT");