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