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/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;
64 connman_bool_t active;
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);
100 if (device == NULL) {
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);
131 if (info->call != NULL)
132 dbus_pending_call_cancel(info->call);
134 if (info->network != NULL) {
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;
150 if (info->device != NULL)
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) == FALSE) {
271 connman_error("Failed to change property: %s %s",
272 info->path, property);
273 dbus_message_unref(message);
277 if (info->call == NULL) {
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);
362 if (dundee_devices == NULL)
365 g_hash_table_iter_init(&iter, dundee_devices);
367 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
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);
418 if (nameservers == NULL) {
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") == TRUE) {
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") == TRUE) {
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") == TRUE) {
474 nameservers = extract_nameservers(&value);
476 DBG("Nameservers %s", nameservers);
477 } else if (g_str_equal(key, "Gateway") == TRUE) {
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);
492 if (info->address == NULL)
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) == FALSE) {
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) == FALSE)
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") == TRUE) {
542 dbus_message_iter_get_basic(&value, &info->active);
544 DBG("%s Active %d", info->path, info->active);
546 if (info->active == TRUE)
549 set_disconnected(info);
550 } else if (g_str_equal(key, "Settings") == TRUE) {
551 DBG("%s Settings", info->path);
553 extract_settings(&value, info);
554 } else if (g_str_equal(key, "Name") == TRUE) {
557 dbus_message_iter_get_basic(&value, &name);
560 info->name = g_strdup(name);
562 DBG("%s Name %s", info->path, info->name);
564 connman_network_set_name(info->network, info->name);
565 connman_network_update(info->network);
571 static void add_device(const char *path, DBusMessageIter *properties)
573 struct dundee_data *info;
576 info = g_hash_table_lookup(dundee_devices, path);
580 info = g_try_new0(struct dundee_data, 1);
584 info->path = g_strdup(path);
586 while (dbus_message_iter_get_arg_type(properties) ==
587 DBUS_TYPE_DICT_ENTRY) {
588 DBusMessageIter entry, value;
591 dbus_message_iter_recurse(properties, &entry);
592 dbus_message_iter_get_basic(&entry, &key);
594 dbus_message_iter_next(&entry);
595 dbus_message_iter_recurse(&entry, &value);
597 if (g_str_equal(key, "Active") == TRUE) {
598 dbus_message_iter_get_basic(&value, &info->active);
600 DBG("%s Active %d", info->path, info->active);
601 } else if (g_str_equal(key, "Settings") == TRUE) {
602 DBG("%s Settings", info->path);
604 extract_settings(&value, info);
605 } else if (g_str_equal(key, "Name") == TRUE) {
608 dbus_message_iter_get_basic(&value, &name);
610 info->name = g_strdup(name);
612 DBG("%s Name %s", info->path, info->name);
615 dbus_message_iter_next(properties);
618 g_hash_table_insert(dundee_devices, g_strdup(path), info);
620 err = create_device(info);
624 err = create_network(info);
626 destroy_device(info);
630 if (info->active == TRUE)
636 g_hash_table_remove(dundee_devices, path);
639 static gboolean device_added(DBusConnection *conn, DBusMessage *message,
642 DBusMessageIter iter, properties;
644 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING
645 DBUS_TYPE_ARRAY_AS_STRING
646 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
647 DBUS_TYPE_STRING_AS_STRING
648 DBUS_TYPE_VARIANT_AS_STRING
649 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
651 if (dbus_message_has_signature(message, signature) == FALSE) {
652 connman_error("dundee signature does not match");
658 if (dbus_message_iter_init(message, &iter) == FALSE)
661 dbus_message_iter_get_basic(&iter, &path);
663 dbus_message_iter_next(&iter);
664 dbus_message_iter_recurse(&iter, &properties);
666 add_device(path, &properties);
671 static void remove_device(DBusConnection *conn, const char *path)
673 DBG("path %s", path);
675 g_hash_table_remove(dundee_devices, path);
678 static gboolean device_removed(DBusConnection *conn, DBusMessage *message,
682 const char *signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
684 if (dbus_message_has_signature(message, signature) == FALSE) {
685 connman_error("dundee signature does not match");
689 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
691 remove_device(conn, path);
695 static void manager_get_devices_reply(DBusPendingCall *call, void *user_data)
699 DBusMessageIter array, dict;
700 const char *signature = DBUS_TYPE_ARRAY_AS_STRING
701 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
702 DBUS_TYPE_OBJECT_PATH_AS_STRING
703 DBUS_TYPE_ARRAY_AS_STRING
704 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
705 DBUS_TYPE_STRING_AS_STRING
706 DBUS_TYPE_VARIANT_AS_STRING
707 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
708 DBUS_STRUCT_END_CHAR_AS_STRING;
712 reply = dbus_pending_call_steal_reply(call);
714 if (dbus_message_has_signature(reply, signature) == FALSE) {
715 connman_error("dundee signature does not match");
719 dbus_error_init(&error);
721 if (dbus_set_error_from_message(&error, reply) == TRUE) {
722 connman_error("%s", error.message);
723 dbus_error_free(&error);
727 if (dbus_message_iter_init(reply, &array) == FALSE)
730 dbus_message_iter_recurse(&array, &dict);
732 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_STRUCT) {
733 DBusMessageIter value, properties;
736 dbus_message_iter_recurse(&dict, &value);
737 dbus_message_iter_get_basic(&value, &path);
739 dbus_message_iter_next(&value);
740 dbus_message_iter_recurse(&value, &properties);
742 add_device(path, &properties);
744 dbus_message_iter_next(&dict);
748 dbus_message_unref(reply);
750 dbus_pending_call_unref(call);
753 static int manager_get_devices(void)
755 DBusMessage *message;
756 DBusPendingCall *call;
760 message = dbus_message_new_method_call(DUNDEE_SERVICE, "/",
761 DUNDEE_MANAGER_INTERFACE, GET_DEVICES);
765 if (dbus_connection_send_with_reply(connection, message,
766 &call, TIMEOUT) == FALSE) {
767 connman_error("Failed to call GetDevices()");
768 dbus_message_unref(message);
773 connman_error("D-Bus connection not available");
774 dbus_message_unref(message);
778 dbus_pending_call_set_notify(call, manager_get_devices_reply,
781 dbus_message_unref(message);
786 static void dundee_connect(DBusConnection *conn, void *user_data)
788 DBG("connection %p", conn);
790 dundee_devices = g_hash_table_new_full(g_str_hash, g_str_equal,
791 g_free, device_destroy);
793 manager_get_devices();
796 static void dundee_disconnect(DBusConnection *conn, void *user_data)
798 DBG("connection %p", conn);
800 g_hash_table_destroy(dundee_devices);
801 dundee_devices = NULL;
805 static guint added_watch;
806 static guint removed_watch;
807 static guint device_watch;
809 static int dundee_init(void)
813 connection = connman_dbus_get_connection();
814 if (connection == NULL)
817 watch = g_dbus_add_service_watch(connection, DUNDEE_SERVICE,
818 dundee_connect, dundee_disconnect, NULL, NULL);
820 added_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE, NULL,
821 DUNDEE_MANAGER_INTERFACE,
822 DEVICE_ADDED, device_added,
825 removed_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
826 NULL, DUNDEE_MANAGER_INTERFACE,
827 DEVICE_REMOVED, device_removed,
830 device_watch = g_dbus_add_signal_watch(connection, DUNDEE_SERVICE,
831 NULL, DUNDEE_DEVICE_INTERFACE,
837 if (watch == 0 || added_watch == 0 || removed_watch == 0 ||
843 err = connman_network_driver_register(&network_driver);
847 err = connman_device_driver_register(&dundee_driver);
849 connman_network_driver_unregister(&network_driver);
856 g_dbus_remove_watch(connection, watch);
857 g_dbus_remove_watch(connection, added_watch);
858 g_dbus_remove_watch(connection, removed_watch);
859 g_dbus_remove_watch(connection, device_watch);
861 dbus_connection_unref(connection);
866 static void dundee_exit(void)
868 g_dbus_remove_watch(connection, watch);
869 g_dbus_remove_watch(connection, added_watch);
870 g_dbus_remove_watch(connection, removed_watch);
871 g_dbus_remove_watch(connection, device_watch);
873 connman_device_driver_unregister(&dundee_driver);
874 connman_network_driver_unregister(&network_driver);
876 dbus_connection_unref(connection);
879 CONNMAN_PLUGIN_DEFINE(dundee, "Dundee plugin", VERSION,
880 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dundee_init, dundee_exit)