56c6eabb935faa6b944ccc8939f9d06ba3c79785
[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, "LegacyManufacturerDataLen",
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, "LegacyManufacturerData",
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
1167                 __bt_free_server_info(agent_info.custom_server);
1168                 agent_info.custom_server = NULL;
1169
1170                 /* Change the control to native */
1171                 if (agent_info.native_server &&
1172                                 agent_info.server_type != BT_NATIVE_SERVER) {
1173
1174                         agent_info.server_type = BT_NATIVE_SERVER;
1175
1176                         if (OAL_STATUS_SUCCESS !=
1177                                 device_set_osp_server(OAL_OSP_SERVER_OBEX, FALSE))
1178                                 BT_ERR("device_set_osp_server failed");
1179                 }
1180         }
1181
1182         return BLUETOOTH_ERROR_NONE;
1183 }
1184
1185 int _bt_obex_server_accept_authorize(const char *filename, gboolean is_native)
1186 {
1187         char file_path[BT_FILE_PATH_MAX] = { 0 };
1188         bt_server_info_t *server_info;
1189
1190         BT_CHECK_PARAMETER(filename, return);
1191
1192         retv_if(agent_info.auth_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1193
1194         retv_if(agent_info.auth_info->reply_context == NULL,
1195                                 BLUETOOTH_ERROR_INTERNAL);
1196
1197         if (is_native == TRUE)
1198                 server_info = agent_info.native_server;
1199         else
1200                 server_info = agent_info.custom_server;
1201
1202         retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1203
1204         if (server_info->dest_path != NULL)
1205                 snprintf(file_path, sizeof(file_path), "%s/%s",
1206                         server_info->dest_path, filename);
1207         else
1208                 snprintf(file_path, sizeof(file_path), "%s", filename);
1209
1210         g_dbus_method_invocation_return_value(agent_info.auth_info->reply_context,
1211                 g_variant_new("(s)", &file_path));
1212         agent_info.auth_info->reply_context = NULL;
1213         agent_info.auth_info->file_path = g_strdup(file_path);
1214         g_free(agent_info.auth_info->filename);
1215         agent_info.auth_info->filename = g_strdup(filename);
1216
1217         return BLUETOOTH_ERROR_NONE;
1218 }
1219
1220 void _bt_obex_server_reply_accept(void)
1221 {
1222         GVariant *param = NULL;
1223         bt_session_info_t *session_info = NULL;
1224         int result = BLUETOOTH_ERROR_NONE;
1225         param = g_variant_new("(istss)", result,
1226                         agent_info.auth_info->filename,
1227                         agent_info.auth_info->file_size,
1228                         agent_info.auth_info->address,
1229                         agent_info.auth_info->device_name);
1230         BT_INFO("Send Obex Authorize");
1231         _bt_send_event(BT_OPP_SERVER_EVENT, BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
1232
1233         session_info = __bt_find_session_by_path(agent_info.auth_info->transfer_path);
1234
1235         if (NULL == session_info)
1236                 BT_ERR("Couldn't get the session info from the list");
1237         else
1238                 session_info->authorized = TRUE;
1239 }
1240
1241 int _bt_obex_server_reject_authorize(void)
1242 {
1243         GError *g_error;
1244
1245         retv_if(agent_info.auth_info->reply_context == NULL,
1246                                 BLUETOOTH_ERROR_INTERNAL);
1247
1248         g_error = g_error_new(__bt_obex_error_quark(),
1249                         BT_OBEX_AGENT_ERROR_CANCEL,
1250                         "CancelledByUser");
1251
1252         g_dbus_method_invocation_return_gerror(agent_info.auth_info->reply_context,
1253                         g_error);
1254         g_error_free(g_error);
1255
1256         __bt_free_auth_info(agent_info.auth_info);
1257         agent_info.auth_info = NULL;
1258
1259         return BLUETOOTH_ERROR_NONE;
1260 }
1261
1262 int _bt_obex_server_set_destination_path(const char *dest_path,
1263                                                 gboolean is_native)
1264 {
1265         bt_server_info_t *server_info;
1266         BT_CHECK_PARAMETER(dest_path, return);
1267
1268         char *dst_path;
1269         dst_path = __bt_transfer_folder_path((char *)dest_path);
1270
1271         DIR *dp = NULL;
1272
1273         dp = opendir(dst_path);
1274
1275         if (dp == NULL) {
1276                 BT_ERR("The directory does not exist");
1277                 g_free(dst_path);
1278                 return BLUETOOTH_ERROR_INVALID_PARAM;
1279         }
1280
1281         closedir(dp);
1282
1283         if (is_native == TRUE)
1284                 server_info = agent_info.native_server;
1285         else
1286                 server_info = agent_info.custom_server;
1287
1288         if (!server_info) {
1289                 BT_ERR("obex server info is NULL");
1290                 g_free(dst_path);
1291                 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
1292         }
1293
1294         g_free(server_info->dest_path);
1295         server_info->dest_path = g_strdup(dst_path);
1296
1297         g_free(dst_path);
1298         return BLUETOOTH_ERROR_NONE;
1299 }
1300
1301 int _bt_obex_server_set_root(const char *root)
1302 {
1303         GVariant *result = NULL;
1304         GError *g_error = NULL;
1305         GVariant *folder = NULL;
1306         char *string = "Root";
1307         DIR *dp = NULL;
1308
1309         BT_CHECK_PARAMETER(root, return);
1310
1311         char *dst_root;
1312         dst_root = __bt_transfer_folder_path((char *)root);
1313
1314         if (!agent_info.proxy) {
1315                 BT_ERR("obex agent_info proxy error");
1316                 g_free(dst_root);
1317                 return BLUETOOTH_ERROR_INTERNAL;
1318         }
1319
1320         dp = opendir(dst_root);
1321
1322         if (dp == NULL) {
1323                 BT_ERR("The directory does not exist");
1324                 g_free(dst_root);
1325                 return BLUETOOTH_ERROR_INVALID_PARAM;
1326         }
1327
1328         closedir(dp);
1329
1330         folder = g_variant_new_string(dst_root);
1331         result = g_dbus_proxy_call_sync(agent_info.proxy, "SetProperty",
1332                         g_variant_new("(sv)", string, folder),
1333                         G_DBUS_CALL_FLAGS_NONE,
1334                         DBUS_TIMEOUT, NULL,
1335                         &g_error);
1336
1337         if (g_error) {
1338                 BT_ERR("SetProperty Fail: %s", g_error->message);
1339                 g_error_free(g_error);
1340                 g_free(dst_root);
1341                 return BLUETOOTH_ERROR_INTERNAL;
1342         }
1343
1344         if (result)
1345                 g_variant_unref(result);
1346
1347         g_free(dst_root);
1348         return BLUETOOTH_ERROR_NONE;
1349 }
1350
1351 int _bt_obex_server_cancel_transfer(int transfer_id)
1352 {
1353         bt_transfer_info_t *transfer = NULL;
1354         GDBusProxy *proxy;
1355         GVariant *result = NULL;
1356         GError *err = NULL;
1357         BT_DBG("+");
1358         transfer = __bt_find_transfer_by_id(transfer_id);
1359
1360         retv_if(transfer == NULL, BLUETOOTH_ERROR_NOT_FOUND);
1361         proxy = __bt_get_transfer_proxy(transfer->path);
1362
1363         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1364
1365         result = g_dbus_proxy_call_sync(proxy, "Cancel", NULL,
1366                 G_DBUS_CALL_FLAGS_NONE,
1367                 DBUS_TIMEOUT, NULL, &err);
1368         if (err) {
1369                 BT_ERR("Dbus Err: %s", err->message);
1370                 g_clear_error(&err);
1371         }
1372
1373         g_object_unref(proxy);
1374
1375         if (result)
1376                 g_variant_unref(result);
1377
1378         return BLUETOOTH_ERROR_NONE;
1379 }
1380
1381 int _bt_obex_server_cancel_all_transfers(void)
1382 {
1383         GSList *l;
1384         bt_transfer_info_t *transfer;
1385
1386         for (l = transfers; l != NULL; l = l->next) {
1387                 transfer = l->data;
1388
1389                 if (transfer == NULL)
1390                         continue;
1391
1392                 _bt_obex_server_cancel_transfer(transfer->transfer_id);
1393         }
1394
1395         return BLUETOOTH_ERROR_NONE;
1396 }
1397
1398 int _bt_obex_server_is_activated(gboolean *activated)
1399 {
1400         BT_CHECK_PARAMETER(activated, return);
1401
1402         if (agent_info.custom_server)
1403                 *activated = TRUE;
1404         else
1405                 *activated = FALSE;
1406
1407         return BLUETOOTH_ERROR_NONE;
1408 }
1409
1410 int _bt_obex_server_check_allocation(gboolean *allocation)
1411 {
1412         BT_CHECK_PARAMETER(allocation, return);
1413
1414         if (agent_info.native_server || agent_info.custom_server)
1415                 *allocation = TRUE;
1416         else
1417                 *allocation = FALSE;
1418
1419         return BLUETOOTH_ERROR_NONE;
1420 }
1421
1422 int _bt_obex_server_check_termination(char *sender)
1423 {
1424         BT_CHECK_PARAMETER(sender, return);
1425
1426         if (agent_info.native_server) {
1427                 if (g_strcmp0(sender, agent_info.native_server->sender) == 0) {
1428                         _bt_obex_server_deallocate(agent_info.native_server->app_pid,
1429                                                 TRUE);
1430                 }
1431         }
1432
1433         if (agent_info.custom_server) {
1434                 if (g_strcmp0(sender, agent_info.custom_server->sender) == 0) {
1435                         _bt_obex_server_deallocate(agent_info.custom_server->app_pid,
1436                                                 FALSE);
1437                 }
1438         }
1439
1440         return BLUETOOTH_ERROR_NONE;
1441 }
1442
1443 int _bt_obex_server_is_receiving(gboolean *receiving)
1444 {
1445         BT_CHECK_PARAMETER(receiving, return);
1446
1447         if (transfers == NULL || g_slist_length(transfers) == 0)
1448                 *receiving = FALSE;
1449         else
1450                 *receiving = TRUE;
1451
1452         return BLUETOOTH_ERROR_NONE;
1453 }
1454
1455 void _bt_obex_server_set_pending_conn_auth_device_addr(const char *address)
1456 {
1457         if (pending_auth_address)
1458                 g_free(pending_auth_address);
1459
1460         pending_auth_address = g_strdup(address);
1461 }
1462
1463 /* To support the BOT  */
1464 int _bt_obex_server_accept_connection(int request_id)
1465 {
1466         int res;
1467         bt_address_t bd_addr;
1468
1469         BT_INFO("address: %s", pending_auth_address);
1470         _bt_convert_addr_string_to_type(bd_addr.addr, pending_auth_address);
1471         res = device_reply_auth_request(&bd_addr, OPP_SERVICE_ID, TRUE, FALSE);
1472         g_free(pending_auth_address);
1473         pending_auth_address = NULL;
1474         if (res != OAL_STATUS_SUCCESS) {
1475                 BT_ERR("device_reply_auth_request failed");
1476                 return BLUETOOTH_ERROR_INTERNAL;
1477         }
1478
1479         agent_info.accept_id = request_id;
1480
1481         return BLUETOOTH_ERROR_NONE;
1482 }
1483
1484 /* To support the BOT  */
1485 int _bt_obex_server_reject_connection(void)
1486 {
1487         int res;
1488         bt_address_t bd_addr;
1489
1490         BT_INFO("address: %s", pending_auth_address);
1491         _bt_convert_addr_string_to_type(bd_addr.addr, pending_auth_address);
1492         res = device_reply_auth_request(&bd_addr, OPP_SERVICE_ID, FALSE, FALSE);
1493         g_free(pending_auth_address);
1494         pending_auth_address = NULL;
1495         if (res != OAL_STATUS_SUCCESS) {
1496                 BT_ERR("device_reply_auth_request failed");
1497                 return BLUETOOTH_ERROR_INTERNAL;
1498         }
1499
1500         return BLUETOOTH_ERROR_NONE;
1501 }
1502
1503 int _bt_opp_get_server_progress(int transfer_id, guint8 *progress)
1504 {
1505         bt_transfer_info_t *requested_transfer = NULL;
1506         requested_transfer = __bt_find_transfer_by_id(transfer_id);
1507         if (requested_transfer == NULL) {
1508                 BT_ERR("No Matching Inbound transfer");
1509                 return BLUETOOTH_ERROR_NOT_FOUND;
1510         }
1511
1512         *progress = (int)(((double)requested_transfer->progress /
1513                         requested_transfer->file_size) * 100);
1514
1515         BT_DBG("Percentage: %d", *progress);
1516         return BLUETOOTH_ERROR_NONE;
1517 }
1518
1519 gboolean _bt_obex_server_is_custom(void)
1520 {
1521         return (agent_info.server_type == BT_CUSTOM_SERVER) ? TRUE : FALSE;
1522 }
1523