e46d1f87c52c8feb42086e06a1b454822004c3bd
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-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 <stdio.h>
19 #include <glib.h>
20 #include <dlog.h>
21 #include <string.h>
22 #include <dirent.h>
23 #ifdef TIZEN_DPM_ENABLE
24 #include "bt-service-dpm.h"
25 #endif
26
27 #include <vconf.h>
28
29 #include <gio/gio.h>
30
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
33
34 #include "bt-service-common.h"
35 #include "bt-service-event.h"
36 #include "bt-service-util.h"
37 #include "bt-service-obex-agent.h"
38 #include "bt-service-obex-server.h"
39 #include "bt-service-agent.h"
40
41 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
42 #define BT_OBEX_SERVER_AGENT_PATH "/org/obex/server_agent"
43
44 #define BT_OBEX_SERVICE "org.bluez.obex"
45 #define BT_OBEX_MANAGER "org.bluez.obex.AgentManager1"
46 #define BT_OBEX_PATH "/org/bluez/obex"
47
48 #define BT_OBEX_PATH_PREFIX "/opt/usr/media"
49 #define BT_OBEX_DEFAULT_PATH "/opt/usr/home/owner/media"
50 #define BT_OBEX_PATH_MAX_LENGTH 255
51
52
53 typedef struct {
54         char *filename;
55         char *file_path;
56         char *path;
57         char *type;
58         char *device_name;
59         int transfer_id;
60         gint64 file_size;
61         gint64 progress;
62         char *address;
63 } bt_transfer_info_t;
64
65 typedef struct {
66         GDBusMethodInvocation *reply_context;
67         guint64 file_size;
68         char *filename;
69         char *file_path;
70         char *device_name;
71         char *transfer_path;
72         char *address;
73         unsigned char contact_auth_info[5];
74 } bt_auth_info_t;
75
76 typedef struct {
77         char *dest_path;
78         char *sender;
79         int app_pid;
80 } bt_server_info_t;
81
82 typedef struct {
83         GDBusProxy *proxy;
84         int server_type;
85         int accept_id;
86         bt_auth_info_t *auth_info;
87         bt_server_info_t *native_server;
88         bt_server_info_t *custom_server;
89 } bt_obex_agent_info_t;
90
91 typedef struct {
92         char *path;
93         char *address;
94         gboolean authorized;
95 } bt_session_info_t;
96
97 static GSList *transfers;
98 static bt_obex_agent_info_t agent_info;
99 static GSList *session_list = NULL;
100
101 static bt_session_info_t *__bt_find_session_by_path(char *transfer_path)
102 {
103         GSList *l;
104         bt_session_info_t *session;
105
106         retv_if(transfer_path == NULL, NULL);
107
108         for (l = session_list; l != NULL; l = l->next) {
109                 session = l->data;
110
111                 if (session == NULL)
112                         continue;
113
114                 if (g_strcmp0(session->path, transfer_path) == 0)
115                         return session;
116         }
117
118         return NULL;
119 }
120
121 static GQuark __bt_obex_error_quark(void)
122 {
123         static GQuark quark = 0;
124         if (!quark)
125                 quark = g_quark_from_static_string("agent");
126
127         return quark;
128 }
129
130 static bt_transfer_info_t *__bt_find_transfer_by_id(int transfer_id)
131 {
132         GSList *l;
133         bt_transfer_info_t *transfer;
134
135         for (l = transfers; l != NULL; l = l->next) {
136                 transfer = l->data;
137
138                 if (transfer == NULL)
139                         continue;
140
141                 if (transfer->transfer_id == transfer_id)
142                         return transfer;
143         }
144
145         return NULL;
146 }
147
148 bt_transfer_info_t *__bt_find_transfer_by_address(const char *address)
149 {
150         BT_DBG("+");
151         GSList *l;
152         bt_transfer_info_t *transfer;
153
154         retv_if(address == NULL, NULL);
155
156         for (l = transfers; l != NULL; l = l->next) {
157                 transfer = l->data;
158
159                 if (transfer == NULL)
160                         continue;
161
162                 if (g_strcmp0(transfer->address, address) == 0)
163                         return transfer;
164         }
165         BT_DBG("-");
166         return NULL;
167 }
168
169 static bt_transfer_info_t *__bt_find_transfer_by_path(const char *transfer_path)
170 {
171         GSList *l;
172         bt_transfer_info_t *transfer;
173
174         retv_if(transfer_path == NULL, NULL);
175
176         for (l = transfers; l != NULL; l = l->next) {
177                 transfer = l->data;
178
179                 if (transfer == NULL)
180                         continue;
181
182                 if (g_strcmp0(transfer->path, transfer_path) == 0)
183                         return transfer;
184         }
185
186         return NULL;
187 }
188
189 static void __bt_free_server_info(bt_server_info_t *server_info)
190 {
191         ret_if(server_info == NULL);
192
193         g_free(server_info->sender);
194         g_free(server_info->dest_path);
195         g_free(server_info);
196 }
197
198 static void __bt_free_auth_info(bt_auth_info_t *auto_info)
199 {
200         ret_if(auto_info == NULL);
201
202         g_free(auto_info->filename);
203         g_free(auto_info->transfer_path);
204         g_free(auto_info->device_name);
205         g_free(auto_info->address);
206         g_free(auto_info);
207 }
208
209 static void __bt_free_transfer_info(bt_transfer_info_t *transfer_info)
210 {
211         ret_if(transfer_info == NULL);
212
213         g_free(transfer_info->path);
214         g_free(transfer_info->filename);
215         g_free(transfer_info->file_path);
216         g_free(transfer_info->type);
217         g_free(transfer_info->device_name);
218         g_free(transfer_info->address);
219         g_free(transfer_info);
220 }
221
222 void _bt_obex_check_pending_transfer(const char *address)
223 {
224         BT_DBG("+");
225         GVariant *param = NULL;
226         bt_transfer_info_t *transfer_info = __bt_find_transfer_by_address(address);
227         if (transfer_info != NULL) {
228                 int result = BLUETOOTH_ERROR_CANCEL;
229                 param = g_variant_new("(issssstii)", result,
230                                         transfer_info->filename,
231                                         transfer_info->type,
232                                         transfer_info->device_name,
233                                         transfer_info->file_path,
234                                         transfer_info->address,
235                                         transfer_info->file_size,
236                                         transfer_info->transfer_id,
237                                         agent_info.server_type);
238                 _bt_send_event(BT_OPP_SERVER_EVENT,
239                         BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
240                         param);
241                 transfers = g_slist_remove(transfers, transfer_info);
242                 __bt_free_transfer_info(transfer_info);
243         }
244         BT_DBG("-");
245 }
246
247 static char *__bt_get_remote_device_name(const char *bdaddress)
248 {
249         char *device_path = NULL;
250         char *name = NULL;
251         GVariant *value;
252         GVariant *result = NULL;
253         GError *err = NULL;
254         GDBusProxy *device_proxy;
255         GDBusConnection *conn;
256
257         retv_if(bdaddress == NULL, NULL);
258
259         device_path = _bt_get_device_object_path((char *)bdaddress);
260         retv_if(device_path == NULL, NULL);
261
262         conn = _bt_gdbus_get_system_gconn();
263         retv_if(conn == NULL, NULL);
264         BT_INFO("Device_path %s", device_path);
265         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
266                                                 NULL, BT_BLUEZ_NAME,
267                                                 device_path,
268                                                 BT_PROPERTIES_INTERFACE,
269                                                 NULL, &err);
270
271         g_free(device_path);
272         retv_if(device_proxy == NULL, NULL);
273
274         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
275                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
276                         G_DBUS_CALL_FLAGS_NONE,
277                         DBUS_TIMEOUT, NULL,
278                         &err);
279         if (err) {
280                 BT_ERR("DBus Error : %s", err->message);
281                 g_clear_error(&err);
282                 return NULL;
283         }
284         if (result == NULL) {
285                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
286                 return NULL;
287         }
288         g_variant_get(result, "(@a{sv})", &value);
289
290         if (value) {
291                 GVariant *temp_value = g_variant_lookup_value(value, "Alias",
292                         G_VARIANT_TYPE_STRING);
293                 g_variant_get(temp_value, "s", &name);
294                 if (temp_value)
295                         g_variant_unref(temp_value);
296
297                 if (name != NULL)
298                         DBG_SECURE("Alias Name [%s]", name);
299                 else {
300                         temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
301                         g_variant_get(temp_value, "s", &name);
302                         if (temp_value)
303                                 g_variant_unref(temp_value);
304                         DBG_SECURE("Name = %s", name);
305                 }
306         }
307         g_variant_unref(result);
308         g_object_unref(device_proxy);
309         return name;
310 }
311
312 static void __bt_get_remote_device_name_authinfo(const char *bdaddress,
313                                         char **device_name, unsigned char *auth_info)
314 {
315         char *device_path = NULL;
316         char *name = NULL;
317         gboolean is_alias_set;
318         GVariant *value;
319         GVariant *result = NULL;
320         GError *err = NULL;
321         GDBusProxy *device_proxy;
322         GDBusConnection *conn;
323
324         ret_if(bdaddress == NULL);
325
326         device_path = _bt_get_device_object_path((char *)bdaddress);
327         ret_if(device_path == NULL);
328
329         conn = _bt_gdbus_get_system_gconn();
330         ret_if(conn == NULL);
331         BT_INFO("Device_path %s", device_path);
332         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
333                                                 NULL, BT_BLUEZ_NAME,
334                                                 device_path,
335                                                 BT_PROPERTIES_INTERFACE,
336                                                 NULL, &err);
337
338         g_free(device_path);
339         ret_if(device_proxy == NULL);
340
341         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
342                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
343                         G_DBUS_CALL_FLAGS_NONE,
344                         DBUS_TIMEOUT, NULL,
345                         &err);
346         if (err) {
347                 BT_ERR("DBus Error : %s", err->message);
348                 g_clear_error(&err);
349                 return;
350         }
351         if (result == NULL) {
352                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
353                 return;
354         }
355         g_variant_get(result, "(@a{sv})", &value);
356
357         if (value) {
358                 GVariant *temp_value = g_variant_lookup_value(value, "Alias",
359                         G_VARIANT_TYPE_STRING);
360                 g_variant_get(temp_value, "s", &name);
361                 if (temp_value)
362                         g_variant_unref(temp_value);
363
364                 if (name != NULL)
365                         DBG_SECURE("Alias Name [%s]", name);
366                 else {
367                         temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
368                         g_variant_get(temp_value, "s", &name);
369                         if (temp_value)
370                                 g_variant_unref(temp_value);
371                         DBG_SECURE("Name = %s", name);
372                 }
373                 temp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
374                 if (temp_value) {
375                         is_alias_set = g_variant_get_boolean(temp_value);
376                         g_variant_unref(temp_value);
377                 } else {
378                         is_alias_set = FALSE;
379                 }
380
381                 if (is_alias_set == FALSE)
382                         __bt_get_auth_info(value, (char *)auth_info);
383         }
384         g_variant_unref(result);
385         g_object_unref(device_proxy);
386
387         *device_name = g_strdup(name);
388         g_free(name);
389         return;
390 }
391
392 static int __bt_get_transfer_id(const char *path)
393 {
394         char *tmp = NULL;
395         if (path == NULL)
396                 return -1;
397
398         tmp = strrchr(path, 'r') + 1;
399         retv_if(tmp == NULL, -1);
400
401         return atoi(tmp);
402 }
403
404 static GDBusProxy *__bt_get_transfer_proxy(const char *transfer_path)
405 {
406         GDBusConnection *conn;
407         GDBusProxy *proxy;
408         GError *err = NULL;
409
410         conn = _bt_gdbus_get_session_gconn();
411         retv_if(conn == NULL, NULL);
412
413         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
414                                         NULL, BT_OBEX_SERVICE_NAME,
415                                         transfer_path,
416                                         BT_OBEX_TRANSFER_INTERFACE,
417                                         NULL, &err);
418
419         if (err) {
420                 BT_ERR("Error : %s", err->message);
421                 g_clear_error(&err);
422                 return NULL;
423         }
424
425         return proxy;
426 }
427
428 static GDBusProxy *__bt_get_transfer_properties_proxy(const char *transfer_path)
429 {
430         GDBusConnection *conn;
431         GDBusProxy *proxy;
432         GError *err = NULL;
433         conn = _bt_gdbus_get_session_gconn();
434         retv_if(conn == NULL, NULL);
435
436         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
437                                         NULL, BT_OBEX_SERVICE_NAME,
438                                         transfer_path,
439                                         BT_PROPERTIES_INTERFACE,
440                                         NULL, &err);
441         if (err) {
442                 BT_ERR("Error : %s", err->message);
443                 g_clear_error(&err);
444                 return NULL;
445         }
446         return proxy;
447 }
448
449 static int __bt_get_transfer_properties(bt_transfer_info_t *transfer_info,
450                                         const char *transfer_path)
451 {
452         GDBusProxy *transfer_proxy;
453         GVariant *result = NULL;
454         GError *err = NULL;
455         GVariantIter *iter = NULL;
456         BT_CHECK_PARAMETER(transfer_info, return);
457         BT_CHECK_PARAMETER(transfer_path, return);
458
459         transfer_proxy = __bt_get_transfer_properties_proxy(transfer_path);
460
461         retv_if(transfer_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
462
463         result = g_dbus_proxy_call_sync(transfer_proxy, "GetAll",
464                         g_variant_new("(s)", BT_OBEX_TRANSFER_INTERFACE),
465                         G_DBUS_CALL_FLAGS_NONE,
466                         DBUS_TIMEOUT, NULL,
467                         &err);
468
469         if (err) {
470                 BT_ERR("DBus Error : %s", err->message);
471                 g_clear_error(&err);
472                 goto fail;
473         }
474         if (result == NULL) {
475                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
476                 goto fail;
477         }
478
479         g_variant_get(result, "(a{sv})", &iter);
480         g_variant_unref(result);
481         if (iter) {
482                 const gchar *key;
483                 GVariant *val;
484                 gsize len = 0;
485                 while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
486                         if (g_strcmp0(key, "Operation") == 0) {
487                                 transfer_info->type = g_variant_dup_string(val, &len);
488                         } else if (g_strcmp0(key, "Name") == 0) {
489                                 transfer_info->filename = g_variant_dup_string(val, &len);
490                         } else if (g_strcmp0(key, "Size") == 0) {
491                                 transfer_info->file_size = g_variant_get_uint64(val);
492                         } else if (g_strcmp0(key, "Address") == 0) {
493                                 transfer_info->address = g_variant_dup_string(val, &len);
494                                 BT_INFO("addressss %s", transfer_info->address);
495                         } else if (g_strcmp0(key, "Filename") == 0) {
496                                 transfer_info->file_path = g_variant_dup_string(val, &len);
497                                 if (!transfer_info->file_path)
498                                         transfer_info->file_path = g_strdup(transfer_info->filename);
499                         }
500                 }
501                 g_variant_iter_free(iter);
502
503                 if (transfer_info->address == NULL)
504                         goto fail;
505                 transfer_info->device_name = __bt_get_remote_device_name(transfer_info->address);
506                 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
507                 if (!transfer_info->device_name)
508                         transfer_info->device_name = g_strdup(transfer_info->address);
509
510                 if (transfer_info->type == NULL)
511                         goto fail;
512
513                 transfer_info->path = g_strdup(transfer_path);
514         }
515
516         g_object_unref(transfer_proxy);
517         return BLUETOOTH_ERROR_NONE;
518
519 fail:
520         g_object_unref(transfer_proxy);
521         return BLUETOOTH_ERROR_INTERNAL;
522 }
523
524 static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
525                                         const char *path,
526                                         gpointer user_data)
527 {
528         char *device_name = NULL;
529         unsigned char auth_info[5] = {0, };
530         int result = BLUETOOTH_ERROR_NONE;
531         GDBusProxy *transfer_properties_proxy;
532         char * bdaddress = NULL;
533         GVariant *ret;
534         GVariantIter *iter;
535         GVariant *param = NULL;
536         GError *err = NULL;
537         bt_session_info_t *session_info = NULL;
538
539         BT_DBG(" path [%s] \n", path);
540
541         transfer_properties_proxy = __bt_get_transfer_properties_proxy(path);
542
543         retv_if(transfer_properties_proxy == NULL, FALSE);
544
545         ret = g_dbus_proxy_call_sync(transfer_properties_proxy, "GetAll",
546                         g_variant_new("(s)", BT_OBEX_TRANSFER_INTERFACE),
547                         G_DBUS_CALL_FLAGS_NONE,
548                         DBUS_TIMEOUT, NULL,
549                         &err);
550         if (err) {
551                 BT_ERR("DBus Error : %s", err->message);
552                 g_clear_error(&err);
553                 return FALSE;
554         }
555         if (ret == NULL) {
556                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
557                 return FALSE;
558         }
559         g_variant_get(ret, "(a{sv})", &iter);
560         g_variant_unref(ret);
561         if (iter == NULL) {
562                 g_object_unref(transfer_properties_proxy);
563                 return FALSE;
564         }
565
566         __bt_free_auth_info(agent_info.auth_info);
567
568         agent_info.auth_info = g_malloc(sizeof(bt_auth_info_t));
569
570         memset(agent_info.auth_info, 0, sizeof(bt_auth_info_t));
571
572         agent_info.auth_info->reply_context = context;
573
574         agent_info.auth_info->transfer_path = g_strdup(path);
575
576 #ifdef TIZEN_DPM_ENABLE
577         if (_bt_dpm_get_allow_bluetooth_mode() == DPM_BT_HANDSFREE_ONLY) {
578                 /* Free auth info in next function */
579                 _bt_obex_server_reject_authorize();
580                 return FALSE;
581         }
582 #endif
583         if (iter) {
584                 const gchar *key;
585                 GVariant *val;
586                 gsize len = 0;
587                 while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
588                         if (g_strcmp0(key, "Name") == 0) {
589                                 agent_info.auth_info->filename = g_variant_dup_string(val, &len);
590                         } else if (g_strcmp0(key, "Address") == 0) {
591                                 bdaddress = g_variant_dup_string(val, &len);
592                         } else if (g_strcmp0(key, "Size") == 0) {
593                                 agent_info.auth_info->file_size = g_variant_get_uint64(val);
594                         }
595                 }
596                 g_variant_iter_free(iter);
597         }
598
599         __bt_get_remote_device_name_authinfo(bdaddress, &device_name, auth_info);
600
601         if (!device_name)
602                 device_name = g_strdup(bdaddress);
603
604         agent_info.auth_info->address = g_strdup(bdaddress);
605         agent_info.auth_info->device_name = device_name;
606         memcpy(agent_info.auth_info->contact_auth_info, auth_info, 5);
607
608         session_info = __bt_find_session_by_path((char *)path);
609         if (NULL == session_info) {
610                 session_info = g_malloc0(sizeof(bt_session_info_t));
611                 session_info->path = g_strdup(path);
612                 session_info->address = g_strdup(bdaddress);
613                 session_info->authorized = FALSE;
614                 session_list = g_slist_append(session_list, session_info);
615         }
616
617         g_object_unref(transfer_properties_proxy);
618         g_free(bdaddress);
619
620         if (agent_info.server_type == BT_CUSTOM_SERVER) {
621                 /* No need to send the event */
622                 _bt_obex_server_accept_authorize(agent_info.auth_info->filename, FALSE);
623                 return TRUE;
624         }
625
626         if (session_info->authorized == FALSE) {
627                 _bt_launch_system_popup(BT_AGENT_EVENT_EXCHANGE_REQUEST, device_name,
628                                         auth_info, NULL, NULL, BT_OBEX_SERVER_AGENT_PATH);
629         } else {
630                 param = g_variant_new("(istss)", result,
631                         agent_info.auth_info->filename,
632                         agent_info.auth_info->file_size,
633                         agent_info.auth_info->address,
634                         agent_info.auth_info->device_name);
635                 _bt_send_event(BT_OPP_SERVER_EVENT,
636                         BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
637         }
638
639         return TRUE;
640 }
641
642 void _bt_obex_transfer_started(const char *transfer_path)
643 {
644         bt_transfer_info_t *transfer_info;
645         request_info_t *req_info;
646         GVariant *out_param1 = NULL;
647         GVariant *param = NULL;
648         GVariantBuilder *builder = NULL;
649         int result = BLUETOOTH_ERROR_NONE;
650         int i = 0;
651
652         BT_DBG("%s", transfer_path);
653
654         transfer_info = g_malloc0(sizeof(bt_transfer_info_t));
655
656         if (agent_info.auth_info != NULL
657              && g_strcmp0(transfer_path, agent_info.auth_info->transfer_path) == 0) {
658                 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
659                 transfer_info->file_size = agent_info.auth_info->file_size;
660                 transfer_info->type = g_strdup(TRANSFER_PUT);
661                 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
662                 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
663                 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
664                 transfer_info->file_path = agent_info.auth_info->file_path;
665                 transfer_info->address = g_strdup(agent_info.auth_info->address);
666
667         } else {
668                 if (__bt_get_transfer_properties(transfer_info, transfer_path) < 0) {
669                         BT_ERR("Get Properties failed");
670                         __bt_free_transfer_info(transfer_info);
671                         return;
672                 }
673                 agent_info.server_type = BT_FTP_SERVER;
674         }
675
676         if (agent_info.server_type == BT_CUSTOM_SERVER) {
677                 if (agent_info.custom_server == NULL) {
678                         __bt_free_transfer_info(transfer_info);
679                         __bt_free_auth_info(agent_info.auth_info);
680                         agent_info.auth_info = NULL;
681                         return;
682                 }
683
684                 req_info = _bt_get_request_info(agent_info.accept_id);
685                 if (req_info == NULL || req_info->context == NULL) {
686                         BT_ERR("info is NULL");
687                         goto done;
688                 }
689
690                 agent_info.accept_id = 0;
691                 result = BLUETOOTH_ERROR_NONE;
692                 GArray *g_out_param1 = NULL;
693                 g_out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
694                 if (out_param1 == NULL) {
695                         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
696                                         g_out_param1->data, g_out_param1->len,
697                                         TRUE, NULL, NULL);
698                 }
699
700                 g_dbus_method_invocation_return_value(req_info->context,
701                                 g_variant_new("(iv)", result, out_param1));
702                 g_array_free(g_out_param1, TRUE);
703                 _bt_delete_request_list(req_info->req_id);
704         }
705 done:
706         transfers = g_slist_append(transfers, transfer_info);
707
708         BT_DBG("Transfer id %d", transfer_info->transfer_id);
709
710         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
711         for (i = 0; i < 5; i++) {
712                 if (agent_info.auth_info)
713                         g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
714         }
715
716         param = g_variant_new("(isssstii(ay))", result,
717                                 transfer_info->device_name,
718                                 transfer_info->filename,
719                                 transfer_info->type,
720                                 transfer_info->address,
721                                 transfer_info->file_size,
722                                 transfer_info->transfer_id,
723                                 agent_info.server_type,
724                                 builder);
725
726         __bt_free_auth_info(agent_info.auth_info);
727         agent_info.auth_info = NULL;
728
729         g_variant_builder_unref(builder);
730
731         _bt_send_event(BT_OPP_SERVER_EVENT,
732                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
733                 param);
734 }
735
736 void _bt_obex_transfer_progress(const char *transfer_path,
737                                         guint64 transferred)
738 {
739         BT_DBG("+");
740         bt_transfer_info_t *transfer_info;
741         int current_progress = 0;
742         int previous_progress;
743         GVariant *param = NULL;
744         int result = BLUETOOTH_ERROR_NONE;
745
746         transfer_info = __bt_find_transfer_by_path(transfer_path);
747         ret_if(transfer_info == NULL);
748
749         current_progress = (int)(((gdouble)transferred /
750                         (gdouble)transfer_info->file_size) * 100);
751
752         previous_progress = (int)(((gdouble)transfer_info->progress /
753                         (gdouble)transfer_info->file_size) * 100);
754
755         if (current_progress == previous_progress) {
756                 BT_DBG("Same Percentage Value: Do not emit Signal");
757                 return;
758         }
759
760         transfer_info->progress = transferred;
761         param = g_variant_new("(isssstiii)", result,
762                                 transfer_info->filename,
763                                 transfer_info->type,
764                                 transfer_info->device_name,
765                                 transfer_info->address,
766                                 transfer_info->file_size,
767                                 transfer_info->transfer_id,
768                                 current_progress,
769                                 agent_info.server_type);
770         _bt_send_event(BT_OPP_SERVER_EVENT,
771                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS,
772                 param);
773         BT_DBG("-");
774 }
775
776 void _bt_obex_transfer_completed(const char *transfer_path, gboolean success)
777 {
778         bt_transfer_info_t *transfer_info;
779         GVariantBuilder *builder = NULL;
780         GVariant *param = NULL;
781         int result;
782         int i = 0;
783         BT_DBG("Transfer [%s] Success [%d] \n", transfer_path, success);
784
785         result = (success == TRUE) ? BLUETOOTH_ERROR_NONE
786                                 : BLUETOOTH_ERROR_CANCEL;
787
788         transfer_info = __bt_find_transfer_by_path(transfer_path);
789
790         if (transfer_info == NULL) {
791                 BT_DBG("Very small files receiving case, did not get Active status from obexd");
792                 if (agent_info.auth_info == NULL ||
793                                 g_strcmp0(transfer_path,
794                                 agent_info.auth_info->transfer_path) != 0) {
795                         BT_ERR("auth_info is NULL, returning");
796                         return;
797                 }
798
799                 transfer_info = g_new0(bt_transfer_info_t, 1);
800
801                 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
802                 transfer_info->file_size = agent_info.auth_info->file_size;
803                 transfer_info->type = g_strdup(TRANSFER_PUT);
804                 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
805                 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
806                 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
807                 transfer_info->file_path = agent_info.auth_info->file_path;
808                 transfer_info->address = g_strdup(agent_info.auth_info->address);
809
810                 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
811                 for (i = 0; i < 5; i++)
812                         g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
813
814                 param = g_variant_new("(isssstii(ay))", result,
815                                         transfer_info->filename,
816                                         transfer_info->type,
817                                         transfer_info->device_name,
818                                         transfer_info->address,
819                                         transfer_info->file_size,
820                                         transfer_info->transfer_id,
821                                         agent_info.server_type,
822                                         builder);
823                 _bt_send_event(BT_OPP_SERVER_EVENT,
824                         BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
825                         param);
826                 g_variant_builder_unref(builder);
827         }
828         param = g_variant_new("(issssstii)", result,
829                                 transfer_info->filename,
830                                 transfer_info->type,
831                                 transfer_info->device_name,
832                                 transfer_info->file_path,
833                                 transfer_info->address,
834                                 transfer_info->file_size,
835                                 transfer_info->transfer_id,
836                                 agent_info.server_type);
837         _bt_send_event(BT_OPP_SERVER_EVENT,
838                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
839                 param);
840         transfers = g_slist_remove(transfers, transfer_info);
841         __bt_free_transfer_info(transfer_info);
842 }
843
844 void _bt_obex_transfer_connected(const char *obj_path)
845 {
846         BT_DBG("+");
847
848         int result = BLUETOOTH_ERROR_NONE;
849         GVariant *param = NULL;
850         bt_transfer_info_t *transfer_info = NULL;
851
852         transfer_info = g_new0(bt_transfer_info_t, 1);
853         __bt_get_transfer_properties(transfer_info, obj_path);
854         INFO_SECURE("Address[%s] Name[%s] TransferID[%d] ", transfer_info->address,
855                         transfer_info->device_name, transfer_info->transfer_id);
856
857         param = g_variant_new("(issi)", result, transfer_info->address,
858                         transfer_info->device_name, transfer_info->transfer_id);
859
860         _bt_send_event(BT_OPP_SERVER_EVENT,
861                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_CONNECTED,
862                 param);
863
864         __bt_free_transfer_info(transfer_info);
865         BT_DBG("-");
866 }
867
868 void _bt_obex_transfer_disconnected(char * obj_path)
869 {
870         BT_DBG("+");
871
872         int result = BLUETOOTH_ERROR_NONE;
873         GVariant *param = NULL;
874         bt_session_info_t *session = NULL;
875         int transfer_id = -1;
876
877         session = __bt_find_session_by_path(obj_path);
878         ret_if(session == NULL);
879
880         transfer_id = __bt_get_transfer_id(obj_path);
881         DBG_SECURE("transfer_id: [%d]", transfer_id);
882
883         DBG_SECURE("%s", session->address);
884         param = g_variant_new("(isi)", result, session->address, transfer_id);
885         _bt_send_event(BT_OPP_SERVER_EVENT,
886                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_DISCONNECTED,
887                 param);
888         session_list = g_slist_remove(session_list, session);
889         g_free(session->address);
890         g_free(session->path);
891         g_free(session);
892         BT_DBG("-");
893 }
894
895 int _bt_register_obex_server(void)
896 {
897         GDBusConnection *g_conn;
898         GDBusProxy *manager_proxy;
899         GVariant *result = NULL;
900         GError *g_error = NULL;
901
902         /* Get the session bus. */
903         g_conn = _bt_gdbus_get_session_gconn();
904         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
905
906         _bt_obex_agent_new(BT_OBEX_SERVER_AGENT_PATH);
907
908         _bt_obex_setup(BT_OBEX_SERVER_AGENT_PATH);
909
910         _bt_obex_set_authorize_cb(BT_OBEX_SERVER_AGENT_PATH,
911                                         __bt_authorize_cb, NULL);
912
913         manager_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
914                                                 NULL, BT_OBEX_SERVICE,
915                                                 BT_OBEX_PATH,
916                                                 BT_OBEX_MANAGER,
917                                                 NULL, &g_error);
918
919         if (manager_proxy == NULL) {
920                 return BLUETOOTH_ERROR_INTERNAL;
921         }
922
923         result = g_dbus_proxy_call_sync(manager_proxy, "RegisterAgent",
924                                 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
925                                 G_DBUS_CALL_FLAGS_NONE,
926                                 DBUS_TIMEOUT, NULL,
927                                 &g_error);
928
929         if (g_error != NULL) {
930                 BT_ERR("Agent registration failed: %s\n", g_error->message);
931                 g_object_unref(manager_proxy);
932                 g_error_free(g_error);
933                 return BLUETOOTH_ERROR_INTERNAL;
934         }
935
936         if (result)
937                 g_variant_unref(result);
938
939         agent_info.proxy = manager_proxy;
940
941         return BLUETOOTH_ERROR_NONE;
942 }
943
944 int _bt_unregister_obex_server(void)
945 {
946         GVariant *result = NULL;
947         GError *g_error = NULL;
948
949         retv_if(agent_info.proxy == NULL,
950                                 BLUETOOTH_ERROR_INTERNAL);
951
952         result = g_dbus_proxy_call_sync(agent_info.proxy, "UnregisterAgent",
953                                 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
954                                 G_DBUS_CALL_FLAGS_NONE,
955                                 DBUS_TIMEOUT, NULL,
956                                 &g_error);
957         if (g_error != NULL) {
958                 BT_ERR("Agent unregistration failed: %s", g_error->message);
959                 g_error_free(g_error);
960         }
961
962         if (result)
963                 g_variant_unref(result);
964
965         _bt_obex_agent_destroy(BT_OBEX_SERVER_AGENT_PATH);
966         g_object_unref(agent_info.proxy);
967         agent_info.proxy = NULL;
968
969         return BLUETOOTH_ERROR_NONE;
970 }
971
972 gboolean __bt_check_folder_path(const char *dest_path)
973 {
974         DIR *dp;
975
976         retv_if(dest_path == NULL, FALSE);
977
978         dp = opendir(dest_path);
979
980         if (dp == NULL) {
981                 BT_ERR("The directory does not exist");
982                 return FALSE;
983         }
984
985         closedir(dp);
986
987         return TRUE;
988 }
989
990 char *__bt_transfer_folder_path(char *dest_path)
991 {
992         char *dst_path = (char *)g_malloc0(BT_OBEX_PATH_MAX_LENGTH);
993         if (g_str_has_prefix(dest_path, BT_OBEX_PATH_PREFIX)) {
994                 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, BT_OBEX_DEFAULT_PATH"%s", dest_path + strlen(BT_OBEX_PATH_PREFIX));
995         } else {
996                 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, "%s", dest_path);
997         }
998
999         BT_INFO("obex transfed path : %s", dst_path);
1000         return dst_path;
1001 }
1002
1003 int _bt_obex_server_allocate(char *sender, const char *dest_path, int app_pid, gboolean is_native)
1004 {
1005         BT_DBG("+");
1006
1007         char *dst_path;
1008         dst_path = __bt_transfer_folder_path((char *)dest_path);
1009
1010         if (__bt_check_folder_path(dst_path) == FALSE) {
1011                 g_free(dst_path);
1012                 return BLUETOOTH_ERROR_INVALID_PARAM;
1013         }
1014
1015         if (is_native == TRUE) {
1016                 if (agent_info.native_server) {
1017                         BT_ERR("obex native server busy");
1018                         g_free(dst_path);
1019                         return BLUETOOTH_ERROR_DEVICE_BUSY;
1020                 }
1021
1022                 /* Force to change the control to native */
1023                 agent_info.native_server = g_malloc0(sizeof(bt_server_info_t));
1024                 agent_info.native_server->dest_path = g_strdup(dst_path);
1025                 agent_info.native_server->sender = g_strdup(sender);
1026                 agent_info.native_server->app_pid = app_pid;
1027                 agent_info.server_type = BT_NATIVE_SERVER;
1028                 _bt_unregister_osp_server_in_agent(BT_OBEX_SERVER, NULL);
1029         } else {
1030                 if (agent_info.custom_server) {
1031                         BT_ERR("obex custom server busy");
1032                         g_free(dst_path);
1033                         return BLUETOOTH_ERROR_DEVICE_BUSY;
1034                 }
1035
1036                 /* Force to change the control to custom */
1037                 agent_info.custom_server = g_malloc0(sizeof(bt_server_info_t));
1038                 agent_info.custom_server->dest_path = g_strdup(dst_path);
1039                 agent_info.custom_server->sender = g_strdup(sender);
1040                 agent_info.custom_server->app_pid = app_pid;
1041                 agent_info.server_type = BT_CUSTOM_SERVER;
1042                 _bt_register_osp_server_in_agent(BT_OBEX_SERVER, NULL, NULL, -1);
1043         }
1044
1045         g_free(dst_path);
1046         BT_DBG("-");
1047         return BLUETOOTH_ERROR_NONE;
1048 }
1049
1050 int _bt_obex_server_deallocate(int app_pid, gboolean is_native)
1051 {
1052         if (is_native == TRUE) {
1053                 retv_if(agent_info.native_server == NULL,
1054                                 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1055
1056                 retv_if(agent_info.native_server->app_pid != app_pid,
1057                                 BLUETOOTH_ERROR_ACCESS_DENIED);
1058
1059                 __bt_free_server_info(agent_info.native_server);
1060                 agent_info.native_server = NULL;
1061
1062                 /* Change the control to custom */
1063                 if (agent_info.custom_server) {
1064                         agent_info.server_type = BT_CUSTOM_SERVER;
1065                         _bt_register_osp_server_in_agent(BT_OBEX_SERVER,
1066                                                         NULL, NULL, -1);
1067                 }
1068         } else {
1069                 retv_if(agent_info.custom_server == NULL,
1070                                 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1071
1072                 retv_if(agent_info.custom_server->app_pid != app_pid,
1073                                 BLUETOOTH_ERROR_ACCESS_DENIED);
1074
1075                 __bt_free_server_info(agent_info.custom_server);
1076                 agent_info.custom_server = NULL;
1077
1078                 _bt_unregister_osp_server_in_agent(BT_OBEX_SERVER, NULL);
1079
1080                 /* Change the control to native */
1081                 if (agent_info.native_server)
1082                         agent_info.server_type = BT_NATIVE_SERVER;
1083         }
1084
1085         return BLUETOOTH_ERROR_NONE;
1086 }
1087
1088 int _bt_obex_server_accept_authorize(const char *filename, gboolean is_native)
1089 {
1090         char file_path[BT_FILE_PATH_MAX] = { 0 };
1091         bt_server_info_t *server_info;
1092
1093         BT_CHECK_PARAMETER(filename, return);
1094
1095         retv_if(agent_info.auth_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1096
1097         retv_if(agent_info.auth_info->reply_context == NULL,
1098                                 BLUETOOTH_ERROR_INTERNAL);
1099
1100         if (is_native == TRUE)
1101                 server_info = agent_info.native_server;
1102         else
1103                 server_info = agent_info.custom_server;
1104
1105         retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1106
1107         if (server_info->dest_path != NULL)
1108                 snprintf(file_path, sizeof(file_path), "%s/%s",
1109                         server_info->dest_path, filename);
1110         else
1111                 snprintf(file_path, sizeof(file_path), "%s", filename);
1112
1113         g_dbus_method_invocation_return_value(agent_info.auth_info->reply_context,
1114                 g_variant_new("(s)", &file_path));
1115         agent_info.auth_info->reply_context = NULL;
1116         agent_info.auth_info->file_path = g_strdup(file_path);
1117         g_free(agent_info.auth_info->filename);
1118         agent_info.auth_info->filename = g_strdup(filename);
1119
1120         return BLUETOOTH_ERROR_NONE;
1121 }
1122
1123 void _bt_obex_server_reply_accept(void)
1124 {
1125         GVariant *param = NULL;
1126         bt_session_info_t *session_info = NULL;
1127         int result = BLUETOOTH_ERROR_NONE;
1128         param = g_variant_new("(istss)", result,
1129                         agent_info.auth_info->filename,
1130                         agent_info.auth_info->file_size,
1131                         agent_info.auth_info->address,
1132                         agent_info.auth_info->device_name);
1133         BT_INFO("Send Obex Authorize");
1134         _bt_send_event(BT_OPP_SERVER_EVENT, BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
1135
1136         session_info = __bt_find_session_by_path(agent_info.auth_info->transfer_path);
1137
1138         if (NULL == session_info)
1139                 BT_ERR("Couldn't get the session info from the list");
1140         else
1141                 session_info->authorized = TRUE;
1142 }
1143
1144 int _bt_obex_server_reject_authorize(void)
1145 {
1146         GError *g_error;
1147
1148         retv_if(agent_info.auth_info->reply_context == NULL,
1149                                 BLUETOOTH_ERROR_INTERNAL);
1150
1151         g_error = g_error_new(__bt_obex_error_quark(),
1152                         BT_OBEX_AGENT_ERROR_CANCEL,
1153                         "CancelledByUser");
1154
1155         g_dbus_method_invocation_return_gerror(agent_info.auth_info->reply_context,
1156                         g_error);
1157         g_error_free(g_error);
1158
1159         __bt_free_auth_info(agent_info.auth_info);
1160         agent_info.auth_info = NULL;
1161
1162         return BLUETOOTH_ERROR_NONE;
1163 }
1164
1165 int _bt_obex_server_set_destination_path(const char *dest_path,
1166                                                 gboolean is_native)
1167 {
1168         bt_server_info_t *server_info;
1169         BT_CHECK_PARAMETER(dest_path, return);
1170
1171         char *dst_path;
1172         dst_path = __bt_transfer_folder_path((char *)dest_path);
1173
1174         DIR *dp = NULL;
1175
1176         dp = opendir(dst_path);
1177
1178         if (dp == NULL) {
1179                 BT_ERR("The directory does not exist");
1180                 g_free(dst_path);
1181                 return BLUETOOTH_ERROR_INVALID_PARAM;
1182         }
1183
1184         closedir(dp);
1185
1186         if (is_native == TRUE)
1187                 server_info = agent_info.native_server;
1188         else
1189                 server_info = agent_info.custom_server;
1190
1191         if (!server_info) {
1192                 BT_ERR("obex server info is NULL");
1193                 g_free(dst_path);
1194                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
1195         }
1196
1197         g_free(server_info->dest_path);
1198         server_info->dest_path = g_strdup(dst_path);
1199
1200         g_free(dst_path);
1201         return BLUETOOTH_ERROR_NONE;
1202 }
1203
1204 int _bt_obex_server_set_root(const char *root)
1205 {
1206         GVariant *result = NULL;
1207         GError *g_error = NULL;
1208         GVariant *folder = NULL;
1209         char *string = "Root";
1210         DIR *dp = NULL;
1211
1212         BT_CHECK_PARAMETER(root, return);
1213
1214         char *dst_root;
1215         dst_root = __bt_transfer_folder_path((char *)root);
1216
1217         if (!agent_info.proxy) {
1218                 BT_ERR("obex agent_info proxy error");
1219                 g_free(dst_root);
1220                 return BLUETOOTH_ERROR_INTERNAL;
1221         }
1222
1223         dp = opendir(dst_root);
1224
1225         if (dp == NULL) {
1226                 BT_ERR("The directory does not exist");
1227                 g_free(dst_root);
1228                 return BLUETOOTH_ERROR_INVALID_PARAM;
1229         }
1230
1231         closedir(dp);
1232
1233         folder = g_variant_new_string(dst_root);
1234         result = g_dbus_proxy_call_sync(agent_info.proxy, "SetProperty",
1235                         g_variant_new("(sv)", string, folder),
1236                         G_DBUS_CALL_FLAGS_NONE,
1237                         DBUS_TIMEOUT, NULL,
1238                         &g_error);
1239
1240         if (g_error) {
1241                 BT_ERR("SetProperty Fail: %s", g_error->message);
1242                 g_error_free(g_error);
1243                 g_free(dst_root);
1244                 return BLUETOOTH_ERROR_INTERNAL;
1245         }
1246
1247         if (result)
1248                 g_variant_unref(result);
1249
1250         g_free(dst_root);
1251         return BLUETOOTH_ERROR_NONE;
1252 }
1253
1254 int _bt_obex_server_cancel_transfer(int transfer_id)
1255 {
1256         bt_transfer_info_t *transfer = NULL;
1257         GDBusProxy *proxy;
1258         GVariant *result = NULL;
1259         GError *err = NULL;
1260         BT_DBG("+");
1261         transfer = __bt_find_transfer_by_id(transfer_id);
1262
1263         retv_if(transfer == NULL, BLUETOOTH_ERROR_NOT_FOUND);
1264         proxy = __bt_get_transfer_proxy(transfer->path);
1265
1266         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1267
1268         result = g_dbus_proxy_call_sync(proxy, "Cancel", NULL,
1269                 G_DBUS_CALL_FLAGS_NONE,
1270                 DBUS_TIMEOUT, NULL, &err);
1271         if (err) {
1272                 BT_ERR("Dbus Err: %s", err->message);
1273                 g_clear_error(&err);
1274         }
1275
1276         g_object_unref(proxy);
1277
1278         if (result)
1279                 g_variant_unref(result);
1280
1281         return BLUETOOTH_ERROR_NONE;
1282 }
1283
1284 int _bt_obex_server_cancel_all_transfers(void)
1285 {
1286         GSList *l;
1287         bt_transfer_info_t *transfer;
1288
1289         for (l = transfers; l != NULL; l = l->next) {
1290                 transfer = l->data;
1291
1292                 if (transfer == NULL)
1293                         continue;
1294
1295                 _bt_obex_server_cancel_transfer(transfer->transfer_id);
1296         }
1297
1298         return BLUETOOTH_ERROR_NONE;
1299 }
1300
1301 int _bt_obex_server_is_activated(gboolean *activated)
1302 {
1303         BT_CHECK_PARAMETER(activated, return);
1304
1305         if (agent_info.custom_server) {
1306                 *activated = TRUE;
1307         } else {
1308                 *activated = FALSE;
1309         }
1310
1311         return BLUETOOTH_ERROR_NONE;
1312 }
1313
1314 int _bt_obex_server_check_allocation(gboolean *allocation)
1315 {
1316         BT_CHECK_PARAMETER(allocation, return);
1317
1318         if (agent_info.native_server || agent_info.custom_server) {
1319                 *allocation = TRUE;
1320         } else {
1321                 *allocation = FALSE;
1322         }
1323
1324         return BLUETOOTH_ERROR_NONE;
1325 }
1326
1327 int _bt_obex_server_check_termination(char *sender)
1328 {
1329         BT_CHECK_PARAMETER(sender, return);
1330
1331         if (agent_info.native_server) {
1332                 if (g_strcmp0(sender, agent_info.native_server->sender) == 0) {
1333                         _bt_obex_server_deallocate(agent_info.native_server->app_pid,
1334                                                 TRUE);
1335                 }
1336         }
1337
1338         if (agent_info.custom_server) {
1339                 if (g_strcmp0(sender, agent_info.custom_server->sender) == 0) {
1340                         _bt_obex_server_deallocate(agent_info.custom_server->app_pid,
1341                                                 FALSE);
1342                 }
1343         }
1344
1345         return BLUETOOTH_ERROR_NONE;
1346 }
1347
1348 int _bt_obex_server_is_receiving(gboolean *receiving)
1349 {
1350         BT_CHECK_PARAMETER(receiving, return);
1351
1352         if (transfers == NULL || g_slist_length(transfers) == 0) {
1353                 *receiving = FALSE;
1354         } else {
1355                 *receiving = TRUE;
1356         }
1357
1358         return BLUETOOTH_ERROR_NONE;
1359 }
1360
1361 gboolean __bt_obex_server_accept_timeout_cb(gpointer user_data)
1362 {
1363         request_info_t *req_info;
1364         GVariant *out_param1 = NULL;
1365         int result = BLUETOOTH_ERROR_TIMEOUT;
1366
1367         /* Already reply in _bt_obex_transfer_started */
1368         retv_if(agent_info.accept_id == 0, FALSE);
1369
1370         req_info = _bt_get_request_info(agent_info.accept_id);
1371         if (req_info == NULL || req_info->context == NULL) {
1372                 BT_ERR("info is NULL");
1373                 return FALSE;
1374         }
1375
1376         agent_info.accept_id = 0;
1377         GArray *g_out_param1 = NULL;
1378         g_out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1379         if (out_param1 == NULL) {
1380                 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1381                                 g_out_param1->data, g_out_param1->len,
1382                                 TRUE, NULL, NULL);
1383         }
1384
1385         g_dbus_method_invocation_return_value(req_info->context,
1386                         g_variant_new("(iv)", result, out_param1));
1387         g_array_free(g_out_param1, TRUE);
1388         _bt_delete_request_list(req_info->req_id);
1389
1390         return FALSE;
1391 }
1392
1393 /* To support the BOT  */
1394 int _bt_obex_server_accept_connection(int request_id)
1395 {
1396         if (!_bt_agent_reply_authorize(TRUE))
1397                 return BLUETOOTH_ERROR_INTERNAL;
1398
1399         agent_info.accept_id = request_id;
1400
1401         g_timeout_add(BT_SERVER_ACCEPT_TIMEOUT,
1402                         (GSourceFunc)__bt_obex_server_accept_timeout_cb,
1403                         NULL);
1404
1405         return BLUETOOTH_ERROR_NONE;
1406 }
1407
1408 /* To support the BOT  */
1409 int _bt_obex_server_reject_connection(void)
1410 {
1411         if (!_bt_agent_reply_authorize(FALSE))
1412                 return BLUETOOTH_ERROR_INTERNAL;
1413
1414         return BLUETOOTH_ERROR_NONE;
1415 }
1416
1417 int _bt_opp_get_server_progress(int transfer_id, guint8 *progress)
1418 {
1419         bt_transfer_info_t *requested_transfer = NULL;
1420         requested_transfer = __bt_find_transfer_by_id(transfer_id);
1421         if (requested_transfer == NULL) {
1422                 BT_ERR("No Matching Inbound transfer");
1423                 return BLUETOOTH_ERROR_NOT_FOUND;
1424         }
1425
1426         *progress = (int)(((double)requested_transfer->progress /
1427                         requested_transfer->file_size) * 100);
1428
1429         BT_DBG("Percentage: %d", *progress);
1430         return BLUETOOTH_ERROR_NONE;
1431 }