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"
30 static GDBusConnection *conn = NULL;
31 static GSList *obex_agent_list = NULL;
41 gpointer authorize_data;
42 gpointer release_data;
43 gpointer request_data;
44 gpointer progress_data;
45 gpointer complete_data;
48 /* callback function */
49 bt_obex_authorize_cb authorize_cb;
50 bt_obex_release_cb release_cb;
51 bt_obex_request_cb request_cb;
52 bt_obex_progress_cb progress_cb;
53 bt_obex_complete_cb complete_cb;
54 bt_obex_error_cb error_cb;
57 static void __new_connection_method(GDBusConnection *connection,
59 const gchar *object_path,
60 const gchar *interface_name,
61 const gchar *method_name,
63 GDBusMethodInvocation *invocation,
65 static const GDBusInterfaceVTable method_table = {
66 __new_connection_method,
71 static const gchar obex_service_agent_xml1[] =
73 " <interface name='org.openobex.Agent'>"
74 " <method name='Request'>"
75 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
76 " <arg type='o' name='transfer'/>"
77 " <arg type='s' name='name' direction='out'/>"
79 " <method name='Progress'>"
80 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
81 " <arg type='o' name='transfer'/>"
82 " <arg type='t' name='transferred'/>"
84 " <method name='Complete'>"
85 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
86 " <arg type='o' name='transfer'/>"
88 " <method name='Release'>"
89 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
91 " <method name='Error'>"
92 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
93 " <arg type='o' name='transfer'/>"
94 " <arg type='s' name='message'/>"
96 " <method name='Authorize'>"
97 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
98 " <arg type='o' name='objpath'/>"
99 " <arg type='s' name='bdaddress'/>"
100 " <arg type='s' name='name'/>"
101 " <arg type='s' name='type'/>"
102 " <arg type='i' name='length'/>"
103 " <arg type='i' name='time'/>"
104 " <arg type='s' name='filepath' direction='out'/>"
109 static const gchar obex_service_agent_xml2[] =
111 " <interface name='org.bluez.obex.Agent1'>"
112 " <method name='AuthorizePush'>"
113 " <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
114 " <arg type='o' name='objpath'/>"
115 " <arg type='s' name='filepath' direction='out'/>"
120 static bt_obex_agent_info *__find_obex_agent_info(char *path)
124 for (l = obex_agent_list; l != NULL; l = l->next) {
125 bt_obex_agent_info *info = l->data;
127 if (g_strcmp0(info->path, path) == 0)
135 static void __new_connection_method(GDBusConnection *connection,
137 const gchar *object_path,
138 const gchar *interface_name,
139 const gchar *method_name,
140 GVariant *parameters,
141 GDBusMethodInvocation *invocation,
144 BT_DBG("method_name %s", method_name);
145 if (g_strcmp0(method_name, "AuthorizePush") == 0) {
146 bt_obex_agent_info *info;
148 info = __find_obex_agent_info((char *)object_path);
153 if (info->authorize_cb == NULL)
156 g_variant_get(parameters, "(&o)", &path);
158 info->authorize_cb(invocation, path,
159 info->authorize_data);
162 } else if (g_strcmp0(method_name, "Authorize") == 0) {
163 g_dbus_method_invocation_return_value(invocation, NULL);
164 } else if (g_strcmp0(method_name, "Request") == 0) {
166 bt_obex_agent_info *info;
172 info = __find_obex_agent_info((char *)object_path);
180 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
182 if (info->name == NULL) {
185 if (g_strcmp0(sender, info->name) != 0) {
190 if (info->request_cb == NULL)
193 g_variant_get(parameters, "(&o&s)", &path, &name);
194 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
196 BT_OBEX_SERVICE_NAME,
198 BT_OBEX_TRANSFER_INTERFACE,
202 BT_ERR("Dbus Err: %s", err->message);
207 info->request_cb(invocation, proxy, info->request_data);
208 g_object_unref(proxy);
211 } else if (g_strcmp0(method_name, "Progress") == 0) {
214 bt_obex_agent_info *info;
221 info = __find_obex_agent_info((char *)object_path);
229 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
231 if (g_strcmp0(sender, info->name) != 0) {
235 if (info->progress_cb == NULL)
238 g_variant_get(parameters, "(&ot)", &path, &transferred);
239 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
241 BT_OBEX_SERVICE_NAME,
243 BT_OBEX_TRANSFER_INTERFACE,
247 BT_ERR("Dbus Err: %s", err->message);
252 info->progress_cb(invocation, proxy, transferred, info->progress_data);
254 g_object_unref(proxy);
259 } else if (g_strcmp0(method_name, "Error") == 0) {
260 bt_obex_agent_info *info;
263 char *path, *message;
266 info = __find_obex_agent_info((char *)object_path);
274 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
276 if (g_strcmp0(sender, info->name) != 0) {
280 if (info->error_cb == NULL)
282 g_variant_get(parameters, "(&o&s)", &path, &message);
283 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
285 BT_OBEX_SERVICE_NAME,
287 BT_OBEX_TRANSFER_INTERFACE,
290 BT_ERR("Dbus Err: %s", err->message);
294 info->error_cb(invocation, proxy, message, info->progress_data);
296 g_object_unref(proxy);
299 } else if (g_strcmp0(method_name, "Complete") == 0) {
300 bt_obex_agent_info *info;
306 info = __find_obex_agent_info((char *)object_path);
314 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
316 if (g_strcmp0(sender, info->name) != 0) {
320 if (info->complete_cb == NULL)
323 g_variant_get(parameters, "(&o)", &path);
324 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_CALL_FLAGS_NONE,
326 BT_OBEX_SERVICE_NAME,
328 BT_OBEX_TRANSFER_INTERFACE,
331 BT_ERR("Dbus Err: %s", err->message);
336 info->complete_cb(invocation, proxy, info->complete_data);
338 g_object_unref(proxy);
341 } else if (g_strcmp0(method_name, "Release") == 0) {
342 bt_obex_agent_info *info;
345 info = __find_obex_agent_info((char *)object_path);
350 sender = (char *)g_dbus_method_invocation_get_sender(invocation);
353 /*In H2 if user denies auth,release will come without request and hence
354 info->name will be NULL */
355 if (g_strcmp0(sender, info->name) != 0) {
360 if (info->release_cb == NULL)
363 info->release_cb(invocation, info->release_data);
369 g_dbus_method_invocation_return_value(invocation, NULL);
372 void _bt_obex_agent_new(char *path)
374 bt_obex_agent_info *info = NULL;
375 GError *error = NULL;
378 conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
380 BT_ERR("Fail to get dbus: %s", error->message);
385 info = (bt_obex_agent_info *)malloc (sizeof(bt_obex_agent_info));
387 memset(info, 0, sizeof(bt_obex_agent_info));
388 info->path = g_strdup(path);
389 obex_agent_list = g_slist_append(obex_agent_list, info);
393 void _bt_obex_agent_destroy(char *path)
395 bt_obex_agent_info *info = NULL;
396 info = __find_obex_agent_info(path);
398 BT_ERR("obex agent info not found on path %s", path);
401 obex_agent_list = g_slist_remove(obex_agent_list, info);
406 if (info->openobex_id)
407 g_dbus_connection_unregister_object(conn,
409 if (info->obex_agent_id)
410 g_dbus_connection_unregister_object(conn,
411 info->obex_agent_id);
415 gboolean _bt_obex_setup(const char *path)
417 bt_obex_agent_info *info;
419 GDBusNodeInfo *new_conn_node;
422 info = __find_obex_agent_info((char *)path);
424 retv_if(info == NULL, FALSE);
426 proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
427 G_DBUS_PROXY_FLAGS_NONE,
429 BT_OBEX_SERVICE_NAME,
431 BT_OBEX_AGENT_INTERFACE,
438 info->name = g_strdup(g_dbus_proxy_get_name(proxy));
439 g_object_unref(proxy);
444 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml1, NULL);
445 if (new_conn_node == NULL)
448 info->openobex_id = g_dbus_connection_register_object(conn, info->path,
449 new_conn_node->interfaces[0],
452 g_dbus_node_info_unref(new_conn_node);
454 BT_INFO("Dbus Err: %s", err->message);
458 if (info->openobex_id == 0)
459 BT_ERR("Error while registering object");
460 new_conn_node = g_dbus_node_info_new_for_xml(obex_service_agent_xml2, NULL);
462 info->obex_agent_id = g_dbus_connection_register_object(conn, info->path,
463 new_conn_node->interfaces[0],
466 g_dbus_node_info_unref(new_conn_node);
467 if (info->obex_agent_id == 0)
468 BT_ERR("Error while registering object");
470 BT_INFO("Dbus Err: %s", err->message);
477 void _bt_obex_set_authorize_cb(char *object_path,
478 bt_obex_authorize_cb func, gpointer data)
480 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
483 info->authorize_cb = func;
484 info->authorize_data = data;
488 void _bt_obex_set_release_cb(char *object_path,
489 bt_obex_release_cb func, gpointer data)
491 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
493 /* Fix : NULL_RETURNS */
497 info->release_cb = func;
498 info->release_data = data;
501 void _bt_obex_set_request_cb(char *object_path,
502 bt_obex_request_cb func, gpointer data)
504 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
506 /* Fix : NULL_RETURNS */
510 info->request_cb = func;
511 info->request_data = data;
514 void _bt_obex_set_progress_cb(char *object_path,
515 bt_obex_progress_cb func, gpointer data)
517 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
519 /* Fix : NULL_RETURNS */
523 info->progress_cb = func;
524 info->progress_data = data;
527 void _bt_obex_set_complete_cb(char *object_path,
528 bt_obex_complete_cb func, gpointer data)
530 bt_obex_agent_info *info =__find_obex_agent_info(object_path);;
532 /* Fix : NULL_RETURNS */
536 info->complete_cb = func;
537 info->complete_data = data;
540 void _bt_obex_set_error_cb(char *object_path,
541 bt_obex_error_cb func, gpointer data)
543 bt_obex_agent_info *info = __find_obex_agent_info(object_path);;
545 /* Fix : NULL_RETURNS */
549 info->error_cb = func;
550 info->error_data = data;