Enable to use USB bluetooth dongle
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-adapter-le.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <stdio.h>
25 #include <vconf.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-common.h"
31 #include "bt-request-sender.h"
32 #include "bt-event-handler.h"
33
34 static gboolean is_le_scanning = FALSE;
35
36 BT_EXPORT_API int bluetooth_check_adapter_le(void)
37 {
38         int ret;
39         int value;
40
41         ret = _bt_get_adapter_path(_bt_gdbus_get_system_gconn(), NULL);
42
43         if (ret != BLUETOOTH_ERROR_NONE) {
44                 return BLUETOOTH_ADAPTER_LE_DISABLED;
45         }
46
47         ret = vconf_get_int(VCONFKEY_BT_LE_STATUS, &value);
48         if (ret != 0) {
49                 BT_ERR("fail to get vconf key!");
50                 return ret;
51         }
52
53         BT_DBG("value : %d", value);
54         return value == VCONFKEY_BT_LE_STATUS_ON ? BLUETOOTH_ADAPTER_LE_ENABLED :
55                                                 BLUETOOTH_ADAPTER_LE_DISABLED;
56 }
57
58 BT_EXPORT_API int bluetooth_enable_adapter_le(void)
59 {
60         int result;
61
62         retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_ENABLED,
63                                 BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED);
64
65         BT_INIT_PARAMS();
66         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
67
68         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_ADAPTER_LE,
69                 in_param1, in_param2, in_param3, in_param4, &out_param);
70
71         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
72         return result;
73 }
74
75 BT_EXPORT_API int bluetooth_disable_adapter_le(void)
76 {
77         int result;
78         retv_if(bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED,
79                                 BLUETOOTH_ERROR_DEVICE_NOT_ENABLED);
80
81         BT_INIT_PARAMS();
82         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
83
84         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_DISABLE_ADAPTER_LE,
85                 in_param1, in_param2, in_param3, in_param4, &out_param);
86
87         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88
89         return result;
90 }
91
92 void _bt_set_le_scan_status(gboolean mode)
93 {
94         BT_DBG("set LE scan mode : %d", mode);
95         is_le_scanning = mode;
96 }
97
98 BT_EXPORT_API gboolean bluetooth_is_le_scanning(void)
99 {
100         return is_le_scanning;
101 }
102
103 BT_EXPORT_API int bluetooth_start_le_discovery(void)
104 {
105         int result;
106
107         BT_CHECK_ENABLED_ANY(return);
108
109         BT_INIT_PARAMS();
110         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
111
112         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_START_LE_DISCOVERY,
113                 in_param1, in_param2, in_param3, in_param4, &out_param);
114
115         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
116
117         if (result == BLUETOOTH_ERROR_NONE)
118                 _bt_set_le_scan_status(TRUE);
119
120         return result;
121 }
122
123 BT_EXPORT_API int bluetooth_stop_le_discovery(void)
124 {
125         int result;
126
127         BT_CHECK_ENABLED_ANY(return);
128
129         BT_INIT_PARAMS();
130         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
131
132         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_STOP_LE_DISCOVERY,
133                 in_param1, in_param2, in_param3, in_param4, &out_param);
134
135         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
136
137         if (result == BLUETOOTH_ERROR_NONE)
138                 _bt_set_le_scan_status(FALSE);
139
140         return result;
141 }
142
143 BT_EXPORT_API int bluetooth_is_le_discovering(void)
144 {
145         int result;
146         int is_discovering = FALSE;
147
148         BT_CHECK_ENABLED_ANY(return);
149
150         BT_INIT_PARAMS();
151         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
152
153         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_LE_DISCOVERYING,
154                 in_param1, in_param2, in_param3, in_param4, &out_param);
155
156         if (result == BLUETOOTH_ERROR_NONE) {
157                 is_discovering = g_array_index(out_param,
158                                 int, 0);
159         } else {
160                 BT_ERR("Fail to send request");
161         }
162
163         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
164
165         return is_discovering;
166 }
167
168 BT_EXPORT_API int bluetooth_register_scan_filter(bluetooth_le_scan_filter_t *filter, int *slot_id)
169 {
170         int result;
171
172         BT_CHECK_ENABLED_ANY(return);
173
174         BT_INIT_PARAMS();
175         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
176
177         g_array_append_vals(in_param1, filter, sizeof(bluetooth_le_scan_filter_t));
178
179         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REGISTER_SCAN_FILTER,
180                 in_param1, in_param2, in_param3, in_param4, &out_param);
181
182         if (result == BLUETOOTH_ERROR_NONE) {
183                 *slot_id = g_array_index(out_param, int, 0);
184         } else {
185                 BT_ERR("Fail to send request");
186         }
187
188         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
189
190         return result;
191 }
192
193 BT_EXPORT_API int bluetooth_unregister_scan_filter(int slot_id)
194 {
195         int result;
196
197         BT_CHECK_ENABLED_ANY(return);
198
199         BT_INIT_PARAMS();
200         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
201
202         g_array_append_vals(in_param1, &slot_id, sizeof(int));
203
204         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_SCAN_FILTER,
205                 in_param1, in_param2, in_param3, in_param4, &out_param);
206
207         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
208
209         return result;
210 }
211
212 BT_EXPORT_API int bluetooth_unregister_all_scan_filters(void)
213 {
214         int result;
215
216         BT_CHECK_ENABLED_ANY(return);
217
218         BT_INIT_PARAMS();
219         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
220
221         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNREGISTER_ALL_SCAN_FILTERS,
222                 in_param1, in_param2, in_param3, in_param4, &out_param);
223
224         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
225
226         return result;
227 }
228
229 #ifdef TIZEN_WEARABLE
230 gboolean __bluetooth_is_privileged_process(void)
231 {
232         FILE *fp= NULL;
233         char path[30] = {0, };
234         char buf[256] = {0, };
235
236         snprintf(path, sizeof(path), "/proc/%d/cmdline", getpid());
237         fp = fopen(path, "r");
238         if (fp == NULL)
239                 return FALSE;
240
241         if (fgets(buf, 256, fp) != NULL) {
242                 if (strstr(buf, "weconnd") != NULL) {
243                         fclose(fp);
244                         return TRUE;
245                 }
246         }
247
248         fclose(fp);
249         return FALSE;
250 }
251 #endif
252
253 BT_EXPORT_API int bluetooth_set_advertising(int handle, gboolean enable)
254 {
255         int result;
256         gboolean use_reserved_slot = FALSE;
257
258         BT_CHECK_ENABLED_ANY(return);
259
260 #ifdef TIZEN_WEARABLE
261         use_reserved_slot = __bluetooth_is_privileged_process();
262 #endif
263
264         BT_INIT_PARAMS();
265         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
266
267         g_array_append_vals(in_param1, &handle, sizeof(int));
268         g_array_append_vals(in_param2, &enable, sizeof(gboolean));
269         g_array_append_vals(in_param3, &use_reserved_slot, sizeof(gboolean));
270
271         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING,
272                 in_param1, in_param2, in_param3, in_param4, &out_param);
273
274         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
275
276         return result;
277 }
278
279 BT_EXPORT_API int bluetooth_set_custom_advertising(int handle, gboolean enable,
280                                                 bluetooth_advertising_params_t *params)
281 {
282         int result;
283         gboolean use_reserved_slot = FALSE;
284
285         BT_CHECK_ENABLED_ANY(return);
286
287 #ifdef TIZEN_WEARABLE
288         use_reserved_slot = __bluetooth_is_privileged_process();
289 #endif
290
291         BT_INIT_PARAMS();
292         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
293
294         g_array_append_vals(in_param1, &handle, sizeof(int));
295         g_array_append_vals(in_param2, &enable, sizeof(gboolean));
296         g_array_append_vals(in_param3, params, sizeof(bluetooth_advertising_params_t));
297         g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
298
299         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_CUSTOM_ADVERTISING,
300                 in_param1, in_param2, in_param3, in_param4, &out_param);
301
302         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
303
304         return result;
305 }
306
307 BT_EXPORT_API int bluetooth_get_advertising_data(bluetooth_advertising_data_t *adv_data, int *length)
308 {
309         int result;
310         guint8 *data;
311
312         BT_CHECK_PARAMETER(adv_data, return);
313         BT_CHECK_PARAMETER(length, return);
314         BT_CHECK_ENABLED_ANY(return);
315
316         BT_INIT_PARAMS();
317         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
318
319         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_ADVERTISING_DATA,
320                 in_param1, in_param2, in_param3, in_param4, &out_param);
321
322         if (result == BLUETOOTH_ERROR_NONE) {
323                 data = &g_array_index(out_param, guint8, 0);
324                 *length = out_param->len;
325
326                 memset(adv_data, 0x00, sizeof(bluetooth_advertising_data_t));
327                 memcpy(adv_data->data, data, *length);
328         }
329
330         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
331
332         return result;
333 }
334
335 BT_EXPORT_API int bluetooth_set_advertising_data(int handle, const bluetooth_advertising_data_t *value, int length)
336 {
337         int result;
338         gboolean use_reserved_slot = FALSE;
339
340         BT_CHECK_PARAMETER(value, return);
341         BT_CHECK_ENABLED_ANY(return);
342
343         if (length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 3)
344                 return BLUETOOTH_ERROR_INVALID_PARAM;
345
346 #ifdef TIZEN_WEARABLE
347         use_reserved_slot = __bluetooth_is_privileged_process();
348 #endif
349
350         BT_INIT_PARAMS();
351         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
352
353         g_array_append_vals(in_param1, &handle, sizeof(int));
354         g_array_append_vals(in_param2, value, sizeof(bluetooth_advertising_data_t));
355         g_array_append_vals(in_param3, &length, sizeof(int));
356         g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
357
358         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ADVERTISING_DATA,
359                 in_param1, in_param2, in_param3, in_param4, &out_param);
360
361         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
362
363         return result;
364 }
365
366 BT_EXPORT_API int bluetooth_get_scan_response_data(bluetooth_scan_resp_data_t *value, int *length)
367 {
368         int result;
369         guint8 *data;
370
371         BT_CHECK_PARAMETER(value, return);
372         BT_CHECK_PARAMETER(length, return);
373         BT_CHECK_ENABLED_ANY(return);
374
375         BT_INIT_PARAMS();
376         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
377
378         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_SCAN_RESPONSE_DATA,
379                 in_param1, in_param2, in_param3, in_param4, &out_param);
380
381         if (result == BLUETOOTH_ERROR_NONE) {
382                 data = &g_array_index(out_param, guint8, 0);
383                 *length = out_param->len;
384
385                 memset(value, 0x00, sizeof(bluetooth_scan_resp_data_t));
386                 memcpy(value->data, data, *length);
387         }
388
389         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
390
391         return result;
392 }
393
394 BT_EXPORT_API int bluetooth_set_scan_response_data(int handle,
395                         const bluetooth_scan_resp_data_t *value, int length)
396 {
397         int result;
398         gboolean use_reserved_slot = FALSE;
399
400         BT_CHECK_PARAMETER(value, return);
401         BT_CHECK_ENABLED_ANY(return);
402
403         if (length > BLUETOOTH_SCAN_RESP_DATA_LENGTH_MAX)
404                 return BLUETOOTH_ERROR_INVALID_PARAM;
405
406 #ifdef TIZEN_WEARABLE
407         use_reserved_slot = __bluetooth_is_privileged_process();
408 #endif
409
410         BT_INIT_PARAMS();
411         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
412
413         g_array_append_vals(in_param1, &handle, sizeof(int));
414         g_array_append_vals(in_param2, value, length);
415         g_array_append_vals(in_param3, &length, sizeof(int));
416         g_array_append_vals(in_param4, &use_reserved_slot, sizeof(gboolean));
417
418         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_RESPONSE_DATA,
419                 in_param1, in_param2, in_param3, in_param4, &out_param);
420
421         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
422
423         return result;
424 }
425
426 BT_EXPORT_API int bluetooth_set_scan_parameters(bluetooth_le_scan_params_t *params)
427 {
428         int result;
429
430         BT_CHECK_ENABLED_ANY(return);
431
432         BT_INIT_PARAMS();
433         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
434
435         g_array_append_vals(in_param1, params, sizeof(bluetooth_le_scan_params_t));
436
437         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_SCAN_PARAMETERS,
438                 in_param1, in_param2, in_param3, in_param4, &out_param);
439
440         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
441
442         return result;
443 }
444
445 BT_EXPORT_API int bluetooth_is_advertising(gboolean *is_advertising)
446 {
447         int result;
448
449         BT_CHECK_ENABLED_ANY(return);
450
451         BT_INIT_PARAMS();
452         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
453
454         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_ADVERTISING,
455                 in_param1, in_param2, in_param3, in_param4, &out_param);
456
457         if (result == BLUETOOTH_ERROR_NONE) {
458                 *is_advertising = g_array_index(out_param, int, 0);
459         } else {
460                 BT_ERR("Fail to send request");
461         }
462
463         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
464
465         return result;
466 }
467
468 BT_EXPORT_API int bluetooth_add_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
469 {
470         int result;
471
472         BT_CHECK_PARAMETER(address, return);
473         BT_CHECK_ENABLED_ANY(return);
474
475         BT_INIT_PARAMS();
476         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
477
478         g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
479         g_array_append_vals(in_param2, &address_type, sizeof(int));
480
481         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ADD_WHITE_LIST,
482                 in_param1, in_param2, in_param3, in_param4, &out_param);
483
484         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
485
486         return result;
487 }
488
489 BT_EXPORT_API int bluetooth_remove_white_list(bluetooth_device_address_t *address, bluetooth_device_address_type_t address_type)
490 {
491         int result;
492
493         BT_CHECK_PARAMETER(address, return);
494         BT_CHECK_ENABLED_ANY(return);
495
496         BT_INIT_PARAMS();
497         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
498
499         g_array_append_vals(in_param1, address, sizeof(bluetooth_device_address_t));
500         g_array_append_vals(in_param2, &address_type, sizeof(int));
501
502         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_REMOVE_WHITE_LIST,
503                 in_param1, in_param2, in_param3, in_param4, &out_param);
504
505         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
506
507         return result;
508 }
509
510 BT_EXPORT_API int bluetooth_clear_white_list(void)
511 {
512         int result;
513
514         BT_CHECK_ENABLED_ANY(return);
515
516         BT_INIT_PARAMS();
517         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
518
519         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CLEAR_WHITE_LIST,
520                 in_param1, in_param2, in_param3, in_param4, &out_param);
521
522         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
523
524         return result;
525 }
526
527 BT_EXPORT_API int bluetooth_enable_le_privacy(gboolean enable_privacy)
528 {
529         int result;
530
531         BT_CHECK_ENABLED_ANY(return);
532
533         BT_INIT_PARAMS();
534         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
535
536         g_array_append_vals(in_param1, &enable_privacy, sizeof(gboolean));
537
538         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_LE_PRIVACY,
539                 in_param1, in_param2, in_param3, in_param4, &out_param);
540
541         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
542
543         return result;
544 }
545
546 BT_EXPORT_API int bluetooth_check_privilege_advertising_parameter(void)
547 {
548         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SET_ADVERTISING_PARAMETERS)
549                      == BLUETOOTH_ERROR_PERMISSION_DEINED) {
550                 BT_ERR("Don't have a privilege to use this API");
551                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
552         }
553
554         return BLUETOOTH_ERROR_NONE;
555 }
556
557 BT_EXPORT_API int bluetooth_le_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
558 {
559         int ret;
560
561         ret = _bt_register_event(BT_LE_ADAPTER_EVENT, (void *)callback_ptr, user_data);
562         if (ret != BLUETOOTH_ERROR_NONE &&
563             ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
564                 BT_ERR("Fail to register BT_LE_ADAPTER_EVENT event : %d", ret);
565                 return ret;
566         }
567
568         return BLUETOOTH_ERROR_NONE;
569 }
570
571 BT_EXPORT_API int bluetooth_le_unregister_callback(void)
572 {
573         _bt_unregister_event(BT_LE_ADAPTER_EVENT);
574
575         return BLUETOOTH_ERROR_NONE;
576 }