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) {
177 bt_obex_agent_info *info;
183 info = __find_obex_agent_info((char *)object_path);
191 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
193 if (info->name == NULL) {
196 if (g_strcmp0(sender, info->name) != 0)
200 if (info->request_cb == NULL)
203 g_variant_get(parameters, "(&o&s)", &path, &name);
204 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
206 BT_OBEX_SERVICE_NAME,
208 BT_OBEX_TRANSFER_INTERFACE,
212 BT_ERR("Dbus Err: %s", err->message);
217 info->request_cb(invocation, proxy, info->request_data);
218 g_object_unref(proxy);
221 } else if (g_strcmp0(method_name, "Progress") == 0) {
224 bt_obex_agent_info *info;
231 info = __find_obex_agent_info((char *)object_path);
239 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
241 if (g_strcmp0(sender, info->name) != 0)
244 if (info->progress_cb == NULL)
247 g_variant_get(parameters, "(&ot)", &path, &transferred);
248 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
250 BT_OBEX_SERVICE_NAME,
252 BT_OBEX_TRANSFER_INTERFACE,
256 BT_ERR("Dbus Err: %s", err->message);
261 info->progress_cb(invocation, proxy, transferred, info->progress_data);
263 g_object_unref(proxy);
268 } else if (g_strcmp0(method_name, "Error") == 0) {
269 bt_obex_agent_info *info;
272 char *path, *message;
275 info = __find_obex_agent_info((char *)object_path);
283 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
285 if (g_strcmp0(sender, info->name) != 0)
288 if (info->error_cb == NULL)
290 g_variant_get(parameters, "(&o&s)", &path, &message);
291 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
293 BT_OBEX_SERVICE_NAME,
295 BT_OBEX_TRANSFER_INTERFACE,
298 BT_ERR("Dbus Err: %s", err->message);
302 info->error_cb(invocation, proxy, message, info->progress_data);
304 g_object_unref(proxy);
307 } else if (g_strcmp0(method_name, "Complete") == 0) {
308 bt_obex_agent_info *info;
314 info = __find_obex_agent_info((char *)object_path);
322 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
324 if (g_strcmp0(sender, info->name) != 0)
327 if (info->complete_cb == NULL)
330 g_variant_get(parameters, "(&o)", &path);
331 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
333 BT_OBEX_SERVICE_NAME,
335 BT_OBEX_TRANSFER_INTERFACE,
338 BT_ERR("Dbus Err: %s", err->message);
343 info->complete_cb(invocation, proxy, info->complete_data);
345 g_object_unref(proxy);
348 } else if (g_strcmp0(method_name, "Release") == 0) {
349 bt_obex_agent_info *info;
352 info = __find_obex_agent_info((char *)object_path);
357 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
360 /*In H2 if user denies auth,release will come without request and hence
361 info->name will be NULL */
362 if (g_strcmp0(sender, info->name) != 0)
366 if (info->release_cb == NULL)
369 info->release_cb(invocation, info->release_data);
372 } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
375 g_variant_get(parameters, "(u)", &accept);
376 BT_DBG("Accept: %d", accept);
379 BT_DBG("Transfer accepted");
380 _bt_obex_server_reply_accept();
382 BT_ERR("Transfer denied");
383 _bt_obex_server_reject_authorize();
388 g_dbus_method_invocation_return_value(invocation, NULL);
391 void _bt_obex_agent_new(char *path)
393 bt_obex_agent_info *info = NULL;
394 GError *error = NULL;
397 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
399 BT_ERR("Fail to get dbus: %s", error->message);
403 auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
405 BT_ERR("Fail to get dbus: %s", error->message);
410 info = (bt_obex_agent_info *)malloc(sizeof(bt_obex_agent_info));
412 memset(info, 0, sizeof(bt_obex_agent_info));
413 info->path = g_strdup(path);
414 obex_agent_list = g_slist_append(obex_agent_list, info);
418 void _bt_obex_agent_destroy(char *path)
420 bt_obex_agent_info *info = NULL;
421 info = __find_obex_agent_info(path);
423 BT_ERR("obex agent info not found on path %s", path);
426 obex_agent_list = g_slist_remove(obex_agent_list, info);
431 if (info->openobex_id)
432 g_dbus_connection_unregister_object(conn,
434 if (info->obex_agent_id)
435 g_dbus_connection_unregister_object(conn,
436 info->obex_agent_id);
437 if (info->obex_reply_id)
438 g_dbus_connection_unregister_object(auth_reply_conn,
439 info->obex_reply_id);
443 gboolean _bt_obex_setup(const char *path)
445 bt_obex_agent_info *info;
447 GDBusNodeInfo *new_conn_node = NULL;
448 GDBusNodeInfo *auth_reply_node = NULL;
451 info = __find_obex_agent_info((char *)path);
453 retv_if(info == NULL, FALSE);
455 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
456 G_DBUS_PROXY_FLAGS_NONE,
458 BT_OBEX_SERVICE_NAME,
460 BT_OBEX_AGENT_INTERFACE,
467 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
468 g_object_unref(proxy);
473 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
474 if (new_conn_node == NULL)
477 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
478 new_conn_node->interfaces[0],
481 g_dbus_node_info_unref(new_conn_node);
483 BT_ERR("Dbus Err: %s", err->message);
487 if (info->openobex_id == 0)
488 BT_ERR("Error while registering object");
490 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
491 if (new_conn_node == NULL)
494 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
495 new_conn_node->interfaces[0],
498 g_dbus_node_info_unref(new_conn_node);
499 if (info->obex_agent_id == 0)
500 BT_ERR("Error while registering object");
502 BT_ERR("Dbus Err: %s", err->message);
507 auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
509 if (auth_reply_node == NULL) {
510 BT_ERR("Fail to create the node info for xml");
514 info->obex_reply_id = g_dbus_connection_register_object(auth_reply_conn, info->path,
515 auth_reply_node->interfaces[0], &method_table, NULL, NULL, &err);
516 g_dbus_node_info_unref(auth_reply_node);
517 if (info->obex_reply_id == 0)
518 BT_ERR("Error while registering object");
520 BT_ERR("Dbus Err: %s", err->message);
527 void _bt_obex_set_authorize_cb(char *object_path,
528 bt_obex_authorize_cb func, gpointer data)
530 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
534 info->authorize_cb = func;
535 info->authorize_data = data;
538 void _bt_obex_set_release_cb(char *object_path,
539 bt_obex_release_cb func, gpointer data)
541 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
545 info->release_cb = func;
546 info->release_data = data;
549 void _bt_obex_set_request_cb(char *object_path,
550 bt_obex_request_cb func, gpointer data)
552 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
556 info->request_cb = func;
557 info->request_data = data;
560 void _bt_obex_set_progress_cb(char *object_path,
561 bt_obex_progress_cb func, gpointer data)
563 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
567 info->progress_cb = func;
568 info->progress_data = data;
571 void _bt_obex_set_complete_cb(char *object_path,
572 bt_obex_complete_cb func, gpointer data)
574 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
578 info->complete_cb = func;
579 info->complete_data = data;
582 void _bt_obex_set_error_cb(char *object_path,
583 bt_obex_error_cb func, gpointer data)
585 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
589 info->error_cb = func;
590 info->error_data = data;