Use `URI` all caps in documentation
[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 "octhread.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 oc_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 oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
67
68 /**
69 * This is the callback which will be called when the adapter state gets changed.
70 *
71 * @param[in] result         Result of the query done to the platform.
72 * @param[in] adapter_state  State of the LE adapter.
73 * @param[in] user_data      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[in] result         Result of the query done to the platform.
84 * @param[in] connected      State of connection.
85 * @param[in] remoteAddress  LE address of the device to be notified.
86 * @param[in] user_data      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(void)
94 {
95     OIC_LOG(DEBUG, TAG, "IN");
96
97     if (NULL == g_bleDeviceStateChangedCbMutex)
98     {
99         g_bleDeviceStateChangedCbMutex = oc_mutex_new();
100         if (NULL == g_bleDeviceStateChangedCbMutex)
101         {
102             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
103             return CA_STATUS_FAILED;
104         }
105     }
106
107     if (NULL == g_bleConnectionStateChangedCbMutex)
108     {
109         g_bleConnectionStateChangedCbMutex = oc_mutex_new();
110         if (NULL == g_bleConnectionStateChangedCbMutex)
111         {
112             OIC_LOG(ERROR, TAG, "oc_mutex_new failed");
113             oc_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(void)
124 {
125     OIC_LOG(DEBUG, TAG, "IN");
126
127     oc_mutex_free(g_bleDeviceStateChangedCbMutex);
128     g_bleDeviceStateChangedCbMutex = NULL;
129
130     oc_mutex_free(g_bleConnectionStateChangedCbMutex);
131     g_bleConnectionStateChangedCbMutex = NULL;
132
133     OIC_LOG(DEBUG, TAG, "OUT");
134 }
135
136 CAResult_t CAInitializeLEAdapter(void)
137 {
138     OIC_LOG(DEBUG, TAG, "IN");
139     OIC_LOG(DEBUG, TAG, "OUT");
140     return CA_STATUS_OK;
141 }
142
143 CAResult_t CAStartLEAdapter(void)
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(void)
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(void)
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     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
247     g_bleDeviceStateChangedCallback = callback;
248     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
249     OIC_LOG(DEBUG, TAG, "OUT");
250     return CA_STATUS_OK;
251 }
252
253 CAResult_t CAUnSetLEAdapterStateChangedCb(void)
254 {
255     OIC_LOG(DEBUG, TAG, "IN");
256     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
257     g_bleDeviceStateChangedCallback = NULL;
258     oc_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     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
267     g_bleConnectionStateChangedCallback = callback;
268     oc_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     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
277     g_bleConnectionStateChangedCallback = NULL;
278     oc_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     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
289
290     if (NULL == g_bleDeviceStateChangedCallback)
291     {
292         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
293         oc_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         oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
302         return;
303     }
304
305     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
306
307     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
308     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
309
310     OIC_LOG(DEBUG, TAG, "OUT");
311 }
312
313 void CALENWConnectionStateChangedCb(int result, bool connected,
314                                     const char *remoteAddress, void *userData)
315 {
316     OIC_LOG(DEBUG, TAG, "IN");
317
318     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
319
320     oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
321     char *addr = OICStrdup(remoteAddress);
322     if (NULL == addr)
323     {
324         OIC_LOG(ERROR, TAG, "addr is NULL");
325         oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
326         return;
327     }
328     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
329     OICFree(addr);
330     oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
331
332     OIC_LOG(DEBUG, TAG, "OUT");
333 }