bt-api: Fix incorrect macro expansion for service function
[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_FEATURE_BT_DPM
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 #ifdef TIZEN_FEATURE_BT_DPM
539         int value = DPM_BT_ERROR;
540 #endif
541
542         BT_DBG(" path [%s] \n", path);
543
544         transfer_properties_proxy = __bt_get_transfer_properties_proxy(path);
545
546         retv_if(transfer_properties_proxy == NULL, FALSE);
547
548         ret = g_dbus_proxy_call_sync(transfer_properties_proxy, "GetAll",
549                         g_variant_new("(s)", BT_OBEX_TRANSFER_INTERFACE),
550                         G_DBUS_CALL_FLAGS_NONE,
551                         DBUS_TIMEOUT, NULL,
552                         &err);
553         if (err) {
554                 BT_ERR("DBus Error : %s", err->message);
555                 g_clear_error(&err);
556                 return FALSE;
557         }
558         if (ret == NULL) {
559                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
560                 return FALSE;
561         }
562         g_variant_get(ret, "(a{sv})", &iter);
563         g_variant_unref(ret);
564         if (iter == NULL) {
565                 g_object_unref(transfer_properties_proxy);
566                 return FALSE;
567         }
568
569         __bt_free_auth_info(agent_info.auth_info);
570
571         agent_info.auth_info = g_malloc(sizeof(bt_auth_info_t));
572
573         memset(agent_info.auth_info, 0, sizeof(bt_auth_info_t));
574
575         agent_info.auth_info->reply_context = context;
576
577         agent_info.auth_info->transfer_path = g_strdup(path);
578
579 #ifdef TIZEN_FEATURE_BT_DPM
580         _bt_dpm_get_allow_bluetooth_mode(&value);
581         if (value == DPM_BT_HANDSFREE_ONLY) {
582                 /* Free auth info in next function */
583                 _bt_obex_server_reject_authorize();
584                 return FALSE;
585         }
586 #endif
587         if (iter) {
588                 const gchar *key;
589                 GVariant *val;
590                 gsize len = 0;
591                 while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
592                         if (g_strcmp0(key, "Name") == 0)
593                                 agent_info.auth_info->filename = g_variant_dup_string(val, &len);
594                         else if (g_strcmp0(key, "Address") == 0)
595                                 bdaddress = g_variant_dup_string(val, &len);
596                         else if (g_strcmp0(key, "Size") == 0)
597                                 agent_info.auth_info->file_size = g_variant_get_uint64(val);
598                 }
599                 g_variant_iter_free(iter);
600         }
601
602         __bt_get_remote_device_name_authinfo(bdaddress, &device_name, auth_info);
603
604         if (!device_name)
605                 device_name = g_strdup(bdaddress);
606
607         agent_info.auth_info->address = g_strdup(bdaddress);
608         agent_info.auth_info->device_name = device_name;
609         memcpy(agent_info.auth_info->contact_auth_info, auth_info, 5);
610
611         session_info = __bt_find_session_by_path((char *)path);
612         if (NULL == session_info) {
613                 session_info = g_malloc0(sizeof(bt_session_info_t));
614                 session_info->path = g_strdup(path);
615                 session_info->address = g_strdup(bdaddress);
616                 session_info->authorized = FALSE;
617                 session_list = g_slist_append(session_list, session_info);
618         }
619
620         g_object_unref(transfer_properties_proxy);
621         g_free(bdaddress);
622
623         if (agent_info.server_type == BT_CUSTOM_SERVER) {
624                 /* No need to send the event */
625                 _bt_obex_server_accept_authorize(agent_info.auth_info->filename, FALSE);
626                 return TRUE;
627         }
628
629         if (session_info->authorized == FALSE) {
630                 _bt_launch_system_popup(BT_AGENT_EVENT_EXCHANGE_REQUEST, device_name,
631                                         auth_info, NULL, NULL, BT_OBEX_SERVER_AGENT_PATH);
632         } else {
633                 param = g_variant_new("(istss)", result,
634                         agent_info.auth_info->filename,
635                         agent_info.auth_info->file_size,
636                         agent_info.auth_info->address,
637                         agent_info.auth_info->device_name);
638                 _bt_send_event(BT_OPP_SERVER_EVENT,
639                         BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
640         }
641
642         return TRUE;
643 }
644
645 void _bt_obex_transfer_started(const char *transfer_path)
646 {
647         bt_transfer_info_t *transfer_info;
648         request_info_t *req_info;
649         GVariant *out_param1 = NULL;
650         GVariant *param = NULL;
651         GVariantBuilder *builder = NULL;
652         int result = BLUETOOTH_ERROR_NONE;
653         int i = 0;
654
655         BT_DBG("%s", transfer_path);
656
657         transfer_info = g_malloc0(sizeof(bt_transfer_info_t));
658
659         if (agent_info.auth_info != NULL
660              && g_strcmp0(transfer_path, agent_info.auth_info->transfer_path) == 0) {
661                 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
662                 transfer_info->file_size = agent_info.auth_info->file_size;
663                 transfer_info->type = g_strdup(TRANSFER_PUT);
664                 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
665                 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
666                 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
667                 transfer_info->file_path = agent_info.auth_info->file_path;
668                 transfer_info->address = g_strdup(agent_info.auth_info->address);
669
670         } else {
671                 if (__bt_get_transfer_properties(transfer_info, transfer_path) < 0) {
672                         BT_ERR("Get Properties failed");
673                         __bt_free_transfer_info(transfer_info);
674                         return;
675                 }
676                 agent_info.server_type = BT_FTP_SERVER;
677         }
678
679         if (agent_info.server_type == BT_CUSTOM_SERVER) {
680                 if (agent_info.custom_server == NULL) {
681                         __bt_free_transfer_info(transfer_info);
682                         __bt_free_auth_info(agent_info.auth_info);
683                         agent_info.auth_info = NULL;
684                         return;
685                 }
686
687                 req_info = _bt_get_request_info(agent_info.accept_id);
688                 if (req_info == NULL || req_info->context == NULL) {
689                         BT_ERR("info is NULL");
690                         goto done;
691                 }
692
693                 agent_info.accept_id = 0;
694                 result = BLUETOOTH_ERROR_NONE;
695                 GArray *g_out_param1 = NULL;
696                 g_out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
697                 if (out_param1 == NULL) {
698                         out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
699                                         g_out_param1->data, g_out_param1->len,
700                                         TRUE, NULL, NULL);
701                 }
702
703                 g_dbus_method_invocation_return_value(req_info->context,
704                                 g_variant_new("(iv)", result, out_param1));
705                 g_array_free(g_out_param1, TRUE);
706                 _bt_delete_request_list(req_info->req_id);
707         }
708 done:
709         transfers = g_slist_append(transfers, transfer_info);
710
711         BT_DBG("Transfer id %d", transfer_info->transfer_id);
712
713         builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
714         for (i = 0; i < 5; i++) {
715                 if (agent_info.auth_info)
716                         g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
717         }
718
719         param = g_variant_new("(isssstii(ay))", result,
720                                 transfer_info->device_name,
721                                 transfer_info->filename,
722                                 transfer_info->type,
723                                 transfer_info->address,
724                                 transfer_info->file_size,
725                                 transfer_info->transfer_id,
726                                 agent_info.server_type,
727                                 builder);
728
729         __bt_free_auth_info(agent_info.auth_info);
730         agent_info.auth_info = NULL;
731
732         g_variant_builder_unref(builder);
733
734         _bt_send_event(BT_OPP_SERVER_EVENT,
735                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
736                 param);
737 }
738
739 void _bt_obex_transfer_progress(const char *transfer_path,
740                                         guint64 transferred)
741 {
742         BT_DBG("+");
743         bt_transfer_info_t *transfer_info;
744         int current_progress = 0;
745         int previous_progress;
746         GVariant *param = NULL;
747         int result = BLUETOOTH_ERROR_NONE;
748
749         transfer_info = __bt_find_transfer_by_path(transfer_path);
750         ret_if(transfer_info == NULL);
751
752         current_progress = (int)(((gdouble)transferred /
753                         (gdouble)transfer_info->file_size) * 100);
754
755         previous_progress = (int)(((gdouble)transfer_info->progress /
756                         (gdouble)transfer_info->file_size) * 100);
757
758         if (current_progress == previous_progress) {
759                 BT_DBG("Same Percentage Value: Do not emit Signal");
760                 return;
761         }
762
763         transfer_info->progress = transferred;
764         param = g_variant_new("(isssstiii)", result,
765                                 transfer_info->filename,
766                                 transfer_info->type,
767                                 transfer_info->device_name,
768                                 transfer_info->address,
769                                 transfer_info->file_size,
770                                 transfer_info->transfer_id,
771                                 current_progress,
772                                 agent_info.server_type);
773         _bt_send_event(BT_OPP_SERVER_EVENT,
774                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS,
775                 param);
776         BT_DBG("-");
777 }
778
779 void _bt_obex_transfer_completed(const char *transfer_path, gboolean success)
780 {
781         bt_transfer_info_t *transfer_info;
782         GVariantBuilder *builder = NULL;
783         GVariant *param = NULL;
784         int result;
785         int i = 0;
786         BT_DBG("Transfer [%s] Success [%d] \n", transfer_path, success);
787
788         result = (success == TRUE) ? BLUETOOTH_ERROR_NONE
789                                 : BLUETOOTH_ERROR_CANCEL;
790
791         transfer_info = __bt_find_transfer_by_path(transfer_path);
792
793         if (transfer_info == NULL) {
794                 BT_DBG("Very small files receiving case, did not get Active status from obexd");
795                 if (agent_info.auth_info == NULL ||
796                                 g_strcmp0(transfer_path,
797                                 agent_info.auth_info->transfer_path) != 0) {
798                         BT_ERR("auth_info is NULL, returning");
799                         return;
800                 }
801
802                 transfer_info = g_new0(bt_transfer_info_t, 1);
803
804                 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
805                 transfer_info->file_size = agent_info.auth_info->file_size;
806                 transfer_info->type = g_strdup(TRANSFER_PUT);
807                 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
808                 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
809                 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
810                 transfer_info->file_path = agent_info.auth_info->file_path;
811                 transfer_info->address = g_strdup(agent_info.auth_info->address);
812
813                 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
814                 for (i = 0; i < 5; i++)
815                         g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
816
817                 param = g_variant_new("(isssstii(ay))", result,
818                                         transfer_info->device_name,
819                                         transfer_info->filename,
820                                         transfer_info->type,
821                                         transfer_info->address,
822                                         transfer_info->file_size,
823                                         transfer_info->transfer_id,
824                                         agent_info.server_type,
825                                         builder);
826                 _bt_send_event(BT_OPP_SERVER_EVENT,
827                         BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
828                         param);
829                 g_variant_builder_unref(builder);
830         }
831         param = g_variant_new("(issssstii)", result,
832                                 transfer_info->filename,
833                                 transfer_info->type,
834                                 transfer_info->device_name,
835                                 transfer_info->file_path,
836                                 transfer_info->address,
837                                 transfer_info->file_size,
838                                 transfer_info->transfer_id,
839                                 agent_info.server_type);
840         _bt_send_event(BT_OPP_SERVER_EVENT,
841                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
842                 param);
843         transfers = g_slist_remove(transfers, transfer_info);
844         __bt_free_transfer_info(transfer_info);
845 }
846
847 void _bt_obex_transfer_connected(const char *obj_path)
848 {
849         BT_DBG("+");
850
851         int result = BLUETOOTH_ERROR_NONE;
852         GVariant *param = NULL;
853         bt_transfer_info_t *transfer_info = NULL;
854
855         transfer_info = g_new0(bt_transfer_info_t, 1);
856         __bt_get_transfer_properties(transfer_info, obj_path);
857         INFO_SECURE("Address[%s] Name[%s] TransferID[%d] ", transfer_info->address,
858                         transfer_info->device_name, transfer_info->transfer_id);
859
860         param = g_variant_new("(issi)", result, transfer_info->address,
861                         transfer_info->device_name, transfer_info->transfer_id);
862
863         _bt_send_event(BT_OPP_SERVER_EVENT,
864                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_CONNECTED,
865                 param);
866
867         __bt_free_transfer_info(transfer_info);
868         BT_DBG("-");
869 }
870
871 void _bt_obex_transfer_disconnected(char * obj_path)
872 {
873         BT_DBG("+");
874
875         int result = BLUETOOTH_ERROR_NONE;
876         GVariant *param = NULL;
877         bt_session_info_t *session = NULL;
878         int transfer_id = -1;
879
880         session = __bt_find_session_by_path(obj_path);
881         ret_if(session == NULL);
882
883         transfer_id = __bt_get_transfer_id(obj_path);
884         DBG_SECURE("transfer_id: [%d]", transfer_id);
885
886         DBG_SECURE("%s", session->address);
887         param = g_variant_new("(isi)", result, session->address, transfer_id);
888         _bt_send_event(BT_OPP_SERVER_EVENT,
889                 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_DISCONNECTED,
890                 param);
891         session_list = g_slist_remove(session_list, session);
892         g_free(session->address);
893         g_free(session->path);
894         g_free(session);
895         BT_DBG("-");
896 }
897
898 int _bt_register_obex_server(void)
899 {
900         GDBusConnection *g_conn;
901         GDBusProxy *manager_proxy;
902         GVariant *result = NULL;
903         GError *g_error = NULL;
904
905         /* Get the session bus. */
906         g_conn = _bt_gdbus_get_session_gconn();
907         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
908
909         _bt_obex_agent_new(BT_OBEX_SERVER_AGENT_PATH);
910
911         _bt_obex_setup(BT_OBEX_SERVER_AGENT_PATH);
912
913         _bt_obex_set_authorize_cb(BT_OBEX_SERVER_AGENT_PATH,
914                                         __bt_authorize_cb, NULL);
915
916         manager_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
917                                                 NULL, BT_OBEX_SERVICE,
918                                                 BT_OBEX_PATH,
919                                                 BT_OBEX_MANAGER,
920                                                 NULL, &g_error);
921
922         if (manager_proxy == NULL)
923                 return BLUETOOTH_ERROR_INTERNAL;
924
925         result = g_dbus_proxy_call_sync(manager_proxy, "RegisterAgent",
926                                 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
927                                 G_DBUS_CALL_FLAGS_NONE,
928                                 DBUS_TIMEOUT, NULL,
929                                 &g_error);
930
931         if (g_error != NULL) {
932                 BT_ERR("Agent registration failed: %s\n", g_error->message);
933                 g_object_unref(manager_proxy);
934                 g_error_free(g_error);
935                 return BLUETOOTH_ERROR_INTERNAL;
936         }
937
938         if (result)
939                 g_variant_unref(result);
940
941         agent_info.proxy = manager_proxy;
942
943         return BLUETOOTH_ERROR_NONE;
944 }
945
946 int _bt_unregister_obex_server(void)
947 {
948         GVariant *result = NULL;
949         GError *g_error = NULL;
950
951         retv_if(agent_info.proxy == NULL,
952                                 BLUETOOTH_ERROR_INTERNAL);
953
954         result = g_dbus_proxy_call_sync(agent_info.proxy, "UnregisterAgent",
955                                 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
956                                 G_DBUS_CALL_FLAGS_NONE,
957                                 DBUS_TIMEOUT, NULL,
958                                 &g_error);
959         if (g_error != NULL) {
960                 BT_ERR("Agent unregistration failed: %s", g_error->message);
961                 g_error_free(g_error);
962         }
963
964         if (result)
965                 g_variant_unref(result);
966
967         _bt_obex_agent_destroy(BT_OBEX_SERVER_AGENT_PATH);
968         g_object_unref(agent_info.proxy);
969         agent_info.proxy = NULL;
970
971         return BLUETOOTH_ERROR_NONE;
972 }
973
974 gboolean __bt_check_folder_path(const char *dest_path)
975 {
976         DIR *dp;
977
978         retv_if(dest_path == NULL, FALSE);
979
980         dp = opendir(dest_path);
981
982         if (dp == NULL) {
983                 BT_ERR("The directory does not exist");
984                 return FALSE;
985         }
986
987         closedir(dp);
988
989         return TRUE;
990 }
991
992 char *__bt_transfer_folder_path(char *dest_path)
993 {
994         char *dst_path = (char *)g_malloc0(BT_OBEX_PATH_MAX_LENGTH);
995         if (g_str_has_prefix(dest_path, BT_OBEX_PATH_PREFIX))
996                 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, BT_OBEX_DEFAULT_PATH"%s", dest_path + strlen(BT_OBEX_PATH_PREFIX));
997         else
998                 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, "%s", dest_path);
999
1000         BT_INFO("obex transfed path : %s", dst_path);
1001         return dst_path;
1002 }
1003
1004 int _bt_obex_server_allocate(char *sender, const char *dest_path, int app_pid, gboolean is_native)
1005 {
1006         BT_DBG("+");
1007
1008         char *dst_path;
1009         dst_path = __bt_transfer_folder_path((char *)dest_path);
1010
1011         if (__bt_check_folder_path(dst_path) == FALSE) {
1012                 g_free(dst_path);
1013                 return BLUETOOTH_ERROR_INVALID_PARAM;
1014         }
1015
1016         if (is_native == TRUE) {
1017                 if (agent_info.native_server) {
1018                         BT_ERR("obex native server busy");
1019                         g_free(dst_path);
1020                         return BLUETOOTH_ERROR_DEVICE_BUSY;
1021                 }
1022
1023                 /* Force to change the control to native */
1024                 agent_info.native_server = g_malloc0(sizeof(bt_server_info_t));
1025                 agent_info.native_server->dest_path = g_strdup(dst_path);
1026                 agent_info.native_server->sender = g_strdup(sender);
1027                 agent_info.native_server->app_pid = app_pid;
1028                 agent_info.server_type = BT_NATIVE_SERVER;
1029                 _bt_unregister_osp_server_in_agent(BT_OBEX_SERVER, NULL);
1030         } else {
1031                 if (agent_info.custom_server) {
1032                         BT_ERR("obex custom server busy");
1033                         g_free(dst_path);
1034                         return BLUETOOTH_ERROR_DEVICE_BUSY;
1035                 }
1036
1037                 /* Force to change the control to custom */
1038                 agent_info.custom_server = g_malloc0(sizeof(bt_server_info_t));
1039                 agent_info.custom_server->dest_path = g_strdup(dst_path);
1040                 agent_info.custom_server->sender = g_strdup(sender);
1041                 agent_info.custom_server->app_pid = app_pid;
1042                 agent_info.server_type = BT_CUSTOM_SERVER;
1043                 _bt_register_osp_server_in_agent(BT_OBEX_SERVER, NULL, NULL, -1);
1044         }
1045
1046         g_free(dst_path);
1047         BT_DBG("-");
1048         return BLUETOOTH_ERROR_NONE;
1049 }
1050
1051 int _bt_obex_server_deallocate(int app_pid, gboolean is_native)
1052 {
1053         if (is_native == TRUE) {
1054                 retv_if(agent_info.native_server == NULL,
1055                                 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1056
1057                 retv_if(agent_info.native_server->app_pid != app_pid,
1058                                 BLUETOOTH_ERROR_ACCESS_DENIED);
1059
1060                 __bt_free_server_info(agent_info.native_server);
1061                 agent_info.native_server = NULL;
1062
1063                 /* Change the control to custom */
1064                 if (agent_info.custom_server) {
1065                         agent_info.server_type = BT_CUSTOM_SERVER;
1066                         _bt_register_osp_server_in_agent(BT_OBEX_SERVER,
1067                                                         NULL, NULL, -1);
1068                 }
1069         } else {
1070                 retv_if(agent_info.custom_server == NULL,
1071                                 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1072
1073                 retv_if(agent_info.custom_server->app_pid != app_pid,
1074                                 BLUETOOTH_ERROR_ACCESS_DENIED);
1075
1076                 __bt_free_server_info(agent_info.custom_server);
1077                 agent_info.custom_server = NULL;
1078
1079                 _bt_unregister_osp_server_in_agent(BT_OBEX_SERVER, NULL);
1080
1081                 /* Change the control to native */
1082                 if (agent_info.native_server)
1083                         agent_info.server_type = BT_NATIVE_SERVER;
1084         }
1085
1086         return BLUETOOTH_ERROR_NONE;
1087 }
1088
1089 int _bt_obex_server_accept_authorize(const char *filename, gboolean is_native)
1090 {
1091         char file_path[BT_FILE_PATH_MAX] = { 0 };
1092         bt_server_info_t *server_info;
1093
1094         BT_CHECK_PARAMETER(filename, return);
1095
1096         retv_if(agent_info.auth_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1097
1098         retv_if(agent_info.auth_info->reply_context == NULL,
1099                                 BLUETOOTH_ERROR_INTERNAL);
1100
1101         if (is_native == TRUE)
1102                 server_info = agent_info.native_server;
1103         else
1104                 server_info = agent_info.custom_server;
1105
1106         retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1107
1108         if (server_info->dest_path != NULL)
1109                 snprintf(file_path, sizeof(file_path), "%s/%s",
1110                         server_info->dest_path, filename);
1111         else
1112                 snprintf(file_path, sizeof(file_path), "%s", filename);
1113
1114         g_dbus_method_invocation_return_value(agent_info.auth_info->reply_context,
1115                 g_variant_new("(s)", &file_path));
1116         agent_info.auth_info->reply_context = NULL;
1117         agent_info.auth_info->file_path = g_strdup(file_path);
1118         g_free(agent_info.auth_info->filename);
1119         agent_info.auth_info->filename = g_strdup(filename);
1120
1121         return BLUETOOTH_ERROR_NONE;
1122 }
1123
1124 void _bt_obex_server_reply_accept(void)
1125 {
1126         GVariant *param = NULL;
1127         bt_session_info_t *session_info = NULL;
1128         int result = BLUETOOTH_ERROR_NONE;
1129         param = g_variant_new("(istss)", result,
1130                         agent_info.auth_info->filename,
1131                         agent_info.auth_info->file_size,
1132                         agent_info.auth_info->address,
1133                         agent_info.auth_info->device_name);
1134         BT_INFO("Send Obex Authorize");
1135         _bt_send_event(BT_OPP_SERVER_EVENT, BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
1136
1137         session_info = __bt_find_session_by_path(agent_info.auth_info->transfer_path);
1138
1139         if (NULL == session_info)
1140                 BT_ERR("Couldn't get the session info from the list");
1141         else
1142                 session_info->authorized = TRUE;
1143 }
1144
1145 int _bt_obex_server_reject_authorize(void)
1146 {
1147         GError *g_error;
1148
1149         retv_if(agent_info.auth_info->reply_context == NULL,
1150                                 BLUETOOTH_ERROR_INTERNAL);
1151
1152         g_error = g_error_new(__bt_obex_error_quark(),
1153                         BT_OBEX_AGENT_ERROR_CANCEL,
1154                         "CancelledByUser");
1155
1156         g_dbus_method_invocation_return_gerror(agent_info.auth_info->reply_context,
1157                         g_error);
1158         g_error_free(g_error);
1159
1160         __bt_free_auth_info(agent_info.auth_info);
1161         agent_info.auth_info = NULL;
1162
1163         return BLUETOOTH_ERROR_NONE;
1164 }
1165
1166 int _bt_obex_server_set_destination_path(const char *dest_path,
1167                                                 gboolean is_native)
1168 {
1169         bt_server_info_t *server_info;
1170         BT_CHECK_PARAMETER(dest_path, return);
1171
1172         char *dst_path;
1173         dst_path = __bt_transfer_folder_path((char *)dest_path);
1174
1175         DIR *dp = NULL;
1176
1177         dp = opendir(dst_path);
1178
1179         if (dp == NULL) {
1180                 BT_ERR("The directory does not exist");
1181                 g_free(dst_path);
1182                 return BLUETOOTH_ERROR_INVALID_PARAM;
1183         }
1184
1185         closedir(dp);
1186
1187         if (is_native == TRUE)
1188                 server_info = agent_info.native_server;
1189         else
1190                 server_info = agent_info.custom_server;
1191
1192         if (!server_info) {
1193                 BT_ERR("obex server info is NULL");
1194                 g_free(dst_path);
1195                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
1196         }
1197
1198         g_free(server_info->dest_path);
1199         server_info->dest_path = g_strdup(dst_path);
1200
1201         g_free(dst_path);
1202         return BLUETOOTH_ERROR_NONE;
1203 }
1204
1205 int _bt_obex_server_set_root(const char *root)
1206 {
1207         GVariant *result = NULL;
1208         GError *g_error = NULL;
1209         GVariant *folder = NULL;
1210         char *string = "Root";
1211         DIR *dp = NULL;
1212
1213         BT_CHECK_PARAMETER(root, return);
1214
1215         char *dst_root;
1216         dst_root = __bt_transfer_folder_path((char *)root);
1217
1218         if (!agent_info.proxy) {
1219                 BT_ERR("obex agent_info proxy error");
1220                 g_free(dst_root);
1221                 return BLUETOOTH_ERROR_INTERNAL;
1222         }
1223
1224         dp = opendir(dst_root);
1225
1226         if (dp == NULL) {
1227                 BT_ERR("The directory does not exist");
1228                 g_free(dst_root);
1229                 return BLUETOOTH_ERROR_INVALID_PARAM;
1230         }
1231
1232         closedir(dp);
1233
1234         folder = g_variant_new_string(dst_root);
1235         result = g_dbus_proxy_call_sync(agent_info.proxy, "SetProperty",
1236                         g_variant_new("(sv)", string, folder),
1237                         G_DBUS_CALL_FLAGS_NONE,
1238                         DBUS_TIMEOUT, NULL,
1239                         &g_error);
1240
1241         if (g_error) {
1242                 BT_ERR("SetProperty Fail: %s", g_error->message);
1243                 g_error_free(g_error);
1244                 g_free(dst_root);
1245                 return BLUETOOTH_ERROR_INTERNAL;
1246         }
1247
1248         if (result)
1249                 g_variant_unref(result);
1250
1251         g_free(dst_root);
1252         return BLUETOOTH_ERROR_NONE;
1253 }
1254
1255 int _bt_obex_server_cancel_transfer(int transfer_id)
1256 {
1257         bt_transfer_info_t *transfer = NULL;
1258         GDBusProxy *proxy;
1259         GVariant *result = NULL;
1260         GError *err = NULL;
1261         BT_DBG("+");
1262         transfer = __bt_find_transfer_by_id(transfer_id);
1263
1264         retv_if(transfer == NULL, BLUETOOTH_ERROR_NOT_FOUND);
1265         proxy = __bt_get_transfer_proxy(transfer->path);
1266
1267         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1268
1269         result = g_dbus_proxy_call_sync(proxy, "Cancel", NULL,
1270                 G_DBUS_CALL_FLAGS_NONE,
1271                 DBUS_TIMEOUT, NULL, &err);
1272         if (err) {
1273                 BT_ERR("Dbus Err: %s", err->message);
1274                 g_clear_error(&err);
1275         }
1276
1277         g_object_unref(proxy);
1278
1279         if (result)
1280                 g_variant_unref(result);
1281
1282         return BLUETOOTH_ERROR_NONE;
1283 }
1284
1285 int _bt_obex_server_cancel_all_transfers(void)
1286 {
1287         GSList *l;
1288         bt_transfer_info_t *transfer;
1289
1290         for (l = transfers; l != NULL; l = l->next) {
1291                 transfer = l->data;
1292
1293                 if (transfer == NULL)
1294                         continue;
1295
1296                 _bt_obex_server_cancel_transfer(transfer->transfer_id);
1297         }
1298
1299         return BLUETOOTH_ERROR_NONE;
1300 }
1301
1302 int _bt_obex_server_is_activated(gboolean *activated)
1303 {
1304         BT_CHECK_PARAMETER(activated, return);
1305
1306         if (agent_info.custom_server)
1307                 *activated = TRUE;
1308         else
1309                 *activated = FALSE;
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         return BLUETOOTH_ERROR_NONE;
1324 }
1325
1326 int _bt_obex_server_check_termination(char *sender)
1327 {
1328         BT_CHECK_PARAMETER(sender, return);
1329
1330         if (agent_info.native_server) {
1331                 if (g_strcmp0(sender, agent_info.native_server->sender) == 0) {
1332                         _bt_obex_server_deallocate(agent_info.native_server->app_pid,
1333                                                 TRUE);
1334                 }
1335         }
1336
1337         if (agent_info.custom_server) {
1338                 if (g_strcmp0(sender, agent_info.custom_server->sender) == 0) {
1339                         _bt_obex_server_deallocate(agent_info.custom_server->app_pid,
1340                                                 FALSE);
1341                 }
1342         }
1343
1344         return BLUETOOTH_ERROR_NONE;
1345 }
1346
1347 int _bt_obex_server_is_receiving(gboolean *receiving)
1348 {
1349         BT_CHECK_PARAMETER(receiving, return);
1350
1351         if (transfers == NULL || g_slist_length(transfers) == 0)
1352                 *receiving = FALSE;
1353         else
1354                 *receiving = TRUE;
1355
1356         return BLUETOOTH_ERROR_NONE;
1357 }
1358
1359 gboolean __bt_obex_server_accept_timeout_cb(gpointer user_data)
1360 {
1361         request_info_t *req_info;
1362         GVariant *out_param1 = NULL;
1363         int result = BLUETOOTH_ERROR_TIMEOUT;
1364
1365         /* Already reply in _bt_obex_transfer_started */
1366         retv_if(agent_info.accept_id == 0, FALSE);
1367
1368         req_info = _bt_get_request_info(agent_info.accept_id);
1369         if (req_info == NULL || req_info->context == NULL) {
1370                 BT_ERR("info is NULL");
1371                 return FALSE;
1372         }
1373
1374         agent_info.accept_id = 0;
1375         GArray *g_out_param1 = NULL;
1376         g_out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1377         if (out_param1 == NULL) {
1378                 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1379                                 g_out_param1->data, g_out_param1->len,
1380                                 TRUE, NULL, NULL);
1381         }
1382
1383         g_dbus_method_invocation_return_value(req_info->context,
1384                         g_variant_new("(iv)", result, out_param1));
1385         g_array_free(g_out_param1, TRUE);
1386         _bt_delete_request_list(req_info->req_id);
1387
1388         return FALSE;
1389 }
1390
1391 /* To support the BOT  */
1392 int _bt_obex_server_accept_connection(int request_id)
1393 {
1394         if (!_bt_agent_reply_authorize(TRUE))
1395                 return BLUETOOTH_ERROR_INTERNAL;
1396
1397         agent_info.accept_id = request_id;
1398
1399         g_timeout_add(BT_SERVER_ACCEPT_TIMEOUT,
1400                         (GSourceFunc)__bt_obex_server_accept_timeout_cb,
1401                         NULL);
1402
1403         return BLUETOOTH_ERROR_NONE;
1404 }
1405
1406 /* To support the BOT  */
1407 int _bt_obex_server_reject_connection(void)
1408 {
1409         if (!_bt_agent_reply_authorize(FALSE))
1410                 return BLUETOOTH_ERROR_INTERNAL;
1411
1412         return BLUETOOTH_ERROR_NONE;
1413 }
1414
1415 int _bt_opp_get_server_progress(int transfer_id, guint8 *progress)
1416 {
1417         bt_transfer_info_t *requested_transfer = NULL;
1418         requested_transfer = __bt_find_transfer_by_id(transfer_id);
1419         if (requested_transfer == NULL) {
1420                 BT_ERR("No Matching Inbound transfer");
1421                 return BLUETOOTH_ERROR_NOT_FOUND;
1422         }
1423
1424         *progress = (int)(((double)requested_transfer->progress /
1425                         requested_transfer->file_size) * 100);
1426
1427         BT_DBG("Percentage: %d", *progress);
1428         return BLUETOOTH_ERROR_NONE;
1429 }