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 __connman_storage_save_device(device);
1065 return __connman_device_enable(device);
1068 int __connman_device_disable(struct connman_device *device)
1072 DBG("device %p", device);
1074 if (!device->driver || !device->driver->disable)
1077 if (device->powered == FALSE)
1080 if (device->powered_pending == FALSE)
1083 device->powered_pending = FALSE;
1085 device->reconnect = FALSE;
1087 clear_scan_trigger(device);
1089 g_hash_table_remove_all(device->networks);
1091 err = device->driver->disable(device);
1095 device->powered = FALSE;
1097 __connman_technology_disable_device(device);
1102 int __connman_device_disable_persistent(struct connman_device *device)
1104 DBG("device %p", device);
1106 device->powered_persistent = FALSE;
1108 __connman_storage_save_device(device);
1110 return __connman_device_disable(device);
1113 int __connman_device_disconnect(struct connman_device *device)
1115 GHashTableIter iter;
1116 gpointer key, value;
1118 DBG("device %p", device);
1120 connman_device_set_disconnected(device, TRUE);
1122 g_hash_table_iter_init(&iter, device->networks);
1124 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1125 struct connman_network *network = value;
1127 if (__connman_network_get_connecting(network) == TRUE) {
1129 * Skip network in the process of connecting.
1130 * This is a workaround for WiFi networks serviced
1131 * by the supplicant plugin that hold a reference
1132 * to the network. If we disconnect the network
1133 * here then the referenced object will not be
1134 * registered and usage (like launching DHCP client)
1135 * will fail. There is nothing to be gained by
1136 * removing the network here anyway.
1138 connman_warn("Skipping disconnect of %s",
1139 connman_network_get_identifier(network));
1143 __connman_network_disconnect(network);
1149 static void mark_network_unavailable(gpointer key, gpointer value,
1152 struct connman_network *network = value;
1154 if (connman_network_get_connected(network) == TRUE)
1157 connman_network_set_available(network, FALSE);
1160 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1163 struct connman_network *network = value;
1165 if (connman_network_get_connected(network) == TRUE)
1168 if (connman_network_get_available(network) == TRUE)
1174 void __connman_device_cleanup_networks(struct connman_device *device)
1176 g_hash_table_foreach_remove(device->networks,
1177 remove_unavailable_network, NULL);
1180 static void scanning_changed(struct connman_device *device)
1182 connman_dbus_property_changed_basic(device->element.path,
1183 CONNMAN_DEVICE_INTERFACE, "Scanning",
1184 DBUS_TYPE_BOOLEAN, &device->scanning);
1188 * connman_device_set_scanning:
1189 * @device: device structure
1190 * @scanning: scanning state
1192 * Change scanning state of device
1194 int connman_device_set_scanning(struct connman_device *device,
1195 connman_bool_t scanning)
1197 DBG("device %p scanning %d", device, scanning);
1199 if (!device->driver || !device->driver->scan)
1202 if (device->scanning == scanning)
1205 device->scanning = scanning;
1207 scanning_changed(device);
1209 if (scanning == TRUE) {
1210 reset_scan_trigger(device);
1212 g_hash_table_foreach(device->networks,
1213 mark_network_unavailable, NULL);
1218 __connman_device_cleanup_networks(device);
1220 if (device->connections > 0)
1223 if (device->disconnected == TRUE)
1226 __connman_service_auto_connect();
1232 * connman_device_set_disconnected:
1233 * @device: device structure
1234 * @disconnected: disconnected state
1236 * Change disconnected state of device (only for device with networks)
1238 int connman_device_set_disconnected(struct connman_device *device,
1239 connman_bool_t disconnected)
1241 DBG("device %p disconnected %d", device, disconnected);
1243 switch (device->mode) {
1244 case CONNMAN_DEVICE_MODE_UNKNOWN:
1246 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1247 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1251 if (device->disconnected == disconnected)
1254 device->disconnected = disconnected;
1256 if (disconnected == TRUE)
1257 force_scan_trigger(device);
1263 * connman_device_get_disconnected:
1264 * @device: device structure
1266 * Get device disconnected state
1268 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1270 return device->disconnected;
1274 * connman_device_set_string:
1275 * @device: device structure
1276 * @key: unique identifier
1277 * @value: string value
1279 * Set string value for specific key
1281 int connman_device_set_string(struct connman_device *device,
1282 const char *key, const char *value)
1284 DBG("device %p key %s value %s", device, key, value);
1286 if (g_str_equal(key, "Address") == TRUE) {
1287 g_free(device->address);
1288 device->address = g_strdup(value);
1289 } else if (g_str_equal(key, "Name") == TRUE) {
1290 g_free(device->name);
1291 device->name = g_strdup(value);
1292 } else if (g_str_equal(key, "Node") == TRUE) {
1293 g_free(device->node);
1294 device->node = g_strdup(value);
1297 return connman_element_set_string(&device->element, key, value);
1301 * connman_device_get_string:
1302 * @device: device structure
1303 * @key: unique identifier
1305 * Get string value for specific key
1307 const char *connman_device_get_string(struct connman_device *device,
1310 DBG("device %p key %s", device, key);
1312 if (g_str_equal(key, "Address") == TRUE)
1313 return device->address;
1314 else if (g_str_equal(key, "Name") == TRUE)
1315 return device->name;
1316 else if (g_str_equal(key, "Node") == TRUE)
1317 return device->node;
1319 return connman_element_get_string(&device->element, key);
1322 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1324 struct connman_device *device = element->device;
1325 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1326 connman_bool_t powered;
1328 DBG("element %p name %s", element, element->name);
1333 device->offlinemode = offlinemode;
1335 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1337 if (device->powered == powered)
1340 if (device->powered_persistent == FALSE)
1343 set_powered(device, powered);
1346 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1348 DBG("offlinmode %d", offlinemode);
1350 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1351 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1353 __connman_notifier_offlinemode(offlinemode);
1358 void __connman_device_increase_connections(struct connman_device *device)
1360 device->connections++;
1363 void __connman_device_decrease_connections(struct connman_device *device)
1365 device->connections--;
1369 * connman_device_add_network:
1370 * @device: device structure
1371 * @network: network structure
1373 * Add new network to the device
1375 int connman_device_add_network(struct connman_device *device,
1376 struct connman_network *network)
1378 const char *identifier = connman_network_get_identifier(network);
1381 DBG("device %p network %p", device, network);
1383 if (identifier == NULL)
1386 switch (device->mode) {
1387 case CONNMAN_DEVICE_MODE_UNKNOWN:
1389 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1390 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1394 __connman_network_set_device(network, device);
1396 err = connman_element_register((struct connman_element *) network,
1399 __connman_network_set_device(network, NULL);
1403 g_hash_table_insert(device->networks, g_strdup(identifier),
1410 * connman_device_get_network:
1411 * @device: device structure
1412 * @identifier: network identifier
1414 * Get network for given identifier
1416 struct connman_network *connman_device_get_network(struct connman_device *device,
1417 const char *identifier)
1419 DBG("device %p identifier %s", device, identifier);
1421 return g_hash_table_lookup(device->networks, identifier);
1425 * connman_device_remove_network:
1426 * @device: device structure
1427 * @identifier: network identifier
1429 * Remove network for given identifier
1431 int connman_device_remove_network(struct connman_device *device,
1432 const char *identifier)
1434 DBG("device %p identifier %s", device, identifier);
1436 g_hash_table_remove(device->networks, identifier);
1441 void connman_device_remove_all_networks(struct connman_device *device)
1443 g_hash_table_remove_all(device->networks);
1446 void __connman_device_set_network(struct connman_device *device,
1447 struct connman_network *network)
1454 if (device->network == network)
1457 if (device->network != NULL)
1458 connman_network_unref(device->network);
1460 if (network != NULL) {
1461 name = connman_network_get_string(network,
1462 CONNMAN_PROPERTY_ID_NAME);
1463 g_free(device->last_network);
1464 device->last_network = g_strdup(name);
1466 device->network = connman_network_ref(network);
1468 g_free(device->last_network);
1469 device->last_network = NULL;
1471 device->network = NULL;
1475 void __connman_device_set_reconnect(struct connman_device *device,
1476 connman_bool_t reconnect)
1478 device->reconnect = reconnect;
1481 connman_bool_t __connman_device_get_reconnect(
1482 struct connman_device *device)
1484 return device->reconnect;
1488 * connman_device_register:
1489 * @device: device structure
1491 * Register device with the system
1493 int connman_device_register(struct connman_device *device)
1495 __connman_storage_load_device(device);
1497 device->offlinemode = __connman_profile_get_offlinemode();
1499 return connman_element_register(&device->element, NULL);
1503 * connman_device_unregister:
1504 * @device: device structure
1506 * Unregister device with the system
1508 void connman_device_unregister(struct connman_device *device)
1510 __connman_storage_save_device(device);
1512 connman_element_unregister(&device->element);
1516 * connman_device_get_data:
1517 * @device: device structure
1519 * Get private device data pointer
1521 void *connman_device_get_data(struct connman_device *device)
1523 return device->driver_data;
1527 * connman_device_set_data:
1528 * @device: device structure
1529 * @data: data pointer
1531 * Set private device data pointer
1533 void connman_device_set_data(struct connman_device *device, void *data)
1535 device->driver_data = data;
1538 static gboolean match_driver(struct connman_device *device,
1539 struct connman_device_driver *driver)
1541 if (device->type == driver->type ||
1542 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1548 static int device_probe(struct connman_element *element)
1550 struct connman_device *device = element->device;
1553 DBG("element %p name %s", element, element->name);
1558 if (device->driver != NULL)
1561 for (list = driver_list; list; list = list->next) {
1562 struct connman_device_driver *driver = list->data;
1564 if (match_driver(device, driver) == FALSE)
1567 DBG("driver %p name %s", driver, driver->name);
1569 if (driver->probe(device) == 0) {
1570 device->driver = driver;
1575 if (device->driver == NULL)
1578 return setup_device(device);
1581 static void device_remove(struct connman_element *element)
1583 struct connman_device *device = element->device;
1585 DBG("element %p name %s", element, element->name);
1590 if (device->driver == NULL)
1593 remove_device(device);
1596 static struct connman_driver device_driver = {
1598 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1599 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1600 .probe = device_probe,
1601 .remove = device_remove,
1604 static int device_load(struct connman_device *device)
1606 const char *ident = __connman_profile_active_ident();
1608 GError *error = NULL;
1610 connman_bool_t powered;
1613 DBG("device %p", device);
1615 keyfile = __connman_storage_open_profile(ident);
1616 if (keyfile == NULL)
1619 identifier = g_strdup_printf("device_%s", device->element.name);
1620 if (identifier == NULL)
1623 powered = g_key_file_get_boolean(keyfile, identifier,
1626 device->powered_persistent = powered;
1627 g_clear_error(&error);
1629 switch (device->mode) {
1630 case CONNMAN_DEVICE_MODE_UNKNOWN:
1632 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1633 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1634 val = g_key_file_get_integer(keyfile, identifier,
1635 "ScanInterval", &error);
1636 if (error == NULL && val > 0)
1637 device->scan_interval = val;
1638 g_clear_error(&error);
1645 __connman_storage_close_profile(ident, keyfile, FALSE);
1650 static int device_save(struct connman_device *device)
1652 const char *ident = __connman_profile_active_ident();
1656 DBG("device %p", device);
1658 keyfile = __connman_storage_open_profile(ident);
1659 if (keyfile == NULL)
1662 identifier = g_strdup_printf("device_%s", device->element.name);
1663 if (identifier == NULL)
1666 g_key_file_set_boolean(keyfile, identifier,
1667 "Powered", device->powered_persistent);
1669 switch (device->mode) {
1670 case CONNMAN_DEVICE_MODE_UNKNOWN:
1672 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1673 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1674 if (device->scan_interval > 0)
1675 g_key_file_set_integer(keyfile, identifier,
1676 "ScanInterval", device->scan_interval);
1683 __connman_storage_close_profile(ident, keyfile, TRUE);
1688 static struct connman_storage device_storage = {
1690 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1691 .device_load = device_load,
1692 .device_save = device_save,
1695 int __connman_device_init(void)
1699 connection = connman_dbus_get_connection();
1701 if (connman_storage_register(&device_storage) < 0)
1702 connman_error("Failed to register device storage");
1704 return connman_driver_register(&device_driver);
1707 void __connman_device_cleanup(void)
1711 connman_driver_unregister(&device_driver);
1713 connman_storage_unregister(&device_storage);
1715 dbus_connection_unref(connection);