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