4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <dbus/dbus-glib-lowlevel.h>
21 #include <dbus/dbus-glib.h>
22 #include <dbus/dbus.h>
27 #include "bluetooth-api.h"
28 #include "bt-service-common.h"
29 #include "bt-service-event.h"
30 #include "bt-service-util.h"
31 #include "bt-service-obex-agent.h"
33 #include "bt-obex-agent-method.h"
35 static DBusGConnection *obex_conn = NULL;
42 gpointer authorize_data;
43 gpointer release_data;
44 gpointer request_data;
45 gpointer progress_data;
46 gpointer complete_data;
49 /* callback function */
50 bt_obex_authorize_cb authorize_cb;
51 bt_obex_release_cb release_cb;
52 bt_obex_request_cb request_cb;
53 bt_obex_progress_cb progress_cb;
54 bt_obex_complete_cb complete_cb;
55 bt_obex_error_cb error_cb;
58 #define BT_OBEX_AGENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
59 BT_OBEX_TYPE_AGENT, bt_obex_agent_info))
61 G_DEFINE_TYPE(BtObexAgent, bt_obex_agent, G_TYPE_OBJECT)
63 gboolean bt_obex_agent_authorize_push(BtObexAgent *agent, const char *path,
64 DBusGMethodInvocation *context)
66 bt_obex_agent_info *info;
69 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
74 if (info->authorize_cb == NULL)
77 result = info->authorize_cb(context, path,
78 info->authorize_data);
82 dbus_g_method_return(context, "");
86 gboolean bt_obex_agent_request(BtObexAgent *agent, const char *path,
87 DBusGMethodInvocation *context)
90 bt_obex_agent_info *info;
94 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
99 if (obex_conn == NULL)
102 sender = dbus_g_method_get_sender(context);
104 BT_DBG("sender %s", sender);
106 if (info->name == NULL) {
109 if (g_strcmp0(sender, info->name) != 0) {
116 if (info->request_cb == NULL)
119 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
120 path, BT_OBEX_TRANSFER_INTERFACE);
122 result = info->request_cb(context, proxy, info->request_data);
123 g_object_unref(proxy);
128 dbus_g_method_return(context, "");
132 gboolean bt_obex_agent_progress(BtObexAgent *agent, const char *path,
133 guint64 transferred, DBusGMethodInvocation *context)
135 bt_obex_agent_info *info;
140 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
145 if (obex_conn == NULL)
148 sender = dbus_g_method_get_sender(context);
150 if (g_strcmp0(sender, info->name) != 0) {
157 if (info->progress_cb == NULL)
160 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
161 path, BT_OBEX_TRANSFER_INTERFACE);
163 result = info->progress_cb(context, proxy, transferred, info->progress_data);
165 g_object_unref(proxy);
170 dbus_g_method_return(context, "");
174 gboolean bt_obex_agent_error(BtObexAgent *agent, const char *path,
175 const char *message, DBusGMethodInvocation *context)
177 bt_obex_agent_info *info;
182 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
187 if (obex_conn == NULL)
190 sender = dbus_g_method_get_sender(context);
192 if (g_strcmp0(sender, info->name) != 0) {
199 if (info->error_cb == NULL)
202 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
203 path, BT_OBEX_TRANSFER_INTERFACE);
205 result = info->error_cb(context, proxy, message, info->progress_data);
207 g_object_unref(proxy);
212 dbus_g_method_return(context, "");
216 gboolean bt_obex_agent_complete(BtObexAgent *agent, const char *path,
217 DBusGMethodInvocation *context)
219 bt_obex_agent_info *info;
224 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
229 if (obex_conn == NULL)
232 sender = dbus_g_method_get_sender(context);
234 if (g_strcmp0(sender, info->name) != 0) {
241 if (info->complete_cb == NULL)
244 proxy = dbus_g_proxy_new_for_name(obex_conn, BT_OBEX_SERVICE_NAME,
245 path, BT_OBEX_TRANSFER_INTERFACE);
247 result = info->complete_cb(context, proxy, info->complete_data);
249 g_object_unref(proxy);
254 dbus_g_method_return(context, "");
258 gboolean bt_obex_agent_release(BtObexAgent *agent, DBusGMethodInvocation *context)
260 bt_obex_agent_info *info;
264 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
269 sender = dbus_g_method_get_sender(context);
272 /*In H2 if user denies auth,release will come without request and hence
273 info->name will be NULL */
274 if (g_strcmp0(sender, info->name) != 0) {
281 if (info->release_cb == NULL)
284 result = info->release_cb(context, info->release_data);
289 dbus_g_method_return(context, "");
293 static void bt_obex_agent_init(BtObexAgent *agent)
295 BT_DBG("agent %p", agent);
298 static void bt_obex_agent_finalize(GObject *agent)
300 bt_obex_agent_info *info;
302 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
309 G_OBJECT_CLASS(bt_obex_agent_parent_class)->finalize(agent);
312 static void bt_obex_agent_class_init(BtObexAgentClass *agent_class)
314 GObjectClass *object_class;
315 GError *error = NULL;
317 object_class = (GObjectClass *)agent_class;
319 g_type_class_add_private(agent_class, sizeof(bt_obex_agent_info));
321 object_class->finalize = bt_obex_agent_finalize;
323 obex_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
326 BT_ERR("Fail to get dbus: %s", error->message);
330 dbus_g_object_type_install_info(BT_OBEX_TYPE_AGENT,
331 &dbus_glib_bt_obex_agent_object_info);
334 BtObexAgent *_bt_obex_agent_new(void)
338 agent = BT_OBEX_AGENT(g_object_new(BT_OBEX_TYPE_AGENT, NULL));
343 gboolean _bt_obex_setup(BtObexAgent *agent, const char *path)
345 bt_obex_agent_info *info;
349 info = BT_OBEX_AGENT_GET_PRIVATE(agent);
351 retv_if(obex_conn == NULL, FALSE);
352 retv_if(info == NULL, FALSE);
353 retv_if(info->path != NULL, FALSE);
355 info->path = g_strdup(path);
357 proxy = dbus_g_proxy_new_for_name_owner(obex_conn, BT_OBEX_SERVICE_NAME,
359 BT_OBEX_AGENT_INTERFACE, NULL);
363 info->name = g_strdup(dbus_g_proxy_get_bus_name(proxy));
364 g_object_unref(proxy);
369 object = dbus_g_connection_lookup_g_object(obex_conn, info->path);
371 g_object_unref(object);
373 dbus_g_connection_register_g_object(obex_conn, info->path, G_OBJECT(agent));
375 dbus_g_object_register_marshaller(marshal_VOID__OBJECT_BOOLEAN,
376 G_TYPE_NONE, DBUS_TYPE_G_OBJECT_PATH, G_TYPE_BOOLEAN,
379 dbus_g_object_register_marshaller(marshal_VOID__INT_INT,
380 G_TYPE_NONE, G_TYPE_INT, G_TYPE_INT, G_TYPE_INVALID);
384 void _bt_obex_set_authorize_cb(BtObexAgent *agent,
385 bt_obex_authorize_cb func, gpointer data)
387 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
389 info->authorize_cb = func;
390 info->authorize_data = data;
393 void _bt_obex_set_release_cb(BtObexAgent *agent,
394 bt_obex_release_cb func, gpointer data)
396 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
398 info->release_cb = func;
399 info->release_data = data;
402 void _bt_obex_set_request_cb(BtObexAgent *agent,
403 bt_obex_request_cb func, gpointer data)
405 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
407 info->request_cb = func;
408 info->request_data = data;
411 void _bt_obex_set_progress_cb(BtObexAgent *agent,
412 bt_obex_progress_cb func, gpointer data)
414 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
416 info->progress_cb = func;
417 info->progress_data = data;
420 void _bt_obex_set_complete_cb(BtObexAgent *agent,
421 bt_obex_complete_cb func, gpointer data)
423 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
425 info->complete_cb = func;
426 info->complete_data = data;
429 void _bt_obex_set_error_cb(BtObexAgent *agent,
430 bt_obex_error_cb func, gpointer data)
432 bt_obex_agent_info *info = BT_OBEX_AGENT_GET_PRIVATE(agent);
434 info->error_cb = func;
435 info->error_data = data;