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 free_network(gpointer data)
423 struct connman_network *network = data;
425 DBG("network %p", network);
427 __connman_network_set_device(network, NULL);
429 connman_network_unref(network);
432 static void device_destruct(struct connman_device *device)
434 DBG("device %p name %s", device, device->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);
443 g_free(device->path);
444 g_free(device->devname);
446 g_free(device->last_network);
448 g_hash_table_destroy(device->networks);
449 device->networks = NULL;
455 * connman_device_create:
456 * @node: device node name (for example an address)
459 * Allocate a new device of given #type and assign the #node name to it.
461 * Returns: a newly-allocated #connman_device structure
463 struct connman_device *connman_device_create(const char *node,
464 enum connman_device_type type)
466 struct connman_device *device;
467 enum connman_service_type service_type;
468 connman_bool_t bg_scan;
470 DBG("node %s type %d", node, type);
472 device = g_try_new0(struct connman_device, 1);
476 DBG("device %p", device);
478 device->refcount = 1;
480 bg_scan = connman_setting_get_bool("BackgroundScanning");
483 device->name = g_strdup(type2description(device->type));
485 device->powered_persistent = TRUE;
487 device->phyindex = -1;
489 service_type = __connman_device_get_service_type(device);
490 device->blocked = __connman_technology_get_blocked(service_type);
491 device->backoff_interval = SCAN_INITIAL_DELAY;
494 case CONNMAN_DEVICE_TYPE_UNKNOWN:
495 case CONNMAN_DEVICE_TYPE_ETHERNET:
496 case CONNMAN_DEVICE_TYPE_WIMAX:
497 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
498 case CONNMAN_DEVICE_TYPE_CELLULAR:
499 case CONNMAN_DEVICE_TYPE_GPS:
500 case CONNMAN_DEVICE_TYPE_GADGET:
501 case CONNMAN_DEVICE_TYPE_VENDOR:
502 device->scan_interval = 0;
504 case CONNMAN_DEVICE_TYPE_WIFI:
506 device->scan_interval = 300;
508 device->scan_interval = 0;
512 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
513 g_free, free_network);
515 device_list = g_slist_append(device_list, device);
521 * connman_device_ref:
522 * @device: device structure
524 * Increase reference counter of device
526 struct connman_device *connman_device_ref(struct connman_device *device)
530 g_atomic_int_inc(&device->refcount);
536 * connman_device_unref:
537 * @device: device structure
539 * Decrease reference counter of device
541 void connman_device_unref(struct connman_device *device)
543 if (g_atomic_int_dec_and_test(&device->refcount) == FALSE)
546 if (device->driver) {
547 device->driver->remove(device);
548 device->driver = NULL;
551 device_list = g_slist_remove(device_list, device);
553 device_destruct(device);
556 const char *__connman_device_get_type(struct connman_device *device)
558 return type2string(device->type);
562 * connman_device_get_type:
563 * @device: device structure
567 enum connman_device_type connman_device_get_type(struct connman_device *device)
573 * connman_device_set_index:
574 * @device: device structure
575 * @index: index number
577 * Set index number of device
579 void connman_device_set_index(struct connman_device *device, int index)
581 device->index = index;
585 * connman_device_get_index:
586 * @device: device structure
588 * Get index number of device
590 int connman_device_get_index(struct connman_device *device)
592 return device->index;
595 int __connman_device_get_phyindex(struct connman_device *device)
597 return device->phyindex;
600 void __connman_device_set_phyindex(struct connman_device *device,
603 device->phyindex = phyindex;
607 * connman_device_set_interface:
608 * @device: device structure
609 * @interface: interface name
611 * Set interface name of device
613 void connman_device_set_interface(struct connman_device *device,
614 const char *interface)
616 g_free(device->devname);
617 device->devname = g_strdup(interface);
619 g_free(device->interface);
620 device->interface = g_strdup(interface);
622 if (device->name == NULL) {
623 const char *str = type2description(device->type);
624 if (str != NULL && device->interface != NULL)
625 device->name = g_strdup_printf("%s (%s)", str,
631 * connman_device_set_ident:
632 * @device: device structure
633 * @ident: unique identifier
635 * Set unique identifier of device
637 void connman_device_set_ident(struct connman_device *device,
640 g_free(device->ident);
641 device->ident = g_strdup(ident);
644 const char *connman_device_get_ident(struct connman_device *device)
646 return device->ident;
650 * connman_device_set_powered:
651 * @device: device structure
652 * @powered: powered state
654 * Change power state of device
656 int connman_device_set_powered(struct connman_device *device,
657 connman_bool_t powered)
660 enum connman_service_type type;
662 DBG("driver %p powered %d", device, powered);
664 if (device->powered == powered) {
665 device->powered_pending = powered;
670 err = __connman_device_enable(device);
672 err = __connman_device_disable(device);
674 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
677 device->powered = powered;
678 device->powered_pending = powered;
680 type = __connman_device_get_service_type(device);
682 if (device->powered == TRUE)
683 __connman_technology_enable(type);
685 __connman_technology_disable(type);
687 if (device->offlinemode == TRUE && powered == TRUE)
688 return connman_device_set_powered(device, FALSE);
690 if (powered == FALSE)
693 reset_scan_trigger(device);
695 if (device->driver && device->driver->scan)
696 device->driver->scan(device);
701 int __connman_device_set_blocked(struct connman_device *device,
702 connman_bool_t blocked)
704 connman_bool_t powered;
706 DBG("device %p blocked %d", device, blocked);
708 device->blocked = blocked;
710 if (device->offlinemode == TRUE)
713 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
715 if (blocked == FALSE)
716 powered = device->powered_persistent;
720 return set_powered(device, powered);
723 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
725 return device->blocked;
728 int __connman_device_scan(struct connman_device *device)
730 if (!device->driver || !device->driver->scan)
733 if (device->powered == FALSE)
736 reset_scan_trigger(device);
738 return device->driver->scan(device);
741 int __connman_device_enable_persistent(struct connman_device *device)
745 DBG("device %p", device);
747 device->powered_persistent = TRUE;
749 __connman_storage_save_device(device);
751 err = __connman_device_enable(device);
752 if (err == 0 || err == -EINPROGRESS) {
753 device->offlinemode = FALSE;
754 if (__connman_profile_get_offlinemode() == TRUE) {
755 __connman_profile_set_offlinemode(FALSE, FALSE);
757 __connman_profile_save_default();
764 int __connman_device_disable_persistent(struct connman_device *device)
766 DBG("device %p", device);
768 device->powered_persistent = FALSE;
770 __connman_storage_save_device(device);
772 return __connman_device_disable(device);
775 int __connman_device_disconnect(struct connman_device *device)
780 DBG("device %p", device);
782 connman_device_set_disconnected(device, TRUE);
784 g_hash_table_iter_init(&iter, device->networks);
786 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
787 struct connman_network *network = value;
789 if (connman_network_get_connecting(network) == TRUE) {
791 * Skip network in the process of connecting.
792 * This is a workaround for WiFi networks serviced
793 * by the supplicant plugin that hold a reference
794 * to the network. If we disconnect the network
795 * here then the referenced object will not be
796 * registered and usage (like launching DHCP client)
797 * will fail. There is nothing to be gained by
798 * removing the network here anyway.
800 connman_warn("Skipping disconnect of %s, network is connecting.",
801 connman_network_get_identifier(network));
805 __connman_network_disconnect(network);
811 static void mark_network_available(gpointer key, gpointer value,
814 struct connman_network *network = value;
816 connman_network_set_available(network, TRUE);
819 static void mark_network_unavailable(gpointer key, gpointer value,
822 struct connman_network *network = value;
824 if (connman_network_get_connected(network) == TRUE)
827 connman_network_set_available(network, FALSE);
830 static gboolean remove_unavailable_network(gpointer key, gpointer value,
833 struct connman_network *network = value;
835 if (connman_network_get_connected(network) == TRUE)
838 if (connman_network_get_available(network) == TRUE)
844 void __connman_device_cleanup_networks(struct connman_device *device)
846 g_hash_table_foreach_remove(device->networks,
847 remove_unavailable_network, NULL);
850 connman_bool_t __connman_device_scanning(struct connman_device *device)
852 return device->scanning;
855 void connman_device_reset_scanning(struct connman_device *device)
857 device->scanning = FALSE;
859 g_hash_table_foreach(device->networks,
860 mark_network_available, NULL);
865 * connman_device_set_scanning:
866 * @device: device structure
867 * @scanning: scanning state
869 * Change scanning state of device
871 int connman_device_set_scanning(struct connman_device *device,
872 connman_bool_t scanning)
874 DBG("device %p scanning %d", device, scanning);
876 if (!device->driver || !device->driver->scan)
879 if (device->scanning == scanning)
882 device->scanning = scanning;
884 if (scanning == TRUE) {
885 reset_scan_trigger(device);
887 g_hash_table_foreach(device->networks,
888 mark_network_unavailable, NULL);
893 __connman_device_cleanup_networks(device);
895 if (device->connections > 0)
898 __connman_service_auto_connect();
904 * connman_device_set_disconnected:
905 * @device: device structure
906 * @disconnected: disconnected state
908 * Change disconnected state of device (only for device with networks)
910 int connman_device_set_disconnected(struct connman_device *device,
911 connman_bool_t disconnected)
913 DBG("device %p disconnected %d", device, disconnected);
915 if (device->disconnected == disconnected)
918 device->disconnected = disconnected;
920 if (disconnected == TRUE)
921 force_scan_trigger(device);
927 * connman_device_get_disconnected:
928 * @device: device structure
930 * Get device disconnected state
932 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
934 return device->disconnected;
938 * connman_device_set_string:
939 * @device: device structure
940 * @key: unique identifier
941 * @value: string value
943 * Set string value for specific key
945 int connman_device_set_string(struct connman_device *device,
946 const char *key, const char *value)
948 DBG("device %p key %s value %s", device, key, value);
950 if (g_str_equal(key, "Address") == TRUE) {
951 g_free(device->address);
952 device->address = g_strdup(value);
953 } else if (g_str_equal(key, "Name") == TRUE) {
954 g_free(device->name);
955 device->name = g_strdup(value);
956 } else if (g_str_equal(key, "Node") == TRUE) {
957 g_free(device->node);
958 device->node = g_strdup(value);
959 } else if (g_str_equal(key, "Path") == TRUE) {
960 g_free(device->path);
961 device->path = g_strdup(value);
970 * connman_device_get_string:
971 * @device: device structure
972 * @key: unique identifier
974 * Get string value for specific key
976 const char *connman_device_get_string(struct connman_device *device,
979 DBG("device %p key %s", device, key);
981 if (g_str_equal(key, "Address") == TRUE)
982 return device->address;
983 else if (g_str_equal(key, "Name") == TRUE)
985 else if (g_str_equal(key, "Node") == TRUE)
987 else if (g_str_equal(key, "Interface") == TRUE)
988 return device->interface;
989 else if (g_str_equal(key, "Path") == TRUE)
995 static void set_offlinemode(struct connman_device *device,
996 connman_bool_t offlinemode)
998 connman_bool_t powered;
1000 DBG("device %p name %s", device, device->name);
1005 device->offlinemode = offlinemode;
1007 if (device->blocked == TRUE)
1010 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1012 if (device->powered == powered)
1015 if (device->powered_persistent == FALSE)
1018 set_powered(device, powered);
1021 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1025 DBG("offlinmode %d", offlinemode);
1027 for (list = device_list; list != NULL; list = list->next) {
1028 struct connman_device *device = list->data;
1030 set_offlinemode(device, offlinemode);
1033 __connman_notifier_offlinemode(offlinemode);
1038 void __connman_device_increase_connections(struct connman_device *device)
1043 device->connections++;
1046 void __connman_device_decrease_connections(struct connman_device *device)
1051 device->connections--;
1053 if (device->connections == 0)
1054 device->backoff_interval = SCAN_INITIAL_DELAY;
1058 * connman_device_add_network:
1059 * @device: device structure
1060 * @network: network structure
1062 * Add new network to the device
1064 int connman_device_add_network(struct connman_device *device,
1065 struct connman_network *network)
1067 const char *identifier = connman_network_get_identifier(network);
1069 DBG("device %p network %p", device, network);
1071 if (identifier == NULL)
1074 connman_network_ref(network);
1076 __connman_network_set_device(network, device);
1078 g_hash_table_insert(device->networks, g_strdup(identifier),
1085 * connman_device_get_network:
1086 * @device: device structure
1087 * @identifier: network identifier
1089 * Get network for given identifier
1091 struct connman_network *connman_device_get_network(struct connman_device *device,
1092 const char *identifier)
1094 DBG("device %p identifier %s", device, identifier);
1096 return g_hash_table_lookup(device->networks, identifier);
1100 * connman_device_remove_network:
1101 * @device: device structure
1102 * @identifier: network identifier
1104 * Remove network for given identifier
1106 int connman_device_remove_network(struct connman_device *device,
1107 struct connman_network *network)
1109 struct connman_service *service;
1110 const char *identifier;
1112 DBG("device %p network %p", device, network);
1114 if (network == NULL)
1117 service = __connman_service_lookup_from_network(network);
1119 identifier = connman_network_get_identifier(network);
1120 g_hash_table_remove(device->networks, identifier);
1122 if (service != NULL)
1123 __connman_service_reset_from_networks(service, device->networks);
1128 void connman_device_remove_all_networks(struct connman_device *device)
1130 g_hash_table_remove_all(device->networks);
1133 void __connman_device_set_network(struct connman_device *device,
1134 struct connman_network *network)
1141 if (device->network == network)
1144 if (network != NULL) {
1145 name = connman_network_get_string(network, "Name");
1146 g_free(device->last_network);
1147 device->last_network = g_strdup(name);
1149 device->network = network;
1151 g_free(device->last_network);
1152 device->last_network = NULL;
1154 device->network = NULL;
1158 void __connman_device_set_reconnect(struct connman_device *device,
1159 connman_bool_t reconnect)
1161 device->reconnect = reconnect;
1164 connman_bool_t __connman_device_get_reconnect(
1165 struct connman_device *device)
1167 return device->reconnect;
1170 static gboolean match_driver(struct connman_device *device,
1171 struct connman_device_driver *driver)
1173 if (device->type == driver->type ||
1174 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1180 static int device_probe(struct connman_device *device)
1184 DBG("device %p name %s", device, device->name);
1186 if (device->driver != NULL)
1189 for (list = driver_list; list; list = list->next) {
1190 struct connman_device_driver *driver = list->data;
1192 if (match_driver(device, driver) == FALSE)
1195 DBG("driver %p name %s", driver, driver->name);
1197 if (driver->probe(device) == 0) {
1198 device->driver = driver;
1203 if (device->driver == NULL)
1206 return setup_device(device);
1209 static void device_remove(struct connman_device *device)
1211 DBG("device %p name %s", device, device->name);
1213 if (device->driver == NULL)
1216 remove_device(device);
1220 * connman_device_register:
1221 * @device: device structure
1223 * Register device with the system
1225 int connman_device_register(struct connman_device *device)
1227 __connman_storage_load_device(device);
1229 device->offlinemode = __connman_profile_get_offlinemode();
1231 return device_probe(device);
1235 * connman_device_unregister:
1236 * @device: device structure
1238 * Unregister device with the system
1240 void connman_device_unregister(struct connman_device *device)
1242 __connman_storage_save_device(device);
1244 device_remove(device);
1248 * connman_device_get_data:
1249 * @device: device structure
1251 * Get private device data pointer
1253 void *connman_device_get_data(struct connman_device *device)
1255 return device->driver_data;
1259 * connman_device_set_data:
1260 * @device: device structure
1261 * @data: data pointer
1263 * Set private device data pointer
1265 void connman_device_set_data(struct connman_device *device, void *data)
1267 device->driver_data = data;
1270 struct connman_device *__connman_device_find_device(
1271 enum connman_service_type type)
1275 for (list = device_list; list != NULL; list = list->next) {
1276 struct connman_device *device = list->data;
1277 enum connman_service_type service_type =
1278 __connman_device_get_service_type(device);
1280 if (service_type != type)
1289 int __connman_device_request_scan(enum connman_service_type type)
1295 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1296 case CONNMAN_SERVICE_TYPE_SYSTEM:
1297 case CONNMAN_SERVICE_TYPE_ETHERNET:
1298 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1299 case CONNMAN_SERVICE_TYPE_CELLULAR:
1300 case CONNMAN_SERVICE_TYPE_GPS:
1301 case CONNMAN_SERVICE_TYPE_VPN:
1302 case CONNMAN_SERVICE_TYPE_GADGET:
1304 case CONNMAN_SERVICE_TYPE_WIFI:
1305 case CONNMAN_SERVICE_TYPE_WIMAX:
1309 for (list = device_list; list != NULL; list = list->next) {
1310 struct connman_device *device = list->data;
1311 enum connman_service_type service_type =
1312 __connman_device_get_service_type(device);
1314 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1315 service_type != type) {
1319 err = __connman_device_scan(device);
1320 if (err < 0 && err != -EINPROGRESS) {
1322 /* XXX maybe only a continue? */
1330 static int set_technology(enum connman_service_type type, connman_bool_t enable)
1335 DBG("type %d enable %d", type, enable);
1338 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1339 case CONNMAN_SERVICE_TYPE_SYSTEM:
1340 case CONNMAN_SERVICE_TYPE_GPS:
1341 case CONNMAN_SERVICE_TYPE_VPN:
1342 case CONNMAN_SERVICE_TYPE_GADGET:
1344 case CONNMAN_SERVICE_TYPE_ETHERNET:
1345 case CONNMAN_SERVICE_TYPE_WIFI:
1346 case CONNMAN_SERVICE_TYPE_WIMAX:
1347 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1348 case CONNMAN_SERVICE_TYPE_CELLULAR:
1352 for (list = device_list; list != NULL; list = list->next) {
1353 struct connman_device *device = list->data;
1354 enum connman_service_type service_type =
1355 __connman_device_get_service_type(device);
1357 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1358 service_type != type) {
1363 err = __connman_device_enable_persistent(device);
1365 err = __connman_device_disable_persistent(device);
1367 if (err < 0 && err != -EINPROGRESS) {
1369 /* XXX maybe only a continue? */
1377 int __connman_device_enable_technology(enum connman_service_type type)
1379 return set_technology(type, TRUE);
1382 int __connman_device_disable_technology(enum connman_service_type type)
1384 return set_technology(type, FALSE);
1387 connman_bool_t __connman_device_isfiltered(const char *devname)
1391 if (device_filter == NULL)
1394 for (pattern = device_filter; *pattern; pattern++) {
1395 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1396 DBG("ignoring device %s (match)", devname);
1402 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1403 DBG("ignoring dummy networking devices");
1407 if (nodevice_filter == NULL)
1410 for (pattern = nodevice_filter; *pattern; pattern++) {
1411 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1412 DBG("ignoring device %s (no match)", devname);
1420 static int device_load(struct connman_device *device)
1422 const char *ident = __connman_profile_active_ident();
1424 GError *error = NULL;
1426 connman_bool_t powered;
1428 DBG("device %p", device);
1430 keyfile = __connman_storage_open_profile(ident);
1431 if (keyfile == NULL)
1434 identifier = g_strdup_printf("device_%s", device->name);
1435 if (identifier == NULL)
1438 powered = g_key_file_get_boolean(keyfile, identifier,
1441 device->powered_persistent = powered;
1442 g_clear_error(&error);
1447 __connman_storage_close_profile(ident, keyfile, FALSE);
1452 static int device_save(struct connman_device *device)
1454 const char *ident = __connman_profile_active_ident();
1458 DBG("device %p", device);
1460 keyfile = __connman_storage_open_profile(ident);
1461 if (keyfile == NULL)
1464 identifier = g_strdup_printf("device_%s", device->name);
1465 if (identifier == NULL)
1468 g_key_file_set_boolean(keyfile, identifier,
1469 "Powered", device->powered_persistent);
1474 __connman_storage_close_profile(ident, keyfile, TRUE);
1479 static struct connman_storage device_storage = {
1481 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1482 .device_load = device_load,
1483 .device_save = device_save,
1486 int __connman_device_init(const char *device, const char *nodevice)
1491 device_filter = g_strsplit(device, ",", -1);
1493 if (nodevice != NULL)
1494 nodevice_filter = g_strsplit(nodevice, ",", -1);
1496 return connman_storage_register(&device_storage);
1499 void __connman_device_cleanup(void)
1503 g_strfreev(nodevice_filter);
1504 g_strfreev(device_filter);
1506 connman_storage_unregister(&device_storage);