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