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
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
137 case CONNMAN_DEVICE_TYPE_MBM:
144 static const char *type2string(enum connman_device_type type)
147 case CONNMAN_DEVICE_TYPE_UNKNOWN:
148 case CONNMAN_DEVICE_TYPE_VENDOR:
150 case CONNMAN_DEVICE_TYPE_ETHERNET:
152 case CONNMAN_DEVICE_TYPE_WIFI:
154 case CONNMAN_DEVICE_TYPE_WIMAX:
156 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
158 case CONNMAN_DEVICE_TYPE_GPS:
160 case CONNMAN_DEVICE_TYPE_CELLULAR:
161 case CONNMAN_DEVICE_TYPE_MBM:
168 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
170 enum connman_device_type type = connman_device_get_type(device);
173 case CONNMAN_DEVICE_TYPE_UNKNOWN:
174 case CONNMAN_DEVICE_TYPE_VENDOR:
175 case CONNMAN_DEVICE_TYPE_GPS:
177 case CONNMAN_DEVICE_TYPE_ETHERNET:
178 return CONNMAN_SERVICE_TYPE_ETHERNET;
179 case CONNMAN_DEVICE_TYPE_WIFI:
180 return CONNMAN_SERVICE_TYPE_WIFI;
181 case CONNMAN_DEVICE_TYPE_WIMAX:
182 return CONNMAN_SERVICE_TYPE_WIMAX;
183 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
184 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
185 case CONNMAN_DEVICE_TYPE_CELLULAR:
186 case CONNMAN_DEVICE_TYPE_MBM:
187 return CONNMAN_SERVICE_TYPE_CELLULAR;
190 return CONNMAN_SERVICE_TYPE_UNKNOWN;
193 static void powered_changed(struct connman_device *device)
195 connman_dbus_property_changed_basic(device->element.path,
196 CONNMAN_DEVICE_INTERFACE, "Powered",
197 DBUS_TYPE_BOOLEAN, &device->powered);
200 static int set_powered(struct connman_device *device, connman_bool_t powered)
202 struct connman_device_driver *driver = device->driver;
205 DBG("device %p powered %d", device, powered);
207 if (device->powered_pending == powered)
213 if (powered == TRUE) {
214 if (driver->enable) {
215 device->powered_pending = powered;
217 err = driver->enable(device);
219 __connman_technology_enable_device(device);
223 device->powered_pending = powered;
225 device->reconnect = FALSE;
227 clear_scan_trigger(device);
229 g_hash_table_remove_all(device->networks);
231 if (driver->disable) {
232 err = driver->disable(device);
234 __connman_technology_disable_device(device);
240 device->powered = powered;
242 if (device->registered == TRUE)
243 powered_changed(device);
249 void __connman_device_list(DBusMessageIter *iter, void *user_data)
251 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
254 static void append_path(gpointer key, gpointer value, gpointer user_data)
256 struct connman_element *element = value;
257 DBusMessageIter *iter = user_data;
259 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
263 static void append_networks(DBusMessageIter *iter, void *user_data)
265 struct connman_device *device = user_data;
267 g_hash_table_foreach(device->networks, append_path, iter);
270 static DBusMessage *get_properties(DBusConnection *conn,
271 DBusMessage *msg, void *data)
273 struct connman_device *device = data;
275 DBusMessageIter array, dict;
278 DBG("conn %p", conn);
280 if (__connman_security_check_privilege(msg,
281 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
282 return __connman_error_permission_denied(msg);
284 reply = dbus_message_new_method_return(msg);
288 dbus_message_iter_init_append(reply, &array);
290 connman_dbus_dict_open(&array, &dict);
292 if (device->name != NULL)
293 connman_dbus_dict_append_basic(&dict, "Name",
294 DBUS_TYPE_STRING, &device->name);
296 str = type2string(device->type);
298 connman_dbus_dict_append_basic(&dict, "Type",
299 DBUS_TYPE_STRING, &str);
301 if (device->address != NULL)
302 connman_dbus_dict_append_basic(&dict, "Address",
303 DBUS_TYPE_STRING, &device->address);
305 if (device->interface != NULL)
306 connman_dbus_dict_append_basic(&dict, "Interface",
307 DBUS_TYPE_STRING, &device->interface);
309 connman_dbus_dict_append_basic(&dict, "Powered",
310 DBUS_TYPE_BOOLEAN, &device->powered);
312 if (device->driver && device->driver->scan)
313 connman_dbus_dict_append_basic(&dict, "Scanning",
314 DBUS_TYPE_BOOLEAN, &device->scanning);
316 switch (device->mode) {
317 case CONNMAN_DEVICE_MODE_UNKNOWN:
319 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
320 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
321 if (device->scan_interval > 0)
322 connman_dbus_dict_append_basic(&dict, "ScanInterval",
323 DBUS_TYPE_UINT16, &device->scan_interval);
325 connman_dbus_dict_append_array(&dict, "Networks",
326 DBUS_TYPE_OBJECT_PATH, append_networks, device);
330 connman_dbus_dict_close(&array, &dict);
335 static gboolean powered_timeout(gpointer user_data)
337 struct connman_device *device = user_data;
339 DBG("device %p", device);
343 if (device->pending != NULL) {
346 reply = __connman_error_operation_timeout(device->pending);
348 g_dbus_send_message(connection, reply);
350 dbus_message_unref(device->pending);
351 device->pending = NULL;
357 static DBusMessage *set_property(DBusConnection *conn,
358 DBusMessage *msg, void *data)
360 struct connman_device *device = data;
361 DBusMessageIter iter, value;
365 DBG("conn %p", conn);
367 if (dbus_message_iter_init(msg, &iter) == FALSE)
368 return __connman_error_invalid_arguments(msg);
370 dbus_message_iter_get_basic(&iter, &name);
371 dbus_message_iter_next(&iter);
372 dbus_message_iter_recurse(&iter, &value);
374 if (__connman_security_check_privilege(msg,
375 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
376 return __connman_error_permission_denied(msg);
378 type = dbus_message_iter_get_arg_type(&value);
380 if (g_str_equal(name, "Powered") == TRUE) {
381 connman_bool_t powered;
384 if (type != DBUS_TYPE_BOOLEAN)
385 return __connman_error_invalid_arguments(msg);
387 dbus_message_iter_get_basic(&value, &powered);
389 device->powered_persistent = powered;
391 __connman_storage_save_device(device);
393 if (device->powered == powered)
394 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
396 if (device->pending != NULL)
397 return __connman_error_in_progress(msg);
399 err = set_powered(device, powered);
401 if (err != -EINPROGRESS)
402 return __connman_error_failed(msg, -err);
404 device->pending = dbus_message_ref(msg);
406 device->timeout = g_timeout_add_seconds(15,
407 powered_timeout, device);
411 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
412 connman_uint16_t interval;
414 switch (device->mode) {
415 case CONNMAN_DEVICE_MODE_UNKNOWN:
416 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
417 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
421 if (type != DBUS_TYPE_UINT16)
422 return __connman_error_invalid_arguments(msg);
424 dbus_message_iter_get_basic(&value, &interval);
426 if (device->scan_interval != interval) {
427 device->scan_interval = interval;
429 __connman_storage_save_device(device);
431 reset_scan_trigger(device);
434 return __connman_error_invalid_property(msg);
436 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
439 static DBusMessage *propose_scan(DBusConnection *conn,
440 DBusMessage *msg, void *data)
442 struct connman_device *device = data;
445 DBG("conn %p", conn);
447 switch (device->mode) {
448 case CONNMAN_DEVICE_MODE_UNKNOWN:
449 return __connman_error_not_supported(msg);
450 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
451 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
455 err = __connman_device_scan(device);
457 return __connman_error_failed(msg, -err);
459 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
462 static GDBusMethodTable device_methods[] = {
463 { "GetProperties", "", "a{sv}", get_properties },
464 { "SetProperty", "sv", "", set_property,
465 G_DBUS_METHOD_FLAG_ASYNC },
466 { "ProposeScan", "", "", propose_scan },
470 static GDBusSignalTable device_signals[] = {
471 { "PropertyChanged", "sv" },
475 static int register_interface(struct connman_element *element)
477 struct connman_device *device = element->device;
479 DBG("element %p name %s", element, element->name);
481 if (g_dbus_register_interface(connection, element->path,
482 CONNMAN_DEVICE_INTERFACE,
483 device_methods, device_signals,
484 NULL, device, NULL) == FALSE) {
485 connman_error("Failed to register %s device", element->path);
489 device->registered = TRUE;
494 static void unregister_interface(struct connman_element *element)
496 struct connman_device *device = element->device;
498 DBG("element %p name %s", element, element->name);
500 device->registered = FALSE;
502 g_dbus_unregister_interface(connection, element->path,
503 CONNMAN_DEVICE_INTERFACE);
506 static int setup_device(struct connman_device *device)
510 DBG("device %p", device);
512 err = register_interface(&device->element);
514 if (device->driver->remove)
515 device->driver->remove(device);
516 device->driver = NULL;
520 __connman_technology_add_device(device);
522 switch (device->mode) {
523 case CONNMAN_DEVICE_MODE_UNKNOWN:
524 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
525 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
529 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
532 if (device->offlinemode == FALSE &&
533 device->powered_persistent == TRUE)
534 __connman_device_enable(device);
539 static void probe_driver(struct connman_element *element, gpointer user_data)
541 struct connman_device_driver *driver = user_data;
543 DBG("element %p name %s", element, element->name);
545 if (element->device == NULL)
548 if (element->device->driver != NULL)
551 if (driver->type != element->device->type)
554 if (driver->probe(element->device) < 0)
557 element->device->driver = driver;
559 setup_device(element->device);
562 static void remove_device(struct connman_device *device)
564 DBG("device %p", device);
566 __connman_device_disable(device);
568 switch (device->mode) {
569 case CONNMAN_DEVICE_MODE_UNKNOWN:
570 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
571 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
575 __connman_technology_remove_device(device);
577 unregister_interface(&device->element);
579 if (device->driver->remove)
580 device->driver->remove(device);
582 device->driver = NULL;
585 static void remove_driver(struct connman_element *element, gpointer user_data)
587 struct connman_device_driver *driver = user_data;
589 DBG("element %p name %s", element, element->name);
591 if (element->device == NULL)
594 if (element->device->driver == driver)
595 remove_device(element->device);
598 connman_bool_t __connman_device_has_driver(struct connman_device *device)
600 if (device == NULL || device->driver == NULL)
603 return device->registered;
606 static GSList *driver_list = NULL;
608 static gint compare_priority(gconstpointer a, gconstpointer b)
610 const struct connman_device_driver *driver1 = a;
611 const struct connman_device_driver *driver2 = b;
613 return driver2->priority - driver1->priority;
617 * connman_device_driver_register:
618 * @driver: device driver definition
620 * Register a new device driver
622 * Returns: %0 on success
624 int connman_device_driver_register(struct connman_device_driver *driver)
626 DBG("driver %p name %s", driver, driver->name);
628 driver_list = g_slist_insert_sorted(driver_list, driver,
631 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
632 probe_driver, driver);
638 * connman_device_driver_unregister:
639 * @driver: device driver definition
641 * Remove a previously registered device driver
643 void connman_device_driver_unregister(struct connman_device_driver *driver)
645 DBG("driver %p name %s", driver, driver->name);
647 driver_list = g_slist_remove(driver_list, driver);
649 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
650 remove_driver, driver);
653 static void unregister_network(gpointer data)
655 struct connman_network *network = data;
657 DBG("network %p", network);
659 connman_element_unregister((struct connman_element *) network);
661 connman_network_unref(network);
664 static void device_destruct(struct connman_element *element)
666 struct connman_device *device = element->device;
668 DBG("element %p name %s", element, element->name);
670 if (device->timeout > 0) {
671 g_source_remove(device->timeout);
675 clear_scan_trigger(device);
677 if (device->pending != NULL) {
678 dbus_message_unref(device->pending);
679 device->pending = NULL;
682 g_free(device->ident);
683 g_free(device->node);
684 g_free(device->name);
685 g_free(device->address);
686 g_free(device->control);
687 g_free(device->interface);
689 g_free(device->last_network);
691 g_hash_table_destroy(device->networks);
692 device->networks = NULL;
696 * connman_device_create:
697 * @node: device node name (for example an address)
700 * Allocate a new device of given #type and assign the #node name to it.
702 * Returns: a newly-allocated #connman_device structure
704 struct connman_device *connman_device_create(const char *node,
705 enum connman_device_type type)
707 struct connman_device *device;
710 DBG("node %s type %d", node, type);
712 device = g_try_new0(struct connman_device, 1);
716 DBG("device %p", device);
718 __connman_element_initialize(&device->element);
720 device->element.name = g_strdup(node);
721 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
723 device->element.device = device;
724 device->element.destruct = device_destruct;
726 str = type2string(type);
728 connman_element_set_string(&device->element,
729 CONNMAN_PROPERTY_ID_TYPE, str);
731 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
734 device->name = g_strdup(type2description(device->type));
735 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
737 device->powered_persistent = TRUE;
739 device->phyindex = -1;
742 case CONNMAN_DEVICE_TYPE_UNKNOWN:
743 case CONNMAN_DEVICE_TYPE_VENDOR:
744 device->scan_interval = 0;
746 case CONNMAN_DEVICE_TYPE_ETHERNET:
747 case CONNMAN_DEVICE_TYPE_WIFI:
748 device->scan_interval = 300;
750 case CONNMAN_DEVICE_TYPE_WIMAX:
751 device->scan_interval = 0;
753 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
754 device->scan_interval = 0;
756 case CONNMAN_DEVICE_TYPE_GPS:
757 device->scan_interval = 0;
759 case CONNMAN_DEVICE_TYPE_CELLULAR:
760 case CONNMAN_DEVICE_TYPE_MBM:
761 device->scan_interval = 0;
765 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
766 g_free, unregister_network);
772 * connman_device_ref:
773 * @device: device structure
775 * Increase reference counter of device
777 struct connman_device *connman_device_ref(struct connman_device *device)
779 if (connman_element_ref(&device->element) == NULL)
786 * connman_device_unref:
787 * @device: device structure
789 * Decrease reference counter of device
791 void connman_device_unref(struct connman_device *device)
793 connman_element_unref(&device->element);
796 const char *__connman_device_get_type(struct connman_device *device)
798 return type2string(device->type);
802 * connman_device_get_type:
803 * @device: device structure
807 enum connman_device_type connman_device_get_type(struct connman_device *device)
813 * connman_device_get_name:
814 * @device: device structure
816 * Get unique name of device
818 const char *connman_device_get_name(struct connman_device *device)
820 return device->element.name;
824 * connman_device_get_path:
825 * @device: device structure
827 * Get path name of device
829 const char *connman_device_get_path(struct connman_device *device)
831 return device->element.path;
835 * connman_device_set_index:
836 * @device: device structure
837 * @index: index number
839 * Set index number of device
841 void connman_device_set_index(struct connman_device *device, int index)
843 device->element.index = index;
847 * connman_device_get_index:
848 * @device: device structure
850 * Get index number of device
852 int connman_device_get_index(struct connman_device *device)
854 return device->element.index;
857 int __connman_device_get_phyindex(struct connman_device *device)
859 return device->phyindex;
862 void __connman_device_set_phyindex(struct connman_device *device,
865 device->phyindex = phyindex;
869 * connman_device_set_interface:
870 * @device: device structure
871 * @interface: interface name
872 * @control: control interface
874 * Set interface name of device
876 void connman_device_set_interface(struct connman_device *device,
877 const char *interface, const char *control)
879 g_free(device->element.devname);
880 device->element.devname = g_strdup(interface);
882 g_free(device->interface);
883 device->interface = g_strdup(interface);
885 g_free(device->control);
886 device->control = g_strdup(control);
888 if (device->name == NULL) {
889 const char *str = type2description(device->type);
890 if (str != NULL && device->interface != NULL)
891 device->name = g_strdup_printf("%s (%s)", str,
896 const char *connman_device_get_control(struct connman_device *device)
898 return device->control;
902 * connman_device_set_ident:
903 * @device: device structure
904 * @ident: unique identifier
906 * Set unique identifier of device
908 void connman_device_set_ident(struct connman_device *device,
911 g_free(device->ident);
912 device->ident = g_strdup(ident);
915 const char *__connman_device_get_ident(struct connman_device *device)
917 return device->ident;
921 * connman_device_set_mode:
922 * @device: device structure
923 * @mode: network mode
925 * Change network mode of device
927 void connman_device_set_mode(struct connman_device *device,
928 enum connman_device_mode mode)
934 * connman_device_get_mode:
935 * @device: device structure
937 * Get network mode of device
939 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
945 * connman_device_set_powered:
946 * @device: device structure
947 * @powered: powered state
949 * Change power state of device
951 int connman_device_set_powered(struct connman_device *device,
952 connman_bool_t powered)
954 DBG("driver %p powered %d", device, powered);
956 if (device->timeout > 0) {
957 g_source_remove(device->timeout);
961 if (device->pending != NULL) {
962 g_dbus_send_reply(connection, device->pending,
965 dbus_message_unref(device->pending);
966 device->pending = NULL;
969 if (device->powered == powered)
972 device->powered = powered;
973 device->powered_pending = powered;
975 if (device->powered == TRUE)
976 __connman_technology_enable_device(device);
978 __connman_technology_disable_device(device);
980 if (device->registered == FALSE)
983 powered_changed(device);
985 if (powered == FALSE)
988 reset_scan_trigger(device);
990 if (device->driver->scan)
991 device->driver->scan(device);
996 int __connman_device_set_blocked(struct connman_device *device,
997 connman_bool_t blocked)
999 connman_bool_t powered;
1001 DBG("device %p blocked %d", device, blocked);
1003 device->blocked = blocked;
1005 if (device->offlinemode == TRUE)
1008 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1010 if (blocked == FALSE)
1011 powered = device->powered_persistent;
1015 return set_powered(device, powered);
1018 int __connman_device_scan(struct connman_device *device)
1020 if (!device->driver || !device->driver->scan)
1023 if (device->powered == FALSE)
1026 reset_scan_trigger(device);
1028 return device->driver->scan(device);
1031 int __connman_device_enable(struct connman_device *device)
1035 DBG("device %p", device);
1037 if (!device->driver || !device->driver->enable)
1040 if (device->powered_pending == TRUE)
1043 device->powered_pending = TRUE;
1045 err = device->driver->enable(device);
1049 device->powered = TRUE;
1051 __connman_technology_enable_device(device);
1056 int __connman_device_enable_persistent(struct connman_device *device)
1058 DBG("device %p", device);
1060 device->powered_persistent = TRUE;
1062 __connman_storage_save_device(device);
1064 return __connman_device_enable(device);
1067 int __connman_device_disable(struct connman_device *device)
1071 DBG("device %p", device);
1073 if (!device->driver || !device->driver->disable)
1076 if (device->powered == FALSE)
1079 if (device->powered_pending == FALSE)
1082 device->powered_pending = FALSE;
1084 device->reconnect = FALSE;
1086 clear_scan_trigger(device);
1088 g_hash_table_remove_all(device->networks);
1090 err = device->driver->disable(device);
1094 device->powered = FALSE;
1096 __connman_technology_disable_device(device);
1101 int __connman_device_disable_persistent(struct connman_device *device)
1103 DBG("device %p", device);
1105 device->powered_persistent = FALSE;
1107 __connman_storage_save_device(device);
1109 return __connman_device_disable(device);
1112 int __connman_device_disconnect(struct connman_device *device)
1114 GHashTableIter iter;
1115 gpointer key, value;
1117 DBG("device %p", device);
1119 connman_device_set_disconnected(device, TRUE);
1121 g_hash_table_iter_init(&iter, device->networks);
1123 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1124 struct connman_network *network = value;
1126 if (__connman_network_get_connecting(network) == TRUE) {
1128 * Skip network in the process of connecting.
1129 * This is a workaround for WiFi networks serviced
1130 * by the supplicant plugin that hold a reference
1131 * to the network. If we disconnect the network
1132 * here then the referenced object will not be
1133 * registered and usage (like launching DHCP client)
1134 * will fail. There is nothing to be gained by
1135 * removing the network here anyway.
1137 connman_warn("Skipping disconnect of %s",
1138 connman_network_get_identifier(network));
1142 __connman_network_disconnect(network);
1148 static void mark_network_unavailable(gpointer key, gpointer value,
1151 struct connman_network *network = value;
1153 if (connman_network_get_connected(network) == TRUE)
1156 connman_network_set_available(network, FALSE);
1159 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1162 struct connman_network *network = value;
1164 if (connman_network_get_connected(network) == TRUE)
1167 if (connman_network_get_available(network) == TRUE)
1173 void __connman_device_cleanup_networks(struct connman_device *device)
1175 g_hash_table_foreach_remove(device->networks,
1176 remove_unavailable_network, NULL);
1179 static void scanning_changed(struct connman_device *device)
1181 connman_dbus_property_changed_basic(device->element.path,
1182 CONNMAN_DEVICE_INTERFACE, "Scanning",
1183 DBUS_TYPE_BOOLEAN, &device->scanning);
1186 static void mark_network_available(gpointer key, gpointer value,
1189 struct connman_network *network = value;
1191 connman_network_set_available(network, TRUE);
1194 void connman_device_cleanup_scanning(struct connman_device *device)
1196 device->scanning = FALSE;
1198 scanning_changed(device);
1200 g_hash_table_foreach(device->networks,
1201 mark_network_available, NULL);
1205 * connman_device_set_scanning:
1206 * @device: device structure
1207 * @scanning: scanning state
1209 * Change scanning state of device
1211 int connman_device_set_scanning(struct connman_device *device,
1212 connman_bool_t scanning)
1214 DBG("device %p scanning %d", device, scanning);
1216 if (!device->driver || !device->driver->scan)
1219 if (device->scanning == scanning)
1222 device->scanning = scanning;
1224 scanning_changed(device);
1226 if (scanning == TRUE) {
1227 reset_scan_trigger(device);
1229 g_hash_table_foreach(device->networks,
1230 mark_network_unavailable, NULL);
1235 __connman_device_cleanup_networks(device);
1237 if (device->connections > 0)
1240 if (device->disconnected == TRUE)
1243 __connman_service_auto_connect();
1249 * connman_device_set_disconnected:
1250 * @device: device structure
1251 * @disconnected: disconnected state
1253 * Change disconnected state of device (only for device with networks)
1255 int connman_device_set_disconnected(struct connman_device *device,
1256 connman_bool_t disconnected)
1258 DBG("device %p disconnected %d", device, disconnected);
1260 switch (device->mode) {
1261 case CONNMAN_DEVICE_MODE_UNKNOWN:
1263 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1264 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1268 if (device->disconnected == disconnected)
1271 device->disconnected = disconnected;
1273 if (disconnected == TRUE)
1274 force_scan_trigger(device);
1280 * connman_device_get_disconnected:
1281 * @device: device structure
1283 * Get device disconnected state
1285 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1287 return device->disconnected;
1291 * connman_device_set_string:
1292 * @device: device structure
1293 * @key: unique identifier
1294 * @value: string value
1296 * Set string value for specific key
1298 int connman_device_set_string(struct connman_device *device,
1299 const char *key, const char *value)
1301 DBG("device %p key %s value %s", device, key, value);
1303 if (g_str_equal(key, "Address") == TRUE) {
1304 g_free(device->address);
1305 device->address = g_strdup(value);
1306 } else if (g_str_equal(key, "Name") == TRUE) {
1307 g_free(device->name);
1308 device->name = g_strdup(value);
1309 } else if (g_str_equal(key, "Node") == TRUE) {
1310 g_free(device->node);
1311 device->node = g_strdup(value);
1314 return connman_element_set_string(&device->element, key, value);
1318 * connman_device_get_string:
1319 * @device: device structure
1320 * @key: unique identifier
1322 * Get string value for specific key
1324 const char *connman_device_get_string(struct connman_device *device,
1327 DBG("device %p key %s", device, key);
1329 if (g_str_equal(key, "Address") == TRUE)
1330 return device->address;
1331 else if (g_str_equal(key, "Name") == TRUE)
1332 return device->name;
1333 else if (g_str_equal(key, "Node") == TRUE)
1334 return device->node;
1336 return connman_element_get_string(&device->element, key);
1339 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1341 struct connman_device *device = element->device;
1342 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1343 connman_bool_t powered;
1345 DBG("element %p name %s", element, element->name);
1350 device->offlinemode = offlinemode;
1352 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1354 if (device->powered == powered)
1357 if (device->powered_persistent == FALSE)
1360 set_powered(device, powered);
1363 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1365 DBG("offlinmode %d", offlinemode);
1367 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1368 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1370 __connman_notifier_offlinemode(offlinemode);
1375 void __connman_device_increase_connections(struct connman_device *device)
1377 device->connections++;
1380 void __connman_device_decrease_connections(struct connman_device *device)
1382 device->connections--;
1386 * connman_device_add_network:
1387 * @device: device structure
1388 * @network: network structure
1390 * Add new network to the device
1392 int connman_device_add_network(struct connman_device *device,
1393 struct connman_network *network)
1395 const char *identifier = connman_network_get_identifier(network);
1398 DBG("device %p network %p", device, network);
1400 if (identifier == NULL)
1403 switch (device->mode) {
1404 case CONNMAN_DEVICE_MODE_UNKNOWN:
1406 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1407 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1411 __connman_network_set_device(network, device);
1413 err = connman_element_register((struct connman_element *) network,
1416 __connman_network_set_device(network, NULL);
1420 g_hash_table_insert(device->networks, g_strdup(identifier),
1427 * connman_device_get_network:
1428 * @device: device structure
1429 * @identifier: network identifier
1431 * Get network for given identifier
1433 struct connman_network *connman_device_get_network(struct connman_device *device,
1434 const char *identifier)
1436 DBG("device %p identifier %s", device, identifier);
1438 return g_hash_table_lookup(device->networks, identifier);
1442 * connman_device_remove_network:
1443 * @device: device structure
1444 * @identifier: network identifier
1446 * Remove network for given identifier
1448 int connman_device_remove_network(struct connman_device *device,
1449 const char *identifier)
1451 DBG("device %p identifier %s", device, identifier);
1453 g_hash_table_remove(device->networks, identifier);
1458 void connman_device_remove_all_networks(struct connman_device *device)
1460 g_hash_table_remove_all(device->networks);
1463 void __connman_device_set_network(struct connman_device *device,
1464 struct connman_network *network)
1468 if (device->network == network)
1471 if (device->network != NULL)
1472 connman_network_unref(device->network);
1474 if (network != NULL) {
1475 name = connman_network_get_string(network,
1476 CONNMAN_PROPERTY_ID_NAME);
1477 g_free(device->last_network);
1478 device->last_network = g_strdup(name);
1480 device->network = connman_network_ref(network);
1482 g_free(device->last_network);
1483 device->last_network = NULL;
1485 device->network = NULL;
1489 void __connman_device_set_reconnect(struct connman_device *device,
1490 connman_bool_t reconnect)
1492 device->reconnect = reconnect;
1495 connman_bool_t __connman_device_get_reconnect(
1496 struct connman_device *device)
1498 return device->reconnect;
1502 * connman_device_register:
1503 * @device: device structure
1505 * Register device with the system
1507 int connman_device_register(struct connman_device *device)
1509 __connman_storage_load_device(device);
1511 device->offlinemode = __connman_profile_get_offlinemode();
1513 return connman_element_register(&device->element, NULL);
1517 * connman_device_unregister:
1518 * @device: device structure
1520 * Unregister device with the system
1522 void connman_device_unregister(struct connman_device *device)
1524 __connman_storage_save_device(device);
1526 connman_element_unregister(&device->element);
1530 * connman_device_get_data:
1531 * @device: device structure
1533 * Get private device data pointer
1535 void *connman_device_get_data(struct connman_device *device)
1537 return device->driver_data;
1541 * connman_device_set_data:
1542 * @device: device structure
1543 * @data: data pointer
1545 * Set private device data pointer
1547 void connman_device_set_data(struct connman_device *device, void *data)
1549 device->driver_data = data;
1552 static gboolean match_driver(struct connman_device *device,
1553 struct connman_device_driver *driver)
1555 if (device->type == driver->type ||
1556 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1562 static int device_probe(struct connman_element *element)
1564 struct connman_device *device = element->device;
1567 DBG("element %p name %s", element, element->name);
1572 if (device->driver != NULL)
1575 for (list = driver_list; list; list = list->next) {
1576 struct connman_device_driver *driver = list->data;
1578 if (match_driver(device, driver) == FALSE)
1581 DBG("driver %p name %s", driver, driver->name);
1583 if (driver->probe(device) == 0) {
1584 device->driver = driver;
1589 if (device->driver == NULL)
1592 return setup_device(device);
1595 static void device_remove(struct connman_element *element)
1597 struct connman_device *device = element->device;
1599 DBG("element %p name %s", element, element->name);
1604 if (device->driver == NULL)
1607 remove_device(device);
1610 static struct connman_driver device_driver = {
1612 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1613 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1614 .probe = device_probe,
1615 .remove = device_remove,
1618 static int device_load(struct connman_device *device)
1620 const char *ident = __connman_profile_active_ident();
1622 GError *error = NULL;
1624 connman_bool_t powered;
1627 DBG("device %p", device);
1629 keyfile = __connman_storage_open_profile(ident);
1630 if (keyfile == NULL)
1633 identifier = g_strdup_printf("device_%s", device->element.name);
1634 if (identifier == NULL)
1637 powered = g_key_file_get_boolean(keyfile, identifier,
1640 device->powered_persistent = powered;
1641 g_clear_error(&error);
1643 switch (device->mode) {
1644 case CONNMAN_DEVICE_MODE_UNKNOWN:
1646 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1647 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1648 val = g_key_file_get_integer(keyfile, identifier,
1649 "ScanInterval", &error);
1650 if (error == NULL && val > 0)
1651 device->scan_interval = val;
1652 g_clear_error(&error);
1659 __connman_storage_close_profile(ident, keyfile, FALSE);
1664 static int device_save(struct connman_device *device)
1666 const char *ident = __connman_profile_active_ident();
1670 DBG("device %p", device);
1672 keyfile = __connman_storage_open_profile(ident);
1673 if (keyfile == NULL)
1676 identifier = g_strdup_printf("device_%s", device->element.name);
1677 if (identifier == NULL)
1680 g_key_file_set_boolean(keyfile, identifier,
1681 "Powered", device->powered_persistent);
1683 switch (device->mode) {
1684 case CONNMAN_DEVICE_MODE_UNKNOWN:
1686 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1687 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1688 if (device->scan_interval > 0)
1689 g_key_file_set_integer(keyfile, identifier,
1690 "ScanInterval", device->scan_interval);
1697 __connman_storage_close_profile(ident, keyfile, TRUE);
1702 static struct connman_storage device_storage = {
1704 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1705 .device_load = device_load,
1706 .device_save = device_save,
1709 int __connman_device_init(void)
1713 connection = connman_dbus_get_connection();
1715 if (connman_storage_register(&device_storage) < 0)
1716 connman_error("Failed to register device storage");
1718 return connman_driver_register(&device_driver);
1721 void __connman_device_cleanup(void)
1725 connman_driver_unregister(&device_driver);
1727 connman_storage_unregister(&device_storage);
1729 dbus_connection_unref(connection);