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
31 struct connman_device {
32 struct connman_element element;
33 enum connman_device_type type;
34 connman_bool_t offlinemode;
35 connman_bool_t blocked;
36 connman_bool_t powered;
37 connman_bool_t powered_pending;
38 connman_bool_t powered_persistent;
39 connman_bool_t scanning;
40 connman_bool_t disconnected;
41 connman_bool_t reconnect;
42 connman_uint16_t scan_interval;
49 unsigned int connections;
52 struct connman_device_driver *driver;
56 struct connman_network *network;
60 static gboolean device_scan_trigger(gpointer user_data)
62 struct connman_device *device = user_data;
64 DBG("device %p", device);
66 if (device->driver == NULL) {
67 device->scan_timeout = 0;
71 if (device->driver->scan)
72 device->driver->scan(device);
77 static void clear_scan_trigger(struct connman_device *device)
79 if (device->scan_timeout > 0) {
80 g_source_remove(device->scan_timeout);
81 device->scan_timeout = 0;
85 static void reset_scan_trigger(struct connman_device *device)
87 clear_scan_trigger(device);
89 if (device->scan_interval > 0) {
90 guint interval = device->scan_interval;
91 device->scan_timeout = g_timeout_add_seconds(interval,
92 device_scan_trigger, device);
96 static void force_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 device->scan_timeout = g_timeout_add_seconds(5,
101 device_scan_trigger, device);
104 void connman_device_schedule_scan(struct connman_device *device)
106 reset_scan_trigger(device);
109 static const char *type2description(enum connman_device_type type)
112 case CONNMAN_DEVICE_TYPE_UNKNOWN:
113 case CONNMAN_DEVICE_TYPE_VENDOR:
115 case CONNMAN_DEVICE_TYPE_ETHERNET:
117 case CONNMAN_DEVICE_TYPE_WIFI:
119 case CONNMAN_DEVICE_TYPE_WIMAX:
121 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
123 case CONNMAN_DEVICE_TYPE_GPS:
125 case CONNMAN_DEVICE_TYPE_CELLULAR:
127 case CONNMAN_DEVICE_TYPE_GADGET:
135 static const char *type2string(enum connman_device_type type)
138 case CONNMAN_DEVICE_TYPE_UNKNOWN:
139 case CONNMAN_DEVICE_TYPE_VENDOR:
141 case CONNMAN_DEVICE_TYPE_ETHERNET:
143 case CONNMAN_DEVICE_TYPE_WIFI:
145 case CONNMAN_DEVICE_TYPE_WIMAX:
147 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
149 case CONNMAN_DEVICE_TYPE_GPS:
151 case CONNMAN_DEVICE_TYPE_CELLULAR:
153 case CONNMAN_DEVICE_TYPE_GADGET:
161 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
163 enum connman_device_type type = connman_device_get_type(device);
166 case CONNMAN_DEVICE_TYPE_UNKNOWN:
167 case CONNMAN_DEVICE_TYPE_VENDOR:
168 case CONNMAN_DEVICE_TYPE_GPS:
170 case CONNMAN_DEVICE_TYPE_ETHERNET:
171 return CONNMAN_SERVICE_TYPE_ETHERNET;
172 case CONNMAN_DEVICE_TYPE_WIFI:
173 return CONNMAN_SERVICE_TYPE_WIFI;
174 case CONNMAN_DEVICE_TYPE_WIMAX:
175 return CONNMAN_SERVICE_TYPE_WIMAX;
176 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
177 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
178 case CONNMAN_DEVICE_TYPE_CELLULAR:
179 return CONNMAN_SERVICE_TYPE_CELLULAR;
180 case CONNMAN_DEVICE_TYPE_GADGET:
181 return CONNMAN_SERVICE_TYPE_GADGET;
185 return CONNMAN_SERVICE_TYPE_UNKNOWN;
188 int __connman_device_enable(struct connman_device *device)
192 DBG("device %p %d", device, device->blocked);
194 if (!device->driver || !device->driver->enable)
197 if (device->powered_pending == TRUE)
200 if (device->blocked == TRUE)
203 err = device->driver->enable(device);
204 if (err < 0 && err != -EALREADY) {
205 if (err == -EINPROGRESS) {
206 device->powered_pending = TRUE;
207 device->offlinemode = FALSE;
208 if (__connman_profile_get_offlinemode() == TRUE)
209 __connman_profile_set_offlinemode(FALSE, FALSE);
214 device->powered_pending = TRUE;
215 device->powered = TRUE;
216 device->offlinemode = FALSE;
217 if (__connman_profile_get_offlinemode() == TRUE)
218 __connman_profile_set_offlinemode(FALSE, FALSE);
220 __connman_technology_enable_device(device);
225 int __connman_device_disable(struct connman_device *device)
229 DBG("device %p", device);
231 if (!device->driver || !device->driver->disable)
234 if (device->powered == FALSE)
237 if (device->powered_pending == FALSE)
240 device->reconnect = FALSE;
242 clear_scan_trigger(device);
244 g_hash_table_remove_all(device->networks);
246 err = device->driver->disable(device);
247 if (err < 0 && err != -EALREADY) {
248 if (err == -EINPROGRESS)
249 device->powered_pending = FALSE;
253 device->connections = 0;
255 device->powered_pending = FALSE;
256 device->powered = FALSE;
258 __connman_technology_disable_device(device);
263 static int set_powered(struct connman_device *device, connman_bool_t powered)
265 DBG("device %p powered %d", device, powered);
268 return __connman_device_enable(device);
270 return __connman_device_disable(device);
273 static int setup_device(struct connman_device *device)
275 DBG("device %p", device);
277 __connman_technology_add_device(device);
279 if (device->offlinemode == FALSE &&
280 device->powered_persistent == TRUE)
281 __connman_device_enable(device);
286 static void probe_driver(struct connman_element *element, gpointer user_data)
288 struct connman_device_driver *driver = user_data;
290 DBG("element %p name %s", element, element->name);
292 if (element->device == NULL)
295 if (element->device->driver != NULL)
298 if (driver->type != element->device->type)
301 if (driver->probe(element->device) < 0)
304 element->device->driver = driver;
306 __connman_element_set_driver(element);
308 setup_device(element->device);
311 static void remove_device(struct connman_device *device)
313 DBG("device %p", device);
315 __connman_device_disable(device);
317 __connman_technology_remove_device(device);
319 if (device->driver->remove)
320 device->driver->remove(device);
322 device->driver = NULL;
325 static void remove_driver(struct connman_element *element, gpointer user_data)
327 struct connman_device_driver *driver = user_data;
329 DBG("element %p name %s", element, element->name);
331 if (element->device == NULL)
334 if (element->device->driver == driver)
335 remove_device(element->device);
338 connman_bool_t __connman_device_has_driver(struct connman_device *device)
340 if (device == NULL || device->driver == NULL)
346 static GSList *driver_list = NULL;
348 static gint compare_priority(gconstpointer a, gconstpointer b)
350 const struct connman_device_driver *driver1 = a;
351 const struct connman_device_driver *driver2 = b;
353 return driver2->priority - driver1->priority;
357 * connman_device_driver_register:
358 * @driver: device driver definition
360 * Register a new device driver
362 * Returns: %0 on success
364 int connman_device_driver_register(struct connman_device_driver *driver)
366 DBG("driver %p name %s", driver, driver->name);
368 driver_list = g_slist_insert_sorted(driver_list, driver,
371 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
372 probe_driver, driver);
378 * connman_device_driver_unregister:
379 * @driver: device driver definition
381 * Remove a previously registered device driver
383 void connman_device_driver_unregister(struct connman_device_driver *driver)
385 DBG("driver %p name %s", driver, driver->name);
387 driver_list = g_slist_remove(driver_list, driver);
389 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
390 remove_driver, driver);
393 static void unregister_network(gpointer data)
395 struct connman_network *network = data;
397 DBG("network %p", network);
399 connman_element_unregister((struct connman_element *) network);
401 __connman_network_set_device(network, NULL);
403 connman_network_unref(network);
406 static void device_destruct(struct connman_element *element)
408 struct connman_device *device = element->device;
410 DBG("element %p name %s", element, element->name);
412 clear_scan_trigger(device);
414 g_free(device->ident);
415 g_free(device->node);
416 g_free(device->name);
417 g_free(device->address);
418 g_free(device->interface);
420 g_free(device->last_network);
422 g_hash_table_destroy(device->networks);
423 device->networks = NULL;
427 * connman_device_create:
428 * @node: device node name (for example an address)
431 * Allocate a new device of given #type and assign the #node name to it.
433 * Returns: a newly-allocated #connman_device structure
435 struct connman_device *connman_device_create(const char *node,
436 enum connman_device_type type)
438 struct connman_device *device;
440 enum connman_service_type service_type;
442 DBG("node %s type %d", node, type);
444 device = g_try_new0(struct connman_device, 1);
448 DBG("device %p", device);
450 __connman_element_initialize(&device->element);
452 device->element.name = g_strdup(node);
453 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
455 device->element.device = device;
456 device->element.destruct = device_destruct;
458 str = type2string(type);
460 connman_element_set_string(&device->element,
461 CONNMAN_PROPERTY_ID_TYPE, str);
463 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
466 device->name = g_strdup(type2description(device->type));
468 device->powered_persistent = TRUE;
470 device->phyindex = -1;
472 service_type = __connman_device_get_service_type(device);
473 device->blocked = __connman_technology_get_blocked(service_type);
476 case CONNMAN_DEVICE_TYPE_UNKNOWN:
477 case CONNMAN_DEVICE_TYPE_VENDOR:
478 device->scan_interval = 0;
480 case CONNMAN_DEVICE_TYPE_ETHERNET:
481 case CONNMAN_DEVICE_TYPE_WIFI:
482 device->scan_interval = 300;
484 case CONNMAN_DEVICE_TYPE_WIMAX:
485 device->scan_interval = 0;
487 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
488 device->scan_interval = 0;
490 case CONNMAN_DEVICE_TYPE_GPS:
491 device->scan_interval = 0;
493 case CONNMAN_DEVICE_TYPE_CELLULAR:
494 device->scan_interval = 0;
496 case CONNMAN_DEVICE_TYPE_GADGET:
497 device->scan_interval = 0;
501 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
502 g_free, unregister_network);
508 * connman_device_ref:
509 * @device: device structure
511 * Increase reference counter of device
513 struct connman_device *connman_device_ref(struct connman_device *device)
515 if (connman_element_ref(&device->element) == NULL)
522 * connman_device_unref:
523 * @device: device structure
525 * Decrease reference counter of device
527 void connman_device_unref(struct connman_device *device)
529 connman_element_unref(&device->element);
532 const char *__connman_device_get_type(struct connman_device *device)
534 return type2string(device->type);
538 * connman_device_get_type:
539 * @device: device structure
543 enum connman_device_type connman_device_get_type(struct connman_device *device)
549 * connman_device_set_index:
550 * @device: device structure
551 * @index: index number
553 * Set index number of device
555 void connman_device_set_index(struct connman_device *device, int index)
557 device->element.index = index;
561 * connman_device_get_index:
562 * @device: device structure
564 * Get index number of device
566 int connman_device_get_index(struct connman_device *device)
568 return device->element.index;
571 int __connman_device_get_phyindex(struct connman_device *device)
573 return device->phyindex;
576 void __connman_device_set_phyindex(struct connman_device *device,
579 device->phyindex = phyindex;
583 * connman_device_set_interface:
584 * @device: device structure
585 * @interface: interface name
587 * Set interface name of device
589 void connman_device_set_interface(struct connman_device *device,
590 const char *interface)
592 g_free(device->element.devname);
593 device->element.devname = g_strdup(interface);
595 g_free(device->interface);
596 device->interface = g_strdup(interface);
598 if (device->name == NULL) {
599 const char *str = type2description(device->type);
600 if (str != NULL && device->interface != NULL)
601 device->name = g_strdup_printf("%s (%s)", str,
607 * connman_device_set_ident:
608 * @device: device structure
609 * @ident: unique identifier
611 * Set unique identifier of device
613 void connman_device_set_ident(struct connman_device *device,
616 g_free(device->ident);
617 device->ident = g_strdup(ident);
620 const char *connman_device_get_ident(struct connman_device *device)
622 return device->ident;
626 * connman_device_set_powered:
627 * @device: device structure
628 * @powered: powered state
630 * Change power state of device
632 int connman_device_set_powered(struct connman_device *device,
633 connman_bool_t powered)
637 DBG("driver %p powered %d", device, powered);
639 if (device->powered == powered) {
640 device->powered_pending = powered;
645 err = __connman_device_enable(device);
647 err = __connman_device_disable(device);
649 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
652 device->powered = powered;
653 device->powered_pending = powered;
655 if (device->powered == TRUE)
656 __connman_technology_enable_device(device);
658 __connman_technology_disable_device(device);
660 if (device->offlinemode == TRUE && powered == TRUE)
661 return connman_device_set_powered(device, FALSE);
663 if (powered == FALSE)
666 reset_scan_trigger(device);
668 if (device->driver && device->driver->scan)
669 device->driver->scan(device);
674 int __connman_device_set_blocked(struct connman_device *device,
675 connman_bool_t blocked)
677 connman_bool_t powered;
679 DBG("device %p blocked %d", device, blocked);
681 device->blocked = blocked;
683 if (device->offlinemode == TRUE)
686 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
688 if (blocked == FALSE)
689 powered = device->powered_persistent;
693 return set_powered(device, powered);
696 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
698 return device->blocked;
701 int __connman_device_scan(struct connman_device *device)
703 if (!device->driver || !device->driver->scan)
706 if (device->powered == FALSE)
709 reset_scan_trigger(device);
711 return device->driver->scan(device);
714 int __connman_device_enable_persistent(struct connman_device *device)
718 DBG("device %p", device);
720 device->powered_persistent = TRUE;
722 __connman_storage_save_device(device);
724 err = __connman_device_enable(device);
725 if (err == 0 || err == -EINPROGRESS) {
726 device->offlinemode = FALSE;
727 if (__connman_profile_get_offlinemode() == TRUE) {
728 __connman_profile_set_offlinemode(FALSE, FALSE);
730 __connman_profile_save_default();
737 int __connman_device_disable_persistent(struct connman_device *device)
739 DBG("device %p", device);
741 device->powered_persistent = FALSE;
743 __connman_storage_save_device(device);
745 return __connman_device_disable(device);
748 int __connman_device_disconnect(struct connman_device *device)
753 DBG("device %p", device);
755 connman_device_set_disconnected(device, TRUE);
757 g_hash_table_iter_init(&iter, device->networks);
759 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
760 struct connman_network *network = value;
762 if (__connman_network_get_connecting(network) == TRUE) {
764 * Skip network in the process of connecting.
765 * This is a workaround for WiFi networks serviced
766 * by the supplicant plugin that hold a reference
767 * to the network. If we disconnect the network
768 * here then the referenced object will not be
769 * registered and usage (like launching DHCP client)
770 * will fail. There is nothing to be gained by
771 * removing the network here anyway.
773 connman_warn("Skipping disconnect of %s",
774 connman_network_get_identifier(network));
778 __connman_network_disconnect(network);
784 static void mark_network_unavailable(gpointer key, gpointer value,
787 struct connman_network *network = value;
789 if (connman_network_get_connected(network) == TRUE)
792 connman_network_set_available(network, FALSE);
795 static gboolean remove_unavailable_network(gpointer key, gpointer value,
798 struct connman_network *network = value;
800 if (connman_network_get_connected(network) == TRUE)
803 if (connman_network_get_available(network) == TRUE)
809 void __connman_device_cleanup_networks(struct connman_device *device)
811 g_hash_table_foreach_remove(device->networks,
812 remove_unavailable_network, NULL);
816 * connman_device_set_scanning:
817 * @device: device structure
818 * @scanning: scanning state
820 * Change scanning state of device
822 int connman_device_set_scanning(struct connman_device *device,
823 connman_bool_t scanning)
825 DBG("device %p scanning %d", device, scanning);
827 if (!device->driver || !device->driver->scan)
830 if (device->scanning == scanning)
833 device->scanning = scanning;
835 if (scanning == TRUE) {
836 reset_scan_trigger(device);
838 g_hash_table_foreach(device->networks,
839 mark_network_unavailable, NULL);
844 __connman_device_cleanup_networks(device);
846 if (device->connections > 0)
849 if (device->disconnected == TRUE)
852 __connman_service_auto_connect();
858 * connman_device_set_disconnected:
859 * @device: device structure
860 * @disconnected: disconnected state
862 * Change disconnected state of device (only for device with networks)
864 int connman_device_set_disconnected(struct connman_device *device,
865 connman_bool_t disconnected)
867 DBG("device %p disconnected %d", device, disconnected);
869 if (device->disconnected == disconnected)
872 device->disconnected = disconnected;
874 if (disconnected == TRUE)
875 force_scan_trigger(device);
881 * connman_device_get_disconnected:
882 * @device: device structure
884 * Get device disconnected state
886 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
888 return device->disconnected;
892 * connman_device_set_string:
893 * @device: device structure
894 * @key: unique identifier
895 * @value: string value
897 * Set string value for specific key
899 int connman_device_set_string(struct connman_device *device,
900 const char *key, const char *value)
902 DBG("device %p key %s value %s", device, key, value);
904 if (g_str_equal(key, "Address") == TRUE) {
905 g_free(device->address);
906 device->address = g_strdup(value);
907 } else if (g_str_equal(key, "Name") == TRUE) {
908 g_free(device->name);
909 device->name = g_strdup(value);
910 } else if (g_str_equal(key, "Node") == TRUE) {
911 g_free(device->node);
912 device->node = g_strdup(value);
915 return connman_element_set_string(&device->element, key, value);
919 * connman_device_get_string:
920 * @device: device structure
921 * @key: unique identifier
923 * Get string value for specific key
925 const char *connman_device_get_string(struct connman_device *device,
928 DBG("device %p key %s", device, key);
930 if (g_str_equal(key, "Address") == TRUE)
931 return device->address;
932 else if (g_str_equal(key, "Name") == TRUE)
934 else if (g_str_equal(key, "Node") == TRUE)
936 else if (g_str_equal(key, "Interface") == TRUE)
937 return device->interface;
939 return connman_element_get_string(&device->element, key);
942 static void set_offlinemode(struct connman_element *element, gpointer user_data)
944 struct connman_device *device = element->device;
945 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
946 connman_bool_t powered;
948 DBG("element %p name %s", element, element->name);
953 device->offlinemode = offlinemode;
955 if (device->blocked == TRUE)
958 powered = (offlinemode == TRUE) ? FALSE : TRUE;
960 if (device->powered == powered)
963 if (device->powered_persistent == FALSE)
966 set_powered(device, powered);
969 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
971 DBG("offlinmode %d", offlinemode);
973 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
974 set_offlinemode, GUINT_TO_POINTER(offlinemode));
976 __connman_notifier_offlinemode(offlinemode);
981 void __connman_device_increase_connections(struct connman_device *device)
986 device->connections++;
989 void __connman_device_decrease_connections(struct connman_device *device)
994 device->connections--;
998 * connman_device_add_network:
999 * @device: device structure
1000 * @network: network structure
1002 * Add new network to the device
1004 int connman_device_add_network(struct connman_device *device,
1005 struct connman_network *network)
1007 const char *identifier = connman_network_get_identifier(network);
1010 DBG("device %p network %p", device, network);
1012 if (identifier == NULL)
1015 __connman_network_set_device(network, device);
1017 err = connman_element_register((struct connman_element *) network,
1020 __connman_network_set_device(network, NULL);
1024 g_hash_table_insert(device->networks, g_strdup(identifier),
1031 * connman_device_get_network:
1032 * @device: device structure
1033 * @identifier: network identifier
1035 * Get network for given identifier
1037 struct connman_network *connman_device_get_network(struct connman_device *device,
1038 const char *identifier)
1040 DBG("device %p identifier %s", device, identifier);
1042 return g_hash_table_lookup(device->networks, identifier);
1046 * connman_device_remove_network:
1047 * @device: device structure
1048 * @identifier: network identifier
1050 * Remove network for given identifier
1052 int connman_device_remove_network(struct connman_device *device,
1053 const char *identifier)
1055 DBG("device %p identifier %s", device, identifier);
1057 g_hash_table_remove(device->networks, identifier);
1062 void connman_device_remove_all_networks(struct connman_device *device)
1064 g_hash_table_remove_all(device->networks);
1067 void __connman_device_set_network(struct connman_device *device,
1068 struct connman_network *network)
1075 if (device->network == network)
1078 if (device->network != NULL)
1079 connman_network_unref(device->network);
1081 if (network != NULL) {
1082 name = connman_network_get_string(network,
1083 CONNMAN_PROPERTY_ID_NAME);
1084 g_free(device->last_network);
1085 device->last_network = g_strdup(name);
1087 device->network = connman_network_ref(network);
1089 g_free(device->last_network);
1090 device->last_network = NULL;
1092 device->network = NULL;
1096 void __connman_device_set_reconnect(struct connman_device *device,
1097 connman_bool_t reconnect)
1099 device->reconnect = reconnect;
1102 connman_bool_t __connman_device_get_reconnect(
1103 struct connman_device *device)
1105 return device->reconnect;
1109 * connman_device_register:
1110 * @device: device structure
1112 * Register device with the system
1114 int connman_device_register(struct connman_device *device)
1116 __connman_storage_load_device(device);
1118 device->offlinemode = __connman_profile_get_offlinemode();
1120 return connman_element_register(&device->element, NULL);
1124 * connman_device_unregister:
1125 * @device: device structure
1127 * Unregister device with the system
1129 void connman_device_unregister(struct connman_device *device)
1131 __connman_storage_save_device(device);
1133 connman_element_unregister(&device->element);
1137 * connman_device_get_data:
1138 * @device: device structure
1140 * Get private device data pointer
1142 void *connman_device_get_data(struct connman_device *device)
1144 return device->driver_data;
1148 * connman_device_set_data:
1149 * @device: device structure
1150 * @data: data pointer
1152 * Set private device data pointer
1154 void connman_device_set_data(struct connman_device *device, void *data)
1156 device->driver_data = data;
1159 static gboolean match_driver(struct connman_device *device,
1160 struct connman_device_driver *driver)
1162 if (device->type == driver->type ||
1163 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1169 static int device_probe(struct connman_element *element)
1171 struct connman_device *device = element->device;
1174 DBG("element %p name %s", element, element->name);
1179 if (device->driver != NULL)
1182 for (list = driver_list; list; list = list->next) {
1183 struct connman_device_driver *driver = list->data;
1185 if (match_driver(device, driver) == FALSE)
1188 DBG("driver %p name %s", driver, driver->name);
1190 if (driver->probe(device) == 0) {
1191 device->driver = driver;
1196 if (device->driver == NULL)
1199 return setup_device(device);
1202 static void device_remove(struct connman_element *element)
1204 struct connman_device *device = element->device;
1206 DBG("element %p name %s", element, element->name);
1211 if (device->driver == NULL)
1214 remove_device(device);
1217 static struct connman_driver device_driver = {
1219 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1220 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1221 .probe = device_probe,
1222 .remove = device_remove,
1225 static int device_load(struct connman_device *device)
1227 const char *ident = __connman_profile_active_ident();
1229 GError *error = NULL;
1231 connman_bool_t powered;
1234 DBG("device %p", device);
1236 keyfile = __connman_storage_open_profile(ident);
1237 if (keyfile == NULL)
1240 identifier = g_strdup_printf("device_%s", device->element.name);
1241 if (identifier == NULL)
1244 powered = g_key_file_get_boolean(keyfile, identifier,
1247 device->powered_persistent = powered;
1248 g_clear_error(&error);
1250 val = g_key_file_get_integer(keyfile, identifier,
1251 "ScanInterval", &error);
1253 device->scan_interval = val;
1254 g_clear_error(&error);
1259 __connman_storage_close_profile(ident, keyfile, FALSE);
1264 static int device_save(struct connman_device *device)
1266 const char *ident = __connman_profile_active_ident();
1270 DBG("device %p", device);
1272 keyfile = __connman_storage_open_profile(ident);
1273 if (keyfile == NULL)
1276 identifier = g_strdup_printf("device_%s", device->element.name);
1277 if (identifier == NULL)
1280 g_key_file_set_boolean(keyfile, identifier,
1281 "Powered", device->powered_persistent);
1283 g_key_file_set_integer(keyfile, identifier,
1284 "ScanInterval", device->scan_interval);
1289 __connman_storage_close_profile(ident, keyfile, TRUE);
1294 static struct connman_storage device_storage = {
1296 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1297 .device_load = device_load,
1298 .device_save = device_save,
1301 int __connman_device_init(void)
1305 if (connman_storage_register(&device_storage) < 0)
1306 connman_error("Failed to register device storage");
1308 return connman_driver_register(&device_driver);
1311 void __connman_device_cleanup(void)
1315 connman_driver_unregister(&device_driver);
1317 connman_storage_unregister(&device_storage);