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 == FALSE) {
597 __connman_technology_disabled(type);
601 __connman_technology_enabled(type);
603 connman_device_set_disconnected(device, FALSE);
604 device->scanning = FALSE;
606 if (device->driver && device->driver->scan_fast)
607 device->driver->scan_fast(device);
608 else if (device->driver && device->driver->scan)
609 device->driver->scan(device);
614 static int device_scan(struct connman_device *device)
616 if (!device->driver || !device->driver->scan)
619 if (device->powered == FALSE)
622 return device->driver->scan(device);
625 int __connman_device_disconnect(struct connman_device *device)
630 DBG("device %p", device);
632 connman_device_set_disconnected(device, TRUE);
634 g_hash_table_iter_init(&iter, device->networks);
636 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
637 struct connman_network *network = value;
639 if (connman_network_get_connecting(network) == TRUE) {
641 * Skip network in the process of connecting.
642 * This is a workaround for WiFi networks serviced
643 * by the supplicant plugin that hold a reference
644 * to the network. If we disconnect the network
645 * here then the referenced object will not be
646 * registered and usage (like launching DHCP client)
647 * will fail. There is nothing to be gained by
648 * removing the network here anyway.
650 connman_warn("Skipping disconnect of %s, network is connecting.",
651 connman_network_get_identifier(network));
655 __connman_network_disconnect(network);
661 static void mark_network_available(gpointer key, gpointer value,
664 struct connman_network *network = value;
666 connman_network_set_available(network, TRUE);
669 static void mark_network_unavailable(gpointer key, gpointer value,
672 struct connman_network *network = value;
674 if (connman_network_get_connected(network) == TRUE)
677 connman_network_set_available(network, FALSE);
680 static gboolean remove_unavailable_network(gpointer key, gpointer value,
683 struct connman_network *network = value;
685 if (connman_network_get_connected(network) == TRUE)
688 if (connman_network_get_available(network) == TRUE)
694 void __connman_device_cleanup_networks(struct connman_device *device)
696 g_hash_table_foreach_remove(device->networks,
697 remove_unavailable_network, NULL);
700 connman_bool_t connman_device_get_scanning(struct connman_device *device)
702 return device->scanning;
705 void connman_device_reset_scanning(struct connman_device *device)
707 g_hash_table_foreach(device->networks,
708 mark_network_available, NULL);
712 * connman_device_set_scanning:
713 * @device: device structure
714 * @scanning: scanning state
716 * Change scanning state of device
718 int connman_device_set_scanning(struct connman_device *device,
719 connman_bool_t scanning)
721 DBG("device %p scanning %d", device, scanning);
723 if (!device->driver || !device->driver->scan)
726 if (device->scanning == scanning)
729 device->scanning = scanning;
731 if (scanning == TRUE) {
732 __connman_technology_scan_started(device);
734 g_hash_table_foreach(device->networks,
735 mark_network_unavailable, NULL);
740 __connman_device_cleanup_networks(device);
742 __connman_technology_scan_stopped(device);
744 __connman_service_auto_connect();
750 * connman_device_set_disconnected:
751 * @device: device structure
752 * @disconnected: disconnected state
754 * Change disconnected state of device (only for device with networks)
756 int connman_device_set_disconnected(struct connman_device *device,
757 connman_bool_t disconnected)
759 DBG("device %p disconnected %d", device, disconnected);
761 if (device->disconnected == disconnected)
764 device->disconnected = disconnected;
770 * connman_device_get_disconnected:
771 * @device: device structure
773 * Get device disconnected state
775 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
777 return device->disconnected;
781 * connman_device_set_string:
782 * @device: device structure
783 * @key: unique identifier
784 * @value: string value
786 * Set string value for specific key
788 int connman_device_set_string(struct connman_device *device,
789 const char *key, const char *value)
791 DBG("device %p key %s value %s", device, key, value);
793 if (g_str_equal(key, "Address") == TRUE) {
794 g_free(device->address);
795 device->address = g_strdup(value);
796 } else if (g_str_equal(key, "Name") == TRUE) {
797 g_free(device->name);
798 device->name = g_strdup(value);
799 } else if (g_str_equal(key, "Node") == TRUE) {
800 g_free(device->node);
801 device->node = g_strdup(value);
802 } else if (g_str_equal(key, "Path") == TRUE) {
803 g_free(device->path);
804 device->path = g_strdup(value);
813 * connman_device_get_string:
814 * @device: device structure
815 * @key: unique identifier
817 * Get string value for specific key
819 const char *connman_device_get_string(struct connman_device *device,
822 DBG("device %p key %s", device, key);
824 if (g_str_equal(key, "Address") == TRUE)
825 return device->address;
826 else if (g_str_equal(key, "Name") == TRUE)
828 else if (g_str_equal(key, "Node") == TRUE)
830 else if (g_str_equal(key, "Interface") == TRUE)
831 return device->interface;
832 else if (g_str_equal(key, "Path") == TRUE)
839 * connman_device_add_network:
840 * @device: device structure
841 * @network: network structure
843 * Add new network to the device
845 int connman_device_add_network(struct connman_device *device,
846 struct connman_network *network)
848 const char *identifier = connman_network_get_identifier(network);
850 DBG("device %p network %p", device, network);
852 if (identifier == NULL)
855 connman_network_ref(network);
857 __connman_network_set_device(network, device);
859 g_hash_table_replace(device->networks, g_strdup(identifier),
866 * connman_device_get_network:
867 * @device: device structure
868 * @identifier: network identifier
870 * Get network for given identifier
872 struct connman_network *connman_device_get_network(struct connman_device *device,
873 const char *identifier)
875 DBG("device %p identifier %s", device, identifier);
877 return g_hash_table_lookup(device->networks, identifier);
881 * connman_device_remove_network:
882 * @device: device structure
883 * @identifier: network identifier
885 * Remove network for given identifier
887 int connman_device_remove_network(struct connman_device *device,
888 struct connman_network *network)
890 const char *identifier;
892 DBG("device %p network %p", device, network);
897 identifier = connman_network_get_identifier(network);
898 g_hash_table_remove(device->networks, identifier);
903 void connman_device_remove_all_networks(struct connman_device *device)
905 g_hash_table_remove_all(device->networks);
908 void __connman_device_set_network(struct connman_device *device,
909 struct connman_network *network)
916 if (device->network == network)
919 if (network != NULL) {
920 name = connman_network_get_string(network, "Name");
921 g_free(device->last_network);
922 device->last_network = g_strdup(name);
924 device->network = network;
926 g_free(device->last_network);
927 device->last_network = NULL;
929 device->network = NULL;
933 void __connman_device_set_reconnect(struct connman_device *device,
934 connman_bool_t reconnect)
936 device->reconnect = reconnect;
939 connman_bool_t __connman_device_get_reconnect(
940 struct connman_device *device)
942 return device->reconnect;
945 static gboolean match_driver(struct connman_device *device,
946 struct connman_device_driver *driver)
948 if (device->type == driver->type ||
949 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
956 * connman_device_register:
957 * @device: device structure
959 * Register device with the system
961 int connman_device_register(struct connman_device *device)
965 DBG("device %p name %s", device, device->name);
967 if (device->driver != NULL)
970 for (list = driver_list; list; list = list->next) {
971 struct connman_device_driver *driver = list->data;
973 if (match_driver(device, driver) == FALSE)
976 DBG("driver %p name %s", driver, driver->name);
978 if (driver->probe(device) == 0) {
979 device->driver = driver;
984 if (device->driver == NULL)
987 return __connman_technology_add_device(device);
991 * connman_device_unregister:
992 * @device: device structure
994 * Unregister device with the system
996 void connman_device_unregister(struct connman_device *device)
998 DBG("device %p name %s", device, device->name);
1000 if (device->driver == NULL)
1003 remove_device(device);
1007 * connman_device_get_data:
1008 * @device: device structure
1010 * Get private device data pointer
1012 void *connman_device_get_data(struct connman_device *device)
1014 return device->driver_data;
1018 * connman_device_set_data:
1019 * @device: device structure
1020 * @data: data pointer
1022 * Set private device data pointer
1024 void connman_device_set_data(struct connman_device *device, void *data)
1026 device->driver_data = data;
1029 struct connman_device *__connman_device_find_device(
1030 enum connman_service_type type)
1034 for (list = device_list; list != NULL; list = list->next) {
1035 struct connman_device *device = list->data;
1036 enum connman_service_type service_type =
1037 __connman_device_get_service_type(device);
1039 if (service_type != type)
1048 int __connman_device_request_scan(enum connman_service_type type)
1050 connman_bool_t success = FALSE;
1051 int last_err = -ENOSYS;
1056 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1057 case CONNMAN_SERVICE_TYPE_SYSTEM:
1058 case CONNMAN_SERVICE_TYPE_ETHERNET:
1059 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1060 case CONNMAN_SERVICE_TYPE_CELLULAR:
1061 case CONNMAN_SERVICE_TYPE_GPS:
1062 case CONNMAN_SERVICE_TYPE_VPN:
1063 case CONNMAN_SERVICE_TYPE_GADGET:
1065 case CONNMAN_SERVICE_TYPE_WIFI:
1066 case CONNMAN_SERVICE_TYPE_WIMAX:
1070 for (list = device_list; list != NULL; list = list->next) {
1071 struct connman_device *device = list->data;
1072 enum connman_service_type service_type =
1073 __connman_device_get_service_type(device);
1075 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
1076 service_type != type) {
1080 err = device_scan(device);
1081 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1085 DBG("device %p err %d", device, err);
1089 if (success == TRUE)
1095 int __connman_device_request_hidden_scan(struct connman_device *device,
1096 const char *ssid, unsigned int ssid_len,
1097 const char *identity, const char *passphrase,
1100 DBG("device %p", device);
1102 if (device == NULL || device->driver == NULL ||
1103 device->driver->scan_hidden == NULL)
1106 if (device->scanning == TRUE)
1109 return device->driver->scan_hidden(device, ssid, ssid_len,
1110 identity, passphrase, user_data);
1113 connman_bool_t __connman_device_isfiltered(const char *devname)
1116 char **blacklisted_interfaces;
1118 if (device_filter == NULL)
1121 for (pattern = device_filter; *pattern; pattern++) {
1122 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1123 DBG("ignoring device %s (match)", devname);
1129 if (g_pattern_match_simple("dummy*", devname) == TRUE) {
1130 DBG("ignoring dummy networking devices");
1134 if (nodevice_filter == NULL)
1137 for (pattern = nodevice_filter; *pattern; pattern++) {
1138 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1139 DBG("ignoring device %s (no match)", devname);
1145 blacklisted_interfaces =
1146 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1147 if (blacklisted_interfaces == NULL)
1150 for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1151 if (g_str_has_prefix(devname, *pattern) == TRUE) {
1152 DBG("ignoring device %s (blacklist)", devname);
1160 int __connman_device_init(const char *device, const char *nodevice)
1165 device_filter = g_strsplit(device, ",", -1);
1167 if (nodevice != NULL)
1168 nodevice_filter = g_strsplit(nodevice, ",", -1);
1173 void __connman_device_cleanup(void)
1177 g_strfreev(nodevice_filter);
1178 g_strfreev(device_filter);