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"
31 static GDBusConnection *conn = NULL;
32 static GDBusConnection *auth_reply_conn = NULL;
33 static GSList *obex_agent_list = NULL;
44 gpointer authorize_data;
45 gpointer release_data;
46 gpointer request_data;
47 gpointer progress_data;
48 gpointer complete_data;
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;
60 static void __new_connection_method(GDBusConnection *connection,
62 const gchar *object_path,
63 const gchar *interface_name,
64 const gchar *method_name,
66 GDBusMethodInvocation *invocation,
68 static const GDBusInterfaceVTable method_table = {
69 __new_connection_method,
74 static const gchar obex_service_agent_xml1[] =
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'/>"
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'/>"
87 " <method name='Complete'>"
88 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
89 " <arg type='o' name='transfer'/>"
91 " <method name='Release'>"
92 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
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'/>"
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'/>"
112 static const gchar obex_service_agent_xml2[] =
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'/>"
123 static const gchar obex_service_reply_xml3[] =
125 " <interface name='org.bluez.obex.reply'>"
126 " <method name='ReplyAuthorize'>"
127 " <arg type='u' name='accept' direction='in'/>"
132 static bt_obex_agent_info *__find_obex_agent_info(char *path)
136 for (l = obex_agent_list; l != NULL; l = l->next) {
137 bt_obex_agent_info *info = l->data;
139 if (g_strcmp0(info->path, path) == 0)
147 static void __new_connection_method(GDBusConnection *connection,
149 const gchar *object_path,
150 const gchar *interface_name,
151 const gchar *method_name,
152 GVariant *parameters,
153 GDBusMethodInvocation *invocation,
156 BT_DBG("method_name %s", method_name);
157 if (g_strcmp0(method_name, "AuthorizePush") == 0) {
158 bt_obex_agent_info *info;
160 info = __find_obex_agent_info((char *)object_path);
165 if (info->authorize_cb == NULL)
168 g_variant_get(parameters, "(&o)", &path);
170 info->authorize_cb(invocation, path,
171 info->authorize_data);
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) {
178 bt_obex_agent_info *info;
184 info = __find_obex_agent_info((char *)object_path);
192 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
194 if (info->name == NULL) {
197 if (g_strcmp0(sender, info->name) != 0)
201 if (info->request_cb == NULL)
204 g_variant_get(parameters, "(&o&s)", &path, &name);
205 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
207 BT_OBEX_SERVICE_NAME,
209 BT_OBEX_TRANSFER_INTERFACE,
213 BT_ERR("Dbus Err: %s", err->message);
218 info->request_cb(invocation, proxy, info->request_data);
219 g_object_unref(proxy);
222 } else if (g_strcmp0(method_name, "Progress") == 0) {
225 bt_obex_agent_info *info;
232 info = __find_obex_agent_info((char *)object_path);
240 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
242 if (g_strcmp0(sender, info->name) != 0)
245 if (info->progress_cb == NULL)
248 g_variant_get(parameters, "(&ot)", &path, &transferred);
249 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
251 BT_OBEX_SERVICE_NAME,
253 BT_OBEX_TRANSFER_INTERFACE,
257 BT_ERR("Dbus Err: %s", err->message);
262 info->progress_cb(invocation, proxy, transferred, info->progress_data);
264 g_object_unref(proxy);
269 } else if (g_strcmp0(method_name, "Error") == 0) {
270 bt_obex_agent_info *info;
273 char *path, *message;
276 info = __find_obex_agent_info((char *)object_path);
284 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
286 if (g_strcmp0(sender, info->name) != 0)
289 if (info->error_cb == NULL)
291 g_variant_get(parameters, "(&o&s)", &path, &message);
292 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
294 BT_OBEX_SERVICE_NAME,
296 BT_OBEX_TRANSFER_INTERFACE,
299 BT_ERR("Dbus Err: %s", err->message);
303 info->error_cb(invocation, proxy, message, info->progress_data);
305 g_object_unref(proxy);
308 } else if (g_strcmp0(method_name, "Complete") == 0) {
309 bt_obex_agent_info *info;
315 info = __find_obex_agent_info((char *)object_path);
323 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
325 if (g_strcmp0(sender, info->name) != 0)
328 if (info->complete_cb == NULL)
331 g_variant_get(parameters, "(&o)", &path);
332 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
334 BT_OBEX_SERVICE_NAME,
336 BT_OBEX_TRANSFER_INTERFACE,
339 BT_ERR("Dbus Err: %s", err->message);
344 info->complete_cb(invocation, proxy, info->complete_data);
346 g_object_unref(proxy);
349 } else if (g_strcmp0(method_name, "Release") == 0) {
350 bt_obex_agent_info *info;
353 info = __find_obex_agent_info((char *)object_path);
358 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
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)
367 if (info->release_cb == NULL)
370 info->release_cb(invocation, info->release_data);
373 } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
376 g_variant_get(parameters, "(u)", &accept);
377 BT_DBG("Accept: %d", accept);
380 BT_DBG("Transfer accepted");
381 _bt_obex_server_reply_accept();
383 BT_ERR("Transfer denied");
384 _bt_obex_server_reject_authorize();
389 g_dbus_method_invocation_return_value(invocation, NULL);
392 void _bt_obex_agent_new(char *path)
394 bt_obex_agent_info *info = NULL;
395 GError *error = NULL;
398 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
400 BT_ERR("Fail to get dbus: %s", error->message);
404 auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
406 BT_ERR("Fail to get dbus: %s", error->message);
411 info = (bt_obex_agent_info *)malloc(sizeof(bt_obex_agent_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);
419 void _bt_obex_agent_destroy(char *path)
421 bt_obex_agent_info *info = NULL;
422 info = __find_obex_agent_info(path);
424 BT_ERR("obex agent info not found on path %s", path);
427 obex_agent_list = g_slist_remove(obex_agent_list, info);
432 if (info->openobex_id)
433 g_dbus_connection_unregister_object(conn,
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);
444 gboolean _bt_obex_setup(const char *path)
446 bt_obex_agent_info *info;
448 GDBusNodeInfo *new_conn_node = NULL;
449 GDBusNodeInfo *auth_reply_node = NULL;
452 info = __find_obex_agent_info((char *)path);
454 retv_if(info == NULL, FALSE);
456 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
457 G_DBUS_PROXY_FLAGS_NONE,
459 BT_OBEX_SERVICE_NAME,
461 BT_OBEX_AGENT_INTERFACE,
468 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
469 g_object_unref(proxy);
474 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
475 if (new_conn_node == NULL)
478 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
479 new_conn_node->interfaces[0],
482 g_dbus_node_info_unref(new_conn_node);
484 BT_ERR("Dbus Err: %s", err->message);
488 if (info->openobex_id == 0)
489 BT_ERR("Error while registering object");
491 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
492 if (new_conn_node == NULL)
495 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
496 new_conn_node->interfaces[0],
499 g_dbus_node_info_unref(new_conn_node);
500 if (info->obex_agent_id == 0)
501 BT_ERR("Error while registering object");
503 BT_ERR("Dbus Err: %s", err->message);
508 auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
510 if (auth_reply_node == NULL) {
511 BT_ERR("Fail to create the node info for xml");
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");
521 BT_ERR("Dbus Err: %s", err->message);
528 void _bt_obex_set_authorize_cb(char *object_path,
529 bt_obex_authorize_cb func, gpointer data)
531 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
535 info->authorize_cb = func;
536 info->authorize_data = data;
539 void _bt_obex_set_release_cb(char *object_path,
540 bt_obex_release_cb func, gpointer data)
542 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
546 info->release_cb = func;
547 info->release_data = data;
550 void _bt_obex_set_request_cb(char *object_path,
551 bt_obex_request_cb func, gpointer data)
553 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
557 info->request_cb = func;
558 info->request_data = data;
561 void _bt_obex_set_progress_cb(char *object_path,
562 bt_obex_progress_cb func, gpointer data)
564 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
568 info->progress_cb = func;
569 info->progress_data = data;
572 void _bt_obex_set_complete_cb(char *object_path,
573 bt_obex_complete_cb func, gpointer data)
575 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
579 info->complete_cb = func;
580 info->complete_data = data;
583 void _bt_obex_set_error_cb(char *object_path,
584 bt_obex_error_cb func, gpointer data)
586 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
590 info->error_cb = func;
591 info->error_data = data;