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 static GSList *device_list = NULL;
32 static gchar **device_filter = NULL;
33 static gchar **nodevice_filter = NULL;
35 struct connman_device {
37 enum connman_device_type type;
38 connman_bool_t offlinemode;
39 connman_bool_t blocked;
40 connman_bool_t powered;
41 connman_bool_t powered_pending;
42 connman_bool_t powered_persistent;
43 connman_bool_t scanning;
44 connman_bool_t disconnected;
45 connman_bool_t reconnect;
46 connman_uint16_t scan_interval;
47 connman_uint16_t backoff_interval;
57 unsigned int connections;
60 struct connman_device_driver *driver;
64 struct connman_network *network;
68 #define SCAN_INITIAL_DELAY 10
70 static gboolean device_scan_trigger(gpointer user_data)
72 struct connman_device *device = user_data;
74 DBG("device %p", device);
76 if (device->driver == NULL) {
77 device->scan_timeout = 0;
81 if (device->driver->scan)
82 device->driver->scan(device);
87 static void clear_scan_trigger(struct connman_device *device)
89 if (device->scan_timeout > 0) {
90 g_source_remove(device->scan_timeout);
91 device->scan_timeout = 0;
95 static void reset_scan_trigger(struct connman_device *device)
97 clear_scan_trigger(device);
99 if (device->scan_interval > 0) {
102 if (g_hash_table_size(device->networks) == 0) {
103 if (device->backoff_interval >= device->scan_interval)
104 device->backoff_interval = SCAN_INITIAL_DELAY;
105 interval = device->backoff_interval;
107 interval = device->scan_interval;
109 DBG("interval %d", interval);
111 device->scan_timeout = g_timeout_add_seconds(interval,
112 device_scan_trigger, device);
114 device->backoff_interval *= 2;
115 if (device->backoff_interval > device->scan_interval)
116 device->backoff_interval = device->scan_interval;
120 static void force_scan_trigger(struct connman_device *device)
122 clear_scan_trigger(device);
124 device->scan_timeout = g_timeout_add_seconds(5,
125 device_scan_trigger, device);
128 void connman_device_schedule_scan(struct connman_device *device)
130 reset_scan_trigger(device);
133 static const char *type2description(enum connman_device_type type)
136 case CONNMAN_DEVICE_TYPE_UNKNOWN:
137 case CONNMAN_DEVICE_TYPE_VENDOR:
139 case CONNMAN_DEVICE_TYPE_ETHERNET:
141 case CONNMAN_DEVICE_TYPE_WIFI:
143 case CONNMAN_DEVICE_TYPE_WIMAX:
145 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
147 case CONNMAN_DEVICE_TYPE_GPS:
149 case CONNMAN_DEVICE_TYPE_CELLULAR:
151 case CONNMAN_DEVICE_TYPE_GADGET:
159 static const char *type2string(enum connman_device_type type)
162 case CONNMAN_DEVICE_TYPE_UNKNOWN:
163 case CONNMAN_DEVICE_TYPE_VENDOR:
165 case CONNMAN_DEVICE_TYPE_ETHERNET:
167 case CONNMAN_DEVICE_TYPE_WIFI:
169 case CONNMAN_DEVICE_TYPE_WIMAX:
171 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_CELLULAR:
177 case CONNMAN_DEVICE_TYPE_GADGET:
185 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
187 enum connman_device_type type = connman_device_get_type(device);
190 case CONNMAN_DEVICE_TYPE_UNKNOWN:
191 case CONNMAN_DEVICE_TYPE_VENDOR:
192 case CONNMAN_DEVICE_TYPE_GPS:
194 case CONNMAN_DEVICE_TYPE_ETHERNET:
195 return CONNMAN_SERVICE_TYPE_ETHERNET;
196 case CONNMAN_DEVICE_TYPE_WIFI:
197 return CONNMAN_SERVICE_TYPE_WIFI;
198 case CONNMAN_DEVICE_TYPE_WIMAX:
199 return CONNMAN_SERVICE_TYPE_WIMAX;
200 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
201 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
202 case CONNMAN_DEVICE_TYPE_CELLULAR:
203 return CONNMAN_SERVICE_TYPE_CELLULAR;
204 case CONNMAN_DEVICE_TYPE_GADGET:
205 return CONNMAN_SERVICE_TYPE_GADGET;
209 return CONNMAN_SERVICE_TYPE_UNKNOWN;
212 int __connman_device_enable(struct connman_device *device)
215 enum connman_service_type type;
217 DBG("device %p %d", device, device->blocked);
219 if (!device->driver || !device->driver->enable)
222 if (device->powered_pending == TRUE)
225 if (device->blocked == TRUE)
228 connman_device_set_disconnected(device, FALSE);
229 device->scanning = FALSE;
231 err = device->driver->enable(device);
232 if (err < 0 && err != -EALREADY) {
233 if (err == -EINPROGRESS) {
234 device->powered_pending = TRUE;
235 device->offlinemode = FALSE;
236 if (__connman_profile_get_offlinemode() == TRUE)
237 __connman_profile_set_offlinemode(FALSE, FALSE);
242 device->powered_pending = TRUE;
243 device->powered = TRUE;
244 device->offlinemode = FALSE;
245 if (__connman_profile_get_offlinemode() == TRUE)
246 __connman_profile_set_offlinemode(FALSE, FALSE);
248 type = __connman_device_get_service_type(device);
249 __connman_technology_enable(type);
254 int __connman_device_disable(struct connman_device *device)
257 enum connman_service_type type;
259 DBG("device %p", device);
261 if (!device->driver || !device->driver->disable)
264 if (device->powered == FALSE)
267 if (device->powered_pending == FALSE)
270 device->reconnect = FALSE;
272 clear_scan_trigger(device);
274 g_hash_table_remove_all(device->networks);
276 err = device->driver->disable(device);
277 if (err < 0 && err != -EALREADY) {
278 if (err == -EINPROGRESS)
279 device->powered_pending = FALSE;
283 device->connections = 0;
285 device->powered_pending = FALSE;
286 device->powered = FALSE;
288 type = __connman_device_get_service_type(device);
289 __connman_technology_disable(type);
294 static int set_powered(struct connman_device *device, connman_bool_t powered)
296 DBG("device %p powered %d", device, powered);
299 return __connman_device_enable(device);
301 return __connman_device_disable(device);
304 static int setup_device(struct connman_device *device)
306 DBG("device %p", device);
308 __connman_technology_add_device(device);
310 if (device->offlinemode == FALSE &&
311 device->powered_persistent == TRUE)
312 __connman_device_enable(device);
317 static void probe_driver(struct connman_device_driver *driver)
321 DBG("driver %p name %s", driver, driver->name);
323 for (list = device_list; list != NULL; list = list->next) {
324 struct connman_device *device = list->data;
326 if (device->driver != NULL)
329 if (driver->type != device->type)
332 if (driver->probe(device) < 0)
335 device->driver = driver;
337 setup_device(device);
341 static void remove_device(struct connman_device *device)
343 DBG("device %p", device);
345 __connman_device_disable(device);
347 __connman_technology_remove_device(device);
349 if (device->driver->remove)
350 device->driver->remove(device);
352 device->driver = NULL;
355 static void remove_driver(struct connman_device_driver *driver)
359 DBG("driver %p name %s", driver, driver->name);
361 for (list = device_list; list != NULL; list = list->next) {
362 struct connman_device *device = list->data;
364 if (device->driver == driver)
365 remove_device(device);
369 connman_bool_t __connman_device_has_driver(struct connman_device *device)
371 if (device == NULL || device->driver == NULL)
377 static GSList *driver_list = NULL;
379 static gint compare_priority(gconstpointer a, gconstpointer b)
381 const struct connman_device_driver *driver1 = a;
382 const struct connman_device_driver *driver2 = b;
384 return driver2->priority - driver1->priority;
388 * connman_device_driver_register:
389 * @driver: device driver definition
391 * Register a new device driver
393 * Returns: %0 on success
395 int connman_device_driver_register(struct connman_device_driver *driver)
397 DBG("driver %p name %s", driver, driver->name);
399 driver_list = g_slist_insert_sorted(driver_list, driver,
401 probe_driver(driver);
407 * connman_device_driver_unregister:
408 * @driver: device driver definition
410 * Remove a previously registered device driver
412 void connman_device_driver_unregister(struct connman_device_driver *driver)
414 DBG("driver %p name %s", driver, driver->name);
416 driver_list = g_slist_remove(driver_list, driver);
418 remove_driver(driver);
421 static void unregister_network(gpointer data)
423 struct connman_network *network = data;
425 DBG("network %p", network);
427 connman_element_unregister((struct connman_element *) network);
429 __connman_network_set_device(network, NULL);
431 connman_network_unref(network);
434 static void device_destruct(struct connman_device *device)
436 DBG("device %p name %s", device, device->name);
438 clear_scan_trigger(device);
440 g_free(device->ident);
441 g_free(device->node);
442 g_free(device->name);
443 g_free(device->address);
444 g_free(device->interface);
445 g_free(device->path);
446 g_free(device->devname);
448 g_free(device->last_network);
450 g_hash_table_destroy(device->networks);
451 device->networks = NULL;
457 * connman_device_create:
458 * @node: device node name (for example an address)
461 * Allocate a new device of given #type and assign the #node name to it.
463 * Returns: a newly-allocated #connman_device structure
465 struct connman_device *connman_device_create(const char *node,
466 enum connman_device_type type)
468 struct connman_device *device;
469 enum connman_service_type service_type;
470 connman_bool_t bg_scan;
472 DBG("node %s type %d", node, type);
474 device = g_try_new0(struct connman_device, 1);
478 DBG("device %p", device);
480 device->refcount = 1;
482 bg_scan = connman_setting_get_bool("BackgroundScanning");
485 device->name = g_strdup(type2description(device->type));
487 device->powered_persistent = TRUE;
489 device->phyindex = -1;
491 service_type = __connman_device_get_service_type(device);
492 device->blocked = __connman_technology_get_blocked(service_type);
493 device->backoff_interval = SCAN_INITIAL_DELAY;
496 case CONNMAN_DEVICE_TYPE_UNKNOWN:
497 case CONNMAN_DEVICE_TYPE_ETHERNET:
498 case CONNMAN_DEVICE_TYPE_WIMAX:
499 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
500 case CONNMAN_DEVICE_TYPE_CELLULAR:
501 case CONNMAN_DEVICE_TYPE_GPS:
502 case CONNMAN_DEVICE_TYPE_GADGET:
503 case CONNMAN_DEVICE_TYPE_VENDOR:
504 device->scan_interval = 0;
506 case CONNMAN_DEVICE_TYPE_WIFI:
508 device->scan_interval = 300;
510 device->scan_interval = 0;
514 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
515 g_free, unregister_network);
517 device_list = g_slist_append(device_list, device);
523 * connman_device_ref:
524 * @device: device structure
526 * Increase reference counter of device
528 struct connman_device *connman_device_ref(struct connman_device *device)
532 g_atomic_int_inc(&device->refcount);
538 * connman_device_unref:
539 * @device: device structure
541 * Decrease reference counter of device
543 void connman_device_unref(struct connman_device *device)
545 if (g_atomic_int_dec_and_test(&device->refcount) == FALSE)
548 if (device->driver) {
549 device->driver->remove(device);
550 device->driver = NULL;
553 device_list = g_slist_remove(device_list, device);
555 device_destruct(device);
558 const char *__connman_device_get_type(struct connman_device *device)
560 return type2string(device->type);
564 * connman_device_get_type:
565 * @device: device structure
569 enum connman_device_type connman_device_get_type(struct connman_device *device)
575 * connman_device_set_index:
576 * @device: device structure
577 * @index: index number
579 * Set index number of device
581 void connman_device_set_index(struct connman_device *device, int index)
583 device->index = index;
587 * connman_device_get_index:
588 * @device: device structure
590 * Get index number of device
592 int connman_device_get_index(struct connman_device *device)
594 return device->index;
597 int __connman_device_get_phyindex(struct connman_device *device)
599 return device->phyindex;
602 void __connman_device_set_phyindex(struct connman_device *device,
605 device->phyindex = phyindex;
609 * connman_device_set_interface:
610 * @device: device structure
611 * @interface: interface name
613 * Set interface name of device
615 void connman_device_set_interface(struct connman_device *device,
616 const char *interface)
618 g_free(device->devname);
619 device->devname = g_strdup(interface);
621 g_free(device->interface);
622 device->interface = g_strdup(interface);
624 if (device->name == NULL) {
625 const char *str = type2description(device->type);
626 if (str != NULL && device->interface != NULL)
627 device->name = g_strdup_printf("%s (%s)", str,
633 * connman_device_set_ident:
634 * @device: device structure
635 * @ident: unique identifier
637 * Set unique identifier of device
639 void connman_device_set_ident(struct connman_device *device,
642 g_free(device->ident);
643 device->ident = g_strdup(ident);
646 const char *connman_device_get_ident(struct connman_device *device)
648 return device->ident;
652 * connman_device_set_powered:
653 * @device: device structure
654 * @powered: powered state
656 * Change power state of device
658 int connman_device_set_powered(struct connman_device *device,
659 connman_bool_t powered)
662 enum connman_service_type type;
664 DBG("driver %p powered %d", device, powered);
666 if (device->powered == powered) {
667 device->powered_pending = powered;
672 err = __connman_device_enable(device);
674 err = __connman_device_disable(device);
676 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
679 device->powered = powered;
680 device->powered_pending = powered;
682 type = __connman_device_get_service_type(device);
684 if (device->powered == TRUE)
685 __connman_technology_enable(type);
687 __connman_technology_disable(type);
689 if (device->offlinemode == TRUE && powered == TRUE)
690 return connman_device_set_powered(device, FALSE);
692 if (powered == FALSE)
695 reset_scan_trigger(device);
697 if (device->driver && device->driver->scan)
698 device->driver->scan(device);
703 int __connman_device_set_blocked(struct connman_device *device,
704 connman_bool_t blocked)
706 connman_bool_t powered;
708 DBG("device %p blocked %d", device, blocked);
710 device->blocked = blocked;
712 if (device->offlinemode == TRUE)
715 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
717 if (blocked == FALSE)
718 powered = device->powered_persistent;
722 return set_powered(device, powered);
725 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
727 return device->blocked;
730 int __connman_device_scan(struct connman_device *device)
732 if (!device->driver || !device->driver->scan)
735 if (device->powered == FALSE)
738 reset_scan_trigger(device);
740 return device->driver->scan(device);
743 int __connman_device_enable_persistent(struct connman_device *device)
747 DBG("device %p", device);
749 device->powered_persistent = TRUE;
751 __connman_storage_save_device(device);
753 err = __connman_device_enable(device);
754 if (err == 0 || err == -EINPROGRESS) {
755 device->offlinemode = FALSE;
756 if (__connman_profile_get_offlinemode() == TRUE) {
757 __connman_profile_set_offlinemode(FALSE, FALSE);
759 __connman_profile_save_default();
766 int __connman_device_disable_persistent(struct connman_device *device)
768 DBG("device %p", device);
770 device->powered_persistent = FALSE;
772 __connman_storage_save_device(device);
774 return __connman_device_disable(device);
777 int __connman_device_disconnect(struct connman_device *device)
782 DBG("device %p", device);
784 connman_device_set_disconnected(device, TRUE);
786 g_hash_table_iter_init(&iter, device->networks);
788 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
789 struct connman_network *network = value;
791 if (connman_network_get_connecting(network) == TRUE) {
793 * Skip network in the process of connecting.
794 * This is a workaround for WiFi networks serviced
795 * by the supplicant plugin that hold a reference
796 * to the network. If we disconnect the network
797 * here then the referenced object will not be
798 * registered and usage (like launching DHCP client)
799 * will fail. There is nothing to be gained by
800 * removing the network here anyway.
802 connman_warn("Skipping disconnect of %s, network is connecting.",
803 connman_network_get_identifier(network));
807 __connman_network_disconnect(network);
813 static void mark_network_available(gpointer key, gpointer value,
816 struct connman_network *network = value;
818 connman_network_set_available(network, TRUE);
821 static void mark_network_unavailable(gpointer key, gpointer value,
824 struct connman_network *network = value;
826 if (connman_network_get_connected(network) == TRUE)
829 connman_network_set_available(network, FALSE);
832 static gboolean remove_unavailable_network(gpointer key, gpointer value,
835 struct connman_network *network = value;
837 if (connman_network_get_connected(network) == TRUE)
840 if (connman_network_get_available(network) == TRUE)
846 void __connman_device_cleanup_networks(struct connman_device *device)
848 g_hash_table_foreach_remove(device->networks,
849 remove_unavailable_network, NULL);
852 connman_bool_t __connman_device_scanning(struct connman_device *device)
854 return device->scanning;
857 void connman_device_reset_scanning(struct connman_device *device)
859 device->scanning = FALSE;
861 g_hash_table_foreach(device->networks,
862 mark_network_available, NULL);
867 * connman_device_set_scanning:
868 * @device: device structure
869 * @scanning: scanning state
871 * Change scanning state of device
873 int connman_device_set_scanning(struct connman_device *device,
874 connman_bool_t scanning)
876 DBG("device %p scanning %d", device, scanning);
878 if (!device->driver || !device->driver->scan)
881 if (device->scanning == scanning)
884 device->scanning = scanning;
886 if (scanning == TRUE) {
887 reset_scan_trigger(device);
889 g_hash_table_foreach(device->networks,
890 mark_network_unavailable, NULL);
895 __connman_device_cleanup_networks(device);
897 if (device->connections > 0)
900 __connman_service_auto_connect();
906 * connman_device_set_disconnected:
907 * @device: device structure
908 * @disconnected: disconnected state
910 * Change disconnected state of device (only for device with networks)
912 int connman_device_set_disconnected(struct connman_device *device,
913 connman_bool_t disconnected)
915 DBG("device %p disconnected %d", device, disconnected);
917 if (device->disconnected == disconnected)
920 device->disconnected = disconnected;
922 if (disconnected == TRUE)
923 force_scan_trigger(device);
929 * connman_device_get_disconnected:
930 * @device: device structure
932 * Get device disconnected state
934 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
936 return device->disconnected;
940 * connman_device_set_string:
941 * @device: device structure
942 * @key: unique identifier
943 * @value: string value
945 * Set string value for specific key
947 int connman_device_set_string(struct connman_device *device,
948 const char *key, const char *value)
950 DBG("device %p key %s value %s", device, key, value);
952 if (g_str_equal(key, "Address") == TRUE) {
953 g_free(device->address);
954 device->address = g_strdup(value);
955 } else if (g_str_equal(key, "Name") == TRUE) {
956 g_free(device->name);
957 device->name = g_strdup(value);
958 } else if (g_str_equal(key, "Node") == TRUE) {
959 g_free(device->node);
960 device->node = g_strdup(value);
961 } else if (g_str_equal(key, "Path") == TRUE) {
962 g_free(device->path);
963 device->path = g_strdup(value);
972 * connman_device_get_string:
973 * @device: device structure
974 * @key: unique identifier
976 * Get string value for specific key
978 const char *connman_device_get_string(struct connman_device *device,
981 DBG("device %p key %s", device, key);
983 if (g_str_equal(key, "Address") == TRUE)
984 return device->address;
985 else if (g_str_equal(key, "Name") == TRUE)
987 else if (g_str_equal(key, "Node") == TRUE)
989 else if (g_str_equal(key, "Interface") == TRUE)
990 return device->interface;
991 else if (g_str_equal(key, "Path") == TRUE)
997 static void set_offlinemode(struct connman_device *device,
998 connman_bool_t offlinemode)
1000 connman_bool_t powered;
1002 DBG("device %p name %s", device, device->name);
1007 device->offlinemode = offlinemode;
1009 if (device->blocked == TRUE)
1012 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1014 if (device->powered == powered)
1017 if (device->powered_persistent == FALSE)
1020 set_powered(device, powered);
1023 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1027 DBG("offlinmode %d", offlinemode);
1029 for (list = device_list; list != NULL; list = list->next) {
1030 struct connman_device *device = list->data;
1032 set_offlinemode(device, offlinemode);
1035 __connman_notifier_offlinemode(offlinemode);
1040 void __connman_device_increase_connections(struct connman_device *device)
1045 device->connections++;
1048 void __connman_device_decrease_connections(struct connman_device *device)
1053 device->connections--;
1055 if (device->connections == 0)
1056 device->backoff_interval = SCAN_INITIAL_DELAY;
1060 * connman_device_add_network:
1061 * @device: device structure
1062 * @network: network structure
1064 * Add new network to the device
1066 int connman_device_add_network(struct connman_device *device,
1067 struct connman_network *network)
1069 const char *identifier = connman_network_get_identifier(network);
1072 DBG("device %p network %p", device, network);
1074 if (identifier == NULL)
1077 __connman_network_set_device(network, device);
1079 err = connman_element_register((struct connman_element *) network,
1082 __connman_network_set_device(network, NULL);
1086 g_hash_table_insert(device->networks, g_strdup(identifier),
1093 * connman_device_get_network:
1094 * @device: device structure
1095 * @identifier: network identifier
1097 * Get network for given identifier
1099 struct connman_network *connman_device_get_network(struct connman_device *device,
1100 const char *identifier)
1102 DBG("device %p identifier %s", device, identifier);
1104 return g_hash_table_lookup(device->networks, identifier);
1108 * connman_device_remove_network:
1109 * @device: device structure
1110 * @identifier: network identifier
1112 * Remove network for given identifier
1114 int connman_device_remove_network(struct connman_device *device,
1115 const char *identifier)
1117 struct connman_network *network;
1119 DBG("device %p identifier %s", device, identifier);
1121 network = connman_device_get_network(device, identifier);
1122 if (network == NULL)
1125 connman_element_unregister((struct connman_element *) network);
1126 g_hash_table_remove(device->networks, identifier);
1131 void connman_device_remove_all_networks(struct connman_device *device)
1133 g_hash_table_remove_all(device->networks);
1136 void __connman_device_set_network(struct connman_device *device,
1137 struct connman_network *network)
1144 if (device->network == network)
1147 if (device->network != NULL)
1148 connman_network_unref(device->network);
1150 if (network != NULL) {
1151 name = connman_network_get_string(network,
1152 CONNMAN_PROPERTY_ID_NAME);
1153 g_free(device->last_network);
1154 device->last_network = g_strdup(name);
1156 device->network = connman_network_ref(network);
1158 g_free(device->last_network);
1159 device->last_network = NULL;
1161 device->network = NULL;
1165 void __connman_device_set_reconnect(struct connman_device *device,
1166 connman_bool_t reconnect)
1168 device->reconnect = reconnect;
1171 connman_bool_t __connman_device_get_reconnect(
1172 struct connman_device *device)
1174 return device->reconnect;
1177 static gboolean match_driver(struct connman_device *device,
1178 struct connman_device_driver *driver)
1180 if (device->type == driver->type ||
1181 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1187 static int device_probe(struct connman_device *device)
1191 DBG("device %p name %s", device, device->name);
1193 if (device->driver != NULL)
1196 for (list = driver_list; list; list = list->next) {
1197 struct connman_device_driver *driver = list->data;
1199 if (match_driver(device, driver) == FALSE)
1202 DBG("driver %p name %s", driver, driver->name);
1204 if (driver->probe(device) == 0) {
1205 device->driver = driver;
1210 if (device->driver == NULL)
1213 return setup_device(device);
1216 static void device_remove(struct connman_device *device)
1218 DBG("device %p name %s", device, device->name);
1220 if (device->driver == NULL)
1223 remove_device(device);
1227 * connman_device_register:
1228 * @device: device structure
1230 * Register device with the system
1232 int connman_device_register(struct connman_device *device)
1234 __connman_storage_load_device(device);
1236 device->offlinemode = __connman_profile_get_offlinemode();
1238 return device_probe(device);
1242 * connman_device_unregister:
1243 * @device: device structure
1245 * Unregister device with the system
1247 void connman_device_unregister(struct connman_device *device)
1249 __connman_storage_save_device(device);
1251 device_remove(device);
1255 * connman_device_get_data:
1256 * @device: device structure
1258 * Get private device data pointer
1260 void *connman_device_get_data(struct connman_device *device)
1262 return device->driver_data;
1266 * connman_device_set_data:
1267 * @device: device structure
1268 * @data: data pointer
1270 * Set private device data pointer
1272 void connman_device_set_data(struct connman_device *device, void *data)
1274 device->driver_data = data;
1277 struct connman_device *__connman_device_find_device(
1278 enum connman_service_type type)
1282 for (list = device_list; list != NULL; list = list->next) {
1283 struct connman_device *device = list->data;
1284 enum connman_service_type service_type =
1285 connman_device_get_type(device);
1287 if (service_type != type)
1296 int __connman_device_request_scan(enum connman_service_type type)
1302 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1303 case CONNMAN_SERVICE_TYPE_SYSTEM:
1304 case CONNMAN_SERVICE_TYPE_ETHERNET:
1305 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1306 case CONNMAN_SERVICE_TYPE_CELLULAR:
1307 case CONNMAN_SERVICE_TYPE_GPS:
1308 case CONNMAN_SERVICE_TYPE_VPN:
1309 case CONNMAN_SERVICE_TYPE_GADGET:
1311 case CONNMAN_SERVICE_TYPE_WIFI:
1312 case CONNMAN_SERVICE_TYPE_WIMAX:
1316 for (list = device_list; list != NULL; list = list->next) {
1317 struct connman_device *device = list->data;
1318 enum connman_service_type service_type =
1319 __connman_device_get_service_type(device);
1321 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1322 service_type != type) {
1326 err = __connman_device_scan(device);
1327 if (err < 0 && err != -EINPROGRESS) {
1329 /* XXX maybe only a continue? */
1337 static int set_technology(enum connman_service_type type, connman_bool_t enable)
1342 DBG("type %d enable %d", type, enable);
1345 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1346 case CONNMAN_SERVICE_TYPE_SYSTEM:
1347 case CONNMAN_SERVICE_TYPE_GPS:
1348 case CONNMAN_SERVICE_TYPE_VPN:
1349 case CONNMAN_SERVICE_TYPE_GADGET:
1351 case CONNMAN_SERVICE_TYPE_ETHERNET:
1352 case CONNMAN_SERVICE_TYPE_WIFI:
1353 case CONNMAN_SERVICE_TYPE_WIMAX:
1354 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1355 case CONNMAN_SERVICE_TYPE_CELLULAR:
1359 for (list = device_list; list != NULL; list = list->next) {
1360 struct connman_device *device = list->data;
1361 enum connman_service_type service_type =
1362 __connman_device_get_service_type(device);
1364 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1365 service_type != type) {
1370 err = __connman_device_enable_persistent(device);
1372 err = __connman_device_disable_persistent(device);
1374 if (err < 0 && err != -EINPROGRESS) {
1376 /* XXX maybe only a continue? */
1384 int __connman_device_enable_technology(enum connman_service_type type)
1386 return set_technology(type, TRUE);
1389 int __connman_device_disable_technology(enum connman_service_type type)
1391 return set_technology(type, FALSE);
1394 connman_bool_t __connman_device_isfiltered(const char *devname)
1398 if (device_filter == NULL)
1401 for (pattern = device_filter; *pattern; pattern++) {
1402 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1403 DBG("ignoring device %s (match)", devname);
1409 if (nodevice_filter == NULL)
1412 for (pattern = nodevice_filter; *pattern; pattern++) {
1413 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1414 DBG("ignoring device %s (no match)", devname);
1422 static int device_load(struct connman_device *device)
1424 const char *ident = __connman_profile_active_ident();
1426 GError *error = NULL;
1428 connman_bool_t powered;
1430 DBG("device %p", device);
1432 keyfile = __connman_storage_open_profile(ident);
1433 if (keyfile == NULL)
1436 identifier = g_strdup_printf("device_%s", device->name);
1437 if (identifier == NULL)
1440 powered = g_key_file_get_boolean(keyfile, identifier,
1443 device->powered_persistent = powered;
1444 g_clear_error(&error);
1449 __connman_storage_close_profile(ident, keyfile, FALSE);
1454 static int device_save(struct connman_device *device)
1456 const char *ident = __connman_profile_active_ident();
1460 DBG("device %p", device);
1462 keyfile = __connman_storage_open_profile(ident);
1463 if (keyfile == NULL)
1466 identifier = g_strdup_printf("device_%s", device->name);
1467 if (identifier == NULL)
1470 g_key_file_set_boolean(keyfile, identifier,
1471 "Powered", device->powered_persistent);
1476 __connman_storage_close_profile(ident, keyfile, TRUE);
1481 static struct connman_storage device_storage = {
1483 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1484 .device_load = device_load,
1485 .device_save = device_save,
1488 int __connman_device_init(const char *device, const char *nodevice)
1493 device_filter = g_strsplit(device, ",", -1);
1495 if (nodevice != NULL)
1496 nodevice_filter = g_strsplit(nodevice, ",", -1);
1498 return connman_storage_register(&device_storage);
1501 void __connman_device_cleanup(void)
1505 g_strfreev(nodevice_filter);
1506 g_strfreev(device_filter);
1508 connman_storage_unregister(&device_storage);