Imported Upstream version 1.1.1
[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] Any 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] Any 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     OIC_LOG(DEBUG, TAG, "OUT");
118
119     return CA_STATUS_OK;
120 }
121
122 void CATerminateLENetworkMonitor()
123 {
124     OIC_LOG(DEBUG, TAG, "IN");
125
126     ca_mutex_free(g_bleDeviceStateChangedCbMutex);
127     g_bleDeviceStateChangedCbMutex = NULL;
128
129     ca_mutex_free(g_bleConnectionStateChangedCbMutex);
130     g_bleConnectionStateChangedCbMutex = NULL;
131     OIC_LOG(DEBUG, TAG, "OUT");
132 }
133
134 CAResult_t CAInitializeLEAdapter()
135 {
136     OIC_LOG(DEBUG, TAG, "IN");
137     OIC_LOG(DEBUG, TAG, "OUT");
138     return CA_STATUS_OK;
139 }
140
141 CAResult_t CAStartLEAdapter()
142 {
143     OIC_LOG(DEBUG, TAG, "IN");
144
145     int ret = bt_initialize();
146     if (BT_ERROR_NONE != ret)
147     {
148         OIC_LOG(ERROR, TAG, "bt_initialize failed");
149         return CA_STATUS_FAILED;
150     }
151     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
152     //Get Bluetooth adapter state
153     ret = bt_adapter_get_state(&adapterState);
154
155     if (BT_ERROR_NONE != ret && BT_ADAPTER_ENABLED == adapterState)
156     {
157         ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
158         if (BT_ERROR_NONE != ret)
159         {
160             OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
161             return CA_STATUS_FAILED;
162         }
163     }
164
165     ret = bt_adapter_set_state_changed_cb(CALEAdapterStateChangedCb, NULL);
166     if (BT_ERROR_NONE != ret)
167     {
168         OIC_LOG(DEBUG, TAG, "bt_adapter_set_state_changed_cb failed");
169         return CA_STATUS_FAILED;
170     }
171
172     ret = bt_gatt_set_connection_state_changed_cb(CALENWConnectionStateChangedCb, NULL);
173     if (BT_ERROR_NONE != ret)
174     {
175         OIC_LOG_V(ERROR, TAG,
176                   "bt_gatt_set_connection_state_changed_cb has failed");
177         return CA_STATUS_FAILED;
178     }
179
180     OIC_LOG(DEBUG, TAG, "OUT");
181     return CA_STATUS_OK;
182 }
183
184 CAResult_t CAStopLEAdapter()
185 {
186
187     int ret = bt_adapter_unset_state_changed_cb();
188     if (BT_ERROR_NONE != ret)
189     {
190         OIC_LOG(DEBUG, TAG, "bt_adapter_unset_state_changed_cb failed");
191         return CA_STATUS_FAILED;
192     }
193
194     ret = bt_deinitialize();
195     if (BT_ERROR_NONE != ret)
196     {
197         OIC_LOG(ERROR, TAG, "bt_deinitialize failed");
198         return CA_STATUS_FAILED;
199     }
200
201     return CA_STATUS_OK;
202 }
203
204 CAResult_t CAGetLEAdapterState()
205 {
206     OIC_LOG(DEBUG, TAG, "IN");
207
208     bt_adapter_state_e adapterState = BT_ADAPTER_DISABLED;
209
210     //Get Bluetooth adapter state
211     int ret = bt_adapter_get_state(&adapterState);
212     if (BT_ERROR_NONE != ret)
213     {
214         OIC_LOG_V(ERROR, TAG, "Bluetooth get state failed!, error num [%x]",
215                   ret);
216         return CA_STATUS_FAILED;
217     }
218
219     if (BT_ADAPTER_ENABLED != adapterState)
220     {
221         OIC_LOG(DEBUG, TAG, "BT Adapter is not enabled");
222         return CA_ADAPTER_NOT_ENABLED;
223     }
224
225     OIC_LOG(DEBUG, TAG, "OUT");
226     return CA_STATUS_OK;
227 }
228
229 CAResult_t CAGetLEAddress(char **local_address)
230 {
231     OIC_LOG(DEBUG, TAG, "IN");
232
233     VERIFY_NON_NULL(local_address, TAG, "local_address is null")
234
235     char *address = NULL;
236
237     int ret = bt_adapter_get_address(&address);
238     if (BT_ERROR_NONE != ret || !address)
239     {
240         OIC_LOG_V(ERROR, TAG, "bt_adapter_get_address failed!, error num [%x]",
241                   ret);
242         return CA_STATUS_FAILED;
243     }
244
245     OIC_LOG_V(DEBUG, TAG, "bd address[%s]", address);
246
247     *local_address = address;
248
249     OIC_LOG(DEBUG, TAG, "OUT");
250
251     return CA_STATUS_OK;
252 }
253
254 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
255 {
256     OIC_LOG(DEBUG, TAG, "IN");
257     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
258     g_bleDeviceStateChangedCallback = callback;
259     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
260     OIC_LOG(DEBUG, TAG, "OUT");
261     return CA_STATUS_OK;
262 }
263
264 CAResult_t CAUnSetLEAdapterStateChangedCb()
265 {
266     OIC_LOG(DEBUG, TAG, "IN");
267     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
268     g_bleDeviceStateChangedCallback = NULL;
269     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
270     OIC_LOG(DEBUG, TAG, "OUT");
271     return CA_STATUS_OK;
272 }
273
274 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
275 {
276     OIC_LOG(DEBUG, TAG, "IN");
277     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
278     g_bleConnectionStateChangedCallback = callback;
279     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
280     OIC_LOG(DEBUG, TAG, "OUT");
281     return CA_STATUS_OK;
282 }
283
284 CAResult_t CAUnsetLENWConnectionStateChangedCb()
285 {
286     OIC_LOG(DEBUG, TAG, "IN");
287     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
288     g_bleConnectionStateChangedCallback = NULL;
289     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
290     OIC_LOG(DEBUG, TAG, "OUT");
291     return CA_STATUS_OK;
292 }
293
294 void CALEAdapterStateChangedCb(int result, bt_adapter_state_e adapter_state,
295                                           void *user_data)
296 {
297     OIC_LOG(DEBUG, TAG, "IN");
298
299     ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
300
301     if (NULL == g_bleDeviceStateChangedCallback)
302     {
303         OIC_LOG(ERROR, TAG, "g_bleDeviceStateChangedCallback is NULL!");
304         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
305         return;
306     }
307
308     if (BT_ADAPTER_DISABLED == adapter_state)
309     {
310         OIC_LOG(DEBUG, TAG, "Adapter is disabled");
311         g_bleDeviceStateChangedCallback(CA_ADAPTER_DISABLED);
312         ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
313         return;
314     }
315
316     OIC_LOG(DEBUG, TAG, "Adapter is Enabled");
317
318     int ret = bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0);
319     if (BT_ERROR_NONE != ret)
320     {
321         OIC_LOG(ERROR, TAG, "bt_adapter_set_visibility failed");
322         return;
323     }
324
325     g_bleDeviceStateChangedCallback(CA_ADAPTER_ENABLED);
326     ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
327
328     OIC_LOG(DEBUG, TAG, "OUT");
329 }
330
331 void CALENWConnectionStateChangedCb(int result, bool connected,
332                                     const char *remoteAddress, void *userData)
333 {
334     OIC_LOG(DEBUG, TAG, "IN");
335
336     VERIFY_NON_NULL_VOID(remoteAddress, TAG, "remote address is NULL");
337
338     ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
339     char *addr = OICStrdup(remoteAddress);
340     if (NULL == addr)
341     {
342         OIC_LOG(ERROR, TAG, "addr is NULL");
343         ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
344         return;
345     }
346     g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, addr, connected);
347     OICFree(addr);
348     ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
349
350     OIC_LOG(DEBUG, TAG, "OUT");
351 }