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;
465 connman_bool_t bg_scan;
467 DBG("node %s type %d", node, type);
469 device = g_try_new0(struct connman_device, 1);
473 DBG("device %p", device);
475 bg_scan = connman_setting_get_bool("BackgroundScanning");
477 __connman_element_initialize(&device->element);
479 device->element.name = g_strdup(node);
480 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
482 device->element.device = device;
483 device->element.destruct = device_destruct;
485 str = type2string(type);
487 connman_element_set_string(&device->element,
488 CONNMAN_PROPERTY_ID_TYPE, str);
491 device->name = g_strdup(type2description(device->type));
493 device->powered_persistent = TRUE;
495 device->phyindex = -1;
497 service_type = __connman_device_get_service_type(device);
498 device->blocked = __connman_technology_get_blocked(service_type);
499 device->backoff_interval = SCAN_INITIAL_DELAY;
502 case CONNMAN_DEVICE_TYPE_UNKNOWN:
503 case CONNMAN_DEVICE_TYPE_ETHERNET:
504 case CONNMAN_DEVICE_TYPE_WIMAX:
505 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
506 case CONNMAN_DEVICE_TYPE_CELLULAR:
507 case CONNMAN_DEVICE_TYPE_GPS:
508 case CONNMAN_DEVICE_TYPE_GADGET:
509 case CONNMAN_DEVICE_TYPE_VENDOR:
510 device->scan_interval = 0;
512 case CONNMAN_DEVICE_TYPE_WIFI:
514 device->scan_interval = 300;
516 device->scan_interval = 0;
520 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
521 g_free, unregister_network);
527 * connman_device_ref:
528 * @device: device structure
530 * Increase reference counter of device
532 struct connman_device *connman_device_ref(struct connman_device *device)
534 if (connman_element_ref(&device->element) == NULL)
541 * connman_device_unref:
542 * @device: device structure
544 * Decrease reference counter of device
546 void connman_device_unref(struct connman_device *device)
548 connman_element_unref(&device->element);
551 const char *__connman_device_get_type(struct connman_device *device)
553 return type2string(device->type);
557 * connman_device_get_type:
558 * @device: device structure
562 enum connman_device_type connman_device_get_type(struct connman_device *device)
568 * connman_device_set_index:
569 * @device: device structure
570 * @index: index number
572 * Set index number of device
574 void connman_device_set_index(struct connman_device *device, int index)
576 device->element.index = index;
580 * connman_device_get_index:
581 * @device: device structure
583 * Get index number of device
585 int connman_device_get_index(struct connman_device *device)
587 return device->element.index;
590 int __connman_device_get_phyindex(struct connman_device *device)
592 return device->phyindex;
595 void __connman_device_set_phyindex(struct connman_device *device,
598 device->phyindex = phyindex;
602 * connman_device_set_interface:
603 * @device: device structure
604 * @interface: interface name
606 * Set interface name of device
608 void connman_device_set_interface(struct connman_device *device,
609 const char *interface)
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 if (device->name == NULL) {
618 const char *str = type2description(device->type);
619 if (str != NULL && device->interface != NULL)
620 device->name = g_strdup_printf("%s (%s)", str,
626 * connman_device_set_ident:
627 * @device: device structure
628 * @ident: unique identifier
630 * Set unique identifier of device
632 void connman_device_set_ident(struct connman_device *device,
635 g_free(device->ident);
636 device->ident = g_strdup(ident);
639 const char *connman_device_get_ident(struct connman_device *device)
641 return device->ident;
645 * connman_device_set_powered:
646 * @device: device structure
647 * @powered: powered state
649 * Change power state of device
651 int connman_device_set_powered(struct connman_device *device,
652 connman_bool_t powered)
655 enum connman_service_type type;
657 DBG("driver %p powered %d", device, powered);
659 if (device->powered == powered) {
660 device->powered_pending = powered;
665 err = __connman_device_enable(device);
667 err = __connman_device_disable(device);
669 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
672 device->powered = powered;
673 device->powered_pending = powered;
675 type = __connman_device_get_service_type(device);
677 if (device->powered == TRUE)
678 __connman_technology_enable(type);
680 __connman_technology_disable(type);
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 && 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_available(gpointer key, gpointer value,
809 struct connman_network *network = value;
811 connman_network_set_available(network, TRUE);
814 static void mark_network_unavailable(gpointer key, gpointer value,
817 struct connman_network *network = value;
819 if (connman_network_get_connected(network) == TRUE)
822 connman_network_set_available(network, FALSE);
825 static gboolean remove_unavailable_network(gpointer key, gpointer value,
828 struct connman_network *network = value;
830 if (connman_network_get_connected(network) == TRUE)
833 if (connman_network_get_available(network) == TRUE)
839 void __connman_device_cleanup_networks(struct connman_device *device)
841 g_hash_table_foreach_remove(device->networks,
842 remove_unavailable_network, NULL);
845 connman_bool_t __connman_device_scanning(struct connman_device *device)
847 return device->scanning;
850 void connman_device_reset_scanning(struct connman_device *device)
852 device->scanning = FALSE;
854 g_hash_table_foreach(device->networks,
855 mark_network_available, NULL);
860 * connman_device_set_scanning:
861 * @device: device structure
862 * @scanning: scanning state
864 * Change scanning state of device
866 int connman_device_set_scanning(struct connman_device *device,
867 connman_bool_t scanning)
869 DBG("device %p scanning %d", device, scanning);
871 if (!device->driver || !device->driver->scan)
874 if (device->scanning == scanning)
877 device->scanning = scanning;
879 if (scanning == TRUE) {
880 reset_scan_trigger(device);
882 g_hash_table_foreach(device->networks,
883 mark_network_unavailable, NULL);
888 __connman_device_cleanup_networks(device);
890 if (device->connections > 0)
893 __connman_service_auto_connect();
899 * connman_device_set_disconnected:
900 * @device: device structure
901 * @disconnected: disconnected state
903 * Change disconnected state of device (only for device with networks)
905 int connman_device_set_disconnected(struct connman_device *device,
906 connman_bool_t disconnected)
908 DBG("device %p disconnected %d", device, disconnected);
910 if (device->disconnected == disconnected)
913 device->disconnected = disconnected;
915 if (disconnected == TRUE)
916 force_scan_trigger(device);
922 * connman_device_get_disconnected:
923 * @device: device structure
925 * Get device disconnected state
927 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
929 return device->disconnected;
933 * connman_device_set_string:
934 * @device: device structure
935 * @key: unique identifier
936 * @value: string value
938 * Set string value for specific key
940 int connman_device_set_string(struct connman_device *device,
941 const char *key, const char *value)
943 DBG("device %p key %s value %s", device, key, value);
945 if (g_str_equal(key, "Address") == TRUE) {
946 g_free(device->address);
947 device->address = g_strdup(value);
948 } else if (g_str_equal(key, "Name") == TRUE) {
949 g_free(device->name);
950 device->name = g_strdup(value);
951 } else if (g_str_equal(key, "Node") == TRUE) {
952 g_free(device->node);
953 device->node = g_strdup(value);
956 return connman_element_set_string(&device->element, key, value);
960 * connman_device_get_string:
961 * @device: device structure
962 * @key: unique identifier
964 * Get string value for specific key
966 const char *connman_device_get_string(struct connman_device *device,
969 DBG("device %p key %s", device, key);
971 if (g_str_equal(key, "Address") == TRUE)
972 return device->address;
973 else if (g_str_equal(key, "Name") == TRUE)
975 else if (g_str_equal(key, "Node") == TRUE)
977 else if (g_str_equal(key, "Interface") == TRUE)
978 return device->interface;
980 return connman_element_get_string(&device->element, key);
983 static void set_offlinemode(struct connman_element *element, gpointer user_data)
985 struct connman_device *device = element->device;
986 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
987 connman_bool_t powered;
989 DBG("element %p name %s", element, element->name);
994 device->offlinemode = offlinemode;
996 if (device->blocked == TRUE)
999 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1001 if (device->powered == powered)
1004 if (device->powered_persistent == FALSE)
1007 set_powered(device, powered);
1010 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1012 DBG("offlinmode %d", offlinemode);
1014 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1015 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1017 __connman_notifier_offlinemode(offlinemode);
1022 void __connman_device_increase_connections(struct connman_device *device)
1027 device->connections++;
1030 void __connman_device_decrease_connections(struct connman_device *device)
1035 device->connections--;
1037 if (device->connections == 0)
1038 device->backoff_interval = SCAN_INITIAL_DELAY;
1042 * connman_device_add_network:
1043 * @device: device structure
1044 * @network: network structure
1046 * Add new network to the device
1048 int connman_device_add_network(struct connman_device *device,
1049 struct connman_network *network)
1051 const char *identifier = connman_network_get_identifier(network);
1054 DBG("device %p network %p", device, network);
1056 if (identifier == NULL)
1059 __connman_network_set_device(network, device);
1061 err = connman_element_register((struct connman_element *) network,
1064 __connman_network_set_device(network, NULL);
1068 g_hash_table_insert(device->networks, g_strdup(identifier),
1075 * connman_device_get_network:
1076 * @device: device structure
1077 * @identifier: network identifier
1079 * Get network for given identifier
1081 struct connman_network *connman_device_get_network(struct connman_device *device,
1082 const char *identifier)
1084 DBG("device %p identifier %s", device, identifier);
1086 return g_hash_table_lookup(device->networks, identifier);
1090 * connman_device_remove_network:
1091 * @device: device structure
1092 * @identifier: network identifier
1094 * Remove network for given identifier
1096 int connman_device_remove_network(struct connman_device *device,
1097 const char *identifier)
1099 DBG("device %p identifier %s", device, identifier);
1101 g_hash_table_remove(device->networks, identifier);
1106 void connman_device_remove_all_networks(struct connman_device *device)
1108 g_hash_table_remove_all(device->networks);
1111 void __connman_device_set_network(struct connman_device *device,
1112 struct connman_network *network)
1119 if (device->network == network)
1122 if (device->network != NULL)
1123 connman_network_unref(device->network);
1125 if (network != NULL) {
1126 name = connman_network_get_string(network,
1127 CONNMAN_PROPERTY_ID_NAME);
1128 g_free(device->last_network);
1129 device->last_network = g_strdup(name);
1131 device->network = connman_network_ref(network);
1133 g_free(device->last_network);
1134 device->last_network = NULL;
1136 device->network = NULL;
1140 void __connman_device_set_reconnect(struct connman_device *device,
1141 connman_bool_t reconnect)
1143 device->reconnect = reconnect;
1146 connman_bool_t __connman_device_get_reconnect(
1147 struct connman_device *device)
1149 return device->reconnect;
1153 * connman_device_register:
1154 * @device: device structure
1156 * Register device with the system
1158 int connman_device_register(struct connman_device *device)
1160 __connman_storage_load_device(device);
1162 device->offlinemode = __connman_profile_get_offlinemode();
1164 return connman_element_register(&device->element, NULL);
1168 * connman_device_unregister:
1169 * @device: device structure
1171 * Unregister device with the system
1173 void connman_device_unregister(struct connman_device *device)
1175 __connman_storage_save_device(device);
1177 connman_element_unregister(&device->element);
1181 * connman_device_get_data:
1182 * @device: device structure
1184 * Get private device data pointer
1186 void *connman_device_get_data(struct connman_device *device)
1188 return device->driver_data;
1192 * connman_device_set_data:
1193 * @device: device structure
1194 * @data: data pointer
1196 * Set private device data pointer
1198 void connman_device_set_data(struct connman_device *device, void *data)
1200 device->driver_data = data;
1203 static gboolean match_driver(struct connman_device *device,
1204 struct connman_device_driver *driver)
1206 if (device->type == driver->type ||
1207 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1213 static int device_probe(struct connman_element *element)
1215 struct connman_device *device = element->device;
1218 DBG("element %p name %s", element, element->name);
1223 if (device->driver != NULL)
1226 for (list = driver_list; list; list = list->next) {
1227 struct connman_device_driver *driver = list->data;
1229 if (match_driver(device, driver) == FALSE)
1232 DBG("driver %p name %s", driver, driver->name);
1234 if (driver->probe(device) == 0) {
1235 device->driver = driver;
1240 if (device->driver == NULL)
1243 return setup_device(device);
1246 static void device_remove(struct connman_element *element)
1248 struct connman_device *device = element->device;
1250 DBG("element %p name %s", element, element->name);
1255 if (device->driver == NULL)
1258 remove_device(device);
1261 static struct connman_driver device_driver = {
1263 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1264 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1265 .probe = device_probe,
1266 .remove = device_remove,
1269 static int device_load(struct connman_device *device)
1271 const char *ident = __connman_profile_active_ident();
1273 GError *error = NULL;
1275 connman_bool_t powered;
1277 DBG("device %p", device);
1279 keyfile = __connman_storage_open_profile(ident);
1280 if (keyfile == NULL)
1283 identifier = g_strdup_printf("device_%s", device->element.name);
1284 if (identifier == NULL)
1287 powered = g_key_file_get_boolean(keyfile, identifier,
1290 device->powered_persistent = powered;
1291 g_clear_error(&error);
1296 __connman_storage_close_profile(ident, keyfile, FALSE);
1301 static int device_save(struct connman_device *device)
1303 const char *ident = __connman_profile_active_ident();
1307 DBG("device %p", device);
1309 keyfile = __connman_storage_open_profile(ident);
1310 if (keyfile == NULL)
1313 identifier = g_strdup_printf("device_%s", device->element.name);
1314 if (identifier == NULL)
1317 g_key_file_set_boolean(keyfile, identifier,
1318 "Powered", device->powered_persistent);
1323 __connman_storage_close_profile(ident, keyfile, TRUE);
1328 static struct connman_storage device_storage = {
1330 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1331 .device_load = device_load,
1332 .device_save = device_save,
1335 int __connman_device_init(void)
1339 if (connman_storage_register(&device_storage) < 0)
1340 connman_error("Failed to register device storage");
1342 return connman_driver_register(&device_driver);
1345 void __connman_device_cleanup(void)
1349 connman_driver_unregister(&device_driver);
1351 connman_storage_unregister(&device_storage);