a0c354d417b53d901edc58952c5e33af9e377aaa
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / 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-core-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         /* Disconnect if connected blacklist device is existing */
181         // TODO: need to implement disconnect logic
182
183         return BLUETOOTH_ERROR_NONE;
184 }
185
186 int _bt_dpm_get_bluetooth_devices_from_blacklist(GArray **out_param1)
187 {
188         int ret = BLUETOOTH_ERROR_INTERNAL;
189         int allow_bt = DPM_BT_ERROR;
190         bt_dpm_device_list_t device_list = { 0, };
191         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list;
192         int i = 0;
193
194         BT_INFO("_bt_dpm_get_bluetooth_devices_from_blacklist");
195
196         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
197         if (allow_bt == DPM_BT_RESTRICTED)
198                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
199
200         if (list) {
201                 ret = BLUETOOTH_ERROR_NONE;
202                 for (; list; list = list->next, i++) {
203                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
204                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
205                 }
206                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list);
207                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
208         } else {
209                 ret = BLUETOOTH_ERROR_NONE;
210                 device_list.count = 0;
211                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
212         }
213         return ret;
214 }
215
216 int _bt_dpm_add_bluetooth_devices_to_whitelist(bluetooth_device_address_t *bd_addr)
217 {
218         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
219         char *dev_addr = NULL;
220         int allow_bt = DPM_BT_ERROR;
221
222         BT_INFO("_bt_dpm_add_bluetooth_devices_to_whitelist");
223
224         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
225         if (allow_bt == DPM_BT_RESTRICTED)
226                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
227
228         BT_CHECK_PARAMETER(bd_addr, return);
229
230         _bt_convert_addr_type_to_string(device_address,
231                         (unsigned char *)bd_addr->addr);
232
233         dev_addr = g_strdup(device_address);
234         if (!dev_addr)
235                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
236         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, dev_addr);
237         return BLUETOOTH_ERROR_NONE;
238 }
239
240 int _bt_dpm_get_bluetooth_devices_from_whitelist(GArray **out_param1)
241 {
242         dpm_result_t ret = BLUETOOTH_ERROR_INTERNAL;
243         int allow_bt = DPM_BT_ERROR;
244         bt_dpm_device_list_t device_list = { 0, };
245         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list;
246         int i = 0;
247
248         BT_INFO("_bt_dpm_get_bluetooth_devices_from_whitelist");
249
250         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
251         if (allow_bt == DPM_BT_RESTRICTED)
252                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
253
254         if (list) {
255                 ret = BLUETOOTH_ERROR_NONE;
256                 for (; list; list = list->next, i++) {
257                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
258                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
259
260                 }
261                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list);
262                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
263         } else {
264                 ret = BLUETOOTH_ERROR_NONE;
265                 device_list.count = 0;
266                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
267         }
268         return ret;
269 }
270
271 int _bt_dpm_add_bluetooth_uuids_to_blacklist(const char *uuid)
272 {
273         char *l_uuid;
274         int allow_bt = DPM_BT_ERROR;
275         //GArray *addr_list = NULL;
276         //int i = 0;
277
278         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_blacklist");
279
280         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
281         if (allow_bt == DPM_BT_RESTRICTED)
282                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
283
284         l_uuid = g_strdup(uuid);
285         if (!l_uuid)
286                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
287
288         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
289
290 #if 0 /* Use device functions if this logic required in later */
291         /* Disconnect if connected blacklist uuid is existing */
292         addr_list = g_array_new(FALSE, FALSE, sizeof(gchar));
293         _bt_get_profile_connected_devices((char *)uuid, &addr_list);
294         for (i = 0; i < (addr_list->len / sizeof(bluetooth_device_address_t)); i++) {
295                 char address[BT_ADDRESS_STRING_SIZE];
296
297                 bluetooth_device_address_t *addr = &g_array_index(addr_list, bluetooth_device_address_t, i);
298                 _bt_convert_addr_type_to_string(address, addr->addr);
299                 BT_INFO("device[%s] is in blacklist uuid, will be disconnected", address);
300                 // TODO: need to implement disconnect logic
301         }
302         g_array_free(addr_list, TRUE);
303 #endif
304
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
565 #if 0 /* Use other functions if this logic required in later */
566         /* In case of restriction, disconnect if connected profile is existing */
567         if (value == DPM_RESTRICTED) {
568                 char *uuid = NULL;
569                 GArray *addr_list = NULL;
570                 int i = 0;
571
572                 switch (profile) {
573                 case DPM_POLICY_BLUETOOTH_A2DP_PROFILE_STATE:
574                         uuid = BT_A2DP_UUID;
575                         break;
576                 case DPM_POLICY_BLUETOOTH_AVRCP_PROFILE_STATE:
577                         uuid = BT_AVRCP_TARGET_UUID;
578                         break;
579                 case DPM_POLICY_BLUETOOTH_BPP_PROFILE_STATE:
580                         uuid = "00001118-0000-1000-8000-00805f9b34fb"; // TODO: need to add definition and below also.
581                         break;
582                 case DPM_POLICY_BLUETOOTH_DUN_PROFILE_STATE:
583                         uuid = "00001103-0000-1000-8000-00805f9b34fb";
584                         break;
585                 case DPM_POLICY_BLUETOOTH_FTP_PROFILE_STATE:
586                         uuid = BT_FTP_UUID;
587                         break;
588                 case DPM_POLICY_BLUETOOTH_HFP_PROFILE_STATE:
589                         uuid = "0000111e-0000-1000-8000-00805f9b34fb";
590                         break;
591                 case DPM_POLICY_BLUETOOTH_HSP_PROFILE_STATE:
592                         uuid = "00001108-0000-1000-8000-00805f9b34fb";
593                         break;
594                 case DPM_POLICY_BLUETOOTH_PBAP_PROFILE_STATE:
595                         uuid = BT_OBEX_PSE_UUID;
596                         break;
597                 case DPM_POLICY_BLUETOOTH_SAP_PROFILE_STATE:
598                         uuid = "0000112d-0000-1000-8000-00805f9b34fb";
599                         break;
600                 case DPM_POLICY_BLUETOOTH_SPP_PROFILE_STATE:
601                         uuid = BT_SPP_UUID;
602                         break;
603                 default:
604                         BT_ERR("Unknown profile %d", profile);
605                         return BLUETOOTH_ERROR_INTERNAL;
606                 }
607
608                 addr_list = g_array_new(FALSE, FALSE, sizeof(gchar));
609                 _bt_get_profile_connected_devices(uuid, &addr_list);
610                 for (i = 0; i < (addr_list->len / sizeof(bluetooth_device_address_t)); i++) {
611                         char address[BT_ADDRESS_STRING_SIZE];
612
613                         bluetooth_device_address_t *addr = &g_array_index(addr_list, bluetooth_device_address_t, i);
614                         _bt_convert_addr_type_to_string(address, addr->addr);
615                         BT_INFO("device[%s] is in blacklist uuid, will be disconnected", address);
616                         // TODO: need to implement disconnect logic
617                 }
618                 g_array_free(addr_list, TRUE);
619         }
620 #endif
621
622         return BLUETOOTH_ERROR_NONE;
623 }
624
625 int _bt_dpm_get_bluetooth_profile_state(dpm_profile_t profile, int *value)
626 {
627         int allow_bt = DPM_BT_ERROR;
628
629         BT_INFO("_bt_dpm_get_bluetooth_profile_state");
630
631         if (profile >= DPM_PROFILE_NONE)
632                 return BLUETOOTH_ERROR_INTERNAL;
633
634         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
635         if (allow_bt == DPM_BT_RESTRICTED)
636                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
637
638         *value = dpm_profile_state[profile].value;
639
640         return BLUETOOTH_ERROR_NONE;
641 }
642
643 int _bt_dpm_set_bluetooth_desktop_connectivity_state(dpm_status_t value)
644 {
645         int allow_bt = DPM_BT_ERROR;
646
647         BT_INFO("_bt_dpm_set_bluetooth_desktop_connectivity_state");
648
649         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
650         if (allow_bt == DPM_BT_RESTRICTED)
651                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
652
653         policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value = value;
654
655         return BLUETOOTH_ERROR_NONE;
656 }
657
658 int _bt_dpm_get_bluetooth_desktop_connectivity_state(int *value)
659 {
660         int allow_bt = DPM_BT_ERROR;
661
662         BT_INFO("_bt_dpm_get_bluetooth_desktop_connectivity_state");
663
664         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
665         if (allow_bt == DPM_BT_RESTRICTED)
666                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
667
668         *value = policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value;
669
670         return BLUETOOTH_ERROR_NONE;
671 }
672
673 int _bt_dpm_set_bluetooth_discoverable_state(dpm_status_t value)
674 {
675         int allow_bt = DPM_BT_ERROR;
676
677         BT_INFO("_bt_dpm_set_bluetooth_discoverable_state");
678
679         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
680         if (allow_bt == DPM_BT_RESTRICTED)
681                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
682
683         if (value == DPM_RESTRICTED) {
684                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
685                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
686         }
687
688         policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value = value;
689
690         return BLUETOOTH_ERROR_NONE;
691 }
692
693 int _bt_dpm_get_bluetooth_discoverable_state(int *value)
694 {
695         int allow_bt = DPM_BT_ERROR;
696
697         BT_INFO("_bt_dpm_get_bluetooth_discoverable_state");
698
699         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
700         if (allow_bt == DPM_BT_RESTRICTED)
701                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
702
703         *value = policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value;
704
705         return BLUETOOTH_ERROR_NONE;
706 }
707
708 int _bt_dpm_set_bluetooth_limited_discoverable_state(dpm_status_t value)
709 {
710         int allow_bt = DPM_BT_ERROR;
711
712         BT_INFO("_bt_dpm_set_bluetooth_limited_discoverable_state");
713
714         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
715         if (allow_bt == DPM_BT_RESTRICTED)
716                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
717
718         if (value == DPM_RESTRICTED) {
719                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
720                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
721         }
722
723         policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value = value;
724
725         return BLUETOOTH_ERROR_NONE;
726 }
727
728 int _bt_dpm_get_bluetooth_limited_discoverable_state(int *value)
729 {
730         int allow_bt = DPM_BT_ERROR;
731
732         BT_INFO("_bt_dpm_get_bluetooth_limited_discoverable_state");
733
734         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
735         if (allow_bt == DPM_BT_RESTRICTED)
736                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
737
738         *value = policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value;
739
740         return BLUETOOTH_ERROR_NONE;
741 }
742
743 int _bt_dpm_set_bluetooth_data_transfer_state(dpm_status_t value)
744 {
745         int allow_bt = DPM_BT_ERROR;
746
747         BT_INFO("_bt_dpm_set_bluetooth_data_transfer_state");
748
749         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
750         if (allow_bt == DPM_BT_RESTRICTED)
751                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
752
753         policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value = value;
754         return BLUETOOTH_ERROR_NONE;
755 }
756
757 int _bt_dpm_get_allow_bluetooth_data_transfer_state(int *value)
758 {
759         int allow_bt = DPM_BT_ERROR;
760
761         BT_INFO("_bt_dpm_get_allow_bluetooth_data_transfer_state");
762
763         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
764         if (allow_bt == DPM_BT_RESTRICTED)
765                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
766
767         *value = policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value;
768
769         return BLUETOOTH_ERROR_NONE;
770 }
771
772 int _bt_dpm_remove_bluetooth_devices_from_whitelist(bluetooth_device_address_t *device_address)
773 {
774         GSList *l = NULL;
775         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
776         int allow_bt = DPM_BT_ERROR;
777
778         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_whitelist");
779
780         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
781         if (allow_bt == DPM_BT_RESTRICTED)
782                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
783
784         _bt_convert_addr_type_to_string(bd_addr,
785                         (unsigned char *)device_address->addr);
786
787         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
788                 char *l_device = l->data;
789                 if (l_device && g_strcmp0(l_device, bd_addr)) {
790                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
791                         g_free(l_device);
792                 }
793         }
794         return BLUETOOTH_ERROR_NONE;
795 }
796
797 int _bt_dpm_remove_bluetooth_devices_from_blacklist(bluetooth_device_address_t *device_address)
798 {
799         GSList *l = NULL;
800         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
801         int allow_bt = DPM_BT_ERROR;
802
803         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_blacklist");
804
805         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
806         if (allow_bt == DPM_BT_RESTRICTED)
807                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
808
809         _bt_convert_addr_type_to_string(bd_addr,
810                         (unsigned char *)device_address->addr);
811
812         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
813                 char *l_device = l->data;
814                 if (l_device && g_strcmp0(l_device, bd_addr)) {
815                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
816                         g_free(l_device);
817                 }
818         }
819
820         return BLUETOOTH_ERROR_NONE;
821 }
822
823 int _bt_dpm_remove_bluetooth_uuids_from_whitelist(const char *uuids)
824 {
825         GSList *l = NULL;
826         int allow_bt = DPM_BT_ERROR;
827
828         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_whitelist");
829
830         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
831         if (allow_bt == DPM_BT_RESTRICTED)
832                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
833
834         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
835                 char *l_uuid = l->data;
836                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
837                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
838                         g_free(l_uuid);
839                 }
840         }
841         return BLUETOOTH_ERROR_NONE;
842 }
843
844 int _bt_dpm_remove_bluetooth_uuids_from_blacklist(const char *uuids)
845 {
846         GSList *l = NULL;
847         int allow_bt = DPM_BT_ERROR;
848
849         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_blacklist");
850
851         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
852         if (allow_bt == DPM_BT_RESTRICTED)
853                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
854
855         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
856                 char *l_uuid = l->data;
857                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
858                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
859                         g_free(l_uuid);
860                 }
861         }
862         return BLUETOOTH_ERROR_NONE;
863 }
864
865 int _bt_dpm_clear_bluetooth_uuids_from_list(void)
866 {
867         int allow_bt = DPM_BT_ERROR;
868         int err = BLUETOOTH_ERROR_INTERNAL;
869
870         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_list");
871
872         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
873         if (allow_bt == DPM_BT_RESTRICTED)
874                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
875
876         err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
877         if (!err)
878                 err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
879
880         return err;
881 }
882
883 int _bt_dpm_clear_bluetooth_devices_from_list(void)
884 {
885         int allow_bt = DPM_BT_ERROR;
886         int err = BLUETOOTH_ERROR_INTERNAL;
887
888         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_list");
889
890         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
891         if (allow_bt == DPM_BT_RESTRICTED)
892                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
893
894         err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
895         if (!err)
896                 err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
897
898         return err;
899 }
900 #endif /* #ifdef TIZEN_FEATURE_BT_DPM */