[bluetooth-frwk] bt_map_client_message_object_h
[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] = { {NULL} },
39         [DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST] = { {NULL} },
40         [DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST] = { {NULL} },
41         [DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST] = { {NULL} },
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         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list);
489         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = NULL;
490
491         return BLUETOOTH_ERROR_NONE;
492 }
493
494 int _bt_dpm_clear_bluetooth_uuids_from_whitelist(void)
495 {
496         GSList *l = NULL;
497         int allow_bt = DPM_BT_ERROR;
498
499         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_whitelist");
500
501         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
502         if (allow_bt == DPM_BT_RESTRICTED)
503                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
504
505         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
506                 char *l_uuid = l->data;
507                 if (l_uuid) {
508                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
509                         g_free(l_uuid);
510                 }
511         }
512         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list);
513         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = NULL;
514
515         return BLUETOOTH_ERROR_NONE;
516 }
517
518 int _bt_dpm_set_bluetooth_pairing_state(dpm_status_t value)
519 {
520         int allow_bt = DPM_BT_ERROR;
521
522         BT_INFO("_bt_dpm_set_bluetooth_pairing_state");
523
524         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
525         if (allow_bt == DPM_BT_RESTRICTED)
526                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
527
528         policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value = value;
529
530         return BLUETOOTH_ERROR_NONE;
531 }
532
533 int _bt_dpm_get_bluetooth_pairing_state(int *value)
534 {
535         int allow_bt = DPM_BT_ERROR;
536
537         BT_INFO("_bt_dpm_get_bluetooth_pairing_state");
538
539         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
540         if (allow_bt == DPM_BT_RESTRICTED)
541                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
542
543         *value = policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value;
544
545         return BLUETOOTH_ERROR_NONE;
546 }
547
548 int _bt_dpm_set_bluetooth_profile_state(dpm_profile_t profile, dpm_status_t value)
549 {
550         int allow_bt = DPM_BT_ERROR;
551
552         BT_INFO("_bt_dpm_set_bluetooth_profile_state");
553
554         if (profile >= DPM_PROFILE_NONE)
555                 return BLUETOOTH_ERROR_INTERNAL;
556
557         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
558         if (allow_bt == DPM_BT_RESTRICTED)
559                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
560
561         dpm_profile_state[profile].value = value;
562
563         return BLUETOOTH_ERROR_NONE;
564 }
565
566 int _bt_dpm_get_bluetooth_profile_state(dpm_profile_t profile, int *value)
567 {
568         int allow_bt = DPM_BT_ERROR;
569
570         BT_INFO("_bt_dpm_get_bluetooth_profile_state");
571
572         if (profile >= DPM_PROFILE_NONE)
573                 return BLUETOOTH_ERROR_INTERNAL;
574
575         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
576         if (allow_bt == DPM_BT_RESTRICTED)
577                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
578
579         *value = dpm_profile_state[profile].value;
580
581         return BLUETOOTH_ERROR_NONE;
582 }
583
584 int _bt_dpm_set_bluetooth_desktop_connectivity_state(dpm_status_t value)
585 {
586         int allow_bt = DPM_BT_ERROR;
587
588         BT_INFO("_bt_dpm_set_bluetooth_desktop_connectivity_state");
589
590         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
591         if (allow_bt == DPM_BT_RESTRICTED)
592                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
593
594         policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value = value;
595
596         return BLUETOOTH_ERROR_NONE;
597 }
598
599 int _bt_dpm_get_bluetooth_desktop_connectivity_state(int *value)
600 {
601         int allow_bt = DPM_BT_ERROR;
602
603         BT_INFO("_bt_dpm_get_bluetooth_desktop_connectivity_state");
604
605         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
606         if (allow_bt == DPM_BT_RESTRICTED)
607                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
608
609         *value = policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value;
610
611         return BLUETOOTH_ERROR_NONE;
612 }
613
614 int _bt_dpm_set_bluetooth_discoverable_state(dpm_status_t value)
615 {
616         int allow_bt = DPM_BT_ERROR;
617
618         BT_INFO("_bt_dpm_set_bluetooth_discoverable_state");
619
620         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
621         if (allow_bt == DPM_BT_RESTRICTED)
622                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
623
624         if (value == DPM_RESTRICTED) {
625                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
626                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
627         }
628
629         policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value = value;
630
631         return BLUETOOTH_ERROR_NONE;
632 }
633
634 int _bt_dpm_get_bluetooth_discoverable_state(int *value)
635 {
636         int allow_bt = DPM_BT_ERROR;
637
638         BT_INFO("_bt_dpm_get_bluetooth_discoverable_state");
639
640         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
641         if (allow_bt == DPM_BT_RESTRICTED)
642                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
643
644         *value = policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value;
645
646         return BLUETOOTH_ERROR_NONE;
647 }
648
649 int _bt_dpm_set_bluetooth_limited_discoverable_state(dpm_status_t value)
650 {
651         int allow_bt = DPM_BT_ERROR;
652
653         BT_INFO("_bt_dpm_set_bluetooth_limited_discoverable_state");
654
655         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
656         if (allow_bt == DPM_BT_RESTRICTED)
657                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
658
659         if (value == DPM_RESTRICTED) {
660                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
661                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
662         }
663
664         policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value = value;
665
666         return BLUETOOTH_ERROR_NONE;
667 }
668
669 int _bt_dpm_get_bluetooth_limited_discoverable_state(int *value)
670 {
671         int allow_bt = DPM_BT_ERROR;
672
673         BT_INFO("_bt_dpm_get_bluetooth_limited_discoverable_state");
674
675         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
676         if (allow_bt == DPM_BT_RESTRICTED)
677                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
678
679         *value = policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value;
680
681         return BLUETOOTH_ERROR_NONE;
682 }
683
684 int _bt_dpm_set_bluetooth_data_transfer_state(dpm_status_t value)
685 {
686         int allow_bt = DPM_BT_ERROR;
687
688         BT_INFO("_bt_dpm_set_bluetooth_data_transfer_state");
689
690         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
691         if (allow_bt == DPM_BT_RESTRICTED)
692                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
693
694         policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value = value;
695         return BLUETOOTH_ERROR_NONE;
696 }
697
698 int _bt_dpm_get_allow_bluetooth_data_transfer_state(int *value)
699 {
700         int allow_bt = DPM_BT_ERROR;
701
702         BT_INFO("_bt_dpm_get_allow_bluetooth_data_transfer_state");
703
704         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
705         if (allow_bt == DPM_BT_RESTRICTED)
706                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
707
708         *value = policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value;
709
710         return BLUETOOTH_ERROR_NONE;
711 }
712
713 int _bt_dpm_remove_bluetooth_devices_from_whitelist(bluetooth_device_address_t *device_address)
714 {
715         GSList *l = NULL;
716         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
717         int allow_bt = DPM_BT_ERROR;
718
719         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_whitelist");
720
721         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
722         if (allow_bt == DPM_BT_RESTRICTED)
723                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
724
725         _bt_convert_addr_type_to_string(bd_addr,
726                         (unsigned char *)device_address->addr);
727
728         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
729                 char *l_device = l->data;
730                 if (l_device && g_strcmp0(l_device, bd_addr)) {
731                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
732                         g_free(l_device);
733                 }
734         }
735         return BLUETOOTH_ERROR_NONE;
736 }
737
738 int _bt_dpm_remove_bluetooth_devices_from_blacklist(bluetooth_device_address_t *device_address)
739 {
740         GSList *l = NULL;
741         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
742         int allow_bt = DPM_BT_ERROR;
743
744         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_blacklist");
745
746         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
747         if (allow_bt == DPM_BT_RESTRICTED)
748                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
749
750         _bt_convert_addr_type_to_string(bd_addr,
751                         (unsigned char *)device_address->addr);
752
753         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
754                 char *l_device = l->data;
755                 if (l_device && g_strcmp0(l_device, bd_addr)) {
756                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
757                         g_free(l_device);
758                 }
759         }
760
761         return BLUETOOTH_ERROR_NONE;
762 }
763
764 int _bt_dpm_remove_bluetooth_uuids_from_whitelist(const char *uuids)
765 {
766         GSList *l = NULL;
767         int allow_bt = DPM_BT_ERROR;
768
769         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_whitelist");
770
771         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
772         if (allow_bt == DPM_BT_RESTRICTED)
773                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
774
775         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
776                 char *l_uuid = l->data;
777                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
778                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
779                         g_free(l_uuid);
780                 }
781         }
782         return BLUETOOTH_ERROR_NONE;
783 }
784
785 int _bt_dpm_remove_bluetooth_uuids_from_blacklist(const char *uuids)
786 {
787         GSList *l = NULL;
788         int allow_bt = DPM_BT_ERROR;
789
790         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_blacklist");
791
792         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
793         if (allow_bt == DPM_BT_RESTRICTED)
794                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
795
796         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
797                 char *l_uuid = l->data;
798                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
799                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
800                         g_free(l_uuid);
801                 }
802         }
803         return BLUETOOTH_ERROR_NONE;
804 }
805
806 int _bt_dpm_clear_bluetooth_uuids_from_list(void)
807 {
808         int allow_bt = DPM_BT_ERROR;
809         int err = BLUETOOTH_ERROR_INTERNAL;
810
811         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_list");
812
813         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
814         if (allow_bt == DPM_BT_RESTRICTED)
815                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
816
817         err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
818         if (!err)
819                 err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
820
821         return err;
822 }
823
824 int _bt_dpm_clear_bluetooth_devices_from_list(void)
825 {
826         int allow_bt = DPM_BT_ERROR;
827         int err = BLUETOOTH_ERROR_INTERNAL;
828
829         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_list");
830
831         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
832         if (allow_bt == DPM_BT_RESTRICTED)
833                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
834
835         err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
836         if (!err)
837                 err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
838
839         return err;
840 }
841 #endif /* #ifdef TIZEN_FEATURE_BT_DPM */