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 SET_PROPERTY "SetProperty"
48 #define GET_DEVICES "GetDevices"
52 static DBusConnection *connection;
54 static GHashTable *dundee_devices = NULL;
60 struct connman_device *device;
61 struct connman_network *network;
63 connman_bool_t active;
68 enum connman_ipconfig_method method;
69 struct connman_ipaddress *address;
72 DBusPendingCall *call;
75 static char *get_ident(const char *path)
82 pos = strrchr(path, '/');
89 static void create_device(struct dundee_data *info)
91 struct connman_device *device;
94 DBG("%s", info->path);
96 ident = g_strdup(get_ident(info->path));
97 device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_BLUETOOTH);
101 DBG("device %p", device);
103 connman_device_set_ident(device, ident);
105 connman_device_set_string(device, "Path", info->path);
107 connman_device_set_data(device, info);
109 if (connman_device_register(device) < 0) {
110 connman_error("Failed to register DUN device");
111 connman_device_unref(device);
115 info->device = device;
121 static void destroy_device(struct dundee_data *info)
123 connman_device_set_powered(info->device, FALSE);
125 if (info->call != NULL)
126 dbus_pending_call_cancel(info->call);
128 if (info->network != NULL) {
129 connman_device_remove_network(info->device, info->network);
130 connman_network_unref(info->network);
131 info->network = NULL;
134 connman_device_unregister(info->device);
135 connman_device_unref(info->device);
140 static void device_destroy(gpointer data)
142 struct dundee_data *info = data;
144 if (info->device != NULL)
145 destroy_device(info);
153 static void create_network(struct dundee_data *info)
155 struct connman_network *network;
158 DBG("%s", info->path);
160 network = connman_network_create(info->path,
161 CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN);
165 DBG("network %p", network);
167 connman_network_set_data(network, info);
169 connman_network_set_string(network, "Path",
172 connman_network_set_name(network, info->name);
174 group = get_ident(info->path);
175 connman_network_set_group(network, group);
177 connman_network_set_available(network, TRUE);
179 if (connman_device_add_network(info->device, network) < 0) {
180 connman_network_unref(network);
184 info->network = network;
187 static void set_connected(struct dundee_data *info)
189 DBG("%s", info->path);
191 connman_inet_ifup(info->index);
193 connman_network_set_index(info->network, info->index);
194 connman_network_set_ipv4_method(info->network,
195 CONNMAN_IPCONFIG_METHOD_FIXED);
196 connman_network_set_ipaddress(info->network, info->address);
197 connman_network_set_nameservers(info->network, info->nameservers);
199 connman_network_set_connected(info->network, TRUE);
202 static void set_disconnected(struct dundee_data *info)
204 DBG("%s", info->path);
206 connman_network_set_connected(info->network, FALSE);
207 connman_inet_ifdown(info->index);
210 static void set_property_reply(DBusPendingCall *call, void *user_data)
212 struct dundee_data *info = user_data;
216 DBG("%s", info->path);
220 dbus_error_init(&error);
222 reply = dbus_pending_call_steal_reply(call);
224 if (dbus_set_error_from_message(&error, reply)) {
225 connman_error("Failed to change property: %s %s %s",
226 info->path, error.name, error.message);
227 dbus_error_free(&error);
229 connman_network_set_error(info->network,
230 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
233 dbus_message_unref(reply);
235 dbus_pending_call_unref(call);
238 static int set_property(struct dundee_data *info,
239 const char *property, int type, void *value)
241 DBusMessage *message;
242 DBusMessageIter iter;
244 DBG("%s %s", info->path, property);
246 message = dbus_message_new_method_call(DUNDEE_SERVICE, info->path,
247 DUNDEE_DEVICE_INTERFACE, SET_PROPERTY);
251 dbus_message_iter_init_append(message, &iter);
252 connman_dbus_property_append_basic(&iter, property, type, value);
254 if (dbus_connection_send_with_reply(connection, message,
255 &info->call, TIMEOUT) == FALSE) {
256 connman_error("Failed to change property: %s %s",
257 info->path, property);
258 dbus_message_unref(message);
262 if (info->call == NULL) {
263 connman_error("D-Bus connection not available");
264 dbus_message_unref(message);
268 dbus_pending_call_set_notify(info->call, set_property_reply,
271 dbus_message_unref(message);
276 static int device_set_active(struct dundee_data *info)
278 dbus_bool_t active = TRUE;
280 DBG("%s", info->path);
282 return set_property(info, "Active", DBUS_TYPE_BOOLEAN,
286 static int device_set_inactive(struct dundee_data *info)
288 dbus_bool_t active = FALSE;
291 DBG("%s", info->path);
293 err = set_property(info, "Active", DBUS_TYPE_BOOLEAN,
295 if (err == -EINPROGRESS)
301 static int network_probe(struct connman_network *network)
303 DBG("network %p", network);
308 static void network_remove(struct connman_network *network)
310 DBG("network %p", network);
313 static int network_connect(struct connman_network *network)
315 struct dundee_data *info = connman_network_get_data(network);
317 DBG("network %p", network);
319 return device_set_active(info);
322 static int network_disconnect(struct connman_network *network)
324 struct dundee_data *info = connman_network_get_data(network);
326 DBG("network %p", network);
328 return device_set_inactive(info);
331 static struct connman_network_driver network_driver = {
333 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN,
334 .probe = network_probe,
335 .remove = network_remove,
336 .connect = network_connect,
337 .disconnect = network_disconnect,
340 static int dundee_probe(struct connman_device *device)
342 DBG("device %p", device);
347 static void dundee_remove(struct connman_device *device)
349 DBG("device %p", device);
352 static int dundee_enable(struct connman_device *device)
354 DBG("device %p", device);
359 static int dundee_disable(struct connman_device *device)
361 DBG("device %p", device);
366 static struct connman_device_driver dundee_driver = {
368 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
369 .probe = dundee_probe,
370 .remove = dundee_remove,
371 .enable = dundee_enable,
372 .disable = dundee_disable,
375 static char *extract_nameservers(DBusMessageIter *array)
377 DBusMessageIter entry;
378 char *nameservers = NULL;
381 dbus_message_iter_recurse(array, &entry);
383 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
384 const char *nameserver;
386 dbus_message_iter_get_basic(&entry, &nameserver);
388 if (nameservers == NULL) {
389 nameservers = g_strdup(nameserver);
392 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
396 dbus_message_iter_next(&entry);
402 static void extract_settings(DBusMessageIter *array,
403 struct dundee_data *info)
405 DBusMessageIter dict;
406 char *address = NULL, *gateway = NULL;
407 char *nameservers = NULL;
408 const char *interface = NULL;
411 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
414 dbus_message_iter_recurse(array, &dict);
416 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
417 DBusMessageIter entry, value;
418 const char *key, *val;
420 dbus_message_iter_recurse(&dict, &entry);
421 dbus_message_iter_get_basic(&entry, &key);
423 dbus_message_iter_next(&entry);
424 dbus_message_iter_recurse(&entry, &value);
426 if (g_str_equal(key, "Interface") == TRUE) {
427 dbus_message_iter_get_basic(&value, &interface);
429 DBG("Interface %s", interface);
431 index = connman_inet_ifindex(interface);
433 DBG("index %d", index);
437 } else if (g_str_equal(key, "Address") == TRUE) {
438 dbus_message_iter_get_basic(&value, &val);
440 address = g_strdup(val);
442 DBG("Address %s", address);
443 } else if (g_str_equal(key, "DomainNameServers") == TRUE) {
444 nameservers = extract_nameservers(&value);
446 DBG("Nameservers %s", nameservers);
447 } else if (g_str_equal(key, "Gateway") == TRUE) {
448 dbus_message_iter_get_basic(&value, &val);
450 gateway = g_strdup(val);
452 DBG("Gateway %s", gateway);
455 dbus_message_iter_next(&dict);
461 info->address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
462 if (info->address == NULL)
466 connman_ipaddress_set_ipv4(info->address, address, NULL, gateway);
468 info->nameservers = nameservers;
471 if (info->nameservers != nameservers)
478 static gboolean device_changed(DBusConnection *connection,
479 DBusMessage *message,
482 const char *path = dbus_message_get_path(message);
483 struct dundee_data *info = NULL;
484 DBusMessageIter iter, value;
486 const char *signature = DBUS_TYPE_STRING_AS_STRING
487 DBUS_TYPE_VARIANT_AS_STRING;
489 if (dbus_message_has_signature(message, signature) == FALSE) {
490 connman_error("dundee signature does not match");
494 info = g_hash_table_lookup(dundee_devices, path);
498 if (dbus_message_iter_init(message, &iter) == FALSE)
501 dbus_message_iter_get_basic(&iter, &key);
503 dbus_message_iter_next(&iter);
504 dbus_message_iter_recurse(&iter, &value);
507 * Dundee guarantees the ordering of Settings and
508 * Active. Settings will always be send before Active = True.
509 * That means we don't have to order here.
511 if (g_str_equal(key, "Active") == TRUE) {
512 dbus_message_iter_get_basic(&value, &info->active);
514 DBG("%s Active %d", info->path, info->active);
516 if (info->active == TRUE)
519 set_disconnected(info);
520 } else if (g_str_equal(key, "Settings") == TRUE) {
521 DBG("%s Settings", info->path);
523 extract_settings(&value, info);
524 } else if (g_str_equal(key, "Name") == TRUE) {
527 dbus_message_iter_get_basic(&value, &name);
530 info->name = g_strdup(name);
532 DBG("%s Name %s", info->path, info->name);
534 connman_network_set_name(info->network, info->name);
535 connman_network_update(info->network);
541 static void add_device(const char *path, DBusMessageIter *properties)
543 struct dundee_data *info;
545 info = g_hash_table_lookup(dundee_devices, path);
549 info = g_try_new0(struct dundee_data, 1);
553 info->path = g_strdup(path);
555 while (dbus_message_iter_get_arg_type(properties) ==
556 DBUS_TYPE_DICT_ENTRY) {
557 DBusMessageIter entry, value;
560 dbus_message_iter_recurse(properties, &entry);
561 dbus_message_iter_get_basic(&entry, &key);
563 dbus_message_iter_next(&entry);
564 dbus_message_iter_recurse(&entry, &value);
566 if (g_str_equal(key, "Active") == TRUE) {
567 dbus_message_iter_get_basic(&value, &info->active);
569 DBG("%s Active %d", info->path, info->active);
570 } else if (g_str_equal(key, "Settings") == TRUE) {
571 DBG("%s Settings", info->path);
573 extract_settings(&value, info);
574 } else if (g_str_equal(key, "Name") == TRUE) {
577 dbus_message_iter_get_basic(&value, &name);
579 info->name = g_strdup(name);
581 DBG("%s Name %s", info->path, info->name);
584 dbus_message_iter_next(properties);
587 g_hash_table_insert(dundee_devices, g_strdup(path), info);
590 create_network(info);
592 if (info->active == TRUE)
596 static gboolean device_added(DBusConnection *connection, DBusMessage *message,
599 DBusMessageIter iter, properties;
601 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
602 DBUS_TYPE_ARRAY_AS_STRING
603 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
604 DBUS_TYPE_STRING_AS_STRING
605 DBUS_TYPE_VARIANT_AS_STRING
606 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
608 if (dbus_message_has_signature(message, signature) == FALSE) {
609 connman_error("dundee signature does not match");
615 if (dbus_message_iter_init(message, &iter) == FALSE)
618 dbus_message_iter_get_basic(&iter, &path);
620 dbus_message_iter_next(&iter);
621 dbus_message_iter_recurse(&iter, &properties);
623 add_device(path, &properties);
628 static void remove_device(DBusConnection *connection, const char *path)
630 DBG("path %s", path);
632 g_hash_table_remove(dundee_devices, path);
635 static gboolean device_removed(DBusConnection *connection, DBusMessage *message,
639 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
641 if (dbus_message_has_signature(message, signature) == FALSE) {
642 connman_error("dundee signature does not match");
646 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
648 remove_device(connection, path);
652 static void manager_get_devices_reply(DBusPendingCall *call, void *user_data)
656 DBusMessageIter array, dict;
657 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
658 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
659 DBUS_TYPE_OBJECT_PATH_AS_STRING
660 DBUS_TYPE_ARRAY_AS_STRING
661 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
662 DBUS_TYPE_STRING_AS_STRING
663 DBUS_TYPE_VARIANT_AS_STRING
664 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
665 DBUS_STRUCT_END_CHAR_AS_STRING;
669 reply = dbus_pending_call_steal_reply(call);
671 if (dbus_message_has_signature(reply, signature) == FALSE) {
672 connman_error("dundee signature does not match");
676 dbus_error_init(&error);
678 if (dbus_set_error_from_message(&error, reply) == TRUE) {
679 connman_error("%s", error.message);
680 dbus_error_free(&error);
684 if (dbus_message_iter_init(reply, &array) == FALSE)
687 dbus_message_iter_recurse(&array, &dict);
689 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
690 DBusMessageIter value, properties;
693 dbus_message_iter_recurse(&dict, &value);
694 dbus_message_iter_get_basic(&value, &path);
696 dbus_message_iter_next(&value);
697 dbus_message_iter_recurse(&value, &properties);
699 add_device(path, &properties);
701 dbus_message_iter_next(&dict);
705 dbus_message_unref(reply);
707 dbus_pending_call_unref(call);
710 static int manager_get_devices(void)
712 DBusMessage *message;
713 DBusPendingCall *call;
717 message = dbus_message_new_method_call(DUNDEE_SERVICE, "/",
718 DUNDEE_MANAGER_INTERFACE, GET_DEVICES);
722 if (dbus_connection_send_with_reply(connection, message,
723 &call, TIMEOUT) == FALSE) {
724 connman_error("Failed to call GetDevices()");
725 dbus_message_unref(message);
730 connman_error("D-Bus connection not available");
731 dbus_message_unref(message);
735 dbus_pending_call_set_notify(call, manager_get_devices_reply,
738 dbus_message_unref(message);
743 static void dundee_connect(DBusConnection *connection, void *user_data)
745 DBG("connection %p", connection);
747 dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
748 g_free, device_destroy);
750 manager_get_devices();
753 static void dundee_disconnect(DBusConnection *connection, void *user_data)
755 DBG("connection %p", connection);
757 g_hash_table_destroy(dundee_devices);
758 dundee_devices = NULL;
762 static guint added_watch;
763 static guint removed_watch;
764 static guint device_watch;
766 static int dundee_init(void)
770 connection = connman_dbus_get_connection();
771 if (connection == NULL)
774 watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
775 dundee_connect, dundee_disconnect, NULL, NULL);
777 added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
778 DUNDEE_MANAGER_INTERFACE,
779 DEVICE_ADDED, device_added,
782 removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
783 DUNDEE_MANAGER_INTERFACE,
784 DEVICE_REMOVED, device_removed,
787 device_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
788 DUNDEE_DEVICE_INTERFACE,
794 if (watch == 0 || added_watch == 0 || removed_watch == 0 ||
800 err = connman_network_driver_register(&network_driver);
804 err = connman_device_driver_register(&dundee_driver);
806 connman_network_driver_unregister(&network_driver);
813 g_dbus_remove_watch(connection, watch);
814 g_dbus_remove_watch(connection, added_watch);
815 g_dbus_remove_watch(connection, removed_watch);
816 g_dbus_remove_watch(connection, device_watch);
818 dbus_connection_unref(connection);
823 static void dundee_exit(void)
825 g_dbus_remove_watch(connection, watch);
826 g_dbus_remove_watch(connection, added_watch);
827 g_dbus_remove_watch(connection, removed_watch);
828 g_dbus_remove_watch(connection, device_watch);
830 connman_device_driver_unregister(&dundee_driver);
831 connman_network_driver_unregister(&network_driver);
833 dbus_connection_unref(connection);
836 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
837 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)