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->powered_pending = FALSE;
254 device->powered = FALSE;
256 __connman_technology_disable_device(device);
261 static int set_powered(struct connman_device *device, connman_bool_t powered)
263 DBG("device %p powered %d", device, powered);
266 return __connman_device_enable(device);
268 return __connman_device_disable(device);
271 static int setup_device(struct connman_device *device)
273 DBG("device %p", device);
275 __connman_technology_add_device(device);
277 if (device->offlinemode == FALSE &&
278 device->powered_persistent == TRUE)
279 __connman_device_enable(device);
284 static void probe_driver(struct connman_element *element, gpointer user_data)
286 struct connman_device_driver *driver = user_data;
288 DBG("element %p name %s", element, element->name);
290 if (element->device == NULL)
293 if (element->device->driver != NULL)
296 if (driver->type != element->device->type)
299 if (driver->probe(element->device) < 0)
302 element->device->driver = driver;
304 __connman_element_set_driver(element);
306 setup_device(element->device);
309 static void remove_device(struct connman_device *device)
311 DBG("device %p", device);
313 __connman_device_disable(device);
315 __connman_technology_remove_device(device);
317 if (device->driver->remove)
318 device->driver->remove(device);
320 device->driver = NULL;
323 static void remove_driver(struct connman_element *element, gpointer user_data)
325 struct connman_device_driver *driver = user_data;
327 DBG("element %p name %s", element, element->name);
329 if (element->device == NULL)
332 if (element->device->driver == driver)
333 remove_device(element->device);
336 connman_bool_t __connman_device_has_driver(struct connman_device *device)
338 if (device == NULL || device->driver == NULL)
344 static GSList *driver_list = NULL;
346 static gint compare_priority(gconstpointer a, gconstpointer b)
348 const struct connman_device_driver *driver1 = a;
349 const struct connman_device_driver *driver2 = b;
351 return driver2->priority - driver1->priority;
355 * connman_device_driver_register:
356 * @driver: device driver definition
358 * Register a new device driver
360 * Returns: %0 on success
362 int connman_device_driver_register(struct connman_device_driver *driver)
364 DBG("driver %p name %s", driver, driver->name);
366 driver_list = g_slist_insert_sorted(driver_list, driver,
369 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
370 probe_driver, driver);
376 * connman_device_driver_unregister:
377 * @driver: device driver definition
379 * Remove a previously registered device driver
381 void connman_device_driver_unregister(struct connman_device_driver *driver)
383 DBG("driver %p name %s", driver, driver->name);
385 driver_list = g_slist_remove(driver_list, driver);
387 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
388 remove_driver, driver);
391 static void unregister_network(gpointer data)
393 struct connman_network *network = data;
395 DBG("network %p", network);
397 connman_element_unregister((struct connman_element *) network);
399 __connman_network_set_device(network, NULL);
401 connman_network_unref(network);
404 static void device_destruct(struct connman_element *element)
406 struct connman_device *device = element->device;
408 DBG("element %p name %s", element, element->name);
410 clear_scan_trigger(device);
412 g_free(device->ident);
413 g_free(device->node);
414 g_free(device->name);
415 g_free(device->address);
416 g_free(device->interface);
418 g_free(device->last_network);
420 g_hash_table_destroy(device->networks);
421 device->networks = NULL;
425 * connman_device_create:
426 * @node: device node name (for example an address)
429 * Allocate a new device of given #type and assign the #node name to it.
431 * Returns: a newly-allocated #connman_device structure
433 struct connman_device *connman_device_create(const char *node,
434 enum connman_device_type type)
436 struct connman_device *device;
438 enum connman_service_type service_type;
440 DBG("node %s type %d", node, type);
442 device = g_try_new0(struct connman_device, 1);
446 DBG("device %p", device);
448 __connman_element_initialize(&device->element);
450 device->element.name = g_strdup(node);
451 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
453 device->element.device = device;
454 device->element.destruct = device_destruct;
456 str = type2string(type);
458 connman_element_set_string(&device->element,
459 CONNMAN_PROPERTY_ID_TYPE, str);
461 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
464 device->name = g_strdup(type2description(device->type));
466 device->powered_persistent = TRUE;
468 device->phyindex = -1;
470 service_type = __connman_device_get_service_type(device);
471 device->blocked = __connman_technology_get_blocked(service_type);
474 case CONNMAN_DEVICE_TYPE_UNKNOWN:
475 case CONNMAN_DEVICE_TYPE_VENDOR:
476 device->scan_interval = 0;
478 case CONNMAN_DEVICE_TYPE_ETHERNET:
479 case CONNMAN_DEVICE_TYPE_WIFI:
480 device->scan_interval = 300;
482 case CONNMAN_DEVICE_TYPE_WIMAX:
483 device->scan_interval = 0;
485 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
486 device->scan_interval = 0;
488 case CONNMAN_DEVICE_TYPE_GPS:
489 device->scan_interval = 0;
491 case CONNMAN_DEVICE_TYPE_CELLULAR:
492 device->scan_interval = 0;
494 case CONNMAN_DEVICE_TYPE_GADGET:
495 device->scan_interval = 0;
499 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
500 g_free, unregister_network);
506 * connman_device_ref:
507 * @device: device structure
509 * Increase reference counter of device
511 struct connman_device *connman_device_ref(struct connman_device *device)
513 if (connman_element_ref(&device->element) == NULL)
520 * connman_device_unref:
521 * @device: device structure
523 * Decrease reference counter of device
525 void connman_device_unref(struct connman_device *device)
527 connman_element_unref(&device->element);
530 const char *__connman_device_get_type(struct connman_device *device)
532 return type2string(device->type);
536 * connman_device_get_type:
537 * @device: device structure
541 enum connman_device_type connman_device_get_type(struct connman_device *device)
547 * connman_device_set_index:
548 * @device: device structure
549 * @index: index number
551 * Set index number of device
553 void connman_device_set_index(struct connman_device *device, int index)
555 device->element.index = index;
559 * connman_device_get_index:
560 * @device: device structure
562 * Get index number of device
564 int connman_device_get_index(struct connman_device *device)
566 return device->element.index;
569 int __connman_device_get_phyindex(struct connman_device *device)
571 return device->phyindex;
574 void __connman_device_set_phyindex(struct connman_device *device,
577 device->phyindex = phyindex;
581 * connman_device_set_interface:
582 * @device: device structure
583 * @interface: interface name
585 * Set interface name of device
587 void connman_device_set_interface(struct connman_device *device,
588 const char *interface)
590 g_free(device->element.devname);
591 device->element.devname = g_strdup(interface);
593 g_free(device->interface);
594 device->interface = g_strdup(interface);
596 if (device->name == NULL) {
597 const char *str = type2description(device->type);
598 if (str != NULL && device->interface != NULL)
599 device->name = g_strdup_printf("%s (%s)", str,
605 * connman_device_set_ident:
606 * @device: device structure
607 * @ident: unique identifier
609 * Set unique identifier of device
611 void connman_device_set_ident(struct connman_device *device,
614 g_free(device->ident);
615 device->ident = g_strdup(ident);
618 const char *connman_device_get_ident(struct connman_device *device)
620 return device->ident;
624 * connman_device_set_powered:
625 * @device: device structure
626 * @powered: powered state
628 * Change power state of device
630 int connman_device_set_powered(struct connman_device *device,
631 connman_bool_t powered)
635 DBG("driver %p powered %d", device, powered);
637 if (device->powered == powered) {
638 device->powered_pending = powered;
643 err = __connman_device_enable(device);
645 err = __connman_device_disable(device);
647 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
650 device->powered = powered;
651 device->powered_pending = powered;
653 if (device->powered == TRUE)
654 __connman_technology_enable_device(device);
656 __connman_technology_disable_device(device);
658 if (device->offlinemode == TRUE && powered == TRUE)
659 return connman_device_set_powered(device, FALSE);
661 if (powered == FALSE)
664 reset_scan_trigger(device);
666 if (device->driver && device->driver->scan)
667 device->driver->scan(device);
672 int __connman_device_set_blocked(struct connman_device *device,
673 connman_bool_t blocked)
675 connman_bool_t powered;
677 DBG("device %p blocked %d", device, blocked);
679 device->blocked = blocked;
681 if (device->offlinemode == TRUE)
684 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
686 if (blocked == FALSE)
687 powered = device->powered_persistent;
691 return set_powered(device, powered);
694 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
696 return device->blocked;
699 int __connman_device_scan(struct connman_device *device)
701 if (!device->driver || !device->driver->scan)
704 if (device->powered == FALSE)
707 reset_scan_trigger(device);
709 return device->driver->scan(device);
712 int __connman_device_enable_persistent(struct connman_device *device)
716 DBG("device %p", device);
718 device->powered_persistent = TRUE;
720 __connman_storage_save_device(device);
722 err = __connman_device_enable(device);
723 if (err == 0 || err == -EINPROGRESS) {
724 device->offlinemode = FALSE;
725 if (__connman_profile_get_offlinemode() == TRUE) {
726 __connman_profile_set_offlinemode(FALSE, FALSE);
728 __connman_profile_save_default();
735 int __connman_device_disable_persistent(struct connman_device *device)
737 DBG("device %p", device);
739 device->powered_persistent = FALSE;
741 __connman_storage_save_device(device);
743 return __connman_device_disable(device);
746 int __connman_device_disconnect(struct connman_device *device)
751 DBG("device %p", device);
753 connman_device_set_disconnected(device, TRUE);
755 g_hash_table_iter_init(&iter, device->networks);
757 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
758 struct connman_network *network = value;
760 if (__connman_network_get_connecting(network) == TRUE) {
762 * Skip network in the process of connecting.
763 * This is a workaround for WiFi networks serviced
764 * by the supplicant plugin that hold a reference
765 * to the network. If we disconnect the network
766 * here then the referenced object will not be
767 * registered and usage (like launching DHCP client)
768 * will fail. There is nothing to be gained by
769 * removing the network here anyway.
771 connman_warn("Skipping disconnect of %s",
772 connman_network_get_identifier(network));
776 __connman_network_disconnect(network);
782 static void mark_network_unavailable(gpointer key, gpointer value,
785 struct connman_network *network = value;
787 if (connman_network_get_connected(network) == TRUE)
790 connman_network_set_available(network, FALSE);
793 static gboolean remove_unavailable_network(gpointer key, gpointer value,
796 struct connman_network *network = value;
798 if (connman_network_get_connected(network) == TRUE)
801 if (connman_network_get_available(network) == TRUE)
807 void __connman_device_cleanup_networks(struct connman_device *device)
809 g_hash_table_foreach_remove(device->networks,
810 remove_unavailable_network, NULL);
814 * connman_device_set_scanning:
815 * @device: device structure
816 * @scanning: scanning state
818 * Change scanning state of device
820 int connman_device_set_scanning(struct connman_device *device,
821 connman_bool_t scanning)
823 DBG("device %p scanning %d", device, scanning);
825 if (!device->driver || !device->driver->scan)
828 if (device->scanning == scanning)
831 device->scanning = scanning;
833 if (scanning == TRUE) {
834 reset_scan_trigger(device);
836 g_hash_table_foreach(device->networks,
837 mark_network_unavailable, NULL);
842 __connman_device_cleanup_networks(device);
844 if (device->connections > 0)
847 if (device->disconnected == TRUE)
850 __connman_service_auto_connect();
856 * connman_device_set_disconnected:
857 * @device: device structure
858 * @disconnected: disconnected state
860 * Change disconnected state of device (only for device with networks)
862 int connman_device_set_disconnected(struct connman_device *device,
863 connman_bool_t disconnected)
865 DBG("device %p disconnected %d", device, disconnected);
867 if (device->disconnected == disconnected)
870 device->disconnected = disconnected;
872 if (disconnected == TRUE)
873 force_scan_trigger(device);
879 * connman_device_get_disconnected:
880 * @device: device structure
882 * Get device disconnected state
884 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
886 return device->disconnected;
890 * connman_device_set_string:
891 * @device: device structure
892 * @key: unique identifier
893 * @value: string value
895 * Set string value for specific key
897 int connman_device_set_string(struct connman_device *device,
898 const char *key, const char *value)
900 DBG("device %p key %s value %s", device, key, value);
902 if (g_str_equal(key, "Address") == TRUE) {
903 g_free(device->address);
904 device->address = g_strdup(value);
905 } else if (g_str_equal(key, "Name") == TRUE) {
906 g_free(device->name);
907 device->name = g_strdup(value);
908 } else if (g_str_equal(key, "Node") == TRUE) {
909 g_free(device->node);
910 device->node = g_strdup(value);
913 return connman_element_set_string(&device->element, key, value);
917 * connman_device_get_string:
918 * @device: device structure
919 * @key: unique identifier
921 * Get string value for specific key
923 const char *connman_device_get_string(struct connman_device *device,
926 DBG("device %p key %s", device, key);
928 if (g_str_equal(key, "Address") == TRUE)
929 return device->address;
930 else if (g_str_equal(key, "Name") == TRUE)
932 else if (g_str_equal(key, "Node") == TRUE)
934 else if (g_str_equal(key, "Interface") == TRUE)
935 return device->interface;
937 return connman_element_get_string(&device->element, key);
940 static void set_offlinemode(struct connman_element *element, gpointer user_data)
942 struct connman_device *device = element->device;
943 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
944 connman_bool_t powered;
946 DBG("element %p name %s", element, element->name);
951 device->offlinemode = offlinemode;
953 if (device->blocked == TRUE)
956 powered = (offlinemode == TRUE) ? FALSE : TRUE;
958 if (device->powered == powered)
961 if (device->powered_persistent == FALSE)
964 set_powered(device, powered);
967 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
969 DBG("offlinmode %d", offlinemode);
971 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
972 set_offlinemode, GUINT_TO_POINTER(offlinemode));
974 __connman_notifier_offlinemode(offlinemode);
979 void __connman_device_increase_connections(struct connman_device *device)
984 device->connections++;
987 void __connman_device_decrease_connections(struct connman_device *device)
992 device->connections--;
996 * connman_device_add_network:
997 * @device: device structure
998 * @network: network structure
1000 * Add new network to the device
1002 int connman_device_add_network(struct connman_device *device,
1003 struct connman_network *network)
1005 const char *identifier = connman_network_get_identifier(network);
1008 DBG("device %p network %p", device, network);
1010 if (identifier == NULL)
1013 __connman_network_set_device(network, device);
1015 err = connman_element_register((struct connman_element *) network,
1018 __connman_network_set_device(network, NULL);
1022 g_hash_table_insert(device->networks, g_strdup(identifier),
1029 * connman_device_get_network:
1030 * @device: device structure
1031 * @identifier: network identifier
1033 * Get network for given identifier
1035 struct connman_network *connman_device_get_network(struct connman_device *device,
1036 const char *identifier)
1038 DBG("device %p identifier %s", device, identifier);
1040 return g_hash_table_lookup(device->networks, identifier);
1044 * connman_device_remove_network:
1045 * @device: device structure
1046 * @identifier: network identifier
1048 * Remove network for given identifier
1050 int connman_device_remove_network(struct connman_device *device,
1051 const char *identifier)
1053 DBG("device %p identifier %s", device, identifier);
1055 g_hash_table_remove(device->networks, identifier);
1060 void connman_device_remove_all_networks(struct connman_device *device)
1062 g_hash_table_remove_all(device->networks);
1065 void __connman_device_set_network(struct connman_device *device,
1066 struct connman_network *network)
1073 if (device->network == network)
1076 if (device->network != NULL)
1077 connman_network_unref(device->network);
1079 if (network != NULL) {
1080 name = connman_network_get_string(network,
1081 CONNMAN_PROPERTY_ID_NAME);
1082 g_free(device->last_network);
1083 device->last_network = g_strdup(name);
1085 device->network = connman_network_ref(network);
1087 g_free(device->last_network);
1088 device->last_network = NULL;
1090 device->network = NULL;
1094 void __connman_device_set_reconnect(struct connman_device *device,
1095 connman_bool_t reconnect)
1097 device->reconnect = reconnect;
1100 connman_bool_t __connman_device_get_reconnect(
1101 struct connman_device *device)
1103 return device->reconnect;
1107 * connman_device_register:
1108 * @device: device structure
1110 * Register device with the system
1112 int connman_device_register(struct connman_device *device)
1114 __connman_storage_load_device(device);
1116 device->offlinemode = __connman_profile_get_offlinemode();
1118 return connman_element_register(&device->element, NULL);
1122 * connman_device_unregister:
1123 * @device: device structure
1125 * Unregister device with the system
1127 void connman_device_unregister(struct connman_device *device)
1129 __connman_storage_save_device(device);
1131 connman_element_unregister(&device->element);
1135 * connman_device_get_data:
1136 * @device: device structure
1138 * Get private device data pointer
1140 void *connman_device_get_data(struct connman_device *device)
1142 return device->driver_data;
1146 * connman_device_set_data:
1147 * @device: device structure
1148 * @data: data pointer
1150 * Set private device data pointer
1152 void connman_device_set_data(struct connman_device *device, void *data)
1154 device->driver_data = data;
1157 static gboolean match_driver(struct connman_device *device,
1158 struct connman_device_driver *driver)
1160 if (device->type == driver->type ||
1161 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1167 static int device_probe(struct connman_element *element)
1169 struct connman_device *device = element->device;
1172 DBG("element %p name %s", element, element->name);
1177 if (device->driver != NULL)
1180 for (list = driver_list; list; list = list->next) {
1181 struct connman_device_driver *driver = list->data;
1183 if (match_driver(device, driver) == FALSE)
1186 DBG("driver %p name %s", driver, driver->name);
1188 if (driver->probe(device) == 0) {
1189 device->driver = driver;
1194 if (device->driver == NULL)
1197 return setup_device(device);
1200 static void device_remove(struct connman_element *element)
1202 struct connman_device *device = element->device;
1204 DBG("element %p name %s", element, element->name);
1209 if (device->driver == NULL)
1212 remove_device(device);
1215 static struct connman_driver device_driver = {
1217 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1218 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1219 .probe = device_probe,
1220 .remove = device_remove,
1223 static int device_load(struct connman_device *device)
1225 const char *ident = __connman_profile_active_ident();
1227 GError *error = NULL;
1229 connman_bool_t powered;
1232 DBG("device %p", device);
1234 keyfile = __connman_storage_open_profile(ident);
1235 if (keyfile == NULL)
1238 identifier = g_strdup_printf("device_%s", device->element.name);
1239 if (identifier == NULL)
1242 powered = g_key_file_get_boolean(keyfile, identifier,
1245 device->powered_persistent = powered;
1246 g_clear_error(&error);
1248 val = g_key_file_get_integer(keyfile, identifier,
1249 "ScanInterval", &error);
1251 device->scan_interval = val;
1252 g_clear_error(&error);
1257 __connman_storage_close_profile(ident, keyfile, FALSE);
1262 static int device_save(struct connman_device *device)
1264 const char *ident = __connman_profile_active_ident();
1268 DBG("device %p", device);
1270 keyfile = __connman_storage_open_profile(ident);
1271 if (keyfile == NULL)
1274 identifier = g_strdup_printf("device_%s", device->element.name);
1275 if (identifier == NULL)
1278 g_key_file_set_boolean(keyfile, identifier,
1279 "Powered", device->powered_persistent);
1281 g_key_file_set_integer(keyfile, identifier,
1282 "ScanInterval", device->scan_interval);
1287 __connman_storage_close_profile(ident, keyfile, TRUE);
1292 static struct connman_storage device_storage = {
1294 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1295 .device_load = device_load,
1296 .device_save = device_save,
1299 int __connman_device_init(void)
1303 if (connman_storage_register(&device_storage) < 0)
1304 connman_error("Failed to register device storage");
1306 return connman_driver_register(&device_driver);
1309 void __connman_device_cleanup(void)
1313 connman_driver_unregister(&device_driver);
1315 connman_storage_unregister(&device_storage);