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 reply = dbus_message_new_method_return(msg);
322 dbus_message_iter_init_append(reply, &array);
324 connman_dbus_dict_open(&array, &dict);
326 if (device->name != NULL)
327 connman_dbus_dict_append_basic(&dict, "Name",
328 DBUS_TYPE_STRING, &device->name);
330 str = type2string(device->type);
332 connman_dbus_dict_append_basic(&dict, "Type",
333 DBUS_TYPE_STRING, &str);
335 if (device->address != NULL)
336 connman_dbus_dict_append_basic(&dict, "Address",
337 DBUS_TYPE_STRING, &device->address);
339 if (device->interface != NULL)
340 connman_dbus_dict_append_basic(&dict, "Interface",
341 DBUS_TYPE_STRING, &device->interface);
343 connman_dbus_dict_append_basic(&dict, "Powered",
344 DBUS_TYPE_BOOLEAN, &device->powered);
346 connman_dbus_dict_append_basic(&dict, "Blocked",
347 DBUS_TYPE_BOOLEAN, &device->blocked);
349 if (device->driver && device->driver->scan)
350 connman_dbus_dict_append_basic(&dict, "Scanning",
351 DBUS_TYPE_BOOLEAN, &device->scanning);
353 switch (device->mode) {
354 case CONNMAN_DEVICE_MODE_UNKNOWN:
356 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
357 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
358 if (device->scan_interval > 0)
359 connman_dbus_dict_append_basic(&dict, "ScanInterval",
360 DBUS_TYPE_UINT16, &device->scan_interval);
362 connman_dbus_dict_append_array(&dict, "Networks",
363 DBUS_TYPE_OBJECT_PATH, append_networks, device);
367 connman_dbus_dict_close(&array, &dict);
372 static gboolean powered_timeout(gpointer user_data)
374 struct connman_device *device = user_data;
376 DBG("device %p", device);
380 if (device->pending != NULL) {
383 reply = __connman_error_operation_timeout(device->pending);
385 g_dbus_send_message(connection, reply);
387 dbus_message_unref(device->pending);
388 device->pending = NULL;
394 static DBusMessage *set_property(DBusConnection *conn,
395 DBusMessage *msg, void *data)
397 struct connman_device *device = data;
398 DBusMessageIter iter, value;
402 DBG("conn %p", conn);
404 if (dbus_message_iter_init(msg, &iter) == FALSE)
405 return __connman_error_invalid_arguments(msg);
407 dbus_message_iter_get_basic(&iter, &name);
408 dbus_message_iter_next(&iter);
409 dbus_message_iter_recurse(&iter, &value);
411 type = dbus_message_iter_get_arg_type(&value);
413 if (g_str_equal(name, "Powered") == TRUE) {
414 connman_bool_t powered;
417 if (type != DBUS_TYPE_BOOLEAN)
418 return __connman_error_invalid_arguments(msg);
420 dbus_message_iter_get_basic(&value, &powered);
422 device->powered_persistent = powered;
424 __connman_storage_save_device(device);
426 if (device->powered == powered)
427 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
429 if (device->pending != NULL)
430 return __connman_error_in_progress(msg);
432 err = set_powered(device, powered);
434 if (err != -EINPROGRESS)
435 return __connman_error_failed(msg, -err);
437 device->pending = dbus_message_ref(msg);
439 device->timeout = g_timeout_add_seconds(15,
440 powered_timeout, device);
444 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
445 connman_uint16_t interval;
447 switch (device->mode) {
448 case CONNMAN_DEVICE_MODE_UNKNOWN:
449 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
450 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
454 if (type != DBUS_TYPE_UINT16)
455 return __connman_error_invalid_arguments(msg);
457 dbus_message_iter_get_basic(&value, &interval);
459 if (device->scan_interval != interval) {
460 device->scan_interval = interval;
462 __connman_storage_save_device(device);
464 reset_scan_trigger(device);
467 return __connman_error_invalid_property(msg);
469 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
472 static DBusMessage *propose_scan(DBusConnection *conn,
473 DBusMessage *msg, void *data)
475 struct connman_device *device = data;
478 DBG("conn %p", conn);
480 switch (device->mode) {
481 case CONNMAN_DEVICE_MODE_UNKNOWN:
482 return __connman_error_not_supported(msg);
483 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
484 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
488 err = __connman_device_scan(device);
490 return __connman_error_failed(msg, -err);
492 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
495 static GDBusMethodTable device_methods[] = {
496 { "GetProperties", "", "a{sv}", get_properties },
497 { "SetProperty", "sv", "", set_property,
498 G_DBUS_METHOD_FLAG_ASYNC },
499 { "ProposeScan", "", "", propose_scan },
503 static GDBusSignalTable device_signals[] = {
504 { "PropertyChanged", "sv" },
508 static int register_interface(struct connman_element *element)
510 struct connman_device *device = element->device;
512 DBG("element %p name %s", element, element->name);
514 if (g_dbus_register_interface(connection, element->path,
515 CONNMAN_DEVICE_INTERFACE,
516 device_methods, device_signals,
517 NULL, device, NULL) == FALSE) {
518 connman_error("Failed to register %s device", element->path);
522 device->registered = TRUE;
527 static void unregister_interface(struct connman_element *element)
529 struct connman_device *device = element->device;
531 DBG("element %p name %s", element, element->name);
533 device->registered = FALSE;
535 g_dbus_unregister_interface(connection, element->path,
536 CONNMAN_DEVICE_INTERFACE);
539 static int setup_device(struct connman_device *device)
543 DBG("device %p", device);
545 err = register_interface(&device->element);
547 if (device->driver->remove)
548 device->driver->remove(device);
549 device->driver = NULL;
553 __connman_technology_add_device(device);
555 switch (device->mode) {
556 case CONNMAN_DEVICE_MODE_UNKNOWN:
557 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
558 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
562 if (device->offlinemode == FALSE &&
563 device->powered_persistent == TRUE)
564 __connman_device_enable(device);
569 static void probe_driver(struct connman_element *element, gpointer user_data)
571 struct connman_device_driver *driver = user_data;
573 DBG("element %p name %s", element, element->name);
575 if (element->device == NULL)
578 if (element->device->driver != NULL)
581 if (driver->type != element->device->type)
584 if (driver->probe(element->device) < 0)
587 element->device->driver = driver;
589 setup_device(element->device);
592 static void remove_device(struct connman_device *device)
596 DBG("device %p", device);
598 err = __connman_device_disable(device);
599 if (err < 0 && err == -EINPROGRESS)
600 __connman_technology_disable_device(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_set_device(network, NULL);
697 connman_network_unref(network);
700 static void device_destruct(struct connman_element *element)
702 struct connman_device *device = element->device;
704 DBG("element %p name %s", element, element->name);
706 if (device->timeout > 0) {
707 g_source_remove(device->timeout);
711 clear_scan_trigger(device);
713 if (device->pending != NULL) {
714 dbus_message_unref(device->pending);
715 device->pending = NULL;
718 g_free(device->ident);
719 g_free(device->node);
720 g_free(device->name);
721 g_free(device->address);
722 g_free(device->control);
723 g_free(device->interface);
725 g_free(device->last_network);
727 g_hash_table_destroy(device->networks);
728 device->networks = NULL;
732 * connman_device_create:
733 * @node: device node name (for example an address)
736 * Allocate a new device of given #type and assign the #node name to it.
738 * Returns: a newly-allocated #connman_device structure
740 struct connman_device *connman_device_create(const char *node,
741 enum connman_device_type type)
743 struct connman_device *device;
746 DBG("node %s type %d", node, type);
748 device = g_try_new0(struct connman_device, 1);
752 DBG("device %p", device);
754 __connman_element_initialize(&device->element);
756 device->element.name = g_strdup(node);
757 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
759 device->element.device = device;
760 device->element.destruct = device_destruct;
762 str = type2string(type);
764 connman_element_set_string(&device->element,
765 CONNMAN_PROPERTY_ID_TYPE, str);
767 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
770 device->name = g_strdup(type2description(device->type));
771 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
773 device->powered_persistent = TRUE;
775 device->phyindex = -1;
778 case CONNMAN_DEVICE_TYPE_UNKNOWN:
779 case CONNMAN_DEVICE_TYPE_VENDOR:
780 device->scan_interval = 0;
782 case CONNMAN_DEVICE_TYPE_ETHERNET:
783 case CONNMAN_DEVICE_TYPE_WIFI:
784 device->scan_interval = 300;
786 case CONNMAN_DEVICE_TYPE_WIMAX:
787 device->scan_interval = 0;
789 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
790 device->scan_interval = 0;
792 case CONNMAN_DEVICE_TYPE_GPS:
793 device->scan_interval = 0;
795 case CONNMAN_DEVICE_TYPE_CELLULAR:
796 device->scan_interval = 0;
800 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
801 g_free, unregister_network);
807 * connman_device_ref:
808 * @device: device structure
810 * Increase reference counter of device
812 struct connman_device *connman_device_ref(struct connman_device *device)
814 if (connman_element_ref(&device->element) == NULL)
821 * connman_device_unref:
822 * @device: device structure
824 * Decrease reference counter of device
826 void connman_device_unref(struct connman_device *device)
828 connman_element_unref(&device->element);
831 const char *__connman_device_get_type(struct connman_device *device)
833 return type2string(device->type);
837 * connman_device_get_type:
838 * @device: device structure
842 enum connman_device_type connman_device_get_type(struct connman_device *device)
848 * connman_device_get_name:
849 * @device: device structure
851 * Get unique name of device
853 const char *connman_device_get_name(struct connman_device *device)
855 return device->element.name;
859 * connman_device_get_path:
860 * @device: device structure
862 * Get path name of device
864 const char *connman_device_get_path(struct connman_device *device)
866 return device->element.path;
870 * connman_device_set_index:
871 * @device: device structure
872 * @index: index number
874 * Set index number of device
876 void connman_device_set_index(struct connman_device *device, int index)
878 device->element.index = index;
882 * connman_device_get_index:
883 * @device: device structure
885 * Get index number of device
887 int connman_device_get_index(struct connman_device *device)
889 return device->element.index;
892 int __connman_device_get_phyindex(struct connman_device *device)
894 return device->phyindex;
897 void __connman_device_set_phyindex(struct connman_device *device,
900 device->phyindex = phyindex;
904 * connman_device_set_interface:
905 * @device: device structure
906 * @interface: interface name
907 * @control: control interface
909 * Set interface name of device
911 void connman_device_set_interface(struct connman_device *device,
912 const char *interface, const char *control)
914 g_free(device->element.devname);
915 device->element.devname = g_strdup(interface);
917 g_free(device->interface);
918 device->interface = g_strdup(interface);
920 g_free(device->control);
921 device->control = g_strdup(control);
923 if (device->name == NULL) {
924 const char *str = type2description(device->type);
925 if (str != NULL && device->interface != NULL)
926 device->name = g_strdup_printf("%s (%s)", str,
931 const char *connman_device_get_control(struct connman_device *device)
933 return device->control;
937 * connman_device_set_ident:
938 * @device: device structure
939 * @ident: unique identifier
941 * Set unique identifier of device
943 void connman_device_set_ident(struct connman_device *device,
946 g_free(device->ident);
947 device->ident = g_strdup(ident);
950 const char *connman_device_get_ident(struct connman_device *device)
952 return device->ident;
956 * connman_device_set_mode:
957 * @device: device structure
958 * @mode: network mode
960 * Change network mode of device
962 void connman_device_set_mode(struct connman_device *device,
963 enum connman_device_mode mode)
969 * connman_device_get_mode:
970 * @device: device structure
972 * Get network mode of device
974 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
980 * connman_device_set_powered:
981 * @device: device structure
982 * @powered: powered state
984 * Change power state of device
986 int connman_device_set_powered(struct connman_device *device,
987 connman_bool_t powered)
989 DBG("driver %p powered %d", device, powered);
991 if (device->timeout > 0) {
992 g_source_remove(device->timeout);
996 if (device->pending != NULL) {
997 g_dbus_send_reply(connection, device->pending,
1000 dbus_message_unref(device->pending);
1001 device->pending = NULL;
1004 if (device->powered == powered) {
1005 device->powered_pending = powered;
1009 if (powered == TRUE)
1010 __connman_device_enable(device);
1012 __connman_device_disable(device);
1014 device->powered = powered;
1015 device->powered_pending = powered;
1017 if (device->powered == TRUE)
1018 __connman_technology_enable_device(device);
1020 __connman_technology_disable_device(device);
1022 if (device->offlinemode == TRUE && powered == TRUE) {
1023 powered_changed(device);
1024 return connman_device_set_powered(device, FALSE);
1027 if (device->registered == FALSE)
1030 powered_changed(device);
1032 if (powered == FALSE)
1035 reset_scan_trigger(device);
1037 if (device->driver->scan)
1038 device->driver->scan(device);
1043 int __connman_device_set_blocked(struct connman_device *device,
1044 connman_bool_t blocked)
1046 connman_bool_t powered;
1048 DBG("device %p blocked %d", device, blocked);
1050 device->blocked = blocked;
1052 blocked_changed(device);
1054 if (device->offlinemode == TRUE)
1057 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1059 if (blocked == FALSE)
1060 powered = device->powered_persistent;
1064 return set_powered(device, powered);
1067 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1069 return device->blocked;
1072 int __connman_device_scan(struct connman_device *device)
1074 if (!device->driver || !device->driver->scan)
1077 if (device->powered == FALSE)
1080 reset_scan_trigger(device);
1082 return device->driver->scan(device);
1085 int __connman_device_enable_persistent(struct connman_device *device)
1089 DBG("device %p", device);
1091 device->powered_persistent = TRUE;
1093 __connman_storage_save_device(device);
1095 err = __connman_device_enable(device);
1096 if (err == 0 || err == -EINPROGRESS) {
1097 device->offlinemode = FALSE;
1098 if (__connman_profile_get_offlinemode() == TRUE) {
1099 __connman_profile_set_offlinemode(FALSE, FALSE);
1101 __connman_profile_save_default();
1108 int __connman_device_disable_persistent(struct connman_device *device)
1110 DBG("device %p", device);
1112 device->powered_persistent = FALSE;
1114 __connman_storage_save_device(device);
1116 return __connman_device_disable(device);
1119 int __connman_device_disconnect(struct connman_device *device)
1121 GHashTableIter iter;
1122 gpointer key, value;
1124 DBG("device %p", device);
1126 connman_device_set_disconnected(device, TRUE);
1128 g_hash_table_iter_init(&iter, device->networks);
1130 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1131 struct connman_network *network = value;
1133 if (__connman_network_get_connecting(network) == TRUE) {
1135 * Skip network in the process of connecting.
1136 * This is a workaround for WiFi networks serviced
1137 * by the supplicant plugin that hold a reference
1138 * to the network. If we disconnect the network
1139 * here then the referenced object will not be
1140 * registered and usage (like launching DHCP client)
1141 * will fail. There is nothing to be gained by
1142 * removing the network here anyway.
1144 connman_warn("Skipping disconnect of %s",
1145 connman_network_get_identifier(network));
1149 __connman_network_disconnect(network);
1155 static void mark_network_unavailable(gpointer key, gpointer value,
1158 struct connman_network *network = value;
1160 if (connman_network_get_connected(network) == TRUE)
1163 connman_network_set_available(network, FALSE);
1166 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1169 struct connman_network *network = value;
1171 if (connman_network_get_connected(network) == TRUE)
1174 if (connman_network_get_available(network) == TRUE)
1180 void __connman_device_cleanup_networks(struct connman_device *device)
1182 g_hash_table_foreach_remove(device->networks,
1183 remove_unavailable_network, NULL);
1186 static void scanning_changed(struct connman_device *device)
1188 connman_dbus_property_changed_basic(device->element.path,
1189 CONNMAN_DEVICE_INTERFACE, "Scanning",
1190 DBUS_TYPE_BOOLEAN, &device->scanning);
1194 * connman_device_set_scanning:
1195 * @device: device structure
1196 * @scanning: scanning state
1198 * Change scanning state of device
1200 int connman_device_set_scanning(struct connman_device *device,
1201 connman_bool_t scanning)
1203 DBG("device %p scanning %d", device, scanning);
1205 if (!device->driver || !device->driver->scan)
1208 if (device->scanning == scanning)
1211 device->scanning = scanning;
1213 scanning_changed(device);
1215 if (scanning == TRUE) {
1216 reset_scan_trigger(device);
1218 g_hash_table_foreach(device->networks,
1219 mark_network_unavailable, NULL);
1224 __connman_device_cleanup_networks(device);
1226 if (device->connections > 0)
1229 if (device->disconnected == TRUE)
1232 __connman_service_auto_connect();
1238 * connman_device_set_disconnected:
1239 * @device: device structure
1240 * @disconnected: disconnected state
1242 * Change disconnected state of device (only for device with networks)
1244 int connman_device_set_disconnected(struct connman_device *device,
1245 connman_bool_t disconnected)
1247 DBG("device %p disconnected %d", device, disconnected);
1249 switch (device->mode) {
1250 case CONNMAN_DEVICE_MODE_UNKNOWN:
1252 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1253 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1257 if (device->disconnected == disconnected)
1260 device->disconnected = disconnected;
1262 if (disconnected == TRUE)
1263 force_scan_trigger(device);
1269 * connman_device_get_disconnected:
1270 * @device: device structure
1272 * Get device disconnected state
1274 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1276 return device->disconnected;
1280 * connman_device_set_string:
1281 * @device: device structure
1282 * @key: unique identifier
1283 * @value: string value
1285 * Set string value for specific key
1287 int connman_device_set_string(struct connman_device *device,
1288 const char *key, const char *value)
1290 DBG("device %p key %s value %s", device, key, value);
1292 if (g_str_equal(key, "Address") == TRUE) {
1293 g_free(device->address);
1294 device->address = g_strdup(value);
1295 } else if (g_str_equal(key, "Name") == TRUE) {
1296 g_free(device->name);
1297 device->name = g_strdup(value);
1298 } else if (g_str_equal(key, "Node") == TRUE) {
1299 g_free(device->node);
1300 device->node = g_strdup(value);
1303 return connman_element_set_string(&device->element, key, value);
1307 * connman_device_get_string:
1308 * @device: device structure
1309 * @key: unique identifier
1311 * Get string value for specific key
1313 const char *connman_device_get_string(struct connman_device *device,
1316 DBG("device %p key %s", device, key);
1318 if (g_str_equal(key, "Address") == TRUE)
1319 return device->address;
1320 else if (g_str_equal(key, "Name") == TRUE)
1321 return device->name;
1322 else if (g_str_equal(key, "Node") == TRUE)
1323 return device->node;
1324 else if (g_str_equal(key, "Interface") == TRUE)
1325 return device->interface;
1327 return connman_element_get_string(&device->element, key);
1330 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1332 struct connman_device *device = element->device;
1333 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1334 connman_bool_t powered;
1336 DBG("element %p name %s", element, element->name);
1341 device->offlinemode = offlinemode;
1343 if (device->blocked == TRUE)
1346 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1348 if (device->powered == powered)
1351 if (device->powered_persistent == FALSE)
1354 set_powered(device, powered);
1357 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1359 DBG("offlinmode %d", offlinemode);
1361 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1362 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1364 __connman_notifier_offlinemode(offlinemode);
1369 void __connman_device_increase_connections(struct connman_device *device)
1374 device->connections++;
1377 void __connman_device_decrease_connections(struct connman_device *device)
1382 device->connections--;
1386 * connman_device_add_network:
1387 * @device: device structure
1388 * @network: network structure
1390 * Add new network to the device
1392 int connman_device_add_network(struct connman_device *device,
1393 struct connman_network *network)
1395 const char *identifier = connman_network_get_identifier(network);
1398 DBG("device %p network %p", device, network);
1400 if (identifier == NULL)
1403 switch (device->mode) {
1404 case CONNMAN_DEVICE_MODE_UNKNOWN:
1406 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1407 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1411 __connman_network_set_device(network, device);
1413 err = connman_element_register((struct connman_element *) network,
1416 __connman_network_set_device(network, NULL);
1420 g_hash_table_insert(device->networks, g_strdup(identifier),
1427 * connman_device_get_network:
1428 * @device: device structure
1429 * @identifier: network identifier
1431 * Get network for given identifier
1433 struct connman_network *connman_device_get_network(struct connman_device *device,
1434 const char *identifier)
1436 DBG("device %p identifier %s", device, identifier);
1438 return g_hash_table_lookup(device->networks, identifier);
1442 * connman_device_remove_network:
1443 * @device: device structure
1444 * @identifier: network identifier
1446 * Remove network for given identifier
1448 int connman_device_remove_network(struct connman_device *device,
1449 const char *identifier)
1451 DBG("device %p identifier %s", device, identifier);
1453 g_hash_table_remove(device->networks, identifier);
1458 void connman_device_remove_all_networks(struct connman_device *device)
1460 g_hash_table_remove_all(device->networks);
1463 void __connman_device_set_network(struct connman_device *device,
1464 struct connman_network *network)
1471 if (device->network == network)
1474 if (device->network != NULL)
1475 connman_network_unref(device->network);
1477 if (network != NULL) {
1478 name = connman_network_get_string(network,
1479 CONNMAN_PROPERTY_ID_NAME);
1480 g_free(device->last_network);
1481 device->last_network = g_strdup(name);
1483 device->network = connman_network_ref(network);
1485 g_free(device->last_network);
1486 device->last_network = NULL;
1488 device->network = NULL;
1492 void __connman_device_set_reconnect(struct connman_device *device,
1493 connman_bool_t reconnect)
1495 device->reconnect = reconnect;
1498 connman_bool_t __connman_device_get_reconnect(
1499 struct connman_device *device)
1501 return device->reconnect;
1505 * connman_device_register:
1506 * @device: device structure
1508 * Register device with the system
1510 int connman_device_register(struct connman_device *device)
1512 __connman_storage_load_device(device);
1514 device->offlinemode = __connman_profile_get_offlinemode();
1516 return connman_element_register(&device->element, NULL);
1520 * connman_device_unregister:
1521 * @device: device structure
1523 * Unregister device with the system
1525 void connman_device_unregister(struct connman_device *device)
1527 __connman_storage_save_device(device);
1529 connman_element_unregister(&device->element);
1533 * connman_device_get_data:
1534 * @device: device structure
1536 * Get private device data pointer
1538 void *connman_device_get_data(struct connman_device *device)
1540 return device->driver_data;
1544 * connman_device_set_data:
1545 * @device: device structure
1546 * @data: data pointer
1548 * Set private device data pointer
1550 void connman_device_set_data(struct connman_device *device, void *data)
1552 device->driver_data = data;
1555 static gboolean match_driver(struct connman_device *device,
1556 struct connman_device_driver *driver)
1558 if (device->type == driver->type ||
1559 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1565 static int device_probe(struct connman_element *element)
1567 struct connman_device *device = element->device;
1570 DBG("element %p name %s", element, element->name);
1575 if (device->driver != NULL)
1578 for (list = driver_list; list; list = list->next) {
1579 struct connman_device_driver *driver = list->data;
1581 if (match_driver(device, driver) == FALSE)
1584 DBG("driver %p name %s", driver, driver->name);
1586 if (driver->probe(device) == 0) {
1587 device->driver = driver;
1592 if (device->driver == NULL)
1595 return setup_device(device);
1598 static void device_remove(struct connman_element *element)
1600 struct connman_device *device = element->device;
1602 DBG("element %p name %s", element, element->name);
1607 if (device->driver == NULL)
1610 remove_device(device);
1613 static struct connman_driver device_driver = {
1615 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1616 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1617 .probe = device_probe,
1618 .remove = device_remove,
1621 static int device_load(struct connman_device *device)
1623 const char *ident = __connman_profile_active_ident();
1625 GError *error = NULL;
1627 connman_bool_t powered;
1630 DBG("device %p", device);
1632 keyfile = __connman_storage_open_profile(ident);
1633 if (keyfile == NULL)
1636 identifier = g_strdup_printf("device_%s", device->element.name);
1637 if (identifier == NULL)
1640 powered = g_key_file_get_boolean(keyfile, identifier,
1643 device->powered_persistent = powered;
1644 g_clear_error(&error);
1646 switch (device->mode) {
1647 case CONNMAN_DEVICE_MODE_UNKNOWN:
1649 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1650 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1651 val = g_key_file_get_integer(keyfile, identifier,
1652 "ScanInterval", &error);
1653 if (error == NULL && val > 0)
1654 device->scan_interval = val;
1655 g_clear_error(&error);
1662 __connman_storage_close_profile(ident, keyfile, FALSE);
1667 static int device_save(struct connman_device *device)
1669 const char *ident = __connman_profile_active_ident();
1673 DBG("device %p", device);
1675 keyfile = __connman_storage_open_profile(ident);
1676 if (keyfile == NULL)
1679 identifier = g_strdup_printf("device_%s", device->element.name);
1680 if (identifier == NULL)
1683 g_key_file_set_boolean(keyfile, identifier,
1684 "Powered", device->powered_persistent);
1686 switch (device->mode) {
1687 case CONNMAN_DEVICE_MODE_UNKNOWN:
1689 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1690 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1691 if (device->scan_interval > 0)
1692 g_key_file_set_integer(keyfile, identifier,
1693 "ScanInterval", device->scan_interval);
1700 __connman_storage_close_profile(ident, keyfile, TRUE);
1705 static struct connman_storage device_storage = {
1707 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1708 .device_load = device_load,
1709 .device_save = device_save,
1712 int __connman_device_init(void)
1716 connection = connman_dbus_get_connection();
1718 if (connman_storage_register(&device_storage) < 0)
1719 connman_error("Failed to register device storage");
1721 return connman_driver_register(&device_driver);
1724 void __connman_device_cleanup(void)
1728 connman_driver_unregister(&device_driver);
1730 connman_storage_unregister(&device_storage);
1732 dbus_connection_unref(connection);