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 void reset_scan_trigger(struct connman_device *device)
87 if (device->scan_timeout > 0) {
88 g_source_remove(device->scan_timeout);
89 device->scan_timeout = 0;
92 if (device->scan_interval > 0) {
93 guint interval = device->scan_interval;
94 device->scan_timeout = g_timeout_add_seconds(interval,
95 device_scan_trigger, device);
99 static const char *type2description(enum connman_device_type type)
102 case CONNMAN_DEVICE_TYPE_UNKNOWN:
103 case CONNMAN_DEVICE_TYPE_VENDOR:
105 case CONNMAN_DEVICE_TYPE_ETHERNET:
107 case CONNMAN_DEVICE_TYPE_WIFI:
109 case CONNMAN_DEVICE_TYPE_WIMAX:
111 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
113 case CONNMAN_DEVICE_TYPE_GPS:
115 case CONNMAN_DEVICE_TYPE_MBM:
116 case CONNMAN_DEVICE_TYPE_HSO:
117 case CONNMAN_DEVICE_TYPE_NOZOMI:
118 case CONNMAN_DEVICE_TYPE_HUAWEI:
119 case CONNMAN_DEVICE_TYPE_NOVATEL:
126 static const char *type2string(enum connman_device_type type)
129 case CONNMAN_DEVICE_TYPE_UNKNOWN:
130 case CONNMAN_DEVICE_TYPE_VENDOR:
132 case CONNMAN_DEVICE_TYPE_ETHERNET:
134 case CONNMAN_DEVICE_TYPE_WIFI:
136 case CONNMAN_DEVICE_TYPE_WIMAX:
138 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
140 case CONNMAN_DEVICE_TYPE_GPS:
142 case CONNMAN_DEVICE_TYPE_MBM:
143 case CONNMAN_DEVICE_TYPE_HSO:
144 case CONNMAN_DEVICE_TYPE_HUAWEI:
145 case CONNMAN_DEVICE_TYPE_NOZOMI:
146 case CONNMAN_DEVICE_TYPE_NOVATEL:
153 static int set_connected(struct connman_device *device,
154 connman_bool_t connected)
156 if (connected == TRUE) {
157 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
158 struct connman_element *element;
160 device->disconnected = TRUE;
162 switch (device->element.ipv4.method) {
163 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
164 case CONNMAN_IPCONFIG_METHOD_OFF:
166 case CONNMAN_IPCONFIG_METHOD_STATIC:
167 type = CONNMAN_ELEMENT_TYPE_IPV4;
169 case CONNMAN_IPCONFIG_METHOD_DHCP:
170 type = CONNMAN_ELEMENT_TYPE_DHCP;
174 element = connman_element_create(NULL);
175 if (element != NULL) {
176 struct connman_service *service;
178 element->type = type;
179 element->index = device->element.index;
181 if (connman_element_register(element,
182 &device->element) < 0)
183 connman_element_unref(element);
185 device->disconnected = FALSE;
187 service = __connman_service_lookup_from_device(device);
188 __connman_service_indicate_state(service,
189 CONNMAN_SERVICE_STATE_CONFIGURATION);
192 struct connman_service *service;
194 connman_element_unregister_children(&device->element);
196 device->disconnected = TRUE;
198 service = __connman_service_lookup_from_device(device);
199 __connman_service_indicate_state(service,
200 CONNMAN_SERVICE_STATE_IDLE);
206 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
208 struct connman_service *service;
211 __connman_profile_add_device(device);
213 __connman_profile_remove_device(device);
215 service = __connman_service_lookup_from_device(device);
216 __connman_service_set_carrier(service, carrier);
218 return set_connected(device, carrier);
221 static int powered_changed(struct connman_device *device)
224 DBusMessageIter entry, value;
225 const char *key = "Powered";
227 signal = dbus_message_new_signal(device->element.path,
228 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
232 dbus_message_iter_init_append(signal, &entry);
234 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
236 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
237 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
238 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
240 dbus_message_iter_close_container(&entry, &value);
242 g_dbus_send_message(connection, signal);
247 static int set_powered(struct connman_device *device, connman_bool_t powered)
249 struct connman_device_driver *driver = device->driver;
252 DBG("device %p powered %d", device, powered);
254 if (device->powered == powered)
260 if (powered == TRUE) {
261 if (driver->enable) {
262 err = driver->enable(device);
263 __connman_notifier_device_type_increase(device->type);
267 g_hash_table_remove_all(device->networks);
269 set_carrier(device, FALSE);
271 if (driver->disable) {
272 err = driver->disable(device);
273 __connman_notifier_device_type_decrease(device->type);
279 device->powered = powered;
280 powered_changed(device);
286 static void append_path(gpointer key, gpointer value, gpointer user_data)
288 struct connman_element *element = value;
289 DBusMessageIter *iter = user_data;
291 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
295 static void append_networks(struct connman_device *device,
296 DBusMessageIter *entry)
298 DBusMessageIter value, iter;
299 const char *key = "Networks";
301 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
303 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
304 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
307 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
308 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
309 g_hash_table_foreach(device->networks, append_path, &iter);
310 dbus_message_iter_close_container(&value, &iter);
312 dbus_message_iter_close_container(entry, &value);
315 static DBusMessage *get_properties(DBusConnection *conn,
316 DBusMessage *msg, void *data)
318 struct connman_device *device = data;
320 DBusMessageIter array, dict, entry;
323 DBG("conn %p", conn);
325 if (__connman_security_check_privilege(msg,
326 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
327 return __connman_error_permission_denied(msg);
329 reply = dbus_message_new_method_return(msg);
333 dbus_message_iter_init_append(reply, &array);
335 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
336 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
337 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
338 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
340 if (device->name != NULL)
341 connman_dbus_dict_append_variant(&dict, "Name",
342 DBUS_TYPE_STRING, &device->name);
344 str = type2string(device->type);
346 connman_dbus_dict_append_variant(&dict, "Type",
347 DBUS_TYPE_STRING, &str);
349 if (device->address != NULL)
350 connman_dbus_dict_append_variant(&dict, "Address",
351 DBUS_TYPE_STRING, &device->address);
353 if (device->interface != NULL)
354 connman_dbus_dict_append_variant(&dict, "Interface",
355 DBUS_TYPE_STRING, &device->interface);
357 connman_dbus_dict_append_variant(&dict, "Powered",
358 DBUS_TYPE_BOOLEAN, &device->powered);
360 if (device->driver && device->driver->scan)
361 connman_dbus_dict_append_variant(&dict, "Scanning",
362 DBUS_TYPE_BOOLEAN, &device->scanning);
364 switch (device->mode) {
365 case CONNMAN_DEVICE_MODE_UNKNOWN:
367 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
368 __connman_element_append_ipv4(&device->element, &dict);
370 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
371 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
372 if (device->scan_interval > 0)
373 connman_dbus_dict_append_variant(&dict, "ScanInterval",
374 DBUS_TYPE_UINT16, &device->scan_interval);
376 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
378 append_networks(device, &entry);
379 dbus_message_iter_close_container(&dict, &entry);
383 dbus_message_iter_close_container(&array, &dict);
388 static gboolean powered_timeout(gpointer user_data)
390 struct connman_device *device = user_data;
392 DBG("device %p", device);
396 if (device->pending != NULL) {
399 reply = __connman_error_operation_timeout(device->pending);
401 g_dbus_send_message(connection, reply);
403 dbus_message_unref(device->pending);
404 device->pending = NULL;
410 static DBusMessage *set_property(DBusConnection *conn,
411 DBusMessage *msg, void *data)
413 struct connman_device *device = data;
414 DBusMessageIter iter, value;
418 DBG("conn %p", conn);
420 if (dbus_message_iter_init(msg, &iter) == FALSE)
421 return __connman_error_invalid_arguments(msg);
423 dbus_message_iter_get_basic(&iter, &name);
424 dbus_message_iter_next(&iter);
425 dbus_message_iter_recurse(&iter, &value);
427 if (__connman_security_check_privilege(msg,
428 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
429 return __connman_error_permission_denied(msg);
431 type = dbus_message_iter_get_arg_type(&value);
433 if (g_str_equal(name, "Powered") == TRUE) {
434 connman_bool_t powered;
437 if (type != DBUS_TYPE_BOOLEAN)
438 return __connman_error_invalid_arguments(msg);
440 dbus_message_iter_get_basic(&value, &powered);
442 if (device->powered == powered)
443 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
445 if (device->pending != NULL)
446 return __connman_error_in_progress(msg);
448 err = set_powered(device, powered);
450 if (err != -EINPROGRESS)
451 return __connman_error_failed(msg, -err);
453 device->pending = dbus_message_ref(msg);
455 device->timeout = g_timeout_add_seconds(15,
456 powered_timeout, device);
460 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
461 connman_uint16_t interval;
463 switch (device->mode) {
464 case CONNMAN_DEVICE_MODE_UNKNOWN:
465 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
466 return __connman_error_invalid_arguments(msg);
467 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
468 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
472 if (type != DBUS_TYPE_UINT16)
473 return __connman_error_invalid_arguments(msg);
475 dbus_message_iter_get_basic(&value, &interval);
477 if (device->scan_interval != interval) {
478 device->scan_interval = interval;
480 reset_scan_trigger(device);
482 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
485 switch (device->mode) {
486 case CONNMAN_DEVICE_MODE_UNKNOWN:
487 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
488 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
489 return __connman_error_invalid_arguments(msg);
490 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
494 err = __connman_ipconfig_set_ipv4(device->ipconfig,
497 return __connman_error_failed(msg, -err);
499 return __connman_error_invalid_property(msg);
501 __connman_storage_save_device(device);
503 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
506 static void convert_name(const char *ssid, char *name,
507 unsigned int ssid_len)
511 for (i = 0; i < ssid_len; i++) {
512 if (g_ascii_isprint(ssid[i]))
519 static struct connman_network *find_network(struct connman_device *device,
520 const void *ssid, const char *address,
521 const char *security, const char *mode)
523 GHashTableIter network_iter;
529 g_hash_table_iter_init(&network_iter, device->networks);
530 while (g_hash_table_iter_next(&network_iter, &key, &value) == TRUE) {
531 const void *tmp_ssid;
532 const char *tmp_security, *tmp_mode, *tmp_address;
533 unsigned int tmp_ssid_size;
535 tmp_ssid = connman_network_get_blob(value, "WiFi.SSID",
537 tmp_security = connman_network_get_string(value,
539 tmp_mode = connman_network_get_string(value, "WiFi.Mode");
540 tmp_address = connman_network_get_string(value, "Address");
542 if (tmp_ssid_size < 1)
545 if (tmp_ssid && memcmp(ssid, tmp_ssid, tmp_ssid_size))
548 if (security && tmp_security &&
549 g_strcmp0(security, tmp_security) != 0)
552 if (mode && tmp_mode && g_strcmp0(mode, tmp_mode) != 0)
555 if (address && tmp_address &&
556 g_strcmp0(address, tmp_address) != 0)
559 return connman_network_ref(value);
565 static DBusMessage *join_network(DBusConnection *conn,
566 DBusMessage *msg, void *data)
568 struct connman_device *device = data;
569 struct connman_network *network, *found_network;
570 enum connman_network_type type;
571 unsigned int ssid_size;
572 const char *group, *mode, *security, *address;
574 DBusMessageIter iter, array;
577 DBG("conn %p", conn);
579 if (__connman_security_check_privilege(msg,
580 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
581 return __connman_error_permission_denied(msg);
583 if (!device->driver || !device->driver->join)
584 return __connman_error_not_supported(msg);
586 dbus_message_iter_init(msg, &iter);
587 dbus_message_iter_recurse(&iter, &array);
589 switch (device->type) {
590 case CONNMAN_DEVICE_TYPE_WIFI:
591 type = CONNMAN_NETWORK_TYPE_WIFI;
594 return __connman_error_not_supported(msg);
597 network = connman_network_create("00_00_00_00_00_00", type);
599 return __connman_error_failed(msg, ENOMEM);
601 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
602 DBusMessageIter entry, value;
603 const char *key, *str;
605 dbus_message_iter_recurse(&array, &entry);
606 dbus_message_iter_get_basic(&entry, &key);
608 dbus_message_iter_next(&entry);
609 dbus_message_iter_recurse(&entry, &value);
611 switch (dbus_message_iter_get_arg_type(&value)) {
612 case DBUS_TYPE_STRING:
613 dbus_message_iter_get_basic(&value, &str);
614 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
617 connman_network_set_blob(network, key,
619 name = g_try_malloc0(strlen(str) + 1);
621 return __connman_error_failed(msg,
624 convert_name((char *) str, name, strlen(str));
625 connman_network_set_name(network, name);
629 connman_network_set_string(network, key, str);
633 dbus_message_iter_next(&array);
636 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_size);
637 security = connman_network_get_string(network, "WiFi.Security");
638 mode = connman_network_get_string(network, "WiFi.Mode");
639 address = connman_network_get_string(network, "Address");
641 found_network = find_network(device, ssid, address, security, mode);
642 if (found_network != NULL) {
643 const char* passphrase;
645 passphrase = connman_network_get_string(network,
647 if (passphrase != NULL)
648 connman_network_set_string(found_network,
651 connman_network_unref(network);
652 network = found_network;
654 err = __connman_network_connect(found_network);
656 connman_network_unref(found_network);
659 return __connman_error_failed(msg, -err);
661 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
664 group = connman_wifi_build_group_name(ssid, ssid_size, mode, security);
666 index = connman_device_get_index(device);
667 connman_network_set_index(network, index);
669 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
671 if (connman_device_get_disconnected(device) == FALSE)
672 __connman_device_disconnect(device);
674 err = device->driver->join(device, network);
676 connman_network_unref(network);
679 return __connman_error_failed(msg, -err);
681 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
684 static DBusMessage *create_network(DBusConnection *conn,
685 DBusMessage *msg, void *data)
687 DBG("conn %p", conn);
689 if (__connman_security_check_privilege(msg,
690 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
691 return __connman_error_permission_denied(msg);
693 return __connman_error_invalid_arguments(msg);
696 static DBusMessage *remove_network(DBusConnection *conn,
697 DBusMessage *msg, void *data)
699 DBG("conn %p", conn);
701 if (__connman_security_check_privilege(msg,
702 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
703 return __connman_error_permission_denied(msg);
705 return __connman_error_invalid_arguments(msg);
708 static DBusMessage *propose_scan(DBusConnection *conn,
709 DBusMessage *msg, void *data)
711 struct connman_device *device = data;
714 DBG("conn %p", conn);
716 switch (device->mode) {
717 case CONNMAN_DEVICE_MODE_UNKNOWN:
718 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
719 return __connman_error_not_supported(msg);
720 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
721 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
725 err = __connman_device_scan(device);
727 return __connman_error_failed(msg, -err);
729 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
732 static GDBusMethodTable device_methods[] = {
733 { "GetProperties", "", "a{sv}", get_properties },
734 { "SetProperty", "sv", "", set_property,
735 G_DBUS_METHOD_FLAG_ASYNC },
736 { "JoinNetwork", "a{sv}", "", join_network },
737 { "CreateNetwork", "a{sv}", "o", create_network },
738 { "RemoveNetwork", "o", "", remove_network },
739 { "ProposeScan", "", "", propose_scan },
743 static GDBusSignalTable device_signals[] = {
744 { "PropertyChanged", "sv" },
748 static void append_devices(DBusMessageIter *entry)
750 DBusMessageIter value, iter;
751 const char *key = "Devices";
753 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
755 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
756 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
759 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
760 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
761 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
762 dbus_message_iter_close_container(&value, &iter);
764 dbus_message_iter_close_container(entry, &value);
767 static void emit_devices_signal(void)
770 DBusMessageIter entry;
772 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
773 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
777 dbus_message_iter_init_append(signal, &entry);
779 append_devices(&entry);
781 g_dbus_send_message(connection, signal);
784 static int register_interface(struct connman_element *element)
786 struct connman_device *device = element->device;
788 DBG("element %p name %s", element, element->name);
790 if (g_dbus_register_interface(connection, element->path,
791 CONNMAN_DEVICE_INTERFACE,
792 device_methods, device_signals,
793 NULL, device, NULL) == FALSE) {
794 connman_error("Failed to register %s device", element->path);
798 device->registered = TRUE;
800 emit_devices_signal();
805 static void unregister_interface(struct connman_element *element)
807 struct connman_device *device = element->device;
809 DBG("element %p name %s", element, element->name);
811 device->registered = FALSE;
813 emit_devices_signal();
815 g_dbus_unregister_interface(connection, element->path,
816 CONNMAN_DEVICE_INTERFACE);
819 static void device_enable(struct connman_device *device)
821 DBG("device %p", device);
823 if (device->powered == TRUE)
826 if (device->driver->enable) {
827 if (device->driver->enable(device) == 0)
828 device->powered = TRUE;
829 __connman_notifier_device_type_increase(device->type);
833 static void device_disable(struct connman_device *device)
835 DBG("device %p", device);
837 if (device->powered == FALSE)
840 g_hash_table_remove_all(device->networks);
842 if (device->driver->disable) {
843 if (device->driver->disable(device) == 0)
844 device->powered = FALSE;
845 __connman_notifier_device_type_decrease(device->type);
849 static int setup_device(struct connman_device *device)
853 DBG("device %p", device);
855 err = register_interface(&device->element);
857 if (device->driver->remove)
858 device->driver->remove(device);
859 device->driver = NULL;
863 switch (device->mode) {
864 case CONNMAN_DEVICE_MODE_UNKNOWN:
865 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
866 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
868 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
869 if (device->carrier == TRUE && device->secondary == FALSE)
870 __connman_profile_add_device(device);
874 device_enable(device);
879 static void probe_driver(struct connman_element *element, gpointer user_data)
881 struct connman_device_driver *driver = user_data;
883 DBG("element %p name %s", element, element->name);
885 if (element->device == NULL)
888 if (element->device->driver != NULL)
891 if (driver->type != element->device->type)
894 if (driver->probe(element->device) < 0)
897 element->device->driver = driver;
899 setup_device(element->device);
902 static void remove_device(struct connman_device *device)
904 DBG("device %p", device);
906 device_disable(device);
908 switch (device->mode) {
909 case CONNMAN_DEVICE_MODE_UNKNOWN:
910 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
911 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
913 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
914 if (device->secondary == FALSE)
915 __connman_profile_remove_device(device);
919 unregister_interface(&device->element);
921 if (device->driver->remove)
922 device->driver->remove(device);
924 device->driver = NULL;
927 static void remove_driver(struct connman_element *element, gpointer user_data)
929 struct connman_device_driver *driver = user_data;
931 DBG("element %p name %s", element, element->name);
933 if (element->device == NULL)
936 if (element->device->driver == driver)
937 remove_device(element->device);
940 connman_bool_t __connman_device_has_driver(struct connman_device *device)
942 if (device == NULL || device->driver == NULL)
945 return device->registered;
948 static GSList *driver_list = NULL;
950 static gint compare_priority(gconstpointer a, gconstpointer b)
952 const struct connman_device_driver *driver1 = a;
953 const struct connman_device_driver *driver2 = b;
955 return driver2->priority - driver1->priority;
959 * connman_device_driver_register:
960 * @driver: device driver definition
962 * Register a new device driver
964 * Returns: %0 on success
966 int connman_device_driver_register(struct connman_device_driver *driver)
968 DBG("driver %p name %s", driver, driver->name);
970 driver_list = g_slist_insert_sorted(driver_list, driver,
973 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
974 probe_driver, driver);
980 * connman_device_driver_unregister:
981 * @driver: device driver definition
983 * Remove a previously registered device driver
985 void connman_device_driver_unregister(struct connman_device_driver *driver)
987 DBG("driver %p name %s", driver, driver->name);
989 driver_list = g_slist_remove(driver_list, driver);
991 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
992 remove_driver, driver);
995 static void unregister_network(gpointer data)
997 struct connman_network *network = data;
999 DBG("network %p", network);
1001 connman_element_unregister((struct connman_element *) network);
1003 connman_network_unref(network);
1006 static void device_destruct(struct connman_element *element)
1008 struct connman_device *device = element->device;
1010 DBG("element %p name %s", element, element->name);
1012 if (device->timeout > 0) {
1013 g_source_remove(device->timeout);
1014 device->timeout = 0;
1017 if (device->pending != NULL) {
1018 dbus_message_unref(device->pending);
1019 device->pending = NULL;
1022 g_free(device->ident);
1023 g_free(device->node);
1024 g_free(device->name);
1025 g_free(device->address);
1026 g_free(device->control);
1027 g_free(device->interface);
1029 connman_ipconfig_unref(device->ipconfig);
1031 g_free(device->last_network);
1033 g_hash_table_destroy(device->networks);
1034 device->networks = NULL;
1038 * connman_device_create:
1039 * @node: device node name (for example an address)
1040 * @type: device type
1042 * Allocate a new device of given #type and assign the #node name to it.
1044 * Returns: a newly-allocated #connman_device structure
1046 struct connman_device *connman_device_create(const char *node,
1047 enum connman_device_type type)
1049 struct connman_device *device;
1052 DBG("node %s type %d", node, type);
1054 device = g_try_new0(struct connman_device, 1);
1058 DBG("device %p", device);
1060 __connman_element_initialize(&device->element);
1062 device->element.name = g_strdup(node);
1063 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
1065 device->element.device = device;
1066 device->element.destruct = device_destruct;
1068 str = type2string(type);
1070 connman_element_set_string(&device->element,
1071 CONNMAN_PROPERTY_ID_TYPE, str);
1073 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1075 device->type = type;
1076 device->name = g_strdup(type2description(device->type));
1077 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
1078 device->secondary = FALSE;
1081 case CONNMAN_DEVICE_TYPE_UNKNOWN:
1082 case CONNMAN_DEVICE_TYPE_VENDOR:
1083 device->scan_interval = 0;
1085 case CONNMAN_DEVICE_TYPE_ETHERNET:
1086 case CONNMAN_DEVICE_TYPE_WIFI:
1087 device->scan_interval = 300;
1089 case CONNMAN_DEVICE_TYPE_WIMAX:
1090 device->scan_interval = 0;
1092 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1093 device->scan_interval = 0;
1095 case CONNMAN_DEVICE_TYPE_GPS:
1096 device->scan_interval = 0;
1098 case CONNMAN_DEVICE_TYPE_MBM:
1099 case CONNMAN_DEVICE_TYPE_HSO:
1100 case CONNMAN_DEVICE_TYPE_NOZOMI:
1101 case CONNMAN_DEVICE_TYPE_HUAWEI:
1102 case CONNMAN_DEVICE_TYPE_NOVATEL:
1103 device->scan_interval = 0;
1107 device->ipconfig = connman_ipconfig_create();
1108 if (device->ipconfig == NULL) {
1109 connman_device_unref(device);
1113 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
1114 g_free, unregister_network);
1120 * connman_device_ref:
1121 * @device: device structure
1123 * Increase reference counter of device
1125 struct connman_device *connman_device_ref(struct connman_device *device)
1127 if (connman_element_ref(&device->element) == NULL)
1134 * connman_device_unref:
1135 * @device: device structure
1137 * Decrease reference counter of device
1139 void connman_device_unref(struct connman_device *device)
1141 connman_element_unref(&device->element);
1144 const char *__connman_device_get_type(struct connman_device *device)
1146 return type2string(device->type);
1150 * connman_device_get_type:
1151 * @device: device structure
1153 * Get type of device
1155 enum connman_device_type connman_device_get_type(struct connman_device *device)
1157 return device->type;
1161 * connman_device_get_name:
1162 * @device: device structure
1164 * Get unique name of device
1166 const char *connman_device_get_name(struct connman_device *device)
1168 return device->element.name;
1172 * connman_device_get_path:
1173 * @device: device structure
1175 * Get path name of device
1177 const char *connman_device_get_path(struct connman_device *device)
1179 return device->element.path;
1183 * connman_device_set_index:
1184 * @device: device structure
1185 * @index: index number
1187 * Set index number of device
1189 void connman_device_set_index(struct connman_device *device, int index)
1191 device->element.index = index;
1195 * connman_device_get_index:
1196 * @device: device structure
1198 * Get index number of device
1200 int connman_device_get_index(struct connman_device *device)
1202 return device->element.index;
1206 * connman_device_set_interface:
1207 * @device: device structure
1208 * @interface: interface name
1209 * @control: control interface
1211 * Set interface name of device
1213 void connman_device_set_interface(struct connman_device *device,
1214 const char *interface, const char *control)
1216 g_free(device->element.devname);
1217 device->element.devname = g_strdup(interface);
1219 g_free(device->interface);
1220 device->interface = g_strdup(interface);
1222 g_free(device->control);
1223 device->control = g_strdup(control);
1225 if (device->name == NULL) {
1226 const char *str = type2description(device->type);
1227 if (str != NULL && device->interface != NULL)
1228 device->name = g_strdup_printf("%s (%s)", str,
1233 const char *connman_device_get_control(struct connman_device *device)
1235 return device->control;
1239 * connman_device_set_ident:
1240 * @device: device structure
1241 * @ident: unique identifier
1243 * Set unique identifier of device
1245 void connman_device_set_ident(struct connman_device *device,
1248 g_free(device->ident);
1249 device->ident = g_strdup(ident);
1252 const char *__connman_device_get_ident(struct connman_device *device)
1254 return device->ident;
1258 * connman_device_set_mode:
1259 * @device: device structure
1260 * @mode: network mode
1262 * Change network mode of device
1264 void connman_device_set_mode(struct connman_device *device,
1265 enum connman_device_mode mode)
1267 device->mode = mode;
1271 * connman_device_get_mode:
1272 * @device: device structure
1274 * Get network mode of device
1276 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1278 return device->mode;
1282 * connman_device_set_secondary:
1283 * @device: device structure
1284 * @secondary: secondary value
1286 * Change secondary value of device
1288 void connman_device_set_secondary(struct connman_device *device,
1289 connman_bool_t secondary)
1291 device->secondary = secondary;
1295 * connman_device_get_secondary:
1296 * @device: device structure
1298 * Get secondary value of device
1300 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1302 return device->secondary;
1306 * connman_device_set_powered:
1307 * @device: device structure
1308 * @powered: powered state
1310 * Change power state of device
1312 int connman_device_set_powered(struct connman_device *device,
1313 connman_bool_t powered)
1315 DBG("driver %p powered %d", device, powered);
1317 if (device->timeout > 0) {
1318 g_source_remove(device->timeout);
1319 device->timeout = 0;
1322 if (device->pending != NULL) {
1323 g_dbus_send_reply(connection, device->pending,
1326 dbus_message_unref(device->pending);
1327 device->pending = NULL;
1330 if (device->powered == powered)
1333 device->powered = powered;
1335 if (device->registered == FALSE)
1338 powered_changed(device);
1340 if (powered == FALSE)
1343 reset_scan_trigger(device);
1345 if (device->driver->scan)
1346 device->driver->scan(device);
1352 * connman_device_set_carrier:
1353 * @device: device structure
1354 * @carrier: carrier state
1356 * Change carrier state of device (only for device without scanning)
1358 int connman_device_set_carrier(struct connman_device *device,
1359 connman_bool_t carrier)
1361 DBG("device %p carrier %d", device, carrier);
1363 switch (device->mode) {
1364 case CONNMAN_DEVICE_MODE_UNKNOWN:
1365 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1366 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1368 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1372 if (device->carrier == carrier)
1375 device->carrier = carrier;
1377 return set_carrier(device, device->carrier);
1380 int __connman_device_scan(struct connman_device *device)
1382 if (!device->driver || !device->driver->scan)
1385 if (device->powered == FALSE)
1388 return device->driver->scan(device);
1391 int __connman_device_enable(struct connman_device *device)
1393 if (!device->driver || !device->driver->enable)
1396 if (device->powered == TRUE)
1399 device_enable(device);
1404 int __connman_device_disable(struct connman_device *device)
1406 if (!device->driver || !device->driver->disable)
1409 if (device->powered == FALSE)
1412 device_disable(device);
1417 int __connman_device_connect(struct connman_device *device)
1419 DBG("device %p", device);
1421 if (device->disconnected == FALSE)
1424 if (device->driver && device->driver->connect)
1425 device->driver->connect(device);
1430 int __connman_device_disconnect(struct connman_device *device)
1432 GHashTableIter iter;
1433 gpointer key, value;
1435 DBG("device %p", device);
1437 connman_device_set_disconnected(device, TRUE);
1439 g_hash_table_iter_init(&iter, device->networks);
1441 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1442 struct connman_network *network = value;
1444 __connman_network_disconnect(network);
1447 if (device->driver && device->driver->disconnect)
1448 device->driver->disconnect(device);
1453 static void mark_network_unavailable(gpointer key, gpointer value,
1456 struct connman_network *network = value;
1458 if (connman_network_get_connected(network) == TRUE)
1461 connman_network_set_available(network, FALSE);
1464 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1467 struct connman_network *network = value;
1469 if (connman_network_get_connected(network) == TRUE)
1472 if (connman_network_get_available(network) == TRUE)
1478 void __connman_device_cleanup_networks(struct connman_device *device)
1480 g_hash_table_foreach_remove(device->networks,
1481 remove_unavailable_network, NULL);
1485 * connman_device_set_scanning:
1486 * @device: device structure
1487 * @scanning: scanning state
1489 * Change scanning state of device
1491 int connman_device_set_scanning(struct connman_device *device,
1492 connman_bool_t scanning)
1494 DBusMessage *signal;
1495 DBusMessageIter entry, value;
1496 const char *key = "Scanning";
1498 DBG("device %p scanning %d", device, scanning);
1500 if (!device->driver || !device->driver->scan)
1503 if (device->scanning == scanning)
1506 device->scanning = scanning;
1508 signal = dbus_message_new_signal(device->element.path,
1509 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1513 dbus_message_iter_init_append(signal, &entry);
1515 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1517 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1518 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1519 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1520 dbus_message_iter_close_container(&entry, &value);
1522 g_dbus_send_message(connection, signal);
1524 if (scanning == TRUE) {
1525 reset_scan_trigger(device);
1527 g_hash_table_foreach(device->networks,
1528 mark_network_unavailable, NULL);
1533 __connman_device_cleanup_networks(device);
1535 if (device->connections > 0)
1538 if (device->disconnected == TRUE)
1541 __connman_service_auto_connect();
1547 * connman_device_set_disconnected:
1548 * @device: device structure
1549 * @disconnected: disconnected state
1551 * Change disconnected state of device (only for device with networks)
1553 int connman_device_set_disconnected(struct connman_device *device,
1554 connman_bool_t disconnected)
1556 DBG("device %p disconnected %d", device, disconnected);
1558 switch (device->mode) {
1559 case CONNMAN_DEVICE_MODE_UNKNOWN:
1560 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1562 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1563 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1567 if (device->disconnected == disconnected)
1570 device->disconnected = disconnected;
1576 * connman_device_get_disconnected:
1577 * @device: device structure
1579 * Get device disconnected state
1581 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1583 return device->disconnected;
1587 * connman_device_set_connected:
1588 * @device: device structure
1589 * @connected: connected state
1591 * Change connected state of device (for Ethernet like devices)
1593 int connman_device_set_connected(struct connman_device *device,
1594 connman_bool_t connected)
1596 DBG("device %p connected %d", device, connected);
1598 switch (device->mode) {
1599 case CONNMAN_DEVICE_MODE_UNKNOWN:
1600 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1601 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1603 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1607 if (device->carrier == FALSE)
1610 return set_connected(device, connected);
1614 * connman_device_set_string:
1615 * @device: device structure
1616 * @key: unique identifier
1617 * @value: string value
1619 * Set string value for specific key
1621 int connman_device_set_string(struct connman_device *device,
1622 const char *key, const char *value)
1624 DBG("device %p key %s value %s", device, key, value);
1626 if (g_str_equal(key, "Address") == TRUE) {
1627 g_free(device->address);
1628 device->address = g_strdup(value);
1629 } else if (g_str_equal(key, "Name") == TRUE) {
1630 g_free(device->name);
1631 device->name = g_strdup(value);
1632 } else if (g_str_equal(key, "Node") == TRUE) {
1633 g_free(device->node);
1634 device->node = g_strdup(value);
1637 return connman_element_set_string(&device->element, key, value);
1641 * connman_device_get_string:
1642 * @device: device structure
1643 * @key: unique identifier
1645 * Get string value for specific key
1647 const char *connman_device_get_string(struct connman_device *device,
1650 DBG("device %p key %s", device, key);
1652 if (g_str_equal(key, "Address") == TRUE)
1653 return device->address;
1654 else if (g_str_equal(key, "Name") == TRUE)
1655 return device->name;
1656 else if (g_str_equal(key, "Node") == TRUE)
1657 return device->node;
1659 return connman_element_get_string(&device->element, key);
1662 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1664 struct connman_device *device = element->device;
1665 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1666 connman_bool_t powered;
1668 DBG("element %p name %s", element, element->name);
1673 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1675 if (device->powered == powered)
1678 set_powered(device, powered);
1681 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1683 DBG("offlinmode %d", offlinemode);
1685 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1686 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1688 __connman_notifier_offline_mode(offlinemode);
1693 void __connman_device_increase_connections(struct connman_device *device)
1695 device->connections++;
1698 void __connman_device_decrease_connections(struct connman_device *device)
1700 device->connections--;
1704 * connman_device_add_network:
1705 * @device: device structure
1706 * @network: network structure
1708 * Add new network to the device
1710 int connman_device_add_network(struct connman_device *device,
1711 struct connman_network *network)
1713 const char *identifier = connman_network_get_identifier(network);
1716 DBG("device %p network %p", device, network);
1718 switch (device->mode) {
1719 case CONNMAN_DEVICE_MODE_UNKNOWN:
1720 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1722 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1723 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1727 __connman_network_set_device(network, device);
1729 __connman_storage_load_network(network);
1731 err = connman_element_register((struct connman_element *) network,
1734 __connman_network_set_device(network, NULL);
1738 g_hash_table_insert(device->networks, g_strdup(identifier),
1745 * connman_device_get_network:
1746 * @device: device structure
1747 * @identifier: network identifier
1749 * Get network for given identifier
1751 struct connman_network *connman_device_get_network(struct connman_device *device,
1752 const char *identifier)
1754 DBG("device %p identifier %s", device, identifier);
1756 return g_hash_table_lookup(device->networks, identifier);
1760 * connman_device_remove_network:
1761 * @device: device structure
1762 * @identifier: network identifier
1764 * Remove network for given identifier
1766 int connman_device_remove_network(struct connman_device *device,
1767 const char *identifier)
1769 DBG("device %p identifier %s", device, identifier);
1771 g_hash_table_remove(device->networks, identifier);
1776 void __connman_device_set_network(struct connman_device *device,
1777 struct connman_network *network)
1781 if (device->network == network)
1784 if (device->network != NULL)
1785 connman_network_unref(device->network);
1787 if (network != NULL) {
1788 name = connman_network_get_string(network,
1789 CONNMAN_PROPERTY_ID_NAME);
1790 g_free(device->last_network);
1791 device->last_network = g_strdup(name);
1793 device->network = connman_network_ref(network);
1795 g_free(device->last_network);
1796 device->last_network = NULL;
1798 device->network = NULL;
1803 * connman_device_register:
1804 * @device: device structure
1806 * Register device with the system
1808 int connman_device_register(struct connman_device *device)
1810 __connman_storage_load_device(device);
1812 switch (device->mode) {
1813 case CONNMAN_DEVICE_MODE_UNKNOWN:
1814 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1816 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1817 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1818 __connman_storage_init_network(device);
1822 __connman_notifier_device_type_register(device->type);
1824 return connman_element_register(&device->element, NULL);
1828 * connman_device_unregister:
1829 * @device: device structure
1831 * Unregister device with the system
1833 void connman_device_unregister(struct connman_device *device)
1835 __connman_storage_save_device(device);
1837 __connman_notifier_device_type_unregister(device->type);
1839 connman_element_unregister(&device->element);
1843 * connman_device_get_data:
1844 * @device: device structure
1846 * Get private device data pointer
1848 void *connman_device_get_data(struct connman_device *device)
1850 return device->driver_data;
1854 * connman_device_set_data:
1855 * @device: device structure
1856 * @data: data pointer
1858 * Set private device data pointer
1860 void connman_device_set_data(struct connman_device *device, void *data)
1862 device->driver_data = data;
1865 static gboolean match_driver(struct connman_device *device,
1866 struct connman_device_driver *driver)
1868 if (device->type == driver->type ||
1869 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1875 static int device_probe(struct connman_element *element)
1877 struct connman_device *device = element->device;
1880 DBG("element %p name %s", element, element->name);
1885 if (device->driver != NULL)
1888 for (list = driver_list; list; list = list->next) {
1889 struct connman_device_driver *driver = list->data;
1891 if (match_driver(device, driver) == FALSE)
1894 DBG("driver %p name %s", driver, driver->name);
1896 if (driver->probe(device) == 0) {
1897 device->driver = driver;
1902 if (device->driver == NULL)
1905 return setup_device(device);
1908 static void device_remove(struct connman_element *element)
1910 struct connman_device *device = element->device;
1912 DBG("element %p name %s", element, element->name);
1917 if (device->driver == NULL)
1920 remove_device(device);
1923 static struct connman_driver device_driver = {
1925 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1926 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1927 .probe = device_probe,
1928 .remove = device_remove,
1931 static int device_load(struct connman_device *device)
1934 gchar *pathname, *identifier, *data = NULL;
1938 DBG("device %p", device);
1940 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1941 __connman_profile_active_ident());
1942 if (pathname == NULL)
1945 keyfile = g_key_file_new();
1947 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1954 if (g_key_file_load_from_data(keyfile, data, length,
1955 0, NULL) == FALSE) {
1962 identifier = g_strdup_printf("device_%s", device->element.name);
1963 if (identifier == NULL)
1966 switch (device->mode) {
1967 case CONNMAN_DEVICE_MODE_UNKNOWN:
1968 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1970 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1971 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1972 val = g_key_file_get_integer(keyfile, identifier,
1973 "ScanInterval", NULL);
1975 device->scan_interval = val;
1980 g_key_file_free(keyfile);
1987 static int device_save(struct connman_device *device)
1990 gchar *pathname, *identifier = NULL, *data = NULL;
1993 DBG("device %p", device);
1995 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1996 __connman_profile_active_ident());
1997 if (pathname == NULL)
2000 keyfile = g_key_file_new();
2002 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
2006 if (g_key_file_load_from_data(keyfile, data, length,
2014 identifier = g_strdup_printf("device_%s", device->element.name);
2015 if (identifier == NULL)
2018 switch (device->mode) {
2019 case CONNMAN_DEVICE_MODE_UNKNOWN:
2020 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
2022 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
2023 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
2024 if (device->scan_interval > 0)
2025 g_key_file_set_integer(keyfile, identifier,
2026 "ScanInterval", device->scan_interval);
2030 data = g_key_file_to_data(keyfile, &length, NULL);
2032 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
2033 connman_error("Failed to store device information");
2038 g_key_file_free(keyfile);
2046 static struct connman_storage device_storage = {
2048 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2049 .device_load = device_load,
2050 .device_save = device_save,
2053 int __connman_device_init(void)
2057 connection = connman_dbus_get_connection();
2059 if (connman_storage_register(&device_storage) < 0)
2060 connman_error("Failed to register device storage");
2062 return connman_driver_register(&device_driver);
2065 void __connman_device_cleanup(void)
2069 connman_driver_unregister(&device_driver);
2071 connman_storage_unregister(&device_storage);
2073 dbus_connection_unref(connection);