5 * Copyright (C) 2007-2010 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 offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
143 static const char *type2string(enum connman_device_type type)
146 case CONNMAN_DEVICE_TYPE_UNKNOWN:
147 case CONNMAN_DEVICE_TYPE_VENDOR:
149 case CONNMAN_DEVICE_TYPE_ETHERNET:
151 case CONNMAN_DEVICE_TYPE_WIFI:
153 case CONNMAN_DEVICE_TYPE_WIMAX:
155 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157 case CONNMAN_DEVICE_TYPE_GPS:
159 case CONNMAN_DEVICE_TYPE_CELLULAR:
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
168 enum connman_device_type type = connman_device_get_type(device);
171 case CONNMAN_DEVICE_TYPE_UNKNOWN:
172 case CONNMAN_DEVICE_TYPE_VENDOR:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_CELLULAR:
184 return CONNMAN_SERVICE_TYPE_CELLULAR;
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 static void powered_changed(struct connman_device *device)
192 connman_dbus_property_changed_basic(device->element.path,
193 CONNMAN_DEVICE_INTERFACE, "Powered",
194 DBUS_TYPE_BOOLEAN, &device->powered);
197 int __connman_device_enable(struct connman_device *device)
201 DBG("device %p", device);
203 if (!device->driver || !device->driver->enable)
206 if (device->powered_pending == TRUE)
209 if (device->blocked == TRUE)
212 err = device->driver->enable(device);
214 if (err == -EINPROGRESS)
215 device->powered_pending = TRUE;
219 device->powered_pending = TRUE;
220 device->powered = TRUE;
222 __connman_technology_enable_device(device);
227 int __connman_device_disable(struct connman_device *device)
231 DBG("device %p", device);
233 if (!device->driver || !device->driver->disable)
236 if (device->powered == FALSE)
239 if (device->powered_pending == FALSE)
242 device->reconnect = FALSE;
244 clear_scan_trigger(device);
246 g_hash_table_remove_all(device->networks);
248 err = device->driver->disable(device);
250 if (err == -EINPROGRESS)
251 device->powered_pending = FALSE;
255 device->powered_pending = FALSE;
256 device->powered = FALSE;
258 __connman_technology_disable_device(device);
263 static int set_powered(struct connman_device *device, connman_bool_t powered)
265 DBG("device %p powered %d", device, powered);
268 return __connman_device_enable(device);
270 return __connman_device_disable(device);
273 void __connman_device_list(DBusMessageIter *iter, void *user_data)
275 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
278 static void append_path(gpointer key, gpointer value, gpointer user_data)
280 struct connman_element *element = value;
281 DBusMessageIter *iter = user_data;
283 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
287 static void append_networks(DBusMessageIter *iter, void *user_data)
289 struct connman_device *device = user_data;
291 g_hash_table_foreach(device->networks, append_path, iter);
294 static DBusMessage *get_properties(DBusConnection *conn,
295 DBusMessage *msg, void *data)
297 struct connman_device *device = data;
299 DBusMessageIter array, dict;
302 DBG("conn %p", conn);
304 if (__connman_security_check_privilege(msg,
305 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
306 return __connman_error_permission_denied(msg);
308 reply = dbus_message_new_method_return(msg);
312 dbus_message_iter_init_append(reply, &array);
314 connman_dbus_dict_open(&array, &dict);
316 if (device->name != NULL)
317 connman_dbus_dict_append_basic(&dict, "Name",
318 DBUS_TYPE_STRING, &device->name);
320 str = type2string(device->type);
322 connman_dbus_dict_append_basic(&dict, "Type",
323 DBUS_TYPE_STRING, &str);
325 if (device->address != NULL)
326 connman_dbus_dict_append_basic(&dict, "Address",
327 DBUS_TYPE_STRING, &device->address);
329 if (device->interface != NULL)
330 connman_dbus_dict_append_basic(&dict, "Interface",
331 DBUS_TYPE_STRING, &device->interface);
333 connman_dbus_dict_append_basic(&dict, "Powered",
334 DBUS_TYPE_BOOLEAN, &device->powered);
336 if (device->driver && device->driver->scan)
337 connman_dbus_dict_append_basic(&dict, "Scanning",
338 DBUS_TYPE_BOOLEAN, &device->scanning);
340 switch (device->mode) {
341 case CONNMAN_DEVICE_MODE_UNKNOWN:
343 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
344 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
345 if (device->scan_interval > 0)
346 connman_dbus_dict_append_basic(&dict, "ScanInterval",
347 DBUS_TYPE_UINT16, &device->scan_interval);
349 connman_dbus_dict_append_array(&dict, "Networks",
350 DBUS_TYPE_OBJECT_PATH, append_networks, device);
354 connman_dbus_dict_close(&array, &dict);
359 static gboolean powered_timeout(gpointer user_data)
361 struct connman_device *device = user_data;
363 DBG("device %p", device);
367 if (device->pending != NULL) {
370 reply = __connman_error_operation_timeout(device->pending);
372 g_dbus_send_message(connection, reply);
374 dbus_message_unref(device->pending);
375 device->pending = NULL;
381 static DBusMessage *set_property(DBusConnection *conn,
382 DBusMessage *msg, void *data)
384 struct connman_device *device = data;
385 DBusMessageIter iter, value;
389 DBG("conn %p", conn);
391 if (dbus_message_iter_init(msg, &iter) == FALSE)
392 return __connman_error_invalid_arguments(msg);
394 dbus_message_iter_get_basic(&iter, &name);
395 dbus_message_iter_next(&iter);
396 dbus_message_iter_recurse(&iter, &value);
398 if (__connman_security_check_privilege(msg,
399 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
400 return __connman_error_permission_denied(msg);
402 type = dbus_message_iter_get_arg_type(&value);
404 if (g_str_equal(name, "Powered") == TRUE) {
405 connman_bool_t powered;
408 if (type != DBUS_TYPE_BOOLEAN)
409 return __connman_error_invalid_arguments(msg);
411 dbus_message_iter_get_basic(&value, &powered);
413 device->powered_persistent = powered;
415 __connman_storage_save_device(device);
417 if (device->powered == powered)
418 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
420 if (device->pending != NULL)
421 return __connman_error_in_progress(msg);
423 err = set_powered(device, powered);
425 if (err != -EINPROGRESS)
426 return __connman_error_failed(msg, -err);
428 device->pending = dbus_message_ref(msg);
430 device->timeout = g_timeout_add_seconds(15,
431 powered_timeout, device);
435 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
436 connman_uint16_t interval;
438 switch (device->mode) {
439 case CONNMAN_DEVICE_MODE_UNKNOWN:
440 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
441 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
445 if (type != DBUS_TYPE_UINT16)
446 return __connman_error_invalid_arguments(msg);
448 dbus_message_iter_get_basic(&value, &interval);
450 if (device->scan_interval != interval) {
451 device->scan_interval = interval;
453 __connman_storage_save_device(device);
455 reset_scan_trigger(device);
458 return __connman_error_invalid_property(msg);
460 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
463 static DBusMessage *propose_scan(DBusConnection *conn,
464 DBusMessage *msg, void *data)
466 struct connman_device *device = data;
469 DBG("conn %p", conn);
471 switch (device->mode) {
472 case CONNMAN_DEVICE_MODE_UNKNOWN:
473 return __connman_error_not_supported(msg);
474 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
475 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
479 err = __connman_device_scan(device);
481 return __connman_error_failed(msg, -err);
483 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
486 static GDBusMethodTable device_methods[] = {
487 { "GetProperties", "", "a{sv}", get_properties },
488 { "SetProperty", "sv", "", set_property,
489 G_DBUS_METHOD_FLAG_ASYNC },
490 { "ProposeScan", "", "", propose_scan },
494 static GDBusSignalTable device_signals[] = {
495 { "PropertyChanged", "sv" },
499 static int register_interface(struct connman_element *element)
501 struct connman_device *device = element->device;
503 DBG("element %p name %s", element, element->name);
505 if (g_dbus_register_interface(connection, element->path,
506 CONNMAN_DEVICE_INTERFACE,
507 device_methods, device_signals,
508 NULL, device, NULL) == FALSE) {
509 connman_error("Failed to register %s device", element->path);
513 device->registered = TRUE;
518 static void unregister_interface(struct connman_element *element)
520 struct connman_device *device = element->device;
522 DBG("element %p name %s", element, element->name);
524 device->registered = FALSE;
526 g_dbus_unregister_interface(connection, element->path,
527 CONNMAN_DEVICE_INTERFACE);
530 static int setup_device(struct connman_device *device)
534 DBG("device %p", device);
536 err = register_interface(&device->element);
538 if (device->driver->remove)
539 device->driver->remove(device);
540 device->driver = NULL;
544 __connman_technology_add_device(device);
546 switch (device->mode) {
547 case CONNMAN_DEVICE_MODE_UNKNOWN:
548 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
549 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
553 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
556 if (device->offlinemode == FALSE &&
557 device->powered_persistent == TRUE)
558 __connman_device_enable(device);
563 static void probe_driver(struct connman_element *element, gpointer user_data)
565 struct connman_device_driver *driver = user_data;
567 DBG("element %p name %s", element, element->name);
569 if (element->device == NULL)
572 if (element->device->driver != NULL)
575 if (driver->type != element->device->type)
578 if (driver->probe(element->device) < 0)
581 element->device->driver = driver;
583 setup_device(element->device);
586 static void remove_device(struct connman_device *device)
588 DBG("device %p", device);
590 __connman_device_disable(device);
592 switch (device->mode) {
593 case CONNMAN_DEVICE_MODE_UNKNOWN:
594 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
595 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
599 __connman_technology_remove_device(device);
601 unregister_interface(&device->element);
603 if (device->driver->remove)
604 device->driver->remove(device);
606 device->driver = NULL;
609 static void remove_driver(struct connman_element *element, gpointer user_data)
611 struct connman_device_driver *driver = user_data;
613 DBG("element %p name %s", element, element->name);
615 if (element->device == NULL)
618 if (element->device->driver == driver)
619 remove_device(element->device);
622 connman_bool_t __connman_device_has_driver(struct connman_device *device)
624 if (device == NULL || device->driver == NULL)
627 return device->registered;
630 static GSList *driver_list = NULL;
632 static gint compare_priority(gconstpointer a, gconstpointer b)
634 const struct connman_device_driver *driver1 = a;
635 const struct connman_device_driver *driver2 = b;
637 return driver2->priority - driver1->priority;
641 * connman_device_driver_register:
642 * @driver: device driver definition
644 * Register a new device driver
646 * Returns: %0 on success
648 int connman_device_driver_register(struct connman_device_driver *driver)
650 DBG("driver %p name %s", driver, driver->name);
652 driver_list = g_slist_insert_sorted(driver_list, driver,
655 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
656 probe_driver, driver);
662 * connman_device_driver_unregister:
663 * @driver: device driver definition
665 * Remove a previously registered device driver
667 void connman_device_driver_unregister(struct connman_device_driver *driver)
669 DBG("driver %p name %s", driver, driver->name);
671 driver_list = g_slist_remove(driver_list, driver);
673 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
674 remove_driver, driver);
677 static void unregister_network(gpointer data)
679 struct connman_network *network = data;
681 DBG("network %p", network);
683 connman_element_unregister((struct connman_element *) network);
685 connman_network_unref(network);
688 static void device_destruct(struct connman_element *element)
690 struct connman_device *device = element->device;
692 DBG("element %p name %s", element, element->name);
694 if (device->timeout > 0) {
695 g_source_remove(device->timeout);
699 clear_scan_trigger(device);
701 if (device->pending != NULL) {
702 dbus_message_unref(device->pending);
703 device->pending = NULL;
706 g_free(device->ident);
707 g_free(device->node);
708 g_free(device->name);
709 g_free(device->address);
710 g_free(device->control);
711 g_free(device->interface);
713 g_free(device->last_network);
715 g_hash_table_destroy(device->networks);
716 device->networks = NULL;
720 * connman_device_create:
721 * @node: device node name (for example an address)
724 * Allocate a new device of given #type and assign the #node name to it.
726 * Returns: a newly-allocated #connman_device structure
728 struct connman_device *connman_device_create(const char *node,
729 enum connman_device_type type)
731 struct connman_device *device;
734 DBG("node %s type %d", node, type);
736 device = g_try_new0(struct connman_device, 1);
740 DBG("device %p", device);
742 __connman_element_initialize(&device->element);
744 device->element.name = g_strdup(node);
745 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
747 device->element.device = device;
748 device->element.destruct = device_destruct;
750 str = type2string(type);
752 connman_element_set_string(&device->element,
753 CONNMAN_PROPERTY_ID_TYPE, str);
755 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
758 device->name = g_strdup(type2description(device->type));
759 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
761 device->powered_persistent = TRUE;
763 device->phyindex = -1;
766 case CONNMAN_DEVICE_TYPE_UNKNOWN:
767 case CONNMAN_DEVICE_TYPE_VENDOR:
768 device->scan_interval = 0;
770 case CONNMAN_DEVICE_TYPE_ETHERNET:
771 case CONNMAN_DEVICE_TYPE_WIFI:
772 device->scan_interval = 300;
774 case CONNMAN_DEVICE_TYPE_WIMAX:
775 device->scan_interval = 0;
777 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
778 device->scan_interval = 0;
780 case CONNMAN_DEVICE_TYPE_GPS:
781 device->scan_interval = 0;
783 case CONNMAN_DEVICE_TYPE_CELLULAR:
784 device->scan_interval = 0;
788 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
789 g_free, unregister_network);
795 * connman_device_ref:
796 * @device: device structure
798 * Increase reference counter of device
800 struct connman_device *connman_device_ref(struct connman_device *device)
802 if (connman_element_ref(&device->element) == NULL)
809 * connman_device_unref:
810 * @device: device structure
812 * Decrease reference counter of device
814 void connman_device_unref(struct connman_device *device)
816 connman_element_unref(&device->element);
819 const char *__connman_device_get_type(struct connman_device *device)
821 return type2string(device->type);
825 * connman_device_get_type:
826 * @device: device structure
830 enum connman_device_type connman_device_get_type(struct connman_device *device)
836 * connman_device_get_name:
837 * @device: device structure
839 * Get unique name of device
841 const char *connman_device_get_name(struct connman_device *device)
843 return device->element.name;
847 * connman_device_get_path:
848 * @device: device structure
850 * Get path name of device
852 const char *connman_device_get_path(struct connman_device *device)
854 return device->element.path;
858 * connman_device_set_index:
859 * @device: device structure
860 * @index: index number
862 * Set index number of device
864 void connman_device_set_index(struct connman_device *device, int index)
866 device->element.index = index;
870 * connman_device_get_index:
871 * @device: device structure
873 * Get index number of device
875 int connman_device_get_index(struct connman_device *device)
877 return device->element.index;
880 int __connman_device_get_phyindex(struct connman_device *device)
882 return device->phyindex;
885 void __connman_device_set_phyindex(struct connman_device *device,
888 device->phyindex = phyindex;
892 * connman_device_set_interface:
893 * @device: device structure
894 * @interface: interface name
895 * @control: control interface
897 * Set interface name of device
899 void connman_device_set_interface(struct connman_device *device,
900 const char *interface, const char *control)
902 g_free(device->element.devname);
903 device->element.devname = g_strdup(interface);
905 g_free(device->interface);
906 device->interface = g_strdup(interface);
908 g_free(device->control);
909 device->control = g_strdup(control);
911 if (device->name == NULL) {
912 const char *str = type2description(device->type);
913 if (str != NULL && device->interface != NULL)
914 device->name = g_strdup_printf("%s (%s)", str,
919 const char *connman_device_get_control(struct connman_device *device)
921 return device->control;
925 * connman_device_set_ident:
926 * @device: device structure
927 * @ident: unique identifier
929 * Set unique identifier of device
931 void connman_device_set_ident(struct connman_device *device,
934 g_free(device->ident);
935 device->ident = g_strdup(ident);
938 const char *__connman_device_get_ident(struct connman_device *device)
940 return device->ident;
944 * connman_device_set_mode:
945 * @device: device structure
946 * @mode: network mode
948 * Change network mode of device
950 void connman_device_set_mode(struct connman_device *device,
951 enum connman_device_mode mode)
957 * connman_device_get_mode:
958 * @device: device structure
960 * Get network mode of device
962 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
968 * connman_device_set_powered:
969 * @device: device structure
970 * @powered: powered state
972 * Change power state of device
974 int connman_device_set_powered(struct connman_device *device,
975 connman_bool_t powered)
977 DBG("driver %p powered %d", device, powered);
979 if (device->timeout > 0) {
980 g_source_remove(device->timeout);
984 if (device->pending != NULL) {
985 g_dbus_send_reply(connection, device->pending,
988 dbus_message_unref(device->pending);
989 device->pending = NULL;
992 if (device->powered == powered) {
993 device->powered_pending = powered;
998 __connman_device_enable(device);
1000 __connman_device_disable(device);
1002 device->powered = powered;
1003 device->powered_pending = powered;
1005 if (device->powered == TRUE)
1006 __connman_technology_enable_device(device);
1008 __connman_technology_disable_device(device);
1010 if (device->registered == FALSE)
1013 powered_changed(device);
1015 if (powered == FALSE)
1018 reset_scan_trigger(device);
1020 if (device->driver->scan)
1021 device->driver->scan(device);
1026 int __connman_device_set_blocked(struct connman_device *device,
1027 connman_bool_t blocked)
1029 connman_bool_t powered;
1031 DBG("device %p blocked %d", device, blocked);
1033 device->blocked = blocked;
1035 if (device->offlinemode == TRUE)
1038 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1040 if (blocked == FALSE)
1041 powered = device->powered_persistent;
1045 return set_powered(device, powered);
1048 int __connman_device_scan(struct connman_device *device)
1050 if (!device->driver || !device->driver->scan)
1053 if (device->powered == FALSE)
1056 reset_scan_trigger(device);
1058 return device->driver->scan(device);
1061 int __connman_device_enable_persistent(struct connman_device *device)
1065 DBG("device %p", device);
1067 device->powered_persistent = TRUE;
1069 __connman_storage_save_device(device);
1071 err = __connman_device_enable(device);
1072 if (err == 0 || err == -EINPROGRESS) {
1073 if (__connman_profile_get_offlinemode() == TRUE)
1074 __connman_profile_set_offlinemode(FALSE, FALSE);
1081 int __connman_device_disable_persistent(struct connman_device *device)
1083 DBG("device %p", device);
1085 device->powered_persistent = FALSE;
1087 __connman_storage_save_device(device);
1089 return __connman_device_disable(device);
1092 int __connman_device_disconnect(struct connman_device *device)
1094 GHashTableIter iter;
1095 gpointer key, value;
1097 DBG("device %p", device);
1099 connman_device_set_disconnected(device, TRUE);
1101 g_hash_table_iter_init(&iter, device->networks);
1103 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1104 struct connman_network *network = value;
1106 if (__connman_network_get_connecting(network) == TRUE) {
1108 * Skip network in the process of connecting.
1109 * This is a workaround for WiFi networks serviced
1110 * by the supplicant plugin that hold a reference
1111 * to the network. If we disconnect the network
1112 * here then the referenced object will not be
1113 * registered and usage (like launching DHCP client)
1114 * will fail. There is nothing to be gained by
1115 * removing the network here anyway.
1117 connman_warn("Skipping disconnect of %s",
1118 connman_network_get_identifier(network));
1122 __connman_network_disconnect(network);
1128 static void mark_network_unavailable(gpointer key, gpointer value,
1131 struct connman_network *network = value;
1133 if (connman_network_get_connected(network) == TRUE)
1136 connman_network_set_available(network, FALSE);
1139 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1142 struct connman_network *network = value;
1144 if (connman_network_get_connected(network) == TRUE)
1147 if (connman_network_get_available(network) == TRUE)
1153 void __connman_device_cleanup_networks(struct connman_device *device)
1155 g_hash_table_foreach_remove(device->networks,
1156 remove_unavailable_network, NULL);
1159 static void scanning_changed(struct connman_device *device)
1161 connman_dbus_property_changed_basic(device->element.path,
1162 CONNMAN_DEVICE_INTERFACE, "Scanning",
1163 DBUS_TYPE_BOOLEAN, &device->scanning);
1167 * connman_device_set_scanning:
1168 * @device: device structure
1169 * @scanning: scanning state
1171 * Change scanning state of device
1173 int connman_device_set_scanning(struct connman_device *device,
1174 connman_bool_t scanning)
1176 DBG("device %p scanning %d", device, scanning);
1178 if (!device->driver || !device->driver->scan)
1181 if (device->scanning == scanning)
1184 device->scanning = scanning;
1186 scanning_changed(device);
1188 if (scanning == TRUE) {
1189 reset_scan_trigger(device);
1191 g_hash_table_foreach(device->networks,
1192 mark_network_unavailable, NULL);
1197 __connman_device_cleanup_networks(device);
1199 if (device->connections > 0)
1202 if (device->disconnected == TRUE)
1205 __connman_service_auto_connect();
1211 * connman_device_set_disconnected:
1212 * @device: device structure
1213 * @disconnected: disconnected state
1215 * Change disconnected state of device (only for device with networks)
1217 int connman_device_set_disconnected(struct connman_device *device,
1218 connman_bool_t disconnected)
1220 DBG("device %p disconnected %d", device, disconnected);
1222 switch (device->mode) {
1223 case CONNMAN_DEVICE_MODE_UNKNOWN:
1225 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1226 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1230 if (device->disconnected == disconnected)
1233 device->disconnected = disconnected;
1235 if (disconnected == TRUE)
1236 force_scan_trigger(device);
1242 * connman_device_get_disconnected:
1243 * @device: device structure
1245 * Get device disconnected state
1247 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1249 return device->disconnected;
1253 * connman_device_set_string:
1254 * @device: device structure
1255 * @key: unique identifier
1256 * @value: string value
1258 * Set string value for specific key
1260 int connman_device_set_string(struct connman_device *device,
1261 const char *key, const char *value)
1263 DBG("device %p key %s value %s", device, key, value);
1265 if (g_str_equal(key, "Address") == TRUE) {
1266 g_free(device->address);
1267 device->address = g_strdup(value);
1268 } else if (g_str_equal(key, "Name") == TRUE) {
1269 g_free(device->name);
1270 device->name = g_strdup(value);
1271 } else if (g_str_equal(key, "Node") == TRUE) {
1272 g_free(device->node);
1273 device->node = g_strdup(value);
1276 return connman_element_set_string(&device->element, key, value);
1280 * connman_device_get_string:
1281 * @device: device structure
1282 * @key: unique identifier
1284 * Get string value for specific key
1286 const char *connman_device_get_string(struct connman_device *device,
1289 DBG("device %p key %s", device, key);
1291 if (g_str_equal(key, "Address") == TRUE)
1292 return device->address;
1293 else if (g_str_equal(key, "Name") == TRUE)
1294 return device->name;
1295 else if (g_str_equal(key, "Node") == TRUE)
1296 return device->node;
1298 return connman_element_get_string(&device->element, key);
1301 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1303 struct connman_device *device = element->device;
1304 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1305 connman_bool_t powered;
1307 DBG("element %p name %s", element, element->name);
1312 device->offlinemode = offlinemode;
1314 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1316 if (device->powered == powered)
1319 if (device->powered_persistent == FALSE)
1322 set_powered(device, powered);
1325 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1327 DBG("offlinmode %d", offlinemode);
1329 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1330 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1332 __connman_notifier_offlinemode(offlinemode);
1337 void __connman_device_increase_connections(struct connman_device *device)
1339 device->connections++;
1342 void __connman_device_decrease_connections(struct connman_device *device)
1344 device->connections--;
1348 * connman_device_add_network:
1349 * @device: device structure
1350 * @network: network structure
1352 * Add new network to the device
1354 int connman_device_add_network(struct connman_device *device,
1355 struct connman_network *network)
1357 const char *identifier = connman_network_get_identifier(network);
1360 DBG("device %p network %p", device, network);
1362 if (identifier == NULL)
1365 switch (device->mode) {
1366 case CONNMAN_DEVICE_MODE_UNKNOWN:
1368 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1369 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1373 __connman_network_set_device(network, device);
1375 err = connman_element_register((struct connman_element *) network,
1378 __connman_network_set_device(network, NULL);
1382 g_hash_table_insert(device->networks, g_strdup(identifier),
1389 * connman_device_get_network:
1390 * @device: device structure
1391 * @identifier: network identifier
1393 * Get network for given identifier
1395 struct connman_network *connman_device_get_network(struct connman_device *device,
1396 const char *identifier)
1398 DBG("device %p identifier %s", device, identifier);
1400 return g_hash_table_lookup(device->networks, identifier);
1404 * connman_device_remove_network:
1405 * @device: device structure
1406 * @identifier: network identifier
1408 * Remove network for given identifier
1410 int connman_device_remove_network(struct connman_device *device,
1411 const char *identifier)
1413 DBG("device %p identifier %s", device, identifier);
1415 g_hash_table_remove(device->networks, identifier);
1420 void connman_device_remove_all_networks(struct connman_device *device)
1422 g_hash_table_remove_all(device->networks);
1425 void __connman_device_set_network(struct connman_device *device,
1426 struct connman_network *network)
1433 if (device->network == network)
1436 if (device->network != NULL)
1437 connman_network_unref(device->network);
1439 if (network != NULL) {
1440 name = connman_network_get_string(network,
1441 CONNMAN_PROPERTY_ID_NAME);
1442 g_free(device->last_network);
1443 device->last_network = g_strdup(name);
1445 device->network = connman_network_ref(network);
1447 g_free(device->last_network);
1448 device->last_network = NULL;
1450 device->network = NULL;
1454 void __connman_device_set_reconnect(struct connman_device *device,
1455 connman_bool_t reconnect)
1457 device->reconnect = reconnect;
1460 connman_bool_t __connman_device_get_reconnect(
1461 struct connman_device *device)
1463 return device->reconnect;
1467 * connman_device_register:
1468 * @device: device structure
1470 * Register device with the system
1472 int connman_device_register(struct connman_device *device)
1474 __connman_storage_load_device(device);
1476 device->offlinemode = __connman_profile_get_offlinemode();
1478 return connman_element_register(&device->element, NULL);
1482 * connman_device_unregister:
1483 * @device: device structure
1485 * Unregister device with the system
1487 void connman_device_unregister(struct connman_device *device)
1489 __connman_storage_save_device(device);
1491 connman_element_unregister(&device->element);
1495 * connman_device_get_data:
1496 * @device: device structure
1498 * Get private device data pointer
1500 void *connman_device_get_data(struct connman_device *device)
1502 return device->driver_data;
1506 * connman_device_set_data:
1507 * @device: device structure
1508 * @data: data pointer
1510 * Set private device data pointer
1512 void connman_device_set_data(struct connman_device *device, void *data)
1514 device->driver_data = data;
1517 static gboolean match_driver(struct connman_device *device,
1518 struct connman_device_driver *driver)
1520 if (device->type == driver->type ||
1521 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1527 static int device_probe(struct connman_element *element)
1529 struct connman_device *device = element->device;
1532 DBG("element %p name %s", element, element->name);
1537 if (device->driver != NULL)
1540 for (list = driver_list; list; list = list->next) {
1541 struct connman_device_driver *driver = list->data;
1543 if (match_driver(device, driver) == FALSE)
1546 DBG("driver %p name %s", driver, driver->name);
1548 if (driver->probe(device) == 0) {
1549 device->driver = driver;
1554 if (device->driver == NULL)
1557 return setup_device(device);
1560 static void device_remove(struct connman_element *element)
1562 struct connman_device *device = element->device;
1564 DBG("element %p name %s", element, element->name);
1569 if (device->driver == NULL)
1572 remove_device(device);
1575 static struct connman_driver device_driver = {
1577 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1578 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1579 .probe = device_probe,
1580 .remove = device_remove,
1583 static int device_load(struct connman_device *device)
1585 const char *ident = __connman_profile_active_ident();
1587 GError *error = NULL;
1589 connman_bool_t powered;
1592 DBG("device %p", device);
1594 keyfile = __connman_storage_open_profile(ident);
1595 if (keyfile == NULL)
1598 identifier = g_strdup_printf("device_%s", device->element.name);
1599 if (identifier == NULL)
1602 powered = g_key_file_get_boolean(keyfile, identifier,
1605 device->powered_persistent = powered;
1606 g_clear_error(&error);
1608 switch (device->mode) {
1609 case CONNMAN_DEVICE_MODE_UNKNOWN:
1611 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1612 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1613 val = g_key_file_get_integer(keyfile, identifier,
1614 "ScanInterval", &error);
1615 if (error == NULL && val > 0)
1616 device->scan_interval = val;
1617 g_clear_error(&error);
1624 __connman_storage_close_profile(ident, keyfile, FALSE);
1629 static int device_save(struct connman_device *device)
1631 const char *ident = __connman_profile_active_ident();
1635 DBG("device %p", device);
1637 keyfile = __connman_storage_open_profile(ident);
1638 if (keyfile == NULL)
1641 identifier = g_strdup_printf("device_%s", device->element.name);
1642 if (identifier == NULL)
1645 g_key_file_set_boolean(keyfile, identifier,
1646 "Powered", device->powered_persistent);
1648 switch (device->mode) {
1649 case CONNMAN_DEVICE_MODE_UNKNOWN:
1651 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1652 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1653 if (device->scan_interval > 0)
1654 g_key_file_set_integer(keyfile, identifier,
1655 "ScanInterval", device->scan_interval);
1662 __connman_storage_close_profile(ident, keyfile, TRUE);
1667 static struct connman_storage device_storage = {
1669 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1670 .device_load = device_load,
1671 .device_save = device_save,
1674 int __connman_device_init(void)
1678 connection = connman_dbus_get_connection();
1680 if (connman_storage_register(&device_storage) < 0)
1681 connman_error("Failed to register device storage");
1683 return connman_driver_register(&device_driver);
1686 void __connman_device_cleanup(void)
1690 connman_driver_unregister(&device_driver);
1692 connman_storage_unregister(&device_storage);
1694 dbus_connection_unref(connection);