2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
30 static GDBusConnection *conn = NULL;
31 static GDBusConnection *auth_reply_conn = NULL;
32 static GSList *obex_agent_list = NULL;
43 gpointer authorize_data;
44 gpointer release_data;
45 gpointer request_data;
46 gpointer progress_data;
47 gpointer complete_data;
50 /* callback function */
51 bt_obex_authorize_cb authorize_cb;
52 bt_obex_release_cb release_cb;
53 bt_obex_request_cb request_cb;
54 bt_obex_progress_cb progress_cb;
55 bt_obex_complete_cb complete_cb;
56 bt_obex_error_cb error_cb;
59 static void __new_connection_method(GDBusConnection *connection,
61 const gchar *object_path,
62 const gchar *interface_name,
63 const gchar *method_name,
65 GDBusMethodInvocation *invocation,
67 static const GDBusInterfaceVTable method_table = {
68 __new_connection_method,
73 static const gchar obex_service_agent_xml1[] =
75 " <interface name='org.openobex.Agent'>"
76 " <method name='Request'>"
77 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
78 " <arg type='o' name='transfer'/>"
79 " <arg type='s' name='name' direction='out'/>"
81 " <method name='Progress'>"
82 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
83 " <arg type='o' name='transfer'/>"
84 " <arg type='t' name='transferred'/>"
86 " <method name='Complete'>"
87 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
88 " <arg type='o' name='transfer'/>"
90 " <method name='Release'>"
91 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
93 " <method name='Error'>"
94 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
95 " <arg type='o' name='transfer'/>"
96 " <arg type='s' name='message'/>"
98 " <method name='Authorize'>"
99 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
100 " <arg type='o' name='objpath'/>"
101 " <arg type='s' name='bdaddress'/>"
102 " <arg type='s' name='name'/>"
103 " <arg type='s' name='type'/>"
104 " <arg type='i' name='length'/>"
105 " <arg type='i' name='time'/>"
106 " <arg type='s' name='filepath' direction='out'/>"
111 static const gchar obex_service_agent_xml2[] =
113 " <interface name='org.bluez.obex.Agent1'>"
114 " <method name='AuthorizePush'>"
115 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
116 " <arg type='o' name='objpath'/>"
117 " <arg type='s' name='filepath' direction='out'/>"
122 static const gchar obex_service_reply_xml3[] =
124 " <interface name='org.bluez.obex.reply'>"
125 " <method name='ReplyAuthorize'>"
126 " <arg type='u' name='accept' direction='in'/>"
131 static bt_obex_agent_info *__find_obex_agent_info(char *path)
135 for (l = obex_agent_list; l != NULL; l = l->next) {
136 bt_obex_agent_info *info = l->data;
138 if (g_strcmp0(info->path, path) == 0)
146 static void __new_connection_method(GDBusConnection *connection,
148 const gchar *object_path,
149 const gchar *interface_name,
150 const gchar *method_name,
151 GVariant *parameters,
152 GDBusMethodInvocation *invocation,
155 BT_DBG("method_name %s", method_name);
156 if (g_strcmp0(method_name, "AuthorizePush") == 0) {
157 bt_obex_agent_info *info;
159 info = __find_obex_agent_info((char *)object_path);
164 if (info->authorize_cb == NULL)
167 g_variant_get(parameters, "(&o)", &path);
169 info->authorize_cb(invocation, path,
170 info->authorize_data);
173 } else if (g_strcmp0(method_name, "Authorize") == 0) {
174 g_dbus_method_invocation_return_value(invocation, NULL);
175 } else if (g_strcmp0(method_name, "Request") == 0) {
176 bt_obex_agent_info *info;
182 info = __find_obex_agent_info((char *)object_path);
190 if (info->name == NULL) {
193 if (g_strcmp0(sender, info->name) != 0)
197 if (info->request_cb == NULL)
200 g_variant_get(parameters, "(&o&s)", &path, &name);
201 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
203 BT_OBEX_SERVICE_NAME,
205 BT_OBEX_TRANSFER_INTERFACE,
209 BT_ERR("Dbus Err: %s", err->message);
214 info->request_cb(invocation, proxy, info->request_data);
215 g_object_unref(proxy);
218 } else if (g_strcmp0(method_name, "Progress") == 0) {
221 bt_obex_agent_info *info;
227 info = __find_obex_agent_info((char *)object_path);
235 if (g_strcmp0(sender, info->name) != 0)
238 if (info->progress_cb == NULL)
241 g_variant_get(parameters, "(&ot)", &path, &transferred);
242 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
244 BT_OBEX_SERVICE_NAME,
246 BT_OBEX_TRANSFER_INTERFACE,
250 BT_ERR("Dbus Err: %s", err->message);
255 info->progress_cb(invocation, proxy, transferred, info->progress_data);
257 g_object_unref(proxy);
262 } else if (g_strcmp0(method_name, "Error") == 0) {
263 bt_obex_agent_info *info;
265 char *path, *message;
268 info = __find_obex_agent_info((char *)object_path);
276 if (g_strcmp0(sender, info->name) != 0)
279 if (info->error_cb == NULL)
281 g_variant_get(parameters, "(&o&s)", &path, &message);
282 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
284 BT_OBEX_SERVICE_NAME,
286 BT_OBEX_TRANSFER_INTERFACE,
289 BT_ERR("Dbus Err: %s", err->message);
293 info->error_cb(invocation, proxy, message, info->progress_data);
295 g_object_unref(proxy);
298 } else if (g_strcmp0(method_name, "Complete") == 0) {
299 bt_obex_agent_info *info;
304 info = __find_obex_agent_info((char *)object_path);
312 if (g_strcmp0(sender, info->name) != 0)
315 if (info->complete_cb == NULL)
318 g_variant_get(parameters, "(&o)", &path);
319 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
321 BT_OBEX_SERVICE_NAME,
323 BT_OBEX_TRANSFER_INTERFACE,
326 BT_ERR("Dbus Err: %s", err->message);
331 info->complete_cb(invocation, proxy, info->complete_data);
333 g_object_unref(proxy);
336 } else if (g_strcmp0(method_name, "Release") == 0) {
337 bt_obex_agent_info *info;
339 info = __find_obex_agent_info((char *)object_path);
345 /*In H2 if user denies auth,release will come without request and hence
346 info->name will be NULL */
347 if (g_strcmp0(sender, info->name) != 0)
351 if (info->release_cb == NULL)
354 info->release_cb(invocation, info->release_data);
357 } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
360 g_variant_get(parameters, "(u)", &accept);
361 BT_DBG("Accept: %d", accept);
364 BT_DBG("Transfer accepted");
365 _bt_obex_server_reply_accept();
367 BT_ERR("Transfer denied");
368 _bt_obex_server_reject_authorize();
374 g_dbus_method_invocation_return_value(invocation, NULL);
377 void _bt_obex_agent_new(char *path)
379 bt_obex_agent_info *info = NULL;
380 GError *error = NULL;
383 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
385 BT_ERR("Fail to get dbus: %s", error->message);
389 auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
391 BT_ERR("Fail to get dbus: %s", error->message);
396 info = (bt_obex_agent_info *)g_malloc0(sizeof(bt_obex_agent_info));
398 memset(info, 0, sizeof(bt_obex_agent_info));
399 info->path = g_strdup(path);
400 obex_agent_list = g_slist_append(obex_agent_list, info);
404 void _bt_obex_agent_destroy(char *path)
406 bt_obex_agent_info *info = NULL;
407 info = __find_obex_agent_info(path);
409 BT_ERR("obex agent info not found on path %s", path);
412 obex_agent_list = g_slist_remove(obex_agent_list, info);
417 if (info->openobex_id)
418 g_dbus_connection_unregister_object(conn,
420 if (info->obex_agent_id)
421 g_dbus_connection_unregister_object(conn,
422 info->obex_agent_id);
423 if (info->obex_reply_id)
424 g_dbus_connection_unregister_object(auth_reply_conn,
425 info->obex_reply_id);
429 gboolean _bt_obex_setup(const char *path)
431 bt_obex_agent_info *info;
433 GDBusNodeInfo *new_conn_node = NULL;
434 GDBusNodeInfo *auth_reply_node = NULL;
437 info = __find_obex_agent_info((char *)path);
439 retv_if(info == NULL, FALSE);
441 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
442 G_DBUS_PROXY_FLAGS_NONE,
444 BT_OBEX_SERVICE_NAME,
446 BT_OBEX_AGENT_INTERFACE,
453 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
454 g_object_unref(proxy);
459 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
460 if (new_conn_node == NULL)
463 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
464 new_conn_node->interfaces[0],
467 g_dbus_node_info_unref(new_conn_node);
469 BT_ERR("Dbus Err: %s", err->message);
473 if (info->openobex_id == 0)
474 BT_ERR("Error while registering object");
476 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
477 if (new_conn_node == NULL)
480 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
481 new_conn_node->interfaces[0],
484 g_dbus_node_info_unref(new_conn_node);
485 if (info->obex_agent_id == 0)
486 BT_ERR("Error while registering object");
488 BT_ERR("Dbus Err: %s", err->message);
493 auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
495 if (auth_reply_node == NULL) {
496 BT_ERR("Fail to create the node info for xml");
500 info->obex_reply_id = g_dbus_connection_register_object(auth_reply_conn, info->path,
501 auth_reply_node->interfaces[0], &method_table, NULL, NULL, &err);
502 g_dbus_node_info_unref(auth_reply_node);
503 if (info->obex_reply_id == 0)
504 BT_ERR("Error while registering object");
506 BT_ERR("Dbus Err: %s", err->message);
513 void _bt_obex_set_authorize_cb(char *object_path,
514 bt_obex_authorize_cb func, gpointer data)
516 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
520 info->authorize_cb = func;
521 info->authorize_data = data;
524 void _bt_obex_set_release_cb(char *object_path,
525 bt_obex_release_cb func, gpointer data)
527 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
531 info->release_cb = func;
532 info->release_data = data;
535 void _bt_obex_set_request_cb(char *object_path,
536 bt_obex_request_cb func, gpointer data)
538 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
542 info->request_cb = func;
543 info->request_data = data;
546 void _bt_obex_set_progress_cb(char *object_path,
547 bt_obex_progress_cb func, gpointer data)
549 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
553 info->progress_cb = func;
554 info->progress_data = data;
557 void _bt_obex_set_complete_cb(char *object_path,
558 bt_obex_complete_cb func, gpointer data)
560 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
564 info->complete_cb = func;
565 info->complete_data = data;
568 void _bt_obex_set_error_cb(char *object_path,
569 bt_obex_error_cb func, gpointer data)
571 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
575 info->error_cb = func;
576 info->error_data = data;