5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
143 static const char *type2string(enum connman_device_type type)
146 case CONNMAN_DEVICE_TYPE_UNKNOWN:
147 case CONNMAN_DEVICE_TYPE_VENDOR:
149 case CONNMAN_DEVICE_TYPE_ETHERNET:
151 case CONNMAN_DEVICE_TYPE_WIFI:
153 case CONNMAN_DEVICE_TYPE_WIMAX:
155 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157 case CONNMAN_DEVICE_TYPE_GPS:
159 case CONNMAN_DEVICE_TYPE_CELLULAR:
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
168 enum connman_device_type type = connman_device_get_type(device);
171 case CONNMAN_DEVICE_TYPE_UNKNOWN:
172 case CONNMAN_DEVICE_TYPE_VENDOR:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_CELLULAR:
184 return CONNMAN_SERVICE_TYPE_CELLULAR;
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 static void powered_changed(struct connman_device *device)
192 connman_dbus_property_changed_basic(device->element.path,
193 CONNMAN_DEVICE_INTERFACE, "Powered",
194 DBUS_TYPE_BOOLEAN, &device->powered);
197 static void blocked_changed(struct connman_device *device)
199 connman_dbus_property_changed_basic(device->element.path,
200 CONNMAN_DEVICE_INTERFACE, "Blocked",
201 DBUS_TYPE_BOOLEAN, &device->blocked);
204 int __connman_device_enable(struct connman_device *device)
208 DBG("device %p", device);
210 if (!device->driver || !device->driver->enable)
213 if (device->powered_pending == TRUE)
216 if (device->blocked == TRUE)
219 err = device->driver->enable(device);
221 if (err == -EINPROGRESS)
222 device->powered_pending = TRUE;
226 device->powered_pending = TRUE;
227 device->powered = TRUE;
229 __connman_technology_enable_device(device);
234 int __connman_device_disable(struct connman_device *device)
238 DBG("device %p", device);
240 if (!device->driver || !device->driver->disable)
243 if (device->powered == FALSE)
246 if (device->powered_pending == FALSE)
249 device->reconnect = FALSE;
251 clear_scan_trigger(device);
253 g_hash_table_remove_all(device->networks);
255 err = device->driver->disable(device);
257 if (err == -EINPROGRESS)
258 device->powered_pending = FALSE;
262 device->powered_pending = FALSE;
263 device->powered = FALSE;
265 __connman_technology_disable_device(device);
270 static int set_powered(struct connman_device *device, connman_bool_t powered)
272 DBG("device %p powered %d", device, powered);
275 return __connman_device_enable(device);
277 return __connman_device_disable(device);
280 void __connman_device_list(DBusMessageIter *iter, void *user_data)
282 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
285 static void append_path(gpointer key, gpointer value, gpointer user_data)
287 struct connman_element *element = value;
288 DBusMessageIter *iter = user_data;
290 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
294 static void append_networks(DBusMessageIter *iter, void *user_data)
296 struct connman_device *device = user_data;
298 g_hash_table_foreach(device->networks, append_path, iter);
301 static DBusMessage *get_properties(DBusConnection *conn,
302 DBusMessage *msg, void *data)
304 struct connman_device *device = data;
306 DBusMessageIter array, dict;
309 DBG("conn %p", conn);
311 if (__connman_security_check_privilege(msg,
312 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
313 return __connman_error_permission_denied(msg);
315 reply = dbus_message_new_method_return(msg);
319 dbus_message_iter_init_append(reply, &array);
321 connman_dbus_dict_open(&array, &dict);
323 if (device->name != NULL)
324 connman_dbus_dict_append_basic(&dict, "Name",
325 DBUS_TYPE_STRING, &device->name);
327 str = type2string(device->type);
329 connman_dbus_dict_append_basic(&dict, "Type",
330 DBUS_TYPE_STRING, &str);
332 if (device->address != NULL)
333 connman_dbus_dict_append_basic(&dict, "Address",
334 DBUS_TYPE_STRING, &device->address);
336 if (device->interface != NULL)
337 connman_dbus_dict_append_basic(&dict, "Interface",
338 DBUS_TYPE_STRING, &device->interface);
340 connman_dbus_dict_append_basic(&dict, "Powered",
341 DBUS_TYPE_BOOLEAN, &device->powered);
343 connman_dbus_dict_append_basic(&dict, "Blocked",
344 DBUS_TYPE_BOOLEAN, &device->blocked);
346 if (device->driver && device->driver->scan)
347 connman_dbus_dict_append_basic(&dict, "Scanning",
348 DBUS_TYPE_BOOLEAN, &device->scanning);
350 switch (device->mode) {
351 case CONNMAN_DEVICE_MODE_UNKNOWN:
353 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
354 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
355 if (device->scan_interval > 0)
356 connman_dbus_dict_append_basic(&dict, "ScanInterval",
357 DBUS_TYPE_UINT16, &device->scan_interval);
359 connman_dbus_dict_append_array(&dict, "Networks",
360 DBUS_TYPE_OBJECT_PATH, append_networks, device);
364 connman_dbus_dict_close(&array, &dict);
369 static gboolean powered_timeout(gpointer user_data)
371 struct connman_device *device = user_data;
373 DBG("device %p", device);
377 if (device->pending != NULL) {
380 reply = __connman_error_operation_timeout(device->pending);
382 g_dbus_send_message(connection, reply);
384 dbus_message_unref(device->pending);
385 device->pending = NULL;
391 static DBusMessage *set_property(DBusConnection *conn,
392 DBusMessage *msg, void *data)
394 struct connman_device *device = data;
395 DBusMessageIter iter, value;
399 DBG("conn %p", conn);
401 if (dbus_message_iter_init(msg, &iter) == FALSE)
402 return __connman_error_invalid_arguments(msg);
404 dbus_message_iter_get_basic(&iter, &name);
405 dbus_message_iter_next(&iter);
406 dbus_message_iter_recurse(&iter, &value);
408 if (__connman_security_check_privilege(msg,
409 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
410 return __connman_error_permission_denied(msg);
412 type = dbus_message_iter_get_arg_type(&value);
414 if (g_str_equal(name, "Powered") == TRUE) {
415 connman_bool_t powered;
418 if (type != DBUS_TYPE_BOOLEAN)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &powered);
423 device->powered_persistent = powered;
425 __connman_storage_save_device(device);
427 if (device->powered == powered)
428 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
430 if (device->pending != NULL)
431 return __connman_error_in_progress(msg);
433 err = set_powered(device, powered);
435 if (err != -EINPROGRESS)
436 return __connman_error_failed(msg, -err);
438 device->pending = dbus_message_ref(msg);
440 device->timeout = g_timeout_add_seconds(15,
441 powered_timeout, device);
445 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
446 connman_uint16_t interval;
448 switch (device->mode) {
449 case CONNMAN_DEVICE_MODE_UNKNOWN:
450 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
451 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
455 if (type != DBUS_TYPE_UINT16)
456 return __connman_error_invalid_arguments(msg);
458 dbus_message_iter_get_basic(&value, &interval);
460 if (device->scan_interval != interval) {
461 device->scan_interval = interval;
463 __connman_storage_save_device(device);
465 reset_scan_trigger(device);
468 return __connman_error_invalid_property(msg);
470 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
473 static DBusMessage *propose_scan(DBusConnection *conn,
474 DBusMessage *msg, void *data)
476 struct connman_device *device = data;
479 DBG("conn %p", conn);
481 switch (device->mode) {
482 case CONNMAN_DEVICE_MODE_UNKNOWN:
483 return __connman_error_not_supported(msg);
484 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
485 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
489 err = __connman_device_scan(device);
491 return __connman_error_failed(msg, -err);
493 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 static GDBusMethodTable device_methods[] = {
497 { "GetProperties", "", "a{sv}", get_properties },
498 { "SetProperty", "sv", "", set_property,
499 G_DBUS_METHOD_FLAG_ASYNC },
500 { "ProposeScan", "", "", propose_scan },
504 static GDBusSignalTable device_signals[] = {
505 { "PropertyChanged", "sv" },
509 static int register_interface(struct connman_element *element)
511 struct connman_device *device = element->device;
513 DBG("element %p name %s", element, element->name);
515 if (g_dbus_register_interface(connection, element->path,
516 CONNMAN_DEVICE_INTERFACE,
517 device_methods, device_signals,
518 NULL, device, NULL) == FALSE) {
519 connman_error("Failed to register %s device", element->path);
523 device->registered = TRUE;
528 static void unregister_interface(struct connman_element *element)
530 struct connman_device *device = element->device;
532 DBG("element %p name %s", element, element->name);
534 device->registered = FALSE;
536 g_dbus_unregister_interface(connection, element->path,
537 CONNMAN_DEVICE_INTERFACE);
540 static int setup_device(struct connman_device *device)
544 DBG("device %p", device);
546 err = register_interface(&device->element);
548 if (device->driver->remove)
549 device->driver->remove(device);
550 device->driver = NULL;
554 __connman_technology_add_device(device);
556 switch (device->mode) {
557 case CONNMAN_DEVICE_MODE_UNKNOWN:
558 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
559 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
563 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
566 if (device->offlinemode == FALSE &&
567 device->powered_persistent == TRUE)
568 __connman_device_enable(device);
573 static void probe_driver(struct connman_element *element, gpointer user_data)
575 struct connman_device_driver *driver = user_data;
577 DBG("element %p name %s", element, element->name);
579 if (element->device == NULL)
582 if (element->device->driver != NULL)
585 if (driver->type != element->device->type)
588 if (driver->probe(element->device) < 0)
591 element->device->driver = driver;
593 setup_device(element->device);
596 static void remove_device(struct connman_device *device)
600 DBG("device %p", device);
602 err = __connman_device_disable(device);
603 if (err < 0 && err == -EINPROGRESS)
604 __connman_technology_disable_device(device);
606 switch (device->mode) {
607 case CONNMAN_DEVICE_MODE_UNKNOWN:
608 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
609 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
613 __connman_technology_remove_device(device);
615 unregister_interface(&device->element);
617 if (device->driver->remove)
618 device->driver->remove(device);
620 device->driver = NULL;
623 static void remove_driver(struct connman_element *element, gpointer user_data)
625 struct connman_device_driver *driver = user_data;
627 DBG("element %p name %s", element, element->name);
629 if (element->device == NULL)
632 if (element->device->driver == driver)
633 remove_device(element->device);
636 connman_bool_t __connman_device_has_driver(struct connman_device *device)
638 if (device == NULL || device->driver == NULL)
641 return device->registered;
644 static GSList *driver_list = NULL;
646 static gint compare_priority(gconstpointer a, gconstpointer b)
648 const struct connman_device_driver *driver1 = a;
649 const struct connman_device_driver *driver2 = b;
651 return driver2->priority - driver1->priority;
655 * connman_device_driver_register:
656 * @driver: device driver definition
658 * Register a new device driver
660 * Returns: %0 on success
662 int connman_device_driver_register(struct connman_device_driver *driver)
664 DBG("driver %p name %s", driver, driver->name);
666 driver_list = g_slist_insert_sorted(driver_list, driver,
669 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
670 probe_driver, driver);
676 * connman_device_driver_unregister:
677 * @driver: device driver definition
679 * Remove a previously registered device driver
681 void connman_device_driver_unregister(struct connman_device_driver *driver)
683 DBG("driver %p name %s", driver, driver->name);
685 driver_list = g_slist_remove(driver_list, driver);
687 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
688 remove_driver, driver);
691 static void unregister_network(gpointer data)
693 struct connman_network *network = data;
695 DBG("network %p", network);
697 connman_element_unregister((struct connman_element *) network);
699 __connman_network_set_device(network, NULL);
701 connman_network_unref(network);
704 static void device_destruct(struct connman_element *element)
706 struct connman_device *device = element->device;
708 DBG("element %p name %s", element, element->name);
710 if (device->timeout > 0) {
711 g_source_remove(device->timeout);
715 clear_scan_trigger(device);
717 if (device->pending != NULL) {
718 dbus_message_unref(device->pending);
719 device->pending = NULL;
722 g_free(device->ident);
723 g_free(device->node);
724 g_free(device->name);
725 g_free(device->address);
726 g_free(device->control);
727 g_free(device->interface);
729 g_free(device->last_network);
731 g_hash_table_destroy(device->networks);
732 device->networks = NULL;
736 * connman_device_create:
737 * @node: device node name (for example an address)
740 * Allocate a new device of given #type and assign the #node name to it.
742 * Returns: a newly-allocated #connman_device structure
744 struct connman_device *connman_device_create(const char *node,
745 enum connman_device_type type)
747 struct connman_device *device;
750 DBG("node %s type %d", node, type);
752 device = g_try_new0(struct connman_device, 1);
756 DBG("device %p", device);
758 __connman_element_initialize(&device->element);
760 device->element.name = g_strdup(node);
761 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
763 device->element.device = device;
764 device->element.destruct = device_destruct;
766 str = type2string(type);
768 connman_element_set_string(&device->element,
769 CONNMAN_PROPERTY_ID_TYPE, str);
771 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
774 device->name = g_strdup(type2description(device->type));
775 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
777 device->powered_persistent = TRUE;
779 device->phyindex = -1;
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;
1329 return connman_element_get_string(&device->element, key);
1332 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1334 struct connman_device *device = element->device;
1335 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1336 connman_bool_t powered;
1338 DBG("element %p name %s", element, element->name);
1343 device->offlinemode = offlinemode;
1345 if (device->blocked == TRUE)
1348 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1350 if (device->powered == powered)
1353 if (device->powered_persistent == FALSE)
1356 set_powered(device, powered);
1359 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1361 DBG("offlinmode %d", offlinemode);
1363 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1364 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1366 __connman_notifier_offlinemode(offlinemode);
1371 void __connman_device_increase_connections(struct connman_device *device)
1373 device->connections++;
1376 void __connman_device_decrease_connections(struct connman_device *device)
1378 device->connections--;
1382 * connman_device_add_network:
1383 * @device: device structure
1384 * @network: network structure
1386 * Add new network to the device
1388 int connman_device_add_network(struct connman_device *device,
1389 struct connman_network *network)
1391 const char *identifier = connman_network_get_identifier(network);
1394 DBG("device %p network %p", device, network);
1396 if (identifier == NULL)
1399 switch (device->mode) {
1400 case CONNMAN_DEVICE_MODE_UNKNOWN:
1402 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1403 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1407 __connman_network_set_device(network, device);
1409 err = connman_element_register((struct connman_element *) network,
1412 __connman_network_set_device(network, NULL);
1416 g_hash_table_insert(device->networks, g_strdup(identifier),
1423 * connman_device_get_network:
1424 * @device: device structure
1425 * @identifier: network identifier
1427 * Get network for given identifier
1429 struct connman_network *connman_device_get_network(struct connman_device *device,
1430 const char *identifier)
1432 DBG("device %p identifier %s", device, identifier);
1434 return g_hash_table_lookup(device->networks, identifier);
1438 * connman_device_remove_network:
1439 * @device: device structure
1440 * @identifier: network identifier
1442 * Remove network for given identifier
1444 int connman_device_remove_network(struct connman_device *device,
1445 const char *identifier)
1447 DBG("device %p identifier %s", device, identifier);
1449 g_hash_table_remove(device->networks, identifier);
1454 void connman_device_remove_all_networks(struct connman_device *device)
1456 g_hash_table_remove_all(device->networks);
1459 void __connman_device_set_network(struct connman_device *device,
1460 struct connman_network *network)
1467 if (device->network == network)
1470 if (device->network != NULL)
1471 connman_network_unref(device->network);
1473 if (network != NULL) {
1474 name = connman_network_get_string(network,
1475 CONNMAN_PROPERTY_ID_NAME);
1476 g_free(device->last_network);
1477 device->last_network = g_strdup(name);
1479 device->network = connman_network_ref(network);
1481 g_free(device->last_network);
1482 device->last_network = NULL;
1484 device->network = NULL;
1488 void __connman_device_set_reconnect(struct connman_device *device,
1489 connman_bool_t reconnect)
1491 device->reconnect = reconnect;
1494 connman_bool_t __connman_device_get_reconnect(
1495 struct connman_device *device)
1497 return device->reconnect;
1501 * connman_device_register:
1502 * @device: device structure
1504 * Register device with the system
1506 int connman_device_register(struct connman_device *device)
1508 __connman_storage_load_device(device);
1510 device->offlinemode = __connman_profile_get_offlinemode();
1512 return connman_element_register(&device->element, NULL);
1516 * connman_device_unregister:
1517 * @device: device structure
1519 * Unregister device with the system
1521 void connman_device_unregister(struct connman_device *device)
1523 __connman_storage_save_device(device);
1525 connman_element_unregister(&device->element);
1529 * connman_device_get_data:
1530 * @device: device structure
1532 * Get private device data pointer
1534 void *connman_device_get_data(struct connman_device *device)
1536 return device->driver_data;
1540 * connman_device_set_data:
1541 * @device: device structure
1542 * @data: data pointer
1544 * Set private device data pointer
1546 void connman_device_set_data(struct connman_device *device, void *data)
1548 device->driver_data = data;
1551 static gboolean match_driver(struct connman_device *device,
1552 struct connman_device_driver *driver)
1554 if (device->type == driver->type ||
1555 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1561 static int device_probe(struct connman_element *element)
1563 struct connman_device *device = element->device;
1566 DBG("element %p name %s", element, element->name);
1571 if (device->driver != NULL)
1574 for (list = driver_list; list; list = list->next) {
1575 struct connman_device_driver *driver = list->data;
1577 if (match_driver(device, driver) == FALSE)
1580 DBG("driver %p name %s", driver, driver->name);
1582 if (driver->probe(device) == 0) {
1583 device->driver = driver;
1588 if (device->driver == NULL)
1591 return setup_device(device);
1594 static void device_remove(struct connman_element *element)
1596 struct connman_device *device = element->device;
1598 DBG("element %p name %s", element, element->name);
1603 if (device->driver == NULL)
1606 remove_device(device);
1609 static struct connman_driver device_driver = {
1611 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1612 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1613 .probe = device_probe,
1614 .remove = device_remove,
1617 static int device_load(struct connman_device *device)
1619 const char *ident = __connman_profile_active_ident();
1621 GError *error = NULL;
1623 connman_bool_t powered;
1626 DBG("device %p", device);
1628 keyfile = __connman_storage_open_profile(ident);
1629 if (keyfile == NULL)
1632 identifier = g_strdup_printf("device_%s", device->element.name);
1633 if (identifier == NULL)
1636 powered = g_key_file_get_boolean(keyfile, identifier,
1639 device->powered_persistent = powered;
1640 g_clear_error(&error);
1642 switch (device->mode) {
1643 case CONNMAN_DEVICE_MODE_UNKNOWN:
1645 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1646 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1647 val = g_key_file_get_integer(keyfile, identifier,
1648 "ScanInterval", &error);
1649 if (error == NULL && val > 0)
1650 device->scan_interval = val;
1651 g_clear_error(&error);
1658 __connman_storage_close_profile(ident, keyfile, FALSE);
1663 static int device_save(struct connman_device *device)
1665 const char *ident = __connman_profile_active_ident();
1669 DBG("device %p", device);
1671 keyfile = __connman_storage_open_profile(ident);
1672 if (keyfile == NULL)
1675 identifier = g_strdup_printf("device_%s", device->element.name);
1676 if (identifier == NULL)
1679 g_key_file_set_boolean(keyfile, identifier,
1680 "Powered", device->powered_persistent);
1682 switch (device->mode) {
1683 case CONNMAN_DEVICE_MODE_UNKNOWN:
1685 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1686 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1687 if (device->scan_interval > 0)
1688 g_key_file_set_integer(keyfile, identifier,
1689 "ScanInterval", device->scan_interval);
1696 __connman_storage_close_profile(ident, keyfile, TRUE);
1701 static struct connman_storage device_storage = {
1703 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1704 .device_load = device_load,
1705 .device_save = device_save,
1708 int __connman_device_init(void)
1712 connection = connman_dbus_get_connection();
1714 if (connman_storage_register(&device_storage) < 0)
1715 connman_error("Failed to register device storage");
1717 return connman_driver_register(&device_driver);
1720 void __connman_device_cleanup(void)
1724 connman_driver_unregister(&device_driver);
1726 connman_storage_unregister(&device_storage);
1728 dbus_connection_unref(connection);