replace : iotivity -> iotivity-sec
[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 "octhread.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 oc_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 oc_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] 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 void CALEMainLoopThread(void *param)
85 {
86     g_main_loop_run(g_mainloop);
87 }
88
89 /**
90 * This is the callback which will be called when the connection state gets changed.
91 *
92 * @param result         [IN] Result of the query done to the platform.
93 * @param connected      [IN] State of connection.
94 * @param remoteAddress  [IN] LE address of the device to be notified.
95 * @param user_data      [IN] User data passed by the caller when querying for the state changed cb.
96 *
97 * @return  None.
98 */
99 void CALENWConnectionStateChangedCb(int result, bool connected,
100                                     const char *remoteAddress, void *userData);
101
102 CAResult_t CAInitializeLENetworkMonitor()
103 {
104     OIC_LOG(DEBUG, TAG, "IN");
105
106     if (NULL == g_bleDeviceStateChangedCbMutex)
107     {
108         g_bleDeviceStateChangedCbMutex = oc_mutex_new();
109         if (NULL == g_bleDeviceStateChangedCbMutex)
110         {
111             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
112             return CA_STATUS_FAILED;
113         }
114     }
115
116     if (NULL == g_bleConnectionStateChangedCbMutex)
117     {
118         g_bleConnectionStateChangedCbMutex = oc_mutex_new();
119         if (NULL == g_bleConnectionStateChangedCbMutex)
120         {
121             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
122             oc_mutex_free(g_bleDeviceStateChangedCbMutex);
123             return CA_STATUS_FAILED;
124         }
125     }
126
127     OIC_LOG(DEBUG, TAG, "OUT");
128
129     return CA_STATUS_OK;
130 }
131
132 void CATerminateLENetworkMonitor()
133 {
134     OIC_LOG(DEBUG, TAG, "IN");
135
136     oc_mutex_free(g_bleDeviceStateChangedCbMutex);
137     g_bleDeviceStateChangedCbMutex = NULL;
138
139     oc_mutex_free(g_bleConnectionStateChangedCbMutex);
140     g_bleConnectionStateChangedCbMutex = NULL;
141
142     OIC_LOG(DEBUG, TAG, "OUT");
143 }
144
145 CAResult_t CAInitializeLEAdapter()
146 {
147     OIC_LOG(DEBUG, TAG, "IN");
148     CAResult_t res = ca_thread_pool_init(2, &g_threadPoolHandle);
149     if (CA_STATUS_OK != res)
150     {
151         OIC_LOG(ERROR, TAG, "thread pool initialize error.");
152         return res;
153     }
154
155     OIC_LOG(DEBUG, TAG, "OUT");
156     return CA_STATUS_OK;
157 }
158
159 CAResult_t CAStartLEAdapter()
160 {
161     OIC_LOG(DEBUG, TAG, "IN");
162
163     g_mainloop = g_main_loop_new(NULL, 0);
164     if(!g_mainloop)
165     {
166         OIC_LOG(ERROR, TAG, "g_main_loop_new failed\n");
167         return CA_STATUS_FAILED;
168     }
169
170     if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CALEMainLoopThread,
171                                                 (void *) NULL, NULL))
172     {
173         OIC_LOG(ERROR, TAG, "Failed to create thread!");
174         return CA_STATUS_FAILED;
175     }
176
177     int ret = bt_initialize();
178     if (BT_ERROR_NONE != ret)
179     {
180         OIC_LOG(ERROR, TAG, "bt_initialize failed");
181         return CA_STATUS_FAILED;
182     }
183
184     ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
185     if (BT_ERROR_NONE != ret)
186     {
187         OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
188         return CA_STATUS_FAILED;
189     }
190
191     ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
192     if (BT_ERROR_NONE != ret)
193     {
194         OIC_LOG_V(ERROR, TAG,
195                   "bt_gatt_set_connection_state_changed_cb has failed");
196         return CA_STATUS_FAILED;
197     }
198
199     OIC_LOG(DEBUG, TAG, "OUT");
200     return CA_STATUS_OK;
201 }
202
203 CAResult_t CAStopLEAdapter()
204 {
205
206     int ret = bt_adapter_unset_state_changed_cb();
207     if (BT_ERROR_NONE != ret)
208     {
209         OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
210         return CA_STATUS_FAILED;
211     }
212
213     ret = bt_deinitialize();
214     if (BT_ERROR_NONE != ret)
215     {
216         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
217         return CA_STATUS_FAILED;
218     }
219
220     if (g_mainloop)
221     {
222         g_main_loop_quit(g_mainloop);
223     }
224
225     ca_thread_pool_free(g_threadPoolHandle);
226     g_threadPoolHandle = NULL;
227     return CA_STATUS_OK;
228 }
229
230 CAResult_t CAGetLEAdapterState()
231 {
232     OIC_LOG(DEBUG, TAG, "IN");
233
234     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
235
236     //Get Bluetooth adapter state
237     int ret = bt_adapter_get_state(&adapterState);
238     if (BT_ERROR_NONE != ret)
239     {
240         OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
241                   ret);
242         return CA_STATUS_FAILED;
243     }
244
245     if (BT_ADAPTER_ENABLED != adapterState)
246     {
247         OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
248         return CA_ADAPTER_NOT_ENABLED;
249     }
250
251     OIC_LOG(DEBUG, TAG, "OUT");
252     return CA_STATUS_OK;
253 }
254
255 CAResult_t CAGetLEAddress(char **local_address)
256 {
257     OIC_LOG(DEBUG, TAG, "IN");
258
259     VERIFY_NON_NULL(local_address, TAG, "local_address is null")
260
261     char *address = NULL;
262
263     int ret = bt_adapter_get_address(&address);
264     if (BT_ERROR_NONE != ret || !address)
265     {
266         OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
267                   ret);
268         return CA_STATUS_FAILED;
269     }
270
271     OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
272
273     *local_address = address;
274
275     OIC_LOG(DEBUG, TAG, "OUT");
276
277     return CA_STATUS_OK;
278 }
279
280 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
281 {
282     OIC_LOG(DEBUG, TAG, "IN");
283     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
284     g_bleDeviceStateChangedCallback = callback;
285     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
286     OIC_LOG(DEBUG, TAG, "OUT");
287     return CA_STATUS_OK;
288 }
289
290 CAResult_t CAUnSetLEAdapterStateChangedCb()
291 {
292     OIC_LOG(DEBUG, TAG, "IN");
293     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
294     g_bleDeviceStateChangedCallback = NULL;
295     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
296     OIC_LOG(DEBUG, TAG, "OUT");
297     return CA_STATUS_OK;
298 }
299
300 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
301 {
302     OIC_LOG(DEBUG, TAG, "IN");
303     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
304     g_bleConnectionStateChangedCallback = callback;
305     oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
306     OIC_LOG(DEBUG, TAG, "OUT");
307     return CA_STATUS_OK;
308 }
309
310 CAResult_t CAUnSetLENWConnectionStateChangedCb()
311 {
312     OIC_LOG(DEBUG, TAG, "IN");
313     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
314     g_bleConnectionStateChangedCallback = NULL;
315     oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
316     OIC_LOG(DEBUG, TAG, "OUT");
317     return CA_STATUS_OK;
318 }
319
320 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
321                                           void *user_data)
322 {
323     OIC_LOG(DEBUG, TAG, "IN");
324
325     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
326
327     if (NULL == g_bleDeviceStateChangedCallback)
328     {
329         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
330         oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
331         return;
332     }
333
334     if (BT_ADAPTER_DISABLED == adapter_state)
335     {
336         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
337         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
338         oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
339         return;
340     }
341
342     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
343
344     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
345     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
346
347     OIC_LOG(DEBUG, TAG, "OUT");
348 }
349
350 void CALENWConnectionStateChangedCb(int result, bool connected,
351                                     const char *remoteAddress, void *userData)
352 {
353     OIC_LOG(DEBUG, TAG, "IN");
354
355     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
356
357     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
358     char *addr = OICStrdup(remoteAddress);
359     if (NULL == addr)
360     {
361         OIC_LOG(ERROR, TAG, "addr is NULL");
362         oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
363         return;
364     }
365     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
366     OICFree(addr);
367     oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
368
369     OIC_LOG(DEBUG, TAG, "OUT");
370 }