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