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