Remove wrong dependency in the systemd service file
[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         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
352         BT_CHECK_ENABLED(return);
353
354         server_type = _bt_get_obex_server_id();
355
356         if (server_type == BT_NO_SERVER)
357                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
358
359         BT_INIT_PARAMS();
360         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
361
362         g_array_append_vals(in_param1, &transfer_id, sizeof(int));
363
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);
367         else
368                 result = _bt_send_request(BT_OBEX_SERVICE, BT_OBEX_SERVER_CANCEL_TRANSFER,
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         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
416
417         return result;
418 }
419