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 connman_device_set_disconnected(device, FALSE);
204 device->scanning = FALSE;
206 err = device->driver->enable(device);
207 if (err < 0 && err != -EALREADY) {
208 if (err == -EINPROGRESS) {
209 device->powered_pending = TRUE;
210 device->offlinemode = FALSE;
211 if (__connman_profile_get_offlinemode() == TRUE)
212 __connman_profile_set_offlinemode(FALSE, FALSE);
217 device->powered_pending = TRUE;
218 device->powered = TRUE;
219 device->offlinemode = FALSE;
220 if (__connman_profile_get_offlinemode() == TRUE)
221 __connman_profile_set_offlinemode(FALSE, FALSE);
223 __connman_technology_enable_device(device);
228 int __connman_device_disable(struct connman_device *device)
232 DBG("device %p", device);
234 if (!device->driver || !device->driver->disable)
237 if (device->powered == FALSE)
240 if (device->powered_pending == FALSE)
243 device->reconnect = FALSE;
245 clear_scan_trigger(device);
247 g_hash_table_remove_all(device->networks);
249 err = device->driver->disable(device);
250 if (err < 0 && err != -EALREADY) {
251 if (err == -EINPROGRESS)
252 device->powered_pending = FALSE;
256 device->connections = 0;
258 device->powered_pending = FALSE;
259 device->powered = FALSE;
261 __connman_technology_disable_device(device);
266 static int set_powered(struct connman_device *device, connman_bool_t powered)
268 DBG("device %p powered %d", device, powered);
271 return __connman_device_enable(device);
273 return __connman_device_disable(device);
276 static int setup_device(struct connman_device *device)
278 DBG("device %p", device);
280 __connman_technology_add_device(device);
282 if (device->offlinemode == FALSE &&
283 device->powered_persistent == TRUE)
284 __connman_device_enable(device);
289 static void probe_driver(struct connman_element *element, gpointer user_data)
291 struct connman_device_driver *driver = user_data;
293 DBG("element %p name %s", element, element->name);
295 if (element->device == NULL)
298 if (element->device->driver != NULL)
301 if (driver->type != element->device->type)
304 if (driver->probe(element->device) < 0)
307 element->device->driver = driver;
309 __connman_element_set_driver(element);
311 setup_device(element->device);
314 static void remove_device(struct connman_device *device)
316 DBG("device %p", device);
318 __connman_device_disable(device);
320 __connman_technology_remove_device(device);
322 if (device->driver->remove)
323 device->driver->remove(device);
325 device->driver = NULL;
328 static void remove_driver(struct connman_element *element, gpointer user_data)
330 struct connman_device_driver *driver = user_data;
332 DBG("element %p name %s", element, element->name);
334 if (element->device == NULL)
337 if (element->device->driver == driver)
338 remove_device(element->device);
341 connman_bool_t __connman_device_has_driver(struct connman_device *device)
343 if (device == NULL || device->driver == NULL)
349 static GSList *driver_list = NULL;
351 static gint compare_priority(gconstpointer a, gconstpointer b)
353 const struct connman_device_driver *driver1 = a;
354 const struct connman_device_driver *driver2 = b;
356 return driver2->priority - driver1->priority;
360 * connman_device_driver_register:
361 * @driver: device driver definition
363 * Register a new device driver
365 * Returns: %0 on success
367 int connman_device_driver_register(struct connman_device_driver *driver)
369 DBG("driver %p name %s", driver, driver->name);
371 driver_list = g_slist_insert_sorted(driver_list, driver,
374 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
375 probe_driver, driver);
381 * connman_device_driver_unregister:
382 * @driver: device driver definition
384 * Remove a previously registered device driver
386 void connman_device_driver_unregister(struct connman_device_driver *driver)
388 DBG("driver %p name %s", driver, driver->name);
390 driver_list = g_slist_remove(driver_list, driver);
392 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
393 remove_driver, driver);
396 static void unregister_network(gpointer data)
398 struct connman_network *network = data;
400 DBG("network %p", network);
402 connman_element_unregister((struct connman_element *) network);
404 __connman_network_set_device(network, NULL);
406 connman_network_unref(network);
409 static void device_destruct(struct connman_element *element)
411 struct connman_device *device = element->device;
413 DBG("element %p name %s", element, element->name);
415 clear_scan_trigger(device);
417 g_free(device->ident);
418 g_free(device->node);
419 g_free(device->name);
420 g_free(device->address);
421 g_free(device->interface);
423 g_free(device->last_network);
425 g_hash_table_destroy(device->networks);
426 device->networks = NULL;
430 * connman_device_create:
431 * @node: device node name (for example an address)
434 * Allocate a new device of given #type and assign the #node name to it.
436 * Returns: a newly-allocated #connman_device structure
438 struct connman_device *connman_device_create(const char *node,
439 enum connman_device_type type)
441 struct connman_device *device;
443 enum connman_service_type service_type;
445 DBG("node %s type %d", node, type);
447 device = g_try_new0(struct connman_device, 1);
451 DBG("device %p", device);
453 __connman_element_initialize(&device->element);
455 device->element.name = g_strdup(node);
456 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
458 device->element.device = device;
459 device->element.destruct = device_destruct;
461 str = type2string(type);
463 connman_element_set_string(&device->element,
464 CONNMAN_PROPERTY_ID_TYPE, str);
466 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
469 device->name = g_strdup(type2description(device->type));
471 device->powered_persistent = TRUE;
473 device->phyindex = -1;
475 service_type = __connman_device_get_service_type(device);
476 device->blocked = __connman_technology_get_blocked(service_type);
479 case CONNMAN_DEVICE_TYPE_UNKNOWN:
480 case CONNMAN_DEVICE_TYPE_VENDOR:
481 device->scan_interval = 0;
483 case CONNMAN_DEVICE_TYPE_ETHERNET:
484 case CONNMAN_DEVICE_TYPE_WIFI:
485 device->scan_interval = 300;
487 case CONNMAN_DEVICE_TYPE_WIMAX:
488 device->scan_interval = 0;
490 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
491 device->scan_interval = 0;
493 case CONNMAN_DEVICE_TYPE_GPS:
494 device->scan_interval = 0;
496 case CONNMAN_DEVICE_TYPE_CELLULAR:
497 device->scan_interval = 0;
499 case CONNMAN_DEVICE_TYPE_GADGET:
500 device->scan_interval = 0;
504 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
505 g_free, unregister_network);
511 * connman_device_ref:
512 * @device: device structure
514 * Increase reference counter of device
516 struct connman_device *connman_device_ref(struct connman_device *device)
518 if (connman_element_ref(&device->element) == NULL)
525 * connman_device_unref:
526 * @device: device structure
528 * Decrease reference counter of device
530 void connman_device_unref(struct connman_device *device)
532 connman_element_unref(&device->element);
535 const char *__connman_device_get_type(struct connman_device *device)
537 return type2string(device->type);
541 * connman_device_get_type:
542 * @device: device structure
546 enum connman_device_type connman_device_get_type(struct connman_device *device)
552 * connman_device_set_index:
553 * @device: device structure
554 * @index: index number
556 * Set index number of device
558 void connman_device_set_index(struct connman_device *device, int index)
560 device->element.index = index;
564 * connman_device_get_index:
565 * @device: device structure
567 * Get index number of device
569 int connman_device_get_index(struct connman_device *device)
571 return device->element.index;
574 int __connman_device_get_phyindex(struct connman_device *device)
576 return device->phyindex;
579 void __connman_device_set_phyindex(struct connman_device *device,
582 device->phyindex = phyindex;
586 * connman_device_set_interface:
587 * @device: device structure
588 * @interface: interface name
590 * Set interface name of device
592 void connman_device_set_interface(struct connman_device *device,
593 const char *interface)
595 g_free(device->element.devname);
596 device->element.devname = g_strdup(interface);
598 g_free(device->interface);
599 device->interface = g_strdup(interface);
601 if (device->name == NULL) {
602 const char *str = type2description(device->type);
603 if (str != NULL && device->interface != NULL)
604 device->name = g_strdup_printf("%s (%s)", str,
610 * connman_device_set_ident:
611 * @device: device structure
612 * @ident: unique identifier
614 * Set unique identifier of device
616 void connman_device_set_ident(struct connman_device *device,
619 g_free(device->ident);
620 device->ident = g_strdup(ident);
623 const char *connman_device_get_ident(struct connman_device *device)
625 return device->ident;
629 * connman_device_set_powered:
630 * @device: device structure
631 * @powered: powered state
633 * Change power state of device
635 int connman_device_set_powered(struct connman_device *device,
636 connman_bool_t powered)
640 DBG("driver %p powered %d", device, powered);
642 if (device->powered == powered) {
643 device->powered_pending = powered;
648 err = __connman_device_enable(device);
650 err = __connman_device_disable(device);
652 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
655 device->powered = powered;
656 device->powered_pending = powered;
658 if (device->powered == TRUE)
659 __connman_technology_enable_device(device);
661 __connman_technology_disable_device(device);
663 if (device->offlinemode == TRUE && powered == TRUE)
664 return connman_device_set_powered(device, FALSE);
666 if (powered == FALSE)
669 reset_scan_trigger(device);
671 if (device->driver && device->driver->scan)
672 device->driver->scan(device);
677 int __connman_device_set_blocked(struct connman_device *device,
678 connman_bool_t blocked)
680 connman_bool_t powered;
682 DBG("device %p blocked %d", device, blocked);
684 device->blocked = blocked;
686 if (device->offlinemode == TRUE)
689 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
691 if (blocked == FALSE)
692 powered = device->powered_persistent;
696 return set_powered(device, powered);
699 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
701 return device->blocked;
704 int __connman_device_scan(struct connman_device *device)
706 if (!device->driver || !device->driver->scan)
709 if (device->powered == FALSE)
712 reset_scan_trigger(device);
714 return device->driver->scan(device);
717 int __connman_device_enable_persistent(struct connman_device *device)
721 DBG("device %p", device);
723 device->powered_persistent = TRUE;
725 __connman_storage_save_device(device);
727 err = __connman_device_enable(device);
728 if (err == 0 || err == -EINPROGRESS) {
729 device->offlinemode = FALSE;
730 if (__connman_profile_get_offlinemode() == TRUE) {
731 __connman_profile_set_offlinemode(FALSE, FALSE);
733 __connman_profile_save_default();
740 int __connman_device_disable_persistent(struct connman_device *device)
742 DBG("device %p", device);
744 device->powered_persistent = FALSE;
746 __connman_storage_save_device(device);
748 return __connman_device_disable(device);
751 int __connman_device_disconnect(struct connman_device *device)
756 DBG("device %p", device);
758 connman_device_set_disconnected(device, TRUE);
760 g_hash_table_iter_init(&iter, device->networks);
762 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
763 struct connman_network *network = value;
765 if (__connman_network_get_connecting(network) == TRUE) {
767 * Skip network in the process of connecting.
768 * This is a workaround for WiFi networks serviced
769 * by the supplicant plugin that hold a reference
770 * to the network. If we disconnect the network
771 * here then the referenced object will not be
772 * registered and usage (like launching DHCP client)
773 * will fail. There is nothing to be gained by
774 * removing the network here anyway.
776 connman_warn("Skipping disconnect of %s",
777 connman_network_get_identifier(network));
781 __connman_network_disconnect(network);
787 static void mark_network_unavailable(gpointer key, gpointer value,
790 struct connman_network *network = value;
792 if (connman_network_get_connected(network) == TRUE)
795 connman_network_set_available(network, FALSE);
798 static gboolean remove_unavailable_network(gpointer key, gpointer value,
801 struct connman_network *network = value;
803 if (connman_network_get_connected(network) == TRUE)
806 if (connman_network_get_available(network) == TRUE)
812 void __connman_device_cleanup_networks(struct connman_device *device)
814 g_hash_table_foreach_remove(device->networks,
815 remove_unavailable_network, NULL);
819 * connman_device_set_scanning:
820 * @device: device structure
821 * @scanning: scanning state
823 * Change scanning state of device
825 int connman_device_set_scanning(struct connman_device *device,
826 connman_bool_t scanning)
828 DBG("device %p scanning %d", device, scanning);
830 if (!device->driver || !device->driver->scan)
833 if (device->scanning == scanning)
836 device->scanning = scanning;
838 if (scanning == TRUE) {
839 reset_scan_trigger(device);
841 g_hash_table_foreach(device->networks,
842 mark_network_unavailable, NULL);
847 __connman_device_cleanup_networks(device);
849 if (device->connections > 0)
852 if (device->disconnected == TRUE)
855 __connman_service_auto_connect();
861 * connman_device_set_disconnected:
862 * @device: device structure
863 * @disconnected: disconnected state
865 * Change disconnected state of device (only for device with networks)
867 int connman_device_set_disconnected(struct connman_device *device,
868 connman_bool_t disconnected)
870 DBG("device %p disconnected %d", device, disconnected);
872 if (device->disconnected == disconnected)
875 device->disconnected = disconnected;
877 if (disconnected == TRUE)
878 force_scan_trigger(device);
884 * connman_device_get_disconnected:
885 * @device: device structure
887 * Get device disconnected state
889 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
891 return device->disconnected;
895 * connman_device_set_string:
896 * @device: device structure
897 * @key: unique identifier
898 * @value: string value
900 * Set string value for specific key
902 int connman_device_set_string(struct connman_device *device,
903 const char *key, const char *value)
905 DBG("device %p key %s value %s", device, key, value);
907 if (g_str_equal(key, "Address") == TRUE) {
908 g_free(device->address);
909 device->address = g_strdup(value);
910 } else if (g_str_equal(key, "Name") == TRUE) {
911 g_free(device->name);
912 device->name = g_strdup(value);
913 } else if (g_str_equal(key, "Node") == TRUE) {
914 g_free(device->node);
915 device->node = g_strdup(value);
918 return connman_element_set_string(&device->element, key, value);
922 * connman_device_get_string:
923 * @device: device structure
924 * @key: unique identifier
926 * Get string value for specific key
928 const char *connman_device_get_string(struct connman_device *device,
931 DBG("device %p key %s", device, key);
933 if (g_str_equal(key, "Address") == TRUE)
934 return device->address;
935 else if (g_str_equal(key, "Name") == TRUE)
937 else if (g_str_equal(key, "Node") == TRUE)
939 else if (g_str_equal(key, "Interface") == TRUE)
940 return device->interface;
942 return connman_element_get_string(&device->element, key);
945 static void set_offlinemode(struct connman_element *element, gpointer user_data)
947 struct connman_device *device = element->device;
948 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
949 connman_bool_t powered;
951 DBG("element %p name %s", element, element->name);
956 device->offlinemode = offlinemode;
958 if (device->blocked == TRUE)
961 powered = (offlinemode == TRUE) ? FALSE : TRUE;
963 if (device->powered == powered)
966 if (device->powered_persistent == FALSE)
969 set_powered(device, powered);
972 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
974 DBG("offlinmode %d", offlinemode);
976 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
977 set_offlinemode, GUINT_TO_POINTER(offlinemode));
979 __connman_notifier_offlinemode(offlinemode);
984 void __connman_device_increase_connections(struct connman_device *device)
989 device->connections++;
992 void __connman_device_decrease_connections(struct connman_device *device)
997 device->connections--;
1001 * connman_device_add_network:
1002 * @device: device structure
1003 * @network: network structure
1005 * Add new network to the device
1007 int connman_device_add_network(struct connman_device *device,
1008 struct connman_network *network)
1010 const char *identifier = connman_network_get_identifier(network);
1013 DBG("device %p network %p", device, network);
1015 if (identifier == NULL)
1018 __connman_network_set_device(network, device);
1020 err = connman_element_register((struct connman_element *) network,
1023 __connman_network_set_device(network, NULL);
1027 g_hash_table_insert(device->networks, g_strdup(identifier),
1034 * connman_device_get_network:
1035 * @device: device structure
1036 * @identifier: network identifier
1038 * Get network for given identifier
1040 struct connman_network *connman_device_get_network(struct connman_device *device,
1041 const char *identifier)
1043 DBG("device %p identifier %s", device, identifier);
1045 return g_hash_table_lookup(device->networks, identifier);
1049 * connman_device_remove_network:
1050 * @device: device structure
1051 * @identifier: network identifier
1053 * Remove network for given identifier
1055 int connman_device_remove_network(struct connman_device *device,
1056 const char *identifier)
1058 DBG("device %p identifier %s", device, identifier);
1060 g_hash_table_remove(device->networks, identifier);
1065 void connman_device_remove_all_networks(struct connman_device *device)
1067 g_hash_table_remove_all(device->networks);
1070 void __connman_device_set_network(struct connman_device *device,
1071 struct connman_network *network)
1078 if (device->network == network)
1081 if (device->network != NULL)
1082 connman_network_unref(device->network);
1084 if (network != NULL) {
1085 name = connman_network_get_string(network,
1086 CONNMAN_PROPERTY_ID_NAME);
1087 g_free(device->last_network);
1088 device->last_network = g_strdup(name);
1090 device->network = connman_network_ref(network);
1092 g_free(device->last_network);
1093 device->last_network = NULL;
1095 device->network = NULL;
1099 void __connman_device_set_reconnect(struct connman_device *device,
1100 connman_bool_t reconnect)
1102 device->reconnect = reconnect;
1105 connman_bool_t __connman_device_get_reconnect(
1106 struct connman_device *device)
1108 return device->reconnect;
1112 * connman_device_register:
1113 * @device: device structure
1115 * Register device with the system
1117 int connman_device_register(struct connman_device *device)
1119 __connman_storage_load_device(device);
1121 device->offlinemode = __connman_profile_get_offlinemode();
1123 return connman_element_register(&device->element, NULL);
1127 * connman_device_unregister:
1128 * @device: device structure
1130 * Unregister device with the system
1132 void connman_device_unregister(struct connman_device *device)
1134 __connman_storage_save_device(device);
1136 connman_element_unregister(&device->element);
1140 * connman_device_get_data:
1141 * @device: device structure
1143 * Get private device data pointer
1145 void *connman_device_get_data(struct connman_device *device)
1147 return device->driver_data;
1151 * connman_device_set_data:
1152 * @device: device structure
1153 * @data: data pointer
1155 * Set private device data pointer
1157 void connman_device_set_data(struct connman_device *device, void *data)
1159 device->driver_data = data;
1162 static gboolean match_driver(struct connman_device *device,
1163 struct connman_device_driver *driver)
1165 if (device->type == driver->type ||
1166 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1172 static int device_probe(struct connman_element *element)
1174 struct connman_device *device = element->device;
1177 DBG("element %p name %s", element, element->name);
1182 if (device->driver != NULL)
1185 for (list = driver_list; list; list = list->next) {
1186 struct connman_device_driver *driver = list->data;
1188 if (match_driver(device, driver) == FALSE)
1191 DBG("driver %p name %s", driver, driver->name);
1193 if (driver->probe(device) == 0) {
1194 device->driver = driver;
1199 if (device->driver == NULL)
1202 return setup_device(device);
1205 static void device_remove(struct connman_element *element)
1207 struct connman_device *device = element->device;
1209 DBG("element %p name %s", element, element->name);
1214 if (device->driver == NULL)
1217 remove_device(device);
1220 static struct connman_driver device_driver = {
1222 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1223 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1224 .probe = device_probe,
1225 .remove = device_remove,
1228 static int device_load(struct connman_device *device)
1230 const char *ident = __connman_profile_active_ident();
1232 GError *error = NULL;
1234 connman_bool_t powered;
1237 DBG("device %p", device);
1239 keyfile = __connman_storage_open_profile(ident);
1240 if (keyfile == NULL)
1243 identifier = g_strdup_printf("device_%s", device->element.name);
1244 if (identifier == NULL)
1247 powered = g_key_file_get_boolean(keyfile, identifier,
1250 device->powered_persistent = powered;
1251 g_clear_error(&error);
1253 val = g_key_file_get_integer(keyfile, identifier,
1254 "ScanInterval", &error);
1256 device->scan_interval = val;
1257 g_clear_error(&error);
1262 __connman_storage_close_profile(ident, keyfile, FALSE);
1267 static int device_save(struct connman_device *device)
1269 const char *ident = __connman_profile_active_ident();
1273 DBG("device %p", device);
1275 keyfile = __connman_storage_open_profile(ident);
1276 if (keyfile == NULL)
1279 identifier = g_strdup_printf("device_%s", device->element.name);
1280 if (identifier == NULL)
1283 g_key_file_set_boolean(keyfile, identifier,
1284 "Powered", device->powered_persistent);
1286 g_key_file_set_integer(keyfile, identifier,
1287 "ScanInterval", device->scan_interval);
1292 __connman_storage_close_profile(ident, keyfile, TRUE);
1297 static struct connman_storage device_storage = {
1299 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1300 .device_load = device_load,
1301 .device_save = device_save,
1304 int __connman_device_init(void)
1308 if (connman_storage_register(&device_storage) < 0)
1309 connman_error("Failed to register device storage");
1311 return connman_driver_register(&device_driver);
1314 void __connman_device_cleanup(void)
1318 connman_driver_unregister(&device_driver);
1320 connman_storage_unregister(&device_storage);