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;
43 connman_uint16_t backoff_interval;
50 unsigned int connections;
53 struct connman_device_driver *driver;
57 struct connman_network *network;
61 #define SCAN_INITIAL_DELAY 10
63 static gboolean device_scan_trigger(gpointer user_data)
65 struct connman_device *device = user_data;
67 DBG("device %p", device);
69 if (device->driver == NULL) {
70 device->scan_timeout = 0;
74 if (device->driver->scan)
75 device->driver->scan(device);
80 static void clear_scan_trigger(struct connman_device *device)
82 if (device->scan_timeout > 0) {
83 g_source_remove(device->scan_timeout);
84 device->scan_timeout = 0;
88 static void reset_scan_trigger(struct connman_device *device)
90 clear_scan_trigger(device);
92 if (device->scan_interval > 0) {
95 if (g_hash_table_size(device->networks) == 0) {
96 if (device->backoff_interval >= device->scan_interval)
97 device->backoff_interval = SCAN_INITIAL_DELAY;
98 interval = device->backoff_interval;
100 interval = device->scan_interval;
102 DBG("interval %d", interval);
104 device->scan_timeout = g_timeout_add_seconds(interval,
105 device_scan_trigger, device);
107 device->backoff_interval *= 2;
108 if (device->backoff_interval > device->scan_interval)
109 device->backoff_interval = device->scan_interval;
113 static void force_scan_trigger(struct connman_device *device)
115 clear_scan_trigger(device);
117 device->scan_timeout = g_timeout_add_seconds(5,
118 device_scan_trigger, device);
121 void connman_device_schedule_scan(struct connman_device *device)
123 reset_scan_trigger(device);
126 static const char *type2description(enum connman_device_type type)
129 case CONNMAN_DEVICE_TYPE_UNKNOWN:
130 case CONNMAN_DEVICE_TYPE_VENDOR:
132 case CONNMAN_DEVICE_TYPE_ETHERNET:
134 case CONNMAN_DEVICE_TYPE_WIFI:
136 case CONNMAN_DEVICE_TYPE_WIMAX:
138 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
140 case CONNMAN_DEVICE_TYPE_GPS:
142 case CONNMAN_DEVICE_TYPE_CELLULAR:
144 case CONNMAN_DEVICE_TYPE_GADGET:
152 static const char *type2string(enum connman_device_type type)
155 case CONNMAN_DEVICE_TYPE_UNKNOWN:
156 case CONNMAN_DEVICE_TYPE_VENDOR:
158 case CONNMAN_DEVICE_TYPE_ETHERNET:
160 case CONNMAN_DEVICE_TYPE_WIFI:
162 case CONNMAN_DEVICE_TYPE_WIMAX:
164 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
166 case CONNMAN_DEVICE_TYPE_GPS:
168 case CONNMAN_DEVICE_TYPE_CELLULAR:
170 case CONNMAN_DEVICE_TYPE_GADGET:
178 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
180 enum connman_device_type type = connman_device_get_type(device);
183 case CONNMAN_DEVICE_TYPE_UNKNOWN:
184 case CONNMAN_DEVICE_TYPE_VENDOR:
185 case CONNMAN_DEVICE_TYPE_GPS:
187 case CONNMAN_DEVICE_TYPE_ETHERNET:
188 return CONNMAN_SERVICE_TYPE_ETHERNET;
189 case CONNMAN_DEVICE_TYPE_WIFI:
190 return CONNMAN_SERVICE_TYPE_WIFI;
191 case CONNMAN_DEVICE_TYPE_WIMAX:
192 return CONNMAN_SERVICE_TYPE_WIMAX;
193 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
194 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
195 case CONNMAN_DEVICE_TYPE_CELLULAR:
196 return CONNMAN_SERVICE_TYPE_CELLULAR;
197 case CONNMAN_DEVICE_TYPE_GADGET:
198 return CONNMAN_SERVICE_TYPE_GADGET;
202 return CONNMAN_SERVICE_TYPE_UNKNOWN;
205 int __connman_device_enable(struct connman_device *device)
208 enum connman_service_type type;
210 DBG("device %p %d", device, device->blocked);
212 if (!device->driver || !device->driver->enable)
215 if (device->powered_pending == TRUE)
218 if (device->blocked == TRUE)
221 connman_device_set_disconnected(device, FALSE);
222 device->scanning = FALSE;
224 err = device->driver->enable(device);
225 if (err < 0 && err != -EALREADY) {
226 if (err == -EINPROGRESS) {
227 device->powered_pending = TRUE;
228 device->offlinemode = FALSE;
229 if (__connman_profile_get_offlinemode() == TRUE)
230 __connman_profile_set_offlinemode(FALSE, FALSE);
235 device->powered_pending = TRUE;
236 device->powered = TRUE;
237 device->offlinemode = FALSE;
238 if (__connman_profile_get_offlinemode() == TRUE)
239 __connman_profile_set_offlinemode(FALSE, FALSE);
241 type = __connman_device_get_service_type(device);
242 __connman_technology_enable(type);
247 int __connman_device_disable(struct connman_device *device)
250 enum connman_service_type type;
252 DBG("device %p", device);
254 if (!device->driver || !device->driver->disable)
257 if (device->powered == FALSE)
260 if (device->powered_pending == FALSE)
263 device->reconnect = FALSE;
265 clear_scan_trigger(device);
267 g_hash_table_remove_all(device->networks);
269 err = device->driver->disable(device);
270 if (err < 0 && err != -EALREADY) {
271 if (err == -EINPROGRESS)
272 device->powered_pending = FALSE;
276 device->connections = 0;
278 device->powered_pending = FALSE;
279 device->powered = FALSE;
281 type = __connman_device_get_service_type(device);
282 __connman_technology_disable(type);
287 static int set_powered(struct connman_device *device, connman_bool_t powered)
289 DBG("device %p powered %d", device, powered);
292 return __connman_device_enable(device);
294 return __connman_device_disable(device);
297 static int setup_device(struct connman_device *device)
299 DBG("device %p", device);
301 __connman_technology_add_device(device);
303 if (device->offlinemode == FALSE &&
304 device->powered_persistent == TRUE)
305 __connman_device_enable(device);
310 static void probe_driver(struct connman_element *element, gpointer user_data)
312 struct connman_device_driver *driver = user_data;
314 DBG("element %p name %s", element, element->name);
316 if (element->device == NULL)
319 if (element->device->driver != NULL)
322 if (driver->type != element->device->type)
325 if (driver->probe(element->device) < 0)
328 element->device->driver = driver;
330 __connman_element_set_driver(element);
332 setup_device(element->device);
335 static void remove_device(struct connman_device *device)
337 DBG("device %p", device);
339 __connman_device_disable(device);
341 __connman_technology_remove_device(device);
343 if (device->driver->remove)
344 device->driver->remove(device);
346 device->driver = NULL;
349 static void remove_driver(struct connman_element *element, gpointer user_data)
351 struct connman_device_driver *driver = user_data;
353 DBG("element %p name %s", element, element->name);
355 if (element->device == NULL)
358 if (element->device->driver == driver)
359 remove_device(element->device);
362 connman_bool_t __connman_device_has_driver(struct connman_device *device)
364 if (device == NULL || device->driver == NULL)
370 static GSList *driver_list = NULL;
372 static gint compare_priority(gconstpointer a, gconstpointer b)
374 const struct connman_device_driver *driver1 = a;
375 const struct connman_device_driver *driver2 = b;
377 return driver2->priority - driver1->priority;
381 * connman_device_driver_register:
382 * @driver: device driver definition
384 * Register a new device driver
386 * Returns: %0 on success
388 int connman_device_driver_register(struct connman_device_driver *driver)
390 DBG("driver %p name %s", driver, driver->name);
392 driver_list = g_slist_insert_sorted(driver_list, driver,
395 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
396 probe_driver, driver);
402 * connman_device_driver_unregister:
403 * @driver: device driver definition
405 * Remove a previously registered device driver
407 void connman_device_driver_unregister(struct connman_device_driver *driver)
409 DBG("driver %p name %s", driver, driver->name);
411 driver_list = g_slist_remove(driver_list, driver);
413 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
414 remove_driver, driver);
417 static void unregister_network(gpointer data)
419 struct connman_network *network = data;
421 DBG("network %p", network);
423 connman_element_unregister((struct connman_element *) network);
425 __connman_network_set_device(network, NULL);
427 connman_network_unref(network);
430 static void device_destruct(struct connman_element *element)
432 struct connman_device *device = element->device;
434 DBG("element %p name %s", element, element->name);
436 clear_scan_trigger(device);
438 g_free(device->ident);
439 g_free(device->node);
440 g_free(device->name);
441 g_free(device->address);
442 g_free(device->interface);
444 g_free(device->last_network);
446 g_hash_table_destroy(device->networks);
447 device->networks = NULL;
451 * connman_device_create:
452 * @node: device node name (for example an address)
455 * Allocate a new device of given #type and assign the #node name to it.
457 * Returns: a newly-allocated #connman_device structure
459 struct connman_device *connman_device_create(const char *node,
460 enum connman_device_type type)
462 struct connman_device *device;
464 enum connman_service_type service_type;
466 DBG("node %s type %d", node, type);
468 device = g_try_new0(struct connman_device, 1);
472 DBG("device %p", device);
474 __connman_element_initialize(&device->element);
476 device->element.name = g_strdup(node);
477 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
479 device->element.device = device;
480 device->element.destruct = device_destruct;
482 str = type2string(type);
484 connman_element_set_string(&device->element,
485 CONNMAN_PROPERTY_ID_TYPE, str);
487 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
490 device->name = g_strdup(type2description(device->type));
492 device->powered_persistent = TRUE;
494 device->phyindex = -1;
496 service_type = __connman_device_get_service_type(device);
497 device->blocked = __connman_technology_get_blocked(service_type);
498 device->backoff_interval = SCAN_INITIAL_DELAY;
501 case CONNMAN_DEVICE_TYPE_UNKNOWN:
502 case CONNMAN_DEVICE_TYPE_ETHERNET:
503 case CONNMAN_DEVICE_TYPE_WIMAX:
504 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
505 case CONNMAN_DEVICE_TYPE_CELLULAR:
506 case CONNMAN_DEVICE_TYPE_GPS:
507 case CONNMAN_DEVICE_TYPE_GADGET:
508 case CONNMAN_DEVICE_TYPE_VENDOR:
509 device->scan_interval = 0;
511 case CONNMAN_DEVICE_TYPE_WIFI:
512 device->scan_interval = 300;
516 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
517 g_free, unregister_network);
523 * connman_device_ref:
524 * @device: device structure
526 * Increase reference counter of device
528 struct connman_device *connman_device_ref(struct connman_device *device)
530 if (connman_element_ref(&device->element) == NULL)
537 * connman_device_unref:
538 * @device: device structure
540 * Decrease reference counter of device
542 void connman_device_unref(struct connman_device *device)
544 connman_element_unref(&device->element);
547 const char *__connman_device_get_type(struct connman_device *device)
549 return type2string(device->type);
553 * connman_device_get_type:
554 * @device: device structure
558 enum connman_device_type connman_device_get_type(struct connman_device *device)
564 * connman_device_set_index:
565 * @device: device structure
566 * @index: index number
568 * Set index number of device
570 void connman_device_set_index(struct connman_device *device, int index)
572 device->element.index = index;
576 * connman_device_get_index:
577 * @device: device structure
579 * Get index number of device
581 int connman_device_get_index(struct connman_device *device)
583 return device->element.index;
586 int __connman_device_get_phyindex(struct connman_device *device)
588 return device->phyindex;
591 void __connman_device_set_phyindex(struct connman_device *device,
594 device->phyindex = phyindex;
598 * connman_device_set_interface:
599 * @device: device structure
600 * @interface: interface name
602 * Set interface name of device
604 void connman_device_set_interface(struct connman_device *device,
605 const char *interface)
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 if (device->name == NULL) {
614 const char *str = type2description(device->type);
615 if (str != NULL && device->interface != NULL)
616 device->name = g_strdup_printf("%s (%s)", str,
622 * connman_device_set_ident:
623 * @device: device structure
624 * @ident: unique identifier
626 * Set unique identifier of device
628 void connman_device_set_ident(struct connman_device *device,
631 g_free(device->ident);
632 device->ident = g_strdup(ident);
635 const char *connman_device_get_ident(struct connman_device *device)
637 return device->ident;
641 * connman_device_set_powered:
642 * @device: device structure
643 * @powered: powered state
645 * Change power state of device
647 int connman_device_set_powered(struct connman_device *device,
648 connman_bool_t powered)
651 enum connman_service_type type;
653 DBG("driver %p powered %d", device, powered);
655 if (device->powered == powered) {
656 device->powered_pending = powered;
661 err = __connman_device_enable(device);
663 err = __connman_device_disable(device);
665 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
668 device->powered = powered;
669 device->powered_pending = powered;
671 type = __connman_device_get_service_type(device);
673 if (device->powered == TRUE)
674 __connman_technology_enable(type);
676 __connman_technology_disable(type);
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 && 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_available(gpointer key, gpointer value,
805 struct connman_network *network = value;
807 connman_network_set_available(network, TRUE);
810 static void mark_network_unavailable(gpointer key, gpointer value,
813 struct connman_network *network = value;
815 if (connman_network_get_connected(network) == TRUE)
818 connman_network_set_available(network, FALSE);
821 static gboolean remove_unavailable_network(gpointer key, gpointer value,
824 struct connman_network *network = value;
826 if (connman_network_get_connected(network) == TRUE)
829 if (connman_network_get_available(network) == TRUE)
835 void __connman_device_cleanup_networks(struct connman_device *device)
837 g_hash_table_foreach_remove(device->networks,
838 remove_unavailable_network, NULL);
841 connman_bool_t __connman_device_scanning(struct connman_device *device)
843 return device->scanning;
846 void connman_device_reset_scanning(struct connman_device *device)
848 device->scanning = FALSE;
850 g_hash_table_foreach(device->networks,
851 mark_network_available, NULL);
856 * connman_device_set_scanning:
857 * @device: device structure
858 * @scanning: scanning state
860 * Change scanning state of device
862 int connman_device_set_scanning(struct connman_device *device,
863 connman_bool_t scanning)
865 DBG("device %p scanning %d", device, scanning);
867 if (!device->driver || !device->driver->scan)
870 if (device->scanning == scanning)
873 device->scanning = scanning;
875 if (scanning == TRUE) {
876 reset_scan_trigger(device);
878 g_hash_table_foreach(device->networks,
879 mark_network_unavailable, NULL);
884 __connman_device_cleanup_networks(device);
886 if (device->connections > 0)
889 __connman_service_auto_connect();
895 * connman_device_set_disconnected:
896 * @device: device structure
897 * @disconnected: disconnected state
899 * Change disconnected state of device (only for device with networks)
901 int connman_device_set_disconnected(struct connman_device *device,
902 connman_bool_t disconnected)
904 DBG("device %p disconnected %d", device, disconnected);
906 if (device->disconnected == disconnected)
909 device->disconnected = disconnected;
911 if (disconnected == TRUE)
912 force_scan_trigger(device);
918 * connman_device_get_disconnected:
919 * @device: device structure
921 * Get device disconnected state
923 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
925 return device->disconnected;
929 * connman_device_set_string:
930 * @device: device structure
931 * @key: unique identifier
932 * @value: string value
934 * Set string value for specific key
936 int connman_device_set_string(struct connman_device *device,
937 const char *key, const char *value)
939 DBG("device %p key %s value %s", device, key, value);
941 if (g_str_equal(key, "Address") == TRUE) {
942 g_free(device->address);
943 device->address = g_strdup(value);
944 } else if (g_str_equal(key, "Name") == TRUE) {
945 g_free(device->name);
946 device->name = g_strdup(value);
947 } else if (g_str_equal(key, "Node") == TRUE) {
948 g_free(device->node);
949 device->node = g_strdup(value);
952 return connman_element_set_string(&device->element, key, value);
956 * connman_device_get_string:
957 * @device: device structure
958 * @key: unique identifier
960 * Get string value for specific key
962 const char *connman_device_get_string(struct connman_device *device,
965 DBG("device %p key %s", device, key);
967 if (g_str_equal(key, "Address") == TRUE)
968 return device->address;
969 else if (g_str_equal(key, "Name") == TRUE)
971 else if (g_str_equal(key, "Node") == TRUE)
973 else if (g_str_equal(key, "Interface") == TRUE)
974 return device->interface;
976 return connman_element_get_string(&device->element, key);
979 static void set_offlinemode(struct connman_element *element, gpointer user_data)
981 struct connman_device *device = element->device;
982 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
983 connman_bool_t powered;
985 DBG("element %p name %s", element, element->name);
990 device->offlinemode = offlinemode;
992 if (device->blocked == TRUE)
995 powered = (offlinemode == TRUE) ? FALSE : TRUE;
997 if (device->powered == powered)
1000 if (device->powered_persistent == FALSE)
1003 set_powered(device, powered);
1006 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1008 DBG("offlinmode %d", offlinemode);
1010 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1011 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1013 __connman_notifier_offlinemode(offlinemode);
1018 void __connman_device_increase_connections(struct connman_device *device)
1023 device->connections++;
1026 void __connman_device_decrease_connections(struct connman_device *device)
1031 device->connections--;
1033 if (device->connections == 0)
1034 device->backoff_interval = SCAN_INITIAL_DELAY;
1038 * connman_device_add_network:
1039 * @device: device structure
1040 * @network: network structure
1042 * Add new network to the device
1044 int connman_device_add_network(struct connman_device *device,
1045 struct connman_network *network)
1047 const char *identifier = connman_network_get_identifier(network);
1050 DBG("device %p network %p", device, network);
1052 if (identifier == NULL)
1055 __connman_network_set_device(network, device);
1057 err = connman_element_register((struct connman_element *) network,
1060 __connman_network_set_device(network, NULL);
1064 g_hash_table_insert(device->networks, g_strdup(identifier),
1071 * connman_device_get_network:
1072 * @device: device structure
1073 * @identifier: network identifier
1075 * Get network for given identifier
1077 struct connman_network *connman_device_get_network(struct connman_device *device,
1078 const char *identifier)
1080 DBG("device %p identifier %s", device, identifier);
1082 return g_hash_table_lookup(device->networks, identifier);
1086 * connman_device_remove_network:
1087 * @device: device structure
1088 * @identifier: network identifier
1090 * Remove network for given identifier
1092 int connman_device_remove_network(struct connman_device *device,
1093 const char *identifier)
1095 DBG("device %p identifier %s", device, identifier);
1097 g_hash_table_remove(device->networks, identifier);
1102 void connman_device_remove_all_networks(struct connman_device *device)
1104 g_hash_table_remove_all(device->networks);
1107 void __connman_device_set_network(struct connman_device *device,
1108 struct connman_network *network)
1115 if (device->network == network)
1118 if (device->network != NULL)
1119 connman_network_unref(device->network);
1121 if (network != NULL) {
1122 name = connman_network_get_string(network,
1123 CONNMAN_PROPERTY_ID_NAME);
1124 g_free(device->last_network);
1125 device->last_network = g_strdup(name);
1127 device->network = connman_network_ref(network);
1129 g_free(device->last_network);
1130 device->last_network = NULL;
1132 device->network = NULL;
1136 void __connman_device_set_reconnect(struct connman_device *device,
1137 connman_bool_t reconnect)
1139 device->reconnect = reconnect;
1142 connman_bool_t __connman_device_get_reconnect(
1143 struct connman_device *device)
1145 return device->reconnect;
1149 * connman_device_register:
1150 * @device: device structure
1152 * Register device with the system
1154 int connman_device_register(struct connman_device *device)
1156 __connman_storage_load_device(device);
1158 device->offlinemode = __connman_profile_get_offlinemode();
1160 return connman_element_register(&device->element, NULL);
1164 * connman_device_unregister:
1165 * @device: device structure
1167 * Unregister device with the system
1169 void connman_device_unregister(struct connman_device *device)
1171 __connman_storage_save_device(device);
1173 connman_element_unregister(&device->element);
1177 * connman_device_get_data:
1178 * @device: device structure
1180 * Get private device data pointer
1182 void *connman_device_get_data(struct connman_device *device)
1184 return device->driver_data;
1188 * connman_device_set_data:
1189 * @device: device structure
1190 * @data: data pointer
1192 * Set private device data pointer
1194 void connman_device_set_data(struct connman_device *device, void *data)
1196 device->driver_data = data;
1199 static gboolean match_driver(struct connman_device *device,
1200 struct connman_device_driver *driver)
1202 if (device->type == driver->type ||
1203 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1209 static int device_probe(struct connman_element *element)
1211 struct connman_device *device = element->device;
1214 DBG("element %p name %s", element, element->name);
1219 if (device->driver != NULL)
1222 for (list = driver_list; list; list = list->next) {
1223 struct connman_device_driver *driver = list->data;
1225 if (match_driver(device, driver) == FALSE)
1228 DBG("driver %p name %s", driver, driver->name);
1230 if (driver->probe(device) == 0) {
1231 device->driver = driver;
1236 if (device->driver == NULL)
1239 return setup_device(device);
1242 static void device_remove(struct connman_element *element)
1244 struct connman_device *device = element->device;
1246 DBG("element %p name %s", element, element->name);
1251 if (device->driver == NULL)
1254 remove_device(device);
1257 static struct connman_driver device_driver = {
1259 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1260 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1261 .probe = device_probe,
1262 .remove = device_remove,
1265 static int device_load(struct connman_device *device)
1267 const char *ident = __connman_profile_active_ident();
1269 GError *error = NULL;
1271 connman_bool_t powered;
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 powered = g_key_file_get_boolean(keyfile, identifier,
1286 device->powered_persistent = powered;
1287 g_clear_error(&error);
1292 __connman_storage_close_profile(ident, keyfile, FALSE);
1297 static int device_save(struct connman_device *device)
1299 const char *ident = __connman_profile_active_ident();
1303 DBG("device %p", device);
1305 keyfile = __connman_storage_open_profile(ident);
1306 if (keyfile == NULL)
1309 identifier = g_strdup_printf("device_%s", device->element.name);
1310 if (identifier == NULL)
1313 g_key_file_set_boolean(keyfile, identifier,
1314 "Powered", device->powered_persistent);
1319 __connman_storage_close_profile(ident, keyfile, TRUE);
1324 static struct connman_storage device_storage = {
1326 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1327 .device_load = device_load,
1328 .device_save = device_save,
1331 int __connman_device_init(void)
1335 if (connman_storage_register(&device_storage) < 0)
1336 connman_error("Failed to register device storage");
1338 return connman_driver_register(&device_driver);
1341 void __connman_device_cleanup(void)
1345 connman_driver_unregister(&device_driver);
1347 connman_storage_unregister(&device_storage);