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;
50 unsigned int connections;
53 struct connman_device_driver *driver;
57 struct connman_network *network;
61 static gboolean device_scan_trigger(gpointer user_data)
63 struct connman_device *device = user_data;
65 DBG("device %p", device);
67 if (device->driver == NULL) {
68 device->scan_timeout = 0;
72 if (device->driver->scan)
73 device->driver->scan(device);
78 static void clear_scan_trigger(struct connman_device *device)
80 if (device->scan_timeout > 0) {
81 g_source_remove(device->scan_timeout);
82 device->scan_timeout = 0;
86 static void reset_scan_trigger(struct connman_device *device)
88 clear_scan_trigger(device);
90 if (device->scan_interval > 0) {
91 guint interval = device->scan_interval;
92 device->scan_timeout = g_timeout_add_seconds(interval,
93 device_scan_trigger, device);
97 static void force_scan_trigger(struct connman_device *device)
99 clear_scan_trigger(device);
101 device->scan_timeout = g_timeout_add_seconds(5,
102 device_scan_trigger, device);
105 void connman_device_schedule_scan(struct connman_device *device)
107 reset_scan_trigger(device);
110 static const char *type2description(enum connman_device_type type)
113 case CONNMAN_DEVICE_TYPE_UNKNOWN:
114 case CONNMAN_DEVICE_TYPE_VENDOR:
116 case CONNMAN_DEVICE_TYPE_ETHERNET:
118 case CONNMAN_DEVICE_TYPE_WIFI:
120 case CONNMAN_DEVICE_TYPE_WIMAX:
122 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
124 case CONNMAN_DEVICE_TYPE_GPS:
126 case CONNMAN_DEVICE_TYPE_CELLULAR:
133 static const char *type2string(enum connman_device_type type)
136 case CONNMAN_DEVICE_TYPE_UNKNOWN:
137 case CONNMAN_DEVICE_TYPE_VENDOR:
139 case CONNMAN_DEVICE_TYPE_ETHERNET:
141 case CONNMAN_DEVICE_TYPE_WIFI:
143 case CONNMAN_DEVICE_TYPE_WIMAX:
145 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
147 case CONNMAN_DEVICE_TYPE_GPS:
149 case CONNMAN_DEVICE_TYPE_CELLULAR:
156 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
158 enum connman_device_type type = connman_device_get_type(device);
161 case CONNMAN_DEVICE_TYPE_UNKNOWN:
162 case CONNMAN_DEVICE_TYPE_VENDOR:
163 case CONNMAN_DEVICE_TYPE_GPS:
165 case CONNMAN_DEVICE_TYPE_ETHERNET:
166 return CONNMAN_SERVICE_TYPE_ETHERNET;
167 case CONNMAN_DEVICE_TYPE_WIFI:
168 return CONNMAN_SERVICE_TYPE_WIFI;
169 case CONNMAN_DEVICE_TYPE_WIMAX:
170 return CONNMAN_SERVICE_TYPE_WIMAX;
171 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
172 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
173 case CONNMAN_DEVICE_TYPE_CELLULAR:
174 return CONNMAN_SERVICE_TYPE_CELLULAR;
177 return CONNMAN_SERVICE_TYPE_UNKNOWN;
180 int __connman_device_enable(struct connman_device *device)
184 DBG("device %p %d", device, device->blocked);
186 if (!device->driver || !device->driver->enable)
189 if (device->powered_pending == TRUE)
192 if (device->blocked == TRUE)
195 err = device->driver->enable(device);
197 if (err == -EINPROGRESS) {
198 device->powered_pending = TRUE;
199 device->offlinemode = FALSE;
200 if (__connman_profile_get_offlinemode() == TRUE)
201 __connman_profile_set_offlinemode(FALSE, FALSE);
206 device->powered_pending = TRUE;
207 device->powered = TRUE;
208 device->offlinemode = FALSE;
209 if (__connman_profile_get_offlinemode() == TRUE)
210 __connman_profile_set_offlinemode(FALSE, FALSE);
212 __connman_technology_enable_device(device);
217 int __connman_device_disable(struct connman_device *device)
221 DBG("device %p", device);
223 if (!device->driver || !device->driver->disable)
226 if (device->powered == FALSE)
229 if (device->powered_pending == FALSE)
232 device->reconnect = FALSE;
234 clear_scan_trigger(device);
236 g_hash_table_remove_all(device->networks);
238 err = device->driver->disable(device);
240 if (err == -EINPROGRESS)
241 device->powered_pending = FALSE;
245 device->powered_pending = FALSE;
246 device->powered = FALSE;
248 __connman_technology_disable_device(device);
253 static int set_powered(struct connman_device *device, connman_bool_t powered)
255 DBG("device %p powered %d", device, powered);
258 return __connman_device_enable(device);
260 return __connman_device_disable(device);
263 static int setup_device(struct connman_device *device)
265 DBG("device %p", device);
267 __connman_technology_add_device(device);
269 if (device->offlinemode == FALSE &&
270 device->powered_persistent == TRUE)
271 __connman_device_enable(device);
276 static void probe_driver(struct connman_element *element, gpointer user_data)
278 struct connman_device_driver *driver = user_data;
280 DBG("element %p name %s", element, element->name);
282 if (element->device == NULL)
285 if (element->device->driver != NULL)
288 if (driver->type != element->device->type)
291 if (driver->probe(element->device) < 0)
294 element->device->driver = driver;
296 __connman_element_set_driver(element);
298 setup_device(element->device);
301 static void remove_device(struct connman_device *device)
305 DBG("device %p", device);
307 err = __connman_device_disable(device);
308 if (err < 0 && err == -EINPROGRESS)
309 __connman_technology_disable_device(device);
311 __connman_technology_remove_device(device);
313 if (device->driver->remove)
314 device->driver->remove(device);
316 device->driver = NULL;
319 static void remove_driver(struct connman_element *element, gpointer user_data)
321 struct connman_device_driver *driver = user_data;
323 DBG("element %p name %s", element, element->name);
325 if (element->device == NULL)
328 if (element->device->driver == driver)
329 remove_device(element->device);
332 connman_bool_t __connman_device_has_driver(struct connman_device *device)
334 if (device == NULL || device->driver == NULL)
340 static GSList *driver_list = NULL;
342 static gint compare_priority(gconstpointer a, gconstpointer b)
344 const struct connman_device_driver *driver1 = a;
345 const struct connman_device_driver *driver2 = b;
347 return driver2->priority - driver1->priority;
351 * connman_device_driver_register:
352 * @driver: device driver definition
354 * Register a new device driver
356 * Returns: %0 on success
358 int connman_device_driver_register(struct connman_device_driver *driver)
360 DBG("driver %p name %s", driver, driver->name);
362 driver_list = g_slist_insert_sorted(driver_list, driver,
365 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
366 probe_driver, driver);
372 * connman_device_driver_unregister:
373 * @driver: device driver definition
375 * Remove a previously registered device driver
377 void connman_device_driver_unregister(struct connman_device_driver *driver)
379 DBG("driver %p name %s", driver, driver->name);
381 driver_list = g_slist_remove(driver_list, driver);
383 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
384 remove_driver, driver);
387 static void unregister_network(gpointer data)
389 struct connman_network *network = data;
391 DBG("network %p", network);
393 connman_element_unregister((struct connman_element *) network);
395 __connman_network_set_device(network, NULL);
397 connman_network_unref(network);
400 static void device_destruct(struct connman_element *element)
402 struct connman_device *device = element->device;
404 DBG("element %p name %s", element, element->name);
406 clear_scan_trigger(device);
408 g_free(device->ident);
409 g_free(device->node);
410 g_free(device->name);
411 g_free(device->address);
412 g_free(device->control);
413 g_free(device->interface);
415 g_free(device->last_network);
417 g_hash_table_destroy(device->networks);
418 device->networks = NULL;
422 * connman_device_create:
423 * @node: device node name (for example an address)
426 * Allocate a new device of given #type and assign the #node name to it.
428 * Returns: a newly-allocated #connman_device structure
430 struct connman_device *connman_device_create(const char *node,
431 enum connman_device_type type)
433 struct connman_device *device;
435 enum connman_service_type service_type;
437 DBG("node %s type %d", node, type);
439 device = g_try_new0(struct connman_device, 1);
443 DBG("device %p", device);
445 __connman_element_initialize(&device->element);
447 device->element.name = g_strdup(node);
448 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
450 device->element.device = device;
451 device->element.destruct = device_destruct;
453 str = type2string(type);
455 connman_element_set_string(&device->element,
456 CONNMAN_PROPERTY_ID_TYPE, str);
458 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
461 device->name = g_strdup(type2description(device->type));
463 device->powered_persistent = TRUE;
465 device->phyindex = -1;
467 service_type = __connman_device_get_service_type(device);
468 device->blocked = __connman_technology_get_blocked(service_type);
471 case CONNMAN_DEVICE_TYPE_UNKNOWN:
472 case CONNMAN_DEVICE_TYPE_VENDOR:
473 device->scan_interval = 0;
475 case CONNMAN_DEVICE_TYPE_ETHERNET:
476 case CONNMAN_DEVICE_TYPE_WIFI:
477 device->scan_interval = 300;
479 case CONNMAN_DEVICE_TYPE_WIMAX:
480 device->scan_interval = 0;
482 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
483 device->scan_interval = 0;
485 case CONNMAN_DEVICE_TYPE_GPS:
486 device->scan_interval = 0;
488 case CONNMAN_DEVICE_TYPE_CELLULAR:
489 device->scan_interval = 0;
493 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
494 g_free, unregister_network);
500 * connman_device_ref:
501 * @device: device structure
503 * Increase reference counter of device
505 struct connman_device *connman_device_ref(struct connman_device *device)
507 if (connman_element_ref(&device->element) == NULL)
514 * connman_device_unref:
515 * @device: device structure
517 * Decrease reference counter of device
519 void connman_device_unref(struct connman_device *device)
521 connman_element_unref(&device->element);
524 const char *__connman_device_get_type(struct connman_device *device)
526 return type2string(device->type);
530 * connman_device_get_type:
531 * @device: device structure
535 enum connman_device_type connman_device_get_type(struct connman_device *device)
541 * connman_device_get_name:
542 * @device: device structure
544 * Get unique name of device
546 const char *connman_device_get_name(struct connman_device *device)
548 return device->element.name;
552 * connman_device_get_path:
553 * @device: device structure
555 * Get path name of device
557 const char *connman_device_get_path(struct connman_device *device)
559 return device->element.path;
563 * connman_device_set_index:
564 * @device: device structure
565 * @index: index number
567 * Set index number of device
569 void connman_device_set_index(struct connman_device *device, int index)
571 device->element.index = index;
575 * connman_device_get_index:
576 * @device: device structure
578 * Get index number of device
580 int connman_device_get_index(struct connman_device *device)
582 return device->element.index;
585 int __connman_device_get_phyindex(struct connman_device *device)
587 return device->phyindex;
590 void __connman_device_set_phyindex(struct connman_device *device,
593 device->phyindex = phyindex;
597 * connman_device_set_interface:
598 * @device: device structure
599 * @interface: interface name
600 * @control: control interface
602 * Set interface name of device
604 void connman_device_set_interface(struct connman_device *device,
605 const char *interface, const char *control)
607 g_free(device->element.devname);
608 device->element.devname = g_strdup(interface);
610 g_free(device->interface);
611 device->interface = g_strdup(interface);
613 g_free(device->control);
614 device->control = g_strdup(control);
616 if (device->name == NULL) {
617 const char *str = type2description(device->type);
618 if (str != NULL && device->interface != NULL)
619 device->name = g_strdup_printf("%s (%s)", str,
624 const char *connman_device_get_control(struct connman_device *device)
626 return device->control;
630 * connman_device_set_ident:
631 * @device: device structure
632 * @ident: unique identifier
634 * Set unique identifier of device
636 void connman_device_set_ident(struct connman_device *device,
639 g_free(device->ident);
640 device->ident = g_strdup(ident);
643 const char *connman_device_get_ident(struct connman_device *device)
645 return device->ident;
649 * connman_device_set_powered:
650 * @device: device structure
651 * @powered: powered state
653 * Change power state of device
655 int connman_device_set_powered(struct connman_device *device,
656 connman_bool_t powered)
658 DBG("driver %p powered %d", device, powered);
660 if (device->powered == powered) {
661 device->powered_pending = powered;
666 __connman_device_enable(device);
668 __connman_device_disable(device);
670 device->powered = powered;
671 device->powered_pending = powered;
673 if (device->powered == TRUE)
674 __connman_technology_enable_device(device);
676 __connman_technology_disable_device(device);
678 if (device->offlinemode == TRUE && powered == TRUE)
679 return connman_device_set_powered(device, FALSE);
681 if (powered == FALSE)
684 reset_scan_trigger(device);
686 if (device->driver->scan)
687 device->driver->scan(device);
692 int __connman_device_set_blocked(struct connman_device *device,
693 connman_bool_t blocked)
695 connman_bool_t powered;
697 DBG("device %p blocked %d", device, blocked);
699 device->blocked = blocked;
701 if (device->offlinemode == TRUE)
704 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
706 if (blocked == FALSE)
707 powered = device->powered_persistent;
711 return set_powered(device, powered);
714 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
716 return device->blocked;
719 int __connman_device_scan(struct connman_device *device)
721 if (!device->driver || !device->driver->scan)
724 if (device->powered == FALSE)
727 reset_scan_trigger(device);
729 return device->driver->scan(device);
732 int __connman_device_enable_persistent(struct connman_device *device)
736 DBG("device %p", device);
738 device->powered_persistent = TRUE;
740 __connman_storage_save_device(device);
742 err = __connman_device_enable(device);
743 if (err == 0 || err == -EINPROGRESS) {
744 device->offlinemode = FALSE;
745 if (__connman_profile_get_offlinemode() == TRUE) {
746 __connman_profile_set_offlinemode(FALSE, FALSE);
748 __connman_profile_save_default();
755 int __connman_device_disable_persistent(struct connman_device *device)
757 DBG("device %p", device);
759 device->powered_persistent = FALSE;
761 __connman_storage_save_device(device);
763 return __connman_device_disable(device);
766 int __connman_device_disconnect(struct connman_device *device)
771 DBG("device %p", device);
773 connman_device_set_disconnected(device, TRUE);
775 g_hash_table_iter_init(&iter, device->networks);
777 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
778 struct connman_network *network = value;
780 if (__connman_network_get_connecting(network) == TRUE) {
782 * Skip network in the process of connecting.
783 * This is a workaround for WiFi networks serviced
784 * by the supplicant plugin that hold a reference
785 * to the network. If we disconnect the network
786 * here then the referenced object will not be
787 * registered and usage (like launching DHCP client)
788 * will fail. There is nothing to be gained by
789 * removing the network here anyway.
791 connman_warn("Skipping disconnect of %s",
792 connman_network_get_identifier(network));
796 __connman_network_disconnect(network);
802 static void mark_network_unavailable(gpointer key, gpointer value,
805 struct connman_network *network = value;
807 if (connman_network_get_connected(network) == TRUE)
810 connman_network_set_available(network, FALSE);
813 static gboolean remove_unavailable_network(gpointer key, gpointer value,
816 struct connman_network *network = value;
818 if (connman_network_get_connected(network) == TRUE)
821 if (connman_network_get_available(network) == TRUE)
827 void __connman_device_cleanup_networks(struct connman_device *device)
829 g_hash_table_foreach_remove(device->networks,
830 remove_unavailable_network, NULL);
834 * connman_device_set_scanning:
835 * @device: device structure
836 * @scanning: scanning state
838 * Change scanning state of device
840 int connman_device_set_scanning(struct connman_device *device,
841 connman_bool_t scanning)
843 DBG("device %p scanning %d", device, scanning);
845 if (!device->driver || !device->driver->scan)
848 if (device->scanning == scanning)
851 device->scanning = scanning;
853 if (scanning == TRUE) {
854 reset_scan_trigger(device);
856 g_hash_table_foreach(device->networks,
857 mark_network_unavailable, NULL);
862 __connman_device_cleanup_networks(device);
864 if (device->connections > 0)
867 if (device->disconnected == TRUE)
870 __connman_service_auto_connect();
876 * connman_device_set_disconnected:
877 * @device: device structure
878 * @disconnected: disconnected state
880 * Change disconnected state of device (only for device with networks)
882 int connman_device_set_disconnected(struct connman_device *device,
883 connman_bool_t disconnected)
885 DBG("device %p disconnected %d", device, disconnected);
887 if (device->disconnected == disconnected)
890 device->disconnected = disconnected;
892 if (disconnected == TRUE)
893 force_scan_trigger(device);
899 * connman_device_get_disconnected:
900 * @device: device structure
902 * Get device disconnected state
904 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
906 return device->disconnected;
910 * connman_device_set_string:
911 * @device: device structure
912 * @key: unique identifier
913 * @value: string value
915 * Set string value for specific key
917 int connman_device_set_string(struct connman_device *device,
918 const char *key, const char *value)
920 DBG("device %p key %s value %s", device, key, value);
922 if (g_str_equal(key, "Address") == TRUE) {
923 g_free(device->address);
924 device->address = g_strdup(value);
925 } else if (g_str_equal(key, "Name") == TRUE) {
926 g_free(device->name);
927 device->name = g_strdup(value);
928 } else if (g_str_equal(key, "Node") == TRUE) {
929 g_free(device->node);
930 device->node = g_strdup(value);
933 return connman_element_set_string(&device->element, key, value);
937 * connman_device_get_string:
938 * @device: device structure
939 * @key: unique identifier
941 * Get string value for specific key
943 const char *connman_device_get_string(struct connman_device *device,
946 DBG("device %p key %s", device, key);
948 if (g_str_equal(key, "Address") == TRUE)
949 return device->address;
950 else if (g_str_equal(key, "Name") == TRUE)
952 else if (g_str_equal(key, "Node") == TRUE)
954 else if (g_str_equal(key, "Interface") == TRUE)
955 return device->interface;
957 return connman_element_get_string(&device->element, key);
960 static void set_offlinemode(struct connman_element *element, gpointer user_data)
962 struct connman_device *device = element->device;
963 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
964 connman_bool_t powered;
966 DBG("element %p name %s", element, element->name);
971 device->offlinemode = offlinemode;
973 if (device->blocked == TRUE)
976 powered = (offlinemode == TRUE) ? FALSE : TRUE;
978 if (device->powered == powered)
981 if (device->powered_persistent == FALSE)
984 set_powered(device, powered);
987 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
989 DBG("offlinmode %d", offlinemode);
991 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
992 set_offlinemode, GUINT_TO_POINTER(offlinemode));
994 __connman_notifier_offlinemode(offlinemode);
999 void __connman_device_increase_connections(struct connman_device *device)
1004 device->connections++;
1007 void __connman_device_decrease_connections(struct connman_device *device)
1012 device->connections--;
1016 * connman_device_add_network:
1017 * @device: device structure
1018 * @network: network structure
1020 * Add new network to the device
1022 int connman_device_add_network(struct connman_device *device,
1023 struct connman_network *network)
1025 const char *identifier = connman_network_get_identifier(network);
1028 DBG("device %p network %p", device, network);
1030 if (identifier == NULL)
1033 __connman_network_set_device(network, device);
1035 err = connman_element_register((struct connman_element *) network,
1038 __connman_network_set_device(network, NULL);
1042 g_hash_table_insert(device->networks, g_strdup(identifier),
1049 * connman_device_get_network:
1050 * @device: device structure
1051 * @identifier: network identifier
1053 * Get network for given identifier
1055 struct connman_network *connman_device_get_network(struct connman_device *device,
1056 const char *identifier)
1058 DBG("device %p identifier %s", device, identifier);
1060 return g_hash_table_lookup(device->networks, identifier);
1064 * connman_device_remove_network:
1065 * @device: device structure
1066 * @identifier: network identifier
1068 * Remove network for given identifier
1070 int connman_device_remove_network(struct connman_device *device,
1071 const char *identifier)
1073 DBG("device %p identifier %s", device, identifier);
1075 g_hash_table_remove(device->networks, identifier);
1080 void connman_device_remove_all_networks(struct connman_device *device)
1082 g_hash_table_remove_all(device->networks);
1085 void __connman_device_set_network(struct connman_device *device,
1086 struct connman_network *network)
1093 if (device->network == network)
1096 if (device->network != NULL)
1097 connman_network_unref(device->network);
1099 if (network != NULL) {
1100 name = connman_network_get_string(network,
1101 CONNMAN_PROPERTY_ID_NAME);
1102 g_free(device->last_network);
1103 device->last_network = g_strdup(name);
1105 device->network = connman_network_ref(network);
1107 g_free(device->last_network);
1108 device->last_network = NULL;
1110 device->network = NULL;
1114 void __connman_device_set_reconnect(struct connman_device *device,
1115 connman_bool_t reconnect)
1117 device->reconnect = reconnect;
1120 connman_bool_t __connman_device_get_reconnect(
1121 struct connman_device *device)
1123 return device->reconnect;
1127 * connman_device_register:
1128 * @device: device structure
1130 * Register device with the system
1132 int connman_device_register(struct connman_device *device)
1134 __connman_storage_load_device(device);
1136 device->offlinemode = __connman_profile_get_offlinemode();
1138 return connman_element_register(&device->element, NULL);
1142 * connman_device_unregister:
1143 * @device: device structure
1145 * Unregister device with the system
1147 void connman_device_unregister(struct connman_device *device)
1149 __connman_storage_save_device(device);
1151 connman_element_unregister(&device->element);
1155 * connman_device_get_data:
1156 * @device: device structure
1158 * Get private device data pointer
1160 void *connman_device_get_data(struct connman_device *device)
1162 return device->driver_data;
1166 * connman_device_set_data:
1167 * @device: device structure
1168 * @data: data pointer
1170 * Set private device data pointer
1172 void connman_device_set_data(struct connman_device *device, void *data)
1174 device->driver_data = data;
1177 static gboolean match_driver(struct connman_device *device,
1178 struct connman_device_driver *driver)
1180 if (device->type == driver->type ||
1181 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1187 static int device_probe(struct connman_element *element)
1189 struct connman_device *device = element->device;
1192 DBG("element %p name %s", element, element->name);
1197 if (device->driver != NULL)
1200 for (list = driver_list; list; list = list->next) {
1201 struct connman_device_driver *driver = list->data;
1203 if (match_driver(device, driver) == FALSE)
1206 DBG("driver %p name %s", driver, driver->name);
1208 if (driver->probe(device) == 0) {
1209 device->driver = driver;
1214 if (device->driver == NULL)
1217 return setup_device(device);
1220 static void device_remove(struct connman_element *element)
1222 struct connman_device *device = element->device;
1224 DBG("element %p name %s", element, element->name);
1229 if (device->driver == NULL)
1232 remove_device(device);
1235 static struct connman_driver device_driver = {
1237 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1238 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1239 .probe = device_probe,
1240 .remove = device_remove,
1243 static int device_load(struct connman_device *device)
1245 const char *ident = __connman_profile_active_ident();
1247 GError *error = NULL;
1249 connman_bool_t powered;
1252 DBG("device %p", device);
1254 keyfile = __connman_storage_open_profile(ident);
1255 if (keyfile == NULL)
1258 identifier = g_strdup_printf("device_%s", device->element.name);
1259 if (identifier == NULL)
1262 powered = g_key_file_get_boolean(keyfile, identifier,
1265 device->powered_persistent = powered;
1266 g_clear_error(&error);
1268 val = g_key_file_get_integer(keyfile, identifier,
1269 "ScanInterval", &error);
1271 device->scan_interval = val;
1272 g_clear_error(&error);
1277 __connman_storage_close_profile(ident, keyfile, FALSE);
1282 static int device_save(struct connman_device *device)
1284 const char *ident = __connman_profile_active_ident();
1288 DBG("device %p", device);
1290 keyfile = __connman_storage_open_profile(ident);
1291 if (keyfile == NULL)
1294 identifier = g_strdup_printf("device_%s", device->element.name);
1295 if (identifier == NULL)
1298 g_key_file_set_boolean(keyfile, identifier,
1299 "Powered", device->powered_persistent);
1301 g_key_file_set_integer(keyfile, identifier,
1302 "ScanInterval", device->scan_interval);
1307 __connman_storage_close_profile(ident, keyfile, TRUE);
1312 static struct connman_storage device_storage = {
1314 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1315 .device_load = device_load,
1316 .device_save = device_save,
1319 int __connman_device_init(void)
1323 if (connman_storage_register(&device_storage) < 0)
1324 connman_error("Failed to register device storage");
1326 return connman_driver_register(&device_driver);
1329 void __connman_device_cleanup(void)
1333 connman_driver_unregister(&device_driver);
1335 connman_storage_unregister(&device_storage);