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);
490 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
493 device->name = g_strdup(type2description(device->type));
495 device->powered_persistent = TRUE;
497 device->phyindex = -1;
499 service_type = __connman_device_get_service_type(device);
500 device->blocked = __connman_technology_get_blocked(service_type);
501 device->backoff_interval = SCAN_INITIAL_DELAY;
504 case CONNMAN_DEVICE_TYPE_UNKNOWN:
505 case CONNMAN_DEVICE_TYPE_ETHERNET:
506 case CONNMAN_DEVICE_TYPE_WIMAX:
507 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
508 case CONNMAN_DEVICE_TYPE_CELLULAR:
509 case CONNMAN_DEVICE_TYPE_GPS:
510 case CONNMAN_DEVICE_TYPE_GADGET:
511 case CONNMAN_DEVICE_TYPE_VENDOR:
512 device->scan_interval = 0;
514 case CONNMAN_DEVICE_TYPE_WIFI:
516 device->scan_interval = 300;
518 device->scan_interval = 0;
522 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
523 g_free, unregister_network);
529 * connman_device_ref:
530 * @device: device structure
532 * Increase reference counter of device
534 struct connman_device *connman_device_ref(struct connman_device *device)
536 if (connman_element_ref(&device->element) == NULL)
543 * connman_device_unref:
544 * @device: device structure
546 * Decrease reference counter of device
548 void connman_device_unref(struct connman_device *device)
550 connman_element_unref(&device->element);
553 const char *__connman_device_get_type(struct connman_device *device)
555 return type2string(device->type);
559 * connman_device_get_type:
560 * @device: device structure
564 enum connman_device_type connman_device_get_type(struct connman_device *device)
570 * connman_device_set_index:
571 * @device: device structure
572 * @index: index number
574 * Set index number of device
576 void connman_device_set_index(struct connman_device *device, int index)
578 device->element.index = index;
582 * connman_device_get_index:
583 * @device: device structure
585 * Get index number of device
587 int connman_device_get_index(struct connman_device *device)
589 return device->element.index;
592 int __connman_device_get_phyindex(struct connman_device *device)
594 return device->phyindex;
597 void __connman_device_set_phyindex(struct connman_device *device,
600 device->phyindex = phyindex;
604 * connman_device_set_interface:
605 * @device: device structure
606 * @interface: interface name
608 * Set interface name of device
610 void connman_device_set_interface(struct connman_device *device,
611 const char *interface)
613 g_free(device->element.devname);
614 device->element.devname = g_strdup(interface);
616 g_free(device->interface);
617 device->interface = g_strdup(interface);
619 if (device->name == NULL) {
620 const char *str = type2description(device->type);
621 if (str != NULL && device->interface != NULL)
622 device->name = g_strdup_printf("%s (%s)", str,
628 * connman_device_set_ident:
629 * @device: device structure
630 * @ident: unique identifier
632 * Set unique identifier of device
634 void connman_device_set_ident(struct connman_device *device,
637 g_free(device->ident);
638 device->ident = g_strdup(ident);
641 const char *connman_device_get_ident(struct connman_device *device)
643 return device->ident;
647 * connman_device_set_powered:
648 * @device: device structure
649 * @powered: powered state
651 * Change power state of device
653 int connman_device_set_powered(struct connman_device *device,
654 connman_bool_t powered)
657 enum connman_service_type type;
659 DBG("driver %p powered %d", device, powered);
661 if (device->powered == powered) {
662 device->powered_pending = powered;
667 err = __connman_device_enable(device);
669 err = __connman_device_disable(device);
671 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
674 device->powered = powered;
675 device->powered_pending = powered;
677 type = __connman_device_get_service_type(device);
679 if (device->powered == TRUE)
680 __connman_technology_enable(type);
682 __connman_technology_disable(type);
684 if (device->offlinemode == TRUE && powered == TRUE)
685 return connman_device_set_powered(device, FALSE);
687 if (powered == FALSE)
690 reset_scan_trigger(device);
692 if (device->driver && device->driver->scan)
693 device->driver->scan(device);
698 int __connman_device_set_blocked(struct connman_device *device,
699 connman_bool_t blocked)
701 connman_bool_t powered;
703 DBG("device %p blocked %d", device, blocked);
705 device->blocked = blocked;
707 if (device->offlinemode == TRUE)
710 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
712 if (blocked == FALSE)
713 powered = device->powered_persistent;
717 return set_powered(device, powered);
720 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
722 return device->blocked;
725 int __connman_device_scan(struct connman_device *device)
727 if (!device->driver || !device->driver->scan)
730 if (device->powered == FALSE)
733 reset_scan_trigger(device);
735 return device->driver->scan(device);
738 int __connman_device_enable_persistent(struct connman_device *device)
742 DBG("device %p", device);
744 device->powered_persistent = TRUE;
746 __connman_storage_save_device(device);
748 err = __connman_device_enable(device);
749 if (err == 0 || err == -EINPROGRESS) {
750 device->offlinemode = FALSE;
751 if (__connman_profile_get_offlinemode() == TRUE) {
752 __connman_profile_set_offlinemode(FALSE, FALSE);
754 __connman_profile_save_default();
761 int __connman_device_disable_persistent(struct connman_device *device)
763 DBG("device %p", device);
765 device->powered_persistent = FALSE;
767 __connman_storage_save_device(device);
769 return __connman_device_disable(device);
772 int __connman_device_disconnect(struct connman_device *device)
777 DBG("device %p", device);
779 connman_device_set_disconnected(device, TRUE);
781 g_hash_table_iter_init(&iter, device->networks);
783 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
784 struct connman_network *network = value;
786 if (connman_network_get_connecting(network) == TRUE) {
788 * Skip network in the process of connecting.
789 * This is a workaround for WiFi networks serviced
790 * by the supplicant plugin that hold a reference
791 * to the network. If we disconnect the network
792 * here then the referenced object will not be
793 * registered and usage (like launching DHCP client)
794 * will fail. There is nothing to be gained by
795 * removing the network here anyway.
797 connman_warn("Skipping disconnect of %s",
798 connman_network_get_identifier(network));
802 __connman_network_disconnect(network);
808 static void mark_network_available(gpointer key, gpointer value,
811 struct connman_network *network = value;
813 connman_network_set_available(network, TRUE);
816 static void mark_network_unavailable(gpointer key, gpointer value,
819 struct connman_network *network = value;
821 if (connman_network_get_connected(network) == TRUE)
824 connman_network_set_available(network, FALSE);
827 static gboolean remove_unavailable_network(gpointer key, gpointer value,
830 struct connman_network *network = value;
832 if (connman_network_get_connected(network) == TRUE)
835 if (connman_network_get_available(network) == TRUE)
841 void __connman_device_cleanup_networks(struct connman_device *device)
843 g_hash_table_foreach_remove(device->networks,
844 remove_unavailable_network, NULL);
847 connman_bool_t __connman_device_scanning(struct connman_device *device)
849 return device->scanning;
852 void connman_device_reset_scanning(struct connman_device *device)
854 device->scanning = FALSE;
856 g_hash_table_foreach(device->networks,
857 mark_network_available, NULL);
862 * connman_device_set_scanning:
863 * @device: device structure
864 * @scanning: scanning state
866 * Change scanning state of device
868 int connman_device_set_scanning(struct connman_device *device,
869 connman_bool_t scanning)
871 DBG("device %p scanning %d", device, scanning);
873 if (!device->driver || !device->driver->scan)
876 if (device->scanning == scanning)
879 device->scanning = scanning;
881 if (scanning == TRUE) {
882 reset_scan_trigger(device);
884 g_hash_table_foreach(device->networks,
885 mark_network_unavailable, NULL);
890 __connman_device_cleanup_networks(device);
892 if (device->connections > 0)
895 __connman_service_auto_connect();
901 * connman_device_set_disconnected:
902 * @device: device structure
903 * @disconnected: disconnected state
905 * Change disconnected state of device (only for device with networks)
907 int connman_device_set_disconnected(struct connman_device *device,
908 connman_bool_t disconnected)
910 DBG("device %p disconnected %d", device, disconnected);
912 if (device->disconnected == disconnected)
915 device->disconnected = disconnected;
917 if (disconnected == TRUE)
918 force_scan_trigger(device);
924 * connman_device_get_disconnected:
925 * @device: device structure
927 * Get device disconnected state
929 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
931 return device->disconnected;
935 * connman_device_set_string:
936 * @device: device structure
937 * @key: unique identifier
938 * @value: string value
940 * Set string value for specific key
942 int connman_device_set_string(struct connman_device *device,
943 const char *key, const char *value)
945 DBG("device %p key %s value %s", device, key, value);
947 if (g_str_equal(key, "Address") == TRUE) {
948 g_free(device->address);
949 device->address = g_strdup(value);
950 } else if (g_str_equal(key, "Name") == TRUE) {
951 g_free(device->name);
952 device->name = g_strdup(value);
953 } else if (g_str_equal(key, "Node") == TRUE) {
954 g_free(device->node);
955 device->node = g_strdup(value);
958 return connman_element_set_string(&device->element, key, value);
962 * connman_device_get_string:
963 * @device: device structure
964 * @key: unique identifier
966 * Get string value for specific key
968 const char *connman_device_get_string(struct connman_device *device,
971 DBG("device %p key %s", device, key);
973 if (g_str_equal(key, "Address") == TRUE)
974 return device->address;
975 else if (g_str_equal(key, "Name") == TRUE)
977 else if (g_str_equal(key, "Node") == TRUE)
979 else if (g_str_equal(key, "Interface") == TRUE)
980 return device->interface;
982 return connman_element_get_string(&device->element, key);
985 static void set_offlinemode(struct connman_element *element, gpointer user_data)
987 struct connman_device *device = element->device;
988 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
989 connman_bool_t powered;
991 DBG("element %p name %s", element, element->name);
996 device->offlinemode = offlinemode;
998 if (device->blocked == TRUE)
1001 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1003 if (device->powered == powered)
1006 if (device->powered_persistent == FALSE)
1009 set_powered(device, powered);
1012 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1014 DBG("offlinmode %d", offlinemode);
1016 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1017 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1019 __connman_notifier_offlinemode(offlinemode);
1024 void __connman_device_increase_connections(struct connman_device *device)
1029 device->connections++;
1032 void __connman_device_decrease_connections(struct connman_device *device)
1037 device->connections--;
1039 if (device->connections == 0)
1040 device->backoff_interval = SCAN_INITIAL_DELAY;
1044 * connman_device_add_network:
1045 * @device: device structure
1046 * @network: network structure
1048 * Add new network to the device
1050 int connman_device_add_network(struct connman_device *device,
1051 struct connman_network *network)
1053 const char *identifier = connman_network_get_identifier(network);
1056 DBG("device %p network %p", device, network);
1058 if (identifier == NULL)
1061 __connman_network_set_device(network, device);
1063 err = connman_element_register((struct connman_element *) network,
1066 __connman_network_set_device(network, NULL);
1070 g_hash_table_insert(device->networks, g_strdup(identifier),
1077 * connman_device_get_network:
1078 * @device: device structure
1079 * @identifier: network identifier
1081 * Get network for given identifier
1083 struct connman_network *connman_device_get_network(struct connman_device *device,
1084 const char *identifier)
1086 DBG("device %p identifier %s", device, identifier);
1088 return g_hash_table_lookup(device->networks, identifier);
1092 * connman_device_remove_network:
1093 * @device: device structure
1094 * @identifier: network identifier
1096 * Remove network for given identifier
1098 int connman_device_remove_network(struct connman_device *device,
1099 const char *identifier)
1101 DBG("device %p identifier %s", device, identifier);
1103 g_hash_table_remove(device->networks, identifier);
1108 void connman_device_remove_all_networks(struct connman_device *device)
1110 g_hash_table_remove_all(device->networks);
1113 void __connman_device_set_network(struct connman_device *device,
1114 struct connman_network *network)
1121 if (device->network == network)
1124 if (device->network != NULL)
1125 connman_network_unref(device->network);
1127 if (network != NULL) {
1128 name = connman_network_get_string(network,
1129 CONNMAN_PROPERTY_ID_NAME);
1130 g_free(device->last_network);
1131 device->last_network = g_strdup(name);
1133 device->network = connman_network_ref(network);
1135 g_free(device->last_network);
1136 device->last_network = NULL;
1138 device->network = NULL;
1142 void __connman_device_set_reconnect(struct connman_device *device,
1143 connman_bool_t reconnect)
1145 device->reconnect = reconnect;
1148 connman_bool_t __connman_device_get_reconnect(
1149 struct connman_device *device)
1151 return device->reconnect;
1155 * connman_device_register:
1156 * @device: device structure
1158 * Register device with the system
1160 int connman_device_register(struct connman_device *device)
1162 __connman_storage_load_device(device);
1164 device->offlinemode = __connman_profile_get_offlinemode();
1166 return connman_element_register(&device->element, NULL);
1170 * connman_device_unregister:
1171 * @device: device structure
1173 * Unregister device with the system
1175 void connman_device_unregister(struct connman_device *device)
1177 __connman_storage_save_device(device);
1179 connman_element_unregister(&device->element);
1183 * connman_device_get_data:
1184 * @device: device structure
1186 * Get private device data pointer
1188 void *connman_device_get_data(struct connman_device *device)
1190 return device->driver_data;
1194 * connman_device_set_data:
1195 * @device: device structure
1196 * @data: data pointer
1198 * Set private device data pointer
1200 void connman_device_set_data(struct connman_device *device, void *data)
1202 device->driver_data = data;
1205 static gboolean match_driver(struct connman_device *device,
1206 struct connman_device_driver *driver)
1208 if (device->type == driver->type ||
1209 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1215 static int device_probe(struct connman_element *element)
1217 struct connman_device *device = element->device;
1220 DBG("element %p name %s", element, element->name);
1225 if (device->driver != NULL)
1228 for (list = driver_list; list; list = list->next) {
1229 struct connman_device_driver *driver = list->data;
1231 if (match_driver(device, driver) == FALSE)
1234 DBG("driver %p name %s", driver, driver->name);
1236 if (driver->probe(device) == 0) {
1237 device->driver = driver;
1242 if (device->driver == NULL)
1245 return setup_device(device);
1248 static void device_remove(struct connman_element *element)
1250 struct connman_device *device = element->device;
1252 DBG("element %p name %s", element, element->name);
1257 if (device->driver == NULL)
1260 remove_device(device);
1263 static struct connman_driver device_driver = {
1265 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1266 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1267 .probe = device_probe,
1268 .remove = device_remove,
1271 static int device_load(struct connman_device *device)
1273 const char *ident = __connman_profile_active_ident();
1275 GError *error = NULL;
1277 connman_bool_t powered;
1279 DBG("device %p", device);
1281 keyfile = __connman_storage_open_profile(ident);
1282 if (keyfile == NULL)
1285 identifier = g_strdup_printf("device_%s", device->element.name);
1286 if (identifier == NULL)
1289 powered = g_key_file_get_boolean(keyfile, identifier,
1292 device->powered_persistent = powered;
1293 g_clear_error(&error);
1298 __connman_storage_close_profile(ident, keyfile, FALSE);
1303 static int device_save(struct connman_device *device)
1305 const char *ident = __connman_profile_active_ident();
1309 DBG("device %p", device);
1311 keyfile = __connman_storage_open_profile(ident);
1312 if (keyfile == NULL)
1315 identifier = g_strdup_printf("device_%s", device->element.name);
1316 if (identifier == NULL)
1319 g_key_file_set_boolean(keyfile, identifier,
1320 "Powered", device->powered_persistent);
1325 __connman_storage_close_profile(ident, keyfile, TRUE);
1330 static struct connman_storage device_storage = {
1332 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1333 .device_load = device_load,
1334 .device_save = device_save,
1337 int __connman_device_init(void)
1341 if (connman_storage_register(&device_storage) < 0)
1342 connman_error("Failed to register device storage");
1344 return connman_driver_register(&device_driver);
1347 void __connman_device_cleanup(void)
1351 connman_driver_unregister(&device_driver);
1353 connman_storage_unregister(&device_storage);