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"
27 BT_EXPORT_API int bluetooth_obex_server_init(const char *dst_path)
31 bt_user_info_t *user_info;
32 gboolean native_service = TRUE;
33 char path[BT_FILE_PATH_MAX];
36 BT_CHECK_ENABLED(return);
38 if (_bt_get_obex_server_id() != BT_NO_SERVER)
39 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
41 user_info = _bt_get_user_data(BT_COMMON);
42 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
45 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
49 g_strlcpy(path, dst_path, sizeof(path));
50 g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
51 g_array_append_vals(in_param2, &native_service, sizeof(gboolean));
52 g_array_append_vals(in_param3, &app_pid, sizeof(int));
54 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
55 in_param1, in_param2, in_param3, in_param4, &out_param);
57 if (result == BLUETOOTH_ERROR_NONE) {
58 _bt_set_obex_server_id(BT_NATIVE_SERVER);
59 res = _bt_register_event(BT_OPP_SERVER_EVENT, user_info->cb,
60 user_info->user_data);
61 if (res != BLUETOOTH_ERROR_NONE)
62 BT_ERR("Fail to _bt_register_event(%d)", res);
64 BT_ERR("Fail to send request");
67 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
72 BT_EXPORT_API int bluetooth_obex_server_deinit(void)
76 gboolean native_service = TRUE;
78 BT_CHECK_ENABLED(return);
80 if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
81 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
84 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88 g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
89 g_array_append_vals(in_param2, &app_pid, sizeof(int));
91 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
92 in_param1, in_param2, in_param3, in_param4, &out_param);
94 _bt_set_obex_server_id(BT_NO_SERVER);
95 _bt_unregister_event(BT_OPP_SERVER_EVENT);
97 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
102 BT_EXPORT_API int bluetooth_obex_server_init_without_agent(const char *dst_path)
106 bt_user_info_t *user_info;
107 gboolean native_service = FALSE;
108 char path[BT_FILE_PATH_MAX];
111 BT_CHECK_ENABLED(return);
113 if (_bt_get_obex_server_id() != BT_NO_SERVER)
114 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
116 user_info = _bt_get_user_data(BT_COMMON);
117 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
120 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
124 g_strlcpy(path, dst_path, sizeof(path));
125 g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
126 g_array_append_vals(in_param2, &native_service, sizeof(gboolean));
127 g_array_append_vals(in_param3, &app_pid, sizeof(int));
129 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
130 in_param1, in_param2, in_param3, in_param4, &out_param);
132 if (result == BLUETOOTH_ERROR_NONE) {
133 _bt_set_obex_server_id(BT_CUSTOM_SERVER);
134 res = _bt_register_event(BT_OPP_SERVER_EVENT, user_info->cb,
135 user_info->user_data);
136 if (res != BLUETOOTH_ERROR_NONE)
137 BT_ERR("Fail to _bt_register_event(%d)", res);
140 BT_ERR("Fail to send request");
143 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
148 BT_EXPORT_API int bluetooth_obex_server_deinit_without_agent(void)
152 gboolean native_service = FALSE;
154 BT_CHECK_ENABLED(return);
156 /* Can't call this API after using bluetooth_obex_server_init
158 if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
159 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
162 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
166 g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
167 g_array_append_vals(in_param2, &app_pid, sizeof(int));
169 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
170 in_param1, in_param2, in_param3, in_param4, &out_param);
172 _bt_set_obex_server_id(BT_NO_SERVER);
173 _bt_unregister_event(BT_OPP_SERVER_EVENT);
175 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
180 BT_EXPORT_API gboolean bluetooth_obex_server_is_activated(void)
183 gboolean is_activated = FALSE;
186 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
188 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_ACTIVATED,
189 in_param1, in_param2, in_param3, in_param4, &out_param);
191 if (result == BLUETOOTH_ERROR_NONE)
192 is_activated = g_array_index(out_param, gboolean, 0);
194 BT_ERR("Fail to send request");
196 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
201 BT_EXPORT_API int bluetooth_obex_server_accept_connection(void)
205 /* Can't use this API in native server
206 In native server, bt-agent will control the connection
207 using system popup */
208 if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
209 return BLUETOOTH_ERROR_INTERNAL;
212 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
214 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_CONNECTION,
215 in_param1, in_param2, in_param3, in_param4, &out_param);
217 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
222 BT_EXPORT_API int bluetooth_obex_server_reject_connection(void)
226 /* Can't use this API in native server
227 In native server, bt-agent will control the connection
228 using system popup */
229 if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
230 return BLUETOOTH_ERROR_INTERNAL;
233 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
235 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_CONNECTION,
236 in_param1, in_param2, in_param3, in_param4, &out_param);
238 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
243 BT_EXPORT_API int bluetooth_obex_server_accept_authorize(const char *filename)
246 char name[BT_FILE_PATH_MAX];
248 BT_CHECK_PARAMETER(filename, return);
249 BT_CHECK_ENABLED(return);
251 if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
252 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
255 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
257 g_strlcpy(name, filename, sizeof(name));
258 g_array_append_vals(in_param1, name, BT_FILE_PATH_MAX);
260 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_FILE,
261 in_param1, in_param2, in_param3, in_param4, &out_param);
263 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
269 BT_EXPORT_API int bluetooth_obex_server_reject_authorize(void)
273 BT_CHECK_ENABLED(return);
275 if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
276 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
279 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
281 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_FILE,
282 in_param1, in_param2, in_param3, in_param4, &out_param);
284 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
289 BT_EXPORT_API int bluetooth_obex_server_set_destination_path(const char *dst_path)
293 gboolean native_service = FALSE;
294 char path[BT_FILE_PATH_MAX];
296 BT_CHECK_PARAMETER(dst_path, return);
297 BT_CHECK_ENABLED(return);
299 server_id = _bt_get_obex_server_id();
301 retv_if(server_id == BT_NO_SERVER,
302 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
305 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
307 native_service = (server_id == BT_NATIVE_SERVER) ? TRUE : FALSE;
309 g_strlcpy(path, dst_path, sizeof(path));
310 g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
311 g_array_append_vals(in_param2, &native_service, sizeof(native_service));
313 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_PATH,
314 in_param1, in_param2, in_param3, in_param4, &out_param);
316 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
322 BT_EXPORT_API int bluetooth_obex_server_set_root(const char *root)
325 char root_path[BT_FILE_PATH_MAX];
327 BT_CHECK_PARAMETER(root, return);
328 BT_CHECK_ENABLED(return);
330 if (_bt_get_obex_server_id() == BT_NO_SERVER)
331 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
334 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
336 g_strlcpy(root_path, root, sizeof(root_path));
337 g_array_append_vals(in_param1, root_path, BT_FILE_PATH_MAX);
339 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_ROOT,
340 in_param1, in_param2, in_param3, in_param4, &out_param);
342 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
347 BT_EXPORT_API int bluetooth_obex_server_cancel_transfer(int transfer_id)
352 BT_CHECK_ENABLED(return);
354 server_type = _bt_get_obex_server_id();
356 if (server_type == BT_NO_SERVER)
357 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
360 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
362 g_array_append_vals(in_param1, &transfer_id, sizeof(int));
364 if (server_type == BT_CUSTOM_SERVER)
365 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS,
366 in_param1, in_param2, in_param3, in_param4, &out_param);
368 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_CANCEL_TRANSFER,
369 in_param1, in_param2, in_param3, in_param4, &out_param);
371 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
376 BT_EXPORT_API int bluetooth_obex_server_cancel_all_transfers(void)
380 BT_CHECK_ENABLED(return);
382 if (_bt_get_obex_server_id() == BT_NO_SERVER)
383 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
386 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
388 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS,
389 in_param1, in_param2, in_param3, in_param4, &out_param);
391 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
396 BT_EXPORT_API int bluetooth_obex_server_is_receiving(gboolean *is_receiving)
400 *is_receiving = FALSE;
402 BT_CHECK_ENABLED(return);
405 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
407 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_RECEIVING,
408 in_param1, in_param2, in_param3, in_param4, &out_param);
410 if (result == BLUETOOTH_ERROR_NONE)
411 *is_receiving = g_array_index(out_param, gboolean, 0);
413 BT_ERR("Fail to send request");
415 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);