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);
1187 static void mark_network_available(gpointer key, gpointer value,
1190 struct connman_network *network = value;
1192 connman_network_set_available(network, TRUE);
1195 void connman_device_cleanup_scanning(struct connman_device *device)
1197 device->scanning = FALSE;
1199 scanning_changed(device);
1201 g_hash_table_foreach(device->networks,
1202 mark_network_available, NULL);
1206 * connman_device_set_scanning:
1207 * @device: device structure
1208 * @scanning: scanning state
1210 * Change scanning state of device
1212 int connman_device_set_scanning(struct connman_device *device,
1213 connman_bool_t scanning)
1215 DBG("device %p scanning %d", device, scanning);
1217 if (!device->driver || !device->driver->scan)
1220 if (device->scanning == scanning)
1223 device->scanning = scanning;
1225 scanning_changed(device);
1227 if (scanning == TRUE) {
1228 reset_scan_trigger(device);
1230 g_hash_table_foreach(device->networks,
1231 mark_network_unavailable, NULL);
1236 __connman_device_cleanup_networks(device);
1238 if (device->connections > 0)
1241 if (device->disconnected == TRUE)
1244 __connman_service_auto_connect();
1250 * connman_device_set_disconnected:
1251 * @device: device structure
1252 * @disconnected: disconnected state
1254 * Change disconnected state of device (only for device with networks)
1256 int connman_device_set_disconnected(struct connman_device *device,
1257 connman_bool_t disconnected)
1259 DBG("device %p disconnected %d", device, disconnected);
1261 switch (device->mode) {
1262 case CONNMAN_DEVICE_MODE_UNKNOWN:
1264 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1265 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1269 if (device->disconnected == disconnected)
1272 device->disconnected = disconnected;
1274 if (disconnected == TRUE)
1275 force_scan_trigger(device);
1281 * connman_device_get_disconnected:
1282 * @device: device structure
1284 * Get device disconnected state
1286 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1288 return device->disconnected;
1292 * connman_device_set_string:
1293 * @device: device structure
1294 * @key: unique identifier
1295 * @value: string value
1297 * Set string value for specific key
1299 int connman_device_set_string(struct connman_device *device,
1300 const char *key, const char *value)
1302 DBG("device %p key %s value %s", device, key, value);
1304 if (g_str_equal(key, "Address") == TRUE) {
1305 g_free(device->address);
1306 device->address = g_strdup(value);
1307 } else if (g_str_equal(key, "Name") == TRUE) {
1308 g_free(device->name);
1309 device->name = g_strdup(value);
1310 } else if (g_str_equal(key, "Node") == TRUE) {
1311 g_free(device->node);
1312 device->node = g_strdup(value);
1315 return connman_element_set_string(&device->element, key, value);
1319 * connman_device_get_string:
1320 * @device: device structure
1321 * @key: unique identifier
1323 * Get string value for specific key
1325 const char *connman_device_get_string(struct connman_device *device,
1328 DBG("device %p key %s", device, key);
1330 if (g_str_equal(key, "Address") == TRUE)
1331 return device->address;
1332 else if (g_str_equal(key, "Name") == TRUE)
1333 return device->name;
1334 else if (g_str_equal(key, "Node") == TRUE)
1335 return device->node;
1337 return connman_element_get_string(&device->element, key);
1340 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1342 struct connman_device *device = element->device;
1343 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1344 connman_bool_t powered;
1346 DBG("element %p name %s", element, element->name);
1351 device->offlinemode = offlinemode;
1353 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1355 if (device->powered == powered)
1358 if (device->powered_persistent == FALSE)
1361 set_powered(device, powered);
1364 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1366 DBG("offlinmode %d", offlinemode);
1368 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1369 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1371 __connman_notifier_offlinemode(offlinemode);
1376 void __connman_device_increase_connections(struct connman_device *device)
1378 device->connections++;
1381 void __connman_device_decrease_connections(struct connman_device *device)
1383 device->connections--;
1387 * connman_device_add_network:
1388 * @device: device structure
1389 * @network: network structure
1391 * Add new network to the device
1393 int connman_device_add_network(struct connman_device *device,
1394 struct connman_network *network)
1396 const char *identifier = connman_network_get_identifier(network);
1399 DBG("device %p network %p", device, network);
1401 if (identifier == NULL)
1404 switch (device->mode) {
1405 case CONNMAN_DEVICE_MODE_UNKNOWN:
1407 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1408 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1412 __connman_network_set_device(network, device);
1414 err = connman_element_register((struct connman_element *) network,
1417 __connman_network_set_device(network, NULL);
1421 g_hash_table_insert(device->networks, g_strdup(identifier),
1428 * connman_device_get_network:
1429 * @device: device structure
1430 * @identifier: network identifier
1432 * Get network for given identifier
1434 struct connman_network *connman_device_get_network(struct connman_device *device,
1435 const char *identifier)
1437 DBG("device %p identifier %s", device, identifier);
1439 return g_hash_table_lookup(device->networks, identifier);
1443 * connman_device_remove_network:
1444 * @device: device structure
1445 * @identifier: network identifier
1447 * Remove network for given identifier
1449 int connman_device_remove_network(struct connman_device *device,
1450 const char *identifier)
1452 DBG("device %p identifier %s", device, identifier);
1454 g_hash_table_remove(device->networks, identifier);
1459 void connman_device_remove_all_networks(struct connman_device *device)
1461 g_hash_table_remove_all(device->networks);
1464 void __connman_device_set_network(struct connman_device *device,
1465 struct connman_network *network)
1472 if (device->network == network)
1475 if (device->network != NULL)
1476 connman_network_unref(device->network);
1478 if (network != NULL) {
1479 name = connman_network_get_string(network,
1480 CONNMAN_PROPERTY_ID_NAME);
1481 g_free(device->last_network);
1482 device->last_network = g_strdup(name);
1484 device->network = connman_network_ref(network);
1486 g_free(device->last_network);
1487 device->last_network = NULL;
1489 device->network = NULL;
1493 void __connman_device_set_reconnect(struct connman_device *device,
1494 connman_bool_t reconnect)
1496 device->reconnect = reconnect;
1499 connman_bool_t __connman_device_get_reconnect(
1500 struct connman_device *device)
1502 return device->reconnect;
1506 * connman_device_register:
1507 * @device: device structure
1509 * Register device with the system
1511 int connman_device_register(struct connman_device *device)
1513 __connman_storage_load_device(device);
1515 device->offlinemode = __connman_profile_get_offlinemode();
1517 return connman_element_register(&device->element, NULL);
1521 * connman_device_unregister:
1522 * @device: device structure
1524 * Unregister device with the system
1526 void connman_device_unregister(struct connman_device *device)
1528 __connman_storage_save_device(device);
1530 connman_element_unregister(&device->element);
1534 * connman_device_get_data:
1535 * @device: device structure
1537 * Get private device data pointer
1539 void *connman_device_get_data(struct connman_device *device)
1541 return device->driver_data;
1545 * connman_device_set_data:
1546 * @device: device structure
1547 * @data: data pointer
1549 * Set private device data pointer
1551 void connman_device_set_data(struct connman_device *device, void *data)
1553 device->driver_data = data;
1556 static gboolean match_driver(struct connman_device *device,
1557 struct connman_device_driver *driver)
1559 if (device->type == driver->type ||
1560 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1566 static int device_probe(struct connman_element *element)
1568 struct connman_device *device = element->device;
1571 DBG("element %p name %s", element, element->name);
1576 if (device->driver != NULL)
1579 for (list = driver_list; list; list = list->next) {
1580 struct connman_device_driver *driver = list->data;
1582 if (match_driver(device, driver) == FALSE)
1585 DBG("driver %p name %s", driver, driver->name);
1587 if (driver->probe(device) == 0) {
1588 device->driver = driver;
1593 if (device->driver == NULL)
1596 return setup_device(device);
1599 static void device_remove(struct connman_element *element)
1601 struct connman_device *device = element->device;
1603 DBG("element %p name %s", element, element->name);
1608 if (device->driver == NULL)
1611 remove_device(device);
1614 static struct connman_driver device_driver = {
1616 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1617 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1618 .probe = device_probe,
1619 .remove = device_remove,
1622 static int device_load(struct connman_device *device)
1624 const char *ident = __connman_profile_active_ident();
1626 GError *error = NULL;
1628 connman_bool_t powered;
1631 DBG("device %p", device);
1633 keyfile = __connman_storage_open_profile(ident);
1634 if (keyfile == NULL)
1637 identifier = g_strdup_printf("device_%s", device->element.name);
1638 if (identifier == NULL)
1641 powered = g_key_file_get_boolean(keyfile, identifier,
1644 device->powered_persistent = powered;
1645 g_clear_error(&error);
1647 switch (device->mode) {
1648 case CONNMAN_DEVICE_MODE_UNKNOWN:
1650 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1651 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1652 val = g_key_file_get_integer(keyfile, identifier,
1653 "ScanInterval", &error);
1654 if (error == NULL && val > 0)
1655 device->scan_interval = val;
1656 g_clear_error(&error);
1663 __connman_storage_close_profile(ident, keyfile, FALSE);
1668 static int device_save(struct connman_device *device)
1670 const char *ident = __connman_profile_active_ident();
1674 DBG("device %p", device);
1676 keyfile = __connman_storage_open_profile(ident);
1677 if (keyfile == NULL)
1680 identifier = g_strdup_printf("device_%s", device->element.name);
1681 if (identifier == NULL)
1684 g_key_file_set_boolean(keyfile, identifier,
1685 "Powered", device->powered_persistent);
1687 switch (device->mode) {
1688 case CONNMAN_DEVICE_MODE_UNKNOWN:
1690 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1691 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1692 if (device->scan_interval > 0)
1693 g_key_file_set_integer(keyfile, identifier,
1694 "ScanInterval", device->scan_interval);
1701 __connman_storage_close_profile(ident, keyfile, TRUE);
1706 static struct connman_storage device_storage = {
1708 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1709 .device_load = device_load,
1710 .device_save = device_save,
1713 int __connman_device_init(void)
1717 connection = connman_dbus_get_connection();
1719 if (connman_storage_register(&device_storage) < 0)
1720 connman_error("Failed to register device storage");
1722 return connman_driver_register(&device_driver);
1725 void __connman_device_cleanup(void)
1729 connman_driver_unregister(&device_driver);
1731 connman_storage_unregister(&device_storage);
1733 dbus_connection_unref(connection);