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:
143 static const char *type2string(enum connman_device_type type)
146 case CONNMAN_DEVICE_TYPE_UNKNOWN:
147 case CONNMAN_DEVICE_TYPE_VENDOR:
149 case CONNMAN_DEVICE_TYPE_ETHERNET:
151 case CONNMAN_DEVICE_TYPE_WIFI:
153 case CONNMAN_DEVICE_TYPE_WIMAX:
155 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157 case CONNMAN_DEVICE_TYPE_GPS:
159 case CONNMAN_DEVICE_TYPE_CELLULAR:
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
168 enum connman_device_type type = connman_device_get_type(device);
171 case CONNMAN_DEVICE_TYPE_UNKNOWN:
172 case CONNMAN_DEVICE_TYPE_VENDOR:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_CELLULAR:
184 return CONNMAN_SERVICE_TYPE_CELLULAR;
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 static void powered_changed(struct connman_device *device)
192 connman_dbus_property_changed_basic(device->element.path,
193 CONNMAN_DEVICE_INTERFACE, "Powered",
194 DBUS_TYPE_BOOLEAN, &device->powered);
197 static int set_powered(struct connman_device *device, connman_bool_t powered)
199 struct connman_device_driver *driver = device->driver;
202 DBG("device %p powered %d", device, powered);
204 if (device->powered_pending == powered)
210 if (powered == TRUE) {
211 if (driver->enable) {
212 device->powered_pending = powered;
214 err = driver->enable(device);
216 __connman_technology_enable_device(device);
220 device->powered_pending = powered;
222 device->reconnect = FALSE;
224 clear_scan_trigger(device);
226 g_hash_table_remove_all(device->networks);
228 if (driver->disable) {
229 err = driver->disable(device);
231 __connman_technology_disable_device(device);
237 device->powered = powered;
239 if (device->registered == TRUE)
240 powered_changed(device);
246 void __connman_device_list(DBusMessageIter *iter, void *user_data)
248 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
251 static void append_path(gpointer key, gpointer value, gpointer user_data)
253 struct connman_element *element = value;
254 DBusMessageIter *iter = user_data;
256 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
260 static void append_networks(DBusMessageIter *iter, void *user_data)
262 struct connman_device *device = user_data;
264 g_hash_table_foreach(device->networks, append_path, iter);
267 static DBusMessage *get_properties(DBusConnection *conn,
268 DBusMessage *msg, void *data)
270 struct connman_device *device = data;
272 DBusMessageIter array, dict;
275 DBG("conn %p", conn);
277 if (__connman_security_check_privilege(msg,
278 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
279 return __connman_error_permission_denied(msg);
281 reply = dbus_message_new_method_return(msg);
285 dbus_message_iter_init_append(reply, &array);
287 connman_dbus_dict_open(&array, &dict);
289 if (device->name != NULL)
290 connman_dbus_dict_append_basic(&dict, "Name",
291 DBUS_TYPE_STRING, &device->name);
293 str = type2string(device->type);
295 connman_dbus_dict_append_basic(&dict, "Type",
296 DBUS_TYPE_STRING, &str);
298 if (device->address != NULL)
299 connman_dbus_dict_append_basic(&dict, "Address",
300 DBUS_TYPE_STRING, &device->address);
302 if (device->interface != NULL)
303 connman_dbus_dict_append_basic(&dict, "Interface",
304 DBUS_TYPE_STRING, &device->interface);
306 connman_dbus_dict_append_basic(&dict, "Powered",
307 DBUS_TYPE_BOOLEAN, &device->powered);
309 if (device->driver && device->driver->scan)
310 connman_dbus_dict_append_basic(&dict, "Scanning",
311 DBUS_TYPE_BOOLEAN, &device->scanning);
313 switch (device->mode) {
314 case CONNMAN_DEVICE_MODE_UNKNOWN:
316 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
317 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
318 if (device->scan_interval > 0)
319 connman_dbus_dict_append_basic(&dict, "ScanInterval",
320 DBUS_TYPE_UINT16, &device->scan_interval);
322 connman_dbus_dict_append_array(&dict, "Networks",
323 DBUS_TYPE_OBJECT_PATH, append_networks, device);
327 connman_dbus_dict_close(&array, &dict);
332 static gboolean powered_timeout(gpointer user_data)
334 struct connman_device *device = user_data;
336 DBG("device %p", device);
340 if (device->pending != NULL) {
343 reply = __connman_error_operation_timeout(device->pending);
345 g_dbus_send_message(connection, reply);
347 dbus_message_unref(device->pending);
348 device->pending = NULL;
354 static DBusMessage *set_property(DBusConnection *conn,
355 DBusMessage *msg, void *data)
357 struct connman_device *device = data;
358 DBusMessageIter iter, value;
362 DBG("conn %p", conn);
364 if (dbus_message_iter_init(msg, &iter) == FALSE)
365 return __connman_error_invalid_arguments(msg);
367 dbus_message_iter_get_basic(&iter, &name);
368 dbus_message_iter_next(&iter);
369 dbus_message_iter_recurse(&iter, &value);
371 if (__connman_security_check_privilege(msg,
372 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
373 return __connman_error_permission_denied(msg);
375 type = dbus_message_iter_get_arg_type(&value);
377 if (g_str_equal(name, "Powered") == TRUE) {
378 connman_bool_t powered;
381 if (type != DBUS_TYPE_BOOLEAN)
382 return __connman_error_invalid_arguments(msg);
384 dbus_message_iter_get_basic(&value, &powered);
386 device->powered_persistent = powered;
388 __connman_storage_save_device(device);
390 if (device->powered == powered)
391 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
393 if (device->pending != NULL)
394 return __connman_error_in_progress(msg);
396 err = set_powered(device, powered);
398 if (err != -EINPROGRESS)
399 return __connman_error_failed(msg, -err);
401 device->pending = dbus_message_ref(msg);
403 device->timeout = g_timeout_add_seconds(15,
404 powered_timeout, device);
408 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
409 connman_uint16_t interval;
411 switch (device->mode) {
412 case CONNMAN_DEVICE_MODE_UNKNOWN:
413 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
414 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
418 if (type != DBUS_TYPE_UINT16)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &interval);
423 if (device->scan_interval != interval) {
424 device->scan_interval = interval;
426 __connman_storage_save_device(device);
428 reset_scan_trigger(device);
431 return __connman_error_invalid_property(msg);
433 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
436 static DBusMessage *propose_scan(DBusConnection *conn,
437 DBusMessage *msg, void *data)
439 struct connman_device *device = data;
442 DBG("conn %p", conn);
444 switch (device->mode) {
445 case CONNMAN_DEVICE_MODE_UNKNOWN:
446 return __connman_error_not_supported(msg);
447 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
448 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
452 err = __connman_device_scan(device);
454 return __connman_error_failed(msg, -err);
456 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
459 static GDBusMethodTable device_methods[] = {
460 { "GetProperties", "", "a{sv}", get_properties },
461 { "SetProperty", "sv", "", set_property,
462 G_DBUS_METHOD_FLAG_ASYNC },
463 { "ProposeScan", "", "", propose_scan },
467 static GDBusSignalTable device_signals[] = {
468 { "PropertyChanged", "sv" },
472 static int register_interface(struct connman_element *element)
474 struct connman_device *device = element->device;
476 DBG("element %p name %s", element, element->name);
478 if (g_dbus_register_interface(connection, element->path,
479 CONNMAN_DEVICE_INTERFACE,
480 device_methods, device_signals,
481 NULL, device, NULL) == FALSE) {
482 connman_error("Failed to register %s device", element->path);
486 device->registered = TRUE;
491 static void unregister_interface(struct connman_element *element)
493 struct connman_device *device = element->device;
495 DBG("element %p name %s", element, element->name);
497 device->registered = FALSE;
499 g_dbus_unregister_interface(connection, element->path,
500 CONNMAN_DEVICE_INTERFACE);
503 static int setup_device(struct connman_device *device)
507 DBG("device %p", device);
509 err = register_interface(&device->element);
511 if (device->driver->remove)
512 device->driver->remove(device);
513 device->driver = NULL;
517 __connman_technology_add_device(device);
519 switch (device->mode) {
520 case CONNMAN_DEVICE_MODE_UNKNOWN:
521 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
522 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
526 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
529 if (device->offlinemode == FALSE &&
530 device->powered_persistent == TRUE)
531 __connman_device_enable(device);
536 static void probe_driver(struct connman_element *element, gpointer user_data)
538 struct connman_device_driver *driver = user_data;
540 DBG("element %p name %s", element, element->name);
542 if (element->device == NULL)
545 if (element->device->driver != NULL)
548 if (driver->type != element->device->type)
551 if (driver->probe(element->device) < 0)
554 element->device->driver = driver;
556 setup_device(element->device);
559 static void remove_device(struct connman_device *device)
561 DBG("device %p", device);
563 __connman_device_disable(device);
565 switch (device->mode) {
566 case CONNMAN_DEVICE_MODE_UNKNOWN:
567 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
568 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
572 __connman_technology_remove_device(device);
574 unregister_interface(&device->element);
576 if (device->driver->remove)
577 device->driver->remove(device);
579 device->driver = NULL;
582 static void remove_driver(struct connman_element *element, gpointer user_data)
584 struct connman_device_driver *driver = user_data;
586 DBG("element %p name %s", element, element->name);
588 if (element->device == NULL)
591 if (element->device->driver == driver)
592 remove_device(element->device);
595 connman_bool_t __connman_device_has_driver(struct connman_device *device)
597 if (device == NULL || device->driver == NULL)
600 return device->registered;
603 static GSList *driver_list = NULL;
605 static gint compare_priority(gconstpointer a, gconstpointer b)
607 const struct connman_device_driver *driver1 = a;
608 const struct connman_device_driver *driver2 = b;
610 return driver2->priority - driver1->priority;
614 * connman_device_driver_register:
615 * @driver: device driver definition
617 * Register a new device driver
619 * Returns: %0 on success
621 int connman_device_driver_register(struct connman_device_driver *driver)
623 DBG("driver %p name %s", driver, driver->name);
625 driver_list = g_slist_insert_sorted(driver_list, driver,
628 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
629 probe_driver, driver);
635 * connman_device_driver_unregister:
636 * @driver: device driver definition
638 * Remove a previously registered device driver
640 void connman_device_driver_unregister(struct connman_device_driver *driver)
642 DBG("driver %p name %s", driver, driver->name);
644 driver_list = g_slist_remove(driver_list, driver);
646 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
647 remove_driver, driver);
650 static void unregister_network(gpointer data)
652 struct connman_network *network = data;
654 DBG("network %p", network);
656 connman_element_unregister((struct connman_element *) network);
658 connman_network_unref(network);
661 static void device_destruct(struct connman_element *element)
663 struct connman_device *device = element->device;
665 DBG("element %p name %s", element, element->name);
667 if (device->timeout > 0) {
668 g_source_remove(device->timeout);
672 clear_scan_trigger(device);
674 if (device->pending != NULL) {
675 dbus_message_unref(device->pending);
676 device->pending = NULL;
679 g_free(device->ident);
680 g_free(device->node);
681 g_free(device->name);
682 g_free(device->address);
683 g_free(device->control);
684 g_free(device->interface);
686 g_free(device->last_network);
688 g_hash_table_destroy(device->networks);
689 device->networks = NULL;
693 * connman_device_create:
694 * @node: device node name (for example an address)
697 * Allocate a new device of given #type and assign the #node name to it.
699 * Returns: a newly-allocated #connman_device structure
701 struct connman_device *connman_device_create(const char *node,
702 enum connman_device_type type)
704 struct connman_device *device;
707 DBG("node %s type %d", node, type);
709 device = g_try_new0(struct connman_device, 1);
713 DBG("device %p", device);
715 __connman_element_initialize(&device->element);
717 device->element.name = g_strdup(node);
718 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
720 device->element.device = device;
721 device->element.destruct = device_destruct;
723 str = type2string(type);
725 connman_element_set_string(&device->element,
726 CONNMAN_PROPERTY_ID_TYPE, str);
728 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
731 device->name = g_strdup(type2description(device->type));
732 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
734 device->powered_persistent = TRUE;
736 device->phyindex = -1;
739 case CONNMAN_DEVICE_TYPE_UNKNOWN:
740 case CONNMAN_DEVICE_TYPE_VENDOR:
741 device->scan_interval = 0;
743 case CONNMAN_DEVICE_TYPE_ETHERNET:
744 case CONNMAN_DEVICE_TYPE_WIFI:
745 device->scan_interval = 300;
747 case CONNMAN_DEVICE_TYPE_WIMAX:
748 device->scan_interval = 0;
750 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
751 device->scan_interval = 0;
753 case CONNMAN_DEVICE_TYPE_GPS:
754 device->scan_interval = 0;
756 case CONNMAN_DEVICE_TYPE_CELLULAR:
757 device->scan_interval = 0;
761 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
762 g_free, unregister_network);
768 * connman_device_ref:
769 * @device: device structure
771 * Increase reference counter of device
773 struct connman_device *connman_device_ref(struct connman_device *device)
775 if (connman_element_ref(&device->element) == NULL)
782 * connman_device_unref:
783 * @device: device structure
785 * Decrease reference counter of device
787 void connman_device_unref(struct connman_device *device)
789 connman_element_unref(&device->element);
792 const char *__connman_device_get_type(struct connman_device *device)
794 return type2string(device->type);
798 * connman_device_get_type:
799 * @device: device structure
803 enum connman_device_type connman_device_get_type(struct connman_device *device)
809 * connman_device_get_name:
810 * @device: device structure
812 * Get unique name of device
814 const char *connman_device_get_name(struct connman_device *device)
816 return device->element.name;
820 * connman_device_get_path:
821 * @device: device structure
823 * Get path name of device
825 const char *connman_device_get_path(struct connman_device *device)
827 return device->element.path;
831 * connman_device_set_index:
832 * @device: device structure
833 * @index: index number
835 * Set index number of device
837 void connman_device_set_index(struct connman_device *device, int index)
839 device->element.index = index;
843 * connman_device_get_index:
844 * @device: device structure
846 * Get index number of device
848 int connman_device_get_index(struct connman_device *device)
850 return device->element.index;
853 int __connman_device_get_phyindex(struct connman_device *device)
855 return device->phyindex;
858 void __connman_device_set_phyindex(struct connman_device *device,
861 device->phyindex = phyindex;
865 * connman_device_set_interface:
866 * @device: device structure
867 * @interface: interface name
868 * @control: control interface
870 * Set interface name of device
872 void connman_device_set_interface(struct connman_device *device,
873 const char *interface, const char *control)
875 g_free(device->element.devname);
876 device->element.devname = g_strdup(interface);
878 g_free(device->interface);
879 device->interface = g_strdup(interface);
881 g_free(device->control);
882 device->control = g_strdup(control);
884 if (device->name == NULL) {
885 const char *str = type2description(device->type);
886 if (str != NULL && device->interface != NULL)
887 device->name = g_strdup_printf("%s (%s)", str,
892 const char *connman_device_get_control(struct connman_device *device)
894 return device->control;
898 * connman_device_set_ident:
899 * @device: device structure
900 * @ident: unique identifier
902 * Set unique identifier of device
904 void connman_device_set_ident(struct connman_device *device,
907 g_free(device->ident);
908 device->ident = g_strdup(ident);
911 const char *__connman_device_get_ident(struct connman_device *device)
913 return device->ident;
917 * connman_device_set_mode:
918 * @device: device structure
919 * @mode: network mode
921 * Change network mode of device
923 void connman_device_set_mode(struct connman_device *device,
924 enum connman_device_mode mode)
930 * connman_device_get_mode:
931 * @device: device structure
933 * Get network mode of device
935 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
941 * connman_device_set_powered:
942 * @device: device structure
943 * @powered: powered state
945 * Change power state of device
947 int connman_device_set_powered(struct connman_device *device,
948 connman_bool_t powered)
950 DBG("driver %p powered %d", device, powered);
952 if (device->timeout > 0) {
953 g_source_remove(device->timeout);
957 if (device->pending != NULL) {
958 g_dbus_send_reply(connection, device->pending,
961 dbus_message_unref(device->pending);
962 device->pending = NULL;
965 if (device->powered == powered)
968 device->powered = powered;
969 device->powered_pending = powered;
971 if (device->powered == TRUE)
972 __connman_technology_enable_device(device);
974 __connman_technology_disable_device(device);
976 if (device->registered == FALSE)
979 powered_changed(device);
981 if (powered == FALSE)
984 reset_scan_trigger(device);
986 if (device->driver->scan)
987 device->driver->scan(device);
992 int __connman_device_set_blocked(struct connman_device *device,
993 connman_bool_t blocked)
995 connman_bool_t powered;
997 DBG("device %p blocked %d", device, blocked);
999 device->blocked = blocked;
1001 if (device->offlinemode == TRUE)
1004 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1006 if (blocked == FALSE)
1007 powered = device->powered_persistent;
1011 return set_powered(device, powered);
1014 int __connman_device_scan(struct connman_device *device)
1016 if (!device->driver || !device->driver->scan)
1019 if (device->powered == FALSE)
1022 reset_scan_trigger(device);
1024 return device->driver->scan(device);
1027 int __connman_device_enable(struct connman_device *device)
1031 DBG("device %p", device);
1033 if (!device->driver || !device->driver->enable)
1036 if (device->powered_pending == TRUE)
1039 device->powered_pending = TRUE;
1041 err = device->driver->enable(device);
1045 device->powered = TRUE;
1047 __connman_technology_enable_device(device);
1052 int __connman_device_enable_persistent(struct connman_device *device)
1054 DBG("device %p", device);
1056 device->powered_persistent = TRUE;
1058 __connman_storage_save_device(device);
1060 return __connman_device_enable(device);
1063 int __connman_device_disable(struct connman_device *device)
1067 DBG("device %p", device);
1069 if (!device->driver || !device->driver->disable)
1072 if (device->powered == FALSE)
1075 if (device->powered_pending == FALSE)
1078 device->powered_pending = FALSE;
1080 device->reconnect = FALSE;
1082 clear_scan_trigger(device);
1084 g_hash_table_remove_all(device->networks);
1086 err = device->driver->disable(device);
1090 device->powered = FALSE;
1092 __connman_technology_disable_device(device);
1097 int __connman_device_disable_persistent(struct connman_device *device)
1099 DBG("device %p", device);
1101 device->powered_persistent = FALSE;
1103 __connman_storage_save_device(device);
1105 return __connman_device_disable(device);
1108 int __connman_device_disconnect(struct connman_device *device)
1110 GHashTableIter iter;
1111 gpointer key, value;
1113 DBG("device %p", device);
1115 connman_device_set_disconnected(device, TRUE);
1117 g_hash_table_iter_init(&iter, device->networks);
1119 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1120 struct connman_network *network = value;
1122 if (__connman_network_get_connecting(network) == TRUE) {
1124 * Skip network in the process of connecting.
1125 * This is a workaround for WiFi networks serviced
1126 * by the supplicant plugin that hold a reference
1127 * to the network. If we disconnect the network
1128 * here then the referenced object will not be
1129 * registered and usage (like launching DHCP client)
1130 * will fail. There is nothing to be gained by
1131 * removing the network here anyway.
1133 connman_warn("Skipping disconnect of %s",
1134 connman_network_get_identifier(network));
1138 __connman_network_disconnect(network);
1144 static void mark_network_unavailable(gpointer key, gpointer value,
1147 struct connman_network *network = value;
1149 if (connman_network_get_connected(network) == TRUE)
1152 connman_network_set_available(network, FALSE);
1155 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1158 struct connman_network *network = value;
1160 if (connman_network_get_connected(network) == TRUE)
1163 if (connman_network_get_available(network) == TRUE)
1169 void __connman_device_cleanup_networks(struct connman_device *device)
1171 g_hash_table_foreach_remove(device->networks,
1172 remove_unavailable_network, NULL);
1175 static void scanning_changed(struct connman_device *device)
1177 connman_dbus_property_changed_basic(device->element.path,
1178 CONNMAN_DEVICE_INTERFACE, "Scanning",
1179 DBUS_TYPE_BOOLEAN, &device->scanning);
1182 static void mark_network_available(gpointer key, gpointer value,
1185 struct connman_network *network = value;
1187 connman_network_set_available(network, TRUE);
1190 void connman_device_cleanup_scanning(struct connman_device *device)
1192 device->scanning = FALSE;
1194 scanning_changed(device);
1196 g_hash_table_foreach(device->networks,
1197 mark_network_available, NULL);
1201 * connman_device_set_scanning:
1202 * @device: device structure
1203 * @scanning: scanning state
1205 * Change scanning state of device
1207 int connman_device_set_scanning(struct connman_device *device,
1208 connman_bool_t scanning)
1210 DBG("device %p scanning %d", device, scanning);
1212 if (!device->driver || !device->driver->scan)
1215 if (device->scanning == scanning)
1218 device->scanning = scanning;
1220 scanning_changed(device);
1222 if (scanning == TRUE) {
1223 reset_scan_trigger(device);
1225 g_hash_table_foreach(device->networks,
1226 mark_network_unavailable, NULL);
1231 __connman_device_cleanup_networks(device);
1233 if (device->connections > 0)
1236 if (device->disconnected == TRUE)
1239 __connman_service_auto_connect();
1245 * connman_device_set_disconnected:
1246 * @device: device structure
1247 * @disconnected: disconnected state
1249 * Change disconnected state of device (only for device with networks)
1251 int connman_device_set_disconnected(struct connman_device *device,
1252 connman_bool_t disconnected)
1254 DBG("device %p disconnected %d", device, disconnected);
1256 switch (device->mode) {
1257 case CONNMAN_DEVICE_MODE_UNKNOWN:
1259 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1260 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1264 if (device->disconnected == disconnected)
1267 device->disconnected = disconnected;
1269 if (disconnected == TRUE)
1270 force_scan_trigger(device);
1276 * connman_device_get_disconnected:
1277 * @device: device structure
1279 * Get device disconnected state
1281 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1283 return device->disconnected;
1287 * connman_device_set_string:
1288 * @device: device structure
1289 * @key: unique identifier
1290 * @value: string value
1292 * Set string value for specific key
1294 int connman_device_set_string(struct connman_device *device,
1295 const char *key, const char *value)
1297 DBG("device %p key %s value %s", device, key, value);
1299 if (g_str_equal(key, "Address") == TRUE) {
1300 g_free(device->address);
1301 device->address = g_strdup(value);
1302 } else if (g_str_equal(key, "Name") == TRUE) {
1303 g_free(device->name);
1304 device->name = g_strdup(value);
1305 } else if (g_str_equal(key, "Node") == TRUE) {
1306 g_free(device->node);
1307 device->node = g_strdup(value);
1310 return connman_element_set_string(&device->element, key, value);
1314 * connman_device_get_string:
1315 * @device: device structure
1316 * @key: unique identifier
1318 * Get string value for specific key
1320 const char *connman_device_get_string(struct connman_device *device,
1323 DBG("device %p key %s", device, key);
1325 if (g_str_equal(key, "Address") == TRUE)
1326 return device->address;
1327 else if (g_str_equal(key, "Name") == TRUE)
1328 return device->name;
1329 else if (g_str_equal(key, "Node") == TRUE)
1330 return device->node;
1332 return connman_element_get_string(&device->element, key);
1335 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1337 struct connman_device *device = element->device;
1338 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1339 connman_bool_t powered;
1341 DBG("element %p name %s", element, element->name);
1346 device->offlinemode = offlinemode;
1348 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1350 if (device->powered == powered)
1353 if (device->powered_persistent == FALSE)
1356 set_powered(device, powered);
1359 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1361 DBG("offlinmode %d", offlinemode);
1363 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1364 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1366 __connman_notifier_offlinemode(offlinemode);
1371 void __connman_device_increase_connections(struct connman_device *device)
1373 device->connections++;
1376 void __connman_device_decrease_connections(struct connman_device *device)
1378 device->connections--;
1382 * connman_device_add_network:
1383 * @device: device structure
1384 * @network: network structure
1386 * Add new network to the device
1388 int connman_device_add_network(struct connman_device *device,
1389 struct connman_network *network)
1391 const char *identifier = connman_network_get_identifier(network);
1394 DBG("device %p network %p", device, network);
1396 if (identifier == NULL)
1399 switch (device->mode) {
1400 case CONNMAN_DEVICE_MODE_UNKNOWN:
1402 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1403 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1407 __connman_network_set_device(network, device);
1409 err = connman_element_register((struct connman_element *) network,
1412 __connman_network_set_device(network, NULL);
1416 g_hash_table_insert(device->networks, g_strdup(identifier),
1423 * connman_device_get_network:
1424 * @device: device structure
1425 * @identifier: network identifier
1427 * Get network for given identifier
1429 struct connman_network *connman_device_get_network(struct connman_device *device,
1430 const char *identifier)
1432 DBG("device %p identifier %s", device, identifier);
1434 return g_hash_table_lookup(device->networks, identifier);
1438 * connman_device_remove_network:
1439 * @device: device structure
1440 * @identifier: network identifier
1442 * Remove network for given identifier
1444 int connman_device_remove_network(struct connman_device *device,
1445 const char *identifier)
1447 DBG("device %p identifier %s", device, identifier);
1449 g_hash_table_remove(device->networks, identifier);
1454 void connman_device_remove_all_networks(struct connman_device *device)
1456 g_hash_table_remove_all(device->networks);
1459 void __connman_device_set_network(struct connman_device *device,
1460 struct connman_network *network)
1464 if (device->network == network)
1467 if (device->network != NULL)
1468 connman_network_unref(device->network);
1470 if (network != NULL) {
1471 name = connman_network_get_string(network,
1472 CONNMAN_PROPERTY_ID_NAME);
1473 g_free(device->last_network);
1474 device->last_network = g_strdup(name);
1476 device->network = connman_network_ref(network);
1478 g_free(device->last_network);
1479 device->last_network = NULL;
1481 device->network = NULL;
1485 void __connman_device_set_reconnect(struct connman_device *device,
1486 connman_bool_t reconnect)
1488 device->reconnect = reconnect;
1491 connman_bool_t __connman_device_get_reconnect(
1492 struct connman_device *device)
1494 return device->reconnect;
1498 * connman_device_register:
1499 * @device: device structure
1501 * Register device with the system
1503 int connman_device_register(struct connman_device *device)
1505 __connman_storage_load_device(device);
1507 device->offlinemode = __connman_profile_get_offlinemode();
1509 return connman_element_register(&device->element, NULL);
1513 * connman_device_unregister:
1514 * @device: device structure
1516 * Unregister device with the system
1518 void connman_device_unregister(struct connman_device *device)
1520 __connman_storage_save_device(device);
1522 connman_element_unregister(&device->element);
1526 * connman_device_get_data:
1527 * @device: device structure
1529 * Get private device data pointer
1531 void *connman_device_get_data(struct connman_device *device)
1533 return device->driver_data;
1537 * connman_device_set_data:
1538 * @device: device structure
1539 * @data: data pointer
1541 * Set private device data pointer
1543 void connman_device_set_data(struct connman_device *device, void *data)
1545 device->driver_data = data;
1548 static gboolean match_driver(struct connman_device *device,
1549 struct connman_device_driver *driver)
1551 if (device->type == driver->type ||
1552 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1558 static int device_probe(struct connman_element *element)
1560 struct connman_device *device = element->device;
1563 DBG("element %p name %s", element, element->name);
1568 if (device->driver != NULL)
1571 for (list = driver_list; list; list = list->next) {
1572 struct connman_device_driver *driver = list->data;
1574 if (match_driver(device, driver) == FALSE)
1577 DBG("driver %p name %s", driver, driver->name);
1579 if (driver->probe(device) == 0) {
1580 device->driver = driver;
1585 if (device->driver == NULL)
1588 return setup_device(device);
1591 static void device_remove(struct connman_element *element)
1593 struct connman_device *device = element->device;
1595 DBG("element %p name %s", element, element->name);
1600 if (device->driver == NULL)
1603 remove_device(device);
1606 static struct connman_driver device_driver = {
1608 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1609 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1610 .probe = device_probe,
1611 .remove = device_remove,
1614 static int device_load(struct connman_device *device)
1616 const char *ident = __connman_profile_active_ident();
1618 GError *error = NULL;
1620 connman_bool_t powered;
1623 DBG("device %p", device);
1625 keyfile = __connman_storage_open_profile(ident);
1626 if (keyfile == NULL)
1629 identifier = g_strdup_printf("device_%s", device->element.name);
1630 if (identifier == NULL)
1633 powered = g_key_file_get_boolean(keyfile, identifier,
1636 device->powered_persistent = powered;
1637 g_clear_error(&error);
1639 switch (device->mode) {
1640 case CONNMAN_DEVICE_MODE_UNKNOWN:
1642 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1643 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1644 val = g_key_file_get_integer(keyfile, identifier,
1645 "ScanInterval", &error);
1646 if (error == NULL && val > 0)
1647 device->scan_interval = val;
1648 g_clear_error(&error);
1655 __connman_storage_close_profile(ident, keyfile, FALSE);
1660 static int device_save(struct connman_device *device)
1662 const char *ident = __connman_profile_active_ident();
1666 DBG("device %p", device);
1668 keyfile = __connman_storage_open_profile(ident);
1669 if (keyfile == NULL)
1672 identifier = g_strdup_printf("device_%s", device->element.name);
1673 if (identifier == NULL)
1676 g_key_file_set_boolean(keyfile, identifier,
1677 "Powered", device->powered_persistent);
1679 switch (device->mode) {
1680 case CONNMAN_DEVICE_MODE_UNKNOWN:
1682 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1683 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1684 if (device->scan_interval > 0)
1685 g_key_file_set_integer(keyfile, identifier,
1686 "ScanInterval", device->scan_interval);
1693 __connman_storage_close_profile(ident, keyfile, TRUE);
1698 static struct connman_storage device_storage = {
1700 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1701 .device_load = device_load,
1702 .device_save = device_save,
1705 int __connman_device_init(void)
1709 connection = connman_dbus_get_connection();
1711 if (connman_storage_register(&device_storage) < 0)
1712 connman_error("Failed to register device storage");
1714 return connman_driver_register(&device_driver);
1717 void __connman_device_cleanup(void)
1721 connman_driver_unregister(&device_driver);
1723 connman_storage_unregister(&device_storage);
1725 dbus_connection_unref(connection);