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
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/device.h>
34 #include <connman/network.h>
35 #include <connman/inet.h>
36 #include <connman/dbus.h>
38 #define DUNDEE_SERVICE "org.ofono.dundee"
39 #define DUNDEE_MANAGER_INTERFACE DUNDEE_SERVICE ".Manager"
40 #define DUNDEE_DEVICE_INTERFACE DUNDEE_SERVICE ".Device"
42 #define DEVICE_ADDED "DeviceAdded"
43 #define DEVICE_REMOVED "DeviceRemoved"
44 #define PROPERTY_CHANGED "PropertyChanged"
46 #define GET_PROPERTIES "GetProperties"
47 #define GET_DEVICES "GetDevices"
51 static DBusConnection *connection;
53 static GHashTable *dundee_devices = NULL;
59 struct connman_device *device;
60 struct connman_network *network;
62 connman_bool_t active;
67 enum connman_ipconfig_method method;
68 struct connman_ipaddress *address;
72 static char *get_ident(const char *path)
79 pos = strrchr(path, '/');
86 static void create_device(struct dundee_data *info)
88 struct connman_device *device;
91 DBG("%s", info->path);
93 ident = g_strdup(get_ident(info->path));
94 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
98 DBG("device %p", device);
100 connman_device_set_ident(device, ident);
102 connman_device_set_string(device, "Path", info->path);
104 connman_device_set_data(device, info);
106 if (connman_device_register(device) < 0) {
107 connman_error("Failed to register DUN device");
108 connman_device_unref(device);
112 info->device = device;
118 static void destroy_device(struct dundee_data *info)
120 connman_device_set_powered(info->device, FALSE);
122 if (info->network != NULL) {
123 connman_device_remove_network(info->device, info->network);
124 connman_network_unref(info->network);
125 info->network = NULL;
128 connman_device_unregister(info->device);
129 connman_device_unref(info->device);
134 static void device_destroy(gpointer data)
136 struct dundee_data *info = data;
138 if (info->device != NULL)
139 destroy_device(info);
147 static void create_network(struct dundee_data *info)
149 struct connman_network *network;
152 DBG("%s", info->path);
154 network = connman_network_create(info->path,
155 CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN);
159 DBG("network %p", network);
161 connman_network_set_data(network, info);
163 connman_network_set_string(network, "Path",
166 connman_network_set_name(network, info->name);
168 group = get_ident(info->path);
169 connman_network_set_group(network, group);
171 connman_network_set_available(network, TRUE);
173 if (connman_device_add_network(info->device, network) < 0) {
174 connman_network_unref(network);
178 info->network = network;
181 static int network_probe(struct connman_network *network)
183 DBG("network %p", network);
188 static void network_remove(struct connman_network *network)
190 DBG("network %p", network);
193 static int network_connect(struct connman_network *network)
195 DBG("network %p", network);
200 static int network_disconnect(struct connman_network *network)
202 DBG("network %p", network);
207 static struct connman_network_driver network_driver = {
209 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN,
210 .probe = network_probe,
211 .remove = network_remove,
212 .connect = network_connect,
213 .disconnect = network_disconnect,
216 static int dundee_probe(struct connman_device *device)
218 DBG("device %p", device);
223 static void dundee_remove(struct connman_device *device)
225 DBG("device %p", device);
228 static int dundee_enable(struct connman_device *device)
230 DBG("device %p", device);
235 static int dundee_disable(struct connman_device *device)
237 DBG("device %p", device);
242 static struct connman_device_driver dundee_driver = {
244 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
245 .probe = dundee_probe,
246 .remove = dundee_remove,
247 .enable = dundee_enable,
248 .disable = dundee_disable,
251 static char *extract_nameservers(DBusMessageIter *array)
253 DBusMessageIter entry;
254 char *nameservers = NULL;
257 dbus_message_iter_recurse(array, &entry);
259 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
260 const char *nameserver;
262 dbus_message_iter_get_basic(&entry, &nameserver);
264 if (nameservers == NULL) {
265 nameservers = g_strdup(nameserver);
268 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
272 dbus_message_iter_next(&entry);
278 static void extract_settings(DBusMessageIter *array,
279 struct dundee_data *info)
281 DBusMessageIter dict;
282 char *address = NULL, *gateway = NULL;
283 char *nameservers = NULL;
284 const char *interface = NULL;
287 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
290 dbus_message_iter_recurse(array, &dict);
292 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
293 DBusMessageIter entry, value;
294 const char *key, *val;
296 dbus_message_iter_recurse(&dict, &entry);
297 dbus_message_iter_get_basic(&entry, &key);
299 dbus_message_iter_next(&entry);
300 dbus_message_iter_recurse(&entry, &value);
302 if (g_str_equal(key, "Interface") == TRUE) {
303 dbus_message_iter_get_basic(&value, &interface);
305 DBG("Interface %s", interface);
307 index = connman_inet_ifindex(interface);
309 DBG("index %d", index);
313 } else if (g_str_equal(key, "Address") == TRUE) {
314 dbus_message_iter_get_basic(&value, &val);
316 address = g_strdup(val);
318 DBG("Address %s", address);
319 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
320 nameservers = extract_nameservers(&value);
322 DBG("Nameservers %s", nameservers);
323 } else if (g_str_equal(key, "Gateway") == TRUE) {
324 dbus_message_iter_get_basic(&value, &val);
326 gateway = g_strdup(val);
328 DBG("Gateway %s", gateway);
331 dbus_message_iter_next(&dict);
337 info->address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
338 if (info->address == NULL)
342 connman_ipaddress_set_ipv4(info->address, address, NULL, gateway);
344 info->nameservers = nameservers;
347 if (info->nameservers != nameservers)
354 static gboolean device_changed(DBusConnection *connection,
355 DBusMessage *message,
358 const char *path = dbus_message_get_path(message);
359 struct dundee_data *info = NULL;
360 DBusMessageIter iter, value;
362 const char *signature = DBUS_TYPE_STRING_AS_STRING
363 DBUS_TYPE_VARIANT_AS_STRING;
365 if (dbus_message_has_signature(message, signature) == FALSE) {
366 connman_error("dundee signature does not match");
370 info = g_hash_table_lookup(dundee_devices, path);
374 if (dbus_message_iter_init(message, &iter) == FALSE)
377 dbus_message_iter_get_basic(&iter, &key);
379 dbus_message_iter_next(&iter);
380 dbus_message_iter_recurse(&iter, &value);
383 * Dundee guarantees the ordering of Settings and
384 * Active. Settings will always be send before Active = True.
385 * That means we don't have to order here.
387 if (g_str_equal(key, "Active") == TRUE) {
388 dbus_message_iter_get_basic(&value, &info->active);
390 DBG("%s Active %d", info->path, info->active);
391 } else if (g_str_equal(key, "Settings") == TRUE) {
392 DBG("%s Settings", info->path);
394 extract_settings(&value, info);
395 } else if (g_str_equal(key, "Name") == TRUE) {
398 dbus_message_iter_get_basic(&value, &name);
401 info->name = g_strdup(name);
403 DBG("%s Name %s", info->path, info->name);
405 connman_network_set_name(info->network, info->name);
406 connman_network_update(info->network);
412 static void add_device(const char *path, DBusMessageIter *properties)
414 struct dundee_data *info;
416 info = g_hash_table_lookup(dundee_devices, path);
420 info = g_try_new0(struct dundee_data, 1);
424 info->path = g_strdup(path);
426 while (dbus_message_iter_get_arg_type(properties) ==
427 DBUS_TYPE_DICT_ENTRY) {
428 DBusMessageIter entry, value;
431 dbus_message_iter_recurse(properties, &entry);
432 dbus_message_iter_get_basic(&entry, &key);
434 dbus_message_iter_next(&entry);
435 dbus_message_iter_recurse(&entry, &value);
437 if (g_str_equal(key, "Active") == TRUE) {
438 dbus_message_iter_get_basic(&value, &info->active);
440 DBG("%s Active %d", info->path, info->active);
441 } else if (g_str_equal(key, "Settings") == TRUE) {
442 DBG("%s Settings", info->path);
444 extract_settings(&value, info);
445 } else if (g_str_equal(key, "Name") == TRUE) {
448 dbus_message_iter_get_basic(&value, &name);
450 info->name = g_strdup(name);
452 DBG("%s Name %s", info->path, info->name);
455 dbus_message_iter_next(properties);
458 g_hash_table_insert(dundee_devices, g_strdup(path), info);
461 create_network(info);
464 static gboolean device_added(DBusConnection *connection, DBusMessage *message,
467 DBusMessageIter iter, properties;
469 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
470 DBUS_TYPE_ARRAY_AS_STRING
471 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
472 DBUS_TYPE_STRING_AS_STRING
473 DBUS_TYPE_VARIANT_AS_STRING
474 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
476 if (dbus_message_has_signature(message, signature) == FALSE) {
477 connman_error("dundee signature does not match");
483 if (dbus_message_iter_init(message, &iter) == FALSE)
486 dbus_message_iter_get_basic(&iter, &path);
488 dbus_message_iter_next(&iter);
489 dbus_message_iter_recurse(&iter, &properties);
491 add_device(path, &properties);
496 static void remove_device(DBusConnection *connection, const char *path)
498 DBG("path %s", path);
500 g_hash_table_remove(dundee_devices, path);
503 static gboolean device_removed(DBusConnection *connection, DBusMessage *message,
507 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
509 if (dbus_message_has_signature(message, signature) == FALSE) {
510 connman_error("dundee signature does not match");
514 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
516 remove_device(connection, path);
520 static void manager_get_devices_reply(DBusPendingCall *call, void *user_data)
524 DBusMessageIter array, dict;
525 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
526 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
527 DBUS_TYPE_OBJECT_PATH_AS_STRING
528 DBUS_TYPE_ARRAY_AS_STRING
529 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
530 DBUS_TYPE_STRING_AS_STRING
531 DBUS_TYPE_VARIANT_AS_STRING
532 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
533 DBUS_STRUCT_END_CHAR_AS_STRING;
537 reply = dbus_pending_call_steal_reply(call);
539 if (dbus_message_has_signature(reply, signature) == FALSE) {
540 connman_error("dundee signature does not match");
544 dbus_error_init(&error);
546 if (dbus_set_error_from_message(&error, reply) == TRUE) {
547 connman_error("%s", error.message);
548 dbus_error_free(&error);
552 if (dbus_message_iter_init(reply, &array) == FALSE)
555 dbus_message_iter_recurse(&array, &dict);
557 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
558 DBusMessageIter value, properties;
561 dbus_message_iter_recurse(&dict, &value);
562 dbus_message_iter_get_basic(&value, &path);
564 dbus_message_iter_next(&value);
565 dbus_message_iter_recurse(&value, &properties);
567 add_device(path, &properties);
569 dbus_message_iter_next(&dict);
573 dbus_message_unref(reply);
575 dbus_pending_call_unref(call);
578 static int manager_get_devices(void)
580 DBusMessage *message;
581 DBusPendingCall *call;
585 message = dbus_message_new_method_call(DUNDEE_SERVICE, "/",
586 DUNDEE_MANAGER_INTERFACE, GET_DEVICES);
590 if (dbus_connection_send_with_reply(connection, message,
591 &call, TIMEOUT) == FALSE) {
592 connman_error("Failed to call GetDevices()");
593 dbus_message_unref(message);
598 connman_error("D-Bus connection not available");
599 dbus_message_unref(message);
603 dbus_pending_call_set_notify(call, manager_get_devices_reply,
606 dbus_message_unref(message);
611 static void dundee_connect(DBusConnection *connection, void *user_data)
613 DBG("connection %p", connection);
615 dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
616 g_free, device_destroy);
618 manager_get_devices();
621 static void dundee_disconnect(DBusConnection *connection, void *user_data)
623 DBG("connection %p", connection);
625 g_hash_table_destroy(dundee_devices);
626 dundee_devices = NULL;
630 static guint added_watch;
631 static guint removed_watch;
632 static guint device_watch;
634 static int dundee_init(void)
638 connection = connman_dbus_get_connection();
639 if (connection == NULL)
642 watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
643 dundee_connect, dundee_disconnect, NULL, NULL);
645 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
646 DUNDEE_MANAGER_INTERFACE,
647 DEVICE_ADDED, device_added,
650 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
651 DUNDEE_MANAGER_INTERFACE,
652 DEVICE_REMOVED, device_removed,
655 device_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
656 DUNDEE_DEVICE_INTERFACE,
662 if (watch == 0 || added_watch == 0 || removed_watch == 0 ||
668 err = connman_network_driver_register(&network_driver);
672 err = connman_device_driver_register(&dundee_driver);
674 connman_network_driver_unregister(&network_driver);
681 g_dbus_remove_watch(connection, watch);
682 g_dbus_remove_watch(connection, added_watch);
683 g_dbus_remove_watch(connection, removed_watch);
684 g_dbus_remove_watch(connection, device_watch);
686 dbus_connection_unref(connection);
691 static void dundee_exit(void)
693 g_dbus_remove_watch(connection, watch);
694 g_dbus_remove_watch(connection, added_watch);
695 g_dbus_remove_watch(connection, removed_watch);
696 g_dbus_remove_watch(connection, device_watch);
698 connman_device_driver_unregister(&dundee_driver);
699 connman_network_driver_unregister(&network_driver);
701 dbus_connection_unref(connection);
704 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
705 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)