5 * Copyright (C) 2012-2014 BMW Car IT GmbH.
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/service.h>
36 #include <connman/inet.h>
37 #include <connman/dbus.h>
39 #define DUNDEE_SERVICE "org.ofono.dundee"
40 #define DUNDEE_MANAGER_INTERFACE DUNDEE_SERVICE ".Manager"
41 #define DUNDEE_DEVICE_INTERFACE DUNDEE_SERVICE ".Device"
43 #define DEVICE_ADDED "DeviceAdded"
44 #define DEVICE_REMOVED "DeviceRemoved"
45 #define PROPERTY_CHANGED "PropertyChanged"
47 #define GET_PROPERTIES "GetProperties"
48 #define SET_PROPERTY "SetProperty"
49 #define GET_DEVICES "GetDevices"
53 static DBusConnection *connection;
55 static GHashTable *dundee_devices = NULL;
61 struct connman_device *device;
62 struct connman_network *network;
69 enum connman_ipconfig_method method;
70 struct connman_ipaddress *address;
73 DBusPendingCall *call;
76 static char *get_ident(const char *path)
83 pos = strrchr(path, '/');
90 static int create_device(struct dundee_data *info)
92 struct connman_device *device;
96 DBG("%s", info->path);
98 ident = g_strdup(get_ident(info->path));
99 device = connman_device_create("dundee", CONNMAN_DEVICE_TYPE_BLUETOOTH);
105 DBG("device %p", device);
107 connman_device_set_ident(device, ident);
109 connman_device_set_string(device, "Path", info->path);
111 connman_device_set_data(device, info);
113 err = connman_device_register(device);
115 connman_error("Failed to register DUN device");
116 connman_device_unref(device);
120 info->device = device;
127 static void destroy_device(struct dundee_data *info)
129 connman_device_set_powered(info->device, false);
132 dbus_pending_call_cancel(info->call);
135 connman_device_remove_network(info->device, info->network);
136 connman_network_unref(info->network);
137 info->network = NULL;
140 connman_device_unregister(info->device);
141 connman_device_unref(info->device);
146 static void device_destroy(gpointer data)
148 struct dundee_data *info = data;
151 destroy_device(info);
159 static int create_network(struct dundee_data *info)
161 struct connman_network *network;
165 DBG("%s", info->path);
167 network = connman_network_create(info->path,
168 CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN);
172 DBG("network %p", network);
174 connman_network_set_data(network, info);
176 connman_network_set_string(network, "Path",
179 connman_network_set_name(network, info->name);
181 group = get_ident(info->path);
182 connman_network_set_group(network, group);
184 err = connman_device_add_network(info->device, network);
186 connman_network_unref(network);
190 info->network = network;
195 static void set_connected(struct dundee_data *info)
197 struct connman_service *service;
199 DBG("%s", info->path);
201 connman_inet_ifup(info->index);
203 service = connman_service_lookup_from_network(info->network);
207 connman_service_create_ip4config(service, info->index);
208 connman_network_set_index(info->network, info->index);
209 connman_network_set_ipv4_method(info->network,
210 CONNMAN_IPCONFIG_METHOD_FIXED);
211 connman_network_set_ipaddress(info->network, info->address);
212 connman_network_set_nameservers(info->network, info->nameservers);
214 connman_network_set_connected(info->network, true);
217 static void set_disconnected(struct dundee_data *info)
219 DBG("%s", info->path);
221 connman_network_set_connected(info->network, false);
222 connman_inet_ifdown(info->index);
225 static void set_property_reply(DBusPendingCall *call, void *user_data)
227 struct dundee_data *info = user_data;
231 DBG("%s", info->path);
235 dbus_error_init(&error);
237 reply = dbus_pending_call_steal_reply(call);
239 if (dbus_set_error_from_message(&error, reply)) {
240 connman_error("Failed to change property: %s %s %s",
241 info->path, error.name, error.message);
242 dbus_error_free(&error);
244 connman_network_set_error(info->network,
245 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
248 dbus_message_unref(reply);
250 dbus_pending_call_unref(call);
253 static int set_property(struct dundee_data *info,
254 const char *property, int type, void *value)
256 DBusMessage *message;
257 DBusMessageIter iter;
259 DBG("%s %s", info->path, property);
261 message = dbus_message_new_method_call(DUNDEE_SERVICE, info->path,
262 DUNDEE_DEVICE_INTERFACE, SET_PROPERTY);
266 dbus_message_iter_init_append(message, &iter);
267 connman_dbus_property_append_basic(&iter, property, type, value);
269 if (!dbus_connection_send_with_reply(connection, message,
270 &info->call, TIMEOUT)) {
271 connman_error("Failed to change property: %s %s",
272 info->path, property);
273 dbus_message_unref(message);
278 connman_error("D-Bus connection not available");
279 dbus_message_unref(message);
283 dbus_pending_call_set_notify(info->call, set_property_reply,
286 dbus_message_unref(message);
291 static int device_set_active(struct dundee_data *info)
293 dbus_bool_t active = TRUE;
295 DBG("%s", info->path);
297 return set_property(info, "Active", DBUS_TYPE_BOOLEAN,
301 static int device_set_inactive(struct dundee_data *info)
303 dbus_bool_t active = FALSE;
306 DBG("%s", info->path);
308 err = set_property(info, "Active", DBUS_TYPE_BOOLEAN,
310 if (err == -EINPROGRESS)
316 static int network_probe(struct connman_network *network)
318 DBG("network %p", network);
323 static void network_remove(struct connman_network *network)
325 DBG("network %p", network);
328 static int network_connect(struct connman_network *network)
330 struct dundee_data *info = connman_network_get_data(network);
332 DBG("network %p", network);
334 return device_set_active(info);
337 static int network_disconnect(struct connman_network *network)
339 struct dundee_data *info = connman_network_get_data(network);
341 DBG("network %p", network);
343 return device_set_inactive(info);
346 static struct connman_network_driver network_driver = {
348 .type = CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN,
349 .probe = network_probe,
350 .remove = network_remove,
351 .connect = network_connect,
352 .disconnect = network_disconnect,
355 static int dundee_probe(struct connman_device *device)
360 DBG("device %p", device);
365 g_hash_table_iter_init(&iter, dundee_devices);
367 while (g_hash_table_iter_next(&iter, &key, &value)) {
368 struct dundee_data *info = value;
370 if (device == info->device)
377 static void dundee_remove(struct connman_device *device)
379 DBG("device %p", device);
382 static int dundee_enable(struct connman_device *device)
384 DBG("device %p", device);
389 static int dundee_disable(struct connman_device *device)
391 DBG("device %p", device);
396 static struct connman_device_driver dundee_driver = {
398 .type = CONNMAN_DEVICE_TYPE_BLUETOOTH,
399 .probe = dundee_probe,
400 .remove = dundee_remove,
401 .enable = dundee_enable,
402 .disable = dundee_disable,
405 static char *extract_nameservers(DBusMessageIter *array)
407 DBusMessageIter entry;
408 char *nameservers = NULL;
411 dbus_message_iter_recurse(array, &entry);
413 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
414 const char *nameserver;
416 dbus_message_iter_get_basic(&entry, &nameserver);
419 nameservers = g_strdup(nameserver);
422 nameservers = g_strdup_printf("%s %s", tmp, nameserver);
426 dbus_message_iter_next(&entry);
432 static void extract_settings(DBusMessageIter *array,
433 struct dundee_data *info)
435 DBusMessageIter dict;
436 char *address = NULL, *gateway = NULL;
437 char *nameservers = NULL;
438 const char *interface = NULL;
441 if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
444 dbus_message_iter_recurse(array, &dict);
446 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
447 DBusMessageIter entry, value;
448 const char *key, *val;
450 dbus_message_iter_recurse(&dict, &entry);
451 dbus_message_iter_get_basic(&entry, &key);
453 dbus_message_iter_next(&entry);
454 dbus_message_iter_recurse(&entry, &value);
456 if (g_str_equal(key, "Interface")) {
457 dbus_message_iter_get_basic(&value, &interface);
459 DBG("Interface %s", interface);
461 index = connman_inet_ifindex(interface);
463 DBG("index %d", index);
467 } else if (g_str_equal(key, "Address")) {
468 dbus_message_iter_get_basic(&value, &val);
470 address = g_strdup(val);
472 DBG("Address %s", address);
473 } else if (g_str_equal(key, "DomainNameServers")) {
474 nameservers = extract_nameservers(&value);
476 DBG("Nameservers %s", nameservers);
477 } else if (g_str_equal(key, "Gateway")) {
478 dbus_message_iter_get_basic(&value, &val);
480 gateway = g_strdup(val);
482 DBG("Gateway %s", gateway);
485 dbus_message_iter_next(&dict);
491 info->address = connman_ipaddress_alloc(CONNMAN_IPCONFIG_TYPE_IPV4);
496 connman_ipaddress_set_ipv4(info->address, address, NULL, gateway);
498 info->nameservers = nameservers;
501 if (info->nameservers != nameservers)
508 static gboolean device_changed(DBusConnection *conn,
509 DBusMessage *message,
512 const char *path = dbus_message_get_path(message);
513 struct dundee_data *info = NULL;
514 DBusMessageIter iter, value;
516 const char *signature = DBUS_TYPE_STRING_AS_STRING
517 DBUS_TYPE_VARIANT_AS_STRING;
519 if (!dbus_message_has_signature(message, signature)) {
520 connman_error("dundee signature does not match");
524 info = g_hash_table_lookup(dundee_devices, path);
528 if (!dbus_message_iter_init(message, &iter))
531 dbus_message_iter_get_basic(&iter, &key);
533 dbus_message_iter_next(&iter);
534 dbus_message_iter_recurse(&iter, &value);
537 * Dundee guarantees the ordering of Settings and
538 * Active. Settings will always be send before Active = True.
539 * That means we don't have to order here.
541 if (g_str_equal(key, "Active")) {
544 dbus_message_iter_get_basic(&value, &active);
545 info->active = active;
547 DBG("%s Active %d", info->path, info->active);
552 set_disconnected(info);
553 } else if (g_str_equal(key, "Settings")) {
554 DBG("%s Settings", info->path);
556 extract_settings(&value, info);
557 } else if (g_str_equal(key, "Name")) {
560 dbus_message_iter_get_basic(&value, &name);
563 info->name = g_strdup(name);
565 DBG("%s Name %s", info->path, info->name);
567 connman_network_set_name(info->network, info->name);
568 connman_network_update(info->network);
574 static void add_device(const char *path, DBusMessageIter *properties)
576 struct dundee_data *info;
579 info = g_hash_table_lookup(dundee_devices, path);
583 info = g_try_new0(struct dundee_data, 1);
587 info->path = g_strdup(path);
589 while (dbus_message_iter_get_arg_type(properties) ==
590 DBUS_TYPE_DICT_ENTRY) {
591 DBusMessageIter entry, value;
594 dbus_message_iter_recurse(properties, &entry);
595 dbus_message_iter_get_basic(&entry, &key);
597 dbus_message_iter_next(&entry);
598 dbus_message_iter_recurse(&entry, &value);
600 if (g_str_equal(key, "Active")) {
603 dbus_message_iter_get_basic(&value, &active);
604 info->active = active;
606 DBG("%s Active %d", info->path, info->active);
607 } else if (g_str_equal(key, "Settings")) {
608 DBG("%s Settings", info->path);
610 extract_settings(&value, info);
611 } else if (g_str_equal(key, "Name")) {
614 dbus_message_iter_get_basic(&value, &name);
616 info->name = g_strdup(name);
618 DBG("%s Name %s", info->path, info->name);
621 dbus_message_iter_next(properties);
624 g_hash_table_insert(dundee_devices, g_strdup(path), info);
626 err = create_device(info);
630 err = create_network(info);
632 destroy_device(info);
642 g_hash_table_remove(dundee_devices, path);
645 static gboolean device_added(DBusConnection *conn, DBusMessage *message,
648 DBusMessageIter iter, properties;
650 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
651 DBUS_TYPE_ARRAY_AS_STRING
652 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
653 DBUS_TYPE_STRING_AS_STRING
654 DBUS_TYPE_VARIANT_AS_STRING
655 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
657 if (!dbus_message_has_signature(message, signature)) {
658 connman_error("dundee signature does not match");
664 if (!dbus_message_iter_init(message, &iter))
667 dbus_message_iter_get_basic(&iter, &path);
669 dbus_message_iter_next(&iter);
670 dbus_message_iter_recurse(&iter, &properties);
672 add_device(path, &properties);
677 static void remove_device(DBusConnection *conn, const char *path)
679 DBG("path %s", path);
681 g_hash_table_remove(dundee_devices, path);
684 static gboolean device_removed(DBusConnection *conn, DBusMessage *message,
688 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
690 if (!dbus_message_has_signature(message, signature)) {
691 connman_error("dundee signature does not match");
695 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
697 remove_device(conn, path);
701 static void manager_get_devices_reply(DBusPendingCall *call, void *user_data)
705 DBusMessageIter array, dict;
706 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
707 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
708 DBUS_TYPE_OBJECT_PATH_AS_STRING
709 DBUS_TYPE_ARRAY_AS_STRING
710 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
711 DBUS_TYPE_STRING_AS_STRING
712 DBUS_TYPE_VARIANT_AS_STRING
713 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
714 DBUS_STRUCT_END_CHAR_AS_STRING;
718 reply = dbus_pending_call_steal_reply(call);
720 if (!dbus_message_has_signature(reply, signature)) {
721 connman_error("dundee signature does not match");
725 dbus_error_init(&error);
727 if (dbus_set_error_from_message(&error, reply)) {
728 connman_error("%s", error.message);
729 dbus_error_free(&error);
733 if (!dbus_message_iter_init(reply, &array))
736 dbus_message_iter_recurse(&array, &dict);
738 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
739 DBusMessageIter value, properties;
742 dbus_message_iter_recurse(&dict, &value);
743 dbus_message_iter_get_basic(&value, &path);
745 dbus_message_iter_next(&value);
746 dbus_message_iter_recurse(&value, &properties);
748 add_device(path, &properties);
750 dbus_message_iter_next(&dict);
754 dbus_message_unref(reply);
756 dbus_pending_call_unref(call);
759 static int manager_get_devices(void)
761 DBusMessage *message;
762 DBusPendingCall *call;
766 message = dbus_message_new_method_call(DUNDEE_SERVICE, "/",
767 DUNDEE_MANAGER_INTERFACE, GET_DEVICES);
771 if (!dbus_connection_send_with_reply(connection, message,
773 connman_error("Failed to call GetDevices()");
774 dbus_message_unref(message);
779 connman_error("D-Bus connection not available");
780 dbus_message_unref(message);
784 dbus_pending_call_set_notify(call, manager_get_devices_reply,
787 dbus_message_unref(message);
792 static void dundee_connect(DBusConnection *conn, void *user_data)
794 DBG("connection %p", conn);
796 dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
797 g_free, device_destroy);
799 manager_get_devices();
802 static void dundee_disconnect(DBusConnection *conn, void *user_data)
804 DBG("connection %p", conn);
806 g_hash_table_destroy(dundee_devices);
807 dundee_devices = NULL;
811 static guint added_watch;
812 static guint removed_watch;
813 static guint device_watch;
815 static int dundee_init(void)
819 connection = connman_dbus_get_connection();
823 watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
824 dundee_connect, dundee_disconnect, NULL, NULL);
826 added_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE, NULL,
827 DUNDEE_MANAGER_INTERFACE,
828 DEVICE_ADDED, device_added,
831 removed_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
832 NULL, DUNDEE_MANAGER_INTERFACE,
833 DEVICE_REMOVED, device_removed,
836 device_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
837 NULL, DUNDEE_DEVICE_INTERFACE,
843 if (watch == 0 || added_watch == 0 || removed_watch == 0 ||
849 err = connman_network_driver_register(&network_driver);
853 err = connman_device_driver_register(&dundee_driver);
855 connman_network_driver_unregister(&network_driver);
862 g_dbus_remove_watch(connection, watch);
863 g_dbus_remove_watch(connection, added_watch);
864 g_dbus_remove_watch(connection, removed_watch);
865 g_dbus_remove_watch(connection, device_watch);
867 dbus_connection_unref(connection);
872 static void dundee_exit(void)
874 g_dbus_remove_watch(connection, watch);
875 g_dbus_remove_watch(connection, added_watch);
876 g_dbus_remove_watch(connection, removed_watch);
877 g_dbus_remove_watch(connection, device_watch);
879 connman_device_driver_unregister(&dundee_driver);
880 connman_network_driver_unregister(&network_driver);
882 dbus_connection_unref(connection);
885 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
886 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)