c5744d98946c183174ad614620c41ebec8a739fc
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-obex-server.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 BT_EXPORT_API int bluetooth_obex_server_init(const char *dst_path)
28 {
29         int result;
30         int app_pid;
31         bt_user_info_t *user_info;
32         gboolean native_service = TRUE;
33         char path[BT_FILE_PATH_MAX];
34         int res;
35
36         BT_CHECK_ENABLED(return);
37
38         if (_bt_get_obex_server_id() != BT_NO_SERVER)
39                 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
40
41         user_info = _bt_get_user_data(BT_COMMON);
42         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
43
44         BT_INIT_PARAMS();
45         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
46
47         app_pid = getpid();
48
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));
53
54         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
55                 in_param1, in_param2, in_param3, in_param4, &out_param);
56
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);
63         } else {
64                 BT_ERR("Fail to send request");
65         }
66
67         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
68
69         return result;
70 }
71
72 BT_EXPORT_API int bluetooth_obex_server_deinit(void)
73 {
74         int result;
75         int app_pid;
76         gboolean native_service = TRUE;
77
78         BT_CHECK_ENABLED(return);
79
80         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
81                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
82
83         BT_INIT_PARAMS();
84         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
85
86         app_pid = getpid();
87
88         g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
89         g_array_append_vals(in_param2, &app_pid, sizeof(int));
90
91         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
92                 in_param1, in_param2, in_param3, in_param4, &out_param);
93
94         _bt_set_obex_server_id(BT_NO_SERVER);
95          _bt_unregister_event(BT_OPP_SERVER_EVENT);
96
97         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
98
99         return result;
100 }
101
102 BT_EXPORT_API int bluetooth_obex_server_init_without_agent(const char *dst_path)
103 {
104         int result;
105         int app_pid;
106         bt_user_info_t *user_info;
107         gboolean native_service = FALSE;
108         char path[BT_FILE_PATH_MAX];
109         int res;
110
111         BT_CHECK_ENABLED(return);
112
113         if (_bt_get_obex_server_id() != BT_NO_SERVER)
114                 return BLUETOOTH_ERROR_AGENT_ALREADY_EXIST;
115
116         user_info = _bt_get_user_data(BT_COMMON);
117         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
118
119         BT_INIT_PARAMS();
120         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
121
122         app_pid = getpid();
123
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));
128
129         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ALLOCATE,
130                 in_param1, in_param2, in_param3, in_param4, &out_param);
131
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);
138
139         } else {
140                 BT_ERR("Fail to send request");
141         }
142
143         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
144
145         return result;
146 }
147
148 BT_EXPORT_API int bluetooth_obex_server_deinit_without_agent(void)
149 {
150         int result;
151         int app_pid;
152         gboolean native_service = FALSE;
153
154         BT_CHECK_ENABLED(return);
155
156         /* Can't call this API after using bluetooth_obex_server_init
157              in same process */
158         if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
159                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
160
161         BT_INIT_PARAMS();
162         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
163
164         app_pid = getpid();
165
166         g_array_append_vals(in_param1, &native_service, sizeof(gboolean));
167         g_array_append_vals(in_param2, &app_pid, sizeof(int));
168
169         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_DEALLOCATE,
170                 in_param1, in_param2, in_param3, in_param4, &out_param);
171
172         _bt_set_obex_server_id(BT_NO_SERVER);
173         _bt_unregister_event(BT_OPP_SERVER_EVENT);
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 gboolean bluetooth_obex_server_is_activated(void)
181 {
182         int result;
183         gboolean is_activated = FALSE;
184
185         BT_INIT_PARAMS();
186         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
187
188         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_ACTIVATED,
189                 in_param1, in_param2, in_param3, in_param4, &out_param);
190
191         if (result == BLUETOOTH_ERROR_NONE) {
192                 is_activated = g_array_index(out_param, gboolean, 0);
193         } else {
194                 BT_ERR("Fail to send request");
195         }
196
197         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
198
199         return is_activated;
200 }
201
202 BT_EXPORT_API int bluetooth_obex_server_accept_connection(void)
203 {
204         int result;
205
206         /* Can't use this API in native server
207             In native server, bt-agent will control the connection
208             using system popup */
209         if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
210                 return BLUETOOTH_ERROR_INTERNAL;
211
212         BT_INIT_PARAMS();
213         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
214
215         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_CONNECTION,
216                 in_param1, in_param2, in_param3, in_param4, &out_param);
217
218         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
219
220         return result;
221 }
222
223 BT_EXPORT_API int bluetooth_obex_server_reject_connection(void)
224 {
225         int result;
226
227         /* Can't use this API in native server
228             In native server, bt-agent will control the connection
229             using system popup */
230         if (_bt_get_obex_server_id() != BT_CUSTOM_SERVER)
231                 return BLUETOOTH_ERROR_INTERNAL;
232
233         BT_INIT_PARAMS();
234         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
235
236         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_CONNECTION,
237                 in_param1, in_param2, in_param3, in_param4, &out_param);
238
239         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
240
241         return result;
242 }
243
244 BT_EXPORT_API int bluetooth_obex_server_accept_authorize(const char *filename)
245 {
246         int result;
247         char name[BT_FILE_PATH_MAX];
248
249         BT_CHECK_PARAMETER(filename, return);
250         BT_CHECK_ENABLED(return);
251
252         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
253                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
254
255         BT_INIT_PARAMS();
256         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
257
258         g_strlcpy(name, filename, sizeof(name));
259         g_array_append_vals(in_param1, name, BT_FILE_PATH_MAX);
260
261         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_ACCEPT_FILE,
262                 in_param1, in_param2, in_param3, in_param4, &out_param);
263
264         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
265
266         return result;
267 }
268
269
270 BT_EXPORT_API int bluetooth_obex_server_reject_authorize(void)
271 {
272         int result;
273
274         BT_CHECK_ENABLED(return);
275
276         if (_bt_get_obex_server_id() != BT_NATIVE_SERVER)
277                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
278
279         BT_INIT_PARAMS();
280         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
281
282         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_REJECT_FILE,
283                 in_param1, in_param2, in_param3, in_param4, &out_param);
284
285         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
286
287         return result;
288 }
289
290 BT_EXPORT_API int bluetooth_obex_server_set_destination_path(const char *dst_path)
291 {
292         int result;
293         int server_id;
294         gboolean native_service = FALSE;
295         char path[BT_FILE_PATH_MAX];
296
297         BT_CHECK_PARAMETER(dst_path, return);
298         BT_CHECK_ENABLED(return);
299
300         server_id = _bt_get_obex_server_id();
301
302         retv_if(server_id == BT_NO_SERVER,
303                         BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
304
305         BT_INIT_PARAMS();
306         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
307
308         native_service = (server_id == BT_NATIVE_SERVER) ? TRUE : FALSE;
309
310         g_strlcpy(path, dst_path, sizeof(path));
311         g_array_append_vals(in_param1, path, BT_FILE_PATH_MAX);
312         g_array_append_vals(in_param2, &native_service, sizeof(native_service));
313
314         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_PATH,
315                 in_param1, in_param2, in_param3, in_param4, &out_param);
316
317         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
318
319         return result;
320 }
321
322
323 BT_EXPORT_API int bluetooth_obex_server_set_root(const char *root)
324 {
325         int result;
326         char root_path[BT_FILE_PATH_MAX];
327
328         BT_CHECK_PARAMETER(root, return);
329         BT_CHECK_ENABLED(return);
330
331         if (_bt_get_obex_server_id() == BT_NO_SERVER)
332                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
333
334         BT_INIT_PARAMS();
335         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
336
337         g_strlcpy(root_path, root, sizeof(root_path));
338         g_array_append_vals(in_param1, root_path, BT_FILE_PATH_MAX);
339
340         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_SET_ROOT,
341                 in_param1, in_param2, in_param3, in_param4, &out_param);
342
343         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
344
345         return result;
346 }
347
348 BT_EXPORT_API int bluetooth_obex_server_cancel_transfer(int transfer_id)
349 {
350         int result;
351         int server_type;
352         int service_function = BT_OBEX_SERVER_CANCEL_TRANSFER;
353
354         BT_CHECK_ENABLED(return);
355
356         server_type = _bt_get_obex_server_id();
357
358         if (server_type == BT_NO_SERVER)
359                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
360         else if (server_type == BT_CUSTOM_SERVER)
361                 service_function = BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS;
362
363         BT_INIT_PARAMS();
364         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
365
366         g_array_append_vals(in_param1, &transfer_id, sizeof(int));
367
368         result = _bt_send_request(BT_OBEX_SERVICE, service_function,
369                 in_param1, in_param2, in_param3, in_param4, &out_param);
370
371         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
372
373         return result;
374 }
375
376 BT_EXPORT_API int bluetooth_obex_server_cancel_all_transfers(void)
377 {
378         int result;
379
380         BT_CHECK_ENABLED(return);
381
382         if (_bt_get_obex_server_id() == BT_NO_SERVER)
383                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
384
385         BT_INIT_PARAMS();
386         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
387
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);
390
391         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
392
393         return result;
394 }
395
396 BT_EXPORT_API int bluetooth_obex_server_is_receiving(gboolean *is_receiving)
397 {
398         int result;
399
400         *is_receiving = FALSE;
401
402         BT_CHECK_ENABLED(return);
403
404         BT_INIT_PARAMS();
405         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
406
407         result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_IS_RECEIVING,
408                 in_param1, in_param2, in_param3, in_param4, &out_param);
409
410         if (result == BLUETOOTH_ERROR_NONE) {
411                 *is_receiving = g_array_index(out_param, gboolean, 0);
412         } else {
413                 BT_ERR("Fail to send request");
414         }
415
416         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
417
418         return result;
419 }
420