5 * Copyright (C) 2007-2012 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 enum connman_pending_type {
41 struct connman_device {
43 enum connman_device_type type;
44 enum connman_pending_type powered_pending; /* Indicates a pending
45 enable/disable request */
46 connman_bool_t powered;
47 connman_bool_t scanning;
48 connman_bool_t disconnected;
49 connman_bool_t reconnect;
59 guint pending_timeout;
61 struct connman_device_driver *driver;
65 struct connman_network *network;
69 static void clear_pending_trigger(struct connman_device *device)
71 if (device->pending_timeout > 0) {
72 g_source_remove(device->pending_timeout);
73 device->pending_timeout = 0;
77 static const char *type2description(enum connman_device_type type)
80 case CONNMAN_DEVICE_TYPE_UNKNOWN:
81 case CONNMAN_DEVICE_TYPE_VENDOR:
83 case CONNMAN_DEVICE_TYPE_ETHERNET:
85 case CONNMAN_DEVICE_TYPE_WIFI:
87 case CONNMAN_DEVICE_TYPE_WIMAX:
89 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
91 case CONNMAN_DEVICE_TYPE_GPS:
93 case CONNMAN_DEVICE_TYPE_CELLULAR:
95 case CONNMAN_DEVICE_TYPE_GADGET:
103 static const char *type2string(enum connman_device_type type)
106 case CONNMAN_DEVICE_TYPE_UNKNOWN:
107 case CONNMAN_DEVICE_TYPE_VENDOR:
109 case CONNMAN_DEVICE_TYPE_ETHERNET:
111 case CONNMAN_DEVICE_TYPE_WIFI:
113 case CONNMAN_DEVICE_TYPE_WIMAX:
115 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
117 case CONNMAN_DEVICE_TYPE_GPS:
119 case CONNMAN_DEVICE_TYPE_CELLULAR:
121 case CONNMAN_DEVICE_TYPE_GADGET:
129 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
131 enum connman_device_type type = connman_device_get_type(device);
134 case CONNMAN_DEVICE_TYPE_UNKNOWN:
135 case CONNMAN_DEVICE_TYPE_VENDOR:
136 case CONNMAN_DEVICE_TYPE_GPS:
138 case CONNMAN_DEVICE_TYPE_ETHERNET:
139 return CONNMAN_SERVICE_TYPE_ETHERNET;
140 case CONNMAN_DEVICE_TYPE_WIFI:
141 return CONNMAN_SERVICE_TYPE_WIFI;
142 case CONNMAN_DEVICE_TYPE_WIMAX:
143 return CONNMAN_SERVICE_TYPE_WIMAX;
144 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
145 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
146 case CONNMAN_DEVICE_TYPE_CELLULAR:
147 return CONNMAN_SERVICE_TYPE_CELLULAR;
148 case CONNMAN_DEVICE_TYPE_GADGET:
149 return CONNMAN_SERVICE_TYPE_GADGET;
153 return CONNMAN_SERVICE_TYPE_UNKNOWN;
156 static gboolean device_pending_reset(gpointer user_data)
158 struct connman_device *device = user_data;
160 DBG("device %p", device);
162 /* Power request timedout, reset power pending state. */
163 device->pending_timeout = 0;
164 device->powered_pending = PENDING_NONE;
169 int __connman_device_enable(struct connman_device *device)
173 DBG("device %p", device);
175 if (!device->driver || !device->driver->enable)
178 /* There is an ongoing power disable request. */
179 if (device->powered_pending == PENDING_DISABLE)
182 if (device->powered_pending == PENDING_ENABLE)
185 if (device->powered_pending == PENDING_NONE && device->powered == TRUE)
188 device->powered_pending = PENDING_ENABLE;
190 err = device->driver->enable(device);
192 * device gets enabled right away.
193 * Invoke the callback
196 connman_device_set_powered(device, TRUE);
200 if (err == -EALREADY) {
201 /* If device is already powered, but connman is not updated */
202 connman_device_set_powered(device, TRUE);
206 * if err == -EINPROGRESS, then the DBus call to the respective daemon
207 * was successful. We set a 4 sec timeout so if the daemon never
208 * returns a reply, we would reset the pending request.
210 if (err == -EINPROGRESS)
211 device->pending_timeout = g_timeout_add_seconds(4,
212 device_pending_reset, device);
217 int __connman_device_disable(struct connman_device *device)
221 DBG("device %p", device);
223 if (!device->driver || !device->driver->disable)
226 /* Ongoing power enable request */
227 if (device->powered_pending == PENDING_ENABLE)
230 if (device->powered_pending == PENDING_DISABLE)
233 if (device->powered_pending == PENDING_NONE && device->powered == FALSE)
236 device->powered_pending = PENDING_DISABLE;
237 device->reconnect = FALSE;
239 if (device->network) {
240 struct connman_service *service =
241 __connman_service_lookup_from_network(device->network);
244 __connman_service_disconnect(service);
246 connman_network_set_connected(device->network, FALSE);
249 err = device->driver->disable(device);
250 if (err == 0 || err == -EALREADY) {
251 connman_device_set_powered(device, FALSE);
255 if (err == -EINPROGRESS)
256 device->pending_timeout = g_timeout_add_seconds(4,
257 device_pending_reset, device);
262 static void probe_driver(struct connman_device_driver *driver)
266 DBG("driver %p name %s", driver, driver->name);
268 for (list = device_list; list != NULL; list = list->next) {
269 struct connman_device *device = list->data;
271 if (device->driver != NULL)
274 if (driver->type != device->type)
277 if (driver->probe(device) < 0)
280 device->driver = driver;
282 __connman_technology_add_device(device);
286 static void remove_device(struct connman_device *device)
288 DBG("device %p", device);
290 __connman_device_disable(device);
292 __connman_technology_remove_device(device);
294 if (device->driver->remove)
295 device->driver->remove(device);
297 device->driver = NULL;
300 static void remove_driver(struct connman_device_driver *driver)
304 DBG("driver %p name %s", driver, driver->name);
306 for (list = device_list; list != NULL; list = list->next) {
307 struct connman_device *device = list->data;
309 if (device->driver == driver)
310 remove_device(device);
314 connman_bool_t __connman_device_has_driver(struct connman_device *device)
316 if (device == NULL || device->driver == NULL)
322 static GSList *driver_list = NULL;
324 static gint compare_priority(gconstpointer a, gconstpointer b)
326 const struct connman_device_driver *driver1 = a;
327 const struct connman_device_driver *driver2 = b;
329 return driver2->priority - driver1->priority;
333 * connman_device_driver_register:
334 * @driver: device driver definition
336 * Register a new device driver
338 * Returns: %0 on success
340 int connman_device_driver_register(struct connman_device_driver *driver)
342 DBG("driver %p name %s", driver, driver->name);
344 driver_list = g_slist_insert_sorted(driver_list, driver,
346 probe_driver(driver);
352 * connman_device_driver_unregister:
353 * @driver: device driver definition
355 * Remove a previously registered device driver
357 void connman_device_driver_unregister(struct connman_device_driver *driver)
359 DBG("driver %p name %s", driver, driver->name);
361 driver_list = g_slist_remove(driver_list, driver);
363 remove_driver(driver);
366 static void free_network(gpointer data)
368 struct connman_network *network = data;
370 DBG("network %p", network);
372 __connman_network_set_device(network, NULL);
374 connman_network_unref(network);
377 static void device_destruct(struct connman_device *device)
379 DBG("device %p name %s", device, device->name);
381 clear_pending_trigger(device);
383 g_free(device->ident);
384 g_free(device->node);
385 g_free(device->name);
386 g_free(device->address);
387 g_free(device->interface);
388 g_free(device->path);
389 g_free(device->devname);
391 g_free(device->last_network);
393 g_hash_table_destroy(device->networks);
394 device->networks = NULL;
400 * connman_device_create:
401 * @node: device node name (for example an address)
404 * Allocate a new device of given #type and assign the #node name to it.
406 * Returns: a newly-allocated #connman_device structure
408 struct connman_device *connman_device_create(const char *node,
409 enum connman_device_type type)
411 struct connman_device *device;
413 DBG("node %s type %d", node, type);
415 device = g_try_new0(struct connman_device, 1);
419 DBG("device %p", device);
421 device->refcount = 1;
424 device->name = g_strdup(type2description(device->type));
426 device->phyindex = -1;
428 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
429 g_free, free_network);
431 device_list = g_slist_append(device_list, device);
437 * connman_device_ref:
438 * @device: device structure
440 * Increase reference counter of device
442 struct connman_device *connman_device_ref_debug(struct connman_device *device,
443 const char *file, int line, const char *caller)
445 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
448 __sync_fetch_and_add(&device->refcount, 1);
454 * connman_device_unref:
455 * @device: device structure
457 * Decrease reference counter of device
459 void connman_device_unref_debug(struct connman_device *device,
460 const char *file, int line, const char *caller)
462 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
465 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
468 if (device->driver) {
469 device->driver->remove(device);
470 device->driver = NULL;
473 device_list = g_slist_remove(device_list, device);
475 device_destruct(device);
478 const char *__connman_device_get_type(struct connman_device *device)
480 return type2string(device->type);
484 * connman_device_get_type:
485 * @device: device structure
489 enum connman_device_type connman_device_get_type(struct connman_device *device)
495 * connman_device_set_index:
496 * @device: device structure
497 * @index: index number
499 * Set index number of device
501 void connman_device_set_index(struct connman_device *device, int index)
503 device->index = index;
507 * connman_device_get_index:
508 * @device: device structure
510 * Get index number of device
512 int connman_device_get_index(struct connman_device *device)
514 return device->index;
517 int __connman_device_get_phyindex(struct connman_device *device)
519 return device->phyindex;
522 void __connman_device_set_phyindex(struct connman_device *device,
525 device->phyindex = phyindex;
529 * connman_device_set_interface:
530 * @device: device structure
531 * @interface: interface name
533 * Set interface name of device
535 void connman_device_set_interface(struct connman_device *device,
536 const char *interface)
538 g_free(device->devname);
539 device->devname = g_strdup(interface);
541 g_free(device->interface);
542 device->interface = g_strdup(interface);
544 if (device->name == NULL) {
545 const char *str = type2description(device->type);
546 if (str != NULL && device->interface != NULL)
547 device->name = g_strdup_printf("%s (%s)", str,
553 * connman_device_set_ident:
554 * @device: device structure
555 * @ident: unique identifier
557 * Set unique identifier of device
559 void connman_device_set_ident(struct connman_device *device,
562 g_free(device->ident);
563 device->ident = g_strdup(ident);
566 const char *connman_device_get_ident(struct connman_device *device)
568 return device->ident;
572 * connman_device_set_powered:
573 * @device: device structure
574 * @powered: powered state
576 * Change power state of device
578 int connman_device_set_powered(struct connman_device *device,
579 connman_bool_t powered)
581 enum connman_service_type type;
583 DBG("driver %p powered %d", device, powered);
585 if (device->powered == powered)
588 clear_pending_trigger(device);
590 device->powered_pending = PENDING_NONE;
592 device->powered = powered;
594 type = __connman_device_get_service_type(device);
596 if (device->powered == TRUE)
597 __connman_technology_enabled(type);
599 __connman_technology_disabled(type);
601 if (powered == FALSE)
604 connman_device_set_disconnected(device, FALSE);
605 device->scanning = FALSE;
607 if (device->driver && device->driver->scan_fast)
608 device->driver->scan_fast(device);
609 else if (device->driver && device->driver->scan)
610 device->driver->scan(device);
615 static int device_scan(struct connman_device *device)
617 if (!device->driver || !device->driver->scan)
620 if (device->powered == FALSE)
623 return device->driver->scan(device);
626 int __connman_device_disconnect(struct connman_device *device)
631 DBG("device %p", device);
633 connman_device_set_disconnected(device, TRUE);
635 g_hash_table_iter_init(&iter, device->networks);
637 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
638 struct connman_network *network = value;
640 if (connman_network_get_connecting(network) == TRUE) {
642 * Skip network in the process of connecting.
643 * This is a workaround for WiFi networks serviced
644 * by the supplicant plugin that hold a reference
645 * to the network. If we disconnect the network
646 * here then the referenced object will not be
647 * registered and usage (like launching DHCP client)
648 * will fail. There is nothing to be gained by
649 * removing the network here anyway.
651 connman_warn("Skipping disconnect of %s, network is connecting.",
652 connman_network_get_identifier(network));
656 __connman_network_disconnect(network);
662 static void mark_network_available(gpointer key, gpointer value,
665 struct connman_network *network = value;
667 connman_network_set_available(network, TRUE);
670 static void mark_network_unavailable(gpointer key, gpointer value,
673 struct connman_network *network = value;
675 if (connman_network_get_connected(network) == TRUE)
678 connman_network_set_available(network, FALSE);
681 static gboolean remove_unavailable_network(gpointer key, gpointer value,
684 struct connman_network *network = value;
686 if (connman_network_get_connected(network) == TRUE)
689 if (connman_network_get_available(network) == TRUE)
695 void __connman_device_cleanup_networks(struct connman_device *device)
697 g_hash_table_foreach_remove(device->networks,
698 remove_unavailable_network, NULL);
701 connman_bool_t connman_device_get_scanning(struct connman_device *device)
703 return device->scanning;
706 void connman_device_reset_scanning(struct connman_device *device)
708 device->scanning = FALSE;
710 g_hash_table_foreach(device->networks,
711 mark_network_available, NULL);
715 * connman_device_set_scanning:
716 * @device: device structure
717 * @scanning: scanning state
719 * Change scanning state of device
721 int connman_device_set_scanning(struct connman_device *device,
722 connman_bool_t scanning)
724 DBG("device %p scanning %d", device, scanning);
726 if (!device->driver || !device->driver->scan)
729 if (device->scanning == scanning)
732 device->scanning = scanning;
734 if (scanning == TRUE) {
735 __connman_technology_scan_started(device);
737 g_hash_table_foreach(device->networks,
738 mark_network_unavailable, NULL);
743 __connman_device_cleanup_networks(device);
745 __connman_technology_scan_stopped(device);
747 __connman_service_auto_connect();
753 * connman_device_set_disconnected:
754 * @device: device structure
755 * @disconnected: disconnected state
757 * Change disconnected state of device (only for device with networks)
759 int connman_device_set_disconnected(struct connman_device *device,
760 connman_bool_t disconnected)
762 DBG("device %p disconnected %d", device, disconnected);
764 if (device->disconnected == disconnected)
767 device->disconnected = disconnected;
773 * connman_device_get_disconnected:
774 * @device: device structure
776 * Get device disconnected state
778 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
780 return device->disconnected;
784 * connman_device_set_string:
785 * @device: device structure
786 * @key: unique identifier
787 * @value: string value
789 * Set string value for specific key
791 int connman_device_set_string(struct connman_device *device,
792 const char *key, const char *value)
794 DBG("device %p key %s value %s", device, key, value);
796 if (g_str_equal(key, "Address") == TRUE) {
797 g_free(device->address);
798 device->address = g_strdup(value);
799 } else if (g_str_equal(key, "Name") == TRUE) {
800 g_free(device->name);
801 device->name = g_strdup(value);
802 } else if (g_str_equal(key, "Node") == TRUE) {
803 g_free(device->node);
804 device->node = g_strdup(value);
805 } else if (g_str_equal(key, "Path") == TRUE) {
806 g_free(device->path);
807 device->path = g_strdup(value);
816 * connman_device_get_string:
817 * @device: device structure
818 * @key: unique identifier
820 * Get string value for specific key
822 const char *connman_device_get_string(struct connman_device *device,
825 DBG("device %p key %s", device, key);
827 if (g_str_equal(key, "Address") == TRUE)
828 return device->address;
829 else if (g_str_equal(key, "Name") == TRUE)
831 else if (g_str_equal(key, "Node") == TRUE)
833 else if (g_str_equal(key, "Interface") == TRUE)
834 return device->interface;
835 else if (g_str_equal(key, "Path") == TRUE)
842 * connman_device_add_network:
843 * @device: device structure
844 * @network: network structure
846 * Add new network to the device
848 int connman_device_add_network(struct connman_device *device,
849 struct connman_network *network)
851 const char *identifier = connman_network_get_identifier(network);
853 DBG("device %p network %p", device, network);
855 if (identifier == NULL)
858 connman_network_ref(network);
860 __connman_network_set_device(network, device);
862 g_hash_table_replace(device->networks, g_strdup(identifier),
869 * connman_device_get_network:
870 * @device: device structure
871 * @identifier: network identifier
873 * Get network for given identifier
875 struct connman_network *connman_device_get_network(struct connman_device *device,
876 const char *identifier)
878 DBG("device %p identifier %s", device, identifier);
880 return g_hash_table_lookup(device->networks, identifier);
884 * connman_device_remove_network:
885 * @device: device structure
886 * @identifier: network identifier
888 * Remove network for given identifier
890 int connman_device_remove_network(struct connman_device *device,
891 struct connman_network *network)
893 const char *identifier;
895 DBG("device %p network %p", device, network);
900 identifier = connman_network_get_identifier(network);
901 g_hash_table_remove(device->networks, identifier);
906 void connman_device_remove_all_networks(struct connman_device *device)
908 g_hash_table_remove_all(device->networks);
911 void __connman_device_set_network(struct connman_device *device,
912 struct connman_network *network)
919 if (device->network == network)
922 if (network != NULL) {
923 name = connman_network_get_string(network, "Name");
924 g_free(device->last_network);
925 device->last_network = g_strdup(name);
927 device->network = network;
929 g_free(device->last_network);
930 device->last_network = NULL;
932 device->network = NULL;
936 void __connman_device_set_reconnect(struct connman_device *device,
937 connman_bool_t reconnect)
939 device->reconnect = reconnect;
942 connman_bool_t __connman_device_get_reconnect(
943 struct connman_device *device)
945 return device->reconnect;
948 static gboolean match_driver(struct connman_device *device,
949 struct connman_device_driver *driver)
951 if (device->type == driver->type ||
952 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
959 * connman_device_register:
960 * @device: device structure
962 * Register device with the system
964 int connman_device_register(struct connman_device *device)
968 DBG("device %p name %s", device, device->name);
970 if (device->driver != NULL)
973 for (list = driver_list; list; list = list->next) {
974 struct connman_device_driver *driver = list->data;
976 if (match_driver(device, driver) == FALSE)
979 DBG("driver %p name %s", driver, driver->name);
981 if (driver->probe(device) == 0) {
982 device->driver = driver;
987 if (device->driver == NULL)
990 return __connman_technology_add_device(device);
994 * connman_device_unregister:
995 * @device: device structure
997 * Unregister device with the system
999 void connman_device_unregister(struct connman_device *device)
1001 DBG("device %p name %s", device, device->name);
1003 if (device->driver == NULL)
1006 remove_device(device);
1010 * connman_device_get_data:
1011 * @device: device structure
1013 * Get private device data pointer
1015 void *connman_device_get_data(struct connman_device *device)
1017 return device->driver_data;
1021 * connman_device_set_data:
1022 * @device: device structure
1023 * @data: data pointer
1025 * Set private device data pointer
1027 void connman_device_set_data(struct connman_device *device, void *data)
1029 device->driver_data = data;
1032 struct connman_device *__connman_device_find_device(
1033 enum connman_service_type type)
1037 for (list = device_list; list != NULL; list = list->next) {
1038 struct connman_device *device = list->data;
1039 enum connman_service_type service_type =
1040 __connman_device_get_service_type(device);
1042 if (service_type != type)
1051 int __connman_device_request_scan(enum connman_service_type type)
1053 connman_bool_t success = FALSE;
1054 int last_err = -ENOSYS;
1059 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1060 case CONNMAN_SERVICE_TYPE_SYSTEM:
1061 case CONNMAN_SERVICE_TYPE_ETHERNET:
1062 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1063 case CONNMAN_SERVICE_TYPE_CELLULAR:
1064 case CONNMAN_SERVICE_TYPE_GPS:
1065 case CONNMAN_SERVICE_TYPE_VPN:
1066 case CONNMAN_SERVICE_TYPE_GADGET:
1068 case CONNMAN_SERVICE_TYPE_WIFI:
1069 case CONNMAN_SERVICE_TYPE_WIMAX:
1073 for (list = device_list; list != NULL; list = list->next) {
1074 struct connman_device *device = list->data;
1075 enum connman_service_type service_type =
1076 __connman_device_get_service_type(device);
1078 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1079 service_type != type) {
1083 err = device_scan(device);
1084 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1088 DBG("device %p err %d", device, err);
1092 if (success == TRUE)
1098 int __connman_device_request_hidden_scan(struct connman_device *device,
1099 const char *ssid, unsigned int ssid_len,
1100 const char *identity, const char *passphrase,
1103 DBG("device %p", device);
1105 if (device == NULL || device->driver == NULL ||
1106 device->driver->scan_hidden == NULL)
1109 if (device->scanning == TRUE)
1112 return device->driver->scan_hidden(device, ssid, ssid_len,
1113 identity, passphrase, user_data);
1116 connman_bool_t __connman_device_isfiltered(const char *devname)
1120 if (device_filter == NULL)
1123 for (pattern = device_filter; *pattern; pattern++) {
1124 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1125 DBG("ignoring device %s (match)", devname);
1131 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1132 DBG("ignoring dummy networking devices");
1136 if (nodevice_filter == NULL)
1139 for (pattern = nodevice_filter; *pattern; pattern++) {
1140 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1141 DBG("ignoring device %s (no match)", devname);
1149 int __connman_device_init(const char *device, const char *nodevice)
1154 device_filter = g_strsplit(device, ",", -1);
1156 if (nodevice != NULL)
1157 nodevice_filter = g_strsplit(nodevice, ",", -1);
1162 void __connman_device_cleanup(void)
1166 g_strfreev(nodevice_filter);
1167 g_strfreev(device_filter);