3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29
#include <dbus/dbus-glib.h>
30
#include <dbus/dbus-glib-lowlevel.h>
32
#include "obex-agent.h"
35
#define DBG(fmt, arg...) printf("%s:%s() " fmt "\n", __FILE__, __FUNCTION__ , ## arg)
40
#define OBEX_SERVICE "org.openobex.client"
42
#define OBEX_CLIENT_PATH "/"
43
#define OBEX_CLIENT_INTERFACE "org.openobex.Client"
44
#define OBEX_TRANSFER_INTERFACE "org.openobex.Transfer"
46
static DBusGConnection *connection = NULL;
48
#define OBEX_AGENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
49
OBEX_TYPE_AGENT, ObexAgentPrivate))
51
typedef struct _ObexAgentPrivate ObexAgentPrivate;
53
struct _ObexAgentPrivate {
57
ObexAgentReleaseFunc release_func;
58
gpointer release_data;
60
ObexAgentRequestFunc request_func;
61
gpointer request_data;
63
ObexAgentProgressFunc progress_func;
64
gpointer progress_data;
66
ObexAgentCompleteFunc complete_func;
67
gpointer complete_data;
70
G_DEFINE_TYPE(ObexAgent, obex_agent, G_TYPE_OBJECT)
72
static gboolean obex_agent_request(ObexAgent *agent, const char *path,
73
DBusGMethodInvocation *context)
75
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
76
const char *sender = dbus_g_method_get_sender(context);
77
gboolean result = FALSE;
79
DBG("agent %p sender %s", agent, sender);
81
if (g_str_equal(sender, priv->busname) == FALSE)
84
if (priv->request_func) {
87
proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
88
path, OBEX_TRANSFER_INTERFACE);
90
result = priv->request_func(context, proxy,
93
g_object_unref(proxy);
95
dbus_g_method_return(context, "");
100
static gboolean obex_agent_progress(ObexAgent *agent, const char *path,
101
guint64 transferred, DBusGMethodInvocation *context)
103
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
104
const char *sender = dbus_g_method_get_sender(context);
105
gboolean result = FALSE;
107
DBG("agent %p sender %s", agent, sender);
109
if (g_str_equal(sender, priv->busname) == FALSE)
112
if (priv->progress_func) {
115
proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
116
path, OBEX_TRANSFER_INTERFACE);
118
result = priv->progress_func(context, proxy, transferred,
119
priv->progress_data);
121
g_object_unref(proxy);
123
dbus_g_method_return(context);
128
static gboolean obex_agent_complete(ObexAgent *agent, const char *path,
129
DBusGMethodInvocation *context)
131
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
132
const char *sender = dbus_g_method_get_sender(context);
133
gboolean result = FALSE;
135
DBG("agent %p sender %s", agent, sender);
137
if (g_str_equal(sender, priv->busname) == FALSE)
140
if (priv->complete_func) {
143
proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
144
path, OBEX_TRANSFER_INTERFACE);
146
result = priv->complete_func(context, proxy,
147
priv->complete_data);
149
g_object_unref(proxy);
151
dbus_g_method_return(context);
156
static gboolean obex_agent_release(ObexAgent *agent,
157
DBusGMethodInvocation *context)
159
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
160
const char *sender = dbus_g_method_get_sender(context);
161
gboolean result = FALSE;
163
DBG("agent %p sender %s", agent, sender);
165
if (g_str_equal(sender, priv->busname) == FALSE)
168
if (priv->release_func)
169
result = priv->release_func(context, priv->release_data);
171
dbus_g_method_return(context);
173
g_object_unref(agent);
178
#include "obex-agent-glue.h"
180
static void obex_agent_init(ObexAgent *agent)
182
DBG("agent %p", agent);
185
static void obex_agent_finalize(GObject *agent)
187
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
189
DBG("agent %p", agent);
192
g_free(priv->busname);
194
G_OBJECT_CLASS(obex_agent_parent_class)->finalize(agent);
197
static void obex_agent_class_init(ObexAgentClass *klass)
199
GObjectClass *object_class = (GObjectClass *) klass;
200
GError *error = NULL;
202
DBG("class %p", klass);
204
g_type_class_add_private(klass, sizeof(ObexAgentPrivate));
206
object_class->finalize = obex_agent_finalize;
208
connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
211
g_printerr("Connecting to session bus failed: %s\n",
216
dbus_g_object_type_install_info(OBEX_TYPE_AGENT,
217
&dbus_glib_obex_agent_object_info);
219
//dbus_g_error_domain_register(AGENT_ERROR, "org.openobex.Error",
220
// AGENT_ERROR_TYPE);
223
ObexAgent *obex_agent_new(void)
227
agent = OBEX_AGENT(g_object_new(OBEX_TYPE_AGENT, NULL));
229
DBG("agent %p", agent);
234
gboolean obex_agent_setup(ObexAgent *agent, const char *path)
236
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
240
DBG("agent %p path %s", agent, path);
242
if (priv->path != NULL)
245
priv->path = g_strdup(path);
247
proxy = dbus_g_proxy_new_for_name_owner(connection, OBEX_SERVICE,
248
OBEX_CLIENT_PATH, OBEX_CLIENT_INTERFACE, NULL);
250
g_free(priv->busname);
253
priv->busname = g_strdup(dbus_g_proxy_get_bus_name(proxy));
254
g_object_unref(proxy);
256
priv->busname = NULL;
258
object = dbus_g_connection_lookup_g_object(connection, priv->path);
260
g_object_unref(object);
262
dbus_g_connection_register_g_object(connection,
263
priv->path, G_OBJECT(agent));
268
void obex_agent_set_release_func(ObexAgent *agent,
269
ObexAgentReleaseFunc func, gpointer data)
271
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
273
DBG("agent %p", agent);
275
priv->release_func = func;
276
priv->release_data = data;
279
void obex_agent_set_request_func(ObexAgent *agent,
280
ObexAgentRequestFunc func, gpointer data)
282
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
284
DBG("agent %p", agent);
286
priv->request_func = func;
287
priv->request_data = data;
290
void obex_agent_set_progress_func(ObexAgent *agent,
291
ObexAgentProgressFunc func, gpointer data)
293
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
295
DBG("agent %p", agent);
297
priv->progress_func = func;
298
priv->progress_data = data;
301
void obex_agent_set_complete_func(ObexAgent *agent,
302
ObexAgentCompleteFunc func, gpointer data)
304
ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
306
DBG("agent %p", agent);
308
priv->complete_func = func;
309
priv->complete_data = data;