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 void blocked_changed(struct connman_device *device)
199 connman_dbus_property_changed_basic(device->element.path,
200 CONNMAN_DEVICE_INTERFACE, "Blocked",
201 DBUS_TYPE_BOOLEAN, &device->blocked);
204 int __connman_device_enable(struct connman_device *device)
208 DBG("device %p", device);
210 if (!device->driver || !device->driver->enable)
213 if (device->powered_pending == TRUE)
216 if (device->blocked == TRUE)
219 err = device->driver->enable(device);
221 if (err == -EINPROGRESS) {
222 device->powered_pending = TRUE;
223 device->offlinemode = FALSE;
224 if (__connman_profile_get_offlinemode() == TRUE)
225 __connman_profile_set_offlinemode(FALSE, FALSE);
230 device->powered_pending = TRUE;
231 device->powered = TRUE;
232 device->offlinemode = FALSE;
233 if (__connman_profile_get_offlinemode() == TRUE)
234 __connman_profile_set_offlinemode(FALSE, FALSE);
236 __connman_technology_enable_device(device);
241 int __connman_device_disable(struct connman_device *device)
245 DBG("device %p", device);
247 if (!device->driver || !device->driver->disable)
250 if (device->powered == FALSE)
253 if (device->powered_pending == FALSE)
256 device->reconnect = FALSE;
258 clear_scan_trigger(device);
260 g_hash_table_remove_all(device->networks);
262 err = device->driver->disable(device);
264 if (err == -EINPROGRESS)
265 device->powered_pending = FALSE;
269 device->powered_pending = FALSE;
270 device->powered = FALSE;
272 __connman_technology_disable_device(device);
277 static int set_powered(struct connman_device *device, connman_bool_t powered)
279 DBG("device %p powered %d", device, powered);
282 return __connman_device_enable(device);
284 return __connman_device_disable(device);
287 void __connman_device_list(DBusMessageIter *iter, void *user_data)
289 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
292 static void append_path(gpointer key, gpointer value, gpointer user_data)
294 struct connman_element *element = value;
295 DBusMessageIter *iter = user_data;
297 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
301 static void append_networks(DBusMessageIter *iter, void *user_data)
303 struct connman_device *device = user_data;
305 g_hash_table_foreach(device->networks, append_path, iter);
308 static DBusMessage *get_properties(DBusConnection *conn,
309 DBusMessage *msg, void *data)
311 struct connman_device *device = data;
313 DBusMessageIter array, dict;
316 DBG("conn %p", conn);
318 reply = dbus_message_new_method_return(msg);
322 dbus_message_iter_init_append(reply, &array);
324 connman_dbus_dict_open(&array, &dict);
326 if (device->name != NULL)
327 connman_dbus_dict_append_basic(&dict, "Name",
328 DBUS_TYPE_STRING, &device->name);
330 str = type2string(device->type);
332 connman_dbus_dict_append_basic(&dict, "Type",
333 DBUS_TYPE_STRING, &str);
335 if (device->address != NULL)
336 connman_dbus_dict_append_basic(&dict, "Address",
337 DBUS_TYPE_STRING, &device->address);
339 if (device->interface != NULL)
340 connman_dbus_dict_append_basic(&dict, "Interface",
341 DBUS_TYPE_STRING, &device->interface);
343 connman_dbus_dict_append_basic(&dict, "Powered",
344 DBUS_TYPE_BOOLEAN, &device->powered);
346 connman_dbus_dict_append_basic(&dict, "Blocked",
347 DBUS_TYPE_BOOLEAN, &device->blocked);
349 if (device->driver && device->driver->scan)
350 connman_dbus_dict_append_basic(&dict, "Scanning",
351 DBUS_TYPE_BOOLEAN, &device->scanning);
353 switch (device->mode) {
354 case CONNMAN_DEVICE_MODE_UNKNOWN:
356 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
357 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
358 if (device->scan_interval > 0)
359 connman_dbus_dict_append_basic(&dict, "ScanInterval",
360 DBUS_TYPE_UINT16, &device->scan_interval);
362 connman_dbus_dict_append_array(&dict, "Networks",
363 DBUS_TYPE_OBJECT_PATH, append_networks, device);
367 connman_dbus_dict_close(&array, &dict);
372 static gboolean powered_timeout(gpointer user_data)
374 struct connman_device *device = user_data;
376 DBG("device %p", device);
380 if (device->pending != NULL) {
383 reply = __connman_error_operation_timeout(device->pending);
385 g_dbus_send_message(connection, reply);
387 dbus_message_unref(device->pending);
388 device->pending = NULL;
394 static DBusMessage *set_property(DBusConnection *conn,
395 DBusMessage *msg, void *data)
397 struct connman_device *device = data;
398 DBusMessageIter iter, value;
402 DBG("conn %p", conn);
404 if (dbus_message_iter_init(msg, &iter) == FALSE)
405 return __connman_error_invalid_arguments(msg);
407 dbus_message_iter_get_basic(&iter, &name);
408 dbus_message_iter_next(&iter);
409 dbus_message_iter_recurse(&iter, &value);
411 type = dbus_message_iter_get_arg_type(&value);
413 if (g_str_equal(name, "Powered") == TRUE) {
414 connman_bool_t powered;
417 if (type != DBUS_TYPE_BOOLEAN)
418 return __connman_error_invalid_arguments(msg);
420 dbus_message_iter_get_basic(&value, &powered);
422 device->powered_persistent = powered;
424 __connman_storage_save_device(device);
426 if (device->powered == powered)
427 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
429 if (device->pending != NULL)
430 return __connman_error_in_progress(msg);
432 err = set_powered(device, powered);
434 if (err != -EINPROGRESS)
435 return __connman_error_failed(msg, -err);
437 device->pending = dbus_message_ref(msg);
439 device->timeout = g_timeout_add_seconds(15,
440 powered_timeout, device);
444 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
445 connman_uint16_t interval;
447 switch (device->mode) {
448 case CONNMAN_DEVICE_MODE_UNKNOWN:
449 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
450 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
454 if (type != DBUS_TYPE_UINT16)
455 return __connman_error_invalid_arguments(msg);
457 dbus_message_iter_get_basic(&value, &interval);
459 if (device->scan_interval != interval) {
460 device->scan_interval = interval;
462 __connman_storage_save_device(device);
464 reset_scan_trigger(device);
467 return __connman_error_invalid_property(msg);
469 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
472 static DBusMessage *propose_scan(DBusConnection *conn,
473 DBusMessage *msg, void *data)
475 struct connman_device *device = data;
478 DBG("conn %p", conn);
480 switch (device->mode) {
481 case CONNMAN_DEVICE_MODE_UNKNOWN:
482 return __connman_error_not_supported(msg);
483 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
484 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
488 err = __connman_device_scan(device);
490 return __connman_error_failed(msg, -err);
492 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
495 static GDBusMethodTable device_methods[] = {
496 { "GetProperties", "", "a{sv}", get_properties },
497 { "SetProperty", "sv", "", set_property,
498 G_DBUS_METHOD_FLAG_ASYNC },
499 { "ProposeScan", "", "", propose_scan },
503 static GDBusSignalTable device_signals[] = {
504 { "PropertyChanged", "sv" },
508 static int register_interface(struct connman_element *element)
510 struct connman_device *device = element->device;
512 DBG("element %p name %s", element, element->name);
514 if (g_dbus_register_interface(connection, element->path,
515 CONNMAN_DEVICE_INTERFACE,
516 device_methods, device_signals,
517 NULL, device, NULL) == FALSE) {
518 connman_error("Failed to register %s device", element->path);
522 device->registered = TRUE;
527 static void unregister_interface(struct connman_element *element)
529 struct connman_device *device = element->device;
531 DBG("element %p name %s", element, element->name);
533 device->registered = FALSE;
535 g_dbus_unregister_interface(connection, element->path,
536 CONNMAN_DEVICE_INTERFACE);
539 static int setup_device(struct connman_device *device)
543 DBG("device %p", device);
545 err = register_interface(&device->element);
547 if (device->driver->remove)
548 device->driver->remove(device);
549 device->driver = NULL;
553 __connman_technology_add_device(device);
555 switch (device->mode) {
556 case CONNMAN_DEVICE_MODE_UNKNOWN:
557 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
558 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
562 if (__connman_detect_get_blocked(device->phyindex) == TRUE)
565 if (device->offlinemode == FALSE &&
566 device->powered_persistent == TRUE)
567 __connman_device_enable(device);
572 static void probe_driver(struct connman_element *element, gpointer user_data)
574 struct connman_device_driver *driver = user_data;
576 DBG("element %p name %s", element, element->name);
578 if (element->device == NULL)
581 if (element->device->driver != NULL)
584 if (driver->type != element->device->type)
587 if (driver->probe(element->device) < 0)
590 element->device->driver = driver;
592 setup_device(element->device);
595 static void remove_device(struct connman_device *device)
599 DBG("device %p", device);
601 err = __connman_device_disable(device);
602 if (err < 0 && err == -EINPROGRESS)
603 __connman_technology_disable_device(device);
605 switch (device->mode) {
606 case CONNMAN_DEVICE_MODE_UNKNOWN:
607 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
608 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
612 __connman_technology_remove_device(device);
614 unregister_interface(&device->element);
616 if (device->driver->remove)
617 device->driver->remove(device);
619 device->driver = NULL;
622 static void remove_driver(struct connman_element *element, gpointer user_data)
624 struct connman_device_driver *driver = user_data;
626 DBG("element %p name %s", element, element->name);
628 if (element->device == NULL)
631 if (element->device->driver == driver)
632 remove_device(element->device);
635 connman_bool_t __connman_device_has_driver(struct connman_device *device)
637 if (device == NULL || device->driver == NULL)
640 return device->registered;
643 static GSList *driver_list = NULL;
645 static gint compare_priority(gconstpointer a, gconstpointer b)
647 const struct connman_device_driver *driver1 = a;
648 const struct connman_device_driver *driver2 = b;
650 return driver2->priority - driver1->priority;
654 * connman_device_driver_register:
655 * @driver: device driver definition
657 * Register a new device driver
659 * Returns: %0 on success
661 int connman_device_driver_register(struct connman_device_driver *driver)
663 DBG("driver %p name %s", driver, driver->name);
665 driver_list = g_slist_insert_sorted(driver_list, driver,
668 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
669 probe_driver, driver);
675 * connman_device_driver_unregister:
676 * @driver: device driver definition
678 * Remove a previously registered device driver
680 void connman_device_driver_unregister(struct connman_device_driver *driver)
682 DBG("driver %p name %s", driver, driver->name);
684 driver_list = g_slist_remove(driver_list, driver);
686 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
687 remove_driver, driver);
690 static void unregister_network(gpointer data)
692 struct connman_network *network = data;
694 DBG("network %p", network);
696 connman_element_unregister((struct connman_element *) network);
698 __connman_network_set_device(network, NULL);
700 connman_network_unref(network);
703 static void device_destruct(struct connman_element *element)
705 struct connman_device *device = element->device;
707 DBG("element %p name %s", element, element->name);
709 if (device->timeout > 0) {
710 g_source_remove(device->timeout);
714 clear_scan_trigger(device);
716 if (device->pending != NULL) {
717 dbus_message_unref(device->pending);
718 device->pending = NULL;
721 g_free(device->ident);
722 g_free(device->node);
723 g_free(device->name);
724 g_free(device->address);
725 g_free(device->control);
726 g_free(device->interface);
728 g_free(device->last_network);
730 g_hash_table_destroy(device->networks);
731 device->networks = NULL;
735 * connman_device_create:
736 * @node: device node name (for example an address)
739 * Allocate a new device of given #type and assign the #node name to it.
741 * Returns: a newly-allocated #connman_device structure
743 struct connman_device *connman_device_create(const char *node,
744 enum connman_device_type type)
746 struct connman_device *device;
749 DBG("node %s type %d", node, type);
751 device = g_try_new0(struct connman_device, 1);
755 DBG("device %p", device);
757 __connman_element_initialize(&device->element);
759 device->element.name = g_strdup(node);
760 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
762 device->element.device = device;
763 device->element.destruct = device_destruct;
765 str = type2string(type);
767 connman_element_set_string(&device->element,
768 CONNMAN_PROPERTY_ID_TYPE, str);
770 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
773 device->name = g_strdup(type2description(device->type));
774 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
776 device->powered_persistent = TRUE;
778 device->phyindex = -1;
781 case CONNMAN_DEVICE_TYPE_UNKNOWN:
782 case CONNMAN_DEVICE_TYPE_VENDOR:
783 device->scan_interval = 0;
785 case CONNMAN_DEVICE_TYPE_ETHERNET:
786 case CONNMAN_DEVICE_TYPE_WIFI:
787 device->scan_interval = 300;
789 case CONNMAN_DEVICE_TYPE_WIMAX:
790 device->scan_interval = 0;
792 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
793 device->scan_interval = 0;
795 case CONNMAN_DEVICE_TYPE_GPS:
796 device->scan_interval = 0;
798 case CONNMAN_DEVICE_TYPE_CELLULAR:
799 device->scan_interval = 0;
803 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
804 g_free, unregister_network);
810 * connman_device_ref:
811 * @device: device structure
813 * Increase reference counter of device
815 struct connman_device *connman_device_ref(struct connman_device *device)
817 if (connman_element_ref(&device->element) == NULL)
824 * connman_device_unref:
825 * @device: device structure
827 * Decrease reference counter of device
829 void connman_device_unref(struct connman_device *device)
831 connman_element_unref(&device->element);
834 const char *__connman_device_get_type(struct connman_device *device)
836 return type2string(device->type);
840 * connman_device_get_type:
841 * @device: device structure
845 enum connman_device_type connman_device_get_type(struct connman_device *device)
851 * connman_device_get_name:
852 * @device: device structure
854 * Get unique name of device
856 const char *connman_device_get_name(struct connman_device *device)
858 return device->element.name;
862 * connman_device_get_path:
863 * @device: device structure
865 * Get path name of device
867 const char *connman_device_get_path(struct connman_device *device)
869 return device->element.path;
873 * connman_device_set_index:
874 * @device: device structure
875 * @index: index number
877 * Set index number of device
879 void connman_device_set_index(struct connman_device *device, int index)
881 device->element.index = index;
885 * connman_device_get_index:
886 * @device: device structure
888 * Get index number of device
890 int connman_device_get_index(struct connman_device *device)
892 return device->element.index;
895 int __connman_device_get_phyindex(struct connman_device *device)
897 return device->phyindex;
900 void __connman_device_set_phyindex(struct connman_device *device,
903 device->phyindex = phyindex;
907 * connman_device_set_interface:
908 * @device: device structure
909 * @interface: interface name
910 * @control: control interface
912 * Set interface name of device
914 void connman_device_set_interface(struct connman_device *device,
915 const char *interface, const char *control)
917 g_free(device->element.devname);
918 device->element.devname = g_strdup(interface);
920 g_free(device->interface);
921 device->interface = g_strdup(interface);
923 g_free(device->control);
924 device->control = g_strdup(control);
926 if (device->name == NULL) {
927 const char *str = type2description(device->type);
928 if (str != NULL && device->interface != NULL)
929 device->name = g_strdup_printf("%s (%s)", str,
934 const char *connman_device_get_control(struct connman_device *device)
936 return device->control;
940 * connman_device_set_ident:
941 * @device: device structure
942 * @ident: unique identifier
944 * Set unique identifier of device
946 void connman_device_set_ident(struct connman_device *device,
949 g_free(device->ident);
950 device->ident = g_strdup(ident);
953 const char *connman_device_get_ident(struct connman_device *device)
955 return device->ident;
959 * connman_device_set_mode:
960 * @device: device structure
961 * @mode: network mode
963 * Change network mode of device
965 void connman_device_set_mode(struct connman_device *device,
966 enum connman_device_mode mode)
972 * connman_device_get_mode:
973 * @device: device structure
975 * Get network mode of device
977 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
983 * connman_device_set_powered:
984 * @device: device structure
985 * @powered: powered state
987 * Change power state of device
989 int connman_device_set_powered(struct connman_device *device,
990 connman_bool_t powered)
992 DBG("driver %p powered %d", device, powered);
994 if (device->timeout > 0) {
995 g_source_remove(device->timeout);
999 if (device->pending != NULL) {
1000 g_dbus_send_reply(connection, device->pending,
1003 dbus_message_unref(device->pending);
1004 device->pending = NULL;
1007 if (device->powered == powered) {
1008 device->powered_pending = powered;
1012 if (powered == TRUE)
1013 __connman_device_enable(device);
1015 __connman_device_disable(device);
1017 device->powered = powered;
1018 device->powered_pending = powered;
1020 if (device->powered == TRUE)
1021 __connman_technology_enable_device(device);
1023 __connman_technology_disable_device(device);
1025 if (device->offlinemode == TRUE && powered == TRUE) {
1026 powered_changed(device);
1027 return connman_device_set_powered(device, FALSE);
1030 if (device->registered == FALSE)
1033 powered_changed(device);
1035 if (powered == FALSE)
1038 reset_scan_trigger(device);
1040 if (device->driver->scan)
1041 device->driver->scan(device);
1046 int __connman_device_set_blocked(struct connman_device *device,
1047 connman_bool_t blocked)
1049 connman_bool_t powered;
1051 DBG("device %p blocked %d", device, blocked);
1053 device->blocked = blocked;
1055 blocked_changed(device);
1057 if (device->offlinemode == TRUE)
1060 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1062 if (blocked == FALSE)
1063 powered = device->powered_persistent;
1067 return set_powered(device, powered);
1070 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1072 return device->blocked;
1075 int __connman_device_scan(struct connman_device *device)
1077 if (!device->driver || !device->driver->scan)
1080 if (device->powered == FALSE)
1083 reset_scan_trigger(device);
1085 return device->driver->scan(device);
1088 int __connman_device_enable_persistent(struct connman_device *device)
1092 DBG("device %p", device);
1094 device->powered_persistent = TRUE;
1096 __connman_storage_save_device(device);
1098 err = __connman_device_enable(device);
1099 if (err == 0 || err == -EINPROGRESS) {
1100 device->offlinemode = FALSE;
1101 if (__connman_profile_get_offlinemode() == TRUE) {
1102 __connman_profile_set_offlinemode(FALSE, FALSE);
1104 __connman_profile_save_default();
1111 int __connman_device_disable_persistent(struct connman_device *device)
1113 DBG("device %p", device);
1115 device->powered_persistent = FALSE;
1117 __connman_storage_save_device(device);
1119 return __connman_device_disable(device);
1122 int __connman_device_disconnect(struct connman_device *device)
1124 GHashTableIter iter;
1125 gpointer key, value;
1127 DBG("device %p", device);
1129 connman_device_set_disconnected(device, TRUE);
1131 g_hash_table_iter_init(&iter, device->networks);
1133 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1134 struct connman_network *network = value;
1136 if (__connman_network_get_connecting(network) == TRUE) {
1138 * Skip network in the process of connecting.
1139 * This is a workaround for WiFi networks serviced
1140 * by the supplicant plugin that hold a reference
1141 * to the network. If we disconnect the network
1142 * here then the referenced object will not be
1143 * registered and usage (like launching DHCP client)
1144 * will fail. There is nothing to be gained by
1145 * removing the network here anyway.
1147 connman_warn("Skipping disconnect of %s",
1148 connman_network_get_identifier(network));
1152 __connman_network_disconnect(network);
1158 static void mark_network_unavailable(gpointer key, gpointer value,
1161 struct connman_network *network = value;
1163 if (connman_network_get_connected(network) == TRUE)
1166 connman_network_set_available(network, FALSE);
1169 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1172 struct connman_network *network = value;
1174 if (connman_network_get_connected(network) == TRUE)
1177 if (connman_network_get_available(network) == TRUE)
1183 void __connman_device_cleanup_networks(struct connman_device *device)
1185 g_hash_table_foreach_remove(device->networks,
1186 remove_unavailable_network, NULL);
1189 static void scanning_changed(struct connman_device *device)
1191 connman_dbus_property_changed_basic(device->element.path,
1192 CONNMAN_DEVICE_INTERFACE, "Scanning",
1193 DBUS_TYPE_BOOLEAN, &device->scanning);
1197 * connman_device_set_scanning:
1198 * @device: device structure
1199 * @scanning: scanning state
1201 * Change scanning state of device
1203 int connman_device_set_scanning(struct connman_device *device,
1204 connman_bool_t scanning)
1206 DBG("device %p scanning %d", device, scanning);
1208 if (!device->driver || !device->driver->scan)
1211 if (device->scanning == scanning)
1214 device->scanning = scanning;
1216 scanning_changed(device);
1218 if (scanning == TRUE) {
1219 reset_scan_trigger(device);
1221 g_hash_table_foreach(device->networks,
1222 mark_network_unavailable, NULL);
1227 __connman_device_cleanup_networks(device);
1229 if (device->connections > 0)
1232 if (device->disconnected == TRUE)
1235 __connman_service_auto_connect();
1241 * connman_device_set_disconnected:
1242 * @device: device structure
1243 * @disconnected: disconnected state
1245 * Change disconnected state of device (only for device with networks)
1247 int connman_device_set_disconnected(struct connman_device *device,
1248 connman_bool_t disconnected)
1250 DBG("device %p disconnected %d", device, disconnected);
1252 switch (device->mode) {
1253 case CONNMAN_DEVICE_MODE_UNKNOWN:
1255 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1256 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1260 if (device->disconnected == disconnected)
1263 device->disconnected = disconnected;
1265 if (disconnected == TRUE)
1266 force_scan_trigger(device);
1272 * connman_device_get_disconnected:
1273 * @device: device structure
1275 * Get device disconnected state
1277 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1279 return device->disconnected;
1283 * connman_device_set_string:
1284 * @device: device structure
1285 * @key: unique identifier
1286 * @value: string value
1288 * Set string value for specific key
1290 int connman_device_set_string(struct connman_device *device,
1291 const char *key, const char *value)
1293 DBG("device %p key %s value %s", device, key, value);
1295 if (g_str_equal(key, "Address") == TRUE) {
1296 g_free(device->address);
1297 device->address = g_strdup(value);
1298 } else if (g_str_equal(key, "Name") == TRUE) {
1299 g_free(device->name);
1300 device->name = g_strdup(value);
1301 } else if (g_str_equal(key, "Node") == TRUE) {
1302 g_free(device->node);
1303 device->node = g_strdup(value);
1306 return connman_element_set_string(&device->element, key, value);
1310 * connman_device_get_string:
1311 * @device: device structure
1312 * @key: unique identifier
1314 * Get string value for specific key
1316 const char *connman_device_get_string(struct connman_device *device,
1319 DBG("device %p key %s", device, key);
1321 if (g_str_equal(key, "Address") == TRUE)
1322 return device->address;
1323 else if (g_str_equal(key, "Name") == TRUE)
1324 return device->name;
1325 else if (g_str_equal(key, "Node") == TRUE)
1326 return device->node;
1327 else if (g_str_equal(key, "Interface") == TRUE)
1328 return device->interface;
1330 return connman_element_get_string(&device->element, key);
1333 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1335 struct connman_device *device = element->device;
1336 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1337 connman_bool_t powered;
1339 DBG("element %p name %s", element, element->name);
1344 device->offlinemode = offlinemode;
1346 if (device->blocked == TRUE)
1349 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1351 if (device->powered == powered)
1354 if (device->powered_persistent == FALSE)
1357 set_powered(device, powered);
1360 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1362 DBG("offlinmode %d", offlinemode);
1364 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1365 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1367 __connman_notifier_offlinemode(offlinemode);
1372 void __connman_device_increase_connections(struct connman_device *device)
1377 device->connections++;
1380 void __connman_device_decrease_connections(struct connman_device *device)
1385 device->connections--;
1389 * connman_device_add_network:
1390 * @device: device structure
1391 * @network: network structure
1393 * Add new network to the device
1395 int connman_device_add_network(struct connman_device *device,
1396 struct connman_network *network)
1398 const char *identifier = connman_network_get_identifier(network);
1401 DBG("device %p network %p", device, network);
1403 if (identifier == NULL)
1406 switch (device->mode) {
1407 case CONNMAN_DEVICE_MODE_UNKNOWN:
1409 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1410 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1414 __connman_network_set_device(network, device);
1416 err = connman_element_register((struct connman_element *) network,
1419 __connman_network_set_device(network, NULL);
1423 g_hash_table_insert(device->networks, g_strdup(identifier),
1430 * connman_device_get_network:
1431 * @device: device structure
1432 * @identifier: network identifier
1434 * Get network for given identifier
1436 struct connman_network *connman_device_get_network(struct connman_device *device,
1437 const char *identifier)
1439 DBG("device %p identifier %s", device, identifier);
1441 return g_hash_table_lookup(device->networks, identifier);
1445 * connman_device_remove_network:
1446 * @device: device structure
1447 * @identifier: network identifier
1449 * Remove network for given identifier
1451 int connman_device_remove_network(struct connman_device *device,
1452 const char *identifier)
1454 DBG("device %p identifier %s", device, identifier);
1456 g_hash_table_remove(device->networks, identifier);
1461 void connman_device_remove_all_networks(struct connman_device *device)
1463 g_hash_table_remove_all(device->networks);
1466 void __connman_device_set_network(struct connman_device *device,
1467 struct connman_network *network)
1474 if (device->network == network)
1477 if (device->network != NULL)
1478 connman_network_unref(device->network);
1480 if (network != NULL) {
1481 name = connman_network_get_string(network,
1482 CONNMAN_PROPERTY_ID_NAME);
1483 g_free(device->last_network);
1484 device->last_network = g_strdup(name);
1486 device->network = connman_network_ref(network);
1488 g_free(device->last_network);
1489 device->last_network = NULL;
1491 device->network = NULL;
1495 void __connman_device_set_reconnect(struct connman_device *device,
1496 connman_bool_t reconnect)
1498 device->reconnect = reconnect;
1501 connman_bool_t __connman_device_get_reconnect(
1502 struct connman_device *device)
1504 return device->reconnect;
1508 * connman_device_register:
1509 * @device: device structure
1511 * Register device with the system
1513 int connman_device_register(struct connman_device *device)
1515 __connman_storage_load_device(device);
1517 device->offlinemode = __connman_profile_get_offlinemode();
1519 return connman_element_register(&device->element, NULL);
1523 * connman_device_unregister:
1524 * @device: device structure
1526 * Unregister device with the system
1528 void connman_device_unregister(struct connman_device *device)
1530 __connman_storage_save_device(device);
1532 connman_element_unregister(&device->element);
1536 * connman_device_get_data:
1537 * @device: device structure
1539 * Get private device data pointer
1541 void *connman_device_get_data(struct connman_device *device)
1543 return device->driver_data;
1547 * connman_device_set_data:
1548 * @device: device structure
1549 * @data: data pointer
1551 * Set private device data pointer
1553 void connman_device_set_data(struct connman_device *device, void *data)
1555 device->driver_data = data;
1558 static gboolean match_driver(struct connman_device *device,
1559 struct connman_device_driver *driver)
1561 if (device->type == driver->type ||
1562 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1568 static int device_probe(struct connman_element *element)
1570 struct connman_device *device = element->device;
1573 DBG("element %p name %s", element, element->name);
1578 if (device->driver != NULL)
1581 for (list = driver_list; list; list = list->next) {
1582 struct connman_device_driver *driver = list->data;
1584 if (match_driver(device, driver) == FALSE)
1587 DBG("driver %p name %s", driver, driver->name);
1589 if (driver->probe(device) == 0) {
1590 device->driver = driver;
1595 if (device->driver == NULL)
1598 return setup_device(device);
1601 static void device_remove(struct connman_element *element)
1603 struct connman_device *device = element->device;
1605 DBG("element %p name %s", element, element->name);
1610 if (device->driver == NULL)
1613 remove_device(device);
1616 static struct connman_driver device_driver = {
1618 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1619 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1620 .probe = device_probe,
1621 .remove = device_remove,
1624 static int device_load(struct connman_device *device)
1626 const char *ident = __connman_profile_active_ident();
1628 GError *error = NULL;
1630 connman_bool_t powered;
1633 DBG("device %p", device);
1635 keyfile = __connman_storage_open_profile(ident);
1636 if (keyfile == NULL)
1639 identifier = g_strdup_printf("device_%s", device->element.name);
1640 if (identifier == NULL)
1643 powered = g_key_file_get_boolean(keyfile, identifier,
1646 device->powered_persistent = powered;
1647 g_clear_error(&error);
1649 switch (device->mode) {
1650 case CONNMAN_DEVICE_MODE_UNKNOWN:
1652 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1653 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1654 val = g_key_file_get_integer(keyfile, identifier,
1655 "ScanInterval", &error);
1656 if (error == NULL && val > 0)
1657 device->scan_interval = val;
1658 g_clear_error(&error);
1665 __connman_storage_close_profile(ident, keyfile, FALSE);
1670 static int device_save(struct connman_device *device)
1672 const char *ident = __connman_profile_active_ident();
1676 DBG("device %p", device);
1678 keyfile = __connman_storage_open_profile(ident);
1679 if (keyfile == NULL)
1682 identifier = g_strdup_printf("device_%s", device->element.name);
1683 if (identifier == NULL)
1686 g_key_file_set_boolean(keyfile, identifier,
1687 "Powered", device->powered_persistent);
1689 switch (device->mode) {
1690 case CONNMAN_DEVICE_MODE_UNKNOWN:
1692 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1693 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1694 if (device->scan_interval > 0)
1695 g_key_file_set_integer(keyfile, identifier,
1696 "ScanInterval", device->scan_interval);
1703 __connman_storage_close_profile(ident, keyfile, TRUE);
1708 static struct connman_storage device_storage = {
1710 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1711 .device_load = device_load,
1712 .device_save = device_save,
1715 int __connman_device_init(void)
1719 connection = connman_dbus_get_connection();
1721 if (connman_storage_register(&device_storage) < 0)
1722 connman_error("Failed to register device storage");
1724 return connman_driver_register(&device_driver);
1727 void __connman_device_cleanup(void)
1731 connman_driver_unregister(&device_driver);
1733 connman_storage_unregister(&device_storage);
1735 dbus_connection_unref(connection);