Merge latest tizen_3.0 bug fix codes
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-dpm.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifdef TIZEN_FEATURE_BT_DPM
18
19 #include <glib.h>
20 #include <gio/gio.h>
21 #include <dlog.h>
22 #include <string.h>
23 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
25 #include <vconf.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-service-common.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-dpm.h"
33
34 static dpm_policy_t policy_table[DPM_POLICY_END] = {
35         [DPM_POLICY_ALLOW_BLUETOOTH] = { {DPM_BT_ERROR} },
36         [DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION] = { {DPM_STATUS_ERROR} },
37         [DPM_POLICY_BLUETOOTH_UUID_RESTRICTION] = { {DPM_STATUS_ERROR} },
38         [DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST] = { { } },
39         [DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST] = { { } },
40         [DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST] = { { } },
41         [DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST] = { { } },
42         [DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL] = { {DPM_STATUS_ERROR} },
43         [DPM_POLICY_BLUETOOTH_PAIRING_STATE] = { {DPM_STATUS_ERROR} },
44         [DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE] = { {DPM_STATUS_ERROR} },
45         [DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE] = { {DPM_STATUS_ERROR} },
46         [DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE] = { {DPM_STATUS_ERROR} },
47         [DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE] = { {DPM_STATUS_ERROR} },
48 };
49
50
51 /**
52  * @brief DPM profile state
53  * @see
54  */
55 static dpm_profile_state_t dpm_profile_state[DPM_PROFILE_NONE]  = {
56         [DPM_POLICY_BLUETOOTH_A2DP_PROFILE_STATE] = {DPM_STATUS_ERROR},
57         [DPM_POLICY_BLUETOOTH_AVRCP_PROFILE_STATE] = {DPM_STATUS_ERROR},
58         [DPM_POLICY_BLUETOOTH_BPP_PROFILE_STATE] = {DPM_STATUS_ERROR},
59         [DPM_POLICY_BLUETOOTH_DUN_PROFILE_STATE] = {DPM_STATUS_ERROR},
60         [DPM_POLICY_BLUETOOTH_FTP_PROFILE_STATE] = {DPM_STATUS_ERROR},
61         [DPM_POLICY_BLUETOOTH_HFP_PROFILE_STATE] = {DPM_STATUS_ERROR},
62         [DPM_POLICY_BLUETOOTH_HSP_PROFILE_STATE] = {DPM_STATUS_ERROR},
63         [DPM_POLICY_BLUETOOTH_PBAP_PROFILE_STATE] = {DPM_STATUS_ERROR},
64         [DPM_POLICY_BLUETOOTH_SAP_PROFILE_STATE] = {DPM_STATUS_ERROR},
65         [DPM_POLICY_BLUETOOTH_SPP_PROFILE_STATE] = {DPM_STATUS_ERROR},
66 };
67
68 int _bt_launch_dpm_popup(char *mode)
69 {
70         int ret = 0;
71         bundle *b;
72
73         b = bundle_create();
74         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
75
76         bundle_add(b, "mode", mode);
77
78         ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
79
80         if (ret < 0)
81                 BT_ERR("Popup launch failed: %d\n", ret);
82
83         bundle_free(b);
84
85         return ret;
86 }
87
88 int _bt_dpm_set_allow_bluetooth_mode(dpm_bt_allow_t value)
89 {
90         BT_INFO("_bt_dpm_set_allow_bluetooth_mode");
91
92 #if 0
93         if (value == DPM_BT_ALLOWED && value == DPM_BT_HANDSFREE_ONLY) {
94                 /* Update Bluetooth DPM Status to notify other modules */
95                 if (vconf_set_int(VCONFKEY_BT_DPM_STATUS, value) != 0)
96                         BT_ERR("Set vconf failed\n");
97                         return DPM_RESULT_FAIL;
98         } else {
99                 /* Update Bluetooth DPM Status to notify other modules */
100                 if (vconf_set_int(VCONFKEY_BT_DPM_STATUS, VCONFKEY_BT_DPM_STATUS_RESTRICTED) != 0)
101                         BT_ERR("Set vconf failed\n");
102                         return DPM_RESULT_FAIL;
103         }
104 #endif
105         policy_table[DPM_POLICY_ALLOW_BLUETOOTH].value  = value;
106
107         return BLUETOOTH_ERROR_NONE;
108 }
109
110 int _bt_dpm_get_allow_bluetooth_mode(int *value)
111 {
112         BT_INFO("_bt_dpm_get_allow_bluetooth_mode");
113
114         *value = policy_table[DPM_POLICY_ALLOW_BLUETOOTH].value;
115
116         return BLUETOOTH_ERROR_NONE;
117 }
118
119 int _bt_dpm_activate_bluetooth_device_restriction(dpm_status_t value)
120 {
121         int allow_bt = DPM_BT_ERROR;
122         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
123
124         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
125         if (allow_bt == DPM_BT_RESTRICTED)
126                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
127
128         policy_table[DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION].value  = value;
129
130         return BLUETOOTH_ERROR_NONE;
131 }
132
133 int _bt_dpm_is_bluetooth_device_restriction_active(int *value)
134 {
135         int allow_bt = DPM_BT_ERROR;
136
137         BT_INFO("_bt_dpm_is_bluetooth_device_restriction_active");
138
139         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
140         if (allow_bt == DPM_BT_RESTRICTED)
141                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
142
143         *value = policy_table[DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION].value;
144
145         return BLUETOOTH_ERROR_NONE;
146 }
147
148 dpm_result_t _bt_dpm_activate_bluetoooth_uuid_restriction(dpm_status_t value)
149 {
150         int allow_bt = DPM_BT_ERROR;
151
152         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
153
154         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
155         if (allow_bt == DPM_BT_RESTRICTED)
156                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
157
158         policy_table[DPM_POLICY_BLUETOOTH_UUID_RESTRICTION].value  = value;
159
160         return BLUETOOTH_ERROR_NONE;
161 }
162
163 dpm_status_t _bt_dpm_is_bluetooth_uuid_restriction_active(int *value)
164 {
165         int allow_bt = DPM_BT_ERROR;
166
167         BT_INFO("_bt_dpm_is_bluetooth_uuid_restriction_active");
168
169         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
170         if (allow_bt == DPM_BT_RESTRICTED)
171                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
172
173         *value = policy_table[DPM_POLICY_BLUETOOTH_UUID_RESTRICTION].value;
174
175         return BLUETOOTH_ERROR_NONE;
176 }
177
178 dpm_result_t _bt_dpm_add_bluetooth_devices_to_blacklist(bluetooth_device_address_t *bd_addr)
179 {
180         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
181         char *dev_addr = NULL;
182         int allow_bt = DPM_BT_ERROR;
183
184         BT_INFO("_bt_dpm_add_bluetooth_devices_to_blacklist");
185
186         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
187         if (allow_bt == DPM_BT_RESTRICTED)
188                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
189
190         BT_CHECK_PARAMETER(bd_addr, return);
191
192         _bt_convert_addr_type_to_string(device_address,
193                         (unsigned char *)bd_addr->addr);
194
195         dev_addr = g_strdup(device_address);
196         if (!dev_addr)
197                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
198
199         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list, dev_addr);
200
201         return BLUETOOTH_ERROR_NONE;
202 }
203
204 int _bt_dpm_get_bluetooth_devices_from_blacklist(GArray **out_param1)
205 {
206         int ret = BLUETOOTH_ERROR_INTERNAL;
207         int allow_bt = DPM_BT_ERROR;
208         bt_dpm_device_list_t device_list;
209         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list;
210         int i = 0;
211
212         BT_INFO("_bt_dpm_get_bluetooth_devices_from_blacklist");
213
214         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
215         if (allow_bt == DPM_BT_RESTRICTED)
216                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
217
218         if (list) {
219                 ret = BLUETOOTH_ERROR_NONE;
220                 for (; list; list = list->next, i++) {
221                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
222                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
223                 }
224                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list);
225                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
226         } else {
227                 ret = BLUETOOTH_ERROR_NONE;
228                 device_list.count = 0;
229                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
230         }
231         return ret;
232 }
233
234 int _bt_dpm_add_bluetooth_devices_to_whitelist(bluetooth_device_address_t *bd_addr)
235 {
236         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
237         char *dev_addr = NULL;
238         int allow_bt = DPM_BT_ERROR;
239
240         BT_INFO("_bt_dpm_add_bluetooth_devices_to_whitelist");
241
242         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
243         if (allow_bt == DPM_BT_RESTRICTED)
244                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
245
246         BT_CHECK_PARAMETER(bd_addr, return);
247
248         _bt_convert_addr_type_to_string(device_address,
249                         (unsigned char *)bd_addr->addr);
250
251         dev_addr = g_strdup(device_address);
252         if (!dev_addr)
253                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
254         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, dev_addr);
255         return BLUETOOTH_ERROR_NONE;
256 }
257
258 int _bt_dpm_get_bluetooth_devices_from_whitelist(GArray **out_param1)
259 {
260         dpm_result_t ret = BLUETOOTH_ERROR_INTERNAL;
261         int allow_bt = DPM_BT_ERROR;
262         bt_dpm_device_list_t device_list;
263         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list;
264         int i = 0;
265
266         BT_INFO("_bt_dpm_get_bluetooth_devices_from_whitelist");
267
268         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
269         if (allow_bt == DPM_BT_RESTRICTED)
270                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
271
272         if (list) {
273                 ret = BLUETOOTH_ERROR_NONE;
274                 for (; list; list = list->next, i++) {
275                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
276                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
277
278                 }
279                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list);
280                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
281         } else {
282                 ret = BLUETOOTH_ERROR_NONE;
283                 device_list.count = 0;
284                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
285         }
286         return ret;
287 }
288
289 int _bt_dpm_add_bluetooth_uuids_to_blacklist(const char *uuid)
290 {
291         char *l_uuid;
292         int allow_bt = DPM_BT_ERROR;
293
294         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_blacklist");
295
296         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
297         if (allow_bt == DPM_BT_RESTRICTED)
298                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
299
300         l_uuid = g_strdup(uuid);
301         if (!l_uuid)
302                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
303
304         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
305         return BLUETOOTH_ERROR_NONE;
306 }
307
308 int _bt_dpm_get_bluetooth_uuids_from_blacklist(GArray **out_param1)
309 {
310         int ret = BLUETOOTH_ERROR_INTERNAL;
311         int allow_bt = DPM_BT_ERROR;
312         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
313         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list;
314         int i = 0;
315
316         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_blacklist");
317
318         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
319         if (allow_bt == DPM_BT_RESTRICTED)
320                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
321
322         if (list) {
323                 ret = BLUETOOTH_ERROR_NONE;
324                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list);
325                 for (; list; list = list->next, i++) {
326                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
327                         g_strlcpy(uuids_list.uuids[i], list->data,
328                                 BLUETOOTH_UUID_STRING_MAX);
329                 }
330                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
331         } else {
332                 ret = BLUETOOTH_ERROR_NONE;
333                 uuids_list.count = 0;
334                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
335         }
336
337         return ret;
338 }
339
340 int _bt_dpm_add_bluetooth_uuids_to_whitelist(const char *uuid)
341 {
342         char *l_uuid;
343         int allow_bt = DPM_BT_ERROR;
344
345         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_whitelist");
346
347         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
348         if (allow_bt == DPM_BT_RESTRICTED)
349                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
350
351         l_uuid = g_strdup(uuid);
352         if (!l_uuid)
353                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
354
355         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
356         return BLUETOOTH_ERROR_NONE;
357 }
358
359
360 int _bt_dpm_get_bluetooth_uuids_from_whitelist(GArray **out_param1)
361 {
362         int ret = BLUETOOTH_ERROR_INTERNAL;
363         int allow_bt = DPM_BT_ERROR;
364         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
365         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list;
366         int i = 0;
367
368         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_whitelist");
369
370         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
371         if (allow_bt == DPM_BT_RESTRICTED)
372                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
373
374         if (list) {
375                 ret = BLUETOOTH_ERROR_NONE;
376                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list);
377                 for (; list; list = list->next, i++) {
378                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
379                         g_strlcpy(uuids_list.uuids[i], list->data,
380                                 BLUETOOTH_UUID_STRING_MAX);
381                 }
382                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
383         } else {
384                 ret = BLUETOOTH_ERROR_NONE;
385                 uuids_list.count = 0;
386                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
387         }
388
389         return ret;
390
391 }
392
393 int _bt_dpm_set_allow_bluetooth_outgoing_call(dpm_status_t value)
394 {
395         int allow_bt = DPM_BT_ERROR;
396
397         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
398
399         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
400         if (allow_bt == DPM_BT_RESTRICTED)
401                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
402
403         policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value  = value;
404
405         return BLUETOOTH_ERROR_NONE;
406 }
407
408 int _bt_dpm_get_allow_bluetooth_outgoing_call(int *value)
409 {
410         int allow_bt = DPM_BT_ERROR;
411
412         BT_INFO("_bt_dpm_get_allow_bluetooth_outgoing_call");
413
414         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
415         if (allow_bt == DPM_BT_RESTRICTED)
416                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
417
418         *value = policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value;
419
420         return BLUETOOTH_ERROR_NONE;
421 }
422
423 int _bt_dpm_clear_bluetooth_devices_from_blacklist(void)
424 {
425         GSList *l = NULL;
426         int allow_bt = DPM_BT_ERROR;
427
428         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
429
430         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
431         if (allow_bt == DPM_BT_RESTRICTED)
432                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
433
434         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
435                 char *address = l->data;
436                 if (address) {
437                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list, address);
438                         g_free(address);
439                 }
440         }
441         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list);
442         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = NULL;
443
444         return BLUETOOTH_ERROR_NONE;
445 }
446
447 int _bt_dpm_clear_bluetooth_devices_from_whitelist(void)
448 {
449         GSList *l = NULL;
450         int allow_bt = DPM_BT_ERROR;
451
452         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
453
454         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
455         if (allow_bt == DPM_BT_RESTRICTED)
456                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
457
458         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
459                 char *address = l->data;
460                 if (address) {
461                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, address);
462                         g_free(address);
463                 }
464         }
465         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list);
466         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = NULL;
467
468         return BLUETOOTH_ERROR_NONE;
469 }
470
471 int _bt_dpm_clear_bluetooth_uuids_from_blacklist(void)
472 {
473         GSList *l = NULL;
474         int allow_bt = DPM_BT_ERROR;
475
476         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
477
478         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
479         if (allow_bt == DPM_BT_RESTRICTED)
480                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
481
482         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
483                 char *l_uuid = l->data;
484                 if (l_uuid) {
485                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
486                         g_free(l_uuid);
487                 }
488         }
489         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list);
490         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = NULL;
491
492         return BLUETOOTH_ERROR_NONE;
493 }
494
495 int _bt_dpm_clear_bluetooth_uuids_from_whitelist(void)
496 {
497         GSList *l = NULL;
498         int allow_bt = DPM_BT_ERROR;
499
500         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_whitelist");
501
502         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
503         if (allow_bt == DPM_BT_RESTRICTED)
504                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
505
506         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
507                 char *l_uuid = l->data;
508                 if (l_uuid) {
509                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
510                         g_free(l_uuid);
511                 }
512         }
513         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list);
514         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = NULL;
515
516         return BLUETOOTH_ERROR_NONE;
517 }
518
519 int _bt_dpm_set_bluetooth_pairing_state(dpm_status_t value)
520 {
521         int allow_bt = DPM_BT_ERROR;
522
523         BT_INFO("_bt_dpm_set_bluetooth_pairing_state");
524
525         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
526         if (allow_bt == DPM_BT_RESTRICTED)
527                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
528
529         policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value = value;
530
531         return BLUETOOTH_ERROR_NONE;
532 }
533
534 int _bt_dpm_get_bluetooth_pairing_state(int *value)
535 {
536         int allow_bt = DPM_BT_ERROR;
537
538         BT_INFO("_bt_dpm_get_bluetooth_pairing_state");
539
540         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
541         if (allow_bt == DPM_BT_RESTRICTED)
542                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
543
544         *value = policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value;
545
546         return BLUETOOTH_ERROR_NONE;
547 }
548
549 int _bt_dpm_set_bluetooth_profile_state(dpm_profile_t profile, dpm_status_t value)
550 {
551         int allow_bt = DPM_BT_ERROR;
552
553         BT_INFO("_bt_dpm_set_bluetooth_profile_state");
554
555         if (profile >= DPM_PROFILE_NONE)
556                 return BLUETOOTH_ERROR_INTERNAL;
557
558         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
559         if (allow_bt == DPM_BT_RESTRICTED)
560                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
561
562         dpm_profile_state[profile].value = value;
563
564         return BLUETOOTH_ERROR_NONE;
565 }
566
567 int _bt_dpm_get_bluetooth_profile_state(dpm_profile_t profile, int *value)
568 {
569         int allow_bt = DPM_BT_ERROR;
570
571         BT_INFO("_bt_dpm_get_bluetooth_profile_state");
572
573         if (profile >= DPM_PROFILE_NONE)
574                 return BLUETOOTH_ERROR_INTERNAL;
575
576         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
577         if (allow_bt == DPM_BT_RESTRICTED)
578                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
579
580         *value = dpm_profile_state[profile].value;
581
582         return BLUETOOTH_ERROR_NONE;
583 }
584
585 int _bt_dpm_set_bluetooth_desktop_connectivity_state(dpm_status_t value)
586 {
587         int allow_bt = DPM_BT_ERROR;
588
589         BT_INFO("_bt_dpm_set_bluetooth_desktop_connectivity_state");
590
591         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
592         if (allow_bt == DPM_BT_RESTRICTED)
593                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
594
595         policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value = value;
596
597         return BLUETOOTH_ERROR_NONE;
598 }
599
600 int _bt_dpm_get_bluetooth_desktop_connectivity_state(int *value)
601 {
602         int allow_bt = DPM_BT_ERROR;
603
604         BT_INFO("_bt_dpm_get_bluetooth_desktop_connectivity_state");
605
606         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
607         if (allow_bt == DPM_BT_RESTRICTED)
608                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
609
610         *value = policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value;
611
612         return BLUETOOTH_ERROR_NONE;
613 }
614
615 int _bt_dpm_set_bluetooth_discoverable_state(dpm_status_t value)
616 {
617         int allow_bt = DPM_BT_ERROR;
618
619         BT_INFO("_bt_dpm_set_bluetooth_discoverable_state");
620
621         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
622         if (allow_bt == DPM_BT_RESTRICTED)
623                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
624
625         if (value == DPM_RESTRICTED) {
626                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
627                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
628         }
629
630         policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value = value;
631
632         return BLUETOOTH_ERROR_NONE;
633 }
634
635 int _bt_dpm_get_bluetooth_discoverable_state(int *value)
636 {
637         int allow_bt = DPM_BT_ERROR;
638
639         BT_INFO("_bt_dpm_get_bluetooth_discoverable_state");
640
641         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
642         if (allow_bt == DPM_BT_RESTRICTED)
643                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
644
645         *value = policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value;
646
647         return BLUETOOTH_ERROR_NONE;
648 }
649
650 int _bt_dpm_set_bluetooth_limited_discoverable_state(dpm_status_t value)
651 {
652         int allow_bt = DPM_BT_ERROR;
653
654         BT_INFO("_bt_dpm_set_bluetooth_limited_discoverable_state");
655
656         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
657         if (allow_bt == DPM_BT_RESTRICTED)
658                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
659
660         if (value == DPM_RESTRICTED) {
661                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
662                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
663         }
664
665         policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value = value;
666
667         return BLUETOOTH_ERROR_NONE;
668 }
669
670 int _bt_dpm_get_bluetooth_limited_discoverable_state(int *value)
671 {
672         int allow_bt = DPM_BT_ERROR;
673
674         BT_INFO("_bt_dpm_get_bluetooth_limited_discoverable_state");
675
676         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
677         if (allow_bt == DPM_BT_RESTRICTED)
678                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
679
680         *value = policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value;
681
682         return BLUETOOTH_ERROR_NONE;
683 }
684
685 int _bt_dpm_set_bluetooth_data_transfer_state(dpm_status_t value)
686 {
687         int allow_bt = DPM_BT_ERROR;
688
689         BT_INFO("_bt_dpm_set_bluetooth_data_transfer_state");
690
691         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
692         if (allow_bt == DPM_BT_RESTRICTED)
693                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
694
695         policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value = value;
696         return BLUETOOTH_ERROR_NONE;
697 }
698
699 int _bt_dpm_get_allow_bluetooth_data_transfer_state(int *value)
700 {
701         int allow_bt = DPM_BT_ERROR;
702
703         BT_INFO("_bt_dpm_get_allow_bluetooth_data_transfer_state");
704
705         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
706         if (allow_bt == DPM_BT_RESTRICTED)
707                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
708
709         *value = policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value;
710
711         return BLUETOOTH_ERROR_NONE;
712 }
713
714 int _bt_dpm_remove_bluetooth_devices_from_whitelist(bluetooth_device_address_t *device_address)
715 {
716         GSList *l = NULL;
717         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
718         int allow_bt = DPM_BT_ERROR;
719
720         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_whitelist");
721
722         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
723         if (allow_bt == DPM_BT_RESTRICTED)
724                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
725
726         _bt_convert_addr_type_to_string(bd_addr,
727                         (unsigned char *)device_address->addr);
728
729         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
730                 char *l_device = l->data;
731                 if (l_device && g_strcmp0(l_device, bd_addr)) {
732                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
733                         g_free(l_device);
734                 }
735         }
736         return BLUETOOTH_ERROR_NONE;
737 }
738
739 int _bt_dpm_remove_bluetooth_devices_from_blacklist(bluetooth_device_address_t *device_address)
740 {
741         GSList *l = NULL;
742         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
743         int allow_bt = DPM_BT_ERROR;
744
745         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_blacklist");
746
747         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
748         if (allow_bt == DPM_BT_RESTRICTED)
749                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
750
751         _bt_convert_addr_type_to_string(bd_addr,
752                         (unsigned char *)device_address->addr);
753
754         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
755                 char *l_device = l->data;
756                 if (l_device && g_strcmp0(l_device, bd_addr)) {
757                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
758                         g_free(l_device);
759                 }
760         }
761
762         return BLUETOOTH_ERROR_NONE;
763 }
764
765 int _bt_dpm_remove_bluetooth_uuids_from_whitelist(const char *uuids)
766 {
767         GSList *l = NULL;
768         int allow_bt = DPM_BT_ERROR;
769
770         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_whitelist");
771
772         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
773         if (allow_bt == DPM_BT_RESTRICTED)
774                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
775
776         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
777                 char *l_uuid = l->data;
778                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
779                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
780                         g_free(l_uuid);
781                 }
782         }
783         return BLUETOOTH_ERROR_NONE;
784 }
785
786 int _bt_dpm_remove_bluetooth_uuids_from_blacklist(const char *uuids)
787 {
788         GSList *l = NULL;
789         int allow_bt = DPM_BT_ERROR;
790
791         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_blacklist");
792
793         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
794         if (allow_bt == DPM_BT_RESTRICTED)
795                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
796
797         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
798                 char *l_uuid = l->data;
799                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
800                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
801                         g_free(l_uuid);
802                 }
803         }
804         return BLUETOOTH_ERROR_NONE;
805 }
806
807 int _bt_dpm_clear_bluetooth_uuids_from_list(void)
808 {
809         int allow_bt = DPM_BT_ERROR;
810         int err = BLUETOOTH_ERROR_INTERNAL;
811
812         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_list");
813
814         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
815         if (allow_bt == DPM_BT_RESTRICTED)
816                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
817
818         err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
819         if (!err)
820                 err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
821
822         return err;
823 }
824
825 int _bt_dpm_clear_bluetooth_devices_from_list(void)
826 {
827         int allow_bt = DPM_BT_ERROR;
828         int err = BLUETOOTH_ERROR_INTERNAL;
829
830         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_list");
831
832         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
833         if (allow_bt == DPM_BT_RESTRICTED)
834                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
835
836         err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
837         if (!err)
838                 err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
839
840         return err;
841 }
842 #endif /* #ifdef TIZEN_FEATURE_BT_DPM */