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
39 #define OBEX_MAS_UUID \
40 "\xBB\x58\x2B\x40\x42\x0C\x11\xDB\xB0\xDE\x08\x00\x20\x0C\x9A\x66"
41 #define OBEX_MAS_UUID_LEN 16
43 #define MAP_INTERFACE "org.openobex.MessageAccess"
44 #define MAS_UUID "00001132-0000-1000-8000-00805f9b34fb"
47 struct obc_session *session;
51 static DBusConnection *conn = NULL;
53 static void simple_cb(struct obc_session *session,
54 struct obc_transfer *transfer,
55 GError *err, void *user_data)
58 struct map_data *map = user_data;
61 reply = g_dbus_create_error(map->msg,
62 "org.openobex.Error.Failed",
65 reply = dbus_message_new_method_return(map->msg);
67 g_dbus_send_message(conn, reply);
68 dbus_message_unref(map->msg);
71 static DBusMessage *map_setpath(DBusConnection *connection,
72 DBusMessage *message, void *user_data)
74 struct map_data *map = user_data;
78 if (dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &folder,
79 DBUS_TYPE_INVALID) == FALSE)
80 return g_dbus_create_error(message,
81 "org.openobex.Error.InvalidArguments",
84 obc_session_setpath(map->session, folder, simple_cb, map, &err);
87 reply = g_dbus_create_error(message,
88 "org.openobex.Error.Failed",
94 map->msg = dbus_message_ref(message);
99 static void buffer_cb(struct obc_session *session,
100 struct obc_transfer *transfer,
101 GError *err, void *user_data)
103 struct map_data *map = user_data;
110 reply = g_dbus_create_error(map->msg,
111 "org.openobex.Error.Failed",
116 perr = obc_transfer_get_contents(transfer, &contents, &size);
118 reply = g_dbus_create_error(map->msg,
119 "org.openobex.Error.Failed",
120 "Error reading contents: %s",
125 reply = g_dbus_create_reply(map->msg, DBUS_TYPE_STRING, &contents,
130 g_dbus_send_message(conn, reply);
131 dbus_message_unref(map->msg);
134 static DBusMessage *map_get_folder_listing(DBusConnection *connection,
135 DBusMessage *message, void *user_data)
137 struct map_data *map = user_data;
138 struct obc_transfer *transfer;
142 transfer = obc_transfer_get("x-obex/folder-listing", NULL, NULL, &err);
143 if (transfer == NULL)
146 if (obc_session_queue(map->session, transfer, buffer_cb, map, &err)) {
147 map->msg = dbus_message_ref(message);
152 reply = g_dbus_create_error(message, "org.openobex.Error.Failed", "%s",
158 static DBusMessage *map_get_message_listing(DBusConnection *connection,
159 DBusMessage *message, void *user_data)
161 struct map_data *map = user_data;
162 struct obc_transfer *transfer;
164 DBusMessageIter msg_iter;
168 dbus_message_iter_init(message, &msg_iter);
170 if (dbus_message_iter_get_arg_type(&msg_iter) != DBUS_TYPE_STRING)
171 return g_dbus_create_error(message,
172 "org.openobex.Error.InvalidArguments", NULL);
174 dbus_message_iter_get_basic(&msg_iter, &folder);
176 transfer = obc_transfer_get("x-bt/MAP-msg-listing", folder, NULL, &err);
177 if (transfer == NULL)
180 if (obc_session_queue(map->session, transfer, buffer_cb, map, &err)) {
181 map->msg = dbus_message_ref(message);
186 reply = g_dbus_create_error(message, "org.openobex.Error.Failed", "%s",
192 static GDBusMethodTable map_methods[] = {
193 { "SetFolder", "s", "", map_setpath,
194 G_DBUS_METHOD_FLAG_ASYNC },
195 { "GetFolderListing", "a{ss}", "s", map_get_folder_listing,
196 G_DBUS_METHOD_FLAG_ASYNC },
197 { "GetMessageListing", "sa{ss}", "s", map_get_message_listing,
198 G_DBUS_METHOD_FLAG_ASYNC },
202 static void map_free(void *data)
204 struct map_data *map = data;
206 obc_session_unref(map->session);
210 static int map_probe(struct obc_session *session)
212 struct map_data *map;
215 path = obc_session_get_path(session);
219 map = g_try_new0(struct map_data, 1);
223 map->session = obc_session_ref(session);
225 if (!g_dbus_register_interface(conn, path, MAP_INTERFACE, map_methods,
226 NULL, NULL, map, map_free)) {
235 static void map_remove(struct obc_session *session)
237 const char *path = obc_session_get_path(session);
241 g_dbus_unregister_interface(conn, path, MAP_INTERFACE);
244 static struct obc_driver map = {
247 .target = OBEX_MAS_UUID,
248 .target_len = OBEX_MAS_UUID_LEN,
259 conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
263 err = obc_driver_register(&map);
265 dbus_connection_unref(conn);
277 dbus_connection_unref(conn);
280 obc_driver_unregister(&map);