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 int __connman_device_scan(struct connman_device *device)
1067 if (!device->driver || !device->driver->scan)
1070 if (device->powered == FALSE)
1073 reset_scan_trigger(device);
1075 return device->driver->scan(device);
1078 int __connman_device_enable_persistent(struct connman_device *device)
1082 DBG("device %p", device);
1084 device->powered_persistent = TRUE;
1086 __connman_storage_save_device(device);
1088 err = __connman_device_enable(device);
1089 if (err == 0 || err == -EINPROGRESS) {
1090 if (__connman_profile_get_offlinemode() == TRUE) {
1091 device->offlinemode = FALSE;
1092 __connman_profile_set_offlinemode(FALSE, FALSE);
1100 int __connman_device_disable_persistent(struct connman_device *device)
1102 DBG("device %p", device);
1104 device->powered_persistent = FALSE;
1106 __connman_storage_save_device(device);
1108 return __connman_device_disable(device);
1111 int __connman_device_disconnect(struct connman_device *device)
1113 GHashTableIter iter;
1114 gpointer key, value;
1116 DBG("device %p", device);
1118 connman_device_set_disconnected(device, TRUE);
1120 g_hash_table_iter_init(&iter, device->networks);
1122 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1123 struct connman_network *network = value;
1125 if (__connman_network_get_connecting(network) == TRUE) {
1127 * Skip network in the process of connecting.
1128 * This is a workaround for WiFi networks serviced
1129 * by the supplicant plugin that hold a reference
1130 * to the network. If we disconnect the network
1131 * here then the referenced object will not be
1132 * registered and usage (like launching DHCP client)
1133 * will fail. There is nothing to be gained by
1134 * removing the network here anyway.
1136 connman_warn("Skipping disconnect of %s",
1137 connman_network_get_identifier(network));
1141 __connman_network_disconnect(network);
1147 static void mark_network_unavailable(gpointer key, gpointer value,
1150 struct connman_network *network = value;
1152 if (connman_network_get_connected(network) == TRUE)
1155 connman_network_set_available(network, FALSE);
1158 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1161 struct connman_network *network = value;
1163 if (connman_network_get_connected(network) == TRUE)
1166 if (connman_network_get_available(network) == TRUE)
1172 void __connman_device_cleanup_networks(struct connman_device *device)
1174 g_hash_table_foreach_remove(device->networks,
1175 remove_unavailable_network, NULL);
1178 static void scanning_changed(struct connman_device *device)
1180 connman_dbus_property_changed_basic(device->element.path,
1181 CONNMAN_DEVICE_INTERFACE, "Scanning",
1182 DBUS_TYPE_BOOLEAN, &device->scanning);
1186 * connman_device_set_scanning:
1187 * @device: device structure
1188 * @scanning: scanning state
1190 * Change scanning state of device
1192 int connman_device_set_scanning(struct connman_device *device,
1193 connman_bool_t scanning)
1195 DBG("device %p scanning %d", device, scanning);
1197 if (!device->driver || !device->driver->scan)
1200 if (device->scanning == scanning)
1203 device->scanning = scanning;
1205 scanning_changed(device);
1207 if (scanning == TRUE) {
1208 reset_scan_trigger(device);
1210 g_hash_table_foreach(device->networks,
1211 mark_network_unavailable, NULL);
1216 __connman_device_cleanup_networks(device);
1218 if (device->connections > 0)
1221 if (device->disconnected == TRUE)
1224 __connman_service_auto_connect();
1230 * connman_device_set_disconnected:
1231 * @device: device structure
1232 * @disconnected: disconnected state
1234 * Change disconnected state of device (only for device with networks)
1236 int connman_device_set_disconnected(struct connman_device *device,
1237 connman_bool_t disconnected)
1239 DBG("device %p disconnected %d", device, disconnected);
1241 switch (device->mode) {
1242 case CONNMAN_DEVICE_MODE_UNKNOWN:
1244 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1245 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1249 if (device->disconnected == disconnected)
1252 device->disconnected = disconnected;
1254 if (disconnected == TRUE)
1255 force_scan_trigger(device);
1261 * connman_device_get_disconnected:
1262 * @device: device structure
1264 * Get device disconnected state
1266 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1268 return device->disconnected;
1272 * connman_device_set_string:
1273 * @device: device structure
1274 * @key: unique identifier
1275 * @value: string value
1277 * Set string value for specific key
1279 int connman_device_set_string(struct connman_device *device,
1280 const char *key, const char *value)
1282 DBG("device %p key %s value %s", device, key, value);
1284 if (g_str_equal(key, "Address") == TRUE) {
1285 g_free(device->address);
1286 device->address = g_strdup(value);
1287 } else if (g_str_equal(key, "Name") == TRUE) {
1288 g_free(device->name);
1289 device->name = g_strdup(value);
1290 } else if (g_str_equal(key, "Node") == TRUE) {
1291 g_free(device->node);
1292 device->node = g_strdup(value);
1295 return connman_element_set_string(&device->element, key, value);
1299 * connman_device_get_string:
1300 * @device: device structure
1301 * @key: unique identifier
1303 * Get string value for specific key
1305 const char *connman_device_get_string(struct connman_device *device,
1308 DBG("device %p key %s", device, key);
1310 if (g_str_equal(key, "Address") == TRUE)
1311 return device->address;
1312 else if (g_str_equal(key, "Name") == TRUE)
1313 return device->name;
1314 else if (g_str_equal(key, "Node") == TRUE)
1315 return device->node;
1317 return connman_element_get_string(&device->element, key);
1320 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1322 struct connman_device *device = element->device;
1323 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1324 connman_bool_t powered;
1326 DBG("element %p name %s", element, element->name);
1331 device->offlinemode = offlinemode;
1333 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1335 if (device->powered == powered)
1338 if (device->powered_persistent == FALSE)
1341 set_powered(device, powered);
1344 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1346 DBG("offlinmode %d", offlinemode);
1348 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1349 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1351 __connman_notifier_offlinemode(offlinemode);
1356 void __connman_device_increase_connections(struct connman_device *device)
1358 device->connections++;
1361 void __connman_device_decrease_connections(struct connman_device *device)
1363 device->connections--;
1367 * connman_device_add_network:
1368 * @device: device structure
1369 * @network: network structure
1371 * Add new network to the device
1373 int connman_device_add_network(struct connman_device *device,
1374 struct connman_network *network)
1376 const char *identifier = connman_network_get_identifier(network);
1379 DBG("device %p network %p", device, network);
1381 if (identifier == NULL)
1384 switch (device->mode) {
1385 case CONNMAN_DEVICE_MODE_UNKNOWN:
1387 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1388 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1392 __connman_network_set_device(network, device);
1394 err = connman_element_register((struct connman_element *) network,
1397 __connman_network_set_device(network, NULL);
1401 g_hash_table_insert(device->networks, g_strdup(identifier),
1408 * connman_device_get_network:
1409 * @device: device structure
1410 * @identifier: network identifier
1412 * Get network for given identifier
1414 struct connman_network *connman_device_get_network(struct connman_device *device,
1415 const char *identifier)
1417 DBG("device %p identifier %s", device, identifier);
1419 return g_hash_table_lookup(device->networks, identifier);
1423 * connman_device_remove_network:
1424 * @device: device structure
1425 * @identifier: network identifier
1427 * Remove network for given identifier
1429 int connman_device_remove_network(struct connman_device *device,
1430 const char *identifier)
1432 DBG("device %p identifier %s", device, identifier);
1434 g_hash_table_remove(device->networks, identifier);
1439 void connman_device_remove_all_networks(struct connman_device *device)
1441 g_hash_table_remove_all(device->networks);
1444 void __connman_device_set_network(struct connman_device *device,
1445 struct connman_network *network)
1452 if (device->network == network)
1455 if (device->network != NULL)
1456 connman_network_unref(device->network);
1458 if (network != NULL) {
1459 name = connman_network_get_string(network,
1460 CONNMAN_PROPERTY_ID_NAME);
1461 g_free(device->last_network);
1462 device->last_network = g_strdup(name);
1464 device->network = connman_network_ref(network);
1466 g_free(device->last_network);
1467 device->last_network = NULL;
1469 device->network = NULL;
1473 void __connman_device_set_reconnect(struct connman_device *device,
1474 connman_bool_t reconnect)
1476 device->reconnect = reconnect;
1479 connman_bool_t __connman_device_get_reconnect(
1480 struct connman_device *device)
1482 return device->reconnect;
1486 * connman_device_register:
1487 * @device: device structure
1489 * Register device with the system
1491 int connman_device_register(struct connman_device *device)
1493 __connman_storage_load_device(device);
1495 device->offlinemode = __connman_profile_get_offlinemode();
1497 return connman_element_register(&device->element, NULL);
1501 * connman_device_unregister:
1502 * @device: device structure
1504 * Unregister device with the system
1506 void connman_device_unregister(struct connman_device *device)
1508 __connman_storage_save_device(device);
1510 connman_element_unregister(&device->element);
1514 * connman_device_get_data:
1515 * @device: device structure
1517 * Get private device data pointer
1519 void *connman_device_get_data(struct connman_device *device)
1521 return device->driver_data;
1525 * connman_device_set_data:
1526 * @device: device structure
1527 * @data: data pointer
1529 * Set private device data pointer
1531 void connman_device_set_data(struct connman_device *device, void *data)
1533 device->driver_data = data;
1536 static gboolean match_driver(struct connman_device *device,
1537 struct connman_device_driver *driver)
1539 if (device->type == driver->type ||
1540 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1546 static int device_probe(struct connman_element *element)
1548 struct connman_device *device = element->device;
1551 DBG("element %p name %s", element, element->name);
1556 if (device->driver != NULL)
1559 for (list = driver_list; list; list = list->next) {
1560 struct connman_device_driver *driver = list->data;
1562 if (match_driver(device, driver) == FALSE)
1565 DBG("driver %p name %s", driver, driver->name);
1567 if (driver->probe(device) == 0) {
1568 device->driver = driver;
1573 if (device->driver == NULL)
1576 return setup_device(device);
1579 static void device_remove(struct connman_element *element)
1581 struct connman_device *device = element->device;
1583 DBG("element %p name %s", element, element->name);
1588 if (device->driver == NULL)
1591 remove_device(device);
1594 static struct connman_driver device_driver = {
1596 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1597 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1598 .probe = device_probe,
1599 .remove = device_remove,
1602 static int device_load(struct connman_device *device)
1604 const char *ident = __connman_profile_active_ident();
1606 GError *error = NULL;
1608 connman_bool_t powered;
1611 DBG("device %p", device);
1613 keyfile = __connman_storage_open_profile(ident);
1614 if (keyfile == NULL)
1617 identifier = g_strdup_printf("device_%s", device->element.name);
1618 if (identifier == NULL)
1621 powered = g_key_file_get_boolean(keyfile, identifier,
1624 device->powered_persistent = powered;
1625 g_clear_error(&error);
1627 switch (device->mode) {
1628 case CONNMAN_DEVICE_MODE_UNKNOWN:
1630 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1631 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1632 val = g_key_file_get_integer(keyfile, identifier,
1633 "ScanInterval", &error);
1634 if (error == NULL && val > 0)
1635 device->scan_interval = val;
1636 g_clear_error(&error);
1643 __connman_storage_close_profile(ident, keyfile, FALSE);
1648 static int device_save(struct connman_device *device)
1650 const char *ident = __connman_profile_active_ident();
1654 DBG("device %p", device);
1656 keyfile = __connman_storage_open_profile(ident);
1657 if (keyfile == NULL)
1660 identifier = g_strdup_printf("device_%s", device->element.name);
1661 if (identifier == NULL)
1664 g_key_file_set_boolean(keyfile, identifier,
1665 "Powered", device->powered_persistent);
1667 switch (device->mode) {
1668 case CONNMAN_DEVICE_MODE_UNKNOWN:
1670 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1671 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1672 if (device->scan_interval > 0)
1673 g_key_file_set_integer(keyfile, identifier,
1674 "ScanInterval", device->scan_interval);
1681 __connman_storage_close_profile(ident, keyfile, TRUE);
1686 static struct connman_storage device_storage = {
1688 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1689 .device_load = device_load,
1690 .device_save = device_save,
1693 int __connman_device_init(void)
1697 connection = connman_dbus_get_connection();
1699 if (connman_storage_register(&device_storage) < 0)
1700 connman_error("Failed to register device storage");
1702 return connman_driver_register(&device_driver);
1705 void __connman_device_cleanup(void)
1709 connman_driver_unregister(&device_driver);
1711 connman_storage_unregister(&device_storage);
1713 dbus_connection_unref(connection);