2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "bluetooth-api.h"
21 #include "bt-internal-types.h"
23 #include "bt-common.h"
24 #include "bt-request-sender.h"
25 #include "bt-event-handler.h"
28 static void __bt_get_file_size(char **files, unsigned long *file_size, int *count)
31 unsigned long size = 0;
33 while (files[file_count] != NULL) {
34 size = size + strlen(files[file_count]);
42 BT_EXPORT_API int bluetooth_opc_init(void)
44 bt_user_info_t *user_info;
46 user_info = _bt_get_user_data(BT_COMMON);
47 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
49 return _bt_register_event(BT_OPP_CLIENT_EVENT, user_info->cb, user_info->user_data);
52 BT_EXPORT_API int bluetooth_opc_deinit(void)
54 return _bt_unregister_event(BT_OPP_CLIENT_EVENT);
57 BT_EXPORT_API int bluetooth_opc_push_files(bluetooth_device_address_t *remote_address,
58 char **file_name_array)
64 bt_user_info_t *user_info;
65 char filename[BT_FILE_PATH_MAX];
67 BT_CHECK_PARAMETER(remote_address, return);
68 BT_CHECK_PARAMETER(file_name_array, return);
69 BT_CHECK_ENABLED(return);
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;
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;
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;
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;
91 _bt_convert_addr_type_to_string(address, remote_address->addr);
92 _bt_get_cod_by_address(address, &dev_class);
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;
100 __bt_get_file_size(file_name_array, &size, &file_count);
101 retv_if(file_count == 0, BLUETOOTH_ERROR_INVALID_PARAM);
103 user_info = _bt_get_user_data(BT_COMMON);
104 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
107 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
109 g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
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;
118 g_strlcpy(filename, file_name_array[i], sizeof(filename));
119 g_array_append_vals(in_param2, filename, BT_FILE_PATH_MAX);
122 g_array_append_vals(in_param3, &file_count, sizeof(int));
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);
128 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
133 BT_EXPORT_API int bluetooth_opc_cancel_push(void)
137 BT_CHECK_ENABLED(return);
140 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
142 result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_CANCEL_PUSH,
143 in_param1, in_param2, in_param3, in_param4, &out_param);
145 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
150 BT_EXPORT_API gboolean bluetooth_opc_session_is_exist(void)
153 gboolean exist = FALSE;
155 BT_CHECK_ENABLED(return);
158 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
160 result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
161 in_param1, in_param2, in_param3, in_param4, &out_param);
163 if (result == BLUETOOTH_ERROR_NONE) {
164 exist = g_array_index(out_param, gboolean, 0);
165 BT_DBG("Exist %d", exist);
167 BT_ERR("Fail to send request");
170 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
175 BT_EXPORT_API int bluetooth_opc_is_sending(gboolean *is_sending)
181 BT_CHECK_ENABLED(return);
184 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
186 result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_IS_PUSHING_FILES,
187 in_param1, in_param2, in_param3, in_param4, &out_param);
189 if (result == BLUETOOTH_ERROR_NONE)
190 *is_sending = g_array_index(out_param, gboolean, 0);
192 BT_ERR("Fail to send request");
195 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
200 BT_EXPORT_API int bluetooth_opp_get_transfer_progress(bluetooth_opp_transfer_type_t transfer_type,
201 int transfer_id, unsigned char *progress)
204 int direction = transfer_type;
206 BT_CHECK_ENABLED(return);
209 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
211 g_array_append_vals(in_param1, &direction, sizeof(int));
212 g_array_append_vals(in_param2, &transfer_id, sizeof(int));
214 result = _bt_send_request(BT_OBEX_SERVICE, BT_OPP_GET_TRANSFER_PROGRESS,
215 in_param1, in_param2, in_param3, in_param4, &out_param);
217 if (result == BLUETOOTH_ERROR_NONE) {
218 percentage = g_array_index(out_param, guint8, 0);
219 BT_DBG("Percentage: %d", percentage);
221 BT_ERR("Fail to send request");
225 *progress = percentage;
227 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);