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