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 static int set_powered(struct connman_device *device, connman_bool_t powered)
199 struct connman_device_driver *driver = device->driver;
202 DBG("device %p powered %d", device, powered);
204 if (device->powered_pending == powered)
210 if (powered == TRUE) {
211 if (driver->enable) {
212 device->powered_pending = powered;
214 err = driver->enable(device);
216 __connman_technology_enable_device(device);
220 device->powered_pending = powered;
222 device->reconnect = FALSE;
224 clear_scan_trigger(device);
226 g_hash_table_remove_all(device->networks);
228 if (driver->disable) {
229 err = driver->disable(device);
231 __connman_technology_disable_device(device);
237 device->powered = powered;
239 if (device->registered == TRUE)
240 powered_changed(device);
246 void __connman_device_list(DBusMessageIter *iter, void *user_data)
248 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
251 static void append_path(gpointer key, gpointer value, gpointer user_data)
253 struct connman_element *element = value;
254 DBusMessageIter *iter = user_data;
256 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
260 static void append_networks(DBusMessageIter *iter, void *user_data)
262 struct connman_device *device = user_data;
264 g_hash_table_foreach(device->networks, append_path, iter);
267 static DBusMessage *get_properties(DBusConnection *conn,
268 DBusMessage *msg, void *data)
270 struct connman_device *device = data;
272 DBusMessageIter array, dict;
275 DBG("conn %p", conn);
277 if (__connman_security_check_privilege(msg,
278 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
279 return __connman_error_permission_denied(msg);
281 reply = dbus_message_new_method_return(msg);
285 dbus_message_iter_init_append(reply, &array);
287 connman_dbus_dict_open(&array, &dict);
289 if (device->name != NULL)
290 connman_dbus_dict_append_basic(&dict, "Name",
291 DBUS_TYPE_STRING, &device->name);
293 str = type2string(device->type);
295 connman_dbus_dict_append_basic(&dict, "Type",
296 DBUS_TYPE_STRING, &str);
298 if (device->address != NULL)
299 connman_dbus_dict_append_basic(&dict, "Address",
300 DBUS_TYPE_STRING, &device->address);
302 if (device->interface != NULL)
303 connman_dbus_dict_append_basic(&dict, "Interface",
304 DBUS_TYPE_STRING, &device->interface);
306 connman_dbus_dict_append_basic(&dict, "Powered",
307 DBUS_TYPE_BOOLEAN, &device->powered);
309 if (device->driver && device->driver->scan)
310 connman_dbus_dict_append_basic(&dict, "Scanning",
311 DBUS_TYPE_BOOLEAN, &device->scanning);
313 switch (device->mode) {
314 case CONNMAN_DEVICE_MODE_UNKNOWN:
316 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
317 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
318 if (device->scan_interval > 0)
319 connman_dbus_dict_append_basic(&dict, "ScanInterval",
320 DBUS_TYPE_UINT16, &device->scan_interval);
322 connman_dbus_dict_append_array(&dict, "Networks",
323 DBUS_TYPE_OBJECT_PATH, append_networks, device);
327 connman_dbus_dict_close(&array, &dict);
332 static gboolean powered_timeout(gpointer user_data)
334 struct connman_device *device = user_data;
336 DBG("device %p", device);
340 if (device->pending != NULL) {
343 reply = __connman_error_operation_timeout(device->pending);
345 g_dbus_send_message(connection, reply);
347 dbus_message_unref(device->pending);
348 device->pending = NULL;
354 static DBusMessage *set_property(DBusConnection *conn,
355 DBusMessage *msg, void *data)
357 struct connman_device *device = data;
358 DBusMessageIter iter, value;
362 DBG("conn %p", conn);
364 if (dbus_message_iter_init(msg, &iter) == FALSE)
365 return __connman_error_invalid_arguments(msg);
367 dbus_message_iter_get_basic(&iter, &name);
368 dbus_message_iter_next(&iter);
369 dbus_message_iter_recurse(&iter, &value);
371 if (__connman_security_check_privilege(msg,
372 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
373 return __connman_error_permission_denied(msg);
375 type = dbus_message_iter_get_arg_type(&value);
377 if (g_str_equal(name, "Powered") == TRUE) {
378 connman_bool_t powered;
381 if (type != DBUS_TYPE_BOOLEAN)
382 return __connman_error_invalid_arguments(msg);
384 dbus_message_iter_get_basic(&value, &powered);
386 device->powered_persistent = powered;
388 __connman_storage_save_device(device);
390 if (device->powered == powered)
391 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
393 if (device->pending != NULL)
394 return __connman_error_in_progress(msg);
396 err = set_powered(device, powered);
398 if (err != -EINPROGRESS)
399 return __connman_error_failed(msg, -err);
401 device->pending = dbus_message_ref(msg);
403 device->timeout = g_timeout_add_seconds(15,
404 powered_timeout, device);
408 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
409 connman_uint16_t interval;
411 switch (device->mode) {
412 case CONNMAN_DEVICE_MODE_UNKNOWN:
413 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
414 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
418 if (type != DBUS_TYPE_UINT16)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &interval);
423 if (device->scan_interval != interval) {
424 device->scan_interval = interval;
426 __connman_storage_save_device(device);
428 reset_scan_trigger(device);
431 return __connman_error_invalid_property(msg);
433 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
436 static DBusMessage *propose_scan(DBusConnection *conn,
437 DBusMessage *msg, void *data)
439 struct connman_device *device = data;
442 DBG("conn %p", conn);
444 switch (device->mode) {
445 case CONNMAN_DEVICE_MODE_UNKNOWN:
446 return __connman_error_not_supported(msg);
447 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
448 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
452 err = __connman_device_scan(device);
454 return __connman_error_failed(msg, -err);
456 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
459 static GDBusMethodTable device_methods[] = {
460 { "GetProperties", "", "a{sv}", get_properties },
461 { "SetProperty", "sv", "", set_property,
462 G_DBUS_METHOD_FLAG_ASYNC },
463 { "ProposeScan", "", "", propose_scan },
467 static GDBusSignalTable device_signals[] = {
468 { "PropertyChanged", "sv" },
472 static int register_interface(struct connman_element *element)
474 struct connman_device *device = element->device;
476 DBG("element %p name %s", element, element->name);
478 if (g_dbus_register_interface(connection, element->path,
479 CONNMAN_DEVICE_INTERFACE,
480 device_methods, device_signals,
481 NULL, device, NULL) == FALSE) {
482 connman_error("Failed to register %s device", element->path);
486 device->registered = TRUE;
491 static void unregister_interface(struct connman_element *element)
493 struct connman_device *device = element->device;
495 DBG("element %p name %s", element, element->name);
497 device->registered = FALSE;
499 g_dbus_unregister_interface(connection, element->path,
500 CONNMAN_DEVICE_INTERFACE);
503 static int setup_device(struct connman_device *device)
507 DBG("device %p", device);
509 err = register_interface(&device->element);
511 if (device->driver->remove)
512 device->driver->remove(device);
513 device->driver = NULL;
517 __connman_technology_add_device(device);
519 switch (device->mode) {
520 case CONNMAN_DEVICE_MODE_UNKNOWN:
521 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
522 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
526 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
529 if (device->offlinemode == FALSE &&
530 device->powered_persistent == TRUE)
531 __connman_device_enable(device);
536 static void probe_driver(struct connman_element *element, gpointer user_data)
538 struct connman_device_driver *driver = user_data;
540 DBG("element %p name %s", element, element->name);
542 if (element->device == NULL)
545 if (element->device->driver != NULL)
548 if (driver->type != element->device->type)
551 if (driver->probe(element->device) < 0)
554 element->device->driver = driver;
556 setup_device(element->device);
559 static void remove_device(struct connman_device *device)
561 DBG("device %p", device);
563 __connman_device_disable(device);
565 switch (device->mode) {
566 case CONNMAN_DEVICE_MODE_UNKNOWN:
567 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
568 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
572 __connman_technology_remove_device(device);
574 unregister_interface(&device->element);
576 if (device->driver->remove)
577 device->driver->remove(device);
579 device->driver = NULL;
582 static void remove_driver(struct connman_element *element, gpointer user_data)
584 struct connman_device_driver *driver = user_data;
586 DBG("element %p name %s", element, element->name);
588 if (element->device == NULL)
591 if (element->device->driver == driver)
592 remove_device(element->device);
595 connman_bool_t __connman_device_has_driver(struct connman_device *device)
597 if (device == NULL || device->driver == NULL)
600 return device->registered;
603 static GSList *driver_list = NULL;
605 static gint compare_priority(gconstpointer a, gconstpointer b)
607 const struct connman_device_driver *driver1 = a;
608 const struct connman_device_driver *driver2 = b;
610 return driver2->priority - driver1->priority;
614 * connman_device_driver_register:
615 * @driver: device driver definition
617 * Register a new device driver
619 * Returns: %0 on success
621 int connman_device_driver_register(struct connman_device_driver *driver)
623 DBG("driver %p name %s", driver, driver->name);
625 driver_list = g_slist_insert_sorted(driver_list, driver,
628 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
629 probe_driver, driver);
635 * connman_device_driver_unregister:
636 * @driver: device driver definition
638 * Remove a previously registered device driver
640 void connman_device_driver_unregister(struct connman_device_driver *driver)
642 DBG("driver %p name %s", driver, driver->name);
644 driver_list = g_slist_remove(driver_list, driver);
646 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
647 remove_driver, driver);
650 static void unregister_network(gpointer data)
652 struct connman_network *network = data;
654 DBG("network %p", network);
656 connman_element_unregister((struct connman_element *) network);
658 connman_network_unref(network);
661 static void device_destruct(struct connman_element *element)
663 struct connman_device *device = element->device;
665 DBG("element %p name %s", element, element->name);
667 if (device->timeout > 0) {
668 g_source_remove(device->timeout);
672 clear_scan_trigger(device);
674 if (device->pending != NULL) {
675 dbus_message_unref(device->pending);
676 device->pending = NULL;
679 g_free(device->ident);
680 g_free(device->node);
681 g_free(device->name);
682 g_free(device->address);
683 g_free(device->control);
684 g_free(device->interface);
686 g_free(device->last_network);
688 g_hash_table_destroy(device->networks);
689 device->networks = NULL;
693 * connman_device_create:
694 * @node: device node name (for example an address)
697 * Allocate a new device of given #type and assign the #node name to it.
699 * Returns: a newly-allocated #connman_device structure
701 struct connman_device *connman_device_create(const char *node,
702 enum connman_device_type type)
704 struct connman_device *device;
707 DBG("node %s type %d", node, type);
709 device = g_try_new0(struct connman_device, 1);
713 DBG("device %p", device);
715 __connman_element_initialize(&device->element);
717 device->element.name = g_strdup(node);
718 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
720 device->element.device = device;
721 device->element.destruct = device_destruct;
723 str = type2string(type);
725 connman_element_set_string(&device->element,
726 CONNMAN_PROPERTY_ID_TYPE, str);
728 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
731 device->name = g_strdup(type2description(device->type));
732 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
734 device->powered_persistent = TRUE;
736 device->phyindex = -1;
739 case CONNMAN_DEVICE_TYPE_UNKNOWN:
740 case CONNMAN_DEVICE_TYPE_VENDOR:
741 device->scan_interval = 0;
743 case CONNMAN_DEVICE_TYPE_ETHERNET:
744 case CONNMAN_DEVICE_TYPE_WIFI:
745 device->scan_interval = 300;
747 case CONNMAN_DEVICE_TYPE_WIMAX:
748 device->scan_interval = 0;
750 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
751 device->scan_interval = 0;
753 case CONNMAN_DEVICE_TYPE_GPS:
754 device->scan_interval = 0;
756 case CONNMAN_DEVICE_TYPE_CELLULAR:
757 device->scan_interval = 0;
761 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
762 g_free, unregister_network);
768 * connman_device_ref:
769 * @device: device structure
771 * Increase reference counter of device
773 struct connman_device *connman_device_ref(struct connman_device *device)
775 if (connman_element_ref(&device->element) == NULL)
782 * connman_device_unref:
783 * @device: device structure
785 * Decrease reference counter of device
787 void connman_device_unref(struct connman_device *device)
789 connman_element_unref(&device->element);
792 const char *__connman_device_get_type(struct connman_device *device)
794 return type2string(device->type);
798 * connman_device_get_type:
799 * @device: device structure
803 enum connman_device_type connman_device_get_type(struct connman_device *device)
809 * connman_device_get_name:
810 * @device: device structure
812 * Get unique name of device
814 const char *connman_device_get_name(struct connman_device *device)
816 return device->element.name;
820 * connman_device_get_path:
821 * @device: device structure
823 * Get path name of device
825 const char *connman_device_get_path(struct connman_device *device)
827 return device->element.path;
831 * connman_device_set_index:
832 * @device: device structure
833 * @index: index number
835 * Set index number of device
837 void connman_device_set_index(struct connman_device *device, int index)
839 device->element.index = index;
843 * connman_device_get_index:
844 * @device: device structure
846 * Get index number of device
848 int connman_device_get_index(struct connman_device *device)
850 return device->element.index;
853 int __connman_device_get_phyindex(struct connman_device *device)
855 return device->phyindex;
858 void __connman_device_set_phyindex(struct connman_device *device,
861 device->phyindex = phyindex;
865 * connman_device_set_interface:
866 * @device: device structure
867 * @interface: interface name
868 * @control: control interface
870 * Set interface name of device
872 void connman_device_set_interface(struct connman_device *device,
873 const char *interface, const char *control)
875 g_free(device->element.devname);
876 device->element.devname = g_strdup(interface);
878 g_free(device->interface);
879 device->interface = g_strdup(interface);
881 g_free(device->control);
882 device->control = g_strdup(control);
884 if (device->name == NULL) {
885 const char *str = type2description(device->type);
886 if (str != NULL && device->interface != NULL)
887 device->name = g_strdup_printf("%s (%s)", str,
892 const char *connman_device_get_control(struct connman_device *device)
894 return device->control;
898 * connman_device_set_ident:
899 * @device: device structure
900 * @ident: unique identifier
902 * Set unique identifier of device
904 void connman_device_set_ident(struct connman_device *device,
907 g_free(device->ident);
908 device->ident = g_strdup(ident);
911 const char *__connman_device_get_ident(struct connman_device *device)
913 return device->ident;
917 * connman_device_set_mode:
918 * @device: device structure
919 * @mode: network mode
921 * Change network mode of device
923 void connman_device_set_mode(struct connman_device *device,
924 enum connman_device_mode mode)
930 * connman_device_get_mode:
931 * @device: device structure
933 * Get network mode of device
935 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
941 * connman_device_set_powered:
942 * @device: device structure
943 * @powered: powered state
945 * Change power state of device
947 int connman_device_set_powered(struct connman_device *device,
948 connman_bool_t powered)
950 DBG("driver %p powered %d", device, powered);
952 if (device->timeout > 0) {
953 g_source_remove(device->timeout);
957 if (device->pending != NULL) {
958 g_dbus_send_reply(connection, device->pending,
961 dbus_message_unref(device->pending);
962 device->pending = NULL;
965 if (device->powered == powered)
969 __connman_device_enable(device);
971 __connman_device_disable(device);
973 device->powered = powered;
974 device->powered_pending = powered;
976 if (device->powered == TRUE)
977 __connman_technology_enable_device(device);
979 __connman_technology_disable_device(device);
981 if (device->registered == FALSE)
984 powered_changed(device);
986 if (powered == FALSE)
989 reset_scan_trigger(device);
991 if (device->driver->scan)
992 device->driver->scan(device);
997 int __connman_device_set_blocked(struct connman_device *device,
998 connman_bool_t blocked)
1000 connman_bool_t powered;
1002 DBG("device %p blocked %d", device, blocked);
1004 device->blocked = blocked;
1006 if (device->offlinemode == TRUE)
1009 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1011 if (blocked == FALSE)
1012 powered = device->powered_persistent;
1016 return set_powered(device, powered);
1019 int __connman_device_scan(struct connman_device *device)
1021 if (!device->driver || !device->driver->scan)
1024 if (device->powered == FALSE)
1027 reset_scan_trigger(device);
1029 return device->driver->scan(device);
1032 int __connman_device_enable(struct connman_device *device)
1036 DBG("device %p", device);
1038 if (!device->driver || !device->driver->enable)
1041 if (device->powered_pending == TRUE)
1044 device->powered_pending = TRUE;
1046 err = device->driver->enable(device);
1050 device->powered = TRUE;
1052 __connman_technology_enable_device(device);
1057 int __connman_device_enable_persistent(struct connman_device *device)
1059 DBG("device %p", device);
1061 device->powered_persistent = TRUE;
1063 if (__connman_profile_get_offlinemode() == TRUE)
1064 __connman_profile_set_offlinemode(FALSE, FALSE);
1066 __connman_storage_save_device(device);
1068 return __connman_device_enable(device);
1071 int __connman_device_disable(struct connman_device *device)
1075 DBG("device %p", device);
1077 if (!device->driver || !device->driver->disable)
1080 if (device->powered == FALSE)
1083 if (device->powered_pending == FALSE)
1086 device->powered_pending = FALSE;
1088 device->reconnect = FALSE;
1090 clear_scan_trigger(device);
1092 g_hash_table_remove_all(device->networks);
1094 err = device->driver->disable(device);
1098 device->powered = FALSE;
1100 __connman_technology_disable_device(device);
1105 int __connman_device_disable_persistent(struct connman_device *device)
1107 DBG("device %p", device);
1109 device->powered_persistent = FALSE;
1111 __connman_storage_save_device(device);
1113 return __connman_device_disable(device);
1116 int __connman_device_disconnect(struct connman_device *device)
1118 GHashTableIter iter;
1119 gpointer key, value;
1121 DBG("device %p", device);
1123 connman_device_set_disconnected(device, TRUE);
1125 g_hash_table_iter_init(&iter, device->networks);
1127 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1128 struct connman_network *network = value;
1130 if (__connman_network_get_connecting(network) == TRUE) {
1132 * Skip network in the process of connecting.
1133 * This is a workaround for WiFi networks serviced
1134 * by the supplicant plugin that hold a reference
1135 * to the network. If we disconnect the network
1136 * here then the referenced object will not be
1137 * registered and usage (like launching DHCP client)
1138 * will fail. There is nothing to be gained by
1139 * removing the network here anyway.
1141 connman_warn("Skipping disconnect of %s",
1142 connman_network_get_identifier(network));
1146 __connman_network_disconnect(network);
1152 static void mark_network_unavailable(gpointer key, gpointer value,
1155 struct connman_network *network = value;
1157 if (connman_network_get_connected(network) == TRUE)
1160 connman_network_set_available(network, FALSE);
1163 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1166 struct connman_network *network = value;
1168 if (connman_network_get_connected(network) == TRUE)
1171 if (connman_network_get_available(network) == TRUE)
1177 void __connman_device_cleanup_networks(struct connman_device *device)
1179 g_hash_table_foreach_remove(device->networks,
1180 remove_unavailable_network, NULL);
1183 static void scanning_changed(struct connman_device *device)
1185 connman_dbus_property_changed_basic(device->element.path,
1186 CONNMAN_DEVICE_INTERFACE, "Scanning",
1187 DBUS_TYPE_BOOLEAN, &device->scanning);
1191 * connman_device_set_scanning:
1192 * @device: device structure
1193 * @scanning: scanning state
1195 * Change scanning state of device
1197 int connman_device_set_scanning(struct connman_device *device,
1198 connman_bool_t scanning)
1200 DBG("device %p scanning %d", device, scanning);
1202 if (!device->driver || !device->driver->scan)
1205 if (device->scanning == scanning)
1208 device->scanning = scanning;
1210 scanning_changed(device);
1212 if (scanning == TRUE) {
1213 reset_scan_trigger(device);
1215 g_hash_table_foreach(device->networks,
1216 mark_network_unavailable, NULL);
1221 __connman_device_cleanup_networks(device);
1223 if (device->connections > 0)
1226 if (device->disconnected == TRUE)
1229 __connman_service_auto_connect();
1235 * connman_device_set_disconnected:
1236 * @device: device structure
1237 * @disconnected: disconnected state
1239 * Change disconnected state of device (only for device with networks)
1241 int connman_device_set_disconnected(struct connman_device *device,
1242 connman_bool_t disconnected)
1244 DBG("device %p disconnected %d", device, disconnected);
1246 switch (device->mode) {
1247 case CONNMAN_DEVICE_MODE_UNKNOWN:
1249 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1250 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1254 if (device->disconnected == disconnected)
1257 device->disconnected = disconnected;
1259 if (disconnected == TRUE)
1260 force_scan_trigger(device);
1266 * connman_device_get_disconnected:
1267 * @device: device structure
1269 * Get device disconnected state
1271 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1273 return device->disconnected;
1277 * connman_device_set_string:
1278 * @device: device structure
1279 * @key: unique identifier
1280 * @value: string value
1282 * Set string value for specific key
1284 int connman_device_set_string(struct connman_device *device,
1285 const char *key, const char *value)
1287 DBG("device %p key %s value %s", device, key, value);
1289 if (g_str_equal(key, "Address") == TRUE) {
1290 g_free(device->address);
1291 device->address = g_strdup(value);
1292 } else if (g_str_equal(key, "Name") == TRUE) {
1293 g_free(device->name);
1294 device->name = g_strdup(value);
1295 } else if (g_str_equal(key, "Node") == TRUE) {
1296 g_free(device->node);
1297 device->node = g_strdup(value);
1300 return connman_element_set_string(&device->element, key, value);
1304 * connman_device_get_string:
1305 * @device: device structure
1306 * @key: unique identifier
1308 * Get string value for specific key
1310 const char *connman_device_get_string(struct connman_device *device,
1313 DBG("device %p key %s", device, key);
1315 if (g_str_equal(key, "Address") == TRUE)
1316 return device->address;
1317 else if (g_str_equal(key, "Name") == TRUE)
1318 return device->name;
1319 else if (g_str_equal(key, "Node") == TRUE)
1320 return device->node;
1322 return connman_element_get_string(&device->element, key);
1325 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1327 struct connman_device *device = element->device;
1328 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1329 connman_bool_t powered;
1331 DBG("element %p name %s", element, element->name);
1336 device->offlinemode = offlinemode;
1338 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1340 if (device->powered == powered)
1343 if (device->powered_persistent == FALSE)
1346 set_powered(device, powered);
1349 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1351 DBG("offlinmode %d", offlinemode);
1353 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1354 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1356 __connman_notifier_offlinemode(offlinemode);
1361 void __connman_device_increase_connections(struct connman_device *device)
1363 device->connections++;
1366 void __connman_device_decrease_connections(struct connman_device *device)
1368 device->connections--;
1372 * connman_device_add_network:
1373 * @device: device structure
1374 * @network: network structure
1376 * Add new network to the device
1378 int connman_device_add_network(struct connman_device *device,
1379 struct connman_network *network)
1381 const char *identifier = connman_network_get_identifier(network);
1384 DBG("device %p network %p", device, network);
1386 if (identifier == NULL)
1389 switch (device->mode) {
1390 case CONNMAN_DEVICE_MODE_UNKNOWN:
1392 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1393 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1397 __connman_network_set_device(network, device);
1399 err = connman_element_register((struct connman_element *) network,
1402 __connman_network_set_device(network, NULL);
1406 g_hash_table_insert(device->networks, g_strdup(identifier),
1413 * connman_device_get_network:
1414 * @device: device structure
1415 * @identifier: network identifier
1417 * Get network for given identifier
1419 struct connman_network *connman_device_get_network(struct connman_device *device,
1420 const char *identifier)
1422 DBG("device %p identifier %s", device, identifier);
1424 return g_hash_table_lookup(device->networks, identifier);
1428 * connman_device_remove_network:
1429 * @device: device structure
1430 * @identifier: network identifier
1432 * Remove network for given identifier
1434 int connman_device_remove_network(struct connman_device *device,
1435 const char *identifier)
1437 DBG("device %p identifier %s", device, identifier);
1439 g_hash_table_remove(device->networks, identifier);
1444 void connman_device_remove_all_networks(struct connman_device *device)
1446 g_hash_table_remove_all(device->networks);
1449 void __connman_device_set_network(struct connman_device *device,
1450 struct connman_network *network)
1457 if (device->network == network)
1460 if (device->network != NULL)
1461 connman_network_unref(device->network);
1463 if (network != NULL) {
1464 name = connman_network_get_string(network,
1465 CONNMAN_PROPERTY_ID_NAME);
1466 g_free(device->last_network);
1467 device->last_network = g_strdup(name);
1469 device->network = connman_network_ref(network);
1471 g_free(device->last_network);
1472 device->last_network = NULL;
1474 device->network = NULL;
1478 void __connman_device_set_reconnect(struct connman_device *device,
1479 connman_bool_t reconnect)
1481 device->reconnect = reconnect;
1484 connman_bool_t __connman_device_get_reconnect(
1485 struct connman_device *device)
1487 return device->reconnect;
1491 * connman_device_register:
1492 * @device: device structure
1494 * Register device with the system
1496 int connman_device_register(struct connman_device *device)
1498 __connman_storage_load_device(device);
1500 device->offlinemode = __connman_profile_get_offlinemode();
1502 return connman_element_register(&device->element, NULL);
1506 * connman_device_unregister:
1507 * @device: device structure
1509 * Unregister device with the system
1511 void connman_device_unregister(struct connman_device *device)
1513 __connman_storage_save_device(device);
1515 connman_element_unregister(&device->element);
1519 * connman_device_get_data:
1520 * @device: device structure
1522 * Get private device data pointer
1524 void *connman_device_get_data(struct connman_device *device)
1526 return device->driver_data;
1530 * connman_device_set_data:
1531 * @device: device structure
1532 * @data: data pointer
1534 * Set private device data pointer
1536 void connman_device_set_data(struct connman_device *device, void *data)
1538 device->driver_data = data;
1541 static gboolean match_driver(struct connman_device *device,
1542 struct connman_device_driver *driver)
1544 if (device->type == driver->type ||
1545 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1551 static int device_probe(struct connman_element *element)
1553 struct connman_device *device = element->device;
1556 DBG("element %p name %s", element, element->name);
1561 if (device->driver != NULL)
1564 for (list = driver_list; list; list = list->next) {
1565 struct connman_device_driver *driver = list->data;
1567 if (match_driver(device, driver) == FALSE)
1570 DBG("driver %p name %s", driver, driver->name);
1572 if (driver->probe(device) == 0) {
1573 device->driver = driver;
1578 if (device->driver == NULL)
1581 return setup_device(device);
1584 static void device_remove(struct connman_element *element)
1586 struct connman_device *device = element->device;
1588 DBG("element %p name %s", element, element->name);
1593 if (device->driver == NULL)
1596 remove_device(device);
1599 static struct connman_driver device_driver = {
1601 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1602 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1603 .probe = device_probe,
1604 .remove = device_remove,
1607 static int device_load(struct connman_device *device)
1609 const char *ident = __connman_profile_active_ident();
1611 GError *error = NULL;
1613 connman_bool_t powered;
1616 DBG("device %p", device);
1618 keyfile = __connman_storage_open_profile(ident);
1619 if (keyfile == NULL)
1622 identifier = g_strdup_printf("device_%s", device->element.name);
1623 if (identifier == NULL)
1626 powered = g_key_file_get_boolean(keyfile, identifier,
1629 device->powered_persistent = powered;
1630 g_clear_error(&error);
1632 switch (device->mode) {
1633 case CONNMAN_DEVICE_MODE_UNKNOWN:
1635 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1636 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1637 val = g_key_file_get_integer(keyfile, identifier,
1638 "ScanInterval", &error);
1639 if (error == NULL && val > 0)
1640 device->scan_interval = val;
1641 g_clear_error(&error);
1648 __connman_storage_close_profile(ident, keyfile, FALSE);
1653 static int device_save(struct connman_device *device)
1655 const char *ident = __connman_profile_active_ident();
1659 DBG("device %p", device);
1661 keyfile = __connman_storage_open_profile(ident);
1662 if (keyfile == NULL)
1665 identifier = g_strdup_printf("device_%s", device->element.name);
1666 if (identifier == NULL)
1669 g_key_file_set_boolean(keyfile, identifier,
1670 "Powered", device->powered_persistent);
1672 switch (device->mode) {
1673 case CONNMAN_DEVICE_MODE_UNKNOWN:
1675 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1676 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1677 if (device->scan_interval > 0)
1678 g_key_file_set_integer(keyfile, identifier,
1679 "ScanInterval", device->scan_interval);
1686 __connman_storage_close_profile(ident, keyfile, TRUE);
1691 static struct connman_storage device_storage = {
1693 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1694 .device_load = device_load,
1695 .device_save = device_save,
1698 int __connman_device_init(void)
1702 connection = connman_dbus_get_connection();
1704 if (connman_storage_register(&device_storage) < 0)
1705 connman_error("Failed to register device storage");
1707 return connman_driver_register(&device_driver);
1710 void __connman_device_cleanup(void)
1714 connman_driver_unregister(&device_driver);
1716 connman_storage_unregister(&device_storage);
1718 dbus_connection_unref(connection);