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 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);
251 connman_device_set_powered(device, FALSE);
255 if (err == -EALREADY) {
256 connman_device_set_powered(device, FALSE);
260 if (err == -EINPROGRESS)
261 device->pending_timeout = g_timeout_add_seconds(4,
262 device_pending_reset, device);
267 static void probe_driver(struct connman_device_driver *driver)
271 DBG("driver %p name %s", driver, driver->name);
273 for (list = device_list; list != NULL; list = list->next) {
274 struct connman_device *device = list->data;
276 if (device->driver != NULL)
279 if (driver->type != device->type)
282 if (driver->probe(device) < 0)
285 device->driver = driver;
287 __connman_technology_add_device(device);
291 static void remove_device(struct connman_device *device)
293 DBG("device %p", device);
295 __connman_device_disable(device);
297 __connman_technology_remove_device(device);
299 if (device->driver->remove)
300 device->driver->remove(device);
302 device->driver = NULL;
305 static void remove_driver(struct connman_device_driver *driver)
309 DBG("driver %p name %s", driver, driver->name);
311 for (list = device_list; list != NULL; list = list->next) {
312 struct connman_device *device = list->data;
314 if (device->driver == driver)
315 remove_device(device);
319 connman_bool_t __connman_device_has_driver(struct connman_device *device)
321 if (device == NULL || device->driver == NULL)
327 static GSList *driver_list = NULL;
329 static gint compare_priority(gconstpointer a, gconstpointer b)
331 const struct connman_device_driver *driver1 = a;
332 const struct connman_device_driver *driver2 = b;
334 return driver2->priority - driver1->priority;
338 * connman_device_driver_register:
339 * @driver: device driver definition
341 * Register a new device driver
343 * Returns: %0 on success
345 int connman_device_driver_register(struct connman_device_driver *driver)
347 DBG("driver %p name %s", driver, driver->name);
349 driver_list = g_slist_insert_sorted(driver_list, driver,
351 probe_driver(driver);
357 * connman_device_driver_unregister:
358 * @driver: device driver definition
360 * Remove a previously registered device driver
362 void connman_device_driver_unregister(struct connman_device_driver *driver)
364 DBG("driver %p name %s", driver, driver->name);
366 driver_list = g_slist_remove(driver_list, driver);
368 remove_driver(driver);
371 static void free_network(gpointer data)
373 struct connman_network *network = data;
375 DBG("network %p", network);
377 __connman_network_set_device(network, NULL);
379 connman_network_unref(network);
382 static void device_destruct(struct connman_device *device)
384 DBG("device %p name %s", device, device->name);
386 clear_pending_trigger(device);
388 g_free(device->ident);
389 g_free(device->node);
390 g_free(device->name);
391 g_free(device->address);
392 g_free(device->interface);
393 g_free(device->path);
394 g_free(device->devname);
396 g_free(device->last_network);
398 g_hash_table_destroy(device->networks);
399 device->networks = NULL;
405 * connman_device_create:
406 * @node: device node name (for example an address)
409 * Allocate a new device of given #type and assign the #node name to it.
411 * Returns: a newly-allocated #connman_device structure
413 struct connman_device *connman_device_create(const char *node,
414 enum connman_device_type type)
416 struct connman_device *device;
418 DBG("node %s type %d", node, type);
420 device = g_try_new0(struct connman_device, 1);
424 DBG("device %p", device);
426 device->refcount = 1;
429 device->name = g_strdup(type2description(device->type));
431 device->phyindex = -1;
433 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
434 g_free, free_network);
436 device_list = g_slist_append(device_list, device);
442 * connman_device_ref:
443 * @device: device structure
445 * Increase reference counter of device
447 struct connman_device *connman_device_ref_debug(struct connman_device *device,
448 const char *file, int line, const char *caller)
450 DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
453 __sync_fetch_and_add(&device->refcount, 1);
459 * connman_device_unref:
460 * @device: device structure
462 * Decrease reference counter of device
464 void connman_device_unref_debug(struct connman_device *device,
465 const char *file, int line, const char *caller)
467 DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
470 if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
473 if (device->driver) {
474 device->driver->remove(device);
475 device->driver = NULL;
478 device_list = g_slist_remove(device_list, device);
480 device_destruct(device);
483 const char *__connman_device_get_type(struct connman_device *device)
485 return type2string(device->type);
489 * connman_device_get_type:
490 * @device: device structure
494 enum connman_device_type connman_device_get_type(struct connman_device *device)
500 * connman_device_set_index:
501 * @device: device structure
502 * @index: index number
504 * Set index number of device
506 void connman_device_set_index(struct connman_device *device, int index)
508 device->index = index;
512 * connman_device_get_index:
513 * @device: device structure
515 * Get index number of device
517 int connman_device_get_index(struct connman_device *device)
519 return device->index;
522 int __connman_device_get_phyindex(struct connman_device *device)
524 return device->phyindex;
527 void __connman_device_set_phyindex(struct connman_device *device,
530 device->phyindex = phyindex;
534 * connman_device_set_interface:
535 * @device: device structure
536 * @interface: interface name
538 * Set interface name of device
540 void connman_device_set_interface(struct connman_device *device,
541 const char *interface)
543 g_free(device->devname);
544 device->devname = g_strdup(interface);
546 g_free(device->interface);
547 device->interface = g_strdup(interface);
549 if (device->name == NULL) {
550 const char *str = type2description(device->type);
551 if (str != NULL && device->interface != NULL)
552 device->name = g_strdup_printf("%s (%s)", str,
558 * connman_device_set_ident:
559 * @device: device structure
560 * @ident: unique identifier
562 * Set unique identifier of device
564 void connman_device_set_ident(struct connman_device *device,
567 g_free(device->ident);
568 device->ident = g_strdup(ident);
571 const char *connman_device_get_ident(struct connman_device *device)
573 return device->ident;
577 * connman_device_set_powered:
578 * @device: device structure
579 * @powered: powered state
581 * Change power state of device
583 int connman_device_set_powered(struct connman_device *device,
584 connman_bool_t powered)
586 enum connman_service_type type;
588 DBG("driver %p powered %d", device, powered);
590 if (device->powered == powered)
593 clear_pending_trigger(device);
595 device->powered_pending = PENDING_NONE;
597 device->powered = powered;
599 type = __connman_device_get_service_type(device);
601 if (device->powered == TRUE)
602 __connman_technology_enabled(type);
604 __connman_technology_disabled(type);
606 if (powered == FALSE)
609 connman_device_set_disconnected(device, FALSE);
610 device->scanning = FALSE;
612 if (device->driver && device->driver->scan_fast)
613 device->driver->scan_fast(device);
614 else if (device->driver && device->driver->scan)
615 device->driver->scan(device);
620 static int device_scan(struct connman_device *device)
622 if (!device->driver || !device->driver->scan)
625 if (device->powered == FALSE)
628 return device->driver->scan(device);
631 int __connman_device_disconnect(struct connman_device *device)
636 DBG("device %p", device);
638 connman_device_set_disconnected(device, TRUE);
640 g_hash_table_iter_init(&iter, device->networks);
642 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
643 struct connman_network *network = value;
645 if (connman_network_get_connecting(network) == TRUE) {
647 * Skip network in the process of connecting.
648 * This is a workaround for WiFi networks serviced
649 * by the supplicant plugin that hold a reference
650 * to the network. If we disconnect the network
651 * here then the referenced object will not be
652 * registered and usage (like launching DHCP client)
653 * will fail. There is nothing to be gained by
654 * removing the network here anyway.
656 connman_warn("Skipping disconnect of %s, network is connecting.",
657 connman_network_get_identifier(network));
661 __connman_network_disconnect(network);
667 static void mark_network_available(gpointer key, gpointer value,
670 struct connman_network *network = value;
672 connman_network_set_available(network, TRUE);
675 static void mark_network_unavailable(gpointer key, gpointer value,
678 struct connman_network *network = value;
680 if (connman_network_get_connected(network) == TRUE)
683 connman_network_set_available(network, FALSE);
686 static gboolean remove_unavailable_network(gpointer key, gpointer value,
689 struct connman_network *network = value;
691 if (connman_network_get_connected(network) == TRUE)
694 if (connman_network_get_available(network) == TRUE)
700 void __connman_device_cleanup_networks(struct connman_device *device)
702 g_hash_table_foreach_remove(device->networks,
703 remove_unavailable_network, NULL);
706 connman_bool_t __connman_device_scanning(struct connman_device *device)
708 return device->scanning;
711 void connman_device_reset_scanning(struct connman_device *device)
713 device->scanning = FALSE;
715 g_hash_table_foreach(device->networks,
716 mark_network_available, NULL);
720 * connman_device_set_scanning:
721 * @device: device structure
722 * @scanning: scanning state
724 * Change scanning state of device
726 int connman_device_set_scanning(struct connman_device *device,
727 connman_bool_t scanning)
729 DBG("device %p scanning %d", device, scanning);
731 if (!device->driver || !device->driver->scan)
734 if (device->scanning == scanning)
737 device->scanning = scanning;
739 if (scanning == TRUE) {
740 __connman_technology_scan_started(device);
742 g_hash_table_foreach(device->networks,
743 mark_network_unavailable, NULL);
748 __connman_device_cleanup_networks(device);
750 __connman_technology_scan_stopped(device);
752 __connman_service_auto_connect();
758 * connman_device_set_disconnected:
759 * @device: device structure
760 * @disconnected: disconnected state
762 * Change disconnected state of device (only for device with networks)
764 int connman_device_set_disconnected(struct connman_device *device,
765 connman_bool_t disconnected)
767 DBG("device %p disconnected %d", device, disconnected);
769 if (device->disconnected == disconnected)
772 device->disconnected = disconnected;
778 * connman_device_get_disconnected:
779 * @device: device structure
781 * Get device disconnected state
783 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
785 return device->disconnected;
789 * connman_device_set_string:
790 * @device: device structure
791 * @key: unique identifier
792 * @value: string value
794 * Set string value for specific key
796 int connman_device_set_string(struct connman_device *device,
797 const char *key, const char *value)
799 DBG("device %p key %s value %s", device, key, value);
801 if (g_str_equal(key, "Address") == TRUE) {
802 g_free(device->address);
803 device->address = g_strdup(value);
804 } else if (g_str_equal(key, "Name") == TRUE) {
805 g_free(device->name);
806 device->name = g_strdup(value);
807 } else if (g_str_equal(key, "Node") == TRUE) {
808 g_free(device->node);
809 device->node = g_strdup(value);
810 } else if (g_str_equal(key, "Path") == TRUE) {
811 g_free(device->path);
812 device->path = g_strdup(value);
821 * connman_device_get_string:
822 * @device: device structure
823 * @key: unique identifier
825 * Get string value for specific key
827 const char *connman_device_get_string(struct connman_device *device,
830 DBG("device %p key %s", device, key);
832 if (g_str_equal(key, "Address") == TRUE)
833 return device->address;
834 else if (g_str_equal(key, "Name") == TRUE)
836 else if (g_str_equal(key, "Node") == TRUE)
838 else if (g_str_equal(key, "Interface") == TRUE)
839 return device->interface;
840 else if (g_str_equal(key, "Path") == TRUE)
847 * connman_device_add_network:
848 * @device: device structure
849 * @network: network structure
851 * Add new network to the device
853 int connman_device_add_network(struct connman_device *device,
854 struct connman_network *network)
856 const char *identifier = connman_network_get_identifier(network);
858 DBG("device %p network %p", device, network);
860 if (identifier == NULL)
863 connman_network_ref(network);
865 __connman_network_set_device(network, device);
867 g_hash_table_replace(device->networks, g_strdup(identifier),
874 * connman_device_get_network:
875 * @device: device structure
876 * @identifier: network identifier
878 * Get network for given identifier
880 struct connman_network *connman_device_get_network(struct connman_device *device,
881 const char *identifier)
883 DBG("device %p identifier %s", device, identifier);
885 return g_hash_table_lookup(device->networks, identifier);
889 * connman_device_remove_network:
890 * @device: device structure
891 * @identifier: network identifier
893 * Remove network for given identifier
895 int connman_device_remove_network(struct connman_device *device,
896 struct connman_network *network)
898 const char *identifier;
900 DBG("device %p network %p", device, network);
905 identifier = connman_network_get_identifier(network);
906 g_hash_table_remove(device->networks, identifier);
911 void connman_device_remove_all_networks(struct connman_device *device)
913 g_hash_table_remove_all(device->networks);
916 void __connman_device_set_network(struct connman_device *device,
917 struct connman_network *network)
924 if (device->network == network)
927 if (network != NULL) {
928 name = connman_network_get_string(network, "Name");
929 g_free(device->last_network);
930 device->last_network = g_strdup(name);
932 device->network = network;
934 g_free(device->last_network);
935 device->last_network = NULL;
937 device->network = NULL;
941 void __connman_device_set_reconnect(struct connman_device *device,
942 connman_bool_t reconnect)
944 device->reconnect = reconnect;
947 connman_bool_t __connman_device_get_reconnect(
948 struct connman_device *device)
950 return device->reconnect;
953 static gboolean match_driver(struct connman_device *device,
954 struct connman_device_driver *driver)
956 if (device->type == driver->type ||
957 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
964 * connman_device_register:
965 * @device: device structure
967 * Register device with the system
969 int connman_device_register(struct connman_device *device)
973 DBG("device %p name %s", device, device->name);
975 if (device->driver != NULL)
978 for (list = driver_list; list; list = list->next) {
979 struct connman_device_driver *driver = list->data;
981 if (match_driver(device, driver) == FALSE)
984 DBG("driver %p name %s", driver, driver->name);
986 if (driver->probe(device) == 0) {
987 device->driver = driver;
992 if (device->driver == NULL)
995 return __connman_technology_add_device(device);
999 * connman_device_unregister:
1000 * @device: device structure
1002 * Unregister device with the system
1004 void connman_device_unregister(struct connman_device *device)
1006 DBG("device %p name %s", device, device->name);
1008 if (device->driver == NULL)
1011 remove_device(device);
1015 * connman_device_get_data:
1016 * @device: device structure
1018 * Get private device data pointer
1020 void *connman_device_get_data(struct connman_device *device)
1022 return device->driver_data;
1026 * connman_device_set_data:
1027 * @device: device structure
1028 * @data: data pointer
1030 * Set private device data pointer
1032 void connman_device_set_data(struct connman_device *device, void *data)
1034 device->driver_data = data;
1037 struct connman_device *__connman_device_find_device(
1038 enum connman_service_type type)
1042 for (list = device_list; list != NULL; list = list->next) {
1043 struct connman_device *device = list->data;
1044 enum connman_service_type service_type =
1045 __connman_device_get_service_type(device);
1047 if (service_type != type)
1056 int __connman_device_request_scan(enum connman_service_type type)
1058 connman_bool_t success = FALSE;
1059 int last_err = -ENOSYS;
1064 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1065 case CONNMAN_SERVICE_TYPE_SYSTEM:
1066 case CONNMAN_SERVICE_TYPE_ETHERNET:
1067 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1068 case CONNMAN_SERVICE_TYPE_CELLULAR:
1069 case CONNMAN_SERVICE_TYPE_GPS:
1070 case CONNMAN_SERVICE_TYPE_VPN:
1071 case CONNMAN_SERVICE_TYPE_GADGET:
1073 case CONNMAN_SERVICE_TYPE_WIFI:
1074 case CONNMAN_SERVICE_TYPE_WIMAX:
1078 for (list = device_list; list != NULL; list = list->next) {
1079 struct connman_device *device = list->data;
1080 enum connman_service_type service_type =
1081 __connman_device_get_service_type(device);
1083 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1084 service_type != type) {
1088 err = device_scan(device);
1089 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1093 DBG("device %p err %d", device, err);
1097 if (success == TRUE)
1103 int __connman_device_request_hidden_scan(struct connman_device *device,
1104 const char *ssid, unsigned int ssid_len,
1105 const char *identity, const char *passphrase)
1107 DBG("device %p", device);
1109 if (device == NULL || device->driver == NULL ||
1110 device->driver->scan_hidden == NULL)
1113 if (device->scanning == TRUE)
1116 return device->driver->scan_hidden(device, ssid, ssid_len,
1117 identity, passphrase);
1120 connman_bool_t __connman_device_isfiltered(const char *devname)
1124 if (device_filter == NULL)
1127 for (pattern = device_filter; *pattern; pattern++) {
1128 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1129 DBG("ignoring device %s (match)", devname);
1135 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1136 DBG("ignoring dummy networking devices");
1140 if (nodevice_filter == NULL)
1143 for (pattern = nodevice_filter; *pattern; pattern++) {
1144 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1145 DBG("ignoring device %s (no match)", devname);
1153 int __connman_device_init(const char *device, const char *nodevice)
1158 device_filter = g_strsplit(device, ",", -1);
1160 if (nodevice != NULL)
1161 nodevice_filter = g_strsplit(nodevice, ",", -1);
1166 void __connman_device_cleanup(void)
1170 g_strfreev(nodevice_filter);
1171 g_strfreev(device_filter);