5 * Copyright (C) 2011 Bartosz Szatkowski <bulislaw@linux.com> for Comarch
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #define OBEX_MAS_UUID \
39 "\xBB\x58\x2B\x40\x42\x0C\x11\xDB\xB0\xDE\x08\x00\x20\x0C\x9A\x66"
40 #define OBEX_MAS_UUID_LEN 16
42 #define MAP_INTERFACE "org.openobex.MessageAccess"
43 #define MAS_UUID "00001132-0000-1000-8000-00805f9b34fb"
46 struct obc_session *session;
50 static DBusConnection *conn = NULL;
52 static void simple_cb(GObex *obex, GError *err, GObexPacket *rsp,
56 struct map_data *map = user_data;
57 guint8 err_code = g_obex_packet_get_operation(rsp, NULL);
60 reply = g_dbus_create_error(map->msg,
61 "org.openobex.Error.Failed",
63 else if (err_code != G_OBEX_RSP_SUCCESS)
64 reply = g_dbus_create_error(map->msg,
65 "org.openobex.Error.Failed",
67 g_obex_strerror(err_code),
70 reply = dbus_message_new_method_return(map->msg);
72 g_dbus_send_message(conn, reply);
73 dbus_message_unref(map->msg);
76 static DBusMessage *map_setpath(DBusConnection *connection,
77 DBusMessage *message, void *user_data)
79 struct map_data *map = user_data;
84 if (dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &folder,
85 DBUS_TYPE_INVALID) == FALSE)
86 return g_dbus_create_error(message,
87 "org.openobex.Error.InvalidArguments",
90 obex = obc_session_get_obex(map->session);
92 g_obex_setpath(obex, folder, simple_cb, map, &err);
95 reply = g_dbus_create_error(message,
96 "org.openobex.Error.Failed",
102 map->msg = dbus_message_ref(message);
107 static void buffer_cb(struct obc_session *session, GError *err,
110 struct obc_transfer *transfer = obc_session_get_transfer(session);
111 struct map_data *map = user_data;
117 reply = g_dbus_create_error(map->msg,
118 "org.openobex.Error.Failed",
123 buf = obc_transfer_get_buffer(transfer, &size);
127 reply = g_dbus_create_reply(map->msg, DBUS_TYPE_STRING, &buf,
130 obc_transfer_clear_buffer(transfer);
133 g_dbus_send_message(conn, reply);
134 dbus_message_unref(map->msg);
135 obc_transfer_unregister(transfer);
138 static DBusMessage *map_get_folder_listing(DBusConnection *connection,
139 DBusMessage *message, void *user_data)
141 struct map_data *map = user_data;
144 err = obc_session_get(map->session, "x-obex/folder-listing",
148 return g_dbus_create_error(message, "org.openobex.Error.Failed",
151 map->msg = dbus_message_ref(message);
156 static DBusMessage *map_get_message_listing(DBusConnection *connection,
157 DBusMessage *message, void *user_data)
159 struct map_data *map = user_data;
162 DBusMessageIter msg_iter;
164 dbus_message_iter_init(message, &msg_iter);
166 if (dbus_message_iter_get_arg_type(&msg_iter) != DBUS_TYPE_STRING)
167 return g_dbus_create_error(message,
168 "org.openobex.Error.InvalidArguments", NULL);
170 dbus_message_iter_get_basic(&msg_iter, &folder);
172 err = obc_session_get(map->session, "x-bt/MAP-msg-listing", folder,
176 return g_dbus_create_error(message, "org.openobex.Error.Failed",
179 map->msg = dbus_message_ref(message);
184 static GDBusMethodTable map_methods[] = {
185 { "SetFolder", "s", "", map_setpath,
186 G_DBUS_METHOD_FLAG_ASYNC },
187 { "GetFolderListing", "a{ss}", "s", map_get_folder_listing,
188 G_DBUS_METHOD_FLAG_ASYNC },
189 { "GetMessageListing", "sa{ss}", "s", map_get_message_listing,
190 G_DBUS_METHOD_FLAG_ASYNC },
194 static void map_free(void *data)
196 struct map_data *map = data;
198 obc_session_unref(map->session);
202 static int map_probe(struct obc_session *session)
204 struct map_data *map;
207 path = obc_session_get_path(session);
211 map = g_try_new0(struct map_data, 1);
215 map->session = obc_session_ref(session);
217 if (!g_dbus_register_interface(conn, path, MAP_INTERFACE, map_methods,
218 NULL, NULL, map, map_free)) {
227 static void map_remove(struct obc_session *session)
229 const char *path = obc_session_get_path(session);
233 g_dbus_unregister_interface(conn, path, MAP_INTERFACE);
236 static struct obc_driver map = {
239 .target = OBEX_MAS_UUID,
240 .target_len = OBEX_MAS_UUID_LEN,
251 conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
255 err = obc_driver_register(&map);
257 dbus_connection_unref(conn);
269 dbus_connection_unref(conn);
272 obc_driver_unregister(&map);