Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / tizen / calenwmonitor.c
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include "caleinterface.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <arpa/inet.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31
32 #include <bluetooth.h>
33 #include <bluetooth_internal.h>
34 #include <bluetooth_type.h>
35
36 #include "camutex.h"
37 #include "caleadapter.h"
38 #include "caadapterutils.h"
39 #include "oic_string.h"
40 #include "oic_malloc.h"
41
42 /**
43  * Logging tag for module name
44  */
45 #define TAG "OIC_CA_LE_MONITOR"
46
47 static GMainLoop *g_mainloop = NULL;
48 static ca_thread_pool_t g_threadPoolHandle = NULL;
49
50 /**
51  * Maintains the callback to be notified on device state changed.
52  */
53 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
54
55 /**
56  * Maintains the callback to be notified on device state changed.
57  */
58 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
59
60 /**
61  * Mutex to synchronize access to the deviceStateChanged Callback when the state
62  *           of the LE adapter gets change.
63  */
64 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
65
66 /**
67  * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
68  * of the LE adapter gets change.
69  */
70 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
71
72 /**
73 * This is the callback which will be called when the adapter state gets changed.
74 *
75 * @param result         [IN] Result of the query done to the platform.
76 * @param adapter_state  [IN] State of the LE adapter.
77 * @param user_data      [IN] Any user_data passed by the caller when querying for the state changed cb.
78 *
79 * @return  None.
80 */
81 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
82                                void *user_data);
83
84 /**
85 * This is the callback which will be called when the connection state gets changed.
86 *
87 * @param result         [IN] Result of the query done to the platform.
88 * @param connected      [IN] State of connection.
89 * @param remoteAddress  [IN] LE address of the device to be notified.
90 * @param user_data      [IN] Any user_data passed by the caller when querying for the state changed cb.
91 *
92 * @return  None.
93 */
94 void CALENWConnectionStateChangedCb(int result, bool connected,
95                                     const char *remoteAddress, void *userData);
96
97 void CALEMainLoopThread(void *param)
98 {
99     g_main_loop_run(g_mainloop);
100 }
101
102 CAResult_t CAInitializeLENetworkMonitor()
103 {
104     OIC_LOG(DEBUG, TAG, "IN");
105
106     if (NULL == g_bleDeviceStateChangedCbMutex)
107     {
108         g_bleDeviceStateChangedCbMutex = ca_mutex_new();
109         if (NULL == g_bleDeviceStateChangedCbMutex)
110         {
111             OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
112             return CA_STATUS_FAILED;
113         }
114     }
115
116     if (NULL == g_bleConnectionStateChangedCbMutex)
117     {
118         g_bleConnectionStateChangedCbMutex = ca_mutex_new();
119         if (NULL == g_bleConnectionStateChangedCbMutex)
120         {
121             OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
122             ca_mutex_free(g_bleDeviceStateChangedCbMutex);
123             return CA_STATUS_FAILED;
124         }
125     }
126     OIC_LOG(DEBUG, TAG, "OUT");
127
128     return CA_STATUS_OK;
129 }
130
131 void CATerminateLENetworkMonitor()
132 {
133     OIC_LOG(DEBUG, TAG, "IN");
134
135     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
136     g_bleDeviceStateChangedCbMutex = NULL;
137
138     ca_mutex_free(g_bleConnectionStateChangedCbMutex);
139     g_bleConnectionStateChangedCbMutex = NULL;
140     OIC_LOG(DEBUG, TAG, "OUT");
141 }
142
143 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
144 {
145     OIC_LOG(DEBUG, TAG, "IN");
146     g_threadPoolHandle = threadPool;
147     OIC_LOG(DEBUG, TAG, "OUT");
148     return CA_STATUS_OK;
149 }
150
151 CAResult_t CAStartLEAdapter()
152 {
153     OIC_LOG(DEBUG, TAG, "IN");
154     g_mainloop = g_main_loop_new(NULL, 0);
155     if(!g_mainloop)
156     {
157         OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
158         return CA_STATUS_FAILED;
159     }
160
161     if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread, (void *) NULL))
162     {
163         OIC_LOG(ERROR, TAG, "Failed to create thread!");
164         return CA_STATUS_FAILED;
165     }
166
167     int ret = bt_initialize();
168     if (0 != ret)
169     {
170         OIC_LOG(ERROR, TAG, "bt_initialize failed");
171         return CA_STATUS_FAILED;
172     }
173
174     ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
175     if (0 != ret)
176     {
177         OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
178         return CA_STATUS_FAILED;
179     }
180
181     ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
182     if (BT_ERROR_NONE != ret)
183     {
184         OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
185         return CA_STATUS_FAILED;
186     }
187
188     ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
189     if (BT_ERROR_NONE != ret)
190     {
191         OIC_LOG_V(ERROR, TAG,
192                   "bt_gatt_set_connection_state_changed_cb has failed");
193         return CA_STATUS_FAILED;
194     }
195
196     OIC_LOG(DEBUG, TAG, "OUT");
197     return CA_STATUS_OK;
198 }
199
200 CAResult_t CAStopLEAdapter()
201 {
202
203     int ret = bt_adapter_unset_state_changed_cb();
204     if (BT_ERROR_NONE != ret)
205     {
206         OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
207         return CA_STATUS_FAILED;
208     }
209
210     ret = bt_deinitialize();
211     if (0 != ret)
212     {
213         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
214         return CA_STATUS_FAILED;
215     }
216
217     if (g_mainloop)
218     {
219         g_main_loop_quit(g_mainloop);
220     }
221     return CA_STATUS_OK;
222 }
223
224 CAResult_t CAGetLEAdapterState()
225 {
226     OIC_LOG(DEBUG, TAG, "IN");
227
228     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
229
230     //Get Bluetooth adapter state
231     int ret = bt_adapter_get_state(&adapterState);
232     if (BT_ERROR_NONE != ret)
233     {
234         OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
235                   ret);
236         return CA_STATUS_FAILED;
237     }
238
239     if (BT_ADAPTER_ENABLED != adapterState)
240     {
241         OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
242         return CA_ADAPTER_NOT_ENABLED;
243     }
244
245     OIC_LOG(DEBUG, TAG, "OUT");
246     return CA_STATUS_OK;
247 }
248
249 CAResult_t CAGetLEAddress(char **local_address)
250 {
251     OIC_LOG(DEBUG, TAG, "IN");
252
253     VERIFY_NON_NULL(local_address, TAG, "local_address is null")
254
255     char *address = NULL;
256
257     int ret = bt_adapter_get_address(&address);
258     if (BT_ERROR_NONE != ret || !address)
259     {
260         OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
261                   ret);
262         return CA_STATUS_FAILED;
263     }
264
265     OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
266
267     *local_address = address;
268
269     OIC_LOG(DEBUG, TAG, "OUT");
270
271     return CA_STATUS_OK;
272 }
273
274 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
275 {
276     OIC_LOG(DEBUG, TAG, "IN");
277     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
278     g_bleDeviceStateChangedCallback = callback;
279     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
280     OIC_LOG(DEBUG, TAG, "OUT");
281     return CA_STATUS_OK;
282 }
283
284 CAResult_t CAUnSetLEAdapterStateChangedCb()
285 {
286     OIC_LOG(DEBUG, TAG, "IN");
287     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
288     g_bleDeviceStateChangedCallback = NULL;
289     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
290     OIC_LOG(DEBUG, TAG, "OUT");
291     return CA_STATUS_OK;
292 }
293
294 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
295 {
296     OIC_LOG(DEBUG, TAG, "IN");
297     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
298     g_bleConnectionStateChangedCallback = callback;
299     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
300     OIC_LOG(DEBUG, TAG, "OUT");
301     return CA_STATUS_OK;
302 }
303
304 CAResult_t CAUnsetLENWConnectionStateChangedCb()
305 {
306     OIC_LOG(DEBUG, TAG, "IN");
307     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
308     g_bleConnectionStateChangedCallback = NULL;
309     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
310     OIC_LOG(DEBUG, TAG, "OUT");
311     return CA_STATUS_OK;
312 }
313
314 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
315                                           void *user_data)
316 {
317     OIC_LOG(DEBUG, TAG, "IN");
318
319     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
320
321     if (NULL == g_bleDeviceStateChangedCallback)
322     {
323         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
324         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
325         return;
326     }
327
328     if (BT_ADAPTER_DISABLED == adapter_state)
329     {
330         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
331         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
332         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
333         return;
334     }
335
336     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
337     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
338     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
339
340     OIC_LOG(DEBUG, TAG, "OUT");
341 }
342
343 void CALENWConnectionStateChangedCb(int result, bool connected,
344                                     const char *remoteAddress, void *userData)
345 {
346     OIC_LOG(DEBUG, TAG, "IN");
347
348     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
349
350     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
351     char *addr = OICStrdup(remoteAddress);
352     if (NULL == addr)
353     {
354         OIC_LOG(ERROR, TAG, "addr is NULL");
355         ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
356         return;
357     }
358     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
359     OICFree(addr);
360     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
361
362     OIC_LOG(DEBUG, TAG, "OUT");
363 }