5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 connman_bool_t offlinemode;
39 connman_bool_t blocked;
40 connman_bool_t powered;
41 connman_bool_t powered_pending;
42 connman_bool_t powered_persistent;
43 connman_bool_t scanning;
44 connman_bool_t disconnected;
45 connman_bool_t reconnect;
46 connman_uint16_t scan_interval;
54 unsigned int connections;
57 struct connman_device_driver *driver;
61 struct connman_network *network;
65 static gboolean device_scan_trigger(gpointer user_data)
67 struct connman_device *device = user_data;
69 DBG("device %p", device);
71 if (device->driver == NULL) {
72 device->scan_timeout = 0;
76 if (device->driver->scan)
77 device->driver->scan(device);
82 static void clear_scan_trigger(struct connman_device *device)
84 if (device->scan_timeout > 0) {
85 g_source_remove(device->scan_timeout);
86 device->scan_timeout = 0;
90 static void reset_scan_trigger(struct connman_device *device)
92 clear_scan_trigger(device);
94 if (device->scan_interval > 0) {
95 guint interval = device->scan_interval;
96 device->scan_timeout = g_timeout_add_seconds(interval,
97 device_scan_trigger, device);
101 static void force_scan_trigger(struct connman_device *device)
103 clear_scan_trigger(device);
105 device->scan_timeout = g_timeout_add_seconds(5,
106 device_scan_trigger, device);
109 void connman_device_schedule_scan(struct connman_device *device)
111 reset_scan_trigger(device);
114 static const char *type2description(enum connman_device_type type)
117 case CONNMAN_DEVICE_TYPE_UNKNOWN:
118 case CONNMAN_DEVICE_TYPE_VENDOR:
120 case CONNMAN_DEVICE_TYPE_ETHERNET:
122 case CONNMAN_DEVICE_TYPE_WIFI:
124 case CONNMAN_DEVICE_TYPE_WIMAX:
126 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
128 case CONNMAN_DEVICE_TYPE_GPS:
130 case CONNMAN_DEVICE_TYPE_CELLULAR:
137 static const char *type2string(enum connman_device_type type)
140 case CONNMAN_DEVICE_TYPE_UNKNOWN:
141 case CONNMAN_DEVICE_TYPE_VENDOR:
143 case CONNMAN_DEVICE_TYPE_ETHERNET:
145 case CONNMAN_DEVICE_TYPE_WIFI:
147 case CONNMAN_DEVICE_TYPE_WIMAX:
149 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
151 case CONNMAN_DEVICE_TYPE_GPS:
153 case CONNMAN_DEVICE_TYPE_CELLULAR:
160 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
162 enum connman_device_type type = connman_device_get_type(device);
165 case CONNMAN_DEVICE_TYPE_UNKNOWN:
166 case CONNMAN_DEVICE_TYPE_VENDOR:
167 case CONNMAN_DEVICE_TYPE_GPS:
169 case CONNMAN_DEVICE_TYPE_ETHERNET:
170 return CONNMAN_SERVICE_TYPE_ETHERNET;
171 case CONNMAN_DEVICE_TYPE_WIFI:
172 return CONNMAN_SERVICE_TYPE_WIFI;
173 case CONNMAN_DEVICE_TYPE_WIMAX:
174 return CONNMAN_SERVICE_TYPE_WIMAX;
175 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
176 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
177 case CONNMAN_DEVICE_TYPE_CELLULAR:
178 return CONNMAN_SERVICE_TYPE_CELLULAR;
181 return CONNMAN_SERVICE_TYPE_UNKNOWN;
184 int __connman_device_enable(struct connman_device *device)
188 DBG("device %p %d", device, device->blocked);
190 if (!device->driver || !device->driver->enable)
193 if (device->powered_pending == TRUE)
196 if (device->blocked == TRUE)
199 err = device->driver->enable(device);
201 if (err == -EINPROGRESS) {
202 device->powered_pending = TRUE;
203 device->offlinemode = FALSE;
204 if (__connman_profile_get_offlinemode() == TRUE)
205 __connman_profile_set_offlinemode(FALSE, FALSE);
210 device->powered_pending = TRUE;
211 device->powered = TRUE;
212 device->offlinemode = FALSE;
213 if (__connman_profile_get_offlinemode() == TRUE)
214 __connman_profile_set_offlinemode(FALSE, FALSE);
216 __connman_technology_enable_device(device);
221 int __connman_device_disable(struct connman_device *device)
225 DBG("device %p", device);
227 if (!device->driver || !device->driver->disable)
230 if (device->powered == FALSE)
233 if (device->powered_pending == FALSE)
236 device->reconnect = FALSE;
238 clear_scan_trigger(device);
240 g_hash_table_remove_all(device->networks);
242 err = device->driver->disable(device);
244 if (err == -EINPROGRESS)
245 device->powered_pending = FALSE;
249 device->powered_pending = FALSE;
250 device->powered = FALSE;
252 __connman_technology_disable_device(device);
257 static int set_powered(struct connman_device *device, connman_bool_t powered)
259 DBG("device %p powered %d", device, powered);
262 return __connman_device_enable(device);
264 return __connman_device_disable(device);
267 static int setup_device(struct connman_device *device)
269 DBG("device %p", device);
271 __connman_technology_add_device(device);
273 if (device->offlinemode == FALSE &&
274 device->powered_persistent == TRUE)
275 __connman_device_enable(device);
280 static void probe_driver(struct connman_element *element, gpointer user_data)
282 struct connman_device_driver *driver = user_data;
284 DBG("element %p name %s", element, element->name);
286 if (element->device == NULL)
289 if (element->device->driver != NULL)
292 if (driver->type != element->device->type)
295 if (driver->probe(element->device) < 0)
298 element->device->driver = driver;
300 __connman_element_set_driver(element);
302 setup_device(element->device);
305 static void remove_device(struct connman_device *device)
309 DBG("device %p", device);
311 err = __connman_device_disable(device);
312 if (err < 0 && err == -EINPROGRESS)
313 __connman_technology_disable_device(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->control);
417 g_free(device->interface);
419 g_free(device->last_network);
421 g_hash_table_destroy(device->networks);
422 device->networks = NULL;
426 * connman_device_create:
427 * @node: device node name (for example an address)
430 * Allocate a new device of given #type and assign the #node name to it.
432 * Returns: a newly-allocated #connman_device structure
434 struct connman_device *connman_device_create(const char *node,
435 enum connman_device_type type)
437 struct connman_device *device;
439 enum connman_service_type service_type;
441 DBG("node %s type %d", node, type);
443 device = g_try_new0(struct connman_device, 1);
447 DBG("device %p", device);
449 __connman_element_initialize(&device->element);
451 device->element.name = g_strdup(node);
452 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
454 device->element.device = device;
455 device->element.destruct = device_destruct;
457 str = type2string(type);
459 connman_element_set_string(&device->element,
460 CONNMAN_PROPERTY_ID_TYPE, str);
462 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
465 device->name = g_strdup(type2description(device->type));
467 device->powered_persistent = TRUE;
469 device->phyindex = -1;
471 service_type = __connman_device_get_service_type(device);
472 device->blocked = __connman_technology_get_blocked(service_type);
475 case CONNMAN_DEVICE_TYPE_UNKNOWN:
476 case CONNMAN_DEVICE_TYPE_VENDOR:
477 device->scan_interval = 0;
479 case CONNMAN_DEVICE_TYPE_ETHERNET:
480 case CONNMAN_DEVICE_TYPE_WIFI:
481 device->scan_interval = 300;
483 case CONNMAN_DEVICE_TYPE_WIMAX:
484 device->scan_interval = 0;
486 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
487 device->scan_interval = 0;
489 case CONNMAN_DEVICE_TYPE_GPS:
490 device->scan_interval = 0;
492 case CONNMAN_DEVICE_TYPE_CELLULAR:
493 device->scan_interval = 0;
497 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
498 g_free, unregister_network);
504 * connman_device_ref:
505 * @device: device structure
507 * Increase reference counter of device
509 struct connman_device *connman_device_ref(struct connman_device *device)
511 if (connman_element_ref(&device->element) == NULL)
518 * connman_device_unref:
519 * @device: device structure
521 * Decrease reference counter of device
523 void connman_device_unref(struct connman_device *device)
525 connman_element_unref(&device->element);
528 const char *__connman_device_get_type(struct connman_device *device)
530 return type2string(device->type);
534 * connman_device_get_type:
535 * @device: device structure
539 enum connman_device_type connman_device_get_type(struct connman_device *device)
545 * connman_device_get_name:
546 * @device: device structure
548 * Get unique name of device
550 const char *connman_device_get_name(struct connman_device *device)
552 return device->element.name;
556 * connman_device_get_path:
557 * @device: device structure
559 * Get path name of device
561 const char *connman_device_get_path(struct connman_device *device)
563 return device->element.path;
567 * connman_device_set_index:
568 * @device: device structure
569 * @index: index number
571 * Set index number of device
573 void connman_device_set_index(struct connman_device *device, int index)
575 device->element.index = index;
579 * connman_device_get_index:
580 * @device: device structure
582 * Get index number of device
584 int connman_device_get_index(struct connman_device *device)
586 return device->element.index;
589 int __connman_device_get_phyindex(struct connman_device *device)
591 return device->phyindex;
594 void __connman_device_set_phyindex(struct connman_device *device,
597 device->phyindex = phyindex;
601 * connman_device_set_interface:
602 * @device: device structure
603 * @interface: interface name
604 * @control: control interface
606 * Set interface name of device
608 void connman_device_set_interface(struct connman_device *device,
609 const char *interface, const char *control)
611 g_free(device->element.devname);
612 device->element.devname = g_strdup(interface);
614 g_free(device->interface);
615 device->interface = g_strdup(interface);
617 g_free(device->control);
618 device->control = g_strdup(control);
620 if (device->name == NULL) {
621 const char *str = type2description(device->type);
622 if (str != NULL && device->interface != NULL)
623 device->name = g_strdup_printf("%s (%s)", str,
628 const char *connman_device_get_control(struct connman_device *device)
630 return device->control;
634 * connman_device_set_ident:
635 * @device: device structure
636 * @ident: unique identifier
638 * Set unique identifier of device
640 void connman_device_set_ident(struct connman_device *device,
643 g_free(device->ident);
644 device->ident = g_strdup(ident);
647 const char *connman_device_get_ident(struct connman_device *device)
649 return device->ident;
653 * connman_device_set_powered:
654 * @device: device structure
655 * @powered: powered state
657 * Change power state of device
659 int connman_device_set_powered(struct connman_device *device,
660 connman_bool_t powered)
662 DBG("driver %p powered %d", device, powered);
664 if (device->powered == powered) {
665 device->powered_pending = powered;
670 __connman_device_enable(device);
672 __connman_device_disable(device);
674 device->powered = powered;
675 device->powered_pending = powered;
677 if (device->powered == TRUE)
678 __connman_technology_enable_device(device);
680 __connman_technology_disable_device(device);
682 if (device->offlinemode == TRUE && powered == TRUE)
683 return connman_device_set_powered(device, FALSE);
685 if (powered == FALSE)
688 reset_scan_trigger(device);
690 if (device->driver->scan)
691 device->driver->scan(device);
696 int __connman_device_set_blocked(struct connman_device *device,
697 connman_bool_t blocked)
699 connman_bool_t powered;
701 DBG("device %p blocked %d", device, blocked);
703 device->blocked = blocked;
705 if (device->offlinemode == TRUE)
708 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
710 if (blocked == FALSE)
711 powered = device->powered_persistent;
715 return set_powered(device, powered);
718 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
720 return device->blocked;
723 int __connman_device_scan(struct connman_device *device)
725 if (!device->driver || !device->driver->scan)
728 if (device->powered == FALSE)
731 reset_scan_trigger(device);
733 return device->driver->scan(device);
736 int __connman_device_enable_persistent(struct connman_device *device)
740 DBG("device %p", device);
742 device->powered_persistent = TRUE;
744 __connman_storage_save_device(device);
746 err = __connman_device_enable(device);
747 if (err == 0 || err == -EINPROGRESS) {
748 device->offlinemode = FALSE;
749 if (__connman_profile_get_offlinemode() == TRUE) {
750 __connman_profile_set_offlinemode(FALSE, FALSE);
752 __connman_profile_save_default();
759 int __connman_device_disable_persistent(struct connman_device *device)
761 DBG("device %p", device);
763 device->powered_persistent = FALSE;
765 __connman_storage_save_device(device);
767 return __connman_device_disable(device);
770 int __connman_device_disconnect(struct connman_device *device)
775 DBG("device %p", device);
777 connman_device_set_disconnected(device, TRUE);
779 g_hash_table_iter_init(&iter, device->networks);
781 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
782 struct connman_network *network = value;
784 if (__connman_network_get_connecting(network) == TRUE) {
786 * Skip network in the process of connecting.
787 * This is a workaround for WiFi networks serviced
788 * by the supplicant plugin that hold a reference
789 * to the network. If we disconnect the network
790 * here then the referenced object will not be
791 * registered and usage (like launching DHCP client)
792 * will fail. There is nothing to be gained by
793 * removing the network here anyway.
795 connman_warn("Skipping disconnect of %s",
796 connman_network_get_identifier(network));
800 __connman_network_disconnect(network);
806 static void mark_network_unavailable(gpointer key, gpointer value,
809 struct connman_network *network = value;
811 if (connman_network_get_connected(network) == TRUE)
814 connman_network_set_available(network, FALSE);
817 static gboolean remove_unavailable_network(gpointer key, gpointer value,
820 struct connman_network *network = value;
822 if (connman_network_get_connected(network) == TRUE)
825 if (connman_network_get_available(network) == TRUE)
831 void __connman_device_cleanup_networks(struct connman_device *device)
833 g_hash_table_foreach_remove(device->networks,
834 remove_unavailable_network, NULL);
838 * connman_device_set_scanning:
839 * @device: device structure
840 * @scanning: scanning state
842 * Change scanning state of device
844 int connman_device_set_scanning(struct connman_device *device,
845 connman_bool_t scanning)
847 DBG("device %p scanning %d", device, scanning);
849 if (!device->driver || !device->driver->scan)
852 if (device->scanning == scanning)
855 device->scanning = scanning;
857 if (scanning == TRUE) {
858 reset_scan_trigger(device);
860 g_hash_table_foreach(device->networks,
861 mark_network_unavailable, NULL);
866 __connman_device_cleanup_networks(device);
868 if (device->connections > 0)
871 if (device->disconnected == TRUE)
874 __connman_service_auto_connect();
880 * connman_device_set_disconnected:
881 * @device: device structure
882 * @disconnected: disconnected state
884 * Change disconnected state of device (only for device with networks)
886 int connman_device_set_disconnected(struct connman_device *device,
887 connman_bool_t disconnected)
889 DBG("device %p disconnected %d", device, disconnected);
891 if (device->disconnected == disconnected)
894 device->disconnected = disconnected;
896 if (disconnected == TRUE)
897 force_scan_trigger(device);
903 * connman_device_get_disconnected:
904 * @device: device structure
906 * Get device disconnected state
908 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
910 return device->disconnected;
914 * connman_device_set_string:
915 * @device: device structure
916 * @key: unique identifier
917 * @value: string value
919 * Set string value for specific key
921 int connman_device_set_string(struct connman_device *device,
922 const char *key, const char *value)
924 DBG("device %p key %s value %s", device, key, value);
926 if (g_str_equal(key, "Address") == TRUE) {
927 g_free(device->address);
928 device->address = g_strdup(value);
929 } else if (g_str_equal(key, "Name") == TRUE) {
930 g_free(device->name);
931 device->name = g_strdup(value);
932 } else if (g_str_equal(key, "Node") == TRUE) {
933 g_free(device->node);
934 device->node = g_strdup(value);
937 return connman_element_set_string(&device->element, key, value);
941 * connman_device_get_string:
942 * @device: device structure
943 * @key: unique identifier
945 * Get string value for specific key
947 const char *connman_device_get_string(struct connman_device *device,
950 DBG("device %p key %s", device, key);
952 if (g_str_equal(key, "Address") == TRUE)
953 return device->address;
954 else if (g_str_equal(key, "Name") == TRUE)
956 else if (g_str_equal(key, "Node") == TRUE)
958 else if (g_str_equal(key, "Interface") == TRUE)
959 return device->interface;
961 return connman_element_get_string(&device->element, key);
964 static void set_offlinemode(struct connman_element *element, gpointer user_data)
966 struct connman_device *device = element->device;
967 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
968 connman_bool_t powered;
970 DBG("element %p name %s", element, element->name);
975 device->offlinemode = offlinemode;
977 if (device->blocked == TRUE)
980 powered = (offlinemode == TRUE) ? FALSE : TRUE;
982 if (device->powered == powered)
985 if (device->powered_persistent == FALSE)
988 set_powered(device, powered);
991 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
993 DBG("offlinmode %d", offlinemode);
995 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
996 set_offlinemode, GUINT_TO_POINTER(offlinemode));
998 __connman_notifier_offlinemode(offlinemode);
1003 void __connman_device_increase_connections(struct connman_device *device)
1008 device->connections++;
1011 void __connman_device_decrease_connections(struct connman_device *device)
1016 device->connections--;
1020 * connman_device_add_network:
1021 * @device: device structure
1022 * @network: network structure
1024 * Add new network to the device
1026 int connman_device_add_network(struct connman_device *device,
1027 struct connman_network *network)
1029 const char *identifier = connman_network_get_identifier(network);
1032 DBG("device %p network %p", device, network);
1034 if (identifier == NULL)
1037 __connman_network_set_device(network, device);
1039 err = connman_element_register((struct connman_element *) network,
1042 __connman_network_set_device(network, NULL);
1046 g_hash_table_insert(device->networks, g_strdup(identifier),
1053 * connman_device_get_network:
1054 * @device: device structure
1055 * @identifier: network identifier
1057 * Get network for given identifier
1059 struct connman_network *connman_device_get_network(struct connman_device *device,
1060 const char *identifier)
1062 DBG("device %p identifier %s", device, identifier);
1064 return g_hash_table_lookup(device->networks, identifier);
1068 * connman_device_remove_network:
1069 * @device: device structure
1070 * @identifier: network identifier
1072 * Remove network for given identifier
1074 int connman_device_remove_network(struct connman_device *device,
1075 const char *identifier)
1077 DBG("device %p identifier %s", device, identifier);
1079 g_hash_table_remove(device->networks, identifier);
1084 void connman_device_remove_all_networks(struct connman_device *device)
1086 g_hash_table_remove_all(device->networks);
1089 void __connman_device_set_network(struct connman_device *device,
1090 struct connman_network *network)
1097 if (device->network == network)
1100 if (device->network != NULL)
1101 connman_network_unref(device->network);
1103 if (network != NULL) {
1104 name = connman_network_get_string(network,
1105 CONNMAN_PROPERTY_ID_NAME);
1106 g_free(device->last_network);
1107 device->last_network = g_strdup(name);
1109 device->network = connman_network_ref(network);
1111 g_free(device->last_network);
1112 device->last_network = NULL;
1114 device->network = NULL;
1118 void __connman_device_set_reconnect(struct connman_device *device,
1119 connman_bool_t reconnect)
1121 device->reconnect = reconnect;
1124 connman_bool_t __connman_device_get_reconnect(
1125 struct connman_device *device)
1127 return device->reconnect;
1131 * connman_device_register:
1132 * @device: device structure
1134 * Register device with the system
1136 int connman_device_register(struct connman_device *device)
1138 __connman_storage_load_device(device);
1140 device->offlinemode = __connman_profile_get_offlinemode();
1142 return connman_element_register(&device->element, NULL);
1146 * connman_device_unregister:
1147 * @device: device structure
1149 * Unregister device with the system
1151 void connman_device_unregister(struct connman_device *device)
1153 __connman_storage_save_device(device);
1155 connman_element_unregister(&device->element);
1159 * connman_device_get_data:
1160 * @device: device structure
1162 * Get private device data pointer
1164 void *connman_device_get_data(struct connman_device *device)
1166 return device->driver_data;
1170 * connman_device_set_data:
1171 * @device: device structure
1172 * @data: data pointer
1174 * Set private device data pointer
1176 void connman_device_set_data(struct connman_device *device, void *data)
1178 device->driver_data = data;
1181 static gboolean match_driver(struct connman_device *device,
1182 struct connman_device_driver *driver)
1184 if (device->type == driver->type ||
1185 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1191 static int device_probe(struct connman_element *element)
1193 struct connman_device *device = element->device;
1196 DBG("element %p name %s", element, element->name);
1201 if (device->driver != NULL)
1204 for (list = driver_list; list; list = list->next) {
1205 struct connman_device_driver *driver = list->data;
1207 if (match_driver(device, driver) == FALSE)
1210 DBG("driver %p name %s", driver, driver->name);
1212 if (driver->probe(device) == 0) {
1213 device->driver = driver;
1218 if (device->driver == NULL)
1221 return setup_device(device);
1224 static void device_remove(struct connman_element *element)
1226 struct connman_device *device = element->device;
1228 DBG("element %p name %s", element, element->name);
1233 if (device->driver == NULL)
1236 remove_device(device);
1239 static struct connman_driver device_driver = {
1241 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1242 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1243 .probe = device_probe,
1244 .remove = device_remove,
1247 static int device_load(struct connman_device *device)
1249 const char *ident = __connman_profile_active_ident();
1251 GError *error = NULL;
1253 connman_bool_t powered;
1256 DBG("device %p", device);
1258 keyfile = __connman_storage_open_profile(ident);
1259 if (keyfile == NULL)
1262 identifier = g_strdup_printf("device_%s", device->element.name);
1263 if (identifier == NULL)
1266 powered = g_key_file_get_boolean(keyfile, identifier,
1269 device->powered_persistent = powered;
1270 g_clear_error(&error);
1272 val = g_key_file_get_integer(keyfile, identifier,
1273 "ScanInterval", &error);
1275 device->scan_interval = val;
1276 g_clear_error(&error);
1281 __connman_storage_close_profile(ident, keyfile, FALSE);
1286 static int device_save(struct connman_device *device)
1288 const char *ident = __connman_profile_active_ident();
1292 DBG("device %p", device);
1294 keyfile = __connman_storage_open_profile(ident);
1295 if (keyfile == NULL)
1298 identifier = g_strdup_printf("device_%s", device->element.name);
1299 if (identifier == NULL)
1302 g_key_file_set_boolean(keyfile, identifier,
1303 "Powered", device->powered_persistent);
1305 g_key_file_set_integer(keyfile, identifier,
1306 "ScanInterval", device->scan_interval);
1311 __connman_storage_close_profile(ident, keyfile, TRUE);
1316 static struct connman_storage device_storage = {
1318 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1319 .device_load = device_load,
1320 .device_save = device_save,
1323 int __connman_device_init(void)
1327 connection = connman_dbus_get_connection();
1329 if (connman_storage_register(&device_storage) < 0)
1330 connman_error("Failed to register device storage");
1332 return connman_driver_register(&device_driver);
1335 void __connman_device_cleanup(void)
1339 connman_driver_unregister(&device_driver);
1341 connman_storage_unregister(&device_storage);
1343 dbus_connection_unref(connection);