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