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;
51 unsigned int connections;
54 struct connman_device_driver *driver;
58 struct connman_network *network;
62 #define SCAN_INITIAL_DELAY 10
64 static gboolean device_scan_trigger(gpointer user_data)
66 struct connman_device *device = user_data;
68 DBG("device %p", device);
70 if (device->driver == NULL) {
71 device->scan_timeout = 0;
75 if (device->driver->scan)
76 device->driver->scan(device);
81 static void clear_scan_trigger(struct connman_device *device)
83 if (device->scan_timeout > 0) {
84 g_source_remove(device->scan_timeout);
85 device->scan_timeout = 0;
89 static void reset_scan_trigger(struct connman_device *device)
91 clear_scan_trigger(device);
93 if (device->scan_interval > 0) {
96 if (g_hash_table_size(device->networks) == 0) {
97 if (device->backoff_interval >= device->scan_interval)
98 device->backoff_interval = SCAN_INITIAL_DELAY;
99 interval = device->backoff_interval;
101 interval = device->scan_interval;
103 DBG("interval %d", interval);
105 device->scan_timeout = g_timeout_add_seconds(interval,
106 device_scan_trigger, device);
108 device->backoff_interval *= 2;
109 if (device->backoff_interval > device->scan_interval)
110 device->backoff_interval = device->scan_interval;
114 static void force_scan_trigger(struct connman_device *device)
116 clear_scan_trigger(device);
118 device->scan_timeout = g_timeout_add_seconds(5,
119 device_scan_trigger, device);
122 void connman_device_schedule_scan(struct connman_device *device)
124 reset_scan_trigger(device);
127 static const char *type2description(enum connman_device_type type)
130 case CONNMAN_DEVICE_TYPE_UNKNOWN:
131 case CONNMAN_DEVICE_TYPE_VENDOR:
133 case CONNMAN_DEVICE_TYPE_ETHERNET:
135 case CONNMAN_DEVICE_TYPE_WIFI:
137 case CONNMAN_DEVICE_TYPE_WIMAX:
139 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
141 case CONNMAN_DEVICE_TYPE_GPS:
143 case CONNMAN_DEVICE_TYPE_CELLULAR:
145 case CONNMAN_DEVICE_TYPE_GADGET:
153 static const char *type2string(enum connman_device_type type)
156 case CONNMAN_DEVICE_TYPE_UNKNOWN:
157 case CONNMAN_DEVICE_TYPE_VENDOR:
159 case CONNMAN_DEVICE_TYPE_ETHERNET:
161 case CONNMAN_DEVICE_TYPE_WIFI:
163 case CONNMAN_DEVICE_TYPE_WIMAX:
165 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
167 case CONNMAN_DEVICE_TYPE_GPS:
169 case CONNMAN_DEVICE_TYPE_CELLULAR:
171 case CONNMAN_DEVICE_TYPE_GADGET:
179 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
181 enum connman_device_type type = connman_device_get_type(device);
184 case CONNMAN_DEVICE_TYPE_UNKNOWN:
185 case CONNMAN_DEVICE_TYPE_VENDOR:
186 case CONNMAN_DEVICE_TYPE_GPS:
188 case CONNMAN_DEVICE_TYPE_ETHERNET:
189 return CONNMAN_SERVICE_TYPE_ETHERNET;
190 case CONNMAN_DEVICE_TYPE_WIFI:
191 return CONNMAN_SERVICE_TYPE_WIFI;
192 case CONNMAN_DEVICE_TYPE_WIMAX:
193 return CONNMAN_SERVICE_TYPE_WIMAX;
194 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
195 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
196 case CONNMAN_DEVICE_TYPE_CELLULAR:
197 return CONNMAN_SERVICE_TYPE_CELLULAR;
198 case CONNMAN_DEVICE_TYPE_GADGET:
199 return CONNMAN_SERVICE_TYPE_GADGET;
203 return CONNMAN_SERVICE_TYPE_UNKNOWN;
206 int __connman_device_enable(struct connman_device *device)
209 enum connman_service_type type;
211 DBG("device %p %d", device, device->blocked);
213 if (!device->driver || !device->driver->enable)
216 if (device->powered_pending == TRUE)
219 if (device->blocked == TRUE)
222 connman_device_set_disconnected(device, FALSE);
223 device->scanning = FALSE;
225 err = device->driver->enable(device);
226 if (err < 0 && err != -EALREADY) {
227 if (err == -EINPROGRESS) {
228 device->powered_pending = TRUE;
229 device->offlinemode = FALSE;
230 if (__connman_profile_get_offlinemode() == TRUE)
231 __connman_profile_set_offlinemode(FALSE, FALSE);
236 device->powered_pending = TRUE;
237 device->powered = TRUE;
238 device->offlinemode = FALSE;
239 if (__connman_profile_get_offlinemode() == TRUE)
240 __connman_profile_set_offlinemode(FALSE, FALSE);
242 type = __connman_device_get_service_type(device);
243 __connman_technology_enable(type);
248 int __connman_device_disable(struct connman_device *device)
251 enum connman_service_type type;
253 DBG("device %p", device);
255 if (!device->driver || !device->driver->disable)
258 if (device->powered == FALSE)
261 if (device->powered_pending == FALSE)
264 device->reconnect = FALSE;
266 clear_scan_trigger(device);
268 g_hash_table_remove_all(device->networks);
270 err = device->driver->disable(device);
271 if (err < 0 && err != -EALREADY) {
272 if (err == -EINPROGRESS)
273 device->powered_pending = FALSE;
277 device->connections = 0;
279 device->powered_pending = FALSE;
280 device->powered = FALSE;
282 type = __connman_device_get_service_type(device);
283 __connman_technology_disable(type);
288 static int set_powered(struct connman_device *device, connman_bool_t powered)
290 DBG("device %p powered %d", device, powered);
293 return __connman_device_enable(device);
295 return __connman_device_disable(device);
298 static int setup_device(struct connman_device *device)
300 DBG("device %p", device);
302 __connman_technology_add_device(device);
304 if (device->offlinemode == FALSE &&
305 device->powered_persistent == TRUE)
306 __connman_device_enable(device);
311 static void probe_driver(struct connman_element *element, gpointer user_data)
313 struct connman_device_driver *driver = user_data;
315 DBG("element %p name %s", element, element->name);
317 if (element->device == NULL)
320 if (element->device->driver != NULL)
323 if (driver->type != element->device->type)
326 if (driver->probe(element->device) < 0)
329 element->device->driver = driver;
331 __connman_element_set_driver(element);
333 setup_device(element->device);
336 static void remove_device(struct connman_device *device)
338 DBG("device %p", device);
340 __connman_device_disable(device);
342 __connman_technology_remove_device(device);
344 if (device->driver->remove)
345 device->driver->remove(device);
347 device->driver = NULL;
350 static void remove_driver(struct connman_element *element, gpointer user_data)
352 struct connman_device_driver *driver = user_data;
354 DBG("element %p name %s", element, element->name);
356 if (element->device == NULL)
359 if (element->device->driver == driver)
360 remove_device(element->device);
363 connman_bool_t __connman_device_has_driver(struct connman_device *device)
365 if (device == NULL || device->driver == NULL)
371 static GSList *driver_list = NULL;
373 static gint compare_priority(gconstpointer a, gconstpointer b)
375 const struct connman_device_driver *driver1 = a;
376 const struct connman_device_driver *driver2 = b;
378 return driver2->priority - driver1->priority;
382 * connman_device_driver_register:
383 * @driver: device driver definition
385 * Register a new device driver
387 * Returns: %0 on success
389 int connman_device_driver_register(struct connman_device_driver *driver)
391 DBG("driver %p name %s", driver, driver->name);
393 driver_list = g_slist_insert_sorted(driver_list, driver,
396 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
397 probe_driver, driver);
403 * connman_device_driver_unregister:
404 * @driver: device driver definition
406 * Remove a previously registered device driver
408 void connman_device_driver_unregister(struct connman_device_driver *driver)
410 DBG("driver %p name %s", driver, driver->name);
412 driver_list = g_slist_remove(driver_list, driver);
414 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
415 remove_driver, driver);
418 static void unregister_network(gpointer data)
420 struct connman_network *network = data;
422 DBG("network %p", network);
424 connman_element_unregister((struct connman_element *) network);
426 __connman_network_set_device(network, NULL);
428 connman_network_unref(network);
431 static void device_destruct(struct connman_element *element)
433 struct connman_device *device = element->device;
435 DBG("element %p name %s", element, element->name);
437 clear_scan_trigger(device);
439 g_free(device->ident);
440 g_free(device->node);
441 g_free(device->name);
442 g_free(device->address);
443 g_free(device->interface);
444 g_free(device->path);
446 g_free(device->last_network);
448 g_hash_table_destroy(device->networks);
449 device->networks = NULL;
453 * connman_device_create:
454 * @node: device node name (for example an address)
457 * Allocate a new device of given #type and assign the #node name to it.
459 * Returns: a newly-allocated #connman_device structure
461 struct connman_device *connman_device_create(const char *node,
462 enum connman_device_type type)
464 struct connman_device *device;
466 enum connman_service_type service_type;
467 connman_bool_t bg_scan;
469 DBG("node %s type %d", node, type);
471 device = g_try_new0(struct connman_device, 1);
475 DBG("device %p", device);
477 bg_scan = connman_setting_get_bool("BackgroundScanning");
479 __connman_element_initialize(&device->element);
481 device->element.name = g_strdup(node);
482 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
484 device->element.device = device;
485 device->element.destruct = device_destruct;
487 str = type2string(type);
489 connman_element_set_string(&device->element,
490 CONNMAN_PROPERTY_ID_TYPE, str);
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);
956 } else if (g_str_equal(key, "Path") == TRUE) {
957 g_free(device->path);
958 device->path = g_strdup(value);
961 return connman_element_set_string(&device->element, key, value);
965 * connman_device_get_string:
966 * @device: device structure
967 * @key: unique identifier
969 * Get string value for specific key
971 const char *connman_device_get_string(struct connman_device *device,
974 DBG("device %p key %s", device, key);
976 if (g_str_equal(key, "Address") == TRUE)
977 return device->address;
978 else if (g_str_equal(key, "Name") == TRUE)
980 else if (g_str_equal(key, "Node") == TRUE)
982 else if (g_str_equal(key, "Interface") == TRUE)
983 return device->interface;
984 else if (g_str_equal(key, "Path") == TRUE)
987 return connman_element_get_string(&device->element, key);
990 static void set_offlinemode(struct connman_element *element, gpointer user_data)
992 struct connman_device *device = element->device;
993 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
994 connman_bool_t powered;
996 DBG("element %p name %s", element, element->name);
1001 device->offlinemode = offlinemode;
1003 if (device->blocked == TRUE)
1006 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1008 if (device->powered == powered)
1011 if (device->powered_persistent == FALSE)
1014 set_powered(device, powered);
1017 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1019 DBG("offlinmode %d", offlinemode);
1021 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1022 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1024 __connman_notifier_offlinemode(offlinemode);
1029 void __connman_device_increase_connections(struct connman_device *device)
1034 device->connections++;
1037 void __connman_device_decrease_connections(struct connman_device *device)
1042 device->connections--;
1044 if (device->connections == 0)
1045 device->backoff_interval = SCAN_INITIAL_DELAY;
1049 * connman_device_add_network:
1050 * @device: device structure
1051 * @network: network structure
1053 * Add new network to the device
1055 int connman_device_add_network(struct connman_device *device,
1056 struct connman_network *network)
1058 const char *identifier = connman_network_get_identifier(network);
1061 DBG("device %p network %p", device, network);
1063 if (identifier == NULL)
1066 __connman_network_set_device(network, device);
1068 err = connman_element_register((struct connman_element *) network,
1071 __connman_network_set_device(network, NULL);
1075 g_hash_table_insert(device->networks, g_strdup(identifier),
1082 * connman_device_get_network:
1083 * @device: device structure
1084 * @identifier: network identifier
1086 * Get network for given identifier
1088 struct connman_network *connman_device_get_network(struct connman_device *device,
1089 const char *identifier)
1091 DBG("device %p identifier %s", device, identifier);
1093 return g_hash_table_lookup(device->networks, identifier);
1097 * connman_device_remove_network:
1098 * @device: device structure
1099 * @identifier: network identifier
1101 * Remove network for given identifier
1103 int connman_device_remove_network(struct connman_device *device,
1104 const char *identifier)
1106 DBG("device %p identifier %s", device, identifier);
1108 g_hash_table_remove(device->networks, identifier);
1113 void connman_device_remove_all_networks(struct connman_device *device)
1115 g_hash_table_remove_all(device->networks);
1118 void __connman_device_set_network(struct connman_device *device,
1119 struct connman_network *network)
1126 if (device->network == network)
1129 if (device->network != NULL)
1130 connman_network_unref(device->network);
1132 if (network != NULL) {
1133 name = connman_network_get_string(network,
1134 CONNMAN_PROPERTY_ID_NAME);
1135 g_free(device->last_network);
1136 device->last_network = g_strdup(name);
1138 device->network = connman_network_ref(network);
1140 g_free(device->last_network);
1141 device->last_network = NULL;
1143 device->network = NULL;
1147 void __connman_device_set_reconnect(struct connman_device *device,
1148 connman_bool_t reconnect)
1150 device->reconnect = reconnect;
1153 connman_bool_t __connman_device_get_reconnect(
1154 struct connman_device *device)
1156 return device->reconnect;
1160 * connman_device_register:
1161 * @device: device structure
1163 * Register device with the system
1165 int connman_device_register(struct connman_device *device)
1167 __connman_storage_load_device(device);
1169 device->offlinemode = __connman_profile_get_offlinemode();
1171 return connman_element_register(&device->element, NULL);
1175 * connman_device_unregister:
1176 * @device: device structure
1178 * Unregister device with the system
1180 void connman_device_unregister(struct connman_device *device)
1182 __connman_storage_save_device(device);
1184 connman_element_unregister(&device->element);
1188 * connman_device_get_data:
1189 * @device: device structure
1191 * Get private device data pointer
1193 void *connman_device_get_data(struct connman_device *device)
1195 return device->driver_data;
1199 * connman_device_set_data:
1200 * @device: device structure
1201 * @data: data pointer
1203 * Set private device data pointer
1205 void connman_device_set_data(struct connman_device *device, void *data)
1207 device->driver_data = data;
1210 static gboolean match_driver(struct connman_device *device,
1211 struct connman_device_driver *driver)
1213 if (device->type == driver->type ||
1214 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1220 static int device_probe(struct connman_element *element)
1222 struct connman_device *device = element->device;
1225 DBG("element %p name %s", element, element->name);
1230 if (device->driver != NULL)
1233 for (list = driver_list; list; list = list->next) {
1234 struct connman_device_driver *driver = list->data;
1236 if (match_driver(device, driver) == FALSE)
1239 DBG("driver %p name %s", driver, driver->name);
1241 if (driver->probe(device) == 0) {
1242 device->driver = driver;
1247 if (device->driver == NULL)
1250 return setup_device(device);
1253 static void device_remove(struct connman_element *element)
1255 struct connman_device *device = element->device;
1257 DBG("element %p name %s", element, element->name);
1262 if (device->driver == NULL)
1265 remove_device(device);
1268 static struct connman_driver device_driver = {
1270 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1271 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1272 .probe = device_probe,
1273 .remove = device_remove,
1276 static int device_load(struct connman_device *device)
1278 const char *ident = __connman_profile_active_ident();
1280 GError *error = NULL;
1282 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);
1303 __connman_storage_close_profile(ident, keyfile, FALSE);
1308 static int device_save(struct connman_device *device)
1310 const char *ident = __connman_profile_active_ident();
1314 DBG("device %p", device);
1316 keyfile = __connman_storage_open_profile(ident);
1317 if (keyfile == NULL)
1320 identifier = g_strdup_printf("device_%s", device->element.name);
1321 if (identifier == NULL)
1324 g_key_file_set_boolean(keyfile, identifier,
1325 "Powered", device->powered_persistent);
1330 __connman_storage_close_profile(ident, keyfile, TRUE);
1335 static struct connman_storage device_storage = {
1337 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1338 .device_load = device_load,
1339 .device_save = device_save,
1342 int __connman_device_init(void)
1346 if (connman_storage_register(&device_storage) < 0)
1347 connman_error("Failed to register device storage");
1349 return connman_driver_register(&device_driver);
1352 void __connman_device_cleanup(void)
1356 connman_driver_unregister(&device_driver);
1358 connman_storage_unregister(&device_storage);