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) {
202 if (info->request_cb == NULL)
205 g_variant_get(parameters, "(&o&s)", &path, &name);
206 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
208 BT_OBEX_SERVICE_NAME,
210 BT_OBEX_TRANSFER_INTERFACE,
214 BT_ERR("Dbus Err: %s", err->message);
219 info->request_cb(invocation, proxy, info->request_data);
220 g_object_unref(proxy);
223 } else if (g_strcmp0(method_name, "Progress") == 0) {
226 bt_obex_agent_info *info;
233 info = __find_obex_agent_info((char *)object_path);
241 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
243 if (g_strcmp0(sender, info->name) != 0) {
247 if (info->progress_cb == NULL)
250 g_variant_get(parameters, "(&ot)", &path, &transferred);
251 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
253 BT_OBEX_SERVICE_NAME,
255 BT_OBEX_TRANSFER_INTERFACE,
259 BT_ERR("Dbus Err: %s", err->message);
264 info->progress_cb(invocation, proxy, transferred, info->progress_data);
266 g_object_unref(proxy);
271 } else if (g_strcmp0(method_name, "Error") == 0) {
272 bt_obex_agent_info *info;
275 char *path, *message;
278 info = __find_obex_agent_info((char *)object_path);
286 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
288 if (g_strcmp0(sender, info->name) != 0) {
292 if (info->error_cb == NULL)
294 g_variant_get(parameters, "(&o&s)", &path, &message);
295 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
297 BT_OBEX_SERVICE_NAME,
299 BT_OBEX_TRANSFER_INTERFACE,
302 BT_ERR("Dbus Err: %s", err->message);
306 info->error_cb(invocation, proxy, message, info->progress_data);
308 g_object_unref(proxy);
311 } else if (g_strcmp0(method_name, "Complete") == 0) {
312 bt_obex_agent_info *info;
318 info = __find_obex_agent_info((char *)object_path);
326 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
328 if (g_strcmp0(sender, info->name) != 0) {
332 if (info->complete_cb == NULL)
335 g_variant_get(parameters, "(&o)", &path);
336 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
338 BT_OBEX_SERVICE_NAME,
340 BT_OBEX_TRANSFER_INTERFACE,
343 BT_ERR("Dbus Err: %s", err->message);
348 info->complete_cb(invocation, proxy, info->complete_data);
350 g_object_unref(proxy);
353 } else if (g_strcmp0(method_name, "Release") == 0) {
354 bt_obex_agent_info *info;
357 info = __find_obex_agent_info((char *)object_path);
362 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
365 /*In H2 if user denies auth,release will come without request and hence
366 info->name will be NULL */
367 if (g_strcmp0(sender, info->name) != 0) {
372 if (info->release_cb == NULL)
375 info->release_cb(invocation, info->release_data);
378 } else if (g_strcmp0(method_name, "ReplyAuthorize") == 0) {
381 g_variant_get(parameters, "(u)", &accept);
382 BT_DBG("Accept: %d", accept);
385 BT_DBG("Transfer accepted");
386 _bt_obex_server_reply_accept();
388 BT_ERR("Transfer denied");
389 _bt_obex_server_reject_authorize();
394 g_dbus_method_invocation_return_value(invocation, NULL);
397 void _bt_obex_agent_new(char *path)
399 bt_obex_agent_info *info = NULL;
400 GError *error = NULL;
403 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
405 BT_ERR("Fail to get dbus: %s", error->message);
409 auth_reply_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
411 BT_ERR("Fail to get dbus: %s", error->message);
416 info = (bt_obex_agent_info *)malloc(sizeof(bt_obex_agent_info));
418 memset(info, 0, sizeof(bt_obex_agent_info));
419 info->path = g_strdup(path);
420 obex_agent_list = g_slist_append(obex_agent_list, info);
424 void _bt_obex_agent_destroy(char *path)
426 bt_obex_agent_info *info = NULL;
427 info = __find_obex_agent_info(path);
429 BT_ERR("obex agent info not found on path %s", path);
432 obex_agent_list = g_slist_remove(obex_agent_list, info);
437 if (info->openobex_id)
438 g_dbus_connection_unregister_object(conn,
440 if (info->obex_agent_id)
441 g_dbus_connection_unregister_object(conn,
442 info->obex_agent_id);
443 if (info->obex_reply_id)
444 g_dbus_connection_unregister_object(auth_reply_conn,
445 info->obex_reply_id);
449 gboolean _bt_obex_setup(const char *path)
451 bt_obex_agent_info *info;
453 GDBusNodeInfo *new_conn_node = NULL;
454 GDBusNodeInfo *auth_reply_node = NULL;
457 info = __find_obex_agent_info((char *)path);
459 retv_if(info == NULL, FALSE);
461 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
462 G_DBUS_PROXY_FLAGS_NONE,
464 BT_OBEX_SERVICE_NAME,
466 BT_OBEX_AGENT_INTERFACE,
473 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
474 g_object_unref(proxy);
479 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
480 if (new_conn_node == NULL)
483 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
484 new_conn_node->interfaces[0],
487 g_dbus_node_info_unref(new_conn_node);
489 BT_ERR("Dbus Err: %s", err->message);
493 if (info->openobex_id == 0)
494 BT_ERR("Error while registering object");
496 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
497 if (new_conn_node == NULL)
500 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
501 new_conn_node->interfaces[0],
504 g_dbus_node_info_unref(new_conn_node);
505 if (info->obex_agent_id == 0)
506 BT_ERR("Error while registering object");
508 BT_ERR("Dbus Err: %s", err->message);
513 auth_reply_node = g_dbus_node_info_new_for_xml(obex_service_reply_xml3, NULL);
515 if (auth_reply_node == NULL) {
516 BT_ERR("Fail to create the node info for xml");
520 info->obex_reply_id = g_dbus_connection_register_object(auth_reply_conn, info->path,
521 auth_reply_node->interfaces[0], &method_table, NULL, NULL, &err);
522 g_dbus_node_info_unref(auth_reply_node);
523 if (info->obex_reply_id == 0)
524 BT_ERR("Error while registering object");
526 BT_ERR("Dbus Err: %s", err->message);
533 void _bt_obex_set_authorize_cb(char *object_path,
534 bt_obex_authorize_cb func, gpointer data)
536 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
540 info->authorize_cb = func;
541 info->authorize_data = data;
544 void _bt_obex_set_release_cb(char *object_path,
545 bt_obex_release_cb func, gpointer data)
547 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
551 info->release_cb = func;
552 info->release_data = data;
555 void _bt_obex_set_request_cb(char *object_path,
556 bt_obex_request_cb func, gpointer data)
558 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
562 info->request_cb = func;
563 info->request_data = data;
566 void _bt_obex_set_progress_cb(char *object_path,
567 bt_obex_progress_cb func, gpointer data)
569 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
573 info->progress_cb = func;
574 info->progress_data = data;
577 void _bt_obex_set_complete_cb(char *object_path,
578 bt_obex_complete_cb func, gpointer data)
580 bt_obex_agent_info *info =__find_obex_agent_info(object_path);;
584 info->complete_cb = func;
585 info->complete_data = data;
588 void _bt_obex_set_error_cb(char *object_path,
589 bt_obex_error_cb func, gpointer data)
591 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
595 info->error_cb = func;
596 info->error_data = data;