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