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 %d", device, device->blocked);
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 (device->offlinemode == FALSE &&
563 device->powered_persistent == TRUE)
564 __connman_device_enable(device);
569 static void probe_driver(struct connman_element *element, gpointer user_data)
571 struct connman_device_driver *driver = user_data;
573 DBG("element %p name %s", element, element->name);
575 if (element->device == NULL)
578 if (element->device->driver != NULL)
581 if (driver->type != element->device->type)
584 if (driver->probe(element->device) < 0)
587 element->device->driver = driver;
589 __connman_element_set_driver(element);
591 setup_device(element->device);
594 static void remove_device(struct connman_device *device)
598 DBG("device %p", device);
600 err = __connman_device_disable(device);
601 if (err < 0 && err == -EINPROGRESS)
602 __connman_technology_disable_device(device);
604 switch (device->mode) {
605 case CONNMAN_DEVICE_MODE_UNKNOWN:
606 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
607 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
611 __connman_technology_remove_device(device);
613 unregister_interface(&device->element);
615 if (device->driver->remove)
616 device->driver->remove(device);
618 device->driver = NULL;
621 static void remove_driver(struct connman_element *element, gpointer user_data)
623 struct connman_device_driver *driver = user_data;
625 DBG("element %p name %s", element, element->name);
627 if (element->device == NULL)
630 if (element->device->driver == driver)
631 remove_device(element->device);
634 connman_bool_t __connman_device_has_driver(struct connman_device *device)
636 if (device == NULL || device->driver == NULL)
639 return device->registered;
642 static GSList *driver_list = NULL;
644 static gint compare_priority(gconstpointer a, gconstpointer b)
646 const struct connman_device_driver *driver1 = a;
647 const struct connman_device_driver *driver2 = b;
649 return driver2->priority - driver1->priority;
653 * connman_device_driver_register:
654 * @driver: device driver definition
656 * Register a new device driver
658 * Returns: %0 on success
660 int connman_device_driver_register(struct connman_device_driver *driver)
662 DBG("driver %p name %s", driver, driver->name);
664 driver_list = g_slist_insert_sorted(driver_list, driver,
667 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
668 probe_driver, driver);
674 * connman_device_driver_unregister:
675 * @driver: device driver definition
677 * Remove a previously registered device driver
679 void connman_device_driver_unregister(struct connman_device_driver *driver)
681 DBG("driver %p name %s", driver, driver->name);
683 driver_list = g_slist_remove(driver_list, driver);
685 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
686 remove_driver, driver);
689 static void unregister_network(gpointer data)
691 struct connman_network *network = data;
693 DBG("network %p", network);
695 connman_element_unregister((struct connman_element *) network);
697 __connman_network_set_device(network, NULL);
699 connman_network_unref(network);
702 static void device_destruct(struct connman_element *element)
704 struct connman_device *device = element->device;
706 DBG("element %p name %s", element, element->name);
708 if (device->timeout > 0) {
709 g_source_remove(device->timeout);
713 clear_scan_trigger(device);
715 if (device->pending != NULL) {
716 dbus_message_unref(device->pending);
717 device->pending = NULL;
720 g_free(device->ident);
721 g_free(device->node);
722 g_free(device->name);
723 g_free(device->address);
724 g_free(device->control);
725 g_free(device->interface);
727 g_free(device->last_network);
729 g_hash_table_destroy(device->networks);
730 device->networks = NULL;
734 * connman_device_create:
735 * @node: device node name (for example an address)
738 * Allocate a new device of given #type and assign the #node name to it.
740 * Returns: a newly-allocated #connman_device structure
742 struct connman_device *connman_device_create(const char *node,
743 enum connman_device_type type)
745 struct connman_device *device;
747 enum connman_service_type service_type;
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;
780 service_type = __connman_device_get_service_type(device);
781 device->blocked = __connman_technology_get_blocked(service_type);
784 case CONNMAN_DEVICE_TYPE_UNKNOWN:
785 case CONNMAN_DEVICE_TYPE_VENDOR:
786 device->scan_interval = 0;
788 case CONNMAN_DEVICE_TYPE_ETHERNET:
789 case CONNMAN_DEVICE_TYPE_WIFI:
790 device->scan_interval = 300;
792 case CONNMAN_DEVICE_TYPE_WIMAX:
793 device->scan_interval = 0;
795 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
796 device->scan_interval = 0;
798 case CONNMAN_DEVICE_TYPE_GPS:
799 device->scan_interval = 0;
801 case CONNMAN_DEVICE_TYPE_CELLULAR:
802 device->scan_interval = 0;
806 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
807 g_free, unregister_network);
813 * connman_device_ref:
814 * @device: device structure
816 * Increase reference counter of device
818 struct connman_device *connman_device_ref(struct connman_device *device)
820 if (connman_element_ref(&device->element) == NULL)
827 * connman_device_unref:
828 * @device: device structure
830 * Decrease reference counter of device
832 void connman_device_unref(struct connman_device *device)
834 connman_element_unref(&device->element);
837 const char *__connman_device_get_type(struct connman_device *device)
839 return type2string(device->type);
843 * connman_device_get_type:
844 * @device: device structure
848 enum connman_device_type connman_device_get_type(struct connman_device *device)
854 * connman_device_get_name:
855 * @device: device structure
857 * Get unique name of device
859 const char *connman_device_get_name(struct connman_device *device)
861 return device->element.name;
865 * connman_device_get_path:
866 * @device: device structure
868 * Get path name of device
870 const char *connman_device_get_path(struct connman_device *device)
872 return device->element.path;
876 * connman_device_set_index:
877 * @device: device structure
878 * @index: index number
880 * Set index number of device
882 void connman_device_set_index(struct connman_device *device, int index)
884 device->element.index = index;
888 * connman_device_get_index:
889 * @device: device structure
891 * Get index number of device
893 int connman_device_get_index(struct connman_device *device)
895 return device->element.index;
898 int __connman_device_get_phyindex(struct connman_device *device)
900 return device->phyindex;
903 void __connman_device_set_phyindex(struct connman_device *device,
906 device->phyindex = phyindex;
910 * connman_device_set_interface:
911 * @device: device structure
912 * @interface: interface name
913 * @control: control interface
915 * Set interface name of device
917 void connman_device_set_interface(struct connman_device *device,
918 const char *interface, const char *control)
920 g_free(device->element.devname);
921 device->element.devname = g_strdup(interface);
923 g_free(device->interface);
924 device->interface = g_strdup(interface);
926 g_free(device->control);
927 device->control = g_strdup(control);
929 if (device->name == NULL) {
930 const char *str = type2description(device->type);
931 if (str != NULL && device->interface != NULL)
932 device->name = g_strdup_printf("%s (%s)", str,
937 const char *connman_device_get_control(struct connman_device *device)
939 return device->control;
943 * connman_device_set_ident:
944 * @device: device structure
945 * @ident: unique identifier
947 * Set unique identifier of device
949 void connman_device_set_ident(struct connman_device *device,
952 g_free(device->ident);
953 device->ident = g_strdup(ident);
956 const char *connman_device_get_ident(struct connman_device *device)
958 return device->ident;
962 * connman_device_set_mode:
963 * @device: device structure
964 * @mode: network mode
966 * Change network mode of device
968 void connman_device_set_mode(struct connman_device *device,
969 enum connman_device_mode mode)
975 * connman_device_get_mode:
976 * @device: device structure
978 * Get network mode of device
980 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
986 * connman_device_set_powered:
987 * @device: device structure
988 * @powered: powered state
990 * Change power state of device
992 int connman_device_set_powered(struct connman_device *device,
993 connman_bool_t powered)
995 DBG("driver %p powered %d", device, powered);
997 if (device->timeout > 0) {
998 g_source_remove(device->timeout);
1002 if (device->pending != NULL) {
1003 g_dbus_send_reply(connection, device->pending,
1006 dbus_message_unref(device->pending);
1007 device->pending = NULL;
1010 if (device->powered == powered) {
1011 device->powered_pending = powered;
1015 if (powered == TRUE)
1016 __connman_device_enable(device);
1018 __connman_device_disable(device);
1020 device->powered = powered;
1021 device->powered_pending = powered;
1023 if (device->powered == TRUE)
1024 __connman_technology_enable_device(device);
1026 __connman_technology_disable_device(device);
1028 if (device->offlinemode == TRUE && powered == TRUE) {
1029 powered_changed(device);
1030 return connman_device_set_powered(device, FALSE);
1033 if (device->registered == FALSE)
1036 powered_changed(device);
1038 if (powered == FALSE)
1041 reset_scan_trigger(device);
1043 if (device->driver->scan)
1044 device->driver->scan(device);
1049 int __connman_device_set_blocked(struct connman_device *device,
1050 connman_bool_t blocked)
1052 connman_bool_t powered;
1054 DBG("device %p blocked %d", device, blocked);
1056 device->blocked = blocked;
1058 blocked_changed(device);
1060 if (device->offlinemode == TRUE)
1063 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1065 if (blocked == FALSE)
1066 powered = device->powered_persistent;
1070 return set_powered(device, powered);
1073 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1075 return device->blocked;
1078 int __connman_device_scan(struct connman_device *device)
1080 if (!device->driver || !device->driver->scan)
1083 if (device->powered == FALSE)
1086 reset_scan_trigger(device);
1088 return device->driver->scan(device);
1091 int __connman_device_enable_persistent(struct connman_device *device)
1095 DBG("device %p", device);
1097 device->powered_persistent = TRUE;
1099 __connman_storage_save_device(device);
1101 err = __connman_device_enable(device);
1102 if (err == 0 || err == -EINPROGRESS) {
1103 device->offlinemode = FALSE;
1104 if (__connman_profile_get_offlinemode() == TRUE) {
1105 __connman_profile_set_offlinemode(FALSE, FALSE);
1107 __connman_profile_save_default();
1114 int __connman_device_disable_persistent(struct connman_device *device)
1116 DBG("device %p", device);
1118 device->powered_persistent = FALSE;
1120 __connman_storage_save_device(device);
1122 return __connman_device_disable(device);
1125 int __connman_device_disconnect(struct connman_device *device)
1127 GHashTableIter iter;
1128 gpointer key, value;
1130 DBG("device %p", device);
1132 connman_device_set_disconnected(device, TRUE);
1134 g_hash_table_iter_init(&iter, device->networks);
1136 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1137 struct connman_network *network = value;
1139 if (__connman_network_get_connecting(network) == TRUE) {
1141 * Skip network in the process of connecting.
1142 * This is a workaround for WiFi networks serviced
1143 * by the supplicant plugin that hold a reference
1144 * to the network. If we disconnect the network
1145 * here then the referenced object will not be
1146 * registered and usage (like launching DHCP client)
1147 * will fail. There is nothing to be gained by
1148 * removing the network here anyway.
1150 connman_warn("Skipping disconnect of %s",
1151 connman_network_get_identifier(network));
1155 __connman_network_disconnect(network);
1161 static void mark_network_unavailable(gpointer key, gpointer value,
1164 struct connman_network *network = value;
1166 if (connman_network_get_connected(network) == TRUE)
1169 connman_network_set_available(network, FALSE);
1172 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1175 struct connman_network *network = value;
1177 if (connman_network_get_connected(network) == TRUE)
1180 if (connman_network_get_available(network) == TRUE)
1186 void __connman_device_cleanup_networks(struct connman_device *device)
1188 g_hash_table_foreach_remove(device->networks,
1189 remove_unavailable_network, NULL);
1192 static void scanning_changed(struct connman_device *device)
1194 connman_dbus_property_changed_basic(device->element.path,
1195 CONNMAN_DEVICE_INTERFACE, "Scanning",
1196 DBUS_TYPE_BOOLEAN, &device->scanning);
1200 * connman_device_set_scanning:
1201 * @device: device structure
1202 * @scanning: scanning state
1204 * Change scanning state of device
1206 int connman_device_set_scanning(struct connman_device *device,
1207 connman_bool_t scanning)
1209 DBG("device %p scanning %d", device, scanning);
1211 if (!device->driver || !device->driver->scan)
1214 if (device->scanning == scanning)
1217 device->scanning = scanning;
1219 scanning_changed(device);
1221 if (scanning == TRUE) {
1222 reset_scan_trigger(device);
1224 g_hash_table_foreach(device->networks,
1225 mark_network_unavailable, NULL);
1230 __connman_device_cleanup_networks(device);
1232 if (device->connections > 0)
1235 if (device->disconnected == TRUE)
1238 __connman_service_auto_connect();
1244 * connman_device_set_disconnected:
1245 * @device: device structure
1246 * @disconnected: disconnected state
1248 * Change disconnected state of device (only for device with networks)
1250 int connman_device_set_disconnected(struct connman_device *device,
1251 connman_bool_t disconnected)
1253 DBG("device %p disconnected %d", device, disconnected);
1255 switch (device->mode) {
1256 case CONNMAN_DEVICE_MODE_UNKNOWN:
1258 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1259 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1263 if (device->disconnected == disconnected)
1266 device->disconnected = disconnected;
1268 if (disconnected == TRUE)
1269 force_scan_trigger(device);
1275 * connman_device_get_disconnected:
1276 * @device: device structure
1278 * Get device disconnected state
1280 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1282 return device->disconnected;
1286 * connman_device_set_string:
1287 * @device: device structure
1288 * @key: unique identifier
1289 * @value: string value
1291 * Set string value for specific key
1293 int connman_device_set_string(struct connman_device *device,
1294 const char *key, const char *value)
1296 DBG("device %p key %s value %s", device, key, value);
1298 if (g_str_equal(key, "Address") == TRUE) {
1299 g_free(device->address);
1300 device->address = g_strdup(value);
1301 } else if (g_str_equal(key, "Name") == TRUE) {
1302 g_free(device->name);
1303 device->name = g_strdup(value);
1304 } else if (g_str_equal(key, "Node") == TRUE) {
1305 g_free(device->node);
1306 device->node = g_strdup(value);
1309 return connman_element_set_string(&device->element, key, value);
1313 * connman_device_get_string:
1314 * @device: device structure
1315 * @key: unique identifier
1317 * Get string value for specific key
1319 const char *connman_device_get_string(struct connman_device *device,
1322 DBG("device %p key %s", device, key);
1324 if (g_str_equal(key, "Address") == TRUE)
1325 return device->address;
1326 else if (g_str_equal(key, "Name") == TRUE)
1327 return device->name;
1328 else if (g_str_equal(key, "Node") == TRUE)
1329 return device->node;
1330 else if (g_str_equal(key, "Interface") == TRUE)
1331 return device->interface;
1333 return connman_element_get_string(&device->element, key);
1336 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1338 struct connman_device *device = element->device;
1339 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1340 connman_bool_t powered;
1342 DBG("element %p name %s", element, element->name);
1347 device->offlinemode = offlinemode;
1349 if (device->blocked == TRUE)
1352 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1354 if (device->powered == powered)
1357 if (device->powered_persistent == FALSE)
1360 set_powered(device, powered);
1363 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1365 DBG("offlinmode %d", offlinemode);
1367 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1368 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1370 __connman_notifier_offlinemode(offlinemode);
1375 void __connman_device_increase_connections(struct connman_device *device)
1380 device->connections++;
1383 void __connman_device_decrease_connections(struct connman_device *device)
1388 device->connections--;
1392 * connman_device_add_network:
1393 * @device: device structure
1394 * @network: network structure
1396 * Add new network to the device
1398 int connman_device_add_network(struct connman_device *device,
1399 struct connman_network *network)
1401 const char *identifier = connman_network_get_identifier(network);
1404 DBG("device %p network %p", device, network);
1406 if (identifier == NULL)
1409 switch (device->mode) {
1410 case CONNMAN_DEVICE_MODE_UNKNOWN:
1412 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1413 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1417 __connman_network_set_device(network, device);
1419 err = connman_element_register((struct connman_element *) network,
1422 __connman_network_set_device(network, NULL);
1426 g_hash_table_insert(device->networks, g_strdup(identifier),
1433 * connman_device_get_network:
1434 * @device: device structure
1435 * @identifier: network identifier
1437 * Get network for given identifier
1439 struct connman_network *connman_device_get_network(struct connman_device *device,
1440 const char *identifier)
1442 DBG("device %p identifier %s", device, identifier);
1444 return g_hash_table_lookup(device->networks, identifier);
1448 * connman_device_remove_network:
1449 * @device: device structure
1450 * @identifier: network identifier
1452 * Remove network for given identifier
1454 int connman_device_remove_network(struct connman_device *device,
1455 const char *identifier)
1457 DBG("device %p identifier %s", device, identifier);
1459 g_hash_table_remove(device->networks, identifier);
1464 void connman_device_remove_all_networks(struct connman_device *device)
1466 g_hash_table_remove_all(device->networks);
1469 void __connman_device_set_network(struct connman_device *device,
1470 struct connman_network *network)
1477 if (device->network == network)
1480 if (device->network != NULL)
1481 connman_network_unref(device->network);
1483 if (network != NULL) {
1484 name = connman_network_get_string(network,
1485 CONNMAN_PROPERTY_ID_NAME);
1486 g_free(device->last_network);
1487 device->last_network = g_strdup(name);
1489 device->network = connman_network_ref(network);
1491 g_free(device->last_network);
1492 device->last_network = NULL;
1494 device->network = NULL;
1498 void __connman_device_set_reconnect(struct connman_device *device,
1499 connman_bool_t reconnect)
1501 device->reconnect = reconnect;
1504 connman_bool_t __connman_device_get_reconnect(
1505 struct connman_device *device)
1507 return device->reconnect;
1511 * connman_device_register:
1512 * @device: device structure
1514 * Register device with the system
1516 int connman_device_register(struct connman_device *device)
1518 __connman_storage_load_device(device);
1520 device->offlinemode = __connman_profile_get_offlinemode();
1522 return connman_element_register(&device->element, NULL);
1526 * connman_device_unregister:
1527 * @device: device structure
1529 * Unregister device with the system
1531 void connman_device_unregister(struct connman_device *device)
1533 __connman_storage_save_device(device);
1535 connman_element_unregister(&device->element);
1539 * connman_device_get_data:
1540 * @device: device structure
1542 * Get private device data pointer
1544 void *connman_device_get_data(struct connman_device *device)
1546 return device->driver_data;
1550 * connman_device_set_data:
1551 * @device: device structure
1552 * @data: data pointer
1554 * Set private device data pointer
1556 void connman_device_set_data(struct connman_device *device, void *data)
1558 device->driver_data = data;
1561 static gboolean match_driver(struct connman_device *device,
1562 struct connman_device_driver *driver)
1564 if (device->type == driver->type ||
1565 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1571 static int device_probe(struct connman_element *element)
1573 struct connman_device *device = element->device;
1576 DBG("element %p name %s", element, element->name);
1581 if (device->driver != NULL)
1584 for (list = driver_list; list; list = list->next) {
1585 struct connman_device_driver *driver = list->data;
1587 if (match_driver(device, driver) == FALSE)
1590 DBG("driver %p name %s", driver, driver->name);
1592 if (driver->probe(device) == 0) {
1593 device->driver = driver;
1598 if (device->driver == NULL)
1601 return setup_device(device);
1604 static void device_remove(struct connman_element *element)
1606 struct connman_device *device = element->device;
1608 DBG("element %p name %s", element, element->name);
1613 if (device->driver == NULL)
1616 remove_device(device);
1619 static struct connman_driver device_driver = {
1621 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1622 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1623 .probe = device_probe,
1624 .remove = device_remove,
1627 static int device_load(struct connman_device *device)
1629 const char *ident = __connman_profile_active_ident();
1631 GError *error = NULL;
1633 connman_bool_t powered;
1636 DBG("device %p", device);
1638 keyfile = __connman_storage_open_profile(ident);
1639 if (keyfile == NULL)
1642 identifier = g_strdup_printf("device_%s", device->element.name);
1643 if (identifier == NULL)
1646 powered = g_key_file_get_boolean(keyfile, identifier,
1649 device->powered_persistent = powered;
1650 g_clear_error(&error);
1652 switch (device->mode) {
1653 case CONNMAN_DEVICE_MODE_UNKNOWN:
1655 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1656 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1657 val = g_key_file_get_integer(keyfile, identifier,
1658 "ScanInterval", &error);
1660 device->scan_interval = val;
1661 g_clear_error(&error);
1668 __connman_storage_close_profile(ident, keyfile, FALSE);
1673 static int device_save(struct connman_device *device)
1675 const char *ident = __connman_profile_active_ident();
1679 DBG("device %p", device);
1681 keyfile = __connman_storage_open_profile(ident);
1682 if (keyfile == NULL)
1685 identifier = g_strdup_printf("device_%s", device->element.name);
1686 if (identifier == NULL)
1689 g_key_file_set_boolean(keyfile, identifier,
1690 "Powered", device->powered_persistent);
1692 switch (device->mode) {
1693 case CONNMAN_DEVICE_MODE_UNKNOWN:
1695 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1696 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1697 g_key_file_set_integer(keyfile, identifier,
1698 "ScanInterval", device->scan_interval);
1705 __connman_storage_close_profile(ident, keyfile, TRUE);
1710 static struct connman_storage device_storage = {
1712 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1713 .device_load = device_load,
1714 .device_save = device_save,
1717 int __connman_device_init(void)
1721 connection = connman_dbus_get_connection();
1723 if (connman_storage_register(&device_storage) < 0)
1724 connman_error("Failed to register device storage");
1726 return connman_driver_register(&device_driver);
1729 void __connman_device_cleanup(void)
1733 connman_driver_unregister(&device_driver);
1735 connman_storage_unregister(&device_storage);
1737 dbus_connection_unref(connection);