Correct the privilege check for BT_UPDATE_LE_CONNECTION_MODE
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-opp-client.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 <string.h>
19
20 #include "bluetooth-api.h"
21 #include "bt-internal-types.h"
22
23 #include "bt-common.h"
24 #include "bt-request-sender.h"
25 #include "bt-event-handler.h"
26 #include "bt-dpm.h"
27
28 static void __bt_get_file_size(char **files, unsigned long *file_size, int *count)
29 {
30         int file_count = 0;
31         unsigned long size = 0;
32
33         while (files[file_count] != NULL) {
34                 size = size + strlen(files[file_count]);
35                 file_count++;
36         }
37
38         *count = file_count;
39         *file_size = size;
40 }
41
42 BT_EXPORT_API int bluetooth_opc_init(void)
43 {
44         bt_user_info_t *user_info;
45
46         user_info = _bt_get_user_data(BT_COMMON);
47         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
48
49         return _bt_register_event(BT_OPP_CLIENT_EVENT, user_info->cb, user_info->user_data);
50 }
51
52 BT_EXPORT_API int bluetooth_opc_deinit(void)
53 {
54         return _bt_unregister_event(BT_OPP_CLIENT_EVENT);
55 }
56
57 BT_EXPORT_API int bluetooth_opc_push_files(bluetooth_device_address_t *remote_address,
58                         char **file_name_array)
59 {
60         int result;
61         int i;
62         int file_count;
63         unsigned long size;
64         bt_user_info_t *user_info;
65         char filename[BT_FILE_PATH_MAX];
66
67         BT_CHECK_PARAMETER(remote_address, return);
68         BT_CHECK_PARAMETER(file_name_array, return);
69         BT_CHECK_ENABLED(return);
70
71         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_OPP_PUSH_FILES)
72              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
73                 BT_ERR("Don't have a privilege to use this API");
74                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
75         }
76
77         if (_bt_check_dpm(BT_DPM_ADDRESS, remote_address) == BT_DPM_RESTRICTED) {
78                 BT_ERR("Blacklist device");
79                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
80         }
81
82         if (_bt_check_dpm(BT_DPM_OPP, NULL) == BT_DPM_RESTRICTED) {
83                 BT_ERR("Not allow to send files");
84                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
85         }
86
87         if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
88                 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
89                 bluetooth_device_class_t dev_class;
90
91                 _bt_convert_addr_type_to_string(address, remote_address->addr);
92                 _bt_get_cod_by_address(address, &dev_class);
93
94                 if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
95                         BT_ERR("Reject a authorization due to MDM Policy");
96                         return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
97                 }
98         }
99
100         __bt_get_file_size(file_name_array, &size, &file_count);
101         retv_if(file_count == 0, BLUETOOTH_ERROR_INVALID_PARAM);
102
103         user_info = _bt_get_user_data(BT_COMMON);
104         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
105
106         BT_INIT_PARAMS();
107         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
108
109         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
110
111         for (i = 0; i < file_count; i++) {
112                 if (strlen(file_name_array[i]) >= sizeof(filename)) {
113                         BT_ERR("[%s] has too long path.", file_name_array[i]);
114                         BT_FREE_PARAMS(in_param1, in_param2, in_param3,
115                                        in_param4, out_param);
116                         return BLUETOOTH_ERROR_INVALID_PARAM;
117                 }
118                 g_strlcpy(filename, file_name_array[i], sizeof(filename));
119                 g_array_append_vals(in_param2, filename, BT_FILE_PATH_MAX);
120         }
121
122         g_array_append_vals(in_param3, &file_count, sizeof(int));
123
124         result = _bt_send_request_async(BT_OBEX_SERVICE, BT_OPP_PUSH_FILES,
125                 in_param1, in_param2, in_param3, in_param4,
126                 user_info->cb, user_info->user_data);
127
128         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
129
130         return result;
131 }
132
133 BT_EXPORT_API int bluetooth_opc_cancel_push(void)
134 {
135         int result;
136
137         BT_CHECK_ENABLED(return);
138
139         BT_INIT_PARAMS();
140         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141
142         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_CANCEL_PUSH,
143                 in_param1, in_param2, in_param3, in_param4, &out_param);
144
145         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
146
147         return result;
148 }
149
150 BT_EXPORT_API gboolean bluetooth_opc_session_is_exist(void)
151 {
152         int result;
153         gboolean exist = FALSE;
154
155         BT_CHECK_ENABLED(return);
156
157         BT_INIT_PARAMS();
158         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
159
160         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
161                 in_param1, in_param2, in_param3, in_param4, &out_param);
162
163         if (result == BLUETOOTH_ERROR_NONE) {
164                 exist = g_array_index(out_param, gboolean, 0);
165                 BT_DBG("Exist %d", exist);
166         } else {
167                 BT_ERR("Fail to send request");
168         }
169
170         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
171
172         return result;
173 }
174
175 BT_EXPORT_API int bluetooth_opc_is_sending(gboolean *is_sending)
176 {
177         int result;
178
179         *is_sending = FALSE;
180
181         BT_CHECK_ENABLED(return);
182
183         BT_INIT_PARAMS();
184         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
185
186         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
187                 in_param1, in_param2, in_param3, in_param4, &out_param);
188
189         if (result == BLUETOOTH_ERROR_NONE)
190                 *is_sending = g_array_index(out_param, gboolean, 0);
191         else
192                 BT_ERR("Fail to send request");
193
194
195         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
196
197         return result;
198 }
199
200 BT_EXPORT_API int bluetooth_opp_get_transfer_progress(bluetooth_opp_transfer_type_t transfer_type,
201                 int transfer_id, unsigned char *progress)
202 {
203         int result;
204         int direction = transfer_type;
205         int percentage = 0;
206         BT_CHECK_ENABLED(return);
207
208         BT_INIT_PARAMS();
209         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
210
211         g_array_append_vals(in_param1, &direction, sizeof(int));
212         g_array_append_vals(in_param2, &transfer_id, sizeof(int));
213
214         result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_GET_TRANSFER_PROGRESS,
215                 in_param1, in_param2, in_param3, in_param4, &out_param);
216
217         if (result == BLUETOOTH_ERROR_NONE) {
218                 percentage = g_array_index(out_param, guint8, 0);
219                 BT_DBG("Percentage: %d", percentage);
220         } else {
221                 BT_ERR("Fail to send request");
222         }
223
224         if (progress)
225                 *progress = percentage;
226
227         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
228
229         return result;
230 }