2e2156b53c8c30019a93f1d1b91d2a4047c64ac2
[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 <arpa/inet.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30
31 #include <bluetooth.h>
32 #include <bluetooth_internal.h>
33 #include <bluetooth_type.h>
34
35 #include "camutex.h"
36 #include "caleadapter.h"
37 #include "caadapterutils.h"
38 #include "oic_string.h"
39 #include "oic_malloc.h"
40
41 /**
42  * Logging tag for module name
43  */
44 #define TAG "OIC_CA_LE_MONITOR"
45
46 /**
47  * Maintains the callback to be notified on device state changed.
48  */
49 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
50
51 /**
52  * Maintains the callback to be notified on device state changed.
53  */
54 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
55
56 /**
57  * Mutex to synchronize access to the deviceStateChanged Callback when the state
58  *           of the LE adapter gets change.
59  */
60 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
61
62 /**
63  * Mutex to synchronize access to the ConnectionStateChanged Callback when the state
64  * of the LE adapter gets change.
65  */
66 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
67
68 /**
69 * This is the callback which will be called when the adapter state gets changed.
70 *
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.
74 *
75 * @return  None.
76 */
77 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
78                                void *user_data);
79
80 /**
81 * This is the callback which will be called when the connection state gets changed.
82 *
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.
87 *
88 * @return  None.
89 */
90 void CALENWConnectionStateChangedCb(int result, bool connected,
91                                     const char *remoteAddress, void *userData);
92
93 CAResult_t CAInitializeLENetworkMonitor()
94 {
95     OIC_LOG(DEBUG, TAG, "IN");
96
97     if (NULL == g_bleDeviceStateChangedCbMutex)
98     {
99         g_bleDeviceStateChangedCbMutex = ca_mutex_new();
100         if (NULL == g_bleDeviceStateChangedCbMutex)
101         {
102             OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
103             return CA_STATUS_FAILED;
104         }
105     }
106
107     if (NULL == g_bleConnectionStateChangedCbMutex)
108     {
109         g_bleConnectionStateChangedCbMutex = ca_mutex_new();
110         if (NULL == g_bleConnectionStateChangedCbMutex)
111         {
112             OIC_LOG(ERROR, TAG, "ca_mutex_new failed");
113             ca_mutex_free(g_bleDeviceStateChangedCbMutex);
114             return CA_STATUS_FAILED;
115         }
116     }
117
118     OIC_LOG(DEBUG, TAG, "OUT");
119
120     return CA_STATUS_OK;
121 }
122
123 void CATerminateLENetworkMonitor()
124 {
125     OIC_LOG(DEBUG, TAG, "IN");
126
127     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
128     g_bleDeviceStateChangedCbMutex = NULL;
129
130     ca_mutex_free(g_bleConnectionStateChangedCbMutex);
131     g_bleConnectionStateChangedCbMutex = NULL;
132
133     OIC_LOG(DEBUG, TAG, "OUT");
134 }
135
136 CAResult_t CAInitializeLEAdapter()
137 {
138     OIC_LOG(DEBUG, TAG, "IN");
139     OIC_LOG(DEBUG, TAG, "OUT");
140     return CA_STATUS_OK;
141 }
142
143 CAResult_t CAStartLEAdapter()
144 {
145     OIC_LOG(DEBUG, TAG, "IN");
146
147     int ret = bt_initialize();
148     if (BT_ERROR_NONE != ret)
149     {
150         OIC_LOG(ERROR, TAG, "bt_initialize failed");
151         return CA_STATUS_FAILED;
152     }
153
154     ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
155     if (BT_ERROR_NONE != ret)
156     {
157         OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
158         return CA_STATUS_FAILED;
159     }
160
161     ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
162     if (BT_ERROR_NONE != ret)
163     {
164         OIC_LOG_V(ERROR, TAG,
165                   "bt_gatt_set_connection_state_changed_cb has failed");
166         return CA_STATUS_FAILED;
167     }
168
169     OIC_LOG(DEBUG, TAG, "OUT");
170     return CA_STATUS_OK;
171 }
172
173 CAResult_t CAStopLEAdapter()
174 {
175
176     int ret = bt_adapter_unset_state_changed_cb();
177     if (BT_ERROR_NONE != ret)
178     {
179         OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
180         return CA_STATUS_FAILED;
181     }
182
183     ret = bt_deinitialize();
184     if (BT_ERROR_NONE != ret)
185     {
186         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
187         return CA_STATUS_FAILED;
188     }
189
190     return CA_STATUS_OK;
191 }
192
193 CAResult_t CAGetLEAdapterState()
194 {
195     OIC_LOG(DEBUG, TAG, "IN");
196
197     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
198
199     //Get Bluetooth adapter state
200     int ret = bt_adapter_get_state(&adapterState);
201     if (BT_ERROR_NONE != ret)
202     {
203         OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
204                   ret);
205         return CA_STATUS_FAILED;
206     }
207
208     if (BT_ADAPTER_ENABLED != adapterState)
209     {
210         OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
211         return CA_ADAPTER_NOT_ENABLED;
212     }
213
214     OIC_LOG(DEBUG, TAG, "OUT");
215     return CA_STATUS_OK;
216 }
217
218 CAResult_t CAGetLEAddress(char **local_address)
219 {
220     OIC_LOG(DEBUG, TAG, "IN");
221
222     VERIFY_NON_NULL(local_address, TAG, "local_address is null")
223
224     char *address = NULL;
225
226     int ret = bt_adapter_get_address(&address);
227     if (BT_ERROR_NONE != ret || !address)
228     {
229         OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
230                   ret);
231         return CA_STATUS_FAILED;
232     }
233
234     OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
235
236     *local_address = address;
237
238     OIC_LOG(DEBUG, TAG, "OUT");
239
240     return CA_STATUS_OK;
241 }
242
243 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
244 {
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");
250     return CA_STATUS_OK;
251 }
252
253 CAResult_t CAUnSetLEAdapterStateChangedCb()
254 {
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");
260     return CA_STATUS_OK;
261 }
262
263 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
264 {
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");
270     return CA_STATUS_OK;
271 }
272
273 CAResult_t CAUnsetLENWConnectionStateChangedCb()
274 {
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");
280     return CA_STATUS_OK;
281 }
282
283 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
284                                           void *user_data)
285 {
286     OIC_LOG(DEBUG, TAG, "IN");
287
288     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
289
290     if (NULL == g_bleDeviceStateChangedCallback)
291     {
292         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
293         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
294         return;
295     }
296
297     if (BT_ADAPTER_DISABLED == adapter_state)
298     {
299         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
300         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
301         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
302         return;
303     }
304
305     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
306
307     int ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
308     if (BT_ERROR_NONE != ret)
309     {
310         OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
311         return;
312     }
313
314     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
315     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
316
317     OIC_LOG(DEBUG, TAG, "OUT");
318 }
319
320 void CALENWConnectionStateChangedCb(int result, bool connected,
321                                     const char *remoteAddress, void *userData)
322 {
323     OIC_LOG(DEBUG, TAG, "IN");
324
325     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
326
327     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
328     char *addr = OICStrdup(remoteAddress);
329     if (NULL == addr)
330     {
331         OIC_LOG(ERROR, TAG, "addr is NULL");
332         ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
333         return;
334     }
335     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
336     OICFree(addr);
337     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
338
339     OIC_LOG(DEBUG, TAG, "OUT");
340 }