Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-obex-server.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <string.h>
25
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
28
29 #include "bt-common.h"
30 #include "bt-request-sender.h"
31 #include "bt-event-handler.h"
32
33 BT_EXPORT_API int bluetooth_obex_server_init(const char *dst_path)
34 {
35         int result;
36         int app_pid;
37         bt_user_info_t *user_info;
38         gboolean native_service = TRUE;
39         char path[BT_FILE_PATH_MAX];
40
41         BT_CHECK_ENABLED(return);
42
43         if (_bt_get_obex_server_id() != BT_NO_SERVER)
44                 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
45
46         user_info = _bt_get_user_data(BT_COMMON);
47         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
48
49         BT_INIT_PARAMS();
50         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
51
52         app_pid = getpid();
53
54         g_strlcpy(path, dst_path, sizeof(path));
55         g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
56         g_array_append_vals(in_param2, &native_service, sizeof(gboolean));
57         g_array_append_vals(in_param3, &app_pid, sizeof(int));
58
59         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
60                 in_param1, in_param2, in_param3, in_param4, &out_param);
61
62         if (result == BLUETOOTH_ERROR_NONE) {
63                 _bt_set_obex_server_id(BT_NATIVE_SERVER);
64                  _bt_register_event(BT_OPP_SERVER_EVENT, user_info->cb,
65                                         user_info->user_data);
66         } else {
67                 BT_ERR("Fail to send request");
68         }
69
70         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
71
72         return result;
73 }
74
75 BT_EXPORT_API int bluetooth_obex_server_deinit(void)
76 {
77         int result;
78         int app_pid;
79         gboolean native_service = TRUE;
80
81         BT_CHECK_ENABLED(return);
82
83         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
84                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
85
86         BT_INIT_PARAMS();
87         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
88
89         app_pid = getpid();
90
91         g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
92         g_array_append_vals(in_param2, &app_pid, sizeof(int));
93
94         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
95                 in_param1, in_param2, in_param3, in_param4, &out_param);
96
97         _bt_set_obex_server_id(BT_NO_SERVER);
98          _bt_unregister_event(BT_OPP_SERVER_EVENT);
99
100         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
101
102         return result;
103 }
104
105 BT_EXPORT_API int bluetooth_obex_server_init_without_agent(const char *dst_path)
106 {
107         int result;
108         int app_pid;
109         bt_user_info_t *user_info;
110         gboolean native_service = FALSE;
111         char path[BT_FILE_PATH_MAX];
112
113         BT_CHECK_ENABLED(return);
114
115         if (_bt_get_obex_server_id() != BT_NO_SERVER)
116                 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
117
118         user_info = _bt_get_user_data(BT_COMMON);
119         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
120
121         BT_INIT_PARAMS();
122         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
123
124         app_pid = getpid();
125
126         g_strlcpy(path, dst_path, sizeof(path));
127         g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
128         g_array_append_vals(in_param2, &native_service, sizeof(gboolean));
129         g_array_append_vals(in_param3, &app_pid, sizeof(int));
130
131         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
132                 in_param1, in_param2, in_param3, in_param4, &out_param);
133
134         if (result == BLUETOOTH_ERROR_NONE) {
135                 _bt_set_obex_server_id(BT_CUSTOM_SERVER);
136                 _bt_register_event(BT_OPP_SERVER_EVENT, user_info->cb,
137                                         user_info->user_data);
138         } else {
139                 BT_ERR("Fail to send request");
140         }
141
142         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
143
144         return result;
145 }
146
147 BT_EXPORT_API int bluetooth_obex_server_deinit_without_agent(void)
148 {
149         int result;
150         int app_pid;
151         gboolean native_service = FALSE;
152
153         BT_CHECK_ENABLED(return);
154
155         /* Can't call this API after using bluetooth_obex_server_init
156              in same process */
157         if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
158                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
159
160         BT_INIT_PARAMS();
161         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
162
163         app_pid = getpid();
164
165         g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
166         g_array_append_vals(in_param2, &app_pid, sizeof(int));
167
168         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
169                 in_param1, in_param2, in_param3, in_param4, &out_param);
170
171         _bt_set_obex_server_id(BT_NO_SERVER);
172         _bt_unregister_event(BT_OPP_SERVER_EVENT);
173
174         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
175
176         return result;
177 }
178
179 BT_EXPORT_API gboolean bluetooth_obex_server_is_activated(void)
180 {
181         int result;
182         gboolean is_activated = FALSE;
183
184         BT_INIT_PARAMS();
185         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
186
187         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_ACTIVATED,
188                 in_param1, in_param2, in_param3, in_param4, &out_param);
189
190         if (result == BLUETOOTH_ERROR_NONE) {
191                 is_activated = g_array_index(out_param, gboolean, 0);
192         } else {
193                 BT_ERR("Fail to send request");
194         }
195
196         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
197
198         return is_activated;
199 }
200
201 BT_EXPORT_API int bluetooth_obex_server_accept_connection(void)
202 {
203         int result;
204
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;
210
211         BT_INIT_PARAMS();
212         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
213
214         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_CONNECTION,
215                 in_param1, in_param2, in_param3, in_param4, &out_param);
216
217         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
218
219         return result;
220 }
221
222 BT_EXPORT_API int bluetooth_obex_server_reject_connection(void)
223 {
224         int result;
225
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;
231
232         BT_INIT_PARAMS();
233         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
234
235         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_CONNECTION,
236                 in_param1, in_param2, in_param3, in_param4, &out_param);
237
238         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
239
240         return result;
241 }
242
243 BT_EXPORT_API int bluetooth_obex_server_accept_authorize(const char *filename)
244 {
245         int result;
246         char name[BT_FILE_PATH_MAX];
247
248         BT_CHECK_PARAMETER(filename, return);
249         BT_CHECK_ENABLED(return);
250
251         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
252                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
253
254         BT_INIT_PARAMS();
255         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
256
257         g_strlcpy(name, filename, sizeof(name));
258         g_array_append_vals(in_param1, name, BT_FILE_PATH_MAX);
259
260         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_FILE,
261                 in_param1, in_param2, in_param3, in_param4, &out_param);
262
263         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
264
265         return result;
266 }
267
268
269 BT_EXPORT_API int bluetooth_obex_server_reject_authorize(void)
270 {
271         int result;
272
273         BT_CHECK_ENABLED(return);
274
275         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
276                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
277
278         BT_INIT_PARAMS();
279         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
280
281         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_FILE,
282                 in_param1, in_param2, in_param3, in_param4, &out_param);
283
284         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
285
286         return result;
287 }
288
289 BT_EXPORT_API int bluetooth_obex_server_set_destination_path(const char *dst_path)
290 {
291         int result;
292         int server_id;
293         gboolean native_service = FALSE;
294         char path[BT_FILE_PATH_MAX];
295
296         BT_CHECK_PARAMETER(dst_path, return);
297         BT_CHECK_ENABLED(return);
298
299         server_id = _bt_get_obex_server_id();
300
301         retv_if(server_id == BT_NO_SERVER,
302                         BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
303
304         BT_INIT_PARAMS();
305         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
306
307         native_service = (server_id == BT_NATIVE_SERVER) ? TRUE : FALSE;
308
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));
312
313         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_PATH,
314                 in_param1, in_param2, in_param3, in_param4, &out_param);
315
316         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
317
318         return result;
319 }
320
321
322 BT_EXPORT_API int bluetooth_obex_server_set_root(const char *root)
323 {
324         int result;
325         char root_path[BT_FILE_PATH_MAX];
326
327         BT_CHECK_PARAMETER(root, return);
328         BT_CHECK_ENABLED(return);
329
330         if (_bt_get_obex_server_id() == BT_NO_SERVER)
331                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
332
333         BT_INIT_PARAMS();
334         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
335
336         g_strlcpy(root_path, root, sizeof(root_path));
337         g_array_append_vals(in_param1, root_path, BT_FILE_PATH_MAX);
338
339         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_ROOT,
340                 in_param1, in_param2, in_param3, in_param4, &out_param);
341
342         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
343
344         return result;
345 }
346
347 BT_EXPORT_API int bluetooth_obex_server_cancel_transfer(int transfer_id)
348 {
349         int result;
350         int server_type;
351         int service_function = BT_OBEX_SERVER_CANCEL_TRANSFER;
352
353         BT_CHECK_ENABLED(return);
354
355         server_type = _bt_get_obex_server_id();
356
357         if (server_type == BT_NO_SERVER)
358                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
359         else if (server_type == BT_CUSTOM_SERVER)
360                 service_function = BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS;
361
362         BT_INIT_PARAMS();
363         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
364
365         g_array_append_vals(in_param1, &transfer_id, sizeof(int));
366
367         result = _bt_send_request(BT_OBEX_SERVICE, service_function,
368                 in_param1, in_param2, in_param3, in_param4, &out_param);
369
370         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
371
372         return result;
373 }
374
375 BT_EXPORT_API int bluetooth_obex_server_cancel_all_transfers(void)
376 {
377         int result;
378
379         BT_CHECK_ENABLED(return);
380
381         if (_bt_get_obex_server_id() == BT_NO_SERVER)
382                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
383
384         BT_INIT_PARAMS();
385         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
386
387         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS,
388                 in_param1, in_param2, in_param3, in_param4, &out_param);
389
390         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
391
392         return result;
393 }
394
395 BT_EXPORT_API int bluetooth_obex_server_is_receiving(gboolean *is_receiving)
396 {
397         int result;
398
399         *is_receiving = FALSE;
400
401         BT_CHECK_ENABLED(return);
402
403         BT_INIT_PARAMS();
404         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
405
406         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_RECEIVING,
407                 in_param1, in_param2, in_param3, in_param4, &out_param);
408
409         if (result == BLUETOOTH_ERROR_NONE) {
410                 *is_receiving = g_array_index(out_param, gboolean, 0);
411         } else {
412                 BT_ERR("Fail to send request");
413         }
414
415         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
416
417         return result;
418 }
419