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] 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] 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;
118 OIC_LOG(DEBUG, TAG, "OUT");
123 void CATerminateLENetworkMonitor()
125 OIC_LOG(DEBUG, TAG, "IN");
127 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
128 g_bleDeviceStateChangedCbMutex = NULL;
130 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
131 g_bleConnectionStateChangedCbMutex = NULL;
133 OIC_LOG(DEBUG, TAG, "OUT");
136 CAResult_t CAInitializeLEAdapter()
138 OIC_LOG(DEBUG, TAG, "IN");
139 OIC_LOG(DEBUG, TAG, "OUT");
143 CAResult_t CAStartLEAdapter()
145 OIC_LOG(DEBUG, TAG, "IN");
147 int ret = bt_initialize();
148 if (BT_ERROR_NONE != ret)
150 OIC_LOG(ERROR, TAG, "bt_initialize failed");
151 return CA_STATUS_FAILED;
154 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
155 if (BT_ERROR_NONE != ret)
157 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
158 return CA_STATUS_FAILED;
161 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
162 if (BT_ERROR_NONE != ret)
164 OIC_LOG_V(ERROR, TAG,
165 "bt_gatt_set_connection_state_changed_cb has failed");
166 return CA_STATUS_FAILED;
169 OIC_LOG(DEBUG, TAG, "OUT");
173 CAResult_t CAStopLEAdapter()
176 int ret = bt_adapter_unset_state_changed_cb();
177 if (BT_ERROR_NONE != ret)
179 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
180 return CA_STATUS_FAILED;
183 ret = bt_deinitialize();
184 if (BT_ERROR_NONE != ret)
186 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
187 return CA_STATUS_FAILED;
193 CAResult_t CAGetLEAdapterState()
195 OIC_LOG(DEBUG, TAG, "IN");
197 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
199 //Get Bluetooth adapter state
200 int ret = bt_adapter_get_state(&adapterState);
201 if (BT_ERROR_NONE != ret)
203 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
205 return CA_STATUS_FAILED;
208 if (BT_ADAPTER_ENABLED != adapterState)
210 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
211 return CA_ADAPTER_NOT_ENABLED;
214 OIC_LOG(DEBUG, TAG, "OUT");
218 CAResult_t CAGetLEAddress(char **local_address)
220 OIC_LOG(DEBUG, TAG, "IN");
222 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
224 char *address = NULL;
226 int ret = bt_adapter_get_address(&address);
227 if (BT_ERROR_NONE != ret || !address)
229 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
231 return CA_STATUS_FAILED;
234 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
236 *local_address = address;
238 OIC_LOG(DEBUG, TAG, "OUT");
243 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
245 OIC_LOG(DEBUG, TAG, "IN");
246 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
247 g_bleDeviceStateChangedCallback = callback;
248 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
249 OIC_LOG(DEBUG, TAG, "OUT");
253 CAResult_t CAUnSetLEAdapterStateChangedCb()
255 OIC_LOG(DEBUG, TAG, "IN");
256 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
257 g_bleDeviceStateChangedCallback = NULL;
258 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
259 OIC_LOG(DEBUG, TAG, "OUT");
263 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
265 OIC_LOG(DEBUG, TAG, "IN");
266 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
267 g_bleConnectionStateChangedCallback = callback;
268 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
269 OIC_LOG(DEBUG, TAG, "OUT");
273 CAResult_t CAUnsetLENWConnectionStateChangedCb()
275 OIC_LOG(DEBUG, TAG, "IN");
276 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
277 g_bleConnectionStateChangedCallback = NULL;
278 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
279 OIC_LOG(DEBUG, TAG, "OUT");
283 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
286 OIC_LOG(DEBUG, TAG, "IN");
288 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
290 if (NULL == g_bleDeviceStateChangedCallback)
292 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
293 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
297 if (BT_ADAPTER_DISABLED == adapter_state)
299 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
300 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
301 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
305 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
307 int ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
308 if (BT_ERROR_NONE != ret)
310 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
314 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
315 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
317 OIC_LOG(DEBUG, TAG, "OUT");
320 void CALENWConnectionStateChangedCb(int result, bool connected,
321 const char *remoteAddress, void *userData)
323 OIC_LOG(DEBUG, TAG, "IN");
325 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
327 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
328 char *addr = OICStrdup(remoteAddress);
331 OIC_LOG(ERROR, TAG, "addr is NULL");
332 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
335 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
337 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
339 OIC_LOG(DEBUG, TAG, "OUT");