4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
29 #include "bluetooth-api.h"
30 #include "bt-service-common.h"
31 #include "bt-service-event.h"
32 #include "bt-service-util.h"
33 #include "bt-service-obex-agent.h"
36 static GDBusConnection *conn = NULL;
37 static GSList *obex_agent_list = NULL;
47 gpointer authorize_data;
48 gpointer release_data;
49 gpointer request_data;
50 gpointer progress_data;
51 gpointer complete_data;
54 /* callback function */
55 bt_obex_authorize_cb authorize_cb;
56 bt_obex_release_cb release_cb;
57 bt_obex_request_cb request_cb;
58 bt_obex_progress_cb progress_cb;
59 bt_obex_complete_cb complete_cb;
60 bt_obex_error_cb error_cb;
63 static void __new_connection_method(GDBusConnection *connection,
65 const gchar *object_path,
66 const gchar *interface_name,
67 const gchar *method_name,
69 GDBusMethodInvocation *invocation,
71 static const GDBusInterfaceVTable method_table = {
72 __new_connection_method,
77 static const gchar obex_service_agent_xml1[] =
79 " <interface name='org.openobex.Agent'>"
80 " <method name='Request'>"
81 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
82 " <arg type='o' name='transfer'/>"
83 " <arg type='s' name='name' direction='out'/>"
85 " <method name='Progress'>"
86 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
87 " <arg type='o' name='transfer'/>"
88 " <arg type='t' name='transferred'/>"
90 " <method name='Complete'>"
91 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
92 " <arg type='o' name='transfer'/>"
94 " <method name='Release'>"
95 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
97 " <method name='Error'>"
98 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
99 " <arg type='o' name='transfer'/>"
100 " <arg type='s' name='message'/>"
102 " <method name='Authorize'>"
103 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
104 " <arg type='o' name='objpath'/>"
105 " <arg type='s' name='bdaddress'/>"
106 " <arg type='s' name='name'/>"
107 " <arg type='s' name='type'/>"
108 " <arg type='i' name='length'/>"
109 " <arg type='i' name='time'/>"
110 " <arg type='s' name='filepath' direction='out'/>"
115 static const gchar obex_service_agent_xml2[] =
117 " <interface name='org.bluez.obex.Agent1'>"
118 " <method name='AuthorizePush'>"
119 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
120 " <arg type='o' name='objpath'/>"
121 " <arg type='s' name='filepath' direction='out'/>"
126 static bt_obex_agent_info *__find_obex_agent_info(char *path)
130 for (l = obex_agent_list; l != NULL; l = l->next) {
131 bt_obex_agent_info *info = l->data;
133 if (g_strcmp0(info->path, path) == 0)
141 static void __new_connection_method(GDBusConnection *connection,
143 const gchar *object_path,
144 const gchar *interface_name,
145 const gchar *method_name,
146 GVariant *parameters,
147 GDBusMethodInvocation *invocation,
150 BT_DBG("method_name %s", method_name);
151 if (g_strcmp0(method_name, "AuthorizePush") == 0) {
152 bt_obex_agent_info *info;
154 info = __find_obex_agent_info((char *)object_path);
159 if (info->authorize_cb == NULL)
162 g_variant_get(parameters, "(&o)", &path);
164 info->authorize_cb(invocation, path,
165 info->authorize_data);
168 } else if (g_strcmp0(method_name, "Authorize") == 0) {
169 g_dbus_method_invocation_return_value(invocation, NULL);
170 } else if (g_strcmp0(method_name, "Request") == 0) {
172 bt_obex_agent_info *info;
178 info = __find_obex_agent_info((char *)object_path);
186 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
188 if (info->name == NULL) {
191 if (g_strcmp0(sender, info->name) != 0) {
196 if (info->request_cb == NULL)
199 g_variant_get(parameters, "(&o&s)", &path, &name);
200 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
202 BT_OBEX_SERVICE_NAME,
204 BT_OBEX_TRANSFER_INTERFACE,
208 BT_ERR("Dbus Err: %s", err->message);
213 info->request_cb(invocation, proxy, info->request_data);
214 g_object_unref(proxy);
217 } else if (g_strcmp0(method_name, "Progress") == 0) {
220 bt_obex_agent_info *info;
227 info = __find_obex_agent_info((char *)object_path);
235 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
237 if (g_strcmp0(sender, info->name) != 0) {
241 if (info->progress_cb == NULL)
244 g_variant_get(parameters, "(&ot)", &path, &transferred);
245 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
247 BT_OBEX_SERVICE_NAME,
249 BT_OBEX_TRANSFER_INTERFACE,
253 BT_ERR("Dbus Err: %s", err->message);
258 info->progress_cb(invocation, proxy, transferred, info->progress_data);
260 g_object_unref(proxy);
265 } else if (g_strcmp0(method_name, "Error") == 0) {
266 bt_obex_agent_info *info;
269 char *path, *message;
272 info = __find_obex_agent_info((char *)object_path);
280 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
282 if (g_strcmp0(sender, info->name) != 0) {
286 if (info->error_cb == NULL)
288 g_variant_get(parameters, "(&o&s)", &path, &message);
289 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
291 BT_OBEX_SERVICE_NAME,
293 BT_OBEX_TRANSFER_INTERFACE,
296 BT_ERR("Dbus Err: %s", err->message);
300 info->error_cb(invocation, proxy, message, info->progress_data);
302 g_object_unref(proxy);
305 } else if (g_strcmp0(method_name, "Complete") == 0) {
306 bt_obex_agent_info *info;
312 info = __find_obex_agent_info((char *)object_path);
320 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
322 if (g_strcmp0(sender, info->name) != 0) {
326 if (info->complete_cb == NULL)
329 g_variant_get(parameters, "(&o)", &path);
330 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
332 BT_OBEX_SERVICE_NAME,
334 BT_OBEX_TRANSFER_INTERFACE,
337 BT_ERR("Dbus Err: %s", err->message);
342 info->complete_cb(invocation, proxy, info->complete_data);
344 g_object_unref(proxy);
347 } else if (g_strcmp0(method_name, "Release") == 0) {
348 bt_obex_agent_info *info;
351 info = __find_obex_agent_info((char *)object_path);
356 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
359 /*In H2 if user denies auth,release will come without request and hence
360 info->name will be NULL */
361 if (g_strcmp0(sender, info->name) != 0) {
366 if (info->release_cb == NULL)
369 info->release_cb(invocation, info->release_data);
375 g_dbus_method_invocation_return_value(invocation, NULL);
378 void _bt_obex_agent_new(char *path)
380 bt_obex_agent_info *info = NULL;
381 GError *error = NULL;
384 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
386 BT_ERR("Fail to get dbus: %s", error->message);
391 info = (bt_obex_agent_info *)malloc (sizeof(bt_obex_agent_info));
393 memset(info, 0, sizeof(bt_obex_agent_info));
394 info->path = g_strdup(path);
395 obex_agent_list = g_slist_append(obex_agent_list, info);
399 void _bt_obex_agent_destroy(char *path)
401 bt_obex_agent_info *info = NULL;
402 info = __find_obex_agent_info(path);
404 BT_ERR("obex agent info not found on path %s", path);
407 obex_agent_list = g_slist_remove(obex_agent_list, info);
412 if (info->openobex_id)
413 g_dbus_connection_unregister_object(conn,
415 if (info->obex_agent_id)
416 g_dbus_connection_unregister_object(conn,
417 info->obex_agent_id);
421 gboolean _bt_obex_setup(const char *path)
423 bt_obex_agent_info *info;
425 GDBusNodeInfo *new_conn_node;
428 info = __find_obex_agent_info((char *)path);
430 retv_if(info == NULL, FALSE);
432 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
433 G_DBUS_PROXY_FLAGS_NONE,
435 BT_OBEX_SERVICE_NAME,
437 BT_OBEX_AGENT_INTERFACE,
444 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
445 g_object_unref(proxy);
450 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
451 if (new_conn_node == NULL)
454 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
455 new_conn_node->interfaces[0],
458 g_dbus_node_info_unref(new_conn_node);
460 BT_INFO("Dbus Err: %s", err->message);
464 if (info->openobex_id == 0)
465 BT_ERR("Error while registering object");
466 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
468 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
469 new_conn_node->interfaces[0],
472 g_dbus_node_info_unref(new_conn_node);
473 if (info->obex_agent_id == 0)
474 BT_ERR("Error while registering object");
476 BT_INFO("Dbus Err: %s", err->message);
483 void _bt_obex_set_authorize_cb(char *object_path,
484 bt_obex_authorize_cb func, gpointer data)
486 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
489 info->authorize_cb = func;
490 info->authorize_data = data;
494 void _bt_obex_set_release_cb(char *object_path,
495 bt_obex_release_cb func, gpointer data)
497 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
499 /* Fix : NULL_RETURNS */
503 info->release_cb = func;
504 info->release_data = data;
507 void _bt_obex_set_request_cb(char *object_path,
508 bt_obex_request_cb func, gpointer data)
510 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
512 /* Fix : NULL_RETURNS */
516 info->request_cb = func;
517 info->request_data = data;
520 void _bt_obex_set_progress_cb(char *object_path,
521 bt_obex_progress_cb func, gpointer data)
523 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
525 /* Fix : NULL_RETURNS */
529 info->progress_cb = func;
530 info->progress_data = data;
533 void _bt_obex_set_complete_cb(char *object_path,
534 bt_obex_complete_cb func, gpointer data)
536 bt_obex_agent_info *info =__find_obex_agent_info(object_path);;
538 /* Fix : NULL_RETURNS */
542 info->complete_cb = func;
543 info->complete_data = data;
546 void _bt_obex_set_error_cb(char *object_path,
547 bt_obex_error_cb func, gpointer data)
549 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
551 /* Fix : NULL_RETURNS */
555 info->error_cb = func;
556 info->error_data = data;