Remove mobile number from log
[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_ALLOWLIST] = { { } },
36         [DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST] = { { } },
37         [DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST] = { { } },
38         [DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST] = { { } },
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 int _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 int _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 int _bt_dpm_add_bluetooth_devices_to_blocklist(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_blocklist");
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_BLOCKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list, dev_addr);
164
165         /* Disconnect if connected blocklist 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_blocklist(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_BLOCKLIST].list;
177         int i = 0;
178
179         BT_INFO("_bt_dpm_get_bluetooth_devices_from_blocklist");
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_BLOCKLIST].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_allowlist(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_allowlist");
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_ALLOWLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list, dev_addr);
222         return BLUETOOTH_ERROR_NONE;
223 }
224
225 int _bt_dpm_get_bluetooth_devices_from_allowlist(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_ALLOWLIST].list;
231         int i = 0;
232
233         BT_INFO("_bt_dpm_get_bluetooth_devices_from_allowlist");
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_ALLOWLIST].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_blocklist(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_blocklist");
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
274         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list, l_uuid);
275
276         /* Use device functions if this logic required in later */
277         /* Disconnect if connected blocklist uuid is existing */
278         /*
279         addr_list = g_array_new(FALSE, FALSE, sizeof(gchar));
280         _bt_get_profile_connected_devices((char *)uuid, &addr_list);
281         for (i = 0; i < (addr_list->len / sizeof(bluetooth_device_address_t)); i++) {
282                 char address[BT_ADDRESS_STRING_SIZE];
283
284                 bluetooth_device_address_t *addr = &g_array_index(addr_list, bluetooth_device_address_t, i);
285                 _bt_convert_addr_type_to_string(address, addr->addr);
286                 BT_INFO("device[%s] is in blocklist uuid, will be disconnected", address);
287                 // TODO: need to implement disconnect logic
288         }
289         g_array_free(addr_list, TRUE);
290         */
291
292         return BLUETOOTH_ERROR_NONE;
293 }
294
295 int _bt_dpm_get_bluetooth_uuids_from_blocklist(GArray **out_param1)
296 {
297         int ret = BLUETOOTH_ERROR_INTERNAL;
298         int allow_bt = DPM_BT_ERROR;
299         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
300         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list;
301         int i = 0;
302
303         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_blocklist");
304
305         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
306         if (allow_bt == DPM_BT_RESTRICTED)
307                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
308
309         if (list) {
310                 ret = BLUETOOTH_ERROR_NONE;
311                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list);
312                 for (; list; list = list->next, i++) {
313                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
314                         g_strlcpy(uuids_list.uuids[i], list->data,
315                                 BLUETOOTH_UUID_STRING_MAX);
316                 }
317                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
318         } else {
319                 ret = BLUETOOTH_ERROR_NONE;
320                 uuids_list.count = 0;
321                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
322         }
323
324         return ret;
325 }
326
327 int _bt_dpm_add_bluetooth_uuids_to_allowlist(const char *uuid)
328 {
329         char *l_uuid;
330         int allow_bt = DPM_BT_ERROR;
331
332         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_allowlist");
333
334         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
335         if (allow_bt == DPM_BT_RESTRICTED)
336                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
337
338         l_uuid = g_strdup(uuid);
339         if (!l_uuid)
340                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
341
342         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list, l_uuid);
343         return BLUETOOTH_ERROR_NONE;
344 }
345
346
347 int _bt_dpm_get_bluetooth_uuids_from_allowlist(GArray **out_param1)
348 {
349         int ret = BLUETOOTH_ERROR_INTERNAL;
350         int allow_bt = DPM_BT_ERROR;
351         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
352         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list;
353         int i = 0;
354
355         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_allowlist");
356
357         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
358         if (allow_bt == DPM_BT_RESTRICTED)
359                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
360
361         if (list) {
362                 ret = BLUETOOTH_ERROR_NONE;
363                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list);
364                 for (; list; list = list->next, i++) {
365                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
366                         g_strlcpy(uuids_list.uuids[i], list->data,
367                                 BLUETOOTH_UUID_STRING_MAX);
368                 }
369                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
370         } else {
371                 ret = BLUETOOTH_ERROR_NONE;
372                 uuids_list.count = 0;
373                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
374         }
375
376         return ret;
377
378 }
379
380 int _bt_dpm_set_allow_bluetooth_outgoing_call(dpm_status_t value)
381 {
382         int allow_bt = DPM_BT_ERROR;
383
384         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
385
386         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
387         if (allow_bt == DPM_BT_RESTRICTED)
388                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
389
390         policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value  = value;
391
392         return BLUETOOTH_ERROR_NONE;
393 }
394
395 int _bt_dpm_get_allow_bluetooth_outgoing_call(int *value)
396 {
397         int allow_bt = DPM_BT_ERROR;
398
399         BT_INFO("_bt_dpm_get_allow_bluetooth_outgoing_call");
400
401         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
402         if (allow_bt == DPM_BT_RESTRICTED)
403                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
404
405         *value = policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value;
406
407         return BLUETOOTH_ERROR_NONE;
408 }
409
410 int _bt_dpm_clear_bluetooth_devices_from_blocklist(void)
411 {
412         GSList *l = NULL;
413         int allow_bt = DPM_BT_ERROR;
414
415         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blocklist");
416
417         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
418         if (allow_bt == DPM_BT_RESTRICTED)
419                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
420
421         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list; l; l = g_slist_next(l)) {
422                 char *address = l->data;
423                 if (address) {
424                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list, address);
425                         g_free(address);
426                 }
427         }
428         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list);
429         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list = NULL;
430
431         return BLUETOOTH_ERROR_NONE;
432 }
433
434 int _bt_dpm_clear_bluetooth_devices_from_allowlist(void)
435 {
436         GSList *l = NULL;
437         int allow_bt = DPM_BT_ERROR;
438
439         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blocklist");
440
441         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
442         if (allow_bt == DPM_BT_RESTRICTED)
443                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
444
445         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list; l; l = g_slist_next(l)) {
446                 char *address = l->data;
447                 if (address) {
448                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list, address);
449                         g_free(address);
450                 }
451         }
452         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list);
453         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list = NULL;
454
455         return BLUETOOTH_ERROR_NONE;
456 }
457
458 int _bt_dpm_clear_bluetooth_uuids_from_blocklist(void)
459 {
460         GSList *l = NULL;
461         int allow_bt = DPM_BT_ERROR;
462
463         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blocklist");
464
465         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
466         if (allow_bt == DPM_BT_RESTRICTED)
467                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
468
469         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list; l; l = g_slist_next(l)) {
470                 char *l_uuid = l->data;
471                 if (l_uuid) {
472                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list, l_uuid);
473                         g_free(l_uuid);
474                 }
475         }
476         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list);
477         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list = NULL;
478
479         return BLUETOOTH_ERROR_NONE;
480 }
481
482 int _bt_dpm_clear_bluetooth_uuids_from_allowlist(void)
483 {
484         GSList *l = NULL;
485         int allow_bt = DPM_BT_ERROR;
486
487         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_allowlist");
488
489         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
490         if (allow_bt == DPM_BT_RESTRICTED)
491                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
492
493         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list; l; l = g_slist_next(l)) {
494                 char *l_uuid = l->data;
495                 if (l_uuid) {
496                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list, l_uuid);
497                         g_free(l_uuid);
498                 }
499         }
500         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list);
501         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list = NULL;
502
503         return BLUETOOTH_ERROR_NONE;
504 }
505
506 int _bt_dpm_set_bluetooth_pairing_state(dpm_status_t value)
507 {
508         int allow_bt = DPM_BT_ERROR;
509
510         BT_INFO("_bt_dpm_set_bluetooth_pairing_state");
511
512         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
513         if (allow_bt == DPM_BT_RESTRICTED)
514                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
515
516         policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value = value;
517
518         return BLUETOOTH_ERROR_NONE;
519 }
520
521 int _bt_dpm_get_bluetooth_pairing_state(int *value)
522 {
523         int allow_bt = DPM_BT_ERROR;
524
525         BT_INFO("_bt_dpm_get_bluetooth_pairing_state");
526
527         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
528         if (allow_bt == DPM_BT_RESTRICTED)
529                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
530
531         *value = policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value;
532
533         return BLUETOOTH_ERROR_NONE;
534 }
535
536 int _bt_dpm_set_bluetooth_profile_state(dpm_profile_t profile, dpm_status_t value)
537 {
538         int allow_bt = DPM_BT_ERROR;
539
540         BT_INFO("_bt_dpm_set_bluetooth_profile_state");
541
542         if (profile >= DPM_PROFILE_NONE)
543                 return BLUETOOTH_ERROR_INTERNAL;
544
545         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
546         if (allow_bt == DPM_BT_RESTRICTED)
547                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
548
549         dpm_profile_state[profile].value = value;
550
551
552         /* Use other functions if this logic required in later */
553         /* In case of restriction, disconnect if connected profile is existing */
554         /*
555         if (value == DPM_RESTRICTED) {
556                 char *uuid = NULL;
557                 GArray *addr_list = NULL;
558                 int i = 0;
559
560                 switch (profile) {
561                 case DPM_POLICY_BLUETOOTH_A2DP_PROFILE_STATE:
562                         uuid = BT_A2DP_UUID;
563                         break;
564                 case DPM_POLICY_BLUETOOTH_AVRCP_PROFILE_STATE:
565                         uuid = BT_AVRCP_TARGET_UUID;
566                         break;
567                 case DPM_POLICY_BLUETOOTH_BPP_PROFILE_STATE:
568                         uuid = "00001118-0000-1000-8000-00805f9b34fb"; // TODO: need to add definition and below also.
569                         break;
570                 case DPM_POLICY_BLUETOOTH_DUN_PROFILE_STATE:
571                         uuid = "00001103-0000-1000-8000-00805f9b34fb";
572                         break;
573                 case DPM_POLICY_BLUETOOTH_FTP_PROFILE_STATE:
574                         uuid = BT_FTP_UUID;
575                         break;
576                 case DPM_POLICY_BLUETOOTH_HFP_PROFILE_STATE:
577                         uuid = "0000111e-0000-1000-8000-00805f9b34fb";
578                         break;
579                 case DPM_POLICY_BLUETOOTH_HSP_PROFILE_STATE:
580                         uuid = "00001108-0000-1000-8000-00805f9b34fb";
581                         break;
582                 case DPM_POLICY_BLUETOOTH_PBAP_PROFILE_STATE:
583                         uuid = BT_OBEX_PSE_UUID;
584                         break;
585                 case DPM_POLICY_BLUETOOTH_SAP_PROFILE_STATE:
586                         uuid = "0000112d-0000-1000-8000-00805f9b34fb";
587                         break;
588                 case DPM_POLICY_BLUETOOTH_SPP_PROFILE_STATE:
589                         uuid = BT_SPP_UUID;
590                         break;
591                 default:
592                         BT_ERR("Unknown profile %d", profile);
593                         return BLUETOOTH_ERROR_INTERNAL;
594                 }
595
596                 addr_list = g_array_new(FALSE, FALSE, sizeof(gchar));
597                 _bt_get_profile_connected_devices(uuid, &addr_list);
598                 for (i = 0; i < (addr_list->len / sizeof(bluetooth_device_address_t)); i++) {
599                         char address[BT_ADDRESS_STRING_SIZE];
600
601                         bluetooth_device_address_t *addr = &g_array_index(addr_list, bluetooth_device_address_t, i);
602                         _bt_convert_addr_type_to_string(address, addr->addr);
603                         BT_INFO("device[%s] is in blocklist uuid, will be disconnected", address);
604                         // TODO: need to implement disconnect logic
605                 }
606                 g_array_free(addr_list, TRUE);
607         }
608         */
609
610         return BLUETOOTH_ERROR_NONE;
611 }
612
613 int _bt_dpm_get_bluetooth_profile_state(dpm_profile_t profile, int *value)
614 {
615         int allow_bt = DPM_BT_ERROR;
616
617         BT_INFO("_bt_dpm_get_bluetooth_profile_state");
618
619         if (profile >= DPM_PROFILE_NONE)
620                 return BLUETOOTH_ERROR_INTERNAL;
621
622         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
623         if (allow_bt == DPM_BT_RESTRICTED)
624                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
625
626         *value = dpm_profile_state[profile].value;
627
628         return BLUETOOTH_ERROR_NONE;
629 }
630
631 int _bt_dpm_set_bluetooth_desktop_connectivity_state(dpm_status_t value)
632 {
633         int allow_bt = DPM_BT_ERROR;
634
635         BT_INFO("_bt_dpm_set_bluetooth_desktop_connectivity_state");
636
637         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
638         if (allow_bt == DPM_BT_RESTRICTED)
639                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
640
641         policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value = value;
642
643         return BLUETOOTH_ERROR_NONE;
644 }
645
646 int _bt_dpm_get_bluetooth_desktop_connectivity_state(int *value)
647 {
648         int allow_bt = DPM_BT_ERROR;
649
650         BT_INFO("_bt_dpm_get_bluetooth_desktop_connectivity_state");
651
652         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
653         if (allow_bt == DPM_BT_RESTRICTED)
654                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
655
656         *value = policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value;
657
658         return BLUETOOTH_ERROR_NONE;
659 }
660
661 int _bt_dpm_set_bluetooth_discoverable_state(dpm_status_t value)
662 {
663         int allow_bt = DPM_BT_ERROR;
664
665         BT_INFO("_bt_dpm_set_bluetooth_discoverable_state");
666
667         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
668         if (allow_bt == DPM_BT_RESTRICTED)
669                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
670
671         if (value == DPM_RESTRICTED) {
672                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
673                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
674         }
675
676         policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value = value;
677
678         return BLUETOOTH_ERROR_NONE;
679 }
680
681 int _bt_dpm_get_bluetooth_discoverable_state(int *value)
682 {
683         int allow_bt = DPM_BT_ERROR;
684
685         BT_INFO("_bt_dpm_get_bluetooth_discoverable_state");
686
687         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
688         if (allow_bt == DPM_BT_RESTRICTED)
689                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
690
691         *value = policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value;
692
693         return BLUETOOTH_ERROR_NONE;
694 }
695
696 int _bt_dpm_set_bluetooth_limited_discoverable_state(dpm_status_t value)
697 {
698         int allow_bt = DPM_BT_ERROR;
699
700         BT_INFO("_bt_dpm_set_bluetooth_limited_discoverable_state");
701
702         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
703         if (allow_bt == DPM_BT_RESTRICTED)
704                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
705
706         if (value == DPM_RESTRICTED) {
707                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
708                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
709         }
710
711         policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value = value;
712
713         return BLUETOOTH_ERROR_NONE;
714 }
715
716 int _bt_dpm_get_bluetooth_limited_discoverable_state(int *value)
717 {
718         int allow_bt = DPM_BT_ERROR;
719
720         BT_INFO("_bt_dpm_get_bluetooth_limited_discoverable_state");
721
722         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
723         if (allow_bt == DPM_BT_RESTRICTED)
724                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
725
726         *value = policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value;
727
728         return BLUETOOTH_ERROR_NONE;
729 }
730
731 int _bt_dpm_set_bluetooth_data_transfer_state(dpm_status_t value)
732 {
733         int allow_bt = DPM_BT_ERROR;
734
735         BT_INFO("_bt_dpm_set_bluetooth_data_transfer_state");
736
737         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
738         if (allow_bt == DPM_BT_RESTRICTED)
739                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
740
741         policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value = value;
742         return BLUETOOTH_ERROR_NONE;
743 }
744
745 int _bt_dpm_get_allow_bluetooth_data_transfer_state(int *value)
746 {
747         int allow_bt = DPM_BT_ERROR;
748
749         BT_INFO("_bt_dpm_get_allow_bluetooth_data_transfer_state");
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         *value = policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value;
756
757         return BLUETOOTH_ERROR_NONE;
758 }
759
760 int _bt_dpm_remove_bluetooth_devices_from_allowlist(bluetooth_device_address_t *device_address)
761 {
762         GSList *l = NULL;
763         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
764         int allow_bt = DPM_BT_ERROR;
765
766         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_allowlist");
767
768         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
769         if (allow_bt == DPM_BT_RESTRICTED)
770                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
771
772         _bt_convert_addr_type_to_string(bd_addr,
773                         (unsigned char *)device_address->addr);
774
775         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list; l; l = g_slist_next(l)) {
776                 char *l_device = l->data;
777                 if (l_device && g_strcmp0(l_device, bd_addr)) {
778                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list, l_device);
779                         g_free(l_device);
780                 }
781         }
782         return BLUETOOTH_ERROR_NONE;
783 }
784
785 int _bt_dpm_remove_bluetooth_devices_from_blocklist(bluetooth_device_address_t *device_address)
786 {
787         GSList *l = NULL;
788         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
789         int allow_bt = DPM_BT_ERROR;
790
791         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_blocklist");
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         _bt_convert_addr_type_to_string(bd_addr,
798                         (unsigned char *)device_address->addr);
799
800         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLOCKLIST].list; l; l = g_slist_next(l)) {
801                 char *l_device = l->data;
802                 if (l_device && g_strcmp0(l_device, bd_addr)) {
803                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_ALLOWLIST].list, l_device);
804                         g_free(l_device);
805                 }
806         }
807
808         return BLUETOOTH_ERROR_NONE;
809 }
810
811 int _bt_dpm_remove_bluetooth_uuids_from_allowlist(const char *uuids)
812 {
813         GSList *l = NULL;
814         int allow_bt = DPM_BT_ERROR;
815
816         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_allowlist");
817
818         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
819         if (allow_bt == DPM_BT_RESTRICTED)
820                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
821
822         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list; l; l = g_slist_next(l)) {
823                 char *l_uuid = l->data;
824                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
825                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_ALLOWLIST].list, l_uuid);
826                         g_free(l_uuid);
827                 }
828         }
829         return BLUETOOTH_ERROR_NONE;
830 }
831
832 int _bt_dpm_remove_bluetooth_uuids_from_blocklist(const char *uuids)
833 {
834         GSList *l = NULL;
835         int allow_bt = DPM_BT_ERROR;
836
837         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_blocklist");
838
839         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
840         if (allow_bt == DPM_BT_RESTRICTED)
841                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
842
843         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list; l; l = g_slist_next(l)) {
844                 char *l_uuid = l->data;
845                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
846                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLOCKLIST].list, l_uuid);
847                         g_free(l_uuid);
848                 }
849         }
850         return BLUETOOTH_ERROR_NONE;
851 }
852
853 int _bt_dpm_clear_bluetooth_uuids_from_list(void)
854 {
855         int allow_bt = DPM_BT_ERROR;
856         int err = BLUETOOTH_ERROR_INTERNAL;
857
858         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_list");
859
860         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
861         if (allow_bt == DPM_BT_RESTRICTED)
862                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
863
864         err = _bt_dpm_clear_bluetooth_uuids_from_blocklist();
865         if (!err)
866                 err = _bt_dpm_clear_bluetooth_uuids_from_blocklist();
867
868         return err;
869 }
870
871 int _bt_dpm_clear_bluetooth_devices_from_list(void)
872 {
873         int allow_bt = DPM_BT_ERROR;
874         int err = BLUETOOTH_ERROR_INTERNAL;
875
876         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_list");
877
878         _bt_dpm_get_allow_bluetooth_mode(&allow_bt);
879         if (allow_bt == DPM_BT_RESTRICTED)
880                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
881
882         err = _bt_dpm_clear_bluetooth_devices_from_blocklist();
883         if (!err)
884                 err = _bt_dpm_clear_bluetooth_devices_from_blocklist();
885
886         return err;
887 }