5 * Copyright (C) 2007-2009 Intel Corporation. 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
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t secondary;
40 connman_bool_t powered;
41 connman_bool_t carrier;
42 connman_bool_t scanning;
43 connman_bool_t disconnected;
44 connman_uint16_t scan_interval;
51 unsigned int connections;
53 struct connman_ipconfig *ipconfig;
55 struct connman_device_driver *driver;
58 connman_bool_t registered;
61 struct connman_network *network;
68 static gboolean device_scan_trigger(gpointer user_data)
70 struct connman_device *device = user_data;
72 DBG("device %p", device);
74 if (device->driver == NULL) {
75 device->scan_timeout = 0;
79 if (device->driver->scan)
80 device->driver->scan(device);
85 static const char *type2description(enum connman_device_type type)
88 case CONNMAN_DEVICE_TYPE_UNKNOWN:
89 case CONNMAN_DEVICE_TYPE_VENDOR:
91 case CONNMAN_DEVICE_TYPE_ETHERNET:
93 case CONNMAN_DEVICE_TYPE_WIFI:
95 case CONNMAN_DEVICE_TYPE_WIMAX:
97 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
99 case CONNMAN_DEVICE_TYPE_GPS:
101 case CONNMAN_DEVICE_TYPE_MBM:
102 case CONNMAN_DEVICE_TYPE_HSO:
103 case CONNMAN_DEVICE_TYPE_NOZOMI:
104 case CONNMAN_DEVICE_TYPE_HUAWEI:
105 case CONNMAN_DEVICE_TYPE_NOVATEL:
112 static const char *type2string(enum connman_device_type type)
115 case CONNMAN_DEVICE_TYPE_UNKNOWN:
116 case CONNMAN_DEVICE_TYPE_VENDOR:
118 case CONNMAN_DEVICE_TYPE_ETHERNET:
120 case CONNMAN_DEVICE_TYPE_WIFI:
122 case CONNMAN_DEVICE_TYPE_WIMAX:
124 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
126 case CONNMAN_DEVICE_TYPE_GPS:
128 case CONNMAN_DEVICE_TYPE_MBM:
129 case CONNMAN_DEVICE_TYPE_HSO:
130 case CONNMAN_DEVICE_TYPE_HUAWEI:
131 case CONNMAN_DEVICE_TYPE_NOZOMI:
132 case CONNMAN_DEVICE_TYPE_NOVATEL:
139 static int set_connected(struct connman_device *device,
140 connman_bool_t connected)
142 if (connected == TRUE) {
143 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
144 struct connman_element *element;
146 device->disconnected = TRUE;
148 switch (device->element.ipv4.method) {
149 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
150 case CONNMAN_IPCONFIG_METHOD_OFF:
152 case CONNMAN_IPCONFIG_METHOD_STATIC:
153 type = CONNMAN_ELEMENT_TYPE_IPV4;
155 case CONNMAN_IPCONFIG_METHOD_DHCP:
156 type = CONNMAN_ELEMENT_TYPE_DHCP;
160 element = connman_element_create(NULL);
161 if (element != NULL) {
162 struct connman_service *service;
164 element->type = type;
165 element->index = device->element.index;
167 if (connman_element_register(element,
168 &device->element) < 0)
169 connman_element_unref(element);
171 device->disconnected = FALSE;
173 service = __connman_service_lookup_from_device(device);
174 __connman_service_indicate_state(service,
175 CONNMAN_SERVICE_STATE_CONFIGURATION);
178 struct connman_service *service;
180 connman_element_unregister_children(&device->element);
182 device->disconnected = TRUE;
184 service = __connman_service_lookup_from_device(device);
185 __connman_service_indicate_state(service,
186 CONNMAN_SERVICE_STATE_IDLE);
192 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
194 struct connman_service *service;
197 __connman_profile_add_device(device);
199 __connman_profile_remove_device(device);
201 service = __connman_service_lookup_from_device(device);
202 __connman_service_set_carrier(service, carrier);
204 return set_connected(device, carrier);
207 static int powered_changed(struct connman_device *device)
210 DBusMessageIter entry, value;
211 const char *key = "Powered";
213 signal = dbus_message_new_signal(device->element.path,
214 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
218 dbus_message_iter_init_append(signal, &entry);
220 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
222 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
223 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
224 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
226 dbus_message_iter_close_container(&entry, &value);
228 g_dbus_send_message(connection, signal);
233 static int set_powered(struct connman_device *device, connman_bool_t powered)
235 struct connman_device_driver *driver = device->driver;
238 DBG("device %p powered %d", device, powered);
240 if (device->powered == powered)
246 if (powered == TRUE) {
247 if (driver->enable) {
248 err = driver->enable(device);
249 __connman_notifier_device_type_increase(device->type);
253 g_hash_table_remove_all(device->networks);
255 set_carrier(device, FALSE);
257 if (driver->disable) {
258 err = driver->disable(device);
259 __connman_notifier_device_type_decrease(device->type);
265 device->powered = powered;
266 powered_changed(device);
272 static void append_path(gpointer key, gpointer value, gpointer user_data)
274 struct connman_element *element = value;
275 DBusMessageIter *iter = user_data;
277 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
281 static void append_networks(struct connman_device *device,
282 DBusMessageIter *entry)
284 DBusMessageIter value, iter;
285 const char *key = "Networks";
287 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
289 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
290 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
293 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
294 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
295 g_hash_table_foreach(device->networks, append_path, &iter);
296 dbus_message_iter_close_container(&value, &iter);
298 dbus_message_iter_close_container(entry, &value);
301 static DBusMessage *get_properties(DBusConnection *conn,
302 DBusMessage *msg, void *data)
304 struct connman_device *device = data;
306 DBusMessageIter array, dict, entry;
309 DBG("conn %p", conn);
311 if (__connman_security_check_privilege(msg,
312 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
313 return __connman_error_permission_denied(msg);
315 reply = dbus_message_new_method_return(msg);
319 dbus_message_iter_init_append(reply, &array);
321 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
322 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
323 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
324 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
326 if (device->name != NULL)
327 connman_dbus_dict_append_variant(&dict, "Name",
328 DBUS_TYPE_STRING, &device->name);
330 str = type2string(device->type);
332 connman_dbus_dict_append_variant(&dict, "Type",
333 DBUS_TYPE_STRING, &str);
335 if (device->address != NULL)
336 connman_dbus_dict_append_variant(&dict, "Address",
337 DBUS_TYPE_STRING, &device->address);
339 if (device->interface != NULL)
340 connman_dbus_dict_append_variant(&dict, "Interface",
341 DBUS_TYPE_STRING, &device->interface);
343 connman_dbus_dict_append_variant(&dict, "Powered",
344 DBUS_TYPE_BOOLEAN, &device->powered);
346 if (device->driver && device->driver->scan)
347 connman_dbus_dict_append_variant(&dict, "Scanning",
348 DBUS_TYPE_BOOLEAN, &device->scanning);
350 switch (device->mode) {
351 case CONNMAN_DEVICE_MODE_UNKNOWN:
353 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
354 __connman_element_append_ipv4(&device->element, &dict);
356 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
357 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
358 if (device->scan_interval > 0)
359 connman_dbus_dict_append_variant(&dict, "ScanInterval",
360 DBUS_TYPE_UINT16, &device->scan_interval);
362 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
364 append_networks(device, &entry);
365 dbus_message_iter_close_container(&dict, &entry);
369 dbus_message_iter_close_container(&array, &dict);
374 static gboolean powered_timeout(gpointer user_data)
376 struct connman_device *device = user_data;
378 DBG("device %p", device);
382 if (device->pending != NULL) {
385 reply = __connman_error_operation_timeout(device->pending);
387 g_dbus_send_message(connection, reply);
389 dbus_message_unref(device->pending);
390 device->pending = NULL;
396 static DBusMessage *set_property(DBusConnection *conn,
397 DBusMessage *msg, void *data)
399 struct connman_device *device = data;
400 DBusMessageIter iter, value;
404 DBG("conn %p", conn);
406 if (dbus_message_iter_init(msg, &iter) == FALSE)
407 return __connman_error_invalid_arguments(msg);
409 dbus_message_iter_get_basic(&iter, &name);
410 dbus_message_iter_next(&iter);
411 dbus_message_iter_recurse(&iter, &value);
413 if (__connman_security_check_privilege(msg,
414 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
415 return __connman_error_permission_denied(msg);
417 type = dbus_message_iter_get_arg_type(&value);
419 if (g_str_equal(name, "Powered") == TRUE) {
420 connman_bool_t powered;
423 if (type != DBUS_TYPE_BOOLEAN)
424 return __connman_error_invalid_arguments(msg);
426 dbus_message_iter_get_basic(&value, &powered);
428 if (device->powered == powered)
429 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
431 if (device->pending != NULL)
432 return __connman_error_in_progress(msg);
434 err = set_powered(device, powered);
436 if (err != -EINPROGRESS)
437 return __connman_error_failed(msg, -err);
439 device->pending = dbus_message_ref(msg);
441 device->timeout = g_timeout_add_seconds(15,
442 powered_timeout, device);
446 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
447 connman_uint16_t interval;
449 switch (device->mode) {
450 case CONNMAN_DEVICE_MODE_UNKNOWN:
451 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
452 return __connman_error_invalid_arguments(msg);
453 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
454 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
458 if (type != DBUS_TYPE_UINT16)
459 return __connman_error_invalid_arguments(msg);
461 dbus_message_iter_get_basic(&value, &interval);
463 device->scan_interval = interval;
465 if (device->scan_timeout > 0) {
466 g_source_remove(device->scan_timeout);
467 device->scan_timeout = 0;
470 if (device->scan_interval > 0) {
471 guint interval = device->scan_interval;
472 device->scan_timeout = g_timeout_add_seconds(interval,
473 device_scan_trigger, device);
475 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
478 switch (device->mode) {
479 case CONNMAN_DEVICE_MODE_UNKNOWN:
480 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
481 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
482 return __connman_error_invalid_arguments(msg);
483 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
487 err = __connman_ipconfig_set_ipv4(device->ipconfig,
490 return __connman_error_failed(msg, -err);
492 return __connman_error_invalid_property(msg);
494 __connman_storage_save_device(device);
496 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
499 static void convert_name(const char *ssid, char *name,
500 unsigned int ssid_len)
504 for (i = 0; i < ssid_len; i++) {
505 if (g_ascii_isprint(ssid[i]))
512 static struct connman_network *find_network(struct connman_device *device,
513 const void *ssid, const char *address,
514 const char *security, const char *mode)
516 GHashTableIter network_iter;
522 g_hash_table_iter_init(&network_iter, device->networks);
523 while (g_hash_table_iter_next(&network_iter, &key, &value) == TRUE) {
524 const void *tmp_ssid;
525 const char *tmp_security, *tmp_mode, *tmp_address;
526 unsigned int tmp_ssid_size;
528 tmp_ssid = connman_network_get_blob(value, "WiFi.SSID",
530 tmp_security = connman_network_get_string(value,
532 tmp_mode = connman_network_get_string(value, "WiFi.Mode");
533 tmp_address = connman_network_get_string(value, "Address");
535 if (tmp_ssid_size < 1)
538 if (tmp_ssid && memcmp(ssid, tmp_ssid, tmp_ssid_size))
541 if (security && tmp_security &&
542 g_strcmp0(security, tmp_security) != 0)
545 if (mode && tmp_mode && g_strcmp0(mode, tmp_mode) != 0)
548 if (address && tmp_address &&
549 g_strcmp0(address, tmp_address) != 0)
552 return connman_network_ref(value);
558 static DBusMessage *join_network(DBusConnection *conn,
559 DBusMessage *msg, void *data)
561 struct connman_device *device = data;
562 struct connman_network *network, *found_network;
563 enum connman_network_type type;
564 unsigned int ssid_size;
565 const char *group, *mode, *security, *address;
567 DBusMessageIter iter, array;
570 DBG("conn %p", conn);
572 if (__connman_security_check_privilege(msg,
573 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
574 return __connman_error_permission_denied(msg);
576 if (!device->driver || !device->driver->join)
577 return __connman_error_not_supported(msg);
579 dbus_message_iter_init(msg, &iter);
580 dbus_message_iter_recurse(&iter, &array);
582 switch (device->type) {
583 case CONNMAN_DEVICE_TYPE_WIFI:
584 type = CONNMAN_NETWORK_TYPE_WIFI;
587 return __connman_error_not_supported(msg);
590 network = connman_network_create("00_00_00_00_00_00", type);
592 return __connman_error_failed(msg, ENOMEM);
594 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
595 DBusMessageIter entry, value;
596 const char *key, *str;
598 dbus_message_iter_recurse(&array, &entry);
599 dbus_message_iter_get_basic(&entry, &key);
601 dbus_message_iter_next(&entry);
602 dbus_message_iter_recurse(&entry, &value);
604 switch (dbus_message_iter_get_arg_type(&value)) {
605 case DBUS_TYPE_STRING:
606 dbus_message_iter_get_basic(&value, &str);
607 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
610 connman_network_set_blob(network, key,
612 name = g_try_malloc0(strlen(str) + 1);
614 return __connman_error_failed(msg,
617 convert_name((char *) str, name, strlen(str));
618 connman_network_set_name(network, name);
622 connman_network_set_string(network, key, str);
626 dbus_message_iter_next(&array);
629 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_size);
630 security = connman_network_get_string(network, "WiFi.Security");
631 mode = connman_network_get_string(network, "WiFi.Mode");
632 address = connman_network_get_string(network, "Address");
634 found_network = find_network(device, ssid, address, security, mode);
635 if (found_network != NULL) {
636 const char* passphrase;
638 passphrase = connman_network_get_string(network,
640 if (passphrase != NULL)
641 connman_network_set_string(found_network,
644 connman_network_unref(network);
645 network = found_network;
647 err = __connman_network_connect(found_network);
649 connman_network_unref(found_network);
652 return __connman_error_failed(msg, -err);
654 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
657 group = connman_wifi_build_group_name(ssid, ssid_size, mode, security);
659 index = connman_device_get_index(device);
660 connman_network_set_index(network, index);
662 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
664 if (connman_device_get_disconnected(device) == FALSE)
665 __connman_device_disconnect(device);
667 err = device->driver->join(device, network);
669 connman_network_unref(network);
672 return __connman_error_failed(msg, -err);
674 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
677 static DBusMessage *create_network(DBusConnection *conn,
678 DBusMessage *msg, void *data)
680 DBG("conn %p", conn);
682 if (__connman_security_check_privilege(msg,
683 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
684 return __connman_error_permission_denied(msg);
686 return __connman_error_invalid_arguments(msg);
689 static DBusMessage *remove_network(DBusConnection *conn,
690 DBusMessage *msg, void *data)
692 DBG("conn %p", conn);
694 if (__connman_security_check_privilege(msg,
695 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
696 return __connman_error_permission_denied(msg);
698 return __connman_error_invalid_arguments(msg);
701 static DBusMessage *propose_scan(DBusConnection *conn,
702 DBusMessage *msg, void *data)
704 struct connman_device *device = data;
707 DBG("conn %p", conn);
709 switch (device->mode) {
710 case CONNMAN_DEVICE_MODE_UNKNOWN:
711 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
712 return __connman_error_not_supported(msg);
713 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
714 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
718 err = __connman_device_scan(device);
720 return __connman_error_failed(msg, -err);
722 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
725 static GDBusMethodTable device_methods[] = {
726 { "GetProperties", "", "a{sv}", get_properties },
727 { "SetProperty", "sv", "", set_property,
728 G_DBUS_METHOD_FLAG_ASYNC },
729 { "JoinNetwork", "a{sv}", "", join_network },
730 { "CreateNetwork", "a{sv}", "o", create_network },
731 { "RemoveNetwork", "o", "", remove_network },
732 { "ProposeScan", "", "", propose_scan },
736 static GDBusSignalTable device_signals[] = {
737 { "PropertyChanged", "sv" },
741 static void append_devices(DBusMessageIter *entry)
743 DBusMessageIter value, iter;
744 const char *key = "Devices";
746 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
748 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
749 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
752 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
753 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
754 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
755 dbus_message_iter_close_container(&value, &iter);
757 dbus_message_iter_close_container(entry, &value);
760 static void emit_devices_signal(void)
763 DBusMessageIter entry;
765 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
766 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
770 dbus_message_iter_init_append(signal, &entry);
772 append_devices(&entry);
774 g_dbus_send_message(connection, signal);
777 static int register_interface(struct connman_element *element)
779 struct connman_device *device = element->device;
781 DBG("element %p name %s", element, element->name);
783 if (g_dbus_register_interface(connection, element->path,
784 CONNMAN_DEVICE_INTERFACE,
785 device_methods, device_signals,
786 NULL, device, NULL) == FALSE) {
787 connman_error("Failed to register %s device", element->path);
791 device->registered = TRUE;
793 emit_devices_signal();
798 static void unregister_interface(struct connman_element *element)
800 struct connman_device *device = element->device;
802 DBG("element %p name %s", element, element->name);
804 device->registered = FALSE;
806 emit_devices_signal();
808 g_dbus_unregister_interface(connection, element->path,
809 CONNMAN_DEVICE_INTERFACE);
812 static void device_enable(struct connman_device *device)
814 DBG("device %p", device);
816 if (device->powered == TRUE)
819 if (device->driver->enable) {
820 if (device->driver->enable(device) == 0)
821 device->powered = TRUE;
822 __connman_notifier_device_type_increase(device->type);
826 static void device_disable(struct connman_device *device)
828 DBG("device %p", device);
830 if (device->powered == FALSE)
833 g_hash_table_remove_all(device->networks);
835 if (device->driver->disable) {
836 if (device->driver->disable(device) == 0)
837 device->powered = FALSE;
838 __connman_notifier_device_type_decrease(device->type);
842 static int setup_device(struct connman_device *device)
846 DBG("device %p", device);
848 err = register_interface(&device->element);
850 if (device->driver->remove)
851 device->driver->remove(device);
852 device->driver = NULL;
856 switch (device->mode) {
857 case CONNMAN_DEVICE_MODE_UNKNOWN:
858 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
859 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
861 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
862 if (device->carrier == TRUE && device->secondary == FALSE)
863 __connman_profile_add_device(device);
867 device_enable(device);
872 static void probe_driver(struct connman_element *element, gpointer user_data)
874 struct connman_device_driver *driver = user_data;
876 DBG("element %p name %s", element, element->name);
878 if (element->device == NULL)
881 if (element->device->driver != NULL)
884 if (driver->type != element->device->type)
887 if (driver->probe(element->device) < 0)
890 element->device->driver = driver;
892 setup_device(element->device);
895 static void remove_device(struct connman_device *device)
897 DBG("device %p", device);
899 device_disable(device);
901 switch (device->mode) {
902 case CONNMAN_DEVICE_MODE_UNKNOWN:
903 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
904 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
906 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
907 if (device->secondary == FALSE)
908 __connman_profile_remove_device(device);
912 unregister_interface(&device->element);
914 if (device->driver->remove)
915 device->driver->remove(device);
917 device->driver = NULL;
920 static void remove_driver(struct connman_element *element, gpointer user_data)
922 struct connman_device_driver *driver = user_data;
924 DBG("element %p name %s", element, element->name);
926 if (element->device == NULL)
929 if (element->device->driver == driver)
930 remove_device(element->device);
933 connman_bool_t __connman_device_has_driver(struct connman_device *device)
935 if (device == NULL || device->driver == NULL)
938 return device->registered;
941 static GSList *driver_list = NULL;
943 static gint compare_priority(gconstpointer a, gconstpointer b)
945 const struct connman_device_driver *driver1 = a;
946 const struct connman_device_driver *driver2 = b;
948 return driver2->priority - driver1->priority;
952 * connman_device_driver_register:
953 * @driver: device driver definition
955 * Register a new device driver
957 * Returns: %0 on success
959 int connman_device_driver_register(struct connman_device_driver *driver)
961 DBG("driver %p name %s", driver, driver->name);
963 driver_list = g_slist_insert_sorted(driver_list, driver,
966 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
967 probe_driver, driver);
973 * connman_device_driver_unregister:
974 * @driver: device driver definition
976 * Remove a previously registered device driver
978 void connman_device_driver_unregister(struct connman_device_driver *driver)
980 DBG("driver %p name %s", driver, driver->name);
982 driver_list = g_slist_remove(driver_list, driver);
984 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
985 remove_driver, driver);
988 static void unregister_network(gpointer data)
990 struct connman_network *network = data;
992 DBG("network %p", network);
994 connman_element_unregister((struct connman_element *) network);
996 connman_network_unref(network);
999 static void device_destruct(struct connman_element *element)
1001 struct connman_device *device = element->device;
1003 DBG("element %p name %s", element, element->name);
1005 if (device->timeout > 0) {
1006 g_source_remove(device->timeout);
1007 device->timeout = 0;
1010 if (device->pending != NULL) {
1011 dbus_message_unref(device->pending);
1012 device->pending = NULL;
1015 g_free(device->ident);
1016 g_free(device->node);
1017 g_free(device->name);
1018 g_free(device->address);
1019 g_free(device->control);
1020 g_free(device->interface);
1022 connman_ipconfig_unref(device->ipconfig);
1024 g_free(device->last_network);
1026 g_hash_table_destroy(device->networks);
1027 device->networks = NULL;
1031 * connman_device_create:
1032 * @node: device node name (for example an address)
1033 * @type: device type
1035 * Allocate a new device of given #type and assign the #node name to it.
1037 * Returns: a newly-allocated #connman_device structure
1039 struct connman_device *connman_device_create(const char *node,
1040 enum connman_device_type type)
1042 struct connman_device *device;
1045 DBG("node %s type %d", node, type);
1047 device = g_try_new0(struct connman_device, 1);
1051 DBG("device %p", device);
1053 __connman_element_initialize(&device->element);
1055 device->element.name = g_strdup(node);
1056 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
1058 device->element.device = device;
1059 device->element.destruct = device_destruct;
1061 str = type2string(type);
1063 connman_element_set_string(&device->element,
1064 CONNMAN_PROPERTY_ID_TYPE, str);
1066 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1068 device->type = type;
1069 device->name = g_strdup(type2description(device->type));
1070 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
1071 device->secondary = FALSE;
1074 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1075 case CONNMAN_DEVICE_TYPE_VENDOR:
1076 device->scan_interval = 0;
1078 case CONNMAN_DEVICE_TYPE_ETHERNET:
1079 case CONNMAN_DEVICE_TYPE_WIFI:
1080 device->scan_interval = 300;
1082 case CONNMAN_DEVICE_TYPE_WIMAX:
1083 device->scan_interval = 0;
1085 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1086 device->scan_interval = 0;
1088 case CONNMAN_DEVICE_TYPE_GPS:
1089 device->scan_interval = 0;
1091 case CONNMAN_DEVICE_TYPE_MBM:
1092 case CONNMAN_DEVICE_TYPE_HSO:
1093 case CONNMAN_DEVICE_TYPE_NOZOMI:
1094 case CONNMAN_DEVICE_TYPE_HUAWEI:
1095 case CONNMAN_DEVICE_TYPE_NOVATEL:
1096 device->scan_interval = 0;
1100 device->ipconfig = connman_ipconfig_create();
1101 if (device->ipconfig == NULL) {
1102 connman_device_unref(device);
1106 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
1107 g_free, unregister_network);
1113 * connman_device_ref:
1114 * @device: device structure
1116 * Increase reference counter of device
1118 struct connman_device *connman_device_ref(struct connman_device *device)
1120 if (connman_element_ref(&device->element) == NULL)
1127 * connman_device_unref:
1128 * @device: device structure
1130 * Decrease reference counter of device
1132 void connman_device_unref(struct connman_device *device)
1134 connman_element_unref(&device->element);
1137 const char *__connman_device_get_type(struct connman_device *device)
1139 return type2string(device->type);
1143 * connman_device_get_type:
1144 * @device: device structure
1146 * Get type of device
1148 enum connman_device_type connman_device_get_type(struct connman_device *device)
1150 return device->type;
1154 * connman_device_get_name:
1155 * @device: device structure
1157 * Get unique name of device
1159 const char *connman_device_get_name(struct connman_device *device)
1161 return device->element.name;
1165 * connman_device_get_path:
1166 * @device: device structure
1168 * Get path name of device
1170 const char *connman_device_get_path(struct connman_device *device)
1172 return device->element.path;
1176 * connman_device_set_index:
1177 * @device: device structure
1178 * @index: index number
1180 * Set index number of device
1182 void connman_device_set_index(struct connman_device *device, int index)
1184 device->element.index = index;
1188 * connman_device_get_index:
1189 * @device: device structure
1191 * Get index number of device
1193 int connman_device_get_index(struct connman_device *device)
1195 return device->element.index;
1199 * connman_device_set_interface:
1200 * @device: device structure
1201 * @interface: interface name
1202 * @control: control interface
1204 * Set interface name of device
1206 void connman_device_set_interface(struct connman_device *device,
1207 const char *interface, const char *control)
1209 g_free(device->element.devname);
1210 device->element.devname = g_strdup(interface);
1212 g_free(device->interface);
1213 device->interface = g_strdup(interface);
1215 g_free(device->control);
1216 device->control = g_strdup(control);
1218 if (device->name == NULL) {
1219 const char *str = type2description(device->type);
1220 if (str != NULL && device->interface != NULL)
1221 device->name = g_strdup_printf("%s (%s)", str,
1226 const char *connman_device_get_control(struct connman_device *device)
1228 return device->control;
1232 * connman_device_set_ident:
1233 * @device: device structure
1234 * @ident: unique identifier
1236 * Set unique identifier of device
1238 void connman_device_set_ident(struct connman_device *device,
1241 g_free(device->ident);
1242 device->ident = g_strdup(ident);
1245 const char *__connman_device_get_ident(struct connman_device *device)
1247 return device->ident;
1251 * connman_device_set_mode:
1252 * @device: device structure
1253 * @mode: network mode
1255 * Change network mode of device
1257 void connman_device_set_mode(struct connman_device *device,
1258 enum connman_device_mode mode)
1260 device->mode = mode;
1264 * connman_device_get_mode:
1265 * @device: device structure
1267 * Get network mode of device
1269 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1271 return device->mode;
1275 * connman_device_set_secondary:
1276 * @device: device structure
1277 * @secondary: secondary value
1279 * Change secondary value of device
1281 void connman_device_set_secondary(struct connman_device *device,
1282 connman_bool_t secondary)
1284 device->secondary = secondary;
1288 * connman_device_get_secondary:
1289 * @device: device structure
1291 * Get secondary value of device
1293 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1295 return device->secondary;
1299 * connman_device_set_powered:
1300 * @device: device structure
1301 * @powered: powered state
1303 * Change power state of device
1305 int connman_device_set_powered(struct connman_device *device,
1306 connman_bool_t powered)
1308 DBG("driver %p powered %d", device, powered);
1310 if (device->timeout > 0) {
1311 g_source_remove(device->timeout);
1312 device->timeout = 0;
1315 if (device->pending != NULL) {
1316 g_dbus_send_reply(connection, device->pending,
1319 dbus_message_unref(device->pending);
1320 device->pending = NULL;
1323 if (device->powered == powered)
1326 device->powered = powered;
1328 if (device->registered == FALSE)
1331 powered_changed(device);
1333 if (powered == FALSE)
1336 if (device->scan_timeout > 0) {
1337 g_source_remove(device->scan_timeout);
1338 device->scan_timeout = 0;
1341 if (device->scan_interval > 0) {
1342 guint interval = device->scan_interval;
1343 device->scan_timeout = g_timeout_add_seconds(interval,
1344 device_scan_trigger, device);
1347 if (device->driver->scan)
1348 device->driver->scan(device);
1354 * connman_device_set_carrier:
1355 * @device: device structure
1356 * @carrier: carrier state
1358 * Change carrier state of device (only for device without scanning)
1360 int connman_device_set_carrier(struct connman_device *device,
1361 connman_bool_t carrier)
1363 DBG("device %p carrier %d", device, carrier);
1365 switch (device->mode) {
1366 case CONNMAN_DEVICE_MODE_UNKNOWN:
1367 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1368 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1370 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1374 if (device->carrier == carrier)
1377 device->carrier = carrier;
1379 return set_carrier(device, device->carrier);
1382 int __connman_device_scan(struct connman_device *device)
1384 if (!device->driver || !device->driver->scan)
1387 if (device->powered == FALSE)
1390 return device->driver->scan(device);
1393 int __connman_device_connect(struct connman_device *device)
1395 DBG("device %p", device);
1397 if (device->disconnected == FALSE)
1400 if (device->driver && device->driver->connect)
1401 device->driver->connect(device);
1406 int __connman_device_disconnect(struct connman_device *device)
1408 GHashTableIter iter;
1409 gpointer key, value;
1411 DBG("device %p", device);
1413 connman_device_set_disconnected(device, TRUE);
1415 g_hash_table_iter_init(&iter, device->networks);
1417 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1418 struct connman_network *network = value;
1420 if (connman_network_get_connected(network) == TRUE ||
1421 connman_network_get_associating(network) == TRUE)
1422 __connman_network_disconnect(network);
1425 if (device->driver && device->driver->disconnect)
1426 device->driver->disconnect(device);
1431 static void mark_network_unavailable(gpointer key, gpointer value,
1434 struct connman_network *network = value;
1436 if (connman_network_get_connected(network) == TRUE)
1439 connman_network_set_available(network, FALSE);
1442 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1445 struct connman_network *network = value;
1447 if (connman_network_get_connected(network) == TRUE)
1450 if (connman_network_get_available(network) == TRUE)
1457 * connman_device_set_scanning:
1458 * @device: device structure
1459 * @scanning: scanning state
1461 * Change scanning state of device
1463 int connman_device_set_scanning(struct connman_device *device,
1464 connman_bool_t scanning)
1466 DBusMessage *signal;
1467 DBusMessageIter entry, value;
1468 const char *key = "Scanning";
1470 DBG("device %p scanning %d", device, scanning);
1472 if (!device->driver || !device->driver->scan)
1475 if (device->scanning == scanning)
1478 device->scanning = scanning;
1480 signal = dbus_message_new_signal(device->element.path,
1481 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1485 dbus_message_iter_init_append(signal, &entry);
1487 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1489 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1490 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1491 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1492 dbus_message_iter_close_container(&entry, &value);
1494 g_dbus_send_message(connection, signal);
1496 if (scanning == TRUE) {
1497 if (device->scan_timeout > 0) {
1498 g_source_remove(device->scan_timeout);
1499 device->scan_timeout = 0;
1502 if (device->scan_interval > 0) {
1503 guint interval = device->scan_interval;
1504 device->scan_timeout = g_timeout_add_seconds(interval,
1505 device_scan_trigger, device);
1508 g_hash_table_foreach(device->networks,
1509 mark_network_unavailable, NULL);
1513 g_hash_table_foreach_remove(device->networks,
1514 remove_unavailable_network, NULL);
1516 if (device->connections > 0)
1519 if (device->disconnected == TRUE)
1526 * connman_device_set_disconnected:
1527 * @device: device structure
1528 * @disconnected: disconnected state
1530 * Change disconnected state of device (only for device with networks)
1532 int connman_device_set_disconnected(struct connman_device *device,
1533 connman_bool_t disconnected)
1535 DBG("device %p disconnected %d", device, disconnected);
1537 switch (device->mode) {
1538 case CONNMAN_DEVICE_MODE_UNKNOWN:
1539 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1541 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1542 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1546 if (device->disconnected == disconnected)
1549 device->disconnected = disconnected;
1555 * connman_device_get_disconnected:
1556 * @device: device structure
1558 * Get device disconnected state
1560 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1562 return device->disconnected;
1566 * connman_device_set_connected:
1567 * @device: device structure
1568 * @connected: connected state
1570 * Change connected state of device (for Ethernet like devices)
1572 int connman_device_set_connected(struct connman_device *device,
1573 connman_bool_t connected)
1575 DBG("device %p connected %d", device, connected);
1577 switch (device->mode) {
1578 case CONNMAN_DEVICE_MODE_UNKNOWN:
1579 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1580 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1582 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1586 if (device->carrier == FALSE)
1589 return set_connected(device, connected);
1593 * connman_device_set_string:
1594 * @device: device structure
1595 * @key: unique identifier
1596 * @value: string value
1598 * Set string value for specific key
1600 int connman_device_set_string(struct connman_device *device,
1601 const char *key, const char *value)
1603 DBG("device %p key %s value %s", device, key, value);
1605 if (g_str_equal(key, "Address") == TRUE) {
1606 g_free(device->address);
1607 device->address = g_strdup(value);
1608 } else if (g_str_equal(key, "Name") == TRUE) {
1609 g_free(device->name);
1610 device->name = g_strdup(value);
1611 } else if (g_str_equal(key, "Node") == TRUE) {
1612 g_free(device->node);
1613 device->node = g_strdup(value);
1616 return connman_element_set_string(&device->element, key, value);
1620 * connman_device_get_string:
1621 * @device: device structure
1622 * @key: unique identifier
1624 * Get string value for specific key
1626 const char *connman_device_get_string(struct connman_device *device,
1629 DBG("device %p key %s", device, key);
1631 if (g_str_equal(key, "Address") == TRUE)
1632 return device->address;
1633 else if (g_str_equal(key, "Name") == TRUE)
1634 return device->name;
1635 else if (g_str_equal(key, "Node") == TRUE)
1636 return device->node;
1638 return connman_element_get_string(&device->element, key);
1641 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1643 struct connman_device *device = element->device;
1644 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1645 connman_bool_t powered;
1647 DBG("element %p name %s", element, element->name);
1652 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1654 if (device->powered == powered)
1657 set_powered(device, powered);
1660 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1662 DBG("offlinmode %d", offlinemode);
1664 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1665 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1667 __connman_notifier_offline_mode(offlinemode);
1672 void __connman_device_increase_connections(struct connman_device *device)
1674 device->connections++;
1677 void __connman_device_decrease_connections(struct connman_device *device)
1679 device->connections--;
1683 * connman_device_add_network:
1684 * @device: device structure
1685 * @network: network structure
1687 * Add new network to the device
1689 int connman_device_add_network(struct connman_device *device,
1690 struct connman_network *network)
1692 const char *identifier = connman_network_get_identifier(network);
1695 DBG("device %p network %p", device, network);
1697 switch (device->mode) {
1698 case CONNMAN_DEVICE_MODE_UNKNOWN:
1699 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1701 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1702 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1706 __connman_network_set_device(network, device);
1708 __connman_storage_load_network(network);
1710 err = connman_element_register((struct connman_element *) network,
1713 __connman_network_set_device(network, NULL);
1717 g_hash_table_insert(device->networks, g_strdup(identifier),
1724 * connman_device_get_network:
1725 * @device: device structure
1726 * @identifier: network identifier
1728 * Get network for given identifier
1730 struct connman_network *connman_device_get_network(struct connman_device *device,
1731 const char *identifier)
1733 DBG("device %p identifier %s", device, identifier);
1735 return g_hash_table_lookup(device->networks, identifier);
1739 * connman_device_remove_network:
1740 * @device: device structure
1741 * @identifier: network identifier
1743 * Remove network for given identifier
1745 int connman_device_remove_network(struct connman_device *device,
1746 const char *identifier)
1748 DBG("device %p identifier %s", device, identifier);
1750 g_hash_table_remove(device->networks, identifier);
1755 void __connman_device_set_network(struct connman_device *device,
1756 struct connman_network *network)
1760 if (device->network == network)
1763 if (device->network != NULL)
1764 connman_network_unref(device->network);
1766 if (network != NULL) {
1767 name = connman_network_get_string(network,
1768 CONNMAN_PROPERTY_ID_NAME);
1769 g_free(device->last_network);
1770 device->last_network = g_strdup(name);
1772 device->network = connman_network_ref(network);
1774 g_free(device->last_network);
1775 device->last_network = NULL;
1777 device->network = NULL;
1782 * connman_device_register:
1783 * @device: device structure
1785 * Register device with the system
1787 int connman_device_register(struct connman_device *device)
1789 __connman_storage_load_device(device);
1791 switch (device->mode) {
1792 case CONNMAN_DEVICE_MODE_UNKNOWN:
1793 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1795 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1796 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1797 __connman_storage_init_network(device);
1801 __connman_notifier_device_type_register(device->type);
1803 return connman_element_register(&device->element, NULL);
1807 * connman_device_unregister:
1808 * @device: device structure
1810 * Unregister device with the system
1812 void connman_device_unregister(struct connman_device *device)
1814 __connman_storage_save_device(device);
1816 __connman_notifier_device_type_unregister(device->type);
1818 connman_element_unregister(&device->element);
1822 * connman_device_get_data:
1823 * @device: device structure
1825 * Get private device data pointer
1827 void *connman_device_get_data(struct connman_device *device)
1829 return device->driver_data;
1833 * connman_device_set_data:
1834 * @device: device structure
1835 * @data: data pointer
1837 * Set private device data pointer
1839 void connman_device_set_data(struct connman_device *device, void *data)
1841 device->driver_data = data;
1844 static gboolean match_driver(struct connman_device *device,
1845 struct connman_device_driver *driver)
1847 if (device->type == driver->type ||
1848 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1854 static int device_probe(struct connman_element *element)
1856 struct connman_device *device = element->device;
1859 DBG("element %p name %s", element, element->name);
1864 if (device->driver != NULL)
1867 for (list = driver_list; list; list = list->next) {
1868 struct connman_device_driver *driver = list->data;
1870 if (match_driver(device, driver) == FALSE)
1873 DBG("driver %p name %s", driver, driver->name);
1875 if (driver->probe(device) == 0) {
1876 device->driver = driver;
1881 if (device->driver == NULL)
1884 return setup_device(device);
1887 static void device_remove(struct connman_element *element)
1889 struct connman_device *device = element->device;
1891 DBG("element %p name %s", element, element->name);
1896 if (device->driver == NULL)
1899 remove_device(device);
1902 static struct connman_driver device_driver = {
1904 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1905 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1906 .probe = device_probe,
1907 .remove = device_remove,
1910 static int device_load(struct connman_device *device)
1913 gchar *pathname, *identifier, *data = NULL;
1917 DBG("device %p", device);
1919 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1920 __connman_profile_active_ident());
1921 if (pathname == NULL)
1924 keyfile = g_key_file_new();
1926 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1933 if (g_key_file_load_from_data(keyfile, data, length,
1934 0, NULL) == FALSE) {
1941 identifier = g_strdup_printf("device_%s", device->element.name);
1942 if (identifier == NULL)
1945 switch (device->mode) {
1946 case CONNMAN_DEVICE_MODE_UNKNOWN:
1947 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1949 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1950 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1951 val = g_key_file_get_integer(keyfile, identifier,
1952 "ScanInterval", NULL);
1954 device->scan_interval = val;
1959 g_key_file_free(keyfile);
1966 static int device_save(struct connman_device *device)
1969 gchar *pathname, *identifier = NULL, *data = NULL;
1972 DBG("device %p", device);
1974 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1975 __connman_profile_active_ident());
1976 if (pathname == NULL)
1979 keyfile = g_key_file_new();
1981 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1985 if (g_key_file_load_from_data(keyfile, data, length,
1993 identifier = g_strdup_printf("device_%s", device->element.name);
1994 if (identifier == NULL)
1997 switch (device->mode) {
1998 case CONNMAN_DEVICE_MODE_UNKNOWN:
1999 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
2001 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
2002 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
2003 if (device->scan_interval > 0)
2004 g_key_file_set_integer(keyfile, identifier,
2005 "ScanInterval", device->scan_interval);
2009 data = g_key_file_to_data(keyfile, &length, NULL);
2011 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2012 connman_error("Failed to store device information");
2017 g_key_file_free(keyfile);
2025 static struct connman_storage device_storage = {
2027 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2028 .device_load = device_load,
2029 .device_save = device_save,
2032 int __connman_device_init(void)
2036 connection = connman_dbus_get_connection();
2038 if (connman_storage_register(&device_storage) < 0)
2039 connman_error("Failed to register device storage");
2041 return connman_driver_register(&device_driver);
2044 void __connman_device_cleanup(void)
2048 connman_driver_unregister(&device_driver);
2050 connman_storage_unregister(&device_storage);
2052 dbus_connection_unref(connection);