Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-mdm.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #ifdef TIZEN_MDM_ENABLE
25 #include <syspopup_caller.h>
26 #include "bt-internal-types.h"
27 #include "bt-common.h"
28 #include "bt-mdm.h"
29
30 int _bt_launch_mdm_popup(char *mode)
31 {
32         int ret = 0;
33         bundle *b;
34
35         b = bundle_create();
36         retv_if(b == NULL, BLUETOOTH_ERROR_INTERNAL);
37
38         bundle_add(b, "mode", mode);
39
40         ret = syspopup_launch(BT_MDM_SYSPOPUP, b);
41
42         if (ret < 0)
43                 BT_ERR("Popup launch failed: %d\n", ret);
44
45         bundle_free(b);
46
47         return ret;
48 }
49
50 bt_mdm_status_e _bt_check_mdm_handsfree_only(void)
51 {
52         mdm_bt_allow_t mode;
53
54         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
55
56         mode = mdm_get_allow_bluetooth_mode();
57         mdm_release_service();
58
59         return (mode == MDM_BT_HANDSFREE_ONLY ? BT_MDM_RESTRICTED : BT_MDM_ALLOWED);
60 }
61
62 #ifdef MDM_PHASE_2
63 bt_mdm_status_e _bt_check_mdm_pairing_restriction(void)
64 {
65         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
66
67         if (mdm_get_service() != MDM_RESULT_SUCCESS)
68                 return BT_MDM_NO_SERVICE;
69
70         if (mdm_get_bluetooth_pairing_state() == MDM_RESTRICTED) {
71                 /* Not allow to visible on */
72                 BT_ERR("Pairing Restricted");
73                 mdm_status = BT_MDM_RESTRICTED;
74         }
75         mdm_release_service();
76
77         return mdm_status;
78 }
79
80 bt_mdm_status_e _bt_check_mdm_transfer_restriction(void)
81 {
82         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
83
84         mdm_status = _bt_check_mdm_blacklist_uuid(BT_OPP_UUID);
85
86         if (mdm_status == BT_MDM_NO_SERVICE || mdm_status == BT_MDM_RESTRICTED) {
87                 return mdm_status;
88         }
89
90         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
91
92         if (mdm_get_bluetooth_data_transfer_state() == MDM_RESTRICTED ||
93              mdm_get_allow_bluetooth_mode() == MDM_BT_HANDSFREE_ONLY) {
94                 /* Not allow to visible on */
95                 BT_ERR("Restricted to set visible mode");
96                 mdm_status = BT_MDM_RESTRICTED;
97         }
98         mdm_release_service();
99
100         return mdm_status;
101 }
102
103 bt_mdm_status_e _bt_check_mdm_hsp_restriction(void)
104 {
105         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
106
107         mdm_status = _bt_check_mdm_blacklist_uuid(BT_HFP_AUDIO_GATEWAY_UUID);
108
109         if (mdm_status == BT_MDM_NO_SERVICE || mdm_status == BT_MDM_RESTRICTED) {
110                 return mdm_status;
111         }
112
113         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
114
115         if (mdm_get_bluetooth_profile_state(BLUETOOTH_HSP_PROFILE) == MDM_RESTRICTED ||
116                 mdm_get_bluetooth_profile_state(BLUETOOTH_HFP_PROFILE) == MDM_RESTRICTED) {
117                 /* Not allow to visible on */
118                 BT_ERR("Restrict hsp / hfp profile");
119                 mdm_status = BT_MDM_RESTRICTED;
120         }
121         mdm_release_service();
122
123         return mdm_status;
124 }
125
126 bt_mdm_status_e _bt_check_mdm_a2dp_restriction(void)
127 {
128         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
129
130         mdm_status = _bt_check_mdm_blacklist_uuid(BT_A2DP_UUID);
131
132         if (mdm_status == BT_MDM_NO_SERVICE || mdm_status == BT_MDM_RESTRICTED) {
133                 return mdm_status;
134         }
135
136         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
137
138         if (mdm_get_bluetooth_profile_state(BLUETOOTH_A2DP_PROFILE) == MDM_RESTRICTED) {
139                 /* Not allow to visible on */
140                 BT_ERR("Restrict a2dp profile");
141                 mdm_status = BT_MDM_RESTRICTED;
142         }
143         mdm_release_service();
144
145         return mdm_status;
146 }
147
148 bt_mdm_status_e _bt_check_mdm_avrcp_restriction(void)
149 {
150         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
151
152
153         mdm_status = _bt_check_mdm_blacklist_uuid(BT_AVRCP_TARGET_UUID);
154
155         if (mdm_status == BT_MDM_NO_SERVICE || mdm_status == BT_MDM_RESTRICTED) {
156                 return mdm_status;
157         }
158
159         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
160
161         if (mdm_get_bluetooth_profile_state(BLUETOOTH_AVRCP_PROFILE) == MDM_RESTRICTED) {
162                 /* Not allow to visible on */
163                 BT_ERR("Restrict avrcp profile");
164                 mdm_status = BT_MDM_RESTRICTED;
165         }
166         mdm_release_service();
167
168         return mdm_status;
169 }
170
171 bt_mdm_status_e _bt_check_mdm_spp_restriction(void)
172 {
173         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
174
175         mdm_status = _bt_check_mdm_blacklist_uuid(BT_SPP_UUID);
176
177         if (mdm_status == BT_MDM_NO_SERVICE || mdm_status == BT_MDM_RESTRICTED) {
178                 return mdm_status;
179         }
180
181         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
182
183         if (mdm_get_bluetooth_profile_state(BLUETOOTH_SPP_PROFILE) == MDM_RESTRICTED) {
184                 /* Not allow to visible on */
185                 BT_ERR("Restrict spp profile");
186                 mdm_status = BT_MDM_RESTRICTED;
187         }
188         mdm_release_service();
189
190         return mdm_status;
191 }
192
193 bt_mdm_status_e _bt_check_mdm_whitelist_devices(const bluetooth_device_address_t *address)
194 {
195         mdm_data_t *lp_data = NULL;
196         GList *whitelist = NULL;
197         char *device_name;
198         bluetooth_device_info_t dev_info;
199         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
200
201         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
202
203         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
204
205         if (bluetooth_get_bonded_device(address,
206                                 &dev_info) != BLUETOOTH_ERROR_NONE) {
207                 BT_ERR("Not paired device");
208                 return mdm_status;
209         }
210
211         lp_data = mdm_get_bluetooth_devices_from_whitelist();
212         if (lp_data == NULL) {
213                 BT_ERR("No whitelist");
214                 mdm_release_service();
215                 return mdm_status;
216         }
217
218         for (whitelist = (GList *)lp_data->data; whitelist; whitelist = whitelist->next) {
219                 device_name = whitelist->data;
220
221                 DBG_SECURE("whitelist device name: %s", device_name);
222
223                 if (g_strcmp0(dev_info.device_name.name,
224                                         device_name) == 0) {
225                         mdm_status = BT_MDM_RESTRICTED;
226                         break;
227                 }
228         }
229
230         mdm_free_data(lp_data);
231         mdm_release_service();
232
233         return mdm_status;
234 }
235
236 bt_mdm_status_e _bt_check_mdm_whitelist_uuid(char *uuid)
237 {
238         mdm_data_t *lp_data;
239         GList *blacklist;
240         char *blacklist_uuid;
241         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
242
243         if (mdm_get_service() != MDM_RESULT_SUCCESS)
244                 return BT_MDM_NO_SERVICE;
245
246         if (uuid == NULL)
247                 return mdm_status;
248
249         lp_data = mdm_get_bluetooth_uuids_from_blacklist();
250         if (lp_data == NULL) {
251                 BT_ERR("No blacklist");
252                 mdm_release_service();
253                 return mdm_status;
254         }
255
256         for (blacklist = (GList *)lp_data->data; blacklist; blacklist = blacklist->next) {
257                 blacklist_uuid = blacklist->data;
258
259                 BT_DBG("blacklist_uuid: %s", blacklist_uuid);
260
261                 if (g_strcmp0(blacklist_uuid, uuid) == 0) {
262                         mdm_status = BT_MDM_RESTRICTED;
263                         break;
264                 }
265         }
266
267         mdm_free_data(lp_data);
268         mdm_release_service();
269
270         return mdm_status;
271 }
272
273 bt_mdm_status_e _bt_check_mdm_blacklist_devices(const bluetooth_device_address_t *address)
274 {
275         mdm_data_t *lp_data;
276         GList *blacklist;
277         char *device_name;
278         bluetooth_device_info_t dev_info;
279         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
280
281         if (mdm_get_service() != MDM_RESULT_SUCCESS) return BT_MDM_NO_SERVICE;
282
283         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
284
285         if (bluetooth_get_bonded_device(address,
286                                 &dev_info) != BLUETOOTH_ERROR_NONE) {
287                 BT_ERR("Not paired device");
288                 return mdm_status;
289         }
290
291         lp_data = mdm_get_bluetooth_devices_from_blacklist();
292         if (lp_data == NULL) {
293                 BT_ERR("No blacklist");
294                 mdm_release_service();
295                 return mdm_status;
296         }
297
298         for (blacklist = (GList *)lp_data->data; blacklist; blacklist = blacklist->next) {
299                 device_name = blacklist->data;
300
301                 DBG_SECURE("blacklist name: %s", device_name);
302
303                 if (g_strcmp0(dev_info.device_name.name,
304                                         device_name) == 0) {
305                         mdm_status = BT_MDM_RESTRICTED;
306                         break;
307                 }
308         }
309
310         mdm_free_data(lp_data);
311         mdm_release_service();
312
313         return mdm_status;
314 }
315
316 bt_mdm_status_e _bt_check_mdm_blacklist_uuid(char *uuid)
317 {
318         mdm_data_t *lp_data;
319         GList *blacklist;
320         char *blacklist_uuid;
321         bt_mdm_status_e mdm_status = BT_MDM_ALLOWED;
322
323         if (mdm_get_service() != MDM_RESULT_SUCCESS)
324                 return BT_MDM_NO_SERVICE;
325
326         if (uuid == NULL)
327                 return mdm_status;
328
329         lp_data = mdm_get_bluetooth_uuids_from_blacklist();
330         if (lp_data == NULL) {
331                 BT_ERR("No blacklist");
332                 mdm_release_service();
333                 return mdm_status;
334         }
335
336         for (blacklist = (GList *)lp_data->data; blacklist; blacklist = blacklist->next) {
337                 blacklist_uuid = blacklist->data;
338
339                 BT_DBG("blacklist_uuid: %s", blacklist_uuid);
340
341                 if (g_strcmp0(blacklist_uuid, uuid) == 0) {
342                         mdm_status = BT_MDM_RESTRICTED;
343                         break;
344                 }
345         }
346
347         mdm_free_data(lp_data);
348         mdm_release_service();
349
350         return mdm_status;
351 }
352 #endif
353 #endif
354