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