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;
49 unsigned int connections;
52 struct connman_device_driver *driver;
56 struct connman_network *network;
60 static gboolean device_scan_trigger(gpointer user_data)
62 struct connman_device *device = user_data;
64 DBG("device %p", device);
66 if (device->driver == NULL) {
67 device->scan_timeout = 0;
71 if (device->driver->scan)
72 device->driver->scan(device);
77 static void clear_scan_trigger(struct connman_device *device)
79 if (device->scan_timeout > 0) {
80 g_source_remove(device->scan_timeout);
81 device->scan_timeout = 0;
85 static void reset_scan_trigger(struct connman_device *device)
87 clear_scan_trigger(device);
89 if (device->scan_interval > 0) {
90 guint interval = device->scan_interval;
91 device->scan_timeout = g_timeout_add_seconds(interval,
92 device_scan_trigger, device);
96 static void force_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 device->scan_timeout = g_timeout_add_seconds(5,
101 device_scan_trigger, device);
104 void connman_device_schedule_scan(struct connman_device *device)
106 reset_scan_trigger(device);
109 static const char *type2description(enum connman_device_type type)
112 case CONNMAN_DEVICE_TYPE_UNKNOWN:
113 case CONNMAN_DEVICE_TYPE_VENDOR:
115 case CONNMAN_DEVICE_TYPE_ETHERNET:
117 case CONNMAN_DEVICE_TYPE_WIFI:
119 case CONNMAN_DEVICE_TYPE_WIMAX:
121 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
123 case CONNMAN_DEVICE_TYPE_GPS:
125 case CONNMAN_DEVICE_TYPE_CELLULAR:
127 case CONNMAN_DEVICE_TYPE_GADGET:
135 static const char *type2string(enum connman_device_type type)
138 case CONNMAN_DEVICE_TYPE_UNKNOWN:
139 case CONNMAN_DEVICE_TYPE_VENDOR:
141 case CONNMAN_DEVICE_TYPE_ETHERNET:
143 case CONNMAN_DEVICE_TYPE_WIFI:
145 case CONNMAN_DEVICE_TYPE_WIMAX:
147 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
149 case CONNMAN_DEVICE_TYPE_GPS:
151 case CONNMAN_DEVICE_TYPE_CELLULAR:
153 case CONNMAN_DEVICE_TYPE_GADGET:
161 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
163 enum connman_device_type type = connman_device_get_type(device);
166 case CONNMAN_DEVICE_TYPE_UNKNOWN:
167 case CONNMAN_DEVICE_TYPE_VENDOR:
168 case CONNMAN_DEVICE_TYPE_GPS:
170 case CONNMAN_DEVICE_TYPE_ETHERNET:
171 return CONNMAN_SERVICE_TYPE_ETHERNET;
172 case CONNMAN_DEVICE_TYPE_WIFI:
173 return CONNMAN_SERVICE_TYPE_WIFI;
174 case CONNMAN_DEVICE_TYPE_WIMAX:
175 return CONNMAN_SERVICE_TYPE_WIMAX;
176 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
177 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
178 case CONNMAN_DEVICE_TYPE_CELLULAR:
179 return CONNMAN_SERVICE_TYPE_CELLULAR;
180 case CONNMAN_DEVICE_TYPE_GADGET:
181 return CONNMAN_SERVICE_TYPE_GADGET;
185 return CONNMAN_SERVICE_TYPE_UNKNOWN;
188 int __connman_device_enable(struct connman_device *device)
191 enum connman_service_type type;
193 DBG("device %p %d", device, device->blocked);
195 if (!device->driver || !device->driver->enable)
198 if (device->powered_pending == TRUE)
201 if (device->blocked == TRUE)
204 connman_device_set_disconnected(device, FALSE);
205 device->scanning = FALSE;
207 err = device->driver->enable(device);
208 if (err < 0 && err != -EALREADY) {
209 if (err == -EINPROGRESS) {
210 device->powered_pending = TRUE;
211 device->offlinemode = FALSE;
212 if (__connman_profile_get_offlinemode() == TRUE)
213 __connman_profile_set_offlinemode(FALSE, FALSE);
218 device->powered_pending = TRUE;
219 device->powered = TRUE;
220 device->offlinemode = FALSE;
221 if (__connman_profile_get_offlinemode() == TRUE)
222 __connman_profile_set_offlinemode(FALSE, FALSE);
224 type = __connman_device_get_service_type(device);
225 __connman_technology_enable(type);
230 int __connman_device_disable(struct connman_device *device)
233 enum connman_service_type type;
235 DBG("device %p", device);
237 if (!device->driver || !device->driver->disable)
240 if (device->powered == FALSE)
243 if (device->powered_pending == FALSE)
246 device->reconnect = FALSE;
248 clear_scan_trigger(device);
250 g_hash_table_remove_all(device->networks);
252 err = device->driver->disable(device);
253 if (err < 0 && err != -EALREADY) {
254 if (err == -EINPROGRESS)
255 device->powered_pending = FALSE;
259 device->connections = 0;
261 device->powered_pending = FALSE;
262 device->powered = FALSE;
264 type = __connman_device_get_service_type(device);
265 __connman_technology_disable(type);
270 static int set_powered(struct connman_device *device, connman_bool_t powered)
272 DBG("device %p powered %d", device, powered);
275 return __connman_device_enable(device);
277 return __connman_device_disable(device);
280 static int setup_device(struct connman_device *device)
282 DBG("device %p", device);
284 __connman_technology_add_device(device);
286 if (device->offlinemode == FALSE &&
287 device->powered_persistent == TRUE)
288 __connman_device_enable(device);
293 static void probe_driver(struct connman_element *element, gpointer user_data)
295 struct connman_device_driver *driver = user_data;
297 DBG("element %p name %s", element, element->name);
299 if (element->device == NULL)
302 if (element->device->driver != NULL)
305 if (driver->type != element->device->type)
308 if (driver->probe(element->device) < 0)
311 element->device->driver = driver;
313 __connman_element_set_driver(element);
315 setup_device(element->device);
318 static void remove_device(struct connman_device *device)
320 DBG("device %p", device);
322 __connman_device_disable(device);
324 __connman_technology_remove_device(device);
326 if (device->driver->remove)
327 device->driver->remove(device);
329 device->driver = NULL;
332 static void remove_driver(struct connman_element *element, gpointer user_data)
334 struct connman_device_driver *driver = user_data;
336 DBG("element %p name %s", element, element->name);
338 if (element->device == NULL)
341 if (element->device->driver == driver)
342 remove_device(element->device);
345 connman_bool_t __connman_device_has_driver(struct connman_device *device)
347 if (device == NULL || device->driver == NULL)
353 static GSList *driver_list = NULL;
355 static gint compare_priority(gconstpointer a, gconstpointer b)
357 const struct connman_device_driver *driver1 = a;
358 const struct connman_device_driver *driver2 = b;
360 return driver2->priority - driver1->priority;
364 * connman_device_driver_register:
365 * @driver: device driver definition
367 * Register a new device driver
369 * Returns: %0 on success
371 int connman_device_driver_register(struct connman_device_driver *driver)
373 DBG("driver %p name %s", driver, driver->name);
375 driver_list = g_slist_insert_sorted(driver_list, driver,
378 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
379 probe_driver, driver);
385 * connman_device_driver_unregister:
386 * @driver: device driver definition
388 * Remove a previously registered device driver
390 void connman_device_driver_unregister(struct connman_device_driver *driver)
392 DBG("driver %p name %s", driver, driver->name);
394 driver_list = g_slist_remove(driver_list, driver);
396 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
397 remove_driver, driver);
400 static void unregister_network(gpointer data)
402 struct connman_network *network = data;
404 DBG("network %p", network);
406 connman_element_unregister((struct connman_element *) network);
408 __connman_network_set_device(network, NULL);
410 connman_network_unref(network);
413 static void device_destruct(struct connman_element *element)
415 struct connman_device *device = element->device;
417 DBG("element %p name %s", element, element->name);
419 clear_scan_trigger(device);
421 g_free(device->ident);
422 g_free(device->node);
423 g_free(device->name);
424 g_free(device->address);
425 g_free(device->interface);
427 g_free(device->last_network);
429 g_hash_table_destroy(device->networks);
430 device->networks = NULL;
434 * connman_device_create:
435 * @node: device node name (for example an address)
438 * Allocate a new device of given #type and assign the #node name to it.
440 * Returns: a newly-allocated #connman_device structure
442 struct connman_device *connman_device_create(const char *node,
443 enum connman_device_type type)
445 struct connman_device *device;
447 enum connman_service_type service_type;
449 DBG("node %s type %d", node, type);
451 device = g_try_new0(struct connman_device, 1);
455 DBG("device %p", device);
457 __connman_element_initialize(&device->element);
459 device->element.name = g_strdup(node);
460 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
462 device->element.device = device;
463 device->element.destruct = device_destruct;
465 str = type2string(type);
467 connman_element_set_string(&device->element,
468 CONNMAN_PROPERTY_ID_TYPE, str);
470 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
473 device->name = g_strdup(type2description(device->type));
475 device->powered_persistent = TRUE;
477 device->phyindex = -1;
479 service_type = __connman_device_get_service_type(device);
480 device->blocked = __connman_technology_get_blocked(service_type);
483 case CONNMAN_DEVICE_TYPE_UNKNOWN:
484 case CONNMAN_DEVICE_TYPE_VENDOR:
485 device->scan_interval = 0;
487 case CONNMAN_DEVICE_TYPE_ETHERNET:
488 case CONNMAN_DEVICE_TYPE_WIFI:
489 device->scan_interval = 300;
491 case CONNMAN_DEVICE_TYPE_WIMAX:
492 device->scan_interval = 0;
494 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
495 device->scan_interval = 0;
497 case CONNMAN_DEVICE_TYPE_GPS:
498 device->scan_interval = 0;
500 case CONNMAN_DEVICE_TYPE_CELLULAR:
501 device->scan_interval = 0;
503 case CONNMAN_DEVICE_TYPE_GADGET:
504 device->scan_interval = 0;
508 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
509 g_free, unregister_network);
515 * connman_device_ref:
516 * @device: device structure
518 * Increase reference counter of device
520 struct connman_device *connman_device_ref(struct connman_device *device)
522 if (connman_element_ref(&device->element) == NULL)
529 * connman_device_unref:
530 * @device: device structure
532 * Decrease reference counter of device
534 void connman_device_unref(struct connman_device *device)
536 connman_element_unref(&device->element);
539 const char *__connman_device_get_type(struct connman_device *device)
541 return type2string(device->type);
545 * connman_device_get_type:
546 * @device: device structure
550 enum connman_device_type connman_device_get_type(struct connman_device *device)
556 * connman_device_set_index:
557 * @device: device structure
558 * @index: index number
560 * Set index number of device
562 void connman_device_set_index(struct connman_device *device, int index)
564 device->element.index = index;
568 * connman_device_get_index:
569 * @device: device structure
571 * Get index number of device
573 int connman_device_get_index(struct connman_device *device)
575 return device->element.index;
578 int __connman_device_get_phyindex(struct connman_device *device)
580 return device->phyindex;
583 void __connman_device_set_phyindex(struct connman_device *device,
586 device->phyindex = phyindex;
590 * connman_device_set_interface:
591 * @device: device structure
592 * @interface: interface name
594 * Set interface name of device
596 void connman_device_set_interface(struct connman_device *device,
597 const char *interface)
599 g_free(device->element.devname);
600 device->element.devname = g_strdup(interface);
602 g_free(device->interface);
603 device->interface = g_strdup(interface);
605 if (device->name == NULL) {
606 const char *str = type2description(device->type);
607 if (str != NULL && device->interface != NULL)
608 device->name = g_strdup_printf("%s (%s)", str,
614 * connman_device_set_ident:
615 * @device: device structure
616 * @ident: unique identifier
618 * Set unique identifier of device
620 void connman_device_set_ident(struct connman_device *device,
623 g_free(device->ident);
624 device->ident = g_strdup(ident);
627 const char *connman_device_get_ident(struct connman_device *device)
629 return device->ident;
633 * connman_device_set_powered:
634 * @device: device structure
635 * @powered: powered state
637 * Change power state of device
639 int connman_device_set_powered(struct connman_device *device,
640 connman_bool_t powered)
643 enum connman_service_type type;
645 DBG("driver %p powered %d", device, powered);
647 if (device->powered == powered) {
648 device->powered_pending = powered;
653 err = __connman_device_enable(device);
655 err = __connman_device_disable(device);
657 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
660 device->powered = powered;
661 device->powered_pending = powered;
663 type = __connman_device_get_service_type(device);
665 if (device->powered == TRUE)
666 __connman_technology_enable(type);
668 __connman_technology_disable(type);
670 if (device->offlinemode == TRUE && powered == TRUE)
671 return connman_device_set_powered(device, FALSE);
673 if (powered == FALSE)
676 reset_scan_trigger(device);
678 if (device->driver && device->driver->scan)
679 device->driver->scan(device);
684 int __connman_device_set_blocked(struct connman_device *device,
685 connman_bool_t blocked)
687 connman_bool_t powered;
689 DBG("device %p blocked %d", device, blocked);
691 device->blocked = blocked;
693 if (device->offlinemode == TRUE)
696 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
698 if (blocked == FALSE)
699 powered = device->powered_persistent;
703 return set_powered(device, powered);
706 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
708 return device->blocked;
711 int __connman_device_scan(struct connman_device *device)
713 if (!device->driver || !device->driver->scan)
716 if (device->powered == FALSE)
719 reset_scan_trigger(device);
721 return device->driver->scan(device);
724 int __connman_device_enable_persistent(struct connman_device *device)
728 DBG("device %p", device);
730 device->powered_persistent = TRUE;
732 __connman_storage_save_device(device);
734 err = __connman_device_enable(device);
735 if (err == 0 || err == -EINPROGRESS) {
736 device->offlinemode = FALSE;
737 if (__connman_profile_get_offlinemode() == TRUE) {
738 __connman_profile_set_offlinemode(FALSE, FALSE);
740 __connman_profile_save_default();
747 int __connman_device_disable_persistent(struct connman_device *device)
749 DBG("device %p", device);
751 device->powered_persistent = FALSE;
753 __connman_storage_save_device(device);
755 return __connman_device_disable(device);
758 int __connman_device_disconnect(struct connman_device *device)
763 DBG("device %p", device);
765 connman_device_set_disconnected(device, TRUE);
767 g_hash_table_iter_init(&iter, device->networks);
769 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
770 struct connman_network *network = value;
772 if (__connman_network_get_connecting(network) == TRUE) {
774 * Skip network in the process of connecting.
775 * This is a workaround for WiFi networks serviced
776 * by the supplicant plugin that hold a reference
777 * to the network. If we disconnect the network
778 * here then the referenced object will not be
779 * registered and usage (like launching DHCP client)
780 * will fail. There is nothing to be gained by
781 * removing the network here anyway.
783 connman_warn("Skipping disconnect of %s",
784 connman_network_get_identifier(network));
788 __connman_network_disconnect(network);
794 static void mark_network_available(gpointer key, gpointer value,
797 struct connman_network *network = value;
799 connman_network_set_available(network, TRUE);
802 static void mark_network_unavailable(gpointer key, gpointer value,
805 struct connman_network *network = value;
807 if (connman_network_get_connected(network) == TRUE)
810 connman_network_set_available(network, FALSE);
813 static gboolean remove_unavailable_network(gpointer key, gpointer value,
816 struct connman_network *network = value;
818 if (connman_network_get_connected(network) == TRUE)
821 if (connman_network_get_available(network) == TRUE)
827 void __connman_device_cleanup_networks(struct connman_device *device)
829 g_hash_table_foreach_remove(device->networks,
830 remove_unavailable_network, NULL);
833 connman_bool_t __connman_device_scanning(struct connman_device *device)
835 return device->scanning;
838 void connman_device_reset_scanning(struct connman_device *device)
840 device->scanning = FALSE;
842 g_hash_table_foreach(device->networks,
843 mark_network_available, NULL);
848 * connman_device_set_scanning:
849 * @device: device structure
850 * @scanning: scanning state
852 * Change scanning state of device
854 int connman_device_set_scanning(struct connman_device *device,
855 connman_bool_t scanning)
857 DBG("device %p scanning %d", device, scanning);
859 if (!device->driver || !device->driver->scan)
862 if (device->scanning == scanning)
865 device->scanning = scanning;
867 if (scanning == TRUE) {
868 reset_scan_trigger(device);
870 g_hash_table_foreach(device->networks,
871 mark_network_unavailable, NULL);
876 __connman_device_cleanup_networks(device);
878 if (device->connections > 0)
881 __connman_service_auto_connect();
887 * connman_device_set_disconnected:
888 * @device: device structure
889 * @disconnected: disconnected state
891 * Change disconnected state of device (only for device with networks)
893 int connman_device_set_disconnected(struct connman_device *device,
894 connman_bool_t disconnected)
896 DBG("device %p disconnected %d", device, disconnected);
898 if (device->disconnected == disconnected)
901 device->disconnected = disconnected;
903 if (disconnected == TRUE)
904 force_scan_trigger(device);
910 * connman_device_get_disconnected:
911 * @device: device structure
913 * Get device disconnected state
915 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
917 return device->disconnected;
921 * connman_device_set_string:
922 * @device: device structure
923 * @key: unique identifier
924 * @value: string value
926 * Set string value for specific key
928 int connman_device_set_string(struct connman_device *device,
929 const char *key, const char *value)
931 DBG("device %p key %s value %s", device, key, value);
933 if (g_str_equal(key, "Address") == TRUE) {
934 g_free(device->address);
935 device->address = g_strdup(value);
936 } else if (g_str_equal(key, "Name") == TRUE) {
937 g_free(device->name);
938 device->name = g_strdup(value);
939 } else if (g_str_equal(key, "Node") == TRUE) {
940 g_free(device->node);
941 device->node = g_strdup(value);
944 return connman_element_set_string(&device->element, key, value);
948 * connman_device_get_string:
949 * @device: device structure
950 * @key: unique identifier
952 * Get string value for specific key
954 const char *connman_device_get_string(struct connman_device *device,
957 DBG("device %p key %s", device, key);
959 if (g_str_equal(key, "Address") == TRUE)
960 return device->address;
961 else if (g_str_equal(key, "Name") == TRUE)
963 else if (g_str_equal(key, "Node") == TRUE)
965 else if (g_str_equal(key, "Interface") == TRUE)
966 return device->interface;
968 return connman_element_get_string(&device->element, key);
971 static void set_offlinemode(struct connman_element *element, gpointer user_data)
973 struct connman_device *device = element->device;
974 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
975 connman_bool_t powered;
977 DBG("element %p name %s", element, element->name);
982 device->offlinemode = offlinemode;
984 if (device->blocked == TRUE)
987 powered = (offlinemode == TRUE) ? FALSE : TRUE;
989 if (device->powered == powered)
992 if (device->powered_persistent == FALSE)
995 set_powered(device, powered);
998 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1000 DBG("offlinmode %d", offlinemode);
1002 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1003 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1005 __connman_notifier_offlinemode(offlinemode);
1010 void __connman_device_increase_connections(struct connman_device *device)
1015 device->connections++;
1018 void __connman_device_decrease_connections(struct connman_device *device)
1023 device->connections--;
1027 * connman_device_add_network:
1028 * @device: device structure
1029 * @network: network structure
1031 * Add new network to the device
1033 int connman_device_add_network(struct connman_device *device,
1034 struct connman_network *network)
1036 const char *identifier = connman_network_get_identifier(network);
1039 DBG("device %p network %p", device, network);
1041 if (identifier == NULL)
1044 __connman_network_set_device(network, device);
1046 err = connman_element_register((struct connman_element *) network,
1049 __connman_network_set_device(network, NULL);
1053 g_hash_table_insert(device->networks, g_strdup(identifier),
1060 * connman_device_get_network:
1061 * @device: device structure
1062 * @identifier: network identifier
1064 * Get network for given identifier
1066 struct connman_network *connman_device_get_network(struct connman_device *device,
1067 const char *identifier)
1069 DBG("device %p identifier %s", device, identifier);
1071 return g_hash_table_lookup(device->networks, identifier);
1075 * connman_device_remove_network:
1076 * @device: device structure
1077 * @identifier: network identifier
1079 * Remove network for given identifier
1081 int connman_device_remove_network(struct connman_device *device,
1082 const char *identifier)
1084 DBG("device %p identifier %s", device, identifier);
1086 g_hash_table_remove(device->networks, identifier);
1091 void connman_device_remove_all_networks(struct connman_device *device)
1093 g_hash_table_remove_all(device->networks);
1096 void __connman_device_set_network(struct connman_device *device,
1097 struct connman_network *network)
1104 if (device->network == network)
1107 if (device->network != NULL)
1108 connman_network_unref(device->network);
1110 if (network != NULL) {
1111 name = connman_network_get_string(network,
1112 CONNMAN_PROPERTY_ID_NAME);
1113 g_free(device->last_network);
1114 device->last_network = g_strdup(name);
1116 device->network = connman_network_ref(network);
1118 g_free(device->last_network);
1119 device->last_network = NULL;
1121 device->network = NULL;
1125 void __connman_device_set_reconnect(struct connman_device *device,
1126 connman_bool_t reconnect)
1128 device->reconnect = reconnect;
1131 connman_bool_t __connman_device_get_reconnect(
1132 struct connman_device *device)
1134 return device->reconnect;
1138 * connman_device_register:
1139 * @device: device structure
1141 * Register device with the system
1143 int connman_device_register(struct connman_device *device)
1145 __connman_storage_load_device(device);
1147 device->offlinemode = __connman_profile_get_offlinemode();
1149 return connman_element_register(&device->element, NULL);
1153 * connman_device_unregister:
1154 * @device: device structure
1156 * Unregister device with the system
1158 void connman_device_unregister(struct connman_device *device)
1160 __connman_storage_save_device(device);
1162 connman_element_unregister(&device->element);
1166 * connman_device_get_data:
1167 * @device: device structure
1169 * Get private device data pointer
1171 void *connman_device_get_data(struct connman_device *device)
1173 return device->driver_data;
1177 * connman_device_set_data:
1178 * @device: device structure
1179 * @data: data pointer
1181 * Set private device data pointer
1183 void connman_device_set_data(struct connman_device *device, void *data)
1185 device->driver_data = data;
1188 static gboolean match_driver(struct connman_device *device,
1189 struct connman_device_driver *driver)
1191 if (device->type == driver->type ||
1192 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1198 static int device_probe(struct connman_element *element)
1200 struct connman_device *device = element->device;
1203 DBG("element %p name %s", element, element->name);
1208 if (device->driver != NULL)
1211 for (list = driver_list; list; list = list->next) {
1212 struct connman_device_driver *driver = list->data;
1214 if (match_driver(device, driver) == FALSE)
1217 DBG("driver %p name %s", driver, driver->name);
1219 if (driver->probe(device) == 0) {
1220 device->driver = driver;
1225 if (device->driver == NULL)
1228 return setup_device(device);
1231 static void device_remove(struct connman_element *element)
1233 struct connman_device *device = element->device;
1235 DBG("element %p name %s", element, element->name);
1240 if (device->driver == NULL)
1243 remove_device(device);
1246 static struct connman_driver device_driver = {
1248 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1249 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1250 .probe = device_probe,
1251 .remove = device_remove,
1254 static int device_load(struct connman_device *device)
1256 const char *ident = __connman_profile_active_ident();
1258 GError *error = NULL;
1260 connman_bool_t powered;
1263 DBG("device %p", device);
1265 keyfile = __connman_storage_open_profile(ident);
1266 if (keyfile == NULL)
1269 identifier = g_strdup_printf("device_%s", device->element.name);
1270 if (identifier == NULL)
1273 powered = g_key_file_get_boolean(keyfile, identifier,
1276 device->powered_persistent = powered;
1277 g_clear_error(&error);
1279 val = g_key_file_get_integer(keyfile, identifier,
1280 "ScanInterval", &error);
1282 device->scan_interval = val;
1283 g_clear_error(&error);
1288 __connman_storage_close_profile(ident, keyfile, FALSE);
1293 static int device_save(struct connman_device *device)
1295 const char *ident = __connman_profile_active_ident();
1299 DBG("device %p", device);
1301 keyfile = __connman_storage_open_profile(ident);
1302 if (keyfile == NULL)
1305 identifier = g_strdup_printf("device_%s", device->element.name);
1306 if (identifier == NULL)
1309 g_key_file_set_boolean(keyfile, identifier,
1310 "Powered", device->powered_persistent);
1312 g_key_file_set_integer(keyfile, identifier,
1313 "ScanInterval", device->scan_interval);
1318 __connman_storage_close_profile(ident, keyfile, TRUE);
1323 static struct connman_storage device_storage = {
1325 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1326 .device_load = device_load,
1327 .device_save = device_save,
1330 int __connman_device_init(void)
1334 if (connman_storage_register(&device_storage) < 0)
1335 connman_error("Failed to register device storage");
1337 return connman_driver_register(&device_driver);
1340 void __connman_device_cleanup(void)
1344 connman_driver_unregister(&device_driver);
1346 connman_storage_unregister(&device_storage);