5 * Copyright (C) 2012 BMW Car IT GmbH. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #define CONNMAN_API_SUBJECT_TO_CHANGE
31 #include <connman/plugin.h>
32 #include <connman/device.h>
33 #include <connman/network.h>
34 #include <connman/dbus.h>
36 #define DUNDEE_SERVICE "org.ofono.dundee"
37 #define DUNDEE_MANAGER_INTERFACE DUNDEE_SERVICE ".Manager"
39 #define DEVICE_ADDED "DeviceAdded"
40 #define DEVICE_REMOVED "DeviceRemoved"
42 static DBusConnection *connection;
44 static GHashTable *dundee_devices = NULL;
50 static void device_destroy(gpointer data)
52 struct dundee_data *info = data;
59 static int network_probe(struct connman_network *network)
61 DBG("network %p", network);
66 static void network_remove(struct connman_network *network)
68 DBG("network %p", network);
71 static int network_connect(struct connman_network *network)
73 DBG("network %p", network);
78 static int network_disconnect(struct connman_network *network)
80 DBG("network %p", network);
85 static struct connman_network_driver network_driver = {
87 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN,
88 .probe = network_probe,
89 .remove = network_remove,
90 .connect = network_connect,
91 .disconnect = network_disconnect,
94 static int dundee_probe(struct connman_device *device)
96 DBG("device %p", device);
101 static void dundee_remove(struct connman_device *device)
103 DBG("device %p", device);
106 static int dundee_enable(struct connman_device *device)
108 DBG("device %p", device);
113 static int dundee_disable(struct connman_device *device)
115 DBG("device %p", device);
120 static struct connman_device_driver dundee_driver = {
122 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
123 .probe = dundee_probe,
124 .remove = dundee_remove,
125 .enable = dundee_enable,
126 .disable = dundee_disable,
129 static void add_device(const char *path, DBusMessageIter *properties)
131 struct dundee_data *info;
133 info = g_hash_table_lookup(dundee_devices, path);
137 info = g_try_new0(struct dundee_data, 1);
141 info->path = g_strdup(path);
143 g_hash_table_insert(dundee_devices, g_strdup(path), info);
146 static gboolean device_added(DBusConnection *connection, DBusMessage *message,
149 DBusMessageIter iter, properties;
151 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
152 DBUS_TYPE_ARRAY_AS_STRING
153 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
154 DBUS_TYPE_STRING_AS_STRING
155 DBUS_TYPE_VARIANT_AS_STRING
156 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
158 if (dbus_message_has_signature(message, signature) == FALSE) {
159 connman_error("dundee signature does not match");
165 if (dbus_message_iter_init(message, &iter) == FALSE)
168 dbus_message_iter_get_basic(&iter, &path);
170 dbus_message_iter_next(&iter);
171 dbus_message_iter_recurse(&iter, &properties);
173 add_device(path, &properties);
178 static void remove_device(DBusConnection *connection, const char *path)
180 DBG("path %s", path);
182 g_hash_table_remove(dundee_devices, path);
185 static gboolean device_removed(DBusConnection *connection, DBusMessage *message,
189 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
191 if (dbus_message_has_signature(message, signature) == FALSE) {
192 connman_error("dundee signature does not match");
196 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
198 remove_device(connection, path);
202 static void dundee_connect(DBusConnection *connection, void *user_data)
204 DBG("connection %p", connection);
206 dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
207 g_free, device_destroy);
210 static void dundee_disconnect(DBusConnection *connection, void *user_data)
212 DBG("connection %p", connection);
214 g_hash_table_destroy(dundee_devices);
215 dundee_devices = NULL;
219 static guint added_watch;
220 static guint removed_watch;
222 static int dundee_init(void)
226 connection = connman_dbus_get_connection();
227 if (connection == NULL)
230 watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
231 dundee_connect, dundee_disconnect, NULL, NULL);
233 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
234 DUNDEE_MANAGER_INTERFACE,
235 DEVICE_ADDED, device_added,
238 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
239 DUNDEE_MANAGER_INTERFACE,
240 DEVICE_REMOVED, device_removed,
243 if (watch == 0 || added_watch == 0 || removed_watch == 0) {
248 err = connman_network_driver_register(&network_driver);
252 err = connman_device_driver_register(&dundee_driver);
254 connman_network_driver_unregister(&network_driver);
261 g_dbus_remove_watch(connection, watch);
262 g_dbus_remove_watch(connection, added_watch);
263 g_dbus_remove_watch(connection, removed_watch);
265 dbus_connection_unref(connection);
270 static void dundee_exit(void)
272 g_dbus_remove_watch(connection, watch);
273 g_dbus_remove_watch(connection, added_watch);
274 g_dbus_remove_watch(connection, removed_watch);
276 connman_device_driver_unregister(&dundee_driver);
277 connman_network_driver_unregister(&network_driver);
279 dbus_connection_unref(connection);
282 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
283 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)