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_VENDOR:
503 device->scan_interval = 0;
505 case CONNMAN_DEVICE_TYPE_ETHERNET:
506 case CONNMAN_DEVICE_TYPE_WIFI:
507 device->scan_interval = 300;
509 case CONNMAN_DEVICE_TYPE_WIMAX:
510 device->scan_interval = 0;
512 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
513 device->scan_interval = 0;
515 case CONNMAN_DEVICE_TYPE_GPS:
516 device->scan_interval = 0;
518 case CONNMAN_DEVICE_TYPE_CELLULAR:
519 device->scan_interval = 0;
521 case CONNMAN_DEVICE_TYPE_GADGET:
522 device->scan_interval = 0;
526 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
527 g_free, unregister_network);
533 * connman_device_ref:
534 * @device: device structure
536 * Increase reference counter of device
538 struct connman_device *connman_device_ref(struct connman_device *device)
540 if (connman_element_ref(&device->element) == NULL)
547 * connman_device_unref:
548 * @device: device structure
550 * Decrease reference counter of device
552 void connman_device_unref(struct connman_device *device)
554 connman_element_unref(&device->element);
557 const char *__connman_device_get_type(struct connman_device *device)
559 return type2string(device->type);
563 * connman_device_get_type:
564 * @device: device structure
568 enum connman_device_type connman_device_get_type(struct connman_device *device)
574 * connman_device_set_index:
575 * @device: device structure
576 * @index: index number
578 * Set index number of device
580 void connman_device_set_index(struct connman_device *device, int index)
582 device->element.index = index;
586 * connman_device_get_index:
587 * @device: device structure
589 * Get index number of device
591 int connman_device_get_index(struct connman_device *device)
593 return device->element.index;
596 int __connman_device_get_phyindex(struct connman_device *device)
598 return device->phyindex;
601 void __connman_device_set_phyindex(struct connman_device *device,
604 device->phyindex = phyindex;
608 * connman_device_set_interface:
609 * @device: device structure
610 * @interface: interface name
612 * Set interface name of device
614 void connman_device_set_interface(struct connman_device *device,
615 const char *interface)
617 g_free(device->element.devname);
618 device->element.devname = g_strdup(interface);
620 g_free(device->interface);
621 device->interface = g_strdup(interface);
623 if (device->name == NULL) {
624 const char *str = type2description(device->type);
625 if (str != NULL && device->interface != NULL)
626 device->name = g_strdup_printf("%s (%s)", str,
632 * connman_device_set_ident:
633 * @device: device structure
634 * @ident: unique identifier
636 * Set unique identifier of device
638 void connman_device_set_ident(struct connman_device *device,
641 g_free(device->ident);
642 device->ident = g_strdup(ident);
645 const char *connman_device_get_ident(struct connman_device *device)
647 return device->ident;
651 * connman_device_set_powered:
652 * @device: device structure
653 * @powered: powered state
655 * Change power state of device
657 int connman_device_set_powered(struct connman_device *device,
658 connman_bool_t powered)
661 enum connman_service_type type;
663 DBG("driver %p powered %d", device, powered);
665 if (device->powered == powered) {
666 device->powered_pending = powered;
671 err = __connman_device_enable(device);
673 err = __connman_device_disable(device);
675 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
678 device->powered = powered;
679 device->powered_pending = powered;
681 type = __connman_device_get_service_type(device);
683 if (device->powered == TRUE)
684 __connman_technology_enable(type);
686 __connman_technology_disable(type);
688 if (device->offlinemode == TRUE && powered == TRUE)
689 return connman_device_set_powered(device, FALSE);
691 if (powered == FALSE)
694 reset_scan_trigger(device);
696 if (device->driver && device->driver->scan)
697 device->driver->scan(device);
702 int __connman_device_set_blocked(struct connman_device *device,
703 connman_bool_t blocked)
705 connman_bool_t powered;
707 DBG("device %p blocked %d", device, blocked);
709 device->blocked = blocked;
711 if (device->offlinemode == TRUE)
714 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
716 if (blocked == FALSE)
717 powered = device->powered_persistent;
721 return set_powered(device, powered);
724 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
726 return device->blocked;
729 int __connman_device_scan(struct connman_device *device)
731 if (!device->driver || !device->driver->scan)
734 if (device->powered == FALSE)
737 reset_scan_trigger(device);
739 return device->driver->scan(device);
742 int __connman_device_enable_persistent(struct connman_device *device)
746 DBG("device %p", device);
748 device->powered_persistent = TRUE;
750 __connman_storage_save_device(device);
752 err = __connman_device_enable(device);
753 if (err == 0 || err == -EINPROGRESS) {
754 device->offlinemode = FALSE;
755 if (__connman_profile_get_offlinemode() == TRUE) {
756 __connman_profile_set_offlinemode(FALSE, FALSE);
758 __connman_profile_save_default();
765 int __connman_device_disable_persistent(struct connman_device *device)
767 DBG("device %p", device);
769 device->powered_persistent = FALSE;
771 __connman_storage_save_device(device);
773 return __connman_device_disable(device);
776 int __connman_device_disconnect(struct connman_device *device)
781 DBG("device %p", device);
783 connman_device_set_disconnected(device, TRUE);
785 g_hash_table_iter_init(&iter, device->networks);
787 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
788 struct connman_network *network = value;
790 if (__connman_network_get_connecting(network) == TRUE) {
792 * Skip network in the process of connecting.
793 * This is a workaround for WiFi networks serviced
794 * by the supplicant plugin that hold a reference
795 * to the network. If we disconnect the network
796 * here then the referenced object will not be
797 * registered and usage (like launching DHCP client)
798 * will fail. There is nothing to be gained by
799 * removing the network here anyway.
801 connman_warn("Skipping disconnect of %s",
802 connman_network_get_identifier(network));
806 __connman_network_disconnect(network);
812 static void mark_network_available(gpointer key, gpointer value,
815 struct connman_network *network = value;
817 connman_network_set_available(network, TRUE);
820 static void mark_network_unavailable(gpointer key, gpointer value,
823 struct connman_network *network = value;
825 if (connman_network_get_connected(network) == TRUE)
828 connman_network_set_available(network, FALSE);
831 static gboolean remove_unavailable_network(gpointer key, gpointer value,
834 struct connman_network *network = value;
836 if (connman_network_get_connected(network) == TRUE)
839 if (connman_network_get_available(network) == TRUE)
845 void __connman_device_cleanup_networks(struct connman_device *device)
847 g_hash_table_foreach_remove(device->networks,
848 remove_unavailable_network, NULL);
851 connman_bool_t __connman_device_scanning(struct connman_device *device)
853 return device->scanning;
856 void connman_device_reset_scanning(struct connman_device *device)
858 device->scanning = FALSE;
860 g_hash_table_foreach(device->networks,
861 mark_network_available, NULL);
866 * connman_device_set_scanning:
867 * @device: device structure
868 * @scanning: scanning state
870 * Change scanning state of device
872 int connman_device_set_scanning(struct connman_device *device,
873 connman_bool_t scanning)
875 DBG("device %p scanning %d", device, scanning);
877 if (!device->driver || !device->driver->scan)
880 if (device->scanning == scanning)
883 device->scanning = scanning;
885 if (scanning == TRUE) {
886 reset_scan_trigger(device);
888 g_hash_table_foreach(device->networks,
889 mark_network_unavailable, NULL);
894 __connman_device_cleanup_networks(device);
896 if (device->connections > 0)
899 __connman_service_auto_connect();
905 * connman_device_set_disconnected:
906 * @device: device structure
907 * @disconnected: disconnected state
909 * Change disconnected state of device (only for device with networks)
911 int connman_device_set_disconnected(struct connman_device *device,
912 connman_bool_t disconnected)
914 DBG("device %p disconnected %d", device, disconnected);
916 if (device->disconnected == disconnected)
919 device->disconnected = disconnected;
921 if (disconnected == TRUE)
922 force_scan_trigger(device);
928 * connman_device_get_disconnected:
929 * @device: device structure
931 * Get device disconnected state
933 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
935 return device->disconnected;
939 * connman_device_set_string:
940 * @device: device structure
941 * @key: unique identifier
942 * @value: string value
944 * Set string value for specific key
946 int connman_device_set_string(struct connman_device *device,
947 const char *key, const char *value)
949 DBG("device %p key %s value %s", device, key, value);
951 if (g_str_equal(key, "Address") == TRUE) {
952 g_free(device->address);
953 device->address = g_strdup(value);
954 } else if (g_str_equal(key, "Name") == TRUE) {
955 g_free(device->name);
956 device->name = g_strdup(value);
957 } else if (g_str_equal(key, "Node") == TRUE) {
958 g_free(device->node);
959 device->node = g_strdup(value);
962 return connman_element_set_string(&device->element, key, value);
966 * connman_device_get_string:
967 * @device: device structure
968 * @key: unique identifier
970 * Get string value for specific key
972 const char *connman_device_get_string(struct connman_device *device,
975 DBG("device %p key %s", device, key);
977 if (g_str_equal(key, "Address") == TRUE)
978 return device->address;
979 else if (g_str_equal(key, "Name") == TRUE)
981 else if (g_str_equal(key, "Node") == TRUE)
983 else if (g_str_equal(key, "Interface") == TRUE)
984 return device->interface;
986 return connman_element_get_string(&device->element, key);
989 static void set_offlinemode(struct connman_element *element, gpointer user_data)
991 struct connman_device *device = element->device;
992 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
993 connman_bool_t powered;
995 DBG("element %p name %s", element, element->name);
1000 device->offlinemode = offlinemode;
1002 if (device->blocked == TRUE)
1005 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1007 if (device->powered == powered)
1010 if (device->powered_persistent == FALSE)
1013 set_powered(device, powered);
1016 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1018 DBG("offlinmode %d", offlinemode);
1020 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1021 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1023 __connman_notifier_offlinemode(offlinemode);
1028 void __connman_device_increase_connections(struct connman_device *device)
1033 device->connections++;
1036 void __connman_device_decrease_connections(struct connman_device *device)
1041 device->connections--;
1043 if (device->connections == 0)
1044 device->backoff_interval = SCAN_INITIAL_DELAY;
1048 * connman_device_add_network:
1049 * @device: device structure
1050 * @network: network structure
1052 * Add new network to the device
1054 int connman_device_add_network(struct connman_device *device,
1055 struct connman_network *network)
1057 const char *identifier = connman_network_get_identifier(network);
1060 DBG("device %p network %p", device, network);
1062 if (identifier == NULL)
1065 __connman_network_set_device(network, device);
1067 err = connman_element_register((struct connman_element *) network,
1070 __connman_network_set_device(network, NULL);
1074 g_hash_table_insert(device->networks, g_strdup(identifier),
1081 * connman_device_get_network:
1082 * @device: device structure
1083 * @identifier: network identifier
1085 * Get network for given identifier
1087 struct connman_network *connman_device_get_network(struct connman_device *device,
1088 const char *identifier)
1090 DBG("device %p identifier %s", device, identifier);
1092 return g_hash_table_lookup(device->networks, identifier);
1096 * connman_device_remove_network:
1097 * @device: device structure
1098 * @identifier: network identifier
1100 * Remove network for given identifier
1102 int connman_device_remove_network(struct connman_device *device,
1103 const char *identifier)
1105 DBG("device %p identifier %s", device, identifier);
1107 g_hash_table_remove(device->networks, identifier);
1112 void connman_device_remove_all_networks(struct connman_device *device)
1114 g_hash_table_remove_all(device->networks);
1117 void __connman_device_set_network(struct connman_device *device,
1118 struct connman_network *network)
1125 if (device->network == network)
1128 if (device->network != NULL)
1129 connman_network_unref(device->network);
1131 if (network != NULL) {
1132 name = connman_network_get_string(network,
1133 CONNMAN_PROPERTY_ID_NAME);
1134 g_free(device->last_network);
1135 device->last_network = g_strdup(name);
1137 device->network = connman_network_ref(network);
1139 g_free(device->last_network);
1140 device->last_network = NULL;
1142 device->network = NULL;
1146 void __connman_device_set_reconnect(struct connman_device *device,
1147 connman_bool_t reconnect)
1149 device->reconnect = reconnect;
1152 connman_bool_t __connman_device_get_reconnect(
1153 struct connman_device *device)
1155 return device->reconnect;
1159 * connman_device_register:
1160 * @device: device structure
1162 * Register device with the system
1164 int connman_device_register(struct connman_device *device)
1166 __connman_storage_load_device(device);
1168 device->offlinemode = __connman_profile_get_offlinemode();
1170 return connman_element_register(&device->element, NULL);
1174 * connman_device_unregister:
1175 * @device: device structure
1177 * Unregister device with the system
1179 void connman_device_unregister(struct connman_device *device)
1181 __connman_storage_save_device(device);
1183 connman_element_unregister(&device->element);
1187 * connman_device_get_data:
1188 * @device: device structure
1190 * Get private device data pointer
1192 void *connman_device_get_data(struct connman_device *device)
1194 return device->driver_data;
1198 * connman_device_set_data:
1199 * @device: device structure
1200 * @data: data pointer
1202 * Set private device data pointer
1204 void connman_device_set_data(struct connman_device *device, void *data)
1206 device->driver_data = data;
1209 static gboolean match_driver(struct connman_device *device,
1210 struct connman_device_driver *driver)
1212 if (device->type == driver->type ||
1213 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1219 static int device_probe(struct connman_element *element)
1221 struct connman_device *device = element->device;
1224 DBG("element %p name %s", element, element->name);
1229 if (device->driver != NULL)
1232 for (list = driver_list; list; list = list->next) {
1233 struct connman_device_driver *driver = list->data;
1235 if (match_driver(device, driver) == FALSE)
1238 DBG("driver %p name %s", driver, driver->name);
1240 if (driver->probe(device) == 0) {
1241 device->driver = driver;
1246 if (device->driver == NULL)
1249 return setup_device(device);
1252 static void device_remove(struct connman_element *element)
1254 struct connman_device *device = element->device;
1256 DBG("element %p name %s", element, element->name);
1261 if (device->driver == NULL)
1264 remove_device(device);
1267 static struct connman_driver device_driver = {
1269 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1270 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1271 .probe = device_probe,
1272 .remove = device_remove,
1275 static int device_load(struct connman_device *device)
1277 const char *ident = __connman_profile_active_ident();
1279 GError *error = NULL;
1281 connman_bool_t powered;
1284 DBG("device %p", device);
1286 keyfile = __connman_storage_open_profile(ident);
1287 if (keyfile == NULL)
1290 identifier = g_strdup_printf("device_%s", device->element.name);
1291 if (identifier == NULL)
1294 powered = g_key_file_get_boolean(keyfile, identifier,
1297 device->powered_persistent = powered;
1298 g_clear_error(&error);
1300 val = g_key_file_get_integer(keyfile, identifier,
1301 "ScanInterval", &error);
1303 device->scan_interval = val;
1304 g_clear_error(&error);
1309 __connman_storage_close_profile(ident, keyfile, FALSE);
1314 static int device_save(struct connman_device *device)
1316 const char *ident = __connman_profile_active_ident();
1320 DBG("device %p", device);
1322 keyfile = __connman_storage_open_profile(ident);
1323 if (keyfile == NULL)
1326 identifier = g_strdup_printf("device_%s", device->element.name);
1327 if (identifier == NULL)
1330 g_key_file_set_boolean(keyfile, identifier,
1331 "Powered", device->powered_persistent);
1333 g_key_file_set_integer(keyfile, identifier,
1334 "ScanInterval", device->scan_interval);
1339 __connman_storage_close_profile(ident, keyfile, TRUE);
1344 static struct connman_storage device_storage = {
1346 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1347 .device_load = device_load,
1348 .device_save = device_save,
1351 int __connman_device_init(void)
1355 if (connman_storage_register(&device_storage) < 0)
1356 connman_error("Failed to register device storage");
1358 return connman_driver_register(&device_driver);
1361 void __connman_device_cleanup(void)
1365 connman_driver_unregister(&device_driver);
1367 connman_storage_unregister(&device_storage);