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.
23 #include <dbus/dbus-glib-lowlevel.h>
24 #include <dbus/dbus-glib.h>
25 #include <dbus/dbus.h>
30 #include "bluetooth-api.h"
31 #include "bt-service-common.h"
32 #include "bt-service-event.h"
33 #include "bt-service-util.h"
34 #include "bt-service-obex-agent.h"
36 #include "bt-obex-agent-method.h"
38 static DBusGConnection *obex_conn = NULL;
45 gpointer authorize_data;
46 gpointer release_data;
47 gpointer request_data;
48 gpointer progress_data;
49 gpointer complete_data;
52 /* callback function */
53 bt_obex_authorize_cb authorize_cb;
54 bt_obex_release_cb release_cb;
55 bt_obex_request_cb request_cb;
56 bt_obex_progress_cb progress_cb;
57 bt_obex_complete_cb complete_cb;
58 bt_obex_error_cb error_cb;
61 #define BT_OBEX_AGENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
62 BT_OBEX_TYPE_AGENT, bt_obex_agent_info))
64 G_DEFINE_TYPE(BtObexAgent, bt_obex_agent, G_TYPE_OBJECT)
67 gboolean bt_obex_agent_authorize_push(BtObexAgent *agent, const char *path,
68 DBusGMethodInvocation *context)
70 bt_obex_agent_info *info;
73 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
78 if (info->authorize_cb == NULL)
81 result = info->authorize_cb(context, path,
82 info->authorize_data);
86 dbus_g_method_return(context, "");
90 gboolean bt_obex_agent_authorize(BtObexAgent *agent, const char *path,
91 const char *bdaddress, const char *name,
92 const char *type, gint length, gint time,
93 DBusGMethodInvocation *context)
95 bt_obex_agent_info *info;
97 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
102 if (info->authorize_cb == NULL)
107 dbus_g_method_return(context, "");
111 gboolean bt_obex_agent_request(BtObexAgent *agent, const char *path,
112 DBusGMethodInvocation *context)
115 bt_obex_agent_info *info;
119 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
124 if (obex_conn == NULL)
127 sender = dbus_g_method_get_sender(context);
129 BT_DBG("sender %s", sender);
131 if (info->name == NULL) {
134 if (g_strcmp0(sender, info->name) != 0) {
141 if (info->request_cb == NULL)
144 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
145 path, BT_OBEX_TRANSFER_INTERFACE);
147 result = info->request_cb(context, proxy, info->request_data);
148 g_object_unref(proxy);
153 dbus_g_method_return(context, "");
157 gboolean bt_obex_agent_progress(BtObexAgent *agent, const char *path,
158 guint64 transferred, DBusGMethodInvocation *context)
162 bt_obex_agent_info *info;
167 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
172 if (obex_conn == NULL)
175 sender = dbus_g_method_get_sender(context);
177 if (g_strcmp0(sender, info->name) != 0) {
184 if (info->progress_cb == NULL)
187 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
188 path, BT_OBEX_TRANSFER_INTERFACE);
190 result = info->progress_cb(context, proxy, transferred, info->progress_data);
192 g_object_unref(proxy);
199 dbus_g_method_return(context, "");
204 gboolean bt_obex_agent_error(BtObexAgent *agent, const char *path,
205 const char *message, DBusGMethodInvocation *context)
207 bt_obex_agent_info *info;
212 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
217 if (obex_conn == NULL)
220 sender = dbus_g_method_get_sender(context);
222 if (g_strcmp0(sender, info->name) != 0) {
229 if (info->error_cb == NULL)
232 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
233 path, BT_OBEX_TRANSFER_INTERFACE);
235 result = info->error_cb(context, proxy, message, info->progress_data);
237 g_object_unref(proxy);
242 dbus_g_method_return(context, "");
246 gboolean bt_obex_agent_complete(BtObexAgent *agent, const char *path,
247 DBusGMethodInvocation *context)
249 bt_obex_agent_info *info;
254 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
259 if (obex_conn == NULL)
262 sender = dbus_g_method_get_sender(context);
264 if (g_strcmp0(sender, info->name) != 0) {
271 if (info->complete_cb == NULL)
274 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
275 path, BT_OBEX_TRANSFER_INTERFACE);
277 result = info->complete_cb(context, proxy, info->complete_data);
279 g_object_unref(proxy);
284 dbus_g_method_return(context, "");
288 gboolean bt_obex_agent_release(BtObexAgent *agent, DBusGMethodInvocation *context)
290 bt_obex_agent_info *info;
294 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
299 sender = dbus_g_method_get_sender(context);
302 /*In H2 if user denies auth,release will come without request and hence
303 info->name will be NULL */
304 if (g_strcmp0(sender, info->name) != 0) {
311 if (info->release_cb == NULL)
314 result = info->release_cb(context, info->release_data);
319 dbus_g_method_return(context, "");
323 static void bt_obex_agent_init(BtObexAgent *agent)
325 BT_DBG("agent %p", agent);
328 static void bt_obex_agent_finalize(GObject *agent)
330 bt_obex_agent_info *info;
332 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
339 G_OBJECT_CLASS(bt_obex_agent_parent_class)->finalize(agent);
342 static void bt_obex_agent_class_init(BtObexAgentClass *agent_class)
344 GObjectClass *object_class;
345 GError *error = NULL;
347 object_class = (GObjectClass *)agent_class;
349 g_type_class_add_private(agent_class, sizeof(bt_obex_agent_info));
351 object_class->finalize = bt_obex_agent_finalize;
353 obex_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
356 BT_ERR("Fail to get dbus: %s", error->message);
360 dbus_g_object_type_install_info(BT_OBEX_TYPE_AGENT,
361 &dbus_glib_bt_obex_agent_object_info);
364 BtObexAgent *_bt_obex_agent_new(void)
368 agent = BT_OBEX_AGENT(g_object_new(BT_OBEX_TYPE_AGENT, NULL));
373 gboolean _bt_obex_setup(BtObexAgent *agent, const char *path)
375 bt_obex_agent_info *info;
379 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
381 retv_if(obex_conn == NULL, FALSE);
382 retv_if(info == NULL, FALSE);
383 retv_if(info->path != NULL, FALSE);
385 info->path = g_strdup(path);
387 proxy = dbus_g_proxy_new_for_name_owner(obex_conn, BT_OBEX_SERVICE_NAME,
389 BT_OBEX_AGENT_INTERFACE, NULL);
393 info->name = g_strdup(dbus_g_proxy_get_bus_name(proxy));
394 g_object_unref(proxy);
399 object = dbus_g_connection_lookup_g_object(obex_conn, info->path);
401 g_object_unref(object);
403 dbus_g_connection_register_g_object(obex_conn, info->path, G_OBJECT(agent));
405 dbus_g_object_register_marshaller(marshal_VOID__OBJECT_BOOLEAN,
406 G_TYPE_NONE, DBUS_TYPE_G_OBJECT_PATH, G_TYPE_BOOLEAN,
409 dbus_g_object_register_marshaller(marshal_VOID__INT_INT,
410 G_TYPE_NONE, G_TYPE_INT, G_TYPE_INT, G_TYPE_INVALID);
414 void _bt_obex_set_authorize_cb(BtObexAgent *agent,
415 bt_obex_authorize_cb func, gpointer data)
417 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
419 info->authorize_cb = func;
420 info->authorize_data = data;
423 void _bt_obex_set_release_cb(BtObexAgent *agent,
424 bt_obex_release_cb func, gpointer data)
426 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
428 info->release_cb = func;
429 info->release_data = data;
432 void _bt_obex_set_request_cb(BtObexAgent *agent,
433 bt_obex_request_cb func, gpointer data)
435 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
437 info->request_cb = func;
438 info->request_data = data;
441 void _bt_obex_set_progress_cb(BtObexAgent *agent,
442 bt_obex_progress_cb func, gpointer data)
444 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
446 info->progress_cb = func;
447 info->progress_data = data;
450 void _bt_obex_set_complete_cb(BtObexAgent *agent,
451 bt_obex_complete_cb func, gpointer data)
453 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
455 info->complete_cb = func;
456 info->complete_data = data;
459 void _bt_obex_set_error_cb(BtObexAgent *agent,
460 bt_obex_error_cb func, gpointer data)
462 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
464 info->error_cb = func;
465 info->error_data = data;