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_VD"
48 * Maintains the callback to be notified on device state changed.
50 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
53 * Maintains the callback to be notified on device state changed.
55 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
58 * Mutex to synchronize access to the deviceStateChanged Callback when the state
59 * of the LE adapter gets change.
61 static oc_mutex g_bleDeviceStateChangedCbMutex = NULL;
64 * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
65 * of the LE adapter gets change.
67 static oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
70 * This is the callback which will be called when the adapter state gets changed.
72 * @param result [IN] Result of the query done to the platform.
73 * @param adapter_state [IN] State of the LE adapter.
74 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
78 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
82 * This is the callback which will be called when the connection state gets changed.
84 * @param result [IN] Result of the query done to the platform.
85 * @param connected [IN] State of connection.
86 * @param remoteAddress [IN] LE address of the device to be notified.
87 * @param user_data [IN] User data passed by the caller when querying for the state changed cb.
91 void CALENWConnectionStateChangedCb(int result, bool connected,
92 const char *remoteAddress, void *userData);
94 CAResult_t CAInitializeLENetworkMonitor()
96 OIC_LOG(DEBUG, TAG, "IN");
98 if (NULL == g_bleDeviceStateChangedCbMutex)
100 g_bleDeviceStateChangedCbMutex = oc_mutex_new();
101 if (NULL == g_bleDeviceStateChangedCbMutex)
103 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
104 return CA_STATUS_FAILED;
108 if (NULL == g_bleConnectionStateChangedCbMutex)
110 g_bleConnectionStateChangedCbMutex = oc_mutex_new();
111 if (NULL == g_bleConnectionStateChangedCbMutex)
113 OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
114 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
115 return CA_STATUS_FAILED;
119 OIC_LOG(DEBUG, TAG, "OUT");
124 void CATerminateLENetworkMonitor()
126 OIC_LOG(DEBUG, TAG, "IN");
128 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
129 g_bleDeviceStateChangedCbMutex = NULL;
131 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
132 g_bleConnectionStateChangedCbMutex = NULL;
134 OIC_LOG(DEBUG, TAG, "OUT");
137 CAResult_t CAInitializeLEAdapter()
139 OIC_LOG(DEBUG, TAG, "IN");
140 OIC_LOG(DEBUG, TAG, "OUT");
144 CAResult_t CAStartLEAdapter()
146 OIC_LOG(DEBUG, TAG, "IN");
148 int ret = bt_initialize();
149 if (BT_ERROR_NONE != ret)
151 OIC_LOG(ERROR, TAG, "bt_initialize failed");
152 return CA_STATUS_FAILED;
155 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
156 if (BT_ERROR_NONE != ret)
158 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
159 return CA_STATUS_FAILED;
162 ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
163 if (BT_ERROR_NONE != ret)
165 OIC_LOG_V(ERROR, TAG,
166 "bt_gatt_set_connection_state_changed_cb has failed");
167 return CA_STATUS_FAILED;
170 OIC_LOG(DEBUG, TAG, "OUT");
174 CAResult_t CAStopLEAdapter()
177 int ret = bt_adapter_unset_state_changed_cb();
178 if (BT_ERROR_NONE != ret)
180 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
181 return CA_STATUS_FAILED;
184 ret = bt_deinitialize();
185 if (BT_ERROR_NONE != ret)
187 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
188 return CA_STATUS_FAILED;
194 CAResult_t CAGetLEAdapterState()
196 OIC_LOG(DEBUG, TAG, "IN");
198 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
200 //Get Bluetooth adapter state
201 int ret = bt_adapter_get_state(&adapterState);
202 if (BT_ERROR_NONE != ret)
204 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
206 return CA_STATUS_FAILED;
209 if (BT_ADAPTER_ENABLED != adapterState)
211 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
212 return CA_ADAPTER_NOT_ENABLED;
215 OIC_LOG(DEBUG, TAG, "OUT");
219 CAResult_t CAGetLEAddress(char **local_address)
221 OIC_LOG(DEBUG, TAG, "IN");
223 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
225 char *address = NULL;
227 int ret = bt_adapter_get_address(&address);
228 if (BT_ERROR_NONE != ret || !address)
230 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
232 return CA_STATUS_FAILED;
235 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
237 *local_address = address;
239 OIC_LOG(DEBUG, TAG, "OUT");
244 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
246 OIC_LOG(DEBUG, TAG, "IN");
247 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
248 g_bleDeviceStateChangedCallback = callback;
249 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
250 OIC_LOG(DEBUG, TAG, "OUT");
254 CAResult_t CAUnSetLEAdapterStateChangedCb()
256 OIC_LOG(DEBUG, TAG, "IN");
257 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
258 g_bleDeviceStateChangedCallback = NULL;
259 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
260 OIC_LOG(DEBUG, TAG, "OUT");
264 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
266 OIC_LOG(DEBUG, TAG, "IN");
267 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
268 g_bleConnectionStateChangedCallback = callback;
269 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
270 OIC_LOG(DEBUG, TAG, "OUT");
274 CAResult_t CAUnSetLENWConnectionStateChangedCb()
276 OIC_LOG(DEBUG, TAG, "IN");
277 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
278 g_bleConnectionStateChangedCallback = NULL;
279 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
280 OIC_LOG(DEBUG, TAG, "OUT");
284 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
290 OIC_LOG(DEBUG, TAG, "IN");
292 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
294 if (NULL == g_bleDeviceStateChangedCallback)
296 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
297 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
301 if (BT_ADAPTER_DISABLED == adapter_state)
303 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
304 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
305 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
309 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
311 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
312 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
314 OIC_LOG(DEBUG, TAG, "OUT");
317 void CALENWConnectionStateChangedCb(int result, bool connected,
318 const char *remoteAddress, void *userData)
323 OIC_LOG(DEBUG, TAG, "IN");
325 VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
327 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
328 char *addr = OICStrdup(remoteAddress);
331 OIC_LOG(ERROR, TAG, "addr is NULL");
332 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
335 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
337 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
339 OIC_LOG(DEBUG, TAG, "OUT");