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