Handle the recovery only for wearable profile
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-network.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
18 #include "bluetooth-api.h"
19 #include "bt-internal-types.h"
20
21 #include "bt-common.h"
22 #include "bt-request-sender.h"
23 #include "bt-event-handler.h"
24
25 #ifdef TIZEN_FEATURE_BT_DPM
26 #include "bt-dpm.h"
27 #endif
28
29 BT_EXPORT_API int bluetooth_network_activate_server()
30 {
31         int result;
32
33         BT_CHECK_ENABLED(return);
34
35         BT_INIT_PARAMS();
36         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
37
38         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_NETWORK_ACTIVATE,
39                 in_param1, in_param2, in_param3, in_param4, &out_param);
40
41         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
42
43         return result;
44 }
45
46 BT_EXPORT_API int bluetooth_network_deactivate_server(void)
47 {
48         int result;
49
50         BT_CHECK_ENABLED(return);
51
52         BT_INIT_PARAMS();
53         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
54
55         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_NETWORK_DEACTIVATE,
56                 in_param1, in_param2, in_param3, in_param4, &out_param);
57
58         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
59
60         return result;
61 }
62
63 BT_EXPORT_API int bluetooth_network_connect(const bluetooth_device_address_t *device_address,
64                                                 bluetooth_network_role_t role,
65                                                 char *custom_uuid)
66 {
67         int result;
68         bt_user_info_t *user_info;
69
70         BT_CHECK_PARAMETER(device_address, return);
71         BT_CHECK_ENABLED(return);
72
73         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_CONNECT)
74              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
75                 BT_ERR("Don't have a privilege to use this API");
76                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
77         }
78
79 #ifdef TIZEN_FEATURE_BT_DPM
80         char *uuid = NULL;
81         if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)device_address) == BT_DPM_RESTRICTED) {
82                 BT_ERR("Blacklist device");
83                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
84         }
85
86         switch (role) {
87         case BLUETOOTH_NETWORK_PANU_ROLE:
88                 uuid = g_strdup(BT_PAN_PANU_UUID);
89                 break;
90         case BLUETOOTH_NETWORK_NAP_ROLE:
91                 uuid = g_strdup(BT_PAN_NAP_UUID);
92                 break;
93         case BLUETOOTH_NETWORK_GN_ROLE:
94                 uuid = g_strdup(BT_PAN_GN_UUID);
95                 break;
96         default:
97                 break;
98         }
99
100         if (_bt_check_dpm(BT_DPM_UUID, uuid) == BT_DPM_RESTRICTED) {
101                 BT_ERR("Blacklist uuid");
102                 g_free(uuid);
103                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
104         }
105
106         if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
107                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
108                 bluetooth_device_class_t dev_class;
109
110                 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
111                 _bt_get_cod_by_address(address, &dev_class);
112
113                 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
114                         BT_ERR("Reject a authorization due to MDM Policy");
115                         g_free(uuid);
116                         return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
117                 }
118         }
119         g_free(uuid);
120 #endif
121
122         user_info = _bt_get_user_data(BT_COMMON);
123         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
124
125         BT_INIT_PARAMS();
126         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
127
128         g_array_append_vals(in_param1, device_address,
129                                 sizeof(bluetooth_device_address_t));
130         g_array_append_vals(in_param2, &role, sizeof(int));
131
132         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_NETWORK_CONNECT,
133                 in_param1, in_param2, in_param3, in_param4,
134                 user_info->cb, user_info->user_data);
135
136         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
137
138         return result;
139 }
140
141 BT_EXPORT_API int bluetooth_network_disconnect(const bluetooth_device_address_t *device_address)
142 {
143         int result;
144         bt_user_info_t *user_info;
145
146         BT_CHECK_PARAMETER(device_address, return);
147         BT_CHECK_ENABLED(return);
148
149         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_DISCONNECT)
150              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
151                 BT_ERR("Don't have a privilege to use this API");
152                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
153         }
154
155         user_info = _bt_get_user_data(BT_COMMON);
156         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
157
158         BT_INIT_PARAMS();
159         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
160
161         g_array_append_vals(in_param1, device_address,
162                                         sizeof(bluetooth_device_address_t));
163
164         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_NETWORK_DISCONNECT,
165                 in_param1, in_param2, in_param3, in_param4,
166                 user_info->cb, user_info->user_data);
167
168         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
169
170         return result;
171 }
172
173 BT_EXPORT_API int bluetooth_network_server_disconnect(const bluetooth_device_address_t *device_address)
174 {
175         int result;
176         bt_user_info_t *user_info;
177
178         BT_CHECK_PARAMETER(device_address, return);
179         BT_CHECK_ENABLED(return);
180
181         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_SERVER_DISCONNECT)
182              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
183                 BT_ERR("Don't have a privilege to use this API");
184                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
185         }
186
187         user_info = _bt_get_user_data(BT_COMMON);
188         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
189
190         BT_INIT_PARAMS();
191         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
192
193         g_array_append_vals(in_param1, device_address,
194                                         sizeof(bluetooth_device_address_t));
195
196         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
197                                 BT_NETWORK_SERVER_DISCONNECT,
198                                 in_param1, in_param2, in_param3, in_param4,
199                                 user_info->cb, user_info->user_data);
200
201         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
202
203         return result;
204 }
205