f8f3af7d9e019fffc3801f987f24a56336f9698d
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-obex-agent.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 <glib.h>
19 #include <dlog.h>
20 #include <string.h>
21 #include <gio/gio.h>
22
23 #include "bluetooth-api.h"
24 #include "bt-service-common.h"
25 #include "bt-service-event.h"
26 #include "bt-service-util.h"
27 #include "bt-service-obex-agent.h"
28 #include "bt-service-obex-server.h"
29 #include "marshal.h"
30
31 static GDBusConnection *conn = NULL;
32 static GDBusConnection *auth_reply_conn = NULL;
33 static GSList *obex_agent_list = NULL;
34
35 typedef struct {
36         gchar *name;
37         gchar *path;
38
39         int openobex_id;
40         int obex_agent_id;
41         int obex_reply_id;
42
43         /* callback data */
44         gpointer authorize_data;
45         gpointer release_data;
46         gpointer request_data;
47         gpointer progress_data;
48         gpointer complete_data;
49         gpointer error_data;
50
51         /* callback function */
52         bt_obex_authorize_cb authorize_cb;
53         bt_obex_release_cb release_cb;
54         bt_obex_request_cb request_cb;
55         bt_obex_progress_cb progress_cb;
56         bt_obex_complete_cb complete_cb;
57         bt_obex_error_cb error_cb;
58 } bt_obex_agent_info;
59
60 static void __new_connection_method(GDBusConnection *connection,
61                                             const gchar *sender,
62                                             const gchar *object_path,
63                                             const gchar *interface_name,
64                                             const gchar *method_name,
65                                             GVariant *parameters,
66                                             GDBusMethodInvocation *invocation,
67                                             gpointer user_data);
68 static const GDBusInterfaceVTable method_table = {
69         __new_connection_method,
70         NULL,
71         NULL,
72 };
73
74 static const gchar obex_service_agent_xml1[] =
75 "<node name='/'>"
76 "  <interface name='org.openobex.Agent'>"
77 "    <method name='Request'>"
78 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
79 "      <arg type='o' name='transfer'/>"
80 "     <arg type='s' name='name' direction='out'/>"
81 "    </method>"
82 "    <method name='Progress'>"
83 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
84 "      <arg type='o' name='transfer'/>"
85 "      <arg type='t' name='transferred'/>"
86 "    </method>"
87 "    <method name='Complete'>"
88 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
89 "      <arg type='o' name='transfer'/>"
90 "   </method>"
91 "    <method name='Release'>"
92 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
93 "    </method>"
94 "    <method name='Error'>"
95 "      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
96 "      <arg type='o' name='transfer'/>"
97 "      <arg type='s' name='message'/>"
98 "    </method>"
99 "    <method name='Authorize'>"
100 "       <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
101 "               <arg type='o' name='objpath'/>"
102 "               <arg type='s' name='bdaddress'/>"
103 "               <arg type='s' name='name'/>"
104 "               <arg type='s' name='type'/>"
105 "               <arg type='i' name='length'/>"
106 "               <arg type='i' name='time'/>"
107 "               <arg type='s' name='filepath' direction='out'/>"
108 "       </method>"
109 "  </interface>"
110 "</node>";
111
112 static const gchar obex_service_agent_xml2[] =
113 "<node name='/'>"
114 "  <interface name='org.bluez.obex.Agent1'>"
115 "    <method name='AuthorizePush'>"
116 "    <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
117 "        <arg type='o' name='objpath'/>"
118 "        <arg type='s' name='filepath' direction='out'/>"
119 "    </method>"
120 "  </interface>"
121 "</node>";
122
123 static const gchar obex_service_reply_xml3[] =
124 "<node name='/'>"
125 "       <interface name='org.bluez.obex.reply'>"
126 "               <method name='ReplyAuthorize'>"
127 "                       <arg type='u' name='accept' direction='in'/>"
128 "               </method>"
129 "       </interface>"
130 "</node>";
131
132 static bt_obex_agent_info *__find_obex_agent_info(char *path)
133 {
134         GSList *l;
135
136         for (l = obex_agent_list; l != NULL; l = l->next) {
137                 bt_obex_agent_info *info = l->data;
138
139                 if (g_strcmp0(info->path, path) == 0)
140                         return info;
141         }
142
143         return NULL;
144 }
145
146
147 static void __new_connection_method(GDBusConnection *connection,
148                                             const gchar *sender,
149                                             const gchar *object_path,
150                                             const gchar *interface_name,
151                                             const gchar *method_name,
152                                             GVariant *parameters,
153                                             GDBusMethodInvocation *invocation,
154                                             gpointer user_data)
155 {
156         BT_DBG("method_name %s", method_name);
157         if (g_strcmp0(method_name, "AuthorizePush") == 0) {
158                 bt_obex_agent_info *info;
159                 char *path = NULL;
160                 info = __find_obex_agent_info((char *)object_path);
161
162                 if (info == NULL)
163                         goto fail;
164
165                 if (info->authorize_cb == NULL)
166                         goto fail;
167
168                 g_variant_get(parameters, "(&o)", &path);
169
170                 info->authorize_cb(invocation, path,
171                                 info->authorize_data);
172
173                 return;
174         } else if (g_strcmp0(method_name, "Authorize") == 0) {
175                 g_dbus_method_invocation_return_value(invocation, NULL);
176         } else if (g_strcmp0(method_name, "Request") == 0) {
177                 char *sender;
178                 bt_obex_agent_info *info;
179                 GDBusProxy *proxy;
180                 char *path = NULL;
181                 char *name = NULL;
182                 GError *err = NULL;
183
184                 info = __find_obex_agent_info((char *)object_path);
185
186                 if (info == NULL)
187                         goto fail;
188
189                 if (conn == NULL)
190                         goto fail;
191
192                 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
193
194                 if (info->name == NULL) {
195                         info->name = sender;
196                 } else {
197                         if (g_strcmp0(sender, info->name) != 0)
198                                 goto fail;
199                 }
200
201                 if (info->request_cb == NULL)
202                         goto fail;
203
204                 g_variant_get(parameters, "(&o&s)", &path, &name);
205                 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
206                                         NULL,
207                                         BT_OBEX_SERVICE_NAME,
208                                         path,
209                                         BT_OBEX_TRANSFER_INTERFACE,
210                                         NULL, &err);
211
212                 if (err) {
213                         BT_ERR("Dbus Err: %s", err->message);
214                         g_clear_error(&err);
215                         goto fail;
216                 }
217
218                 info->request_cb(invocation, proxy, info->request_data);
219                 g_object_unref(proxy);
220                 return;
221
222         } else if (g_strcmp0(method_name, "Progress") == 0) {
223                 BT_DBG("+");
224
225                 bt_obex_agent_info *info;
226                 char *sender;
227                 char *path = NULL;
228                 gint64 transferred;
229                 GDBusProxy *proxy;
230                 GError *err = NULL;
231
232                 info = __find_obex_agent_info((char *)object_path);
233
234                 if (info == NULL)
235                         goto fail;
236
237                 if (conn == NULL)
238                         goto fail;
239
240                 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
241
242                 if (g_strcmp0(sender, info->name) != 0)
243                         goto fail;
244
245                 if (info->progress_cb == NULL)
246                         goto fail;
247
248                 g_variant_get(parameters, "(&ot)", &path, &transferred);
249                 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
250                                         NULL,
251                                         BT_OBEX_SERVICE_NAME,
252                                         path,
253                                         BT_OBEX_TRANSFER_INTERFACE,
254                                         NULL, &err);
255
256                 if (err) {
257                         BT_ERR("Dbus Err: %s", err->message);
258                         g_clear_error(&err);
259                         goto fail;
260                 }
261
262                 info->progress_cb(invocation, proxy, transferred, info->progress_data);
263
264                 g_object_unref(proxy);
265
266                 BT_DBG("-");
267
268                 return;
269         } else if (g_strcmp0(method_name, "Error") == 0) {
270                 bt_obex_agent_info *info;
271                 char *sender;
272                 GDBusProxy *proxy;
273                 char *path, *message;
274                 GError *err = NULL;
275
276                 info = __find_obex_agent_info((char *)object_path);
277
278                 if (info == NULL)
279                         goto fail;
280
281                 if (conn == NULL)
282                         goto fail;
283
284                 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
285
286                 if (g_strcmp0(sender, info->name) != 0)
287                         goto fail;
288
289                 if (info->error_cb == NULL)
290                         goto fail;
291                 g_variant_get(parameters, "(&o&s)", &path, &message);
292                 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
293                                         NULL,
294                                         BT_OBEX_SERVICE_NAME,
295                                         path,
296                                         BT_OBEX_TRANSFER_INTERFACE,
297                                         NULL, &err);
298                 if (err) {
299                         BT_ERR("Dbus Err: %s", err->message);
300                         g_clear_error(&err);
301                         goto fail;
302                 }
303                 info->error_cb(invocation, proxy, message, info->progress_data);
304
305                 g_object_unref(proxy);
306
307                 return;
308         } else if (g_strcmp0(method_name, "Complete") == 0) {
309                 bt_obex_agent_info *info;
310                 char *sender;
311                 GDBusProxy *proxy;
312                 char *path = NULL;
313                 GError *err = NULL;
314
315                 info = __find_obex_agent_info((char *)object_path);
316
317                 if (info == NULL)
318                         goto fail;
319
320                 if (conn == NULL)
321                         goto fail;
322
323                 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
324
325                 if (g_strcmp0(sender, info->name) != 0)
326                         goto fail;
327
328                 if (info->complete_cb == NULL)
329                         goto fail;
330
331                 g_variant_get(parameters, "(&o)", &path);
332                 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
333                                         NULL,
334                                         BT_OBEX_SERVICE_NAME,
335                                         path,
336                                         BT_OBEX_TRANSFER_INTERFACE,
337                                         NULL, &err);
338                 if (err) {
339                         BT_ERR("Dbus Err: %s", err->message);
340                         g_clear_error(&err);
341                         goto fail;
342                 }
343
344                 info->complete_cb(invocation, proxy, info->complete_data);
345
346                 g_object_unref(proxy);
347
348                 return;
349         } else if (g_strcmp0(method_name, "Release") == 0) {
350                 bt_obex_agent_info *info;
351                 char *sender;
352
353                 info = __find_obex_agent_info((char *)object_path);
354
355                 if (info == NULL)
356                         goto fail;
357
358                 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
359
360                 if (info->name) {
361                         /*In H2 if user denies auth,release will come without request and hence
362                         info->name will be NULL */
363                         if (g_strcmp0(sender, info->name) != 0)
364                                 goto fail;
365                 }
366
367                 if (info->release_cb == NULL)
368                         goto fail;
369
370                 info->release_cb(invocation, info->release_data);
371
372                 return;
373         } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
374                 const guint accept;
375
376                 g_variant_get(parameters, "(u)", &accept);
377                 BT_DBG("Accept: %d", accept);
378
379                 if (accept == 0) {
380                         BT_DBG("Transfer accepted");
381                         _bt_obex_server_reply_accept();
382                 } else {
383                         BT_ERR("Transfer denied");
384                         _bt_obex_server_reject_authorize();
385                 }
386         }
387 fail:
388                 BT_ERR("Fail case");
389                 g_dbus_method_invocation_return_value(invocation, NULL);
390 }
391
392 void _bt_obex_agent_new(char *path)
393 {
394         bt_obex_agent_info *info = NULL;
395         GError *error = NULL;
396
397         if (conn == NULL) {
398                 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
399                 if (error != NULL) {
400                         BT_ERR("Fail to get dbus: %s", error->message);
401                         g_error_free(error);
402                         return;
403                 }
404                 auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
405                 if (error != NULL) {
406                         BT_ERR("Fail to get dbus: %s", error->message);
407                         g_error_free(error);
408                         return;
409                 }
410         }
411         info = (bt_obex_agent_info *)malloc(sizeof(bt_obex_agent_info));
412         if (info) {
413                 memset(info, 0, sizeof(bt_obex_agent_info));
414                 info->path = g_strdup(path);
415                 obex_agent_list = g_slist_append(obex_agent_list, info);
416         }
417 }
418
419 void _bt_obex_agent_destroy(char *path)
420 {
421         bt_obex_agent_info *info = NULL;
422         info = __find_obex_agent_info(path);
423         if (info == NULL) {
424                 BT_ERR("obex agent info not found on path %s", path);
425                 return;
426         }
427         obex_agent_list = g_slist_remove(obex_agent_list, info);
428         if (info->path)
429                 g_free(info->path);
430         if (info->name)
431                 g_free(info->name);
432         if (info->openobex_id)
433                 g_dbus_connection_unregister_object(conn,
434                         info->openobex_id);
435         if (info->obex_agent_id)
436                 g_dbus_connection_unregister_object(conn,
437                         info->obex_agent_id);
438         if (info->obex_reply_id)
439                 g_dbus_connection_unregister_object(auth_reply_conn,
440                                         info->obex_reply_id);
441
442         g_free(info);
443 }
444 gboolean _bt_obex_setup(const char *path)
445 {
446         bt_obex_agent_info *info;
447         GDBusProxy *proxy;
448         GDBusNodeInfo *new_conn_node = NULL;
449         GDBusNodeInfo *auth_reply_node = NULL;
450         GError *err = NULL;
451
452         info = __find_obex_agent_info((char *)path);
453
454         retv_if(info == NULL, FALSE);
455
456         proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
457                                 G_DBUS_PROXY_FLAGS_NONE,
458                                 NULL,
459                                 BT_OBEX_SERVICE_NAME,
460                                 BT_OBEX_CLIENT_PATH,
461                                 BT_OBEX_AGENT_INTERFACE,
462                                 NULL,
463                                 &err);
464
465         g_free(info->name);
466
467         if (proxy != NULL) {
468                 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
469                 g_object_unref(proxy);
470         } else {
471                 info->name = NULL;
472         }
473
474         new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
475         if (new_conn_node == NULL)
476                 return FALSE;
477
478         info->openobex_id = g_dbus_connection_register_object(conn, info->path,
479                                                 new_conn_node->interfaces[0],
480                                                 &method_table,
481                                                 NULL, NULL, &err);
482         g_dbus_node_info_unref(new_conn_node);
483         if (err) {
484                 BT_ERR("Dbus Err: %s", err->message);
485                 g_clear_error(&err);
486                 return FALSE;
487         }
488         if (info->openobex_id == 0)
489                 BT_ERR("Error while registering object");
490
491         new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
492         if (new_conn_node == NULL)
493                 return FALSE;
494
495         info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
496                                                 new_conn_node->interfaces[0],
497                                                 &method_table,
498                                                 NULL, NULL, &err);
499         g_dbus_node_info_unref(new_conn_node);
500         if (info->obex_agent_id == 0)
501                 BT_ERR("Error while registering object");
502         if (err) {
503                 BT_ERR("Dbus Err: %s", err->message);
504                 g_clear_error(&err);
505                 return FALSE;
506         }
507
508         auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
509
510         if (auth_reply_node == NULL) {
511                 BT_ERR("Fail to create the node info for xml");
512                 return FALSE;
513         }
514
515         info->obex_reply_id = g_dbus_connection_register_object(auth_reply_conn, info->path,
516                 auth_reply_node->interfaces[0], &method_table, NULL, NULL, &err);
517         g_dbus_node_info_unref(auth_reply_node);
518         if (info->obex_reply_id == 0)
519                 BT_ERR("Error while registering object");
520         if (err) {
521                 BT_ERR("Dbus Err: %s", err->message);
522                 g_clear_error(&err);
523                 return FALSE;
524         }
525         return TRUE;
526 }
527
528 void _bt_obex_set_authorize_cb(char *object_path,
529                          bt_obex_authorize_cb func, gpointer data)
530 {
531         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
532         if (info == NULL)
533                 return;
534
535         info->authorize_cb = func;
536         info->authorize_data = data;
537 }
538
539 void _bt_obex_set_release_cb(char *object_path,
540                        bt_obex_release_cb func, gpointer data)
541 {
542         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
543         if (info == NULL)
544                 return;
545
546         info->release_cb = func;
547         info->release_data = data;
548 }
549
550 void _bt_obex_set_request_cb(char *object_path,
551                        bt_obex_request_cb func, gpointer data)
552 {
553         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
554         if (info == NULL)
555                 return;
556
557         info->request_cb = func;
558         info->request_data = data;
559 }
560
561 void _bt_obex_set_progress_cb(char *object_path,
562                         bt_obex_progress_cb func, gpointer data)
563 {
564         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
565         if (info == NULL)
566                 return;
567
568         info->progress_cb = func;
569         info->progress_data = data;
570 }
571
572 void _bt_obex_set_complete_cb(char *object_path,
573                         bt_obex_complete_cb func, gpointer data)
574 {
575         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
576         if (info == NULL)
577                 return;
578
579         info->complete_cb = func;
580         info->complete_data = data;
581 }
582
583 void _bt_obex_set_error_cb(char *object_path,
584                         bt_obex_error_cb func, gpointer data)
585 {
586         bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
587         if (info == NULL)
588                 return;
589
590         info->error_cb = func;
591         info->error_data = data;
592 }