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 void blocked_changed(struct connman_device *device)
199 connman_dbus_property_changed_basic(device->element.path,
200 CONNMAN_DEVICE_INTERFACE, "Blocked",
201 DBUS_TYPE_BOOLEAN, &device->blocked);
204 int __connman_device_enable(struct connman_device *device)
208 DBG("device %p", device);
210 if (!device->driver || !device->driver->enable)
213 if (device->powered_pending == TRUE)
216 if (device->blocked == TRUE)
219 err = device->driver->enable(device);
221 if (err == -EINPROGRESS)
222 device->powered_pending = TRUE;
226 device->powered_pending = TRUE;
227 device->powered = TRUE;
229 __connman_technology_enable_device(device);
234 int __connman_device_disable(struct connman_device *device)
238 DBG("device %p", device);
240 if (!device->driver || !device->driver->disable)
243 if (device->powered == FALSE)
246 if (device->powered_pending == FALSE)
249 device->reconnect = FALSE;
251 clear_scan_trigger(device);
253 g_hash_table_remove_all(device->networks);
255 err = device->driver->disable(device);
257 if (err == -EINPROGRESS)
258 device->powered_pending = FALSE;
262 device->powered_pending = FALSE;
263 device->powered = FALSE;
265 __connman_technology_disable_device(device);
270 static int set_powered(struct connman_device *device, connman_bool_t powered)
272 DBG("device %p powered %d", device, powered);
275 return __connman_device_enable(device);
277 return __connman_device_disable(device);
280 void __connman_device_list(DBusMessageIter *iter, void *user_data)
282 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
285 static void append_path(gpointer key, gpointer value, gpointer user_data)
287 struct connman_element *element = value;
288 DBusMessageIter *iter = user_data;
290 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
294 static void append_networks(DBusMessageIter *iter, void *user_data)
296 struct connman_device *device = user_data;
298 g_hash_table_foreach(device->networks, append_path, iter);
301 static DBusMessage *get_properties(DBusConnection *conn,
302 DBusMessage *msg, void *data)
304 struct connman_device *device = data;
306 DBusMessageIter array, dict;
309 DBG("conn %p", conn);
311 if (__connman_security_check_privilege(msg,
312 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
313 return __connman_error_permission_denied(msg);
315 reply = dbus_message_new_method_return(msg);
319 dbus_message_iter_init_append(reply, &array);
321 connman_dbus_dict_open(&array, &dict);
323 if (device->name != NULL)
324 connman_dbus_dict_append_basic(&dict, "Name",
325 DBUS_TYPE_STRING, &device->name);
327 str = type2string(device->type);
329 connman_dbus_dict_append_basic(&dict, "Type",
330 DBUS_TYPE_STRING, &str);
332 if (device->address != NULL)
333 connman_dbus_dict_append_basic(&dict, "Address",
334 DBUS_TYPE_STRING, &device->address);
336 if (device->interface != NULL)
337 connman_dbus_dict_append_basic(&dict, "Interface",
338 DBUS_TYPE_STRING, &device->interface);
340 connman_dbus_dict_append_basic(&dict, "Powered",
341 DBUS_TYPE_BOOLEAN, &device->powered);
343 connman_dbus_dict_append_basic(&dict, "Blocked",
344 DBUS_TYPE_BOOLEAN, &device->blocked);
346 if (device->driver && device->driver->scan)
347 connman_dbus_dict_append_basic(&dict, "Scanning",
348 DBUS_TYPE_BOOLEAN, &device->scanning);
350 switch (device->mode) {
351 case CONNMAN_DEVICE_MODE_UNKNOWN:
353 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
354 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
355 if (device->scan_interval > 0)
356 connman_dbus_dict_append_basic(&dict, "ScanInterval",
357 DBUS_TYPE_UINT16, &device->scan_interval);
359 connman_dbus_dict_append_array(&dict, "Networks",
360 DBUS_TYPE_OBJECT_PATH, append_networks, device);
364 connman_dbus_dict_close(&array, &dict);
369 static gboolean powered_timeout(gpointer user_data)
371 struct connman_device *device = user_data;
373 DBG("device %p", device);
377 if (device->pending != NULL) {
380 reply = __connman_error_operation_timeout(device->pending);
382 g_dbus_send_message(connection, reply);
384 dbus_message_unref(device->pending);
385 device->pending = NULL;
391 static DBusMessage *set_property(DBusConnection *conn,
392 DBusMessage *msg, void *data)
394 struct connman_device *device = data;
395 DBusMessageIter iter, value;
399 DBG("conn %p", conn);
401 if (dbus_message_iter_init(msg, &iter) == FALSE)
402 return __connman_error_invalid_arguments(msg);
404 dbus_message_iter_get_basic(&iter, &name);
405 dbus_message_iter_next(&iter);
406 dbus_message_iter_recurse(&iter, &value);
408 if (__connman_security_check_privilege(msg,
409 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
410 return __connman_error_permission_denied(msg);
412 type = dbus_message_iter_get_arg_type(&value);
414 if (g_str_equal(name, "Powered") == TRUE) {
415 connman_bool_t powered;
418 if (type != DBUS_TYPE_BOOLEAN)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &powered);
423 device->powered_persistent = powered;
425 __connman_storage_save_device(device);
427 if (device->powered == powered)
428 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
430 if (device->pending != NULL)
431 return __connman_error_in_progress(msg);
433 err = set_powered(device, powered);
435 if (err != -EINPROGRESS)
436 return __connman_error_failed(msg, -err);
438 device->pending = dbus_message_ref(msg);
440 device->timeout = g_timeout_add_seconds(15,
441 powered_timeout, device);
445 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
446 connman_uint16_t interval;
448 switch (device->mode) {
449 case CONNMAN_DEVICE_MODE_UNKNOWN:
450 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
451 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
455 if (type != DBUS_TYPE_UINT16)
456 return __connman_error_invalid_arguments(msg);
458 dbus_message_iter_get_basic(&value, &interval);
460 if (device->scan_interval != interval) {
461 device->scan_interval = interval;
463 __connman_storage_save_device(device);
465 reset_scan_trigger(device);
468 return __connman_error_invalid_property(msg);
470 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
473 static DBusMessage *propose_scan(DBusConnection *conn,
474 DBusMessage *msg, void *data)
476 struct connman_device *device = data;
479 DBG("conn %p", conn);
481 switch (device->mode) {
482 case CONNMAN_DEVICE_MODE_UNKNOWN:
483 return __connman_error_not_supported(msg);
484 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
485 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
489 err = __connman_device_scan(device);
491 return __connman_error_failed(msg, -err);
493 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 static GDBusMethodTable device_methods[] = {
497 { "GetProperties", "", "a{sv}", get_properties },
498 { "SetProperty", "sv", "", set_property,
499 G_DBUS_METHOD_FLAG_ASYNC },
500 { "ProposeScan", "", "", propose_scan },
504 static GDBusSignalTable device_signals[] = {
505 { "PropertyChanged", "sv" },
509 static int register_interface(struct connman_element *element)
511 struct connman_device *device = element->device;
513 DBG("element %p name %s", element, element->name);
515 if (g_dbus_register_interface(connection, element->path,
516 CONNMAN_DEVICE_INTERFACE,
517 device_methods, device_signals,
518 NULL, device, NULL) == FALSE) {
519 connman_error("Failed to register %s device", element->path);
523 device->registered = TRUE;
528 static void unregister_interface(struct connman_element *element)
530 struct connman_device *device = element->device;
532 DBG("element %p name %s", element, element->name);
534 device->registered = FALSE;
536 g_dbus_unregister_interface(connection, element->path,
537 CONNMAN_DEVICE_INTERFACE);
540 static int setup_device(struct connman_device *device)
544 DBG("device %p", device);
546 err = register_interface(&device->element);
548 if (device->driver->remove)
549 device->driver->remove(device);
550 device->driver = NULL;
554 __connman_technology_add_device(device);
556 switch (device->mode) {
557 case CONNMAN_DEVICE_MODE_UNKNOWN:
558 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
559 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
563 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
566 if (device->offlinemode == FALSE &&
567 device->powered_persistent == TRUE)
568 __connman_device_enable(device);
573 static void probe_driver(struct connman_element *element, gpointer user_data)
575 struct connman_device_driver *driver = user_data;
577 DBG("element %p name %s", element, element->name);
579 if (element->device == NULL)
582 if (element->device->driver != NULL)
585 if (driver->type != element->device->type)
588 if (driver->probe(element->device) < 0)
591 element->device->driver = driver;
593 setup_device(element->device);
596 static void remove_device(struct connman_device *device)
598 DBG("device %p", device);
600 __connman_device_disable(device);
602 switch (device->mode) {
603 case CONNMAN_DEVICE_MODE_UNKNOWN:
604 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
605 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
609 __connman_technology_remove_device(device);
611 unregister_interface(&device->element);
613 if (device->driver->remove)
614 device->driver->remove(device);
616 device->driver = NULL;
619 static void remove_driver(struct connman_element *element, gpointer user_data)
621 struct connman_device_driver *driver = user_data;
623 DBG("element %p name %s", element, element->name);
625 if (element->device == NULL)
628 if (element->device->driver == driver)
629 remove_device(element->device);
632 connman_bool_t __connman_device_has_driver(struct connman_device *device)
634 if (device == NULL || device->driver == NULL)
637 return device->registered;
640 static GSList *driver_list = NULL;
642 static gint compare_priority(gconstpointer a, gconstpointer b)
644 const struct connman_device_driver *driver1 = a;
645 const struct connman_device_driver *driver2 = b;
647 return driver2->priority - driver1->priority;
651 * connman_device_driver_register:
652 * @driver: device driver definition
654 * Register a new device driver
656 * Returns: %0 on success
658 int connman_device_driver_register(struct connman_device_driver *driver)
660 DBG("driver %p name %s", driver, driver->name);
662 driver_list = g_slist_insert_sorted(driver_list, driver,
665 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
666 probe_driver, driver);
672 * connman_device_driver_unregister:
673 * @driver: device driver definition
675 * Remove a previously registered device driver
677 void connman_device_driver_unregister(struct connman_device_driver *driver)
679 DBG("driver %p name %s", driver, driver->name);
681 driver_list = g_slist_remove(driver_list, driver);
683 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
684 remove_driver, driver);
687 static void unregister_network(gpointer data)
689 struct connman_network *network = data;
691 DBG("network %p", network);
693 connman_element_unregister((struct connman_element *) network);
695 connman_network_unref(network);
698 static void device_destruct(struct connman_element *element)
700 struct connman_device *device = element->device;
702 DBG("element %p name %s", element, element->name);
704 if (device->timeout > 0) {
705 g_source_remove(device->timeout);
709 clear_scan_trigger(device);
711 if (device->pending != NULL) {
712 dbus_message_unref(device->pending);
713 device->pending = NULL;
716 g_free(device->ident);
717 g_free(device->node);
718 g_free(device->name);
719 g_free(device->address);
720 g_free(device->control);
721 g_free(device->interface);
723 g_free(device->last_network);
725 g_hash_table_destroy(device->networks);
726 device->networks = NULL;
730 * connman_device_create:
731 * @node: device node name (for example an address)
734 * Allocate a new device of given #type and assign the #node name to it.
736 * Returns: a newly-allocated #connman_device structure
738 struct connman_device *connman_device_create(const char *node,
739 enum connman_device_type type)
741 struct connman_device *device;
744 DBG("node %s type %d", node, type);
746 device = g_try_new0(struct connman_device, 1);
750 DBG("device %p", device);
752 __connman_element_initialize(&device->element);
754 device->element.name = g_strdup(node);
755 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
757 device->element.device = device;
758 device->element.destruct = device_destruct;
760 str = type2string(type);
762 connman_element_set_string(&device->element,
763 CONNMAN_PROPERTY_ID_TYPE, str);
765 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
768 device->name = g_strdup(type2description(device->type));
769 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
771 device->powered_persistent = TRUE;
773 device->phyindex = -1;
776 case CONNMAN_DEVICE_TYPE_UNKNOWN:
777 case CONNMAN_DEVICE_TYPE_VENDOR:
778 device->scan_interval = 0;
780 case CONNMAN_DEVICE_TYPE_ETHERNET:
781 case CONNMAN_DEVICE_TYPE_WIFI:
782 device->scan_interval = 300;
784 case CONNMAN_DEVICE_TYPE_WIMAX:
785 device->scan_interval = 0;
787 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
788 device->scan_interval = 0;
790 case CONNMAN_DEVICE_TYPE_GPS:
791 device->scan_interval = 0;
793 case CONNMAN_DEVICE_TYPE_CELLULAR:
794 device->scan_interval = 0;
798 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
799 g_free, unregister_network);
805 * connman_device_ref:
806 * @device: device structure
808 * Increase reference counter of device
810 struct connman_device *connman_device_ref(struct connman_device *device)
812 if (connman_element_ref(&device->element) == NULL)
819 * connman_device_unref:
820 * @device: device structure
822 * Decrease reference counter of device
824 void connman_device_unref(struct connman_device *device)
826 connman_element_unref(&device->element);
829 const char *__connman_device_get_type(struct connman_device *device)
831 return type2string(device->type);
835 * connman_device_get_type:
836 * @device: device structure
840 enum connman_device_type connman_device_get_type(struct connman_device *device)
846 * connman_device_get_name:
847 * @device: device structure
849 * Get unique name of device
851 const char *connman_device_get_name(struct connman_device *device)
853 return device->element.name;
857 * connman_device_get_path:
858 * @device: device structure
860 * Get path name of device
862 const char *connman_device_get_path(struct connman_device *device)
864 return device->element.path;
868 * connman_device_set_index:
869 * @device: device structure
870 * @index: index number
872 * Set index number of device
874 void connman_device_set_index(struct connman_device *device, int index)
876 device->element.index = index;
880 * connman_device_get_index:
881 * @device: device structure
883 * Get index number of device
885 int connman_device_get_index(struct connman_device *device)
887 return device->element.index;
890 int __connman_device_get_phyindex(struct connman_device *device)
892 return device->phyindex;
895 void __connman_device_set_phyindex(struct connman_device *device,
898 device->phyindex = phyindex;
902 * connman_device_set_interface:
903 * @device: device structure
904 * @interface: interface name
905 * @control: control interface
907 * Set interface name of device
909 void connman_device_set_interface(struct connman_device *device,
910 const char *interface, const char *control)
912 g_free(device->element.devname);
913 device->element.devname = g_strdup(interface);
915 g_free(device->interface);
916 device->interface = g_strdup(interface);
918 g_free(device->control);
919 device->control = g_strdup(control);
921 if (device->name == NULL) {
922 const char *str = type2description(device->type);
923 if (str != NULL && device->interface != NULL)
924 device->name = g_strdup_printf("%s (%s)", str,
929 const char *connman_device_get_control(struct connman_device *device)
931 return device->control;
935 * connman_device_set_ident:
936 * @device: device structure
937 * @ident: unique identifier
939 * Set unique identifier of device
941 void connman_device_set_ident(struct connman_device *device,
944 g_free(device->ident);
945 device->ident = g_strdup(ident);
948 const char *__connman_device_get_ident(struct connman_device *device)
950 return device->ident;
954 * connman_device_set_mode:
955 * @device: device structure
956 * @mode: network mode
958 * Change network mode of device
960 void connman_device_set_mode(struct connman_device *device,
961 enum connman_device_mode mode)
967 * connman_device_get_mode:
968 * @device: device structure
970 * Get network mode of device
972 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
978 * connman_device_set_powered:
979 * @device: device structure
980 * @powered: powered state
982 * Change power state of device
984 int connman_device_set_powered(struct connman_device *device,
985 connman_bool_t powered)
987 DBG("driver %p powered %d", device, powered);
989 if (device->timeout > 0) {
990 g_source_remove(device->timeout);
994 if (device->pending != NULL) {
995 g_dbus_send_reply(connection, device->pending,
998 dbus_message_unref(device->pending);
999 device->pending = NULL;
1002 if (device->powered == powered) {
1003 device->powered_pending = powered;
1007 if (powered == TRUE)
1008 __connman_device_enable(device);
1010 __connman_device_disable(device);
1012 device->powered = powered;
1013 device->powered_pending = powered;
1015 if (device->powered == TRUE)
1016 __connman_technology_enable_device(device);
1018 __connman_technology_disable_device(device);
1020 if (device->offlinemode == TRUE && powered == TRUE) {
1021 powered_changed(device);
1022 return connman_device_set_powered(device, FALSE);
1025 if (device->registered == FALSE)
1028 powered_changed(device);
1030 if (powered == FALSE)
1033 reset_scan_trigger(device);
1035 if (device->driver->scan)
1036 device->driver->scan(device);
1041 int __connman_device_set_blocked(struct connman_device *device,
1042 connman_bool_t blocked)
1044 connman_bool_t powered;
1046 DBG("device %p blocked %d", device, blocked);
1048 device->blocked = blocked;
1050 blocked_changed(device);
1052 if (device->offlinemode == TRUE)
1055 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1057 if (blocked == FALSE)
1058 powered = device->powered_persistent;
1062 return set_powered(device, powered);
1065 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1067 return device->blocked;
1070 int __connman_device_scan(struct connman_device *device)
1072 if (!device->driver || !device->driver->scan)
1075 if (device->powered == FALSE)
1078 reset_scan_trigger(device);
1080 return device->driver->scan(device);
1083 int __connman_device_enable_persistent(struct connman_device *device)
1087 DBG("device %p", device);
1089 device->powered_persistent = TRUE;
1091 __connman_storage_save_device(device);
1093 err = __connman_device_enable(device);
1094 if (err == 0 || err == -EINPROGRESS) {
1095 device->offlinemode = FALSE;
1096 if (__connman_profile_get_offlinemode() == TRUE)
1097 __connman_profile_set_offlinemode(FALSE, FALSE);
1103 int __connman_device_disable_persistent(struct connman_device *device)
1105 DBG("device %p", device);
1107 device->powered_persistent = FALSE;
1109 __connman_storage_save_device(device);
1111 return __connman_device_disable(device);
1114 int __connman_device_disconnect(struct connman_device *device)
1116 GHashTableIter iter;
1117 gpointer key, value;
1119 DBG("device %p", device);
1121 connman_device_set_disconnected(device, TRUE);
1123 g_hash_table_iter_init(&iter, device->networks);
1125 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1126 struct connman_network *network = value;
1128 if (__connman_network_get_connecting(network) == TRUE) {
1130 * Skip network in the process of connecting.
1131 * This is a workaround for WiFi networks serviced
1132 * by the supplicant plugin that hold a reference
1133 * to the network. If we disconnect the network
1134 * here then the referenced object will not be
1135 * registered and usage (like launching DHCP client)
1136 * will fail. There is nothing to be gained by
1137 * removing the network here anyway.
1139 connman_warn("Skipping disconnect of %s",
1140 connman_network_get_identifier(network));
1144 __connman_network_disconnect(network);
1150 static void mark_network_unavailable(gpointer key, gpointer value,
1153 struct connman_network *network = value;
1155 if (connman_network_get_connected(network) == TRUE)
1158 connman_network_set_available(network, FALSE);
1161 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1164 struct connman_network *network = value;
1166 if (connman_network_get_connected(network) == TRUE)
1169 if (connman_network_get_available(network) == TRUE)
1175 void __connman_device_cleanup_networks(struct connman_device *device)
1177 g_hash_table_foreach_remove(device->networks,
1178 remove_unavailable_network, NULL);
1181 static void scanning_changed(struct connman_device *device)
1183 connman_dbus_property_changed_basic(device->element.path,
1184 CONNMAN_DEVICE_INTERFACE, "Scanning",
1185 DBUS_TYPE_BOOLEAN, &device->scanning);
1189 * connman_device_set_scanning:
1190 * @device: device structure
1191 * @scanning: scanning state
1193 * Change scanning state of device
1195 int connman_device_set_scanning(struct connman_device *device,
1196 connman_bool_t scanning)
1198 DBG("device %p scanning %d", device, scanning);
1200 if (!device->driver || !device->driver->scan)
1203 if (device->scanning == scanning)
1206 device->scanning = scanning;
1208 scanning_changed(device);
1210 if (scanning == TRUE) {
1211 reset_scan_trigger(device);
1213 g_hash_table_foreach(device->networks,
1214 mark_network_unavailable, NULL);
1219 __connman_device_cleanup_networks(device);
1221 if (device->connections > 0)
1224 if (device->disconnected == TRUE)
1227 __connman_service_auto_connect();
1233 * connman_device_set_disconnected:
1234 * @device: device structure
1235 * @disconnected: disconnected state
1237 * Change disconnected state of device (only for device with networks)
1239 int connman_device_set_disconnected(struct connman_device *device,
1240 connman_bool_t disconnected)
1242 DBG("device %p disconnected %d", device, disconnected);
1244 switch (device->mode) {
1245 case CONNMAN_DEVICE_MODE_UNKNOWN:
1247 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1248 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1252 if (device->disconnected == disconnected)
1255 device->disconnected = disconnected;
1257 if (disconnected == TRUE)
1258 force_scan_trigger(device);
1264 * connman_device_get_disconnected:
1265 * @device: device structure
1267 * Get device disconnected state
1269 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1271 return device->disconnected;
1275 * connman_device_set_string:
1276 * @device: device structure
1277 * @key: unique identifier
1278 * @value: string value
1280 * Set string value for specific key
1282 int connman_device_set_string(struct connman_device *device,
1283 const char *key, const char *value)
1285 DBG("device %p key %s value %s", device, key, value);
1287 if (g_str_equal(key, "Address") == TRUE) {
1288 g_free(device->address);
1289 device->address = g_strdup(value);
1290 } else if (g_str_equal(key, "Name") == TRUE) {
1291 g_free(device->name);
1292 device->name = g_strdup(value);
1293 } else if (g_str_equal(key, "Node") == TRUE) {
1294 g_free(device->node);
1295 device->node = g_strdup(value);
1298 return connman_element_set_string(&device->element, key, value);
1302 * connman_device_get_string:
1303 * @device: device structure
1304 * @key: unique identifier
1306 * Get string value for specific key
1308 const char *connman_device_get_string(struct connman_device *device,
1311 DBG("device %p key %s", device, key);
1313 if (g_str_equal(key, "Address") == TRUE)
1314 return device->address;
1315 else if (g_str_equal(key, "Name") == TRUE)
1316 return device->name;
1317 else if (g_str_equal(key, "Node") == TRUE)
1318 return device->node;
1320 return connman_element_get_string(&device->element, key);
1323 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1325 struct connman_device *device = element->device;
1326 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1327 connman_bool_t powered;
1329 DBG("element %p name %s", element, element->name);
1334 device->offlinemode = offlinemode;
1336 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1338 if (device->powered == powered)
1341 if (device->powered_persistent == FALSE)
1344 set_powered(device, powered);
1347 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1349 DBG("offlinmode %d", offlinemode);
1351 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1352 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1354 __connman_notifier_offlinemode(offlinemode);
1359 void __connman_device_increase_connections(struct connman_device *device)
1361 device->connections++;
1364 void __connman_device_decrease_connections(struct connman_device *device)
1366 device->connections--;
1370 * connman_device_add_network:
1371 * @device: device structure
1372 * @network: network structure
1374 * Add new network to the device
1376 int connman_device_add_network(struct connman_device *device,
1377 struct connman_network *network)
1379 const char *identifier = connman_network_get_identifier(network);
1382 DBG("device %p network %p", device, network);
1384 if (identifier == NULL)
1387 switch (device->mode) {
1388 case CONNMAN_DEVICE_MODE_UNKNOWN:
1390 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1391 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1395 __connman_network_set_device(network, device);
1397 err = connman_element_register((struct connman_element *) network,
1400 __connman_network_set_device(network, NULL);
1404 g_hash_table_insert(device->networks, g_strdup(identifier),
1411 * connman_device_get_network:
1412 * @device: device structure
1413 * @identifier: network identifier
1415 * Get network for given identifier
1417 struct connman_network *connman_device_get_network(struct connman_device *device,
1418 const char *identifier)
1420 DBG("device %p identifier %s", device, identifier);
1422 return g_hash_table_lookup(device->networks, identifier);
1426 * connman_device_remove_network:
1427 * @device: device structure
1428 * @identifier: network identifier
1430 * Remove network for given identifier
1432 int connman_device_remove_network(struct connman_device *device,
1433 const char *identifier)
1435 DBG("device %p identifier %s", device, identifier);
1437 g_hash_table_remove(device->networks, identifier);
1442 void connman_device_remove_all_networks(struct connman_device *device)
1444 g_hash_table_remove_all(device->networks);
1447 void __connman_device_set_network(struct connman_device *device,
1448 struct connman_network *network)
1455 if (device->network == network)
1458 if (device->network != NULL)
1459 connman_network_unref(device->network);
1461 if (network != NULL) {
1462 name = connman_network_get_string(network,
1463 CONNMAN_PROPERTY_ID_NAME);
1464 g_free(device->last_network);
1465 device->last_network = g_strdup(name);
1467 device->network = connman_network_ref(network);
1469 g_free(device->last_network);
1470 device->last_network = NULL;
1472 device->network = NULL;
1476 void __connman_device_set_reconnect(struct connman_device *device,
1477 connman_bool_t reconnect)
1479 device->reconnect = reconnect;
1482 connman_bool_t __connman_device_get_reconnect(
1483 struct connman_device *device)
1485 return device->reconnect;
1489 * connman_device_register:
1490 * @device: device structure
1492 * Register device with the system
1494 int connman_device_register(struct connman_device *device)
1496 __connman_storage_load_device(device);
1498 device->offlinemode = __connman_profile_get_offlinemode();
1500 return connman_element_register(&device->element, NULL);
1504 * connman_device_unregister:
1505 * @device: device structure
1507 * Unregister device with the system
1509 void connman_device_unregister(struct connman_device *device)
1511 __connman_storage_save_device(device);
1513 connman_element_unregister(&device->element);
1517 * connman_device_get_data:
1518 * @device: device structure
1520 * Get private device data pointer
1522 void *connman_device_get_data(struct connman_device *device)
1524 return device->driver_data;
1528 * connman_device_set_data:
1529 * @device: device structure
1530 * @data: data pointer
1532 * Set private device data pointer
1534 void connman_device_set_data(struct connman_device *device, void *data)
1536 device->driver_data = data;
1539 static gboolean match_driver(struct connman_device *device,
1540 struct connman_device_driver *driver)
1542 if (device->type == driver->type ||
1543 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1549 static int device_probe(struct connman_element *element)
1551 struct connman_device *device = element->device;
1554 DBG("element %p name %s", element, element->name);
1559 if (device->driver != NULL)
1562 for (list = driver_list; list; list = list->next) {
1563 struct connman_device_driver *driver = list->data;
1565 if (match_driver(device, driver) == FALSE)
1568 DBG("driver %p name %s", driver, driver->name);
1570 if (driver->probe(device) == 0) {
1571 device->driver = driver;
1576 if (device->driver == NULL)
1579 return setup_device(device);
1582 static void device_remove(struct connman_element *element)
1584 struct connman_device *device = element->device;
1586 DBG("element %p name %s", element, element->name);
1591 if (device->driver == NULL)
1594 remove_device(device);
1597 static struct connman_driver device_driver = {
1599 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1600 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1601 .probe = device_probe,
1602 .remove = device_remove,
1605 static int device_load(struct connman_device *device)
1607 const char *ident = __connman_profile_active_ident();
1609 GError *error = NULL;
1611 connman_bool_t powered;
1614 DBG("device %p", device);
1616 keyfile = __connman_storage_open_profile(ident);
1617 if (keyfile == NULL)
1620 identifier = g_strdup_printf("device_%s", device->element.name);
1621 if (identifier == NULL)
1624 powered = g_key_file_get_boolean(keyfile, identifier,
1627 device->powered_persistent = powered;
1628 g_clear_error(&error);
1630 switch (device->mode) {
1631 case CONNMAN_DEVICE_MODE_UNKNOWN:
1633 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1634 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1635 val = g_key_file_get_integer(keyfile, identifier,
1636 "ScanInterval", &error);
1637 if (error == NULL && val > 0)
1638 device->scan_interval = val;
1639 g_clear_error(&error);
1646 __connman_storage_close_profile(ident, keyfile, FALSE);
1651 static int device_save(struct connman_device *device)
1653 const char *ident = __connman_profile_active_ident();
1657 DBG("device %p", device);
1659 keyfile = __connman_storage_open_profile(ident);
1660 if (keyfile == NULL)
1663 identifier = g_strdup_printf("device_%s", device->element.name);
1664 if (identifier == NULL)
1667 g_key_file_set_boolean(keyfile, identifier,
1668 "Powered", device->powered_persistent);
1670 switch (device->mode) {
1671 case CONNMAN_DEVICE_MODE_UNKNOWN:
1673 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1674 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1675 if (device->scan_interval > 0)
1676 g_key_file_set_integer(keyfile, identifier,
1677 "ScanInterval", device->scan_interval);
1684 __connman_storage_close_profile(ident, keyfile, TRUE);
1689 static struct connman_storage device_storage = {
1691 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1692 .device_load = device_load,
1693 .device_save = device_save,
1696 int __connman_device_init(void)
1700 connection = connman_dbus_get_connection();
1702 if (connman_storage_register(&device_storage) < 0)
1703 connman_error("Failed to register device storage");
1705 return connman_driver_register(&device_driver);
1708 void __connman_device_cleanup(void)
1712 connman_driver_unregister(&device_driver);
1714 connman_storage_unregister(&device_storage);
1716 dbus_connection_unref(connection);