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;
223 device->offlinemode = FALSE;
224 if (__connman_profile_get_offlinemode() == TRUE)
225 __connman_profile_set_offlinemode(FALSE, FALSE);
230 device->powered_pending = TRUE;
231 device->powered = TRUE;
232 device->offlinemode = FALSE;
233 if (__connman_profile_get_offlinemode() == TRUE)
234 __connman_profile_set_offlinemode(FALSE, FALSE);
236 __connman_technology_enable_device(device);
241 int __connman_device_disable(struct connman_device *device)
245 DBG("device %p", device);
247 if (!device->driver || !device->driver->disable)
250 if (device->powered == FALSE)
253 if (device->powered_pending == FALSE)
256 device->reconnect = FALSE;
258 clear_scan_trigger(device);
260 g_hash_table_remove_all(device->networks);
262 err = device->driver->disable(device);
264 if (err == -EINPROGRESS)
265 device->powered_pending = FALSE;
269 device->powered_pending = FALSE;
270 device->powered = FALSE;
272 __connman_technology_disable_device(device);
277 static int set_powered(struct connman_device *device, connman_bool_t powered)
279 DBG("device %p powered %d", device, powered);
282 return __connman_device_enable(device);
284 return __connman_device_disable(device);
287 void __connman_device_list(DBusMessageIter *iter, void *user_data)
289 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
292 static void append_path(gpointer key, gpointer value, gpointer user_data)
294 struct connman_element *element = value;
295 DBusMessageIter *iter = user_data;
297 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
301 static void append_networks(DBusMessageIter *iter, void *user_data)
303 struct connman_device *device = user_data;
305 g_hash_table_foreach(device->networks, append_path, iter);
308 static DBusMessage *get_properties(DBusConnection *conn,
309 DBusMessage *msg, void *data)
311 struct connman_device *device = data;
313 DBusMessageIter array, dict;
316 DBG("conn %p", conn);
318 if (__connman_security_check_privilege(msg,
319 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
320 return __connman_error_permission_denied(msg);
322 reply = dbus_message_new_method_return(msg);
326 dbus_message_iter_init_append(reply, &array);
328 connman_dbus_dict_open(&array, &dict);
330 if (device->name != NULL)
331 connman_dbus_dict_append_basic(&dict, "Name",
332 DBUS_TYPE_STRING, &device->name);
334 str = type2string(device->type);
336 connman_dbus_dict_append_basic(&dict, "Type",
337 DBUS_TYPE_STRING, &str);
339 if (device->address != NULL)
340 connman_dbus_dict_append_basic(&dict, "Address",
341 DBUS_TYPE_STRING, &device->address);
343 if (device->interface != NULL)
344 connman_dbus_dict_append_basic(&dict, "Interface",
345 DBUS_TYPE_STRING, &device->interface);
347 connman_dbus_dict_append_basic(&dict, "Powered",
348 DBUS_TYPE_BOOLEAN, &device->powered);
350 connman_dbus_dict_append_basic(&dict, "Blocked",
351 DBUS_TYPE_BOOLEAN, &device->blocked);
353 if (device->driver && device->driver->scan)
354 connman_dbus_dict_append_basic(&dict, "Scanning",
355 DBUS_TYPE_BOOLEAN, &device->scanning);
357 switch (device->mode) {
358 case CONNMAN_DEVICE_MODE_UNKNOWN:
360 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
361 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
362 if (device->scan_interval > 0)
363 connman_dbus_dict_append_basic(&dict, "ScanInterval",
364 DBUS_TYPE_UINT16, &device->scan_interval);
366 connman_dbus_dict_append_array(&dict, "Networks",
367 DBUS_TYPE_OBJECT_PATH, append_networks, device);
371 connman_dbus_dict_close(&array, &dict);
376 static gboolean powered_timeout(gpointer user_data)
378 struct connman_device *device = user_data;
380 DBG("device %p", device);
384 if (device->pending != NULL) {
387 reply = __connman_error_operation_timeout(device->pending);
389 g_dbus_send_message(connection, reply);
391 dbus_message_unref(device->pending);
392 device->pending = NULL;
398 static DBusMessage *set_property(DBusConnection *conn,
399 DBusMessage *msg, void *data)
401 struct connman_device *device = data;
402 DBusMessageIter iter, value;
406 DBG("conn %p", conn);
408 if (dbus_message_iter_init(msg, &iter) == FALSE)
409 return __connman_error_invalid_arguments(msg);
411 dbus_message_iter_get_basic(&iter, &name);
412 dbus_message_iter_next(&iter);
413 dbus_message_iter_recurse(&iter, &value);
415 if (__connman_security_check_privilege(msg,
416 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
417 return __connman_error_permission_denied(msg);
419 type = dbus_message_iter_get_arg_type(&value);
421 if (g_str_equal(name, "Powered") == TRUE) {
422 connman_bool_t powered;
425 if (type != DBUS_TYPE_BOOLEAN)
426 return __connman_error_invalid_arguments(msg);
428 dbus_message_iter_get_basic(&value, &powered);
430 device->powered_persistent = powered;
432 __connman_storage_save_device(device);
434 if (device->powered == powered)
435 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
437 if (device->pending != NULL)
438 return __connman_error_in_progress(msg);
440 err = set_powered(device, powered);
442 if (err != -EINPROGRESS)
443 return __connman_error_failed(msg, -err);
445 device->pending = dbus_message_ref(msg);
447 device->timeout = g_timeout_add_seconds(15,
448 powered_timeout, device);
452 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
453 connman_uint16_t interval;
455 switch (device->mode) {
456 case CONNMAN_DEVICE_MODE_UNKNOWN:
457 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
458 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
462 if (type != DBUS_TYPE_UINT16)
463 return __connman_error_invalid_arguments(msg);
465 dbus_message_iter_get_basic(&value, &interval);
467 if (device->scan_interval != interval) {
468 device->scan_interval = interval;
470 __connman_storage_save_device(device);
472 reset_scan_trigger(device);
475 return __connman_error_invalid_property(msg);
477 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
480 static DBusMessage *propose_scan(DBusConnection *conn,
481 DBusMessage *msg, void *data)
483 struct connman_device *device = data;
486 DBG("conn %p", conn);
488 switch (device->mode) {
489 case CONNMAN_DEVICE_MODE_UNKNOWN:
490 return __connman_error_not_supported(msg);
491 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
492 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
496 err = __connman_device_scan(device);
498 return __connman_error_failed(msg, -err);
500 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
503 static GDBusMethodTable device_methods[] = {
504 { "GetProperties", "", "a{sv}", get_properties },
505 { "SetProperty", "sv", "", set_property,
506 G_DBUS_METHOD_FLAG_ASYNC },
507 { "ProposeScan", "", "", propose_scan },
511 static GDBusSignalTable device_signals[] = {
512 { "PropertyChanged", "sv" },
516 static int register_interface(struct connman_element *element)
518 struct connman_device *device = element->device;
520 DBG("element %p name %s", element, element->name);
522 if (g_dbus_register_interface(connection, element->path,
523 CONNMAN_DEVICE_INTERFACE,
524 device_methods, device_signals,
525 NULL, device, NULL) == FALSE) {
526 connman_error("Failed to register %s device", element->path);
530 device->registered = TRUE;
535 static void unregister_interface(struct connman_element *element)
537 struct connman_device *device = element->device;
539 DBG("element %p name %s", element, element->name);
541 device->registered = FALSE;
543 g_dbus_unregister_interface(connection, element->path,
544 CONNMAN_DEVICE_INTERFACE);
547 static int setup_device(struct connman_device *device)
551 DBG("device %p", device);
553 err = register_interface(&device->element);
555 if (device->driver->remove)
556 device->driver->remove(device);
557 device->driver = NULL;
561 __connman_technology_add_device(device);
563 switch (device->mode) {
564 case CONNMAN_DEVICE_MODE_UNKNOWN:
565 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
566 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
570 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
573 if (device->offlinemode == FALSE &&
574 device->powered_persistent == TRUE)
575 __connman_device_enable(device);
580 static void probe_driver(struct connman_element *element, gpointer user_data)
582 struct connman_device_driver *driver = user_data;
584 DBG("element %p name %s", element, element->name);
586 if (element->device == NULL)
589 if (element->device->driver != NULL)
592 if (driver->type != element->device->type)
595 if (driver->probe(element->device) < 0)
598 element->device->driver = driver;
600 setup_device(element->device);
603 static void remove_device(struct connman_device *device)
607 DBG("device %p", device);
609 err = __connman_device_disable(device);
610 if (err < 0 && err == -EINPROGRESS)
611 __connman_technology_disable_device(device);
613 switch (device->mode) {
614 case CONNMAN_DEVICE_MODE_UNKNOWN:
615 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
616 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
620 __connman_technology_remove_device(device);
622 unregister_interface(&device->element);
624 if (device->driver->remove)
625 device->driver->remove(device);
627 device->driver = NULL;
630 static void remove_driver(struct connman_element *element, gpointer user_data)
632 struct connman_device_driver *driver = user_data;
634 DBG("element %p name %s", element, element->name);
636 if (element->device == NULL)
639 if (element->device->driver == driver)
640 remove_device(element->device);
643 connman_bool_t __connman_device_has_driver(struct connman_device *device)
645 if (device == NULL || device->driver == NULL)
648 return device->registered;
651 static GSList *driver_list = NULL;
653 static gint compare_priority(gconstpointer a, gconstpointer b)
655 const struct connman_device_driver *driver1 = a;
656 const struct connman_device_driver *driver2 = b;
658 return driver2->priority - driver1->priority;
662 * connman_device_driver_register:
663 * @driver: device driver definition
665 * Register a new device driver
667 * Returns: %0 on success
669 int connman_device_driver_register(struct connman_device_driver *driver)
671 DBG("driver %p name %s", driver, driver->name);
673 driver_list = g_slist_insert_sorted(driver_list, driver,
676 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
677 probe_driver, driver);
683 * connman_device_driver_unregister:
684 * @driver: device driver definition
686 * Remove a previously registered device driver
688 void connman_device_driver_unregister(struct connman_device_driver *driver)
690 DBG("driver %p name %s", driver, driver->name);
692 driver_list = g_slist_remove(driver_list, driver);
694 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
695 remove_driver, driver);
698 static void unregister_network(gpointer data)
700 struct connman_network *network = data;
702 DBG("network %p", network);
704 connman_element_unregister((struct connman_element *) network);
706 __connman_network_set_device(network, NULL);
708 connman_network_unref(network);
711 static void device_destruct(struct connman_element *element)
713 struct connman_device *device = element->device;
715 DBG("element %p name %s", element, element->name);
717 if (device->timeout > 0) {
718 g_source_remove(device->timeout);
722 clear_scan_trigger(device);
724 if (device->pending != NULL) {
725 dbus_message_unref(device->pending);
726 device->pending = NULL;
729 g_free(device->ident);
730 g_free(device->node);
731 g_free(device->name);
732 g_free(device->address);
733 g_free(device->control);
734 g_free(device->interface);
736 g_free(device->last_network);
738 g_hash_table_destroy(device->networks);
739 device->networks = NULL;
743 * connman_device_create:
744 * @node: device node name (for example an address)
747 * Allocate a new device of given #type and assign the #node name to it.
749 * Returns: a newly-allocated #connman_device structure
751 struct connman_device *connman_device_create(const char *node,
752 enum connman_device_type type)
754 struct connman_device *device;
757 DBG("node %s type %d", node, type);
759 device = g_try_new0(struct connman_device, 1);
763 DBG("device %p", device);
765 __connman_element_initialize(&device->element);
767 device->element.name = g_strdup(node);
768 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
770 device->element.device = device;
771 device->element.destruct = device_destruct;
773 str = type2string(type);
775 connman_element_set_string(&device->element,
776 CONNMAN_PROPERTY_ID_TYPE, str);
778 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
781 device->name = g_strdup(type2description(device->type));
782 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
784 device->powered_persistent = TRUE;
786 device->phyindex = -1;
789 case CONNMAN_DEVICE_TYPE_UNKNOWN:
790 case CONNMAN_DEVICE_TYPE_VENDOR:
791 device->scan_interval = 0;
793 case CONNMAN_DEVICE_TYPE_ETHERNET:
794 case CONNMAN_DEVICE_TYPE_WIFI:
795 device->scan_interval = 300;
797 case CONNMAN_DEVICE_TYPE_WIMAX:
798 device->scan_interval = 0;
800 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
801 device->scan_interval = 0;
803 case CONNMAN_DEVICE_TYPE_GPS:
804 device->scan_interval = 0;
806 case CONNMAN_DEVICE_TYPE_CELLULAR:
807 device->scan_interval = 0;
811 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
812 g_free, unregister_network);
818 * connman_device_ref:
819 * @device: device structure
821 * Increase reference counter of device
823 struct connman_device *connman_device_ref(struct connman_device *device)
825 if (connman_element_ref(&device->element) == NULL)
832 * connman_device_unref:
833 * @device: device structure
835 * Decrease reference counter of device
837 void connman_device_unref(struct connman_device *device)
839 connman_element_unref(&device->element);
842 const char *__connman_device_get_type(struct connman_device *device)
844 return type2string(device->type);
848 * connman_device_get_type:
849 * @device: device structure
853 enum connman_device_type connman_device_get_type(struct connman_device *device)
859 * connman_device_get_name:
860 * @device: device structure
862 * Get unique name of device
864 const char *connman_device_get_name(struct connman_device *device)
866 return device->element.name;
870 * connman_device_get_path:
871 * @device: device structure
873 * Get path name of device
875 const char *connman_device_get_path(struct connman_device *device)
877 return device->element.path;
881 * connman_device_set_index:
882 * @device: device structure
883 * @index: index number
885 * Set index number of device
887 void connman_device_set_index(struct connman_device *device, int index)
889 device->element.index = index;
893 * connman_device_get_index:
894 * @device: device structure
896 * Get index number of device
898 int connman_device_get_index(struct connman_device *device)
900 return device->element.index;
903 int __connman_device_get_phyindex(struct connman_device *device)
905 return device->phyindex;
908 void __connman_device_set_phyindex(struct connman_device *device,
911 device->phyindex = phyindex;
915 * connman_device_set_interface:
916 * @device: device structure
917 * @interface: interface name
918 * @control: control interface
920 * Set interface name of device
922 void connman_device_set_interface(struct connman_device *device,
923 const char *interface, const char *control)
925 g_free(device->element.devname);
926 device->element.devname = g_strdup(interface);
928 g_free(device->interface);
929 device->interface = g_strdup(interface);
931 g_free(device->control);
932 device->control = g_strdup(control);
934 if (device->name == NULL) {
935 const char *str = type2description(device->type);
936 if (str != NULL && device->interface != NULL)
937 device->name = g_strdup_printf("%s (%s)", str,
942 const char *connman_device_get_control(struct connman_device *device)
944 return device->control;
948 * connman_device_set_ident:
949 * @device: device structure
950 * @ident: unique identifier
952 * Set unique identifier of device
954 void connman_device_set_ident(struct connman_device *device,
957 g_free(device->ident);
958 device->ident = g_strdup(ident);
961 const char *__connman_device_get_ident(struct connman_device *device)
963 return device->ident;
967 * connman_device_set_mode:
968 * @device: device structure
969 * @mode: network mode
971 * Change network mode of device
973 void connman_device_set_mode(struct connman_device *device,
974 enum connman_device_mode mode)
980 * connman_device_get_mode:
981 * @device: device structure
983 * Get network mode of device
985 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
991 * connman_device_set_powered:
992 * @device: device structure
993 * @powered: powered state
995 * Change power state of device
997 int connman_device_set_powered(struct connman_device *device,
998 connman_bool_t powered)
1000 DBG("driver %p powered %d", device, powered);
1002 if (device->timeout > 0) {
1003 g_source_remove(device->timeout);
1004 device->timeout = 0;
1007 if (device->pending != NULL) {
1008 g_dbus_send_reply(connection, device->pending,
1011 dbus_message_unref(device->pending);
1012 device->pending = NULL;
1015 if (device->powered == powered) {
1016 device->powered_pending = powered;
1020 if (powered == TRUE)
1021 __connman_device_enable(device);
1023 __connman_device_disable(device);
1025 device->powered = powered;
1026 device->powered_pending = powered;
1028 if (device->powered == TRUE)
1029 __connman_technology_enable_device(device);
1031 __connman_technology_disable_device(device);
1033 if (device->offlinemode == TRUE && powered == TRUE) {
1034 powered_changed(device);
1035 return connman_device_set_powered(device, FALSE);
1038 if (device->registered == FALSE)
1041 powered_changed(device);
1043 if (powered == FALSE)
1046 reset_scan_trigger(device);
1048 if (device->driver->scan)
1049 device->driver->scan(device);
1054 int __connman_device_set_blocked(struct connman_device *device,
1055 connman_bool_t blocked)
1057 connman_bool_t powered;
1059 DBG("device %p blocked %d", device, blocked);
1061 device->blocked = blocked;
1063 blocked_changed(device);
1065 if (device->offlinemode == TRUE)
1068 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1070 if (blocked == FALSE)
1071 powered = device->powered_persistent;
1075 return set_powered(device, powered);
1078 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1080 return device->blocked;
1083 int __connman_device_scan(struct connman_device *device)
1085 if (!device->driver || !device->driver->scan)
1088 if (device->powered == FALSE)
1091 reset_scan_trigger(device);
1093 return device->driver->scan(device);
1096 int __connman_device_enable_persistent(struct connman_device *device)
1100 DBG("device %p", device);
1102 device->powered_persistent = TRUE;
1104 __connman_storage_save_device(device);
1106 err = __connman_device_enable(device);
1107 if (err == 0 || err == -EINPROGRESS) {
1108 device->offlinemode = FALSE;
1109 if (__connman_profile_get_offlinemode() == TRUE) {
1110 __connman_profile_set_offlinemode(FALSE, FALSE);
1112 __connman_profile_save_default();
1119 int __connman_device_disable_persistent(struct connman_device *device)
1121 DBG("device %p", device);
1123 device->powered_persistent = FALSE;
1125 __connman_storage_save_device(device);
1127 return __connman_device_disable(device);
1130 int __connman_device_disconnect(struct connman_device *device)
1132 GHashTableIter iter;
1133 gpointer key, value;
1135 DBG("device %p", device);
1137 connman_device_set_disconnected(device, TRUE);
1139 g_hash_table_iter_init(&iter, device->networks);
1141 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1142 struct connman_network *network = value;
1144 if (__connman_network_get_connecting(network) == TRUE) {
1146 * Skip network in the process of connecting.
1147 * This is a workaround for WiFi networks serviced
1148 * by the supplicant plugin that hold a reference
1149 * to the network. If we disconnect the network
1150 * here then the referenced object will not be
1151 * registered and usage (like launching DHCP client)
1152 * will fail. There is nothing to be gained by
1153 * removing the network here anyway.
1155 connman_warn("Skipping disconnect of %s",
1156 connman_network_get_identifier(network));
1160 __connman_network_disconnect(network);
1166 static void mark_network_unavailable(gpointer key, gpointer value,
1169 struct connman_network *network = value;
1171 if (connman_network_get_connected(network) == TRUE)
1174 connman_network_set_available(network, FALSE);
1177 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1180 struct connman_network *network = value;
1182 if (connman_network_get_connected(network) == TRUE)
1185 if (connman_network_get_available(network) == TRUE)
1191 void __connman_device_cleanup_networks(struct connman_device *device)
1193 g_hash_table_foreach_remove(device->networks,
1194 remove_unavailable_network, NULL);
1197 static void scanning_changed(struct connman_device *device)
1199 connman_dbus_property_changed_basic(device->element.path,
1200 CONNMAN_DEVICE_INTERFACE, "Scanning",
1201 DBUS_TYPE_BOOLEAN, &device->scanning);
1205 * connman_device_set_scanning:
1206 * @device: device structure
1207 * @scanning: scanning state
1209 * Change scanning state of device
1211 int connman_device_set_scanning(struct connman_device *device,
1212 connman_bool_t scanning)
1214 DBG("device %p scanning %d", device, scanning);
1216 if (!device->driver || !device->driver->scan)
1219 if (device->scanning == scanning)
1222 device->scanning = scanning;
1224 scanning_changed(device);
1226 if (scanning == TRUE) {
1227 reset_scan_trigger(device);
1229 g_hash_table_foreach(device->networks,
1230 mark_network_unavailable, NULL);
1235 __connman_device_cleanup_networks(device);
1237 if (device->connections > 0)
1240 if (device->disconnected == TRUE)
1243 __connman_service_auto_connect();
1249 * connman_device_set_disconnected:
1250 * @device: device structure
1251 * @disconnected: disconnected state
1253 * Change disconnected state of device (only for device with networks)
1255 int connman_device_set_disconnected(struct connman_device *device,
1256 connman_bool_t disconnected)
1258 DBG("device %p disconnected %d", device, disconnected);
1260 switch (device->mode) {
1261 case CONNMAN_DEVICE_MODE_UNKNOWN:
1263 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1264 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1268 if (device->disconnected == disconnected)
1271 device->disconnected = disconnected;
1273 if (disconnected == TRUE)
1274 force_scan_trigger(device);
1280 * connman_device_get_disconnected:
1281 * @device: device structure
1283 * Get device disconnected state
1285 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1287 return device->disconnected;
1291 * connman_device_set_string:
1292 * @device: device structure
1293 * @key: unique identifier
1294 * @value: string value
1296 * Set string value for specific key
1298 int connman_device_set_string(struct connman_device *device,
1299 const char *key, const char *value)
1301 DBG("device %p key %s value %s", device, key, value);
1303 if (g_str_equal(key, "Address") == TRUE) {
1304 g_free(device->address);
1305 device->address = g_strdup(value);
1306 } else if (g_str_equal(key, "Name") == TRUE) {
1307 g_free(device->name);
1308 device->name = g_strdup(value);
1309 } else if (g_str_equal(key, "Node") == TRUE) {
1310 g_free(device->node);
1311 device->node = g_strdup(value);
1314 return connman_element_set_string(&device->element, key, value);
1318 * connman_device_get_string:
1319 * @device: device structure
1320 * @key: unique identifier
1322 * Get string value for specific key
1324 const char *connman_device_get_string(struct connman_device *device,
1327 DBG("device %p key %s", device, key);
1329 if (g_str_equal(key, "Address") == TRUE)
1330 return device->address;
1331 else if (g_str_equal(key, "Name") == TRUE)
1332 return device->name;
1333 else if (g_str_equal(key, "Node") == TRUE)
1334 return device->node;
1336 return connman_element_get_string(&device->element, key);
1339 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1341 struct connman_device *device = element->device;
1342 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1343 connman_bool_t powered;
1345 DBG("element %p name %s", element, element->name);
1350 device->offlinemode = offlinemode;
1352 if (device->blocked == TRUE)
1355 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1357 if (device->powered == powered)
1360 if (device->powered_persistent == FALSE)
1363 set_powered(device, powered);
1366 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1368 DBG("offlinmode %d", offlinemode);
1370 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1371 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1373 __connman_notifier_offlinemode(offlinemode);
1378 void __connman_device_increase_connections(struct connman_device *device)
1380 device->connections++;
1383 void __connman_device_decrease_connections(struct connman_device *device)
1385 device->connections--;
1389 * connman_device_add_network:
1390 * @device: device structure
1391 * @network: network structure
1393 * Add new network to the device
1395 int connman_device_add_network(struct connman_device *device,
1396 struct connman_network *network)
1398 const char *identifier = connman_network_get_identifier(network);
1401 DBG("device %p network %p", device, network);
1403 if (identifier == NULL)
1406 switch (device->mode) {
1407 case CONNMAN_DEVICE_MODE_UNKNOWN:
1409 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1410 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1414 __connman_network_set_device(network, device);
1416 err = connman_element_register((struct connman_element *) network,
1419 __connman_network_set_device(network, NULL);
1423 g_hash_table_insert(device->networks, g_strdup(identifier),
1430 * connman_device_get_network:
1431 * @device: device structure
1432 * @identifier: network identifier
1434 * Get network for given identifier
1436 struct connman_network *connman_device_get_network(struct connman_device *device,
1437 const char *identifier)
1439 DBG("device %p identifier %s", device, identifier);
1441 return g_hash_table_lookup(device->networks, identifier);
1445 * connman_device_remove_network:
1446 * @device: device structure
1447 * @identifier: network identifier
1449 * Remove network for given identifier
1451 int connman_device_remove_network(struct connman_device *device,
1452 const char *identifier)
1454 DBG("device %p identifier %s", device, identifier);
1456 g_hash_table_remove(device->networks, identifier);
1461 void connman_device_remove_all_networks(struct connman_device *device)
1463 g_hash_table_remove_all(device->networks);
1466 void __connman_device_set_network(struct connman_device *device,
1467 struct connman_network *network)
1474 if (device->network == network)
1477 if (device->network != NULL)
1478 connman_network_unref(device->network);
1480 if (network != NULL) {
1481 name = connman_network_get_string(network,
1482 CONNMAN_PROPERTY_ID_NAME);
1483 g_free(device->last_network);
1484 device->last_network = g_strdup(name);
1486 device->network = connman_network_ref(network);
1488 g_free(device->last_network);
1489 device->last_network = NULL;
1491 device->network = NULL;
1495 void __connman_device_set_reconnect(struct connman_device *device,
1496 connman_bool_t reconnect)
1498 device->reconnect = reconnect;
1501 connman_bool_t __connman_device_get_reconnect(
1502 struct connman_device *device)
1504 return device->reconnect;
1508 * connman_device_register:
1509 * @device: device structure
1511 * Register device with the system
1513 int connman_device_register(struct connman_device *device)
1515 __connman_storage_load_device(device);
1517 device->offlinemode = __connman_profile_get_offlinemode();
1519 return connman_element_register(&device->element, NULL);
1523 * connman_device_unregister:
1524 * @device: device structure
1526 * Unregister device with the system
1528 void connman_device_unregister(struct connman_device *device)
1530 __connman_storage_save_device(device);
1532 connman_element_unregister(&device->element);
1536 * connman_device_get_data:
1537 * @device: device structure
1539 * Get private device data pointer
1541 void *connman_device_get_data(struct connman_device *device)
1543 return device->driver_data;
1547 * connman_device_set_data:
1548 * @device: device structure
1549 * @data: data pointer
1551 * Set private device data pointer
1553 void connman_device_set_data(struct connman_device *device, void *data)
1555 device->driver_data = data;
1558 static gboolean match_driver(struct connman_device *device,
1559 struct connman_device_driver *driver)
1561 if (device->type == driver->type ||
1562 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1568 static int device_probe(struct connman_element *element)
1570 struct connman_device *device = element->device;
1573 DBG("element %p name %s", element, element->name);
1578 if (device->driver != NULL)
1581 for (list = driver_list; list; list = list->next) {
1582 struct connman_device_driver *driver = list->data;
1584 if (match_driver(device, driver) == FALSE)
1587 DBG("driver %p name %s", driver, driver->name);
1589 if (driver->probe(device) == 0) {
1590 device->driver = driver;
1595 if (device->driver == NULL)
1598 return setup_device(device);
1601 static void device_remove(struct connman_element *element)
1603 struct connman_device *device = element->device;
1605 DBG("element %p name %s", element, element->name);
1610 if (device->driver == NULL)
1613 remove_device(device);
1616 static struct connman_driver device_driver = {
1618 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1619 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1620 .probe = device_probe,
1621 .remove = device_remove,
1624 static int device_load(struct connman_device *device)
1626 const char *ident = __connman_profile_active_ident();
1628 GError *error = NULL;
1630 connman_bool_t powered;
1633 DBG("device %p", device);
1635 keyfile = __connman_storage_open_profile(ident);
1636 if (keyfile == NULL)
1639 identifier = g_strdup_printf("device_%s", device->element.name);
1640 if (identifier == NULL)
1643 powered = g_key_file_get_boolean(keyfile, identifier,
1646 device->powered_persistent = powered;
1647 g_clear_error(&error);
1649 switch (device->mode) {
1650 case CONNMAN_DEVICE_MODE_UNKNOWN:
1652 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1653 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1654 val = g_key_file_get_integer(keyfile, identifier,
1655 "ScanInterval", &error);
1656 if (error == NULL && val > 0)
1657 device->scan_interval = val;
1658 g_clear_error(&error);
1665 __connman_storage_close_profile(ident, keyfile, FALSE);
1670 static int device_save(struct connman_device *device)
1672 const char *ident = __connman_profile_active_ident();
1676 DBG("device %p", device);
1678 keyfile = __connman_storage_open_profile(ident);
1679 if (keyfile == NULL)
1682 identifier = g_strdup_printf("device_%s", device->element.name);
1683 if (identifier == NULL)
1686 g_key_file_set_boolean(keyfile, identifier,
1687 "Powered", device->powered_persistent);
1689 switch (device->mode) {
1690 case CONNMAN_DEVICE_MODE_UNKNOWN:
1692 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1693 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1694 if (device->scan_interval > 0)
1695 g_key_file_set_integer(keyfile, identifier,
1696 "ScanInterval", device->scan_interval);
1703 __connman_storage_close_profile(ident, keyfile, TRUE);
1708 static struct connman_storage device_storage = {
1710 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1711 .device_load = device_load,
1712 .device_save = device_save,
1715 int __connman_device_init(void)
1719 connection = connman_dbus_get_connection();
1721 if (connman_storage_register(&device_storage) < 0)
1722 connman_error("Failed to register device storage");
1724 return connman_driver_register(&device_driver);
1727 void __connman_device_cleanup(void)
1731 connman_driver_unregister(&device_driver);
1733 connman_storage_unregister(&device_storage);
1735 dbus_connection_unref(connection);