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 * Mutex to synchronize access to the deviceStateChanged Callback when the state
56 * of the LE adapter gets change.
58 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
61 * This is the callback which will be called when the adapter state gets changed.
63 * @param result [IN] Result of the query done to the platform.
64 * @param adapter_state [IN] State of the LE adapter.
65 * @param user_data [IN] Any user_data passed by the caller when querying for the state changed cb.
69 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
72 void *CALEMainLoopThread (void *param)
74 g_main_loop_run(g_mainloop);
78 CAResult_t CAInitializeLENetworkMonitor()
80 OIC_LOG(DEBUG, TAG, "IN");
82 if (NULL == g_bleDeviceStateChangedCbMutex)
84 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
85 if (NULL == g_bleDeviceStateChangedCbMutex)
87 OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
88 return CA_STATUS_FAILED;
91 OIC_LOG(DEBUG, TAG, "OUT");
96 void CATerminateLENetworkMonitor()
98 OIC_LOG(DEBUG, TAG, "IN");
100 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
101 g_bleDeviceStateChangedCbMutex = NULL;
103 OIC_LOG(DEBUG, TAG, "OUT");
106 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
108 OIC_LOG(DEBUG, TAG, "IN");
109 g_threadPoolHandle = threadPool;
110 OIC_LOG(DEBUG, TAG, "OUT");
114 CAResult_t CAStartLEAdapter()
116 OIC_LOG(DEBUG, TAG, "IN");
117 g_mainloop = g_main_loop_new(NULL, 0);
120 OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
121 return CA_STATUS_FAILED;
124 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
126 OIC_LOG(ERROR, TAG, "Failed to create thread!");
127 return CA_STATUS_FAILED;
130 int ret = bt_initialize();
133 OIC_LOG(ERROR, TAG, "bt_initialize failed");
134 return CA_STATUS_FAILED;
137 ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
140 OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
141 return CA_STATUS_FAILED;
144 ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
145 if (BT_ERROR_NONE != ret)
147 OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
148 return CA_STATUS_FAILED;
151 OIC_LOG(DEBUG, TAG, "OUT");
155 CAResult_t CAStopLEAdapter()
158 int ret = bt_adapter_unset_state_changed_cb();
159 if (BT_ERROR_NONE != ret)
161 OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
162 return CA_STATUS_FAILED;
165 ret = bt_deinitialize();
168 OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
169 return CA_STATUS_FAILED;
174 g_main_loop_quit(g_mainloop);
179 CAResult_t CAGetLEAdapterState()
181 OIC_LOG(DEBUG, TAG, "IN");
183 bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
185 //Get Bluetooth adapter state
186 int ret = bt_adapter_get_state(&adapterState);
187 if (BT_ERROR_NONE != ret)
189 OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
191 return CA_STATUS_FAILED;
194 if (BT_ADAPTER_ENABLED != adapterState)
196 OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
197 return CA_ADAPTER_NOT_ENABLED;
200 OIC_LOG(DEBUG, TAG, "OUT");
204 CAResult_t CAGetLEAddress(char **local_address)
206 OIC_LOG(DEBUG, TAG, "IN");
208 VERIFY_NON_NULL(local_address, TAG, "local_address is null")
210 char *address = NULL;
212 int ret = bt_adapter_get_address(&address);
213 if (BT_ERROR_NONE != ret || !address)
215 OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
217 return CA_STATUS_FAILED;
220 OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
222 *local_address = address;
224 OIC_LOG(DEBUG, TAG, "OUT");
229 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
231 OIC_LOG(DEBUG, TAG, "IN");
232 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
233 g_bleDeviceStateChangedCallback = callback;
234 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
235 OIC_LOG(DEBUG, TAG, "OUT");
239 CAResult_t CAUnSetLEAdapterStateChangedCb()
241 OIC_LOG(DEBUG, TAG, "IN");
242 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
243 g_bleDeviceStateChangedCallback = NULL;
244 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
245 OIC_LOG(DEBUG, TAG, "OUT");
249 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
252 OIC_LOG(DEBUG, TAG, "IN");
254 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
256 if (NULL == g_bleDeviceStateChangedCallback)
258 OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
259 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
263 if (BT_ADAPTER_DISABLED == adapter_state)
265 OIC_LOG(DEBUG, TAG, "Adapter is disabled");
266 g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
267 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
271 OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
272 g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
273 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
275 OIC_LOG(DEBUG, TAG, "OUT");