5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
143 static const char *type2string(enum connman_device_type type)
146 case CONNMAN_DEVICE_TYPE_UNKNOWN:
147 case CONNMAN_DEVICE_TYPE_VENDOR:
149 case CONNMAN_DEVICE_TYPE_ETHERNET:
151 case CONNMAN_DEVICE_TYPE_WIFI:
153 case CONNMAN_DEVICE_TYPE_WIMAX:
155 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157 case CONNMAN_DEVICE_TYPE_GPS:
159 case CONNMAN_DEVICE_TYPE_CELLULAR:
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
168 enum connman_device_type type = connman_device_get_type(device);
171 case CONNMAN_DEVICE_TYPE_UNKNOWN:
172 case CONNMAN_DEVICE_TYPE_VENDOR:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_CELLULAR:
184 return CONNMAN_SERVICE_TYPE_CELLULAR;
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 static void powered_changed(struct connman_device *device)
192 connman_dbus_property_changed_basic(device->element.path,
193 CONNMAN_DEVICE_INTERFACE, "Powered",
194 DBUS_TYPE_BOOLEAN, &device->powered);
197 int __connman_device_enable(struct connman_device *device)
201 DBG("device %p", device);
203 if (!device->driver || !device->driver->enable)
206 if (device->powered_pending == TRUE)
209 err = device->driver->enable(device);
211 if (err == -EINPROGRESS)
212 device->powered_pending = TRUE;
216 device->powered_pending = TRUE;
217 device->powered = TRUE;
219 __connman_technology_enable_device(device);
224 int __connman_device_disable(struct connman_device *device)
228 DBG("device %p", device);
230 if (!device->driver || !device->driver->disable)
233 if (device->powered == FALSE)
236 if (device->powered_pending == FALSE)
239 device->reconnect = FALSE;
241 clear_scan_trigger(device);
243 g_hash_table_remove_all(device->networks);
245 err = device->driver->disable(device);
247 if (err == -EINPROGRESS)
248 device->powered_pending = FALSE;
252 device->powered_pending = FALSE;
253 device->powered = FALSE;
255 __connman_technology_disable_device(device);
260 static int set_powered(struct connman_device *device, connman_bool_t powered)
262 DBG("device %p powered %d", device, powered);
265 return __connman_device_enable(device);
267 return __connman_device_disable(device);
270 void __connman_device_list(DBusMessageIter *iter, void *user_data)
272 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
275 static void append_path(gpointer key, gpointer value, gpointer user_data)
277 struct connman_element *element = value;
278 DBusMessageIter *iter = user_data;
280 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
284 static void append_networks(DBusMessageIter *iter, void *user_data)
286 struct connman_device *device = user_data;
288 g_hash_table_foreach(device->networks, append_path, iter);
291 static DBusMessage *get_properties(DBusConnection *conn,
292 DBusMessage *msg, void *data)
294 struct connman_device *device = data;
296 DBusMessageIter array, dict;
299 DBG("conn %p", conn);
301 if (__connman_security_check_privilege(msg,
302 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
303 return __connman_error_permission_denied(msg);
305 reply = dbus_message_new_method_return(msg);
309 dbus_message_iter_init_append(reply, &array);
311 connman_dbus_dict_open(&array, &dict);
313 if (device->name != NULL)
314 connman_dbus_dict_append_basic(&dict, "Name",
315 DBUS_TYPE_STRING, &device->name);
317 str = type2string(device->type);
319 connman_dbus_dict_append_basic(&dict, "Type",
320 DBUS_TYPE_STRING, &str);
322 if (device->address != NULL)
323 connman_dbus_dict_append_basic(&dict, "Address",
324 DBUS_TYPE_STRING, &device->address);
326 if (device->interface != NULL)
327 connman_dbus_dict_append_basic(&dict, "Interface",
328 DBUS_TYPE_STRING, &device->interface);
330 connman_dbus_dict_append_basic(&dict, "Powered",
331 DBUS_TYPE_BOOLEAN, &device->powered);
333 if (device->driver && device->driver->scan)
334 connman_dbus_dict_append_basic(&dict, "Scanning",
335 DBUS_TYPE_BOOLEAN, &device->scanning);
337 switch (device->mode) {
338 case CONNMAN_DEVICE_MODE_UNKNOWN:
340 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
341 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
342 if (device->scan_interval > 0)
343 connman_dbus_dict_append_basic(&dict, "ScanInterval",
344 DBUS_TYPE_UINT16, &device->scan_interval);
346 connman_dbus_dict_append_array(&dict, "Networks",
347 DBUS_TYPE_OBJECT_PATH, append_networks, device);
351 connman_dbus_dict_close(&array, &dict);
356 static gboolean powered_timeout(gpointer user_data)
358 struct connman_device *device = user_data;
360 DBG("device %p", device);
364 if (device->pending != NULL) {
367 reply = __connman_error_operation_timeout(device->pending);
369 g_dbus_send_message(connection, reply);
371 dbus_message_unref(device->pending);
372 device->pending = NULL;
378 static DBusMessage *set_property(DBusConnection *conn,
379 DBusMessage *msg, void *data)
381 struct connman_device *device = data;
382 DBusMessageIter iter, value;
386 DBG("conn %p", conn);
388 if (dbus_message_iter_init(msg, &iter) == FALSE)
389 return __connman_error_invalid_arguments(msg);
391 dbus_message_iter_get_basic(&iter, &name);
392 dbus_message_iter_next(&iter);
393 dbus_message_iter_recurse(&iter, &value);
395 if (__connman_security_check_privilege(msg,
396 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
397 return __connman_error_permission_denied(msg);
399 type = dbus_message_iter_get_arg_type(&value);
401 if (g_str_equal(name, "Powered") == TRUE) {
402 connman_bool_t powered;
405 if (type != DBUS_TYPE_BOOLEAN)
406 return __connman_error_invalid_arguments(msg);
408 dbus_message_iter_get_basic(&value, &powered);
410 device->powered_persistent = powered;
412 __connman_storage_save_device(device);
414 if (device->powered == powered)
415 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
417 if (device->pending != NULL)
418 return __connman_error_in_progress(msg);
420 err = set_powered(device, powered);
422 if (err != -EINPROGRESS)
423 return __connman_error_failed(msg, -err);
425 device->pending = dbus_message_ref(msg);
427 device->timeout = g_timeout_add_seconds(15,
428 powered_timeout, device);
432 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
433 connman_uint16_t interval;
435 switch (device->mode) {
436 case CONNMAN_DEVICE_MODE_UNKNOWN:
437 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
438 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
442 if (type != DBUS_TYPE_UINT16)
443 return __connman_error_invalid_arguments(msg);
445 dbus_message_iter_get_basic(&value, &interval);
447 if (device->scan_interval != interval) {
448 device->scan_interval = interval;
450 __connman_storage_save_device(device);
452 reset_scan_trigger(device);
455 return __connman_error_invalid_property(msg);
457 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
460 static DBusMessage *propose_scan(DBusConnection *conn,
461 DBusMessage *msg, void *data)
463 struct connman_device *device = data;
466 DBG("conn %p", conn);
468 switch (device->mode) {
469 case CONNMAN_DEVICE_MODE_UNKNOWN:
470 return __connman_error_not_supported(msg);
471 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
472 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
476 err = __connman_device_scan(device);
478 return __connman_error_failed(msg, -err);
480 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
483 static GDBusMethodTable device_methods[] = {
484 { "GetProperties", "", "a{sv}", get_properties },
485 { "SetProperty", "sv", "", set_property,
486 G_DBUS_METHOD_FLAG_ASYNC },
487 { "ProposeScan", "", "", propose_scan },
491 static GDBusSignalTable device_signals[] = {
492 { "PropertyChanged", "sv" },
496 static int register_interface(struct connman_element *element)
498 struct connman_device *device = element->device;
500 DBG("element %p name %s", element, element->name);
502 if (g_dbus_register_interface(connection, element->path,
503 CONNMAN_DEVICE_INTERFACE,
504 device_methods, device_signals,
505 NULL, device, NULL) == FALSE) {
506 connman_error("Failed to register %s device", element->path);
510 device->registered = TRUE;
515 static void unregister_interface(struct connman_element *element)
517 struct connman_device *device = element->device;
519 DBG("element %p name %s", element, element->name);
521 device->registered = FALSE;
523 g_dbus_unregister_interface(connection, element->path,
524 CONNMAN_DEVICE_INTERFACE);
527 static int setup_device(struct connman_device *device)
531 DBG("device %p", device);
533 err = register_interface(&device->element);
535 if (device->driver->remove)
536 device->driver->remove(device);
537 device->driver = NULL;
541 __connman_technology_add_device(device);
543 switch (device->mode) {
544 case CONNMAN_DEVICE_MODE_UNKNOWN:
545 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
546 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
550 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
553 if (device->offlinemode == FALSE &&
554 device->powered_persistent == TRUE)
555 __connman_device_enable(device);
560 static void probe_driver(struct connman_element *element, gpointer user_data)
562 struct connman_device_driver *driver = user_data;
564 DBG("element %p name %s", element, element->name);
566 if (element->device == NULL)
569 if (element->device->driver != NULL)
572 if (driver->type != element->device->type)
575 if (driver->probe(element->device) < 0)
578 element->device->driver = driver;
580 setup_device(element->device);
583 static void remove_device(struct connman_device *device)
585 DBG("device %p", device);
587 __connman_device_disable(device);
589 switch (device->mode) {
590 case CONNMAN_DEVICE_MODE_UNKNOWN:
591 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
592 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
596 __connman_technology_remove_device(device);
598 unregister_interface(&device->element);
600 if (device->driver->remove)
601 device->driver->remove(device);
603 device->driver = NULL;
606 static void remove_driver(struct connman_element *element, gpointer user_data)
608 struct connman_device_driver *driver = user_data;
610 DBG("element %p name %s", element, element->name);
612 if (element->device == NULL)
615 if (element->device->driver == driver)
616 remove_device(element->device);
619 connman_bool_t __connman_device_has_driver(struct connman_device *device)
621 if (device == NULL || device->driver == NULL)
624 return device->registered;
627 static GSList *driver_list = NULL;
629 static gint compare_priority(gconstpointer a, gconstpointer b)
631 const struct connman_device_driver *driver1 = a;
632 const struct connman_device_driver *driver2 = b;
634 return driver2->priority - driver1->priority;
638 * connman_device_driver_register:
639 * @driver: device driver definition
641 * Register a new device driver
643 * Returns: %0 on success
645 int connman_device_driver_register(struct connman_device_driver *driver)
647 DBG("driver %p name %s", driver, driver->name);
649 driver_list = g_slist_insert_sorted(driver_list, driver,
652 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
653 probe_driver, driver);
659 * connman_device_driver_unregister:
660 * @driver: device driver definition
662 * Remove a previously registered device driver
664 void connman_device_driver_unregister(struct connman_device_driver *driver)
666 DBG("driver %p name %s", driver, driver->name);
668 driver_list = g_slist_remove(driver_list, driver);
670 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
671 remove_driver, driver);
674 static void unregister_network(gpointer data)
676 struct connman_network *network = data;
678 DBG("network %p", network);
680 connman_element_unregister((struct connman_element *) network);
682 connman_network_unref(network);
685 static void device_destruct(struct connman_element *element)
687 struct connman_device *device = element->device;
689 DBG("element %p name %s", element, element->name);
691 if (device->timeout > 0) {
692 g_source_remove(device->timeout);
696 clear_scan_trigger(device);
698 if (device->pending != NULL) {
699 dbus_message_unref(device->pending);
700 device->pending = NULL;
703 g_free(device->ident);
704 g_free(device->node);
705 g_free(device->name);
706 g_free(device->address);
707 g_free(device->control);
708 g_free(device->interface);
710 g_free(device->last_network);
712 g_hash_table_destroy(device->networks);
713 device->networks = NULL;
717 * connman_device_create:
718 * @node: device node name (for example an address)
721 * Allocate a new device of given #type and assign the #node name to it.
723 * Returns: a newly-allocated #connman_device structure
725 struct connman_device *connman_device_create(const char *node,
726 enum connman_device_type type)
728 struct connman_device *device;
731 DBG("node %s type %d", node, type);
733 device = g_try_new0(struct connman_device, 1);
737 DBG("device %p", device);
739 __connman_element_initialize(&device->element);
741 device->element.name = g_strdup(node);
742 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
744 device->element.device = device;
745 device->element.destruct = device_destruct;
747 str = type2string(type);
749 connman_element_set_string(&device->element,
750 CONNMAN_PROPERTY_ID_TYPE, str);
752 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
755 device->name = g_strdup(type2description(device->type));
756 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
758 device->powered_persistent = TRUE;
760 device->phyindex = -1;
763 case CONNMAN_DEVICE_TYPE_UNKNOWN:
764 case CONNMAN_DEVICE_TYPE_VENDOR:
765 device->scan_interval = 0;
767 case CONNMAN_DEVICE_TYPE_ETHERNET:
768 case CONNMAN_DEVICE_TYPE_WIFI:
769 device->scan_interval = 300;
771 case CONNMAN_DEVICE_TYPE_WIMAX:
772 device->scan_interval = 0;
774 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
775 device->scan_interval = 0;
777 case CONNMAN_DEVICE_TYPE_GPS:
778 device->scan_interval = 0;
780 case CONNMAN_DEVICE_TYPE_CELLULAR:
781 device->scan_interval = 0;
785 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
786 g_free, unregister_network);
792 * connman_device_ref:
793 * @device: device structure
795 * Increase reference counter of device
797 struct connman_device *connman_device_ref(struct connman_device *device)
799 if (connman_element_ref(&device->element) == NULL)
806 * connman_device_unref:
807 * @device: device structure
809 * Decrease reference counter of device
811 void connman_device_unref(struct connman_device *device)
813 connman_element_unref(&device->element);
816 const char *__connman_device_get_type(struct connman_device *device)
818 return type2string(device->type);
822 * connman_device_get_type:
823 * @device: device structure
827 enum connman_device_type connman_device_get_type(struct connman_device *device)
833 * connman_device_get_name:
834 * @device: device structure
836 * Get unique name of device
838 const char *connman_device_get_name(struct connman_device *device)
840 return device->element.name;
844 * connman_device_get_path:
845 * @device: device structure
847 * Get path name of device
849 const char *connman_device_get_path(struct connman_device *device)
851 return device->element.path;
855 * connman_device_set_index:
856 * @device: device structure
857 * @index: index number
859 * Set index number of device
861 void connman_device_set_index(struct connman_device *device, int index)
863 device->element.index = index;
867 * connman_device_get_index:
868 * @device: device structure
870 * Get index number of device
872 int connman_device_get_index(struct connman_device *device)
874 return device->element.index;
877 int __connman_device_get_phyindex(struct connman_device *device)
879 return device->phyindex;
882 void __connman_device_set_phyindex(struct connman_device *device,
885 device->phyindex = phyindex;
889 * connman_device_set_interface:
890 * @device: device structure
891 * @interface: interface name
892 * @control: control interface
894 * Set interface name of device
896 void connman_device_set_interface(struct connman_device *device,
897 const char *interface, const char *control)
899 g_free(device->element.devname);
900 device->element.devname = g_strdup(interface);
902 g_free(device->interface);
903 device->interface = g_strdup(interface);
905 g_free(device->control);
906 device->control = g_strdup(control);
908 if (device->name == NULL) {
909 const char *str = type2description(device->type);
910 if (str != NULL && device->interface != NULL)
911 device->name = g_strdup_printf("%s (%s)", str,
916 const char *connman_device_get_control(struct connman_device *device)
918 return device->control;
922 * connman_device_set_ident:
923 * @device: device structure
924 * @ident: unique identifier
926 * Set unique identifier of device
928 void connman_device_set_ident(struct connman_device *device,
931 g_free(device->ident);
932 device->ident = g_strdup(ident);
935 const char *__connman_device_get_ident(struct connman_device *device)
937 return device->ident;
941 * connman_device_set_mode:
942 * @device: device structure
943 * @mode: network mode
945 * Change network mode of device
947 void connman_device_set_mode(struct connman_device *device,
948 enum connman_device_mode mode)
954 * connman_device_get_mode:
955 * @device: device structure
957 * Get network mode of device
959 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
965 * connman_device_set_powered:
966 * @device: device structure
967 * @powered: powered state
969 * Change power state of device
971 int connman_device_set_powered(struct connman_device *device,
972 connman_bool_t powered)
974 DBG("driver %p powered %d", device, powered);
976 if (device->timeout > 0) {
977 g_source_remove(device->timeout);
981 if (device->pending != NULL) {
982 g_dbus_send_reply(connection, device->pending,
985 dbus_message_unref(device->pending);
986 device->pending = NULL;
989 if (device->powered == powered)
993 __connman_device_enable(device);
995 __connman_device_disable(device);
997 device->powered = powered;
998 device->powered_pending = powered;
1000 if (device->powered == TRUE)
1001 __connman_technology_enable_device(device);
1003 __connman_technology_disable_device(device);
1005 if (device->registered == FALSE)
1008 powered_changed(device);
1010 if (powered == FALSE)
1013 reset_scan_trigger(device);
1015 if (device->driver->scan)
1016 device->driver->scan(device);
1021 int __connman_device_set_blocked(struct connman_device *device,
1022 connman_bool_t blocked)
1024 connman_bool_t powered;
1026 DBG("device %p blocked %d", device, blocked);
1028 device->blocked = blocked;
1030 if (device->offlinemode == TRUE)
1033 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1035 if (blocked == FALSE)
1036 powered = device->powered_persistent;
1040 return set_powered(device, powered);
1043 int __connman_device_scan(struct connman_device *device)
1045 if (!device->driver || !device->driver->scan)
1048 if (device->powered == FALSE)
1051 reset_scan_trigger(device);
1053 return device->driver->scan(device);
1056 int __connman_device_enable_persistent(struct connman_device *device)
1060 DBG("device %p", device);
1062 device->powered_persistent = TRUE;
1064 __connman_storage_save_device(device);
1066 err = __connman_device_enable(device);
1067 if (err == 0 || err == -EINPROGRESS) {
1068 if (__connman_profile_get_offlinemode() == TRUE)
1069 __connman_profile_set_offlinemode(FALSE, FALSE);
1076 int __connman_device_disable_persistent(struct connman_device *device)
1078 DBG("device %p", device);
1080 device->powered_persistent = FALSE;
1082 __connman_storage_save_device(device);
1084 return __connman_device_disable(device);
1087 int __connman_device_disconnect(struct connman_device *device)
1089 GHashTableIter iter;
1090 gpointer key, value;
1092 DBG("device %p", device);
1094 connman_device_set_disconnected(device, TRUE);
1096 g_hash_table_iter_init(&iter, device->networks);
1098 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1099 struct connman_network *network = value;
1101 if (__connman_network_get_connecting(network) == TRUE) {
1103 * Skip network in the process of connecting.
1104 * This is a workaround for WiFi networks serviced
1105 * by the supplicant plugin that hold a reference
1106 * to the network. If we disconnect the network
1107 * here then the referenced object will not be
1108 * registered and usage (like launching DHCP client)
1109 * will fail. There is nothing to be gained by
1110 * removing the network here anyway.
1112 connman_warn("Skipping disconnect of %s",
1113 connman_network_get_identifier(network));
1117 __connman_network_disconnect(network);
1123 static void mark_network_unavailable(gpointer key, gpointer value,
1126 struct connman_network *network = value;
1128 if (connman_network_get_connected(network) == TRUE)
1131 connman_network_set_available(network, FALSE);
1134 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1137 struct connman_network *network = value;
1139 if (connman_network_get_connected(network) == TRUE)
1142 if (connman_network_get_available(network) == TRUE)
1148 void __connman_device_cleanup_networks(struct connman_device *device)
1150 g_hash_table_foreach_remove(device->networks,
1151 remove_unavailable_network, NULL);
1154 static void scanning_changed(struct connman_device *device)
1156 connman_dbus_property_changed_basic(device->element.path,
1157 CONNMAN_DEVICE_INTERFACE, "Scanning",
1158 DBUS_TYPE_BOOLEAN, &device->scanning);
1162 * connman_device_set_scanning:
1163 * @device: device structure
1164 * @scanning: scanning state
1166 * Change scanning state of device
1168 int connman_device_set_scanning(struct connman_device *device,
1169 connman_bool_t scanning)
1171 DBG("device %p scanning %d", device, scanning);
1173 if (!device->driver || !device->driver->scan)
1176 if (device->scanning == scanning)
1179 device->scanning = scanning;
1181 scanning_changed(device);
1183 if (scanning == TRUE) {
1184 reset_scan_trigger(device);
1186 g_hash_table_foreach(device->networks,
1187 mark_network_unavailable, NULL);
1192 __connman_device_cleanup_networks(device);
1194 if (device->connections > 0)
1197 if (device->disconnected == TRUE)
1200 __connman_service_auto_connect();
1206 * connman_device_set_disconnected:
1207 * @device: device structure
1208 * @disconnected: disconnected state
1210 * Change disconnected state of device (only for device with networks)
1212 int connman_device_set_disconnected(struct connman_device *device,
1213 connman_bool_t disconnected)
1215 DBG("device %p disconnected %d", device, disconnected);
1217 switch (device->mode) {
1218 case CONNMAN_DEVICE_MODE_UNKNOWN:
1220 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1221 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1225 if (device->disconnected == disconnected)
1228 device->disconnected = disconnected;
1230 if (disconnected == TRUE)
1231 force_scan_trigger(device);
1237 * connman_device_get_disconnected:
1238 * @device: device structure
1240 * Get device disconnected state
1242 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1244 return device->disconnected;
1248 * connman_device_set_string:
1249 * @device: device structure
1250 * @key: unique identifier
1251 * @value: string value
1253 * Set string value for specific key
1255 int connman_device_set_string(struct connman_device *device,
1256 const char *key, const char *value)
1258 DBG("device %p key %s value %s", device, key, value);
1260 if (g_str_equal(key, "Address") == TRUE) {
1261 g_free(device->address);
1262 device->address = g_strdup(value);
1263 } else if (g_str_equal(key, "Name") == TRUE) {
1264 g_free(device->name);
1265 device->name = g_strdup(value);
1266 } else if (g_str_equal(key, "Node") == TRUE) {
1267 g_free(device->node);
1268 device->node = g_strdup(value);
1271 return connman_element_set_string(&device->element, key, value);
1275 * connman_device_get_string:
1276 * @device: device structure
1277 * @key: unique identifier
1279 * Get string value for specific key
1281 const char *connman_device_get_string(struct connman_device *device,
1284 DBG("device %p key %s", device, key);
1286 if (g_str_equal(key, "Address") == TRUE)
1287 return device->address;
1288 else if (g_str_equal(key, "Name") == TRUE)
1289 return device->name;
1290 else if (g_str_equal(key, "Node") == TRUE)
1291 return device->node;
1293 return connman_element_get_string(&device->element, key);
1296 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1298 struct connman_device *device = element->device;
1299 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1300 connman_bool_t powered;
1302 DBG("element %p name %s", element, element->name);
1307 device->offlinemode = offlinemode;
1309 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1311 if (device->powered == powered)
1314 if (device->powered_persistent == FALSE)
1317 set_powered(device, powered);
1320 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1322 DBG("offlinmode %d", offlinemode);
1324 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1325 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1327 __connman_notifier_offlinemode(offlinemode);
1332 void __connman_device_increase_connections(struct connman_device *device)
1334 device->connections++;
1337 void __connman_device_decrease_connections(struct connman_device *device)
1339 device->connections--;
1343 * connman_device_add_network:
1344 * @device: device structure
1345 * @network: network structure
1347 * Add new network to the device
1349 int connman_device_add_network(struct connman_device *device,
1350 struct connman_network *network)
1352 const char *identifier = connman_network_get_identifier(network);
1355 DBG("device %p network %p", device, network);
1357 if (identifier == NULL)
1360 switch (device->mode) {
1361 case CONNMAN_DEVICE_MODE_UNKNOWN:
1363 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1364 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1368 __connman_network_set_device(network, device);
1370 err = connman_element_register((struct connman_element *) network,
1373 __connman_network_set_device(network, NULL);
1377 g_hash_table_insert(device->networks, g_strdup(identifier),
1384 * connman_device_get_network:
1385 * @device: device structure
1386 * @identifier: network identifier
1388 * Get network for given identifier
1390 struct connman_network *connman_device_get_network(struct connman_device *device,
1391 const char *identifier)
1393 DBG("device %p identifier %s", device, identifier);
1395 return g_hash_table_lookup(device->networks, identifier);
1399 * connman_device_remove_network:
1400 * @device: device structure
1401 * @identifier: network identifier
1403 * Remove network for given identifier
1405 int connman_device_remove_network(struct connman_device *device,
1406 const char *identifier)
1408 DBG("device %p identifier %s", device, identifier);
1410 g_hash_table_remove(device->networks, identifier);
1415 void connman_device_remove_all_networks(struct connman_device *device)
1417 g_hash_table_remove_all(device->networks);
1420 void __connman_device_set_network(struct connman_device *device,
1421 struct connman_network *network)
1428 if (device->network == network)
1431 if (device->network != NULL)
1432 connman_network_unref(device->network);
1434 if (network != NULL) {
1435 name = connman_network_get_string(network,
1436 CONNMAN_PROPERTY_ID_NAME);
1437 g_free(device->last_network);
1438 device->last_network = g_strdup(name);
1440 device->network = connman_network_ref(network);
1442 g_free(device->last_network);
1443 device->last_network = NULL;
1445 device->network = NULL;
1449 void __connman_device_set_reconnect(struct connman_device *device,
1450 connman_bool_t reconnect)
1452 device->reconnect = reconnect;
1455 connman_bool_t __connman_device_get_reconnect(
1456 struct connman_device *device)
1458 return device->reconnect;
1462 * connman_device_register:
1463 * @device: device structure
1465 * Register device with the system
1467 int connman_device_register(struct connman_device *device)
1469 __connman_storage_load_device(device);
1471 device->offlinemode = __connman_profile_get_offlinemode();
1473 return connman_element_register(&device->element, NULL);
1477 * connman_device_unregister:
1478 * @device: device structure
1480 * Unregister device with the system
1482 void connman_device_unregister(struct connman_device *device)
1484 __connman_storage_save_device(device);
1486 connman_element_unregister(&device->element);
1490 * connman_device_get_data:
1491 * @device: device structure
1493 * Get private device data pointer
1495 void *connman_device_get_data(struct connman_device *device)
1497 return device->driver_data;
1501 * connman_device_set_data:
1502 * @device: device structure
1503 * @data: data pointer
1505 * Set private device data pointer
1507 void connman_device_set_data(struct connman_device *device, void *data)
1509 device->driver_data = data;
1512 static gboolean match_driver(struct connman_device *device,
1513 struct connman_device_driver *driver)
1515 if (device->type == driver->type ||
1516 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1522 static int device_probe(struct connman_element *element)
1524 struct connman_device *device = element->device;
1527 DBG("element %p name %s", element, element->name);
1532 if (device->driver != NULL)
1535 for (list = driver_list; list; list = list->next) {
1536 struct connman_device_driver *driver = list->data;
1538 if (match_driver(device, driver) == FALSE)
1541 DBG("driver %p name %s", driver, driver->name);
1543 if (driver->probe(device) == 0) {
1544 device->driver = driver;
1549 if (device->driver == NULL)
1552 return setup_device(device);
1555 static void device_remove(struct connman_element *element)
1557 struct connman_device *device = element->device;
1559 DBG("element %p name %s", element, element->name);
1564 if (device->driver == NULL)
1567 remove_device(device);
1570 static struct connman_driver device_driver = {
1572 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1573 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1574 .probe = device_probe,
1575 .remove = device_remove,
1578 static int device_load(struct connman_device *device)
1580 const char *ident = __connman_profile_active_ident();
1582 GError *error = NULL;
1584 connman_bool_t powered;
1587 DBG("device %p", device);
1589 keyfile = __connman_storage_open_profile(ident);
1590 if (keyfile == NULL)
1593 identifier = g_strdup_printf("device_%s", device->element.name);
1594 if (identifier == NULL)
1597 powered = g_key_file_get_boolean(keyfile, identifier,
1600 device->powered_persistent = powered;
1601 g_clear_error(&error);
1603 switch (device->mode) {
1604 case CONNMAN_DEVICE_MODE_UNKNOWN:
1606 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1607 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1608 val = g_key_file_get_integer(keyfile, identifier,
1609 "ScanInterval", &error);
1610 if (error == NULL && val > 0)
1611 device->scan_interval = val;
1612 g_clear_error(&error);
1619 __connman_storage_close_profile(ident, keyfile, FALSE);
1624 static int device_save(struct connman_device *device)
1626 const char *ident = __connman_profile_active_ident();
1630 DBG("device %p", device);
1632 keyfile = __connman_storage_open_profile(ident);
1633 if (keyfile == NULL)
1636 identifier = g_strdup_printf("device_%s", device->element.name);
1637 if (identifier == NULL)
1640 g_key_file_set_boolean(keyfile, identifier,
1641 "Powered", device->powered_persistent);
1643 switch (device->mode) {
1644 case CONNMAN_DEVICE_MODE_UNKNOWN:
1646 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1647 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1648 if (device->scan_interval > 0)
1649 g_key_file_set_integer(keyfile, identifier,
1650 "ScanInterval", device->scan_interval);
1657 __connman_storage_close_profile(ident, keyfile, TRUE);
1662 static struct connman_storage device_storage = {
1664 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1665 .device_load = device_load,
1666 .device_save = device_save,
1669 int __connman_device_init(void)
1673 connection = connman_dbus_get_connection();
1675 if (connman_storage_register(&device_storage) < 0)
1676 connman_error("Failed to register device storage");
1678 return connman_driver_register(&device_driver);
1681 void __connman_device_cleanup(void)
1685 connman_driver_unregister(&device_driver);
1687 connman_storage_unregister(&device_storage);
1689 dbus_connection_unref(connection);