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_network_set_device(network, NULL);
429 connman_network_unregister(network);
430 connman_network_unref(network);
433 static void device_destruct(struct connman_device *device)
435 DBG("device %p name %s", device, device->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);
445 g_free(device->devname);
447 g_free(device->last_network);
449 g_hash_table_destroy(device->networks);
450 device->networks = NULL;
456 * connman_device_create:
457 * @node: device node name (for example an address)
460 * Allocate a new device of given #type and assign the #node name to it.
462 * Returns: a newly-allocated #connman_device structure
464 struct connman_device *connman_device_create(const char *node,
465 enum connman_device_type type)
467 struct connman_device *device;
468 enum connman_service_type service_type;
469 connman_bool_t bg_scan;
471 DBG("node %s type %d", node, type);
473 device = g_try_new0(struct connman_device, 1);
477 DBG("device %p", device);
479 device->refcount = 1;
481 bg_scan = connman_setting_get_bool("BackgroundScanning");
484 device->name = g_strdup(type2description(device->type));
486 device->powered_persistent = TRUE;
488 device->phyindex = -1;
490 service_type = __connman_device_get_service_type(device);
491 device->blocked = __connman_technology_get_blocked(service_type);
492 device->backoff_interval = SCAN_INITIAL_DELAY;
495 case CONNMAN_DEVICE_TYPE_UNKNOWN:
496 case CONNMAN_DEVICE_TYPE_ETHERNET:
497 case CONNMAN_DEVICE_TYPE_WIMAX:
498 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
499 case CONNMAN_DEVICE_TYPE_CELLULAR:
500 case CONNMAN_DEVICE_TYPE_GPS:
501 case CONNMAN_DEVICE_TYPE_GADGET:
502 case CONNMAN_DEVICE_TYPE_VENDOR:
503 device->scan_interval = 0;
505 case CONNMAN_DEVICE_TYPE_WIFI:
507 device->scan_interval = 300;
509 device->scan_interval = 0;
513 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
514 g_free, unregister_network);
516 device_list = g_slist_append(device_list, device);
522 * connman_device_ref:
523 * @device: device structure
525 * Increase reference counter of device
527 struct connman_device *connman_device_ref(struct connman_device *device)
531 g_atomic_int_inc(&device->refcount);
537 * connman_device_unref:
538 * @device: device structure
540 * Decrease reference counter of device
542 void connman_device_unref(struct connman_device *device)
544 if (g_atomic_int_dec_and_test(&device->refcount) == FALSE)
547 if (device->driver) {
548 device->driver->remove(device);
549 device->driver = NULL;
552 device_list = g_slist_remove(device_list, device);
554 device_destruct(device);
557 const char *__connman_device_get_type(struct connman_device *device)
559 return type2string(device->type);
563 * connman_device_get_type:
564 * @device: device structure
568 enum connman_device_type connman_device_get_type(struct connman_device *device)
574 * connman_device_set_index:
575 * @device: device structure
576 * @index: index number
578 * Set index number of device
580 void connman_device_set_index(struct connman_device *device, int index)
582 device->index = index;
586 * connman_device_get_index:
587 * @device: device structure
589 * Get index number of device
591 int connman_device_get_index(struct connman_device *device)
593 return device->index;
596 int __connman_device_get_phyindex(struct connman_device *device)
598 return device->phyindex;
601 void __connman_device_set_phyindex(struct connman_device *device,
604 device->phyindex = phyindex;
608 * connman_device_set_interface:
609 * @device: device structure
610 * @interface: interface name
612 * Set interface name of device
614 void connman_device_set_interface(struct connman_device *device,
615 const char *interface)
617 g_free(device->devname);
618 device->devname = g_strdup(interface);
620 g_free(device->interface);
621 device->interface = g_strdup(interface);
623 if (device->name == NULL) {
624 const char *str = type2description(device->type);
625 if (str != NULL && device->interface != NULL)
626 device->name = g_strdup_printf("%s (%s)", str,
632 * connman_device_set_ident:
633 * @device: device structure
634 * @ident: unique identifier
636 * Set unique identifier of device
638 void connman_device_set_ident(struct connman_device *device,
641 g_free(device->ident);
642 device->ident = g_strdup(ident);
645 const char *connman_device_get_ident(struct connman_device *device)
647 return device->ident;
651 * connman_device_set_powered:
652 * @device: device structure
653 * @powered: powered state
655 * Change power state of device
657 int connman_device_set_powered(struct connman_device *device,
658 connman_bool_t powered)
661 enum connman_service_type type;
663 DBG("driver %p powered %d", device, powered);
665 if (device->powered == powered) {
666 device->powered_pending = powered;
671 err = __connman_device_enable(device);
673 err = __connman_device_disable(device);
675 if (err < 0 && err != -EINPROGRESS && err != -EALREADY)
678 device->powered = powered;
679 device->powered_pending = powered;
681 type = __connman_device_get_service_type(device);
683 if (device->powered == TRUE)
684 __connman_technology_enable(type);
686 __connman_technology_disable(type);
688 if (device->offlinemode == TRUE && powered == TRUE)
689 return connman_device_set_powered(device, FALSE);
691 if (powered == FALSE)
694 reset_scan_trigger(device);
696 if (device->driver && device->driver->scan)
697 device->driver->scan(device);
702 int __connman_device_set_blocked(struct connman_device *device,
703 connman_bool_t blocked)
705 connman_bool_t powered;
707 DBG("device %p blocked %d", device, blocked);
709 device->blocked = blocked;
711 if (device->offlinemode == TRUE)
714 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
716 if (blocked == FALSE)
717 powered = device->powered_persistent;
721 return set_powered(device, powered);
724 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
726 return device->blocked;
729 int __connman_device_scan(struct connman_device *device)
731 if (!device->driver || !device->driver->scan)
734 if (device->powered == FALSE)
737 reset_scan_trigger(device);
739 return device->driver->scan(device);
742 int __connman_device_enable_persistent(struct connman_device *device)
746 DBG("device %p", device);
748 device->powered_persistent = TRUE;
750 __connman_storage_save_device(device);
752 err = __connman_device_enable(device);
753 if (err == 0 || err == -EINPROGRESS) {
754 device->offlinemode = FALSE;
755 if (__connman_profile_get_offlinemode() == TRUE) {
756 __connman_profile_set_offlinemode(FALSE, FALSE);
758 __connman_profile_save_default();
765 int __connman_device_disable_persistent(struct connman_device *device)
767 DBG("device %p", device);
769 device->powered_persistent = FALSE;
771 __connman_storage_save_device(device);
773 return __connman_device_disable(device);
776 int __connman_device_disconnect(struct connman_device *device)
781 DBG("device %p", device);
783 connman_device_set_disconnected(device, TRUE);
785 g_hash_table_iter_init(&iter, device->networks);
787 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
788 struct connman_network *network = value;
790 if (connman_network_get_connecting(network) == TRUE) {
792 * Skip network in the process of connecting.
793 * This is a workaround for WiFi networks serviced
794 * by the supplicant plugin that hold a reference
795 * to the network. If we disconnect the network
796 * here then the referenced object will not be
797 * registered and usage (like launching DHCP client)
798 * will fail. There is nothing to be gained by
799 * removing the network here anyway.
801 connman_warn("Skipping disconnect of %s, network is connecting.",
802 connman_network_get_identifier(network));
806 __connman_network_disconnect(network);
812 static void mark_network_available(gpointer key, gpointer value,
815 struct connman_network *network = value;
817 connman_network_set_available(network, TRUE);
820 static void mark_network_unavailable(gpointer key, gpointer value,
823 struct connman_network *network = value;
825 if (connman_network_get_connected(network) == TRUE)
828 connman_network_set_available(network, FALSE);
831 static gboolean remove_unavailable_network(gpointer key, gpointer value,
834 struct connman_network *network = value;
836 if (connman_network_get_connected(network) == TRUE)
839 if (connman_network_get_available(network) == TRUE)
845 void __connman_device_cleanup_networks(struct connman_device *device)
847 g_hash_table_foreach_remove(device->networks,
848 remove_unavailable_network, NULL);
851 connman_bool_t __connman_device_scanning(struct connman_device *device)
853 return device->scanning;
856 void connman_device_reset_scanning(struct connman_device *device)
858 device->scanning = FALSE;
860 g_hash_table_foreach(device->networks,
861 mark_network_available, NULL);
866 * connman_device_set_scanning:
867 * @device: device structure
868 * @scanning: scanning state
870 * Change scanning state of device
872 int connman_device_set_scanning(struct connman_device *device,
873 connman_bool_t scanning)
875 DBG("device %p scanning %d", device, scanning);
877 if (!device->driver || !device->driver->scan)
880 if (device->scanning == scanning)
883 device->scanning = scanning;
885 if (scanning == TRUE) {
886 reset_scan_trigger(device);
888 g_hash_table_foreach(device->networks,
889 mark_network_unavailable, NULL);
894 __connman_device_cleanup_networks(device);
896 if (device->connections > 0)
899 __connman_service_auto_connect();
905 * connman_device_set_disconnected:
906 * @device: device structure
907 * @disconnected: disconnected state
909 * Change disconnected state of device (only for device with networks)
911 int connman_device_set_disconnected(struct connman_device *device,
912 connman_bool_t disconnected)
914 DBG("device %p disconnected %d", device, disconnected);
916 if (device->disconnected == disconnected)
919 device->disconnected = disconnected;
921 if (disconnected == TRUE)
922 force_scan_trigger(device);
928 * connman_device_get_disconnected:
929 * @device: device structure
931 * Get device disconnected state
933 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
935 return device->disconnected;
939 * connman_device_set_string:
940 * @device: device structure
941 * @key: unique identifier
942 * @value: string value
944 * Set string value for specific key
946 int connman_device_set_string(struct connman_device *device,
947 const char *key, const char *value)
949 DBG("device %p key %s value %s", device, key, value);
951 if (g_str_equal(key, "Address") == TRUE) {
952 g_free(device->address);
953 device->address = g_strdup(value);
954 } else if (g_str_equal(key, "Name") == TRUE) {
955 g_free(device->name);
956 device->name = g_strdup(value);
957 } else if (g_str_equal(key, "Node") == TRUE) {
958 g_free(device->node);
959 device->node = g_strdup(value);
960 } else if (g_str_equal(key, "Path") == TRUE) {
961 g_free(device->path);
962 device->path = g_strdup(value);
971 * connman_device_get_string:
972 * @device: device structure
973 * @key: unique identifier
975 * Get string value for specific key
977 const char *connman_device_get_string(struct connman_device *device,
980 DBG("device %p key %s", device, key);
982 if (g_str_equal(key, "Address") == TRUE)
983 return device->address;
984 else if (g_str_equal(key, "Name") == TRUE)
986 else if (g_str_equal(key, "Node") == TRUE)
988 else if (g_str_equal(key, "Interface") == TRUE)
989 return device->interface;
990 else if (g_str_equal(key, "Path") == TRUE)
996 static void set_offlinemode(struct connman_device *device,
997 connman_bool_t offlinemode)
999 connman_bool_t powered;
1001 DBG("device %p name %s", device, device->name);
1006 device->offlinemode = offlinemode;
1008 if (device->blocked == TRUE)
1011 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1013 if (device->powered == powered)
1016 if (device->powered_persistent == FALSE)
1019 set_powered(device, powered);
1022 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1026 DBG("offlinmode %d", offlinemode);
1028 for (list = device_list; list != NULL; list = list->next) {
1029 struct connman_device *device = list->data;
1031 set_offlinemode(device, offlinemode);
1034 __connman_notifier_offlinemode(offlinemode);
1039 void __connman_device_increase_connections(struct connman_device *device)
1044 device->connections++;
1047 void __connman_device_decrease_connections(struct connman_device *device)
1052 device->connections--;
1054 if (device->connections == 0)
1055 device->backoff_interval = SCAN_INITIAL_DELAY;
1059 * connman_device_add_network:
1060 * @device: device structure
1061 * @network: network structure
1063 * Add new network to the device
1065 int connman_device_add_network(struct connman_device *device,
1066 struct connman_network *network)
1068 const char *identifier = connman_network_get_identifier(network);
1070 DBG("device %p network %p", device, network);
1072 if (identifier == NULL)
1075 connman_network_ref(network);
1077 __connman_network_set_device(network, device);
1079 g_hash_table_insert(device->networks, g_strdup(identifier),
1086 * connman_device_get_network:
1087 * @device: device structure
1088 * @identifier: network identifier
1090 * Get network for given identifier
1092 struct connman_network *connman_device_get_network(struct connman_device *device,
1093 const char *identifier)
1095 DBG("device %p identifier %s", device, identifier);
1097 return g_hash_table_lookup(device->networks, identifier);
1101 * connman_device_remove_network:
1102 * @device: device structure
1103 * @identifier: network identifier
1105 * Remove network for given identifier
1107 int connman_device_remove_network(struct connman_device *device,
1108 const char *identifier)
1110 struct connman_network *network;
1112 DBG("device %p identifier %s", device, identifier);
1114 network = connman_device_get_network(device, identifier);
1115 if (network == NULL)
1118 __connman_network_set_device(network, NULL);
1120 g_hash_table_remove(device->networks, identifier);
1125 void connman_device_remove_all_networks(struct connman_device *device)
1127 g_hash_table_remove_all(device->networks);
1130 void __connman_device_set_network(struct connman_device *device,
1131 struct connman_network *network)
1138 if (device->network == network)
1141 if (device->network != NULL)
1142 connman_network_unref(device->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 = connman_network_ref(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_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 (nodevice_filter == NULL)
1405 for (pattern = nodevice_filter; *pattern; pattern++) {
1406 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1407 DBG("ignoring device %s (no match)", devname);
1415 static int device_load(struct connman_device *device)
1417 const char *ident = __connman_profile_active_ident();
1419 GError *error = NULL;
1421 connman_bool_t powered;
1423 DBG("device %p", device);
1425 keyfile = __connman_storage_open_profile(ident);
1426 if (keyfile == NULL)
1429 identifier = g_strdup_printf("device_%s", device->name);
1430 if (identifier == NULL)
1433 powered = g_key_file_get_boolean(keyfile, identifier,
1436 device->powered_persistent = powered;
1437 g_clear_error(&error);
1442 __connman_storage_close_profile(ident, keyfile, FALSE);
1447 static int device_save(struct connman_device *device)
1449 const char *ident = __connman_profile_active_ident();
1453 DBG("device %p", device);
1455 keyfile = __connman_storage_open_profile(ident);
1456 if (keyfile == NULL)
1459 identifier = g_strdup_printf("device_%s", device->name);
1460 if (identifier == NULL)
1463 g_key_file_set_boolean(keyfile, identifier,
1464 "Powered", device->powered_persistent);
1469 __connman_storage_close_profile(ident, keyfile, TRUE);
1474 static struct connman_storage device_storage = {
1476 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1477 .device_load = device_load,
1478 .device_save = device_save,
1481 int __connman_device_init(const char *device, const char *nodevice)
1486 device_filter = g_strsplit(device, ",", -1);
1488 if (nodevice != NULL)
1489 nodevice_filter = g_strsplit(nodevice, ",", -1);
1491 return connman_storage_register(&device_storage);
1494 void __connman_device_cleanup(void)
1498 g_strfreev(nodevice_filter);
1499 g_strfreev(device_filter);
1501 connman_storage_unregister(&device_storage);