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