Fix the coding rule
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-dpm.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifdef TIZEN_DPM_ENABLE
18
19 #include <glib.h>
20 #include <gio/gio.h>
21 #include <dlog.h>
22 #include <string.h>
23 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
25 #include <vconf.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
29
30 #include "bt-service-common.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-dpm.h"
33
34 static dpm_policy_t policy_table[DPM_POLICY_END] = {
35         [DPM_POLICY_ALLOW_BLUETOOTH] = { {DPM_BT_ERROR} },
36         [DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION] = { {DPM_STATUS_ERROR} },
37         [DPM_POLICY_BLUETOOTH_UUID_RESTRICTION] = { {DPM_STATUS_ERROR} },
38         [DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST] = { {NULL} },
39         [DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST] = { {NULL} },
40         [DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST] = { {NULL} },
41         [DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST] = { {NULL} },
42         [DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL] = { {DPM_STATUS_ERROR} },
43         [DPM_POLICY_BLUETOOTH_PAIRING_STATE] = { {DPM_STATUS_ERROR} },
44         [DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE] = { {DPM_STATUS_ERROR} },
45         [DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE] = { {DPM_STATUS_ERROR} },
46         [DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE] = { {DPM_STATUS_ERROR} },
47         [DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE] = { {DPM_STATUS_ERROR} },
48 };
49
50
51 /**
52  * @brief DPM profile state
53  * @see
54  */
55 static dpm_profile_state_t dpm_profile_state[DPM_PROFILE_NONE]  = {
56         [DPM_POLICY_BLUETOOTH_A2DP_PROFILE_STATE] = {DPM_STATUS_ERROR},
57         [DPM_POLICY_BLUETOOTH_AVRCP_PROFILE_STATE] = {DPM_STATUS_ERROR},
58         [DPM_POLICY_BLUETOOTH_BPP_PROFILE_STATE] = {DPM_STATUS_ERROR},
59         [DPM_POLICY_BLUETOOTH_DUN_PROFILE_STATE] = {DPM_STATUS_ERROR},
60         [DPM_POLICY_BLUETOOTH_FTP_PROFILE_STATE] = {DPM_STATUS_ERROR},
61         [DPM_POLICY_BLUETOOTH_HFP_PROFILE_STATE] = {DPM_STATUS_ERROR},
62         [DPM_POLICY_BLUETOOTH_HSP_PROFILE_STATE] = {DPM_STATUS_ERROR},
63         [DPM_POLICY_BLUETOOTH_PBAP_PROFILE_STATE] = {DPM_STATUS_ERROR},
64         [DPM_POLICY_BLUETOOTH_SAP_PROFILE_STATE] = {DPM_STATUS_ERROR},
65         [DPM_POLICY_BLUETOOTH_SPP_PROFILE_STATE] = {DPM_STATUS_ERROR},
66 };
67
68 int _bt_launch_dpm_popup(char *mode)
69 {
70         int ret = 0;
71         bundle *b;
72
73         b = bundle_create();
74         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
75
76         bundle_add(b, "mode", mode);
77
78         ret = syspopup_launch(BT_DPM_SYSPOPUP, b);
79
80         if (ret < 0)
81                 BT_ERR("Popup launch failed: %d\n", ret);
82
83         bundle_free(b);
84
85         return ret;
86 }
87
88 dpm_result_t _bt_dpm_set_allow_bluetooth_mode(dpm_bt_allow_t value)
89 {
90         BT_INFO("_bt_dpm_set_allow_bluetooth_mode");
91
92 #if 0
93         if (value == DPM_BT_ALLOWED && value == DPM_BT_HANDSFREE_ONLY) {
94                 /* Update Bluetooth DPM Status to notify other modules */
95                 if (vconf_set_int(VCONFKEY_BT_DPM_STATUS, value) != 0)
96                         BT_ERR("Set vconf failed\n");
97                         return DPM_RESULT_FAIL;
98         } else {
99                 /* Update Bluetooth DPM Status to notify other modules */
100                 if (vconf_set_int(VCONFKEY_BT_DPM_STATUS, VCONFKEY_BT_DPM_STATUS_RESTRICTED) != 0)
101                         BT_ERR("Set vconf failed\n");
102                         return DPM_RESULT_FAIL;
103         }
104 #endif
105         policy_table[DPM_POLICY_ALLOW_BLUETOOTH].value  = value;
106
107         return DPM_RESULT_SUCCESS;
108 }
109
110 dpm_bt_allow_t _bt_dpm_get_allow_bluetooth_mode(void)
111 {
112         BT_INFO("_bt_dpm_get_allow_bluetooth_mode");
113
114         return policy_table[DPM_POLICY_ALLOW_BLUETOOTH].value;
115 }
116
117 dpm_result_t _bt_dpm_activate_bluetooth_device_restriction(dpm_status_t value)
118 {
119         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
120         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
121                 return DPM_RESULT_ACCESS_DENIED;
122
123         policy_table[DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION].value  = value;
124
125         return DPM_RESULT_SUCCESS;
126 }
127
128 dpm_status_t _bt_dpm_is_bluetooth_device_restriction_active(void)
129 {
130         BT_INFO("_bt_dpm_is_bluetooth_device_restriction_active");
131
132         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
133                 return DPM_RESTRICTED;
134
135         return policy_table[DPM_POLICY_BLUETOOTH_DEVICE_RESTRICTION].value;
136 }
137
138 dpm_result_t _bt_dpm_activate_bluetoooth_uuid_restriction(dpm_status_t value)
139 {
140         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
141         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
142                 return DPM_RESULT_ACCESS_DENIED;
143
144         policy_table[DPM_POLICY_BLUETOOTH_UUID_RESTRICTION].value  = value;
145
146         return DPM_RESULT_SUCCESS;
147 }
148
149 dpm_status_t _bt_dpm_is_bluetooth_uuid_restriction_active(void)
150 {
151         BT_INFO("_bt_dpm_is_bluetooth_uuid_restriction_active");
152
153         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
154                 return DPM_RESTRICTED;
155
156         return policy_table[DPM_POLICY_BLUETOOTH_UUID_RESTRICTION].value;
157 }
158
159 dpm_result_t _bt_dpm_add_bluetooth_devices_to_blacklist(bluetooth_device_address_t *bd_addr)
160 {
161         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
162         char *dev_addr = NULL;
163
164         BT_INFO("_bt_dpm_add_bluetooth_devices_to_blacklist");
165
166         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
167                 return DPM_RESULT_ACCESS_DENIED;
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 DPM_RESULT_FAIL;
177         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list, dev_addr);
178
179         return DPM_RESULT_SUCCESS;
180 }
181
182 dpm_result_t _bt_dpm_get_bluetooth_devices_from_blacklist(GArray **out_param1)
183 {
184         dpm_result_t ret = DPM_RESULT_FAIL;
185         bt_dpm_device_list_t device_list;
186         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list;
187         int i = 0;
188
189         BT_INFO("_bt_dpm_get_bluetooth_devices_from_blacklist");
190
191         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
192                 return ret;
193
194         if (list) {
195                 ret = DPM_RESULT_SUCCESS;
196                 for (; list; list = list->next, i++) {
197                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
198                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
199                 }
200                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list);
201                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
202         } else {
203                 ret = DPM_RESULT_SUCCESS;
204                 device_list.count = 0;
205                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
206         }
207         return ret;
208 }
209
210 dpm_result_t _bt_dpm_add_bluetooth_devices_to_whitelist(bluetooth_device_address_t *bd_addr)
211 {
212         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
213         char *dev_addr = NULL;
214
215         BT_INFO("_bt_dpm_add_bluetooth_devices_to_whitelist");
216
217         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
218                 return DPM_RESULT_ACCESS_DENIED;
219
220         BT_CHECK_PARAMETER(bd_addr, return);
221
222         _bt_convert_addr_type_to_string(device_address,
223                         (unsigned char *)bd_addr->addr);
224
225         dev_addr = g_strdup(device_address);
226         if (!dev_addr)
227                 return DPM_RESULT_FAIL;
228         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, dev_addr);
229         return DPM_RESULT_SUCCESS;
230 }
231
232 dpm_result_t _bt_dpm_get_bluetooth_devices_from_whitelist(GArray **out_param1)
233 {
234         dpm_result_t ret = DPM_RESULT_FAIL;
235         bt_dpm_device_list_t device_list;
236         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list;
237         int i = 0;
238
239         BT_INFO("_bt_dpm_get_bluetooth_devices_from_whitelist");
240
241         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
242                 return ret;
243
244         if (list) {
245                 ret = DPM_RESULT_SUCCESS;
246                 for (; list; list = list->next, i++) {
247                         memset(device_list.addresses[i].addr, 0, BLUETOOTH_ADDRESS_LENGTH);
248                         _bt_convert_addr_string_to_type(device_list.addresses[i].addr, list->data);
249
250                 }
251                 device_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list);
252                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
253         } else {
254                 ret = DPM_RESULT_SUCCESS;
255                 device_list.count = 0;
256                 g_array_append_vals(*out_param1, &device_list, sizeof(bt_dpm_device_list_t));
257         }
258         return ret;
259 }
260
261 dpm_result_t _bt_dpm_add_bluetooth_uuids_to_blacklist(const char *uuid)
262 {
263         char *l_uuid;
264         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_blacklist");
265
266         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
267                 return DPM_RESULT_ACCESS_DENIED;
268
269         l_uuid = g_strdup(uuid);
270         if (!l_uuid)
271                 return DPM_RESULT_FAIL;
272         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
273         return DPM_RESULT_SUCCESS;
274 }
275
276 dpm_result_t _bt_dpm_get_bluetooth_uuids_from_blacklist(GArray **out_param1)
277 {
278         dpm_result_t ret = DPM_RESULT_FAIL;
279         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
280         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list;
281         int i = 0;
282
283         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_blacklist");
284
285         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
286                 return ret;
287
288         if (list) {
289                 ret = DPM_RESULT_SUCCESS;
290                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list);
291                 for (; list; list = list->next, i++) {
292                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
293                         g_strlcpy(uuids_list.uuids[i], list->data,
294                                 BLUETOOTH_UUID_STRING_MAX);
295                 }
296                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
297         } else {
298                 ret = DPM_RESULT_SUCCESS;
299                 uuids_list.count = 0;
300                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
301         }
302
303         return ret;
304 }
305
306 dpm_result_t _bt_dpm_add_bluetooth_uuids_to_whitelist(const char *uuid)
307 {
308         char *l_uuid;
309         BT_INFO("_bt_dpm_add_bluetooth_uuids_to_whitelist");
310
311         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
312                 return DPM_RESULT_ACCESS_DENIED;
313
314         l_uuid = g_strdup(uuid);
315         if (!l_uuid)
316                 return DPM_RESULT_FAIL;
317         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list  = g_slist_append(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
318         return DPM_RESULT_SUCCESS;
319 }
320
321
322 dpm_result_t _bt_dpm_get_bluetooth_uuids_from_whitelist(GArray **out_param1)
323 {
324         dpm_result_t ret = DPM_RESULT_FAIL;
325         bt_dpm_uuids_list_t uuids_list = {0, { {0}, } };
326         GSList *list = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list;
327         int i = 0;
328
329         BT_INFO("_bt_dpm_get_bluetooth_uuids_from_whitelist");
330
331         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
332                 return ret;
333
334         if (list) {
335                 ret = DPM_RESULT_SUCCESS;
336                 uuids_list.count = g_slist_length(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list);
337                 for (; list; list = list->next, i++) {
338                         memset(uuids_list.uuids[i], 0, BLUETOOTH_UUID_STRING_MAX);
339                         g_strlcpy(uuids_list.uuids[i], list->data,
340                                 BLUETOOTH_UUID_STRING_MAX);
341                 }
342                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
343         } else {
344                 ret = DPM_RESULT_SUCCESS;
345                 uuids_list.count = 0;
346                 g_array_append_vals(*out_param1, &uuids_list, sizeof(bt_dpm_uuids_list_t));
347         }
348
349         return ret;
350
351 }
352
353 dpm_result_t _bt_dpm_set_allow_bluetooth_outgoing_call(dpm_status_t value)
354 {
355         BT_INFO("_bt_dpm_activate_bluetooth_device_restriction");
356         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
357                 return DPM_RESULT_ACCESS_DENIED;
358
359         policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value  = value;
360
361         return DPM_RESULT_SUCCESS;
362 }
363
364 dpm_status_t _bt_dpm_get_allow_bluetooth_outgoing_call(void)
365 {
366         BT_INFO("_bt_dpm_get_allow_bluetooth_outgoing_call");
367
368         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
369                 return DPM_RESTRICTED;
370
371         return policy_table[DPM_POLICY_ALLOW_BLUETOOTH_OUTGOING_CALL].value;
372 }
373
374 dpm_result_t _bt_dpm_clear_bluetooth_devices_from_blacklist(void)
375 {
376         GSList *l = NULL;
377         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
378
379         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
380                 return DPM_RESULT_ACCESS_DENIED;
381
382         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
383                 char *address = l->data;
384                 if (address) {
385                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list, address);
386                         g_free(address);
387                 }
388         }
389         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list);
390         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = NULL;
391         return DPM_RESULT_SUCCESS;
392 }
393
394 dpm_result_t _bt_dpm_clear_bluetooth_devices_from_whitelist(void)
395 {
396         GSList *l = NULL;
397         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
398
399         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
400                 return DPM_RESULT_ACCESS_DENIED;
401
402         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
403                 char *address = l->data;
404                 if (address) {
405                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, address);
406                         g_free(address);
407                 }
408         }
409         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list);
410         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = NULL;
411         return DPM_RESULT_SUCCESS;
412 }
413
414 dpm_result_t _bt_dpm_clear_bluetooth_uuids_from_blacklist(void)
415 {
416         GSList *l = NULL;
417         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_blacklist");
418
419         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
420                 return DPM_RESULT_ACCESS_DENIED;
421
422         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
423                 char *l_uuid = l->data;
424                 if (l_uuid)
425                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
426                         g_free(l_uuid);
427         }
428         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list);
429         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = NULL;
430         return DPM_RESULT_SUCCESS;
431 }
432
433 dpm_result_t _bt_dpm_clear_bluetooth_uuids_from_whitelist(void)
434 {
435         GSList *l = NULL;
436         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_whitelist");
437
438         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
439                 return DPM_RESULT_ACCESS_DENIED;
440
441         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
442                 char *l_uuid = l->data;
443                 if (l_uuid) {
444                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
445                         g_free(l_uuid);
446                 }
447         }
448         g_slist_free(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list);
449         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = NULL;
450         return DPM_RESULT_SUCCESS;
451 }
452
453 dpm_status_t _bt_dpm_set_bluetooth_pairing_state(dpm_status_t value)
454 {
455         BT_INFO("_bt_dpm_set_bluetooth_pairing_state");
456
457         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
458                 return DPM_RESTRICTED;
459
460         policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value = value;
461
462         return DPM_RESULT_SUCCESS;
463 }
464
465 dpm_status_t _bt_dpm_get_bluetooth_pairing_state(void)
466 {
467         BT_INFO("_bt_dpm_get_bluetooth_pairing_state");
468
469         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
470                 return DPM_RESTRICTED;
471
472         return policy_table[DPM_POLICY_BLUETOOTH_PAIRING_STATE].value;
473 }
474
475 dpm_status_t _bt_dpm_set_bluetooth_profile_state(dpm_profile_t profile, dpm_status_t value)
476 {
477         BT_INFO("_bt_dpm_set_bluetooth_profile_state");
478
479         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
480                 return DPM_RESTRICTED;
481
482         dpm_profile_state[profile].value = value;
483
484         return DPM_RESULT_SUCCESS;
485 }
486
487 dpm_status_t _bt_dpm_get_bluetooth_profile_state(dpm_profile_t profile)
488 {
489         BT_INFO("_bt_dpm_get_bluetooth_profile_state");
490
491         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
492                 return DPM_RESTRICTED;
493
494         return dpm_profile_state[profile].value;
495 }
496
497 dpm_status_t _bt_dpm_set_bluetooth_desktop_connectivity_state(dpm_status_t value)
498 {
499         BT_INFO("_bt_dpm_set_bluetooth_desktop_connectivity_state");
500
501         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
502                 return DPM_RESTRICTED;
503
504         policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value = value;
505
506         return DPM_RESULT_SUCCESS;
507 }
508
509 dpm_status_t _bt_dpm_get_bluetooth_desktop_connectivity_state(void)
510 {
511         BT_INFO("_bt_dpm_get_bluetooth_desktop_connectivity_state");
512
513         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
514                 return DPM_RESTRICTED;
515
516         return policy_table[DPM_POLICY_BLUETOOTH_DESKTOP_CONNECTIVITY_STATE].value;
517 }
518
519 dpm_status_t _bt_dpm_set_bluetooth_discoverable_state(dpm_status_t value)
520 {
521         BT_INFO("_bt_dpm_set_bluetooth_discoverable_state");
522
523         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
524                 return DPM_RESTRICTED;
525
526         if (value == DPM_RESTRICTED) {
527                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
528                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
529         }
530
531         policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value = value;
532
533         return DPM_RESULT_SUCCESS;
534 }
535
536 dpm_status_t _bt_dpm_get_bluetooth_discoverable_state(void)
537 {
538         BT_INFO("_bt_dpm_get_bluetooth_discoverable_state");
539
540         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
541                 return DPM_RESTRICTED;
542
543         return policy_table[DPM_POLICY_BLUETOOTH_DISCOVERABLE_STATE].value;
544 }
545
546 dpm_status_t _bt_dpm_set_bluetooth_limited_discoverable_state(dpm_status_t value)
547 {
548         BT_INFO("_bt_dpm_set_bluetooth_limited_discoverable_state");
549
550         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
551                 return DPM_RESTRICTED;
552
553         if (value == DPM_RESTRICTED) {
554                 /* Since Discoverable mode is restricted, stop the ongoing discoverable mode */
555                 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
556         }
557
558         policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value = value;
559
560         return DPM_RESULT_SUCCESS;
561 }
562
563 dpm_status_t _bt_dpm_get_bluetooth_limited_discoverable_state(void)
564 {
565         BT_INFO("_bt_dpm_get_bluetooth_limited_discoverable_state");
566
567         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
568                 return DPM_RESTRICTED;
569
570         return policy_table[DPM_POLICY_BLUETOOTH_LIMITED_DISCOVERABLE_STATE].value;
571 }
572
573 dpm_status_t _bt_dpm_set_bluetooth_data_transfer_state(dpm_status_t value)
574 {
575         BT_INFO("_bt_dpm_set_bluetooth_data_transfer_state");
576
577         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
578                 return DPM_RESTRICTED;
579
580         return policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value = value;
581 }
582
583 dpm_status_t _bt_dpm_get_allow_bluetooth_data_transfer_state(void)
584 {
585         BT_INFO("_bt_dpm_get_allow_bluetooth_data_transfer_state");
586
587         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
588                 return DPM_RESTRICTED;
589
590         return policy_table[DPM_POLICY_BLUETOOTH_DATA_TRANSFER_STATE].value;
591 }
592
593 dpm_result_t _bt_dpm_remove_bluetooth_devices_from_whitelist(bluetooth_device_address_t *device_address)
594 {
595         GSList *l = NULL;
596         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
597         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_whitelist");
598
599         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
600                 return DPM_RESULT_ACCESS_DENIED;
601
602         _bt_convert_addr_type_to_string(bd_addr,
603                         (unsigned char *)device_address->addr);
604
605         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
606                 return DPM_RESULT_ACCESS_DENIED;
607
608         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list; l; l = g_slist_next(l)) {
609                 char *l_device = l->data;
610                 if (l_device && g_strcmp0(l_device, bd_addr)) {
611                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
612                         g_free(l_device);
613                 }
614         }
615         return DPM_RESULT_SUCCESS;
616 }
617
618 dpm_result_t _bt_dpm_remove_bluetooth_devices_from_blacklist(bluetooth_device_address_t *device_address)
619 {
620         GSList *l = NULL;
621         char bd_addr[BT_ADDRESS_STRING_SIZE] = { 0 };
622
623         BT_INFO("_bt_dpm_remove_bluetooth_devices_from_blacklist");
624
625         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
626                 return DPM_RESULT_ACCESS_DENIED;
627
628         _bt_convert_addr_type_to_string(bd_addr,
629                         (unsigned char *)device_address->addr);
630
631         for (l = policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list; l; l = g_slist_next(l)) {
632                 char *l_device = l->data;
633                 if (l_device && g_strcmp0(l_device, bd_addr)) {
634                         policy_table[DPM_POLICY_BLUETOOTH_DEVICES_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_DEVICES_WHITELIST].list, l_device);
635                         g_free(l_device);
636                 }
637         }
638
639         return DPM_RESULT_SUCCESS;
640 }
641
642 dpm_result_t _bt_dpm_remove_bluetooth_uuids_from_whitelist(const char *uuids)
643 {
644         GSList *l = NULL;
645         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_whitelist");
646
647         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
648                 return DPM_RESULT_ACCESS_DENIED;
649         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list; l; l = g_slist_next(l)) {
650                 char *l_uuid = l->data;
651                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
652                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_WHITELIST].list, l_uuid);
653                         g_free(l_uuid);
654                 }
655         }
656         return DPM_RESULT_SUCCESS;
657 }
658
659 dpm_result_t _bt_dpm_remove_bluetooth_uuids_from_blacklist(const char *uuids)
660 {
661         GSList *l = NULL;
662         BT_INFO("_bt_dpm_remove_bluetooth_uuids_from_blacklist");
663
664         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
665                 return DPM_RESULT_ACCESS_DENIED;
666
667         for (l = policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list; l; l = g_slist_next(l)) {
668                 char *l_uuid = l->data;
669                 if (l_uuid && g_strcmp0(l_uuid, uuids)) {
670                         policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list = g_slist_remove(policy_table[DPM_POLICY_BLUETOOTH_UUIDS_BLACKLIST].list, l_uuid);
671                         g_free(l_uuid);
672                 }
673         }
674         return DPM_RESULT_SUCCESS;
675 }
676
677 dpm_result_t _bt_dpm_clear_bluetooth_uuids_from_list(void)
678 {
679         BT_INFO("_bt_dpm_clear_bluetooth_uuids_from_list");
680         dpm_result_t err = DPM_RESULT_FAIL;
681
682         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
683                 return DPM_RESULT_ACCESS_DENIED;
684
685         err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
686         if (!err)
687                 err = _bt_dpm_clear_bluetooth_uuids_from_blacklist();
688
689         return err;
690 }
691
692 dpm_result_t _bt_dpm_clear_bluetooth_devices_from_list(void)
693 {
694         BT_INFO("_bt_dpm_clear_bluetooth_devices_from_list");
695         dpm_result_t err = DPM_RESULT_FAIL;
696
697         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_RESTRICTED)
698                 return DPM_RESULT_ACCESS_DENIED;
699
700         err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
701         if (!err)
702                 err = _bt_dpm_clear_bluetooth_devices_from_blacklist();
703
704         return err;
705 }
706 #endif /* #ifdef TIZEN_DPM_ENABLE */