Remove security-server dependency
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-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 <dbus/dbus-glib.h>
26 #include <dbus/dbus.h>
27 #include <glib.h>
28 #include <dlog.h>
29 #include <string.h>
30 #include <vconf.h>
31 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
32 #include <syspopup_caller.h>
33 #endif
34 #include <aul.h>
35 #include <notification.h>
36 #ifdef ENABLE_TIZEN_2_4
37 #include <journal/device.h>
38 #endif
39
40 #include "bt-internal-types.h"
41 #include "bt-service-common.h"
42 #include "bt-service-event.h"
43 #include "bt-service-adapter.h"
44 #include "bt-service-adapter-le.h"
45
46
47 #define BT_ADV_INTERVAL_MIN 20 /* msec */
48 #define BT_ADV_INTERVAL_MAX 10240
49 #define BT_ADV_INTERVAL_SPLIT 0.625
50 #define BT_DEFAULT_ADV_MIN_INTERVAL 500
51 #define BT_DEFAULT_ADV_MAX_INTERVAL 500
52 #define BT_ADV_FILTER_POLICY_DEFAULT    0x00
53 #define BT_ADV_TYPE_DEFAULT     0x00
54 #define BT_ADV_FILTER_POLICY_ALLOW_SCAN_CONN_WL_ONLY    0x03
55
56 typedef struct {
57         int adv_inst_max;
58         int rpa_offloading;
59         int max_filter;
60 } bt_adapter_le_feature_info_t;
61
62 typedef struct {
63         char *sender;
64         gboolean is_advertising;
65 } bt_adapter_le_adv_slot_t;
66
67 static bluetooth_advertising_params_t adv_params = {
68         BT_DEFAULT_ADV_MIN_INTERVAL,
69         BT_DEFAULT_ADV_MAX_INTERVAL,
70         BT_ADV_FILTER_POLICY_DEFAULT,
71         BT_ADV_TYPE_DEFAULT};
72 static bluetooth_advertising_data_t adv_data = { {0} };
73 static int adv_data_len;
74 static bluetooth_scan_resp_data_t resp_data = { {0} };
75 static int resp_data_len;
76
77 static bt_adapter_le_feature_info_t le_feature_info = { 1, 0, 0 };
78 static bt_adapter_le_adv_slot_t *le_adv_slot = NULL;
79
80 void __bt_free_le_adv_slot(void)
81 {
82         int i;
83
84         if (le_adv_slot == NULL)
85                 return;
86
87         for (i = 0; i < le_feature_info.adv_inst_max; i++) {
88                 if (le_adv_slot[i].sender)
89                         g_free(le_adv_slot[i].sender);
90         }
91         g_free(le_adv_slot);
92         le_adv_slot = NULL;
93 }
94
95 int _bt_service_adapter_le_init(void)
96 {
97         le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
98
99         return BLUETOOTH_ERROR_NONE;
100 }
101
102 void _bt_service_adapter_le_deinit(void)
103 {
104         __bt_free_le_adv_slot();
105 }
106
107 gboolean _bt_update_le_feature_support(const char *item, const char *value)
108 {
109         if (item== NULL || value == NULL)
110                 return FALSE;
111
112         if (g_strcmp0(item, "adv_inst_max") == 0) {
113                 if (atoi(value) != le_feature_info.adv_inst_max) {
114                         __bt_free_le_adv_slot();
115                         le_feature_info.adv_inst_max = atoi(value);
116                         le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
117                 }
118         } else if (g_strcmp0(item, "rpa_offloading") == 0) {
119                 le_feature_info.rpa_offloading = atoi(value);
120         } else if (g_strcmp0(item, "max_filter") == 0) {
121                 le_feature_info.max_filter = atoi(value);
122         } else {
123                 BT_DBG("No registered item");
124                 return FALSE;
125         }
126
127         return TRUE;
128 }
129
130 static gboolean __bt_is_factory_test_mode(void)
131 {
132         int mode = 0;
133 #ifdef ENABLE_TIZEN_2_4
134         if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
135                 BT_ERR("Get the DUT Mode fail");
136                 return TRUE;
137         }
138 #endif
139         if (mode != FALSE) {
140                 BT_INFO("DUT Test Mode !!");
141                 return TRUE;
142         }
143
144         return FALSE;
145 }
146
147 int __bt_get_available_adv_slot_id(const char *sender, gboolean use_reserved_slot)
148 {
149         int i;
150
151         if (le_adv_slot == NULL)
152                 return -1;
153
154         if (use_reserved_slot == TRUE) {
155                 if (le_feature_info.adv_inst_max > 1)
156                         return 0;
157                 else if (le_adv_slot[0].sender == NULL ||
158                         g_strcmp0(le_adv_slot[0].sender, sender) == 0)
159                         return 0;
160                 else
161                         return -1;
162         }
163
164         for (i = 0; i < le_feature_info.adv_inst_max; i++) {
165                 if (le_adv_slot[i].sender == NULL)
166                         continue;
167                 if (g_strcmp0(le_adv_slot[i].sender, sender) == 0)
168                         return i;
169         }
170
171         for (i = 0; i < le_feature_info.adv_inst_max; i++) {
172                 if (le_adv_slot[i].sender == NULL)
173                         return i;
174         }
175
176         return -1;
177 }
178
179 void __bt_register_adv_slot_owner(const char *sender, int slot_id)
180 {
181         if (le_adv_slot[slot_id].sender == NULL)
182                 le_adv_slot[slot_id].sender = strdup(sender);
183 }
184
185 void __bt_unregister_adv_slot_owner(int slot_id)
186 {
187         g_free(le_adv_slot[slot_id].sender);
188         le_adv_slot[slot_id].sender = NULL;
189         le_adv_slot[slot_id].is_advertising = FALSE;
190 }
191
192 const char* _bt_get_adv_slot_owner(int slot_id)
193 {
194         if (le_adv_slot == NULL)
195                 return NULL;
196
197         return le_adv_slot[slot_id].sender;
198 }
199
200 void _bt_set_advertising_status(int slot_id, gboolean mode)
201 {
202         le_adv_slot[slot_id].is_advertising = mode;
203 }
204
205 gboolean _bt_is_advertising(void)
206 {
207         gboolean status = FALSE;
208         int i;
209
210         for (i = 0; i < le_feature_info.adv_inst_max; i++) {
211                 if (le_adv_slot[i].is_advertising == TRUE)
212                         status = TRUE;
213         }
214
215         return status;
216 }
217
218 void _bt_stop_advertising_by_terminated_process(const char* terminated_name)
219 {
220         int i;
221
222         if (le_adv_slot == NULL)
223                 return;
224
225         for (i = 0; i < le_feature_info.adv_inst_max; i++) {
226                 if (le_adv_slot[i].sender != NULL) {
227                         if (strcasecmp(terminated_name, le_adv_slot[i].sender) == 0) {
228                                 BT_ERR("Stop advertising by terminated process(%s).", terminated_name);
229                                 _bt_set_advertising(FALSE, terminated_name, FALSE);
230                         }
231                 }
232         }
233 }
234
235 gboolean _bt_get_advertising_params(bluetooth_advertising_params_t *params)
236 {
237         if (params == NULL)
238                 return FALSE;
239
240         memcpy(params, &adv_params, sizeof(bluetooth_advertising_params_t));
241
242         return TRUE;
243 }
244
245 int _bt_set_advertising(gboolean enable, const char *sender, gboolean use_reserved_slot)
246 {
247         DBusGProxy *proxy;
248         GError *error = NULL;
249         int slot_id;
250
251         if (__bt_is_factory_test_mode()) {
252                 BT_ERR("Unable to start advertising in factory binary !!");
253                 return BLUETOOTH_ERROR_NOT_SUPPORT;
254         }
255
256         if (_bt_adapter_get_status() != BT_ACTIVATED &&
257                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
258                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
259         }
260
261         slot_id = __bt_get_available_adv_slot_id(sender, use_reserved_slot);
262         if (slot_id == -1) {
263                 BT_ERR("There is NO available slot!!");
264                 return BLUETOOTH_ERROR_NO_RESOURCES;
265         }
266
267         if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
268                 return BLUETOOTH_ERROR_IN_PROGRESS;
269
270         if (le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
271                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
272
273         proxy = _bt_get_adapter_proxy();
274         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
275
276         dbus_g_proxy_call(proxy, "SetAdvertising", &error,
277                         G_TYPE_BOOLEAN, enable,
278                         G_TYPE_INT, slot_id,
279                         G_TYPE_INVALID, G_TYPE_INVALID);
280
281         if (error) {
282                 BT_ERR("SetAdvertising Fail: %s", error->message);
283                 g_error_free(error);
284                 return BLUETOOTH_ERROR_INTERNAL;
285         }
286
287         le_adv_slot[slot_id].is_advertising = enable;
288
289         if (enable == TRUE)
290                 __bt_register_adv_slot_owner(sender, slot_id);
291         else
292                 __bt_unregister_adv_slot_owner(slot_id);
293
294         BT_INFO("Set advertising [%d]", enable);
295
296         return BLUETOOTH_ERROR_NONE;
297 }
298
299 int _bt_set_custom_advertising(gboolean enable, bluetooth_advertising_params_t *params,
300                                 const char *sender, gboolean use_reserved_slot)
301 {
302         DBusGProxy *proxy;
303         GError *error = NULL;
304         guint32 min = 0;
305         guint32 max = 0;
306         int slot_id;
307
308         BT_CHECK_PARAMETER(params, return);
309
310         if (__bt_is_factory_test_mode()) {
311                 BT_ERR("Unable to start advertising in factory binary !!");
312                 return BLUETOOTH_ERROR_NOT_SUPPORT;
313         }
314
315         if (_bt_adapter_get_status() != BT_ACTIVATED &&
316                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
317                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
318         }
319
320         if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
321                 return BLUETOOTH_ERROR_IN_PROGRESS;
322
323         if (le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
324                 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
325
326         proxy = _bt_get_adapter_proxy();
327         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
328
329         if (params->interval_min > params->interval_max ||
330                         params->interval_min < BT_ADV_INTERVAL_MIN ||
331                         params->interval_max > BT_ADV_INTERVAL_MAX)
332                 return BLUETOOTH_ERROR_INVALID_PARAM;
333
334         if (params->filter_policy > BLUETOOTH_ALLOW_SCAN_CONN_WHITE_LIST)
335                 return BLUETOOTH_ERROR_INVALID_PARAM;
336
337         if (params->type  == BLUETOOTH_ADV_CONNECTABLE_DIRECT_HIGH ||
338                         params->type == BLUETOOTH_ADV_CONNECTABLE_DIRECT_LOW ||
339                         params->type == BLUETOOTH_ADV_NON_CONNECTABLE)
340                 return BLUETOOTH_ERROR_NOT_SUPPORT;
341
342         min = params->interval_min / BT_ADV_INTERVAL_SPLIT;
343         max = params->interval_max / BT_ADV_INTERVAL_SPLIT;
344
345         slot_id = __bt_get_available_adv_slot_id(sender, use_reserved_slot);
346         if (slot_id == -1) {
347                 BT_ERR("There is NO available slot!!");
348                 return BLUETOOTH_ERROR_NO_RESOURCES;
349         }
350
351         dbus_g_proxy_call(proxy, "SetAdvertisingParameters", &error,
352                         G_TYPE_UINT, min,
353                         G_TYPE_UINT, max,
354                         G_TYPE_UINT, params->filter_policy,
355                         G_TYPE_UINT, params->type,
356                         G_TYPE_INT, slot_id,
357                         G_TYPE_INVALID, G_TYPE_INVALID);
358
359         if (error) {
360                 BT_ERR("SetAdvertisingParameters Fail: %s", error->message);
361                 g_error_free(error);
362                 return BLUETOOTH_ERROR_INTERNAL;
363         }
364
365         adv_params.interval_min = params->interval_min;
366         adv_params.interval_max = params->interval_max;
367         adv_params.filter_policy = params->filter_policy;
368         adv_params.type= params->type;
369
370         dbus_g_proxy_call(proxy, "SetAdvertising", &error,
371                         G_TYPE_BOOLEAN, enable,
372                         G_TYPE_INT, slot_id,
373                         G_TYPE_INVALID, G_TYPE_INVALID);
374
375         if (error) {
376                 BT_ERR("SetAdvertising Fail: %s", error->message);
377                 g_error_free(error);
378                 return BLUETOOTH_ERROR_INTERNAL;
379         }
380
381         le_adv_slot[slot_id].is_advertising = enable;
382
383         if (enable == TRUE)
384                 __bt_register_adv_slot_owner(sender, slot_id);
385         else
386                 __bt_unregister_adv_slot_owner(slot_id);
387
388         BT_INFO_C("Set advertising [%d]", enable);
389         return BLUETOOTH_ERROR_NONE;
390 }
391
392 static int __bt_get_ad_data_by_type(char *in_data, int in_len,
393                 char in_type, char **data, int *data_len)
394 {
395         if (in_data == NULL || data == NULL || data_len == NULL)
396                 return BLUETOOTH_ERROR_INTERNAL;
397
398         if (in_len < 0)
399                 return BLUETOOTH_ERROR_INTERNAL;
400
401         int i;
402         int len = 0;
403         int type = 0;
404
405         for (i = 0; i < in_len; i++) {
406                 len = in_data[i];
407                 if (len <= 0 || i + 1 >= in_len) {
408                         BT_ERR("Invalid advertising data");
409                         return BLUETOOTH_ERROR_INTERNAL;
410                 }
411
412                 type = in_data[i + 1];
413                 if (type == in_type) {
414                         i = i + 2;
415                         len--;
416                         break;
417                 }
418
419                 i += len;
420                 len = 0;
421         }
422
423         if (i + len > in_len) {
424                 BT_ERR("Invalid advertising data");
425                 return BLUETOOTH_ERROR_INTERNAL;
426         } else if (len == 0) {
427                 BT_DBG("AD Type 0x%02x data is not set", in_type);
428                 *data = NULL;
429                 *data_len = 0;
430                 return BLUETOOTH_ERROR_NONE;
431         }
432
433         *data = g_memdup(&in_data[i], len);
434         if (*data == NULL)
435                 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
436         *data_len = len;
437
438         return BLUETOOTH_ERROR_NONE;
439 }
440
441 int _bt_get_advertising_data(bluetooth_advertising_data_t *adv, int *length)
442 {
443         BT_CHECK_PARAMETER(adv, return);
444         BT_CHECK_PARAMETER(length, return);
445
446         memcpy(adv, &adv_data, sizeof(adv_data));
447         *length = adv_data_len;
448
449         return BLUETOOTH_ERROR_NONE;
450 }
451
452 int _bt_set_advertising_data(bluetooth_advertising_data_t *adv, int length,
453                                 const char *sender, gboolean use_reserved_slot)
454 {
455         DBusGProxy *proxy;
456         GError *error = NULL;
457         GArray *arr;
458         int i;
459         char *old_mdata = NULL;
460         char *new_mdata = NULL;
461         int old_len = 0;
462         int new_len = 0;
463         int slot_id;
464
465         if (__bt_is_factory_test_mode()) {
466                 BT_ERR("Unable to set advertising data in factory binary !!");
467                 return BLUETOOTH_ERROR_NOT_SUPPORT;
468         }
469
470         BT_CHECK_PARAMETER(adv, return);
471
472         if (_bt_adapter_get_status() != BT_ACTIVATED &&
473                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
474                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
475         }
476
477         proxy = _bt_get_adapter_proxy();
478         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
479
480         arr = g_array_new(TRUE, TRUE, sizeof(guint8));
481
482         for (i = 0; i < length; i++)
483                 g_array_append_vals(arr, &(adv->data[i]), sizeof(guint8));
484
485         slot_id = __bt_get_available_adv_slot_id(sender, use_reserved_slot);
486         if (slot_id == -1) {
487                 BT_ERR("There is NO available slot!!");
488                 return BLUETOOTH_ERROR_NO_RESOURCES;
489         }
490
491         dbus_g_proxy_call(proxy, "SetAdvertisingData", &error,
492                         DBUS_TYPE_G_UCHAR_ARRAY, arr,
493                         G_TYPE_INT, slot_id,
494                         G_TYPE_INVALID, G_TYPE_INVALID);
495
496         g_array_free(arr, TRUE);
497
498         if (error) {
499                 BT_ERR("SetAdvertisingData Fail: %s", error->message);
500                 g_error_free(error);
501                 return BLUETOOTH_ERROR_INTERNAL;
502         }
503
504         __bt_register_adv_slot_owner(sender, slot_id);
505
506         __bt_get_ad_data_by_type((char *)adv_data.data, adv_data_len, 0xff,
507                         &old_mdata, &old_len);
508         __bt_get_ad_data_by_type((char *)adv->data, length, 0xff,
509                         &new_mdata, &new_len);
510         if (old_len != new_len ||
511                         (old_mdata && new_mdata &&
512                          memcmp(old_mdata, new_mdata, new_len))) {
513                 _bt_send_event(BT_ADAPTER_EVENT,
514                                 BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
515                                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
516                                 &new_mdata, new_len,
517                                 DBUS_TYPE_INVALID);
518         }
519         g_free(new_mdata);
520         g_free(old_mdata);
521
522         memset(&adv_data, 0x00, sizeof(bluetooth_advertising_data_t));
523         memcpy(&adv_data, adv, length);
524         adv_data_len = length;
525
526         BT_INFO("Set advertising data");
527
528         return BLUETOOTH_ERROR_NONE;
529 }
530
531 int _bt_get_scan_response_data(bluetooth_scan_resp_data_t *response, int *length)
532 {
533         BT_CHECK_PARAMETER(response, return);
534         BT_CHECK_PARAMETER(length, return);
535
536         memcpy(response, &resp_data, sizeof(resp_data));
537         *length = resp_data_len;
538
539         return BLUETOOTH_ERROR_NONE;
540 }
541
542 int _bt_set_scan_response_data(bluetooth_scan_resp_data_t *response, int length,
543                                 const char *sender, gboolean use_reserved_slot)
544 {
545         DBusGProxy *proxy;
546         GError *error = NULL;
547         GArray *arr;
548         int i;
549         char *old_mdata = NULL;
550         char *new_mdata = NULL;
551         int old_len = 0;
552         int new_len = 0;
553         int slot_id;
554
555         if (__bt_is_factory_test_mode()) {
556                 BT_ERR("Unable to set scan response list in factory binary !!");
557                 return BLUETOOTH_ERROR_NOT_SUPPORT;
558         }
559
560         BT_CHECK_PARAMETER(response, return);
561
562         if (_bt_adapter_get_status() != BT_ACTIVATED &&
563                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
564                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
565         }
566
567         proxy = _bt_get_adapter_proxy();
568         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
569
570         arr = g_array_new(TRUE, TRUE, sizeof(guint8));
571
572         for (i = 0; i < length; i++)
573                 g_array_append_vals(arr, &(response->data[i]), sizeof(guint8));
574
575         slot_id = __bt_get_available_adv_slot_id(sender, use_reserved_slot);
576         if (slot_id == -1) {
577                 BT_ERR("There is NO available slot!!");
578                 return BLUETOOTH_ERROR_NO_RESOURCES;
579         }
580
581         dbus_g_proxy_call(proxy, "SetScanRespData", &error,
582                         DBUS_TYPE_G_UCHAR_ARRAY, arr,
583                         G_TYPE_INT, slot_id,
584                         G_TYPE_INVALID, G_TYPE_INVALID);
585
586         g_array_free(arr, TRUE);
587
588         if (error) {
589                 BT_ERR("SetScanRespData Fail: %s", error->message);
590                 g_error_free(error);
591                 return BLUETOOTH_ERROR_INTERNAL;
592         }
593
594         __bt_register_adv_slot_owner(sender, slot_id);
595
596         /* Compare with previous scan resp data */
597         __bt_get_ad_data_by_type((char *)resp_data.data, resp_data_len, 0xff,
598                         &old_mdata, &old_len);
599         __bt_get_ad_data_by_type((char *)response->data, length, 0xff,
600                         &new_mdata, &new_len);
601         if (old_len != new_len ||
602                         (old_mdata && new_mdata &&
603                          memcmp(old_mdata, new_mdata, new_len))) {
604                 _bt_send_event(BT_ADAPTER_EVENT,
605                                 BLUETOOTH_EVENT_SCAN_RESPONSE_MANUFACTURER_DATA_CHANGED,
606                                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
607                                 &new_mdata, new_len,
608                                 DBUS_TYPE_INVALID);
609         }
610         g_free(new_mdata);
611         g_free(old_mdata);
612
613         memset(&resp_data, 0x00, sizeof(bluetooth_scan_resp_data_t));
614         memcpy(&resp_data, response, length);
615         resp_data_len = length;
616
617         BT_INFO("Set scan response data");
618         return BLUETOOTH_ERROR_NONE;
619 }
620
621 int _bt_set_scan_parameters(bluetooth_le_scan_params_t *params)
622 {
623         DBusGProxy *proxy;
624         GError *error = NULL;
625         guint32 itv = 0;
626         guint32 win = 0;
627
628         BT_CHECK_PARAMETER(params, return);
629
630         if (_bt_adapter_get_status() != BT_ACTIVATED &&
631                 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
632                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
633         }
634
635         proxy = _bt_get_adapter_proxy();
636         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
637
638         if (params->interval < BT_LE_SCAN_INTERVAL_MIN || params->interval > BT_LE_SCAN_INTERVAL_MAX)
639                 return BLUETOOTH_ERROR_INVALID_PARAM;
640
641         if (params->window < BT_LE_SCAN_WINDOW_MIN || params->window > BT_LE_SCAN_WINDOW_MAX)
642                 return BLUETOOTH_ERROR_INVALID_PARAM;
643
644         if (params->window > params->interval)
645                 return BLUETOOTH_ERROR_INVALID_PARAM;
646
647         itv = params->interval / BT_ADV_INTERVAL_SPLIT;
648         win = params->window / BT_ADV_INTERVAL_SPLIT;
649
650         dbus_g_proxy_call(proxy, "SetScanParameters", &error,
651                         G_TYPE_UINT, params->type,
652                         G_TYPE_UINT, itv,
653                         G_TYPE_UINT, win,
654                         G_TYPE_INVALID, G_TYPE_INVALID);
655
656         if (error) {
657                 BT_ERR("SetScanParameters Fail: %s", error->message);
658                 g_error_free(error);
659                 return BLUETOOTH_ERROR_INTERNAL;
660         }
661
662         _bt_set_le_discovery_type(params->type);
663
664         BT_INFO("Set scan parameters");
665         return BLUETOOTH_ERROR_NONE;
666 }
667
668 int _bt_add_white_list(bluetooth_device_address_t *device_address, bluetooth_device_address_type_t address_type)
669 {
670         DBusGProxy *proxy;
671         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
672         GError *error = NULL;
673
674         if (__bt_is_factory_test_mode()) {
675                 BT_ERR("Unable to add white list in factory binary !!");
676                 return BLUETOOTH_ERROR_NOT_SUPPORT;
677         }
678
679         BT_CHECK_PARAMETER(device_address, return);
680
681         if (address_type != BLUETOOTH_DEVICE_PUBLIC_ADDRESS &&
682                 address_type != BLUETOOTH_DEVICE_RANDOM_ADDRESS)
683                 return BLUETOOTH_ERROR_INVALID_PARAM;
684
685         _bt_convert_addr_type_to_string(address, device_address->addr);
686
687         proxy = _bt_get_adapter_proxy();
688         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
689
690         dbus_g_proxy_call(proxy, "AddDeviceWhiteList", &error,
691                   G_TYPE_STRING, address,
692                   G_TYPE_UINT, address_type,
693                   G_TYPE_INVALID, G_TYPE_INVALID);
694
695         if (error) {
696                 BT_ERR("AddDeviceWhiteList Fail: %s", error->message);
697                 g_error_free(error);
698                 return BLUETOOTH_ERROR_INTERNAL;
699         }
700
701         BT_INFO("Add white list");
702
703         return BLUETOOTH_ERROR_NONE;
704 }
705
706 int _bt_remove_white_list(bluetooth_device_address_t *device_address, bluetooth_device_address_type_t address_type)
707 {
708         DBusGProxy *proxy;
709         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
710         GError *error = NULL;
711
712         if (__bt_is_factory_test_mode()) {
713                 BT_ERR("Unable to remove white list in factory binary !!");
714                 return BLUETOOTH_ERROR_NOT_SUPPORT;
715         }
716
717         BT_CHECK_PARAMETER(device_address, return);
718
719         if (address_type != BLUETOOTH_DEVICE_PUBLIC_ADDRESS &&
720                 address_type != BLUETOOTH_DEVICE_RANDOM_ADDRESS)
721                 return BLUETOOTH_ERROR_INVALID_PARAM;
722
723         _bt_convert_addr_type_to_string(address, device_address->addr);
724
725         proxy = _bt_get_adapter_proxy();
726         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
727
728         dbus_g_proxy_call(proxy, "RemoveDeviceWhiteList", &error,
729                   G_TYPE_STRING, address,
730                    G_TYPE_UINT, address_type,
731                   G_TYPE_INVALID, G_TYPE_INVALID);
732
733         if (error) {
734                 BT_ERR("RemoveDeviceWhiteList Fail: %s", error->message);
735                 g_error_free(error);
736                 return BLUETOOTH_ERROR_INTERNAL;
737         }
738
739         BT_INFO("Remove white list");
740
741         return BLUETOOTH_ERROR_NONE;
742 }
743
744 int _bt_clear_white_list(void)
745 {
746         DBusGProxy *proxy;
747         GError *error = NULL;
748
749         if (__bt_is_factory_test_mode()) {
750                 BT_ERR("Unable to clear white list in factory binary !!");
751                 return BLUETOOTH_ERROR_NOT_SUPPORT;
752         }
753
754         proxy = _bt_get_adapter_proxy();
755         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
756
757         dbus_g_proxy_call(proxy, "ClearDeviceWhiteList", &error,
758                   G_TYPE_INVALID, G_TYPE_INVALID);
759
760         if (error) {
761                 BT_ERR("ClearDeviceWhiteList Fail: %s", error->message);
762                 g_error_free(error);
763                 return BLUETOOTH_ERROR_INTERNAL;
764         }
765
766         BT_INFO("Clear white list");
767
768         return BLUETOOTH_ERROR_NONE;
769 }
770