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 %d", device, device->blocked);
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;
745 enum connman_service_type service_type;
747 DBG("node %s type %d", node, type);
749 device = g_try_new0(struct connman_device, 1);
753 DBG("device %p", device);
755 __connman_element_initialize(&device->element);
757 device->element.name = g_strdup(node);
758 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
760 device->element.device = device;
761 device->element.destruct = device_destruct;
763 str = type2string(type);
765 connman_element_set_string(&device->element,
766 CONNMAN_PROPERTY_ID_TYPE, str);
768 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
771 device->name = g_strdup(type2description(device->type));
772 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
774 device->powered_persistent = TRUE;
776 device->phyindex = -1;
778 service_type = __connman_device_get_service_type(device);
779 device->blocked = __connman_technology_get_blocked(service_type);
782 case CONNMAN_DEVICE_TYPE_UNKNOWN:
783 case CONNMAN_DEVICE_TYPE_VENDOR:
784 device->scan_interval = 0;
786 case CONNMAN_DEVICE_TYPE_ETHERNET:
787 case CONNMAN_DEVICE_TYPE_WIFI:
788 device->scan_interval = 300;
790 case CONNMAN_DEVICE_TYPE_WIMAX:
791 device->scan_interval = 0;
793 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
794 device->scan_interval = 0;
796 case CONNMAN_DEVICE_TYPE_GPS:
797 device->scan_interval = 0;
799 case CONNMAN_DEVICE_TYPE_CELLULAR:
800 device->scan_interval = 0;
804 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
805 g_free, unregister_network);
811 * connman_device_ref:
812 * @device: device structure
814 * Increase reference counter of device
816 struct connman_device *connman_device_ref(struct connman_device *device)
818 if (connman_element_ref(&device->element) == NULL)
825 * connman_device_unref:
826 * @device: device structure
828 * Decrease reference counter of device
830 void connman_device_unref(struct connman_device *device)
832 connman_element_unref(&device->element);
835 const char *__connman_device_get_type(struct connman_device *device)
837 return type2string(device->type);
841 * connman_device_get_type:
842 * @device: device structure
846 enum connman_device_type connman_device_get_type(struct connman_device *device)
852 * connman_device_get_name:
853 * @device: device structure
855 * Get unique name of device
857 const char *connman_device_get_name(struct connman_device *device)
859 return device->element.name;
863 * connman_device_get_path:
864 * @device: device structure
866 * Get path name of device
868 const char *connman_device_get_path(struct connman_device *device)
870 return device->element.path;
874 * connman_device_set_index:
875 * @device: device structure
876 * @index: index number
878 * Set index number of device
880 void connman_device_set_index(struct connman_device *device, int index)
882 device->element.index = index;
886 * connman_device_get_index:
887 * @device: device structure
889 * Get index number of device
891 int connman_device_get_index(struct connman_device *device)
893 return device->element.index;
896 int __connman_device_get_phyindex(struct connman_device *device)
898 return device->phyindex;
901 void __connman_device_set_phyindex(struct connman_device *device,
904 device->phyindex = phyindex;
908 * connman_device_set_interface:
909 * @device: device structure
910 * @interface: interface name
911 * @control: control interface
913 * Set interface name of device
915 void connman_device_set_interface(struct connman_device *device,
916 const char *interface, const char *control)
918 g_free(device->element.devname);
919 device->element.devname = g_strdup(interface);
921 g_free(device->interface);
922 device->interface = g_strdup(interface);
924 g_free(device->control);
925 device->control = g_strdup(control);
927 if (device->name == NULL) {
928 const char *str = type2description(device->type);
929 if (str != NULL && device->interface != NULL)
930 device->name = g_strdup_printf("%s (%s)", str,
935 const char *connman_device_get_control(struct connman_device *device)
937 return device->control;
941 * connman_device_set_ident:
942 * @device: device structure
943 * @ident: unique identifier
945 * Set unique identifier of device
947 void connman_device_set_ident(struct connman_device *device,
950 g_free(device->ident);
951 device->ident = g_strdup(ident);
954 const char *connman_device_get_ident(struct connman_device *device)
956 return device->ident;
960 * connman_device_set_mode:
961 * @device: device structure
962 * @mode: network mode
964 * Change network mode of device
966 void connman_device_set_mode(struct connman_device *device,
967 enum connman_device_mode mode)
973 * connman_device_get_mode:
974 * @device: device structure
976 * Get network mode of device
978 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
984 * connman_device_set_powered:
985 * @device: device structure
986 * @powered: powered state
988 * Change power state of device
990 int connman_device_set_powered(struct connman_device *device,
991 connman_bool_t powered)
993 DBG("driver %p powered %d", device, powered);
995 if (device->timeout > 0) {
996 g_source_remove(device->timeout);
1000 if (device->pending != NULL) {
1001 g_dbus_send_reply(connection, device->pending,
1004 dbus_message_unref(device->pending);
1005 device->pending = NULL;
1008 if (device->powered == powered) {
1009 device->powered_pending = powered;
1013 if (powered == TRUE)
1014 __connman_device_enable(device);
1016 __connman_device_disable(device);
1018 device->powered = powered;
1019 device->powered_pending = powered;
1021 if (device->powered == TRUE)
1022 __connman_technology_enable_device(device);
1024 __connman_technology_disable_device(device);
1026 if (device->offlinemode == TRUE && powered == TRUE) {
1027 powered_changed(device);
1028 return connman_device_set_powered(device, FALSE);
1031 if (device->registered == FALSE)
1034 powered_changed(device);
1036 if (powered == FALSE)
1039 reset_scan_trigger(device);
1041 if (device->driver->scan)
1042 device->driver->scan(device);
1047 int __connman_device_set_blocked(struct connman_device *device,
1048 connman_bool_t blocked)
1050 connman_bool_t powered;
1052 DBG("device %p blocked %d", device, blocked);
1054 device->blocked = blocked;
1056 blocked_changed(device);
1058 if (device->offlinemode == TRUE)
1061 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1063 if (blocked == FALSE)
1064 powered = device->powered_persistent;
1068 return set_powered(device, powered);
1071 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1073 return device->blocked;
1076 int __connman_device_scan(struct connman_device *device)
1078 if (!device->driver || !device->driver->scan)
1081 if (device->powered == FALSE)
1084 reset_scan_trigger(device);
1086 return device->driver->scan(device);
1089 int __connman_device_enable_persistent(struct connman_device *device)
1093 DBG("device %p", device);
1095 device->powered_persistent = TRUE;
1097 __connman_storage_save_device(device);
1099 err = __connman_device_enable(device);
1100 if (err == 0 || err == -EINPROGRESS) {
1101 device->offlinemode = FALSE;
1102 if (__connman_profile_get_offlinemode() == TRUE) {
1103 __connman_profile_set_offlinemode(FALSE, FALSE);
1105 __connman_profile_save_default();
1112 int __connman_device_disable_persistent(struct connman_device *device)
1114 DBG("device %p", device);
1116 device->powered_persistent = FALSE;
1118 __connman_storage_save_device(device);
1120 return __connman_device_disable(device);
1123 int __connman_device_disconnect(struct connman_device *device)
1125 GHashTableIter iter;
1126 gpointer key, value;
1128 DBG("device %p", device);
1130 connman_device_set_disconnected(device, TRUE);
1132 g_hash_table_iter_init(&iter, device->networks);
1134 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1135 struct connman_network *network = value;
1137 if (__connman_network_get_connecting(network) == TRUE) {
1139 * Skip network in the process of connecting.
1140 * This is a workaround for WiFi networks serviced
1141 * by the supplicant plugin that hold a reference
1142 * to the network. If we disconnect the network
1143 * here then the referenced object will not be
1144 * registered and usage (like launching DHCP client)
1145 * will fail. There is nothing to be gained by
1146 * removing the network here anyway.
1148 connman_warn("Skipping disconnect of %s",
1149 connman_network_get_identifier(network));
1153 __connman_network_disconnect(network);
1159 static void mark_network_unavailable(gpointer key, gpointer value,
1162 struct connman_network *network = value;
1164 if (connman_network_get_connected(network) == TRUE)
1167 connman_network_set_available(network, FALSE);
1170 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1173 struct connman_network *network = value;
1175 if (connman_network_get_connected(network) == TRUE)
1178 if (connman_network_get_available(network) == TRUE)
1184 void __connman_device_cleanup_networks(struct connman_device *device)
1186 g_hash_table_foreach_remove(device->networks,
1187 remove_unavailable_network, NULL);
1190 static void scanning_changed(struct connman_device *device)
1192 connman_dbus_property_changed_basic(device->element.path,
1193 CONNMAN_DEVICE_INTERFACE, "Scanning",
1194 DBUS_TYPE_BOOLEAN, &device->scanning);
1198 * connman_device_set_scanning:
1199 * @device: device structure
1200 * @scanning: scanning state
1202 * Change scanning state of device
1204 int connman_device_set_scanning(struct connman_device *device,
1205 connman_bool_t scanning)
1207 DBG("device %p scanning %d", device, scanning);
1209 if (!device->driver || !device->driver->scan)
1212 if (device->scanning == scanning)
1215 device->scanning = scanning;
1217 scanning_changed(device);
1219 if (scanning == TRUE) {
1220 reset_scan_trigger(device);
1222 g_hash_table_foreach(device->networks,
1223 mark_network_unavailable, NULL);
1228 __connman_device_cleanup_networks(device);
1230 if (device->connections > 0)
1233 if (device->disconnected == TRUE)
1236 __connman_service_auto_connect();
1242 * connman_device_set_disconnected:
1243 * @device: device structure
1244 * @disconnected: disconnected state
1246 * Change disconnected state of device (only for device with networks)
1248 int connman_device_set_disconnected(struct connman_device *device,
1249 connman_bool_t disconnected)
1251 DBG("device %p disconnected %d", device, disconnected);
1253 switch (device->mode) {
1254 case CONNMAN_DEVICE_MODE_UNKNOWN:
1256 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1257 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1261 if (device->disconnected == disconnected)
1264 device->disconnected = disconnected;
1266 if (disconnected == TRUE)
1267 force_scan_trigger(device);
1273 * connman_device_get_disconnected:
1274 * @device: device structure
1276 * Get device disconnected state
1278 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1280 return device->disconnected;
1284 * connman_device_set_string:
1285 * @device: device structure
1286 * @key: unique identifier
1287 * @value: string value
1289 * Set string value for specific key
1291 int connman_device_set_string(struct connman_device *device,
1292 const char *key, const char *value)
1294 DBG("device %p key %s value %s", device, key, value);
1296 if (g_str_equal(key, "Address") == TRUE) {
1297 g_free(device->address);
1298 device->address = g_strdup(value);
1299 } else if (g_str_equal(key, "Name") == TRUE) {
1300 g_free(device->name);
1301 device->name = g_strdup(value);
1302 } else if (g_str_equal(key, "Node") == TRUE) {
1303 g_free(device->node);
1304 device->node = g_strdup(value);
1307 return connman_element_set_string(&device->element, key, value);
1311 * connman_device_get_string:
1312 * @device: device structure
1313 * @key: unique identifier
1315 * Get string value for specific key
1317 const char *connman_device_get_string(struct connman_device *device,
1320 DBG("device %p key %s", device, key);
1322 if (g_str_equal(key, "Address") == TRUE)
1323 return device->address;
1324 else if (g_str_equal(key, "Name") == TRUE)
1325 return device->name;
1326 else if (g_str_equal(key, "Node") == TRUE)
1327 return device->node;
1328 else if (g_str_equal(key, "Interface") == TRUE)
1329 return device->interface;
1331 return connman_element_get_string(&device->element, key);
1334 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1336 struct connman_device *device = element->device;
1337 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1338 connman_bool_t powered;
1340 DBG("element %p name %s", element, element->name);
1345 device->offlinemode = offlinemode;
1347 if (device->blocked == TRUE)
1350 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1352 if (device->powered == powered)
1355 if (device->powered_persistent == FALSE)
1358 set_powered(device, powered);
1361 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1363 DBG("offlinmode %d", offlinemode);
1365 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1366 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1368 __connman_notifier_offlinemode(offlinemode);
1373 void __connman_device_increase_connections(struct connman_device *device)
1378 device->connections++;
1381 void __connman_device_decrease_connections(struct connman_device *device)
1386 device->connections--;
1390 * connman_device_add_network:
1391 * @device: device structure
1392 * @network: network structure
1394 * Add new network to the device
1396 int connman_device_add_network(struct connman_device *device,
1397 struct connman_network *network)
1399 const char *identifier = connman_network_get_identifier(network);
1402 DBG("device %p network %p", device, network);
1404 if (identifier == NULL)
1407 switch (device->mode) {
1408 case CONNMAN_DEVICE_MODE_UNKNOWN:
1410 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1411 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1415 __connman_network_set_device(network, device);
1417 err = connman_element_register((struct connman_element *) network,
1420 __connman_network_set_device(network, NULL);
1424 g_hash_table_insert(device->networks, g_strdup(identifier),
1431 * connman_device_get_network:
1432 * @device: device structure
1433 * @identifier: network identifier
1435 * Get network for given identifier
1437 struct connman_network *connman_device_get_network(struct connman_device *device,
1438 const char *identifier)
1440 DBG("device %p identifier %s", device, identifier);
1442 return g_hash_table_lookup(device->networks, identifier);
1446 * connman_device_remove_network:
1447 * @device: device structure
1448 * @identifier: network identifier
1450 * Remove network for given identifier
1452 int connman_device_remove_network(struct connman_device *device,
1453 const char *identifier)
1455 DBG("device %p identifier %s", device, identifier);
1457 g_hash_table_remove(device->networks, identifier);
1462 void connman_device_remove_all_networks(struct connman_device *device)
1464 g_hash_table_remove_all(device->networks);
1467 void __connman_device_set_network(struct connman_device *device,
1468 struct connman_network *network)
1475 if (device->network == network)
1478 if (device->network != NULL)
1479 connman_network_unref(device->network);
1481 if (network != NULL) {
1482 name = connman_network_get_string(network,
1483 CONNMAN_PROPERTY_ID_NAME);
1484 g_free(device->last_network);
1485 device->last_network = g_strdup(name);
1487 device->network = connman_network_ref(network);
1489 g_free(device->last_network);
1490 device->last_network = NULL;
1492 device->network = NULL;
1496 void __connman_device_set_reconnect(struct connman_device *device,
1497 connman_bool_t reconnect)
1499 device->reconnect = reconnect;
1502 connman_bool_t __connman_device_get_reconnect(
1503 struct connman_device *device)
1505 return device->reconnect;
1509 * connman_device_register:
1510 * @device: device structure
1512 * Register device with the system
1514 int connman_device_register(struct connman_device *device)
1516 __connman_storage_load_device(device);
1518 device->offlinemode = __connman_profile_get_offlinemode();
1520 return connman_element_register(&device->element, NULL);
1524 * connman_device_unregister:
1525 * @device: device structure
1527 * Unregister device with the system
1529 void connman_device_unregister(struct connman_device *device)
1531 __connman_storage_save_device(device);
1533 connman_element_unregister(&device->element);
1537 * connman_device_get_data:
1538 * @device: device structure
1540 * Get private device data pointer
1542 void *connman_device_get_data(struct connman_device *device)
1544 return device->driver_data;
1548 * connman_device_set_data:
1549 * @device: device structure
1550 * @data: data pointer
1552 * Set private device data pointer
1554 void connman_device_set_data(struct connman_device *device, void *data)
1556 device->driver_data = data;
1559 static gboolean match_driver(struct connman_device *device,
1560 struct connman_device_driver *driver)
1562 if (device->type == driver->type ||
1563 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1569 static int device_probe(struct connman_element *element)
1571 struct connman_device *device = element->device;
1574 DBG("element %p name %s", element, element->name);
1579 if (device->driver != NULL)
1582 for (list = driver_list; list; list = list->next) {
1583 struct connman_device_driver *driver = list->data;
1585 if (match_driver(device, driver) == FALSE)
1588 DBG("driver %p name %s", driver, driver->name);
1590 if (driver->probe(device) == 0) {
1591 device->driver = driver;
1596 if (device->driver == NULL)
1599 return setup_device(device);
1602 static void device_remove(struct connman_element *element)
1604 struct connman_device *device = element->device;
1606 DBG("element %p name %s", element, element->name);
1611 if (device->driver == NULL)
1614 remove_device(device);
1617 static struct connman_driver device_driver = {
1619 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1620 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1621 .probe = device_probe,
1622 .remove = device_remove,
1625 static int device_load(struct connman_device *device)
1627 const char *ident = __connman_profile_active_ident();
1629 GError *error = NULL;
1631 connman_bool_t powered;
1634 DBG("device %p", device);
1636 keyfile = __connman_storage_open_profile(ident);
1637 if (keyfile == NULL)
1640 identifier = g_strdup_printf("device_%s", device->element.name);
1641 if (identifier == NULL)
1644 powered = g_key_file_get_boolean(keyfile, identifier,
1647 device->powered_persistent = powered;
1648 g_clear_error(&error);
1650 switch (device->mode) {
1651 case CONNMAN_DEVICE_MODE_UNKNOWN:
1653 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1654 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1655 val = g_key_file_get_integer(keyfile, identifier,
1656 "ScanInterval", &error);
1657 if (error == NULL && val > 0)
1658 device->scan_interval = val;
1659 g_clear_error(&error);
1666 __connman_storage_close_profile(ident, keyfile, FALSE);
1671 static int device_save(struct connman_device *device)
1673 const char *ident = __connman_profile_active_ident();
1677 DBG("device %p", device);
1679 keyfile = __connman_storage_open_profile(ident);
1680 if (keyfile == NULL)
1683 identifier = g_strdup_printf("device_%s", device->element.name);
1684 if (identifier == NULL)
1687 g_key_file_set_boolean(keyfile, identifier,
1688 "Powered", device->powered_persistent);
1690 switch (device->mode) {
1691 case CONNMAN_DEVICE_MODE_UNKNOWN:
1693 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1694 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1695 if (device->scan_interval > 0)
1696 g_key_file_set_integer(keyfile, identifier,
1697 "ScanInterval", device->scan_interval);
1704 __connman_storage_close_profile(ident, keyfile, TRUE);
1709 static struct connman_storage device_storage = {
1711 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1712 .device_load = device_load,
1713 .device_save = device_save,
1716 int __connman_device_init(void)
1720 connection = connman_dbus_get_connection();
1722 if (connman_storage_register(&device_storage) < 0)
1723 connman_error("Failed to register device storage");
1725 return connman_driver_register(&device_driver);
1728 void __connman_device_cleanup(void)
1732 connman_driver_unregister(&device_driver);
1734 connman_storage_unregister(&device_storage);
1736 dbus_connection_unref(connection);