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 int __connman_device_enable(struct connman_device *device)
201 DBG("device %p", device);
203 if (!device->driver || !device->driver->enable)
206 if (device->powered_pending == TRUE)
209 device->powered_pending = TRUE;
211 err = device->driver->enable(device);
215 device->powered = TRUE;
217 __connman_technology_enable_device(device);
222 int __connman_device_disable(struct connman_device *device)
226 DBG("device %p", device);
228 if (!device->driver || !device->driver->disable)
231 if (device->powered == FALSE)
234 if (device->powered_pending == FALSE)
237 device->powered_pending = FALSE;
239 device->reconnect = FALSE;
241 clear_scan_trigger(device);
243 g_hash_table_remove_all(device->networks);
245 err = device->driver->disable(device);
249 device->powered = FALSE;
251 __connman_technology_disable_device(device);
256 static int set_powered(struct connman_device *device, connman_bool_t powered)
258 DBG("device %p powered %d", device, powered);
261 return __connman_device_enable(device);
263 return __connman_device_disable(device);
266 void __connman_device_list(DBusMessageIter *iter, void *user_data)
268 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
271 static void append_path(gpointer key, gpointer value, gpointer user_data)
273 struct connman_element *element = value;
274 DBusMessageIter *iter = user_data;
276 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
280 static void append_networks(DBusMessageIter *iter, void *user_data)
282 struct connman_device *device = user_data;
284 g_hash_table_foreach(device->networks, append_path, iter);
287 static DBusMessage *get_properties(DBusConnection *conn,
288 DBusMessage *msg, void *data)
290 struct connman_device *device = data;
292 DBusMessageIter array, dict;
295 DBG("conn %p", conn);
297 if (__connman_security_check_privilege(msg,
298 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
299 return __connman_error_permission_denied(msg);
301 reply = dbus_message_new_method_return(msg);
305 dbus_message_iter_init_append(reply, &array);
307 connman_dbus_dict_open(&array, &dict);
309 if (device->name != NULL)
310 connman_dbus_dict_append_basic(&dict, "Name",
311 DBUS_TYPE_STRING, &device->name);
313 str = type2string(device->type);
315 connman_dbus_dict_append_basic(&dict, "Type",
316 DBUS_TYPE_STRING, &str);
318 if (device->address != NULL)
319 connman_dbus_dict_append_basic(&dict, "Address",
320 DBUS_TYPE_STRING, &device->address);
322 if (device->interface != NULL)
323 connman_dbus_dict_append_basic(&dict, "Interface",
324 DBUS_TYPE_STRING, &device->interface);
326 connman_dbus_dict_append_basic(&dict, "Powered",
327 DBUS_TYPE_BOOLEAN, &device->powered);
329 if (device->driver && device->driver->scan)
330 connman_dbus_dict_append_basic(&dict, "Scanning",
331 DBUS_TYPE_BOOLEAN, &device->scanning);
333 switch (device->mode) {
334 case CONNMAN_DEVICE_MODE_UNKNOWN:
336 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
337 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
338 if (device->scan_interval > 0)
339 connman_dbus_dict_append_basic(&dict, "ScanInterval",
340 DBUS_TYPE_UINT16, &device->scan_interval);
342 connman_dbus_dict_append_array(&dict, "Networks",
343 DBUS_TYPE_OBJECT_PATH, append_networks, device);
347 connman_dbus_dict_close(&array, &dict);
352 static gboolean powered_timeout(gpointer user_data)
354 struct connman_device *device = user_data;
356 DBG("device %p", device);
360 if (device->pending != NULL) {
363 reply = __connman_error_operation_timeout(device->pending);
365 g_dbus_send_message(connection, reply);
367 dbus_message_unref(device->pending);
368 device->pending = NULL;
374 static DBusMessage *set_property(DBusConnection *conn,
375 DBusMessage *msg, void *data)
377 struct connman_device *device = data;
378 DBusMessageIter iter, value;
382 DBG("conn %p", conn);
384 if (dbus_message_iter_init(msg, &iter) == FALSE)
385 return __connman_error_invalid_arguments(msg);
387 dbus_message_iter_get_basic(&iter, &name);
388 dbus_message_iter_next(&iter);
389 dbus_message_iter_recurse(&iter, &value);
391 if (__connman_security_check_privilege(msg,
392 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
393 return __connman_error_permission_denied(msg);
395 type = dbus_message_iter_get_arg_type(&value);
397 if (g_str_equal(name, "Powered") == TRUE) {
398 connman_bool_t powered;
401 if (type != DBUS_TYPE_BOOLEAN)
402 return __connman_error_invalid_arguments(msg);
404 dbus_message_iter_get_basic(&value, &powered);
406 device->powered_persistent = powered;
408 __connman_storage_save_device(device);
410 if (device->powered == powered)
411 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
413 if (device->pending != NULL)
414 return __connman_error_in_progress(msg);
416 err = set_powered(device, powered);
418 if (err != -EINPROGRESS)
419 return __connman_error_failed(msg, -err);
421 device->pending = dbus_message_ref(msg);
423 device->timeout = g_timeout_add_seconds(15,
424 powered_timeout, device);
428 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
429 connman_uint16_t interval;
431 switch (device->mode) {
432 case CONNMAN_DEVICE_MODE_UNKNOWN:
433 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
434 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
438 if (type != DBUS_TYPE_UINT16)
439 return __connman_error_invalid_arguments(msg);
441 dbus_message_iter_get_basic(&value, &interval);
443 if (device->scan_interval != interval) {
444 device->scan_interval = interval;
446 __connman_storage_save_device(device);
448 reset_scan_trigger(device);
451 return __connman_error_invalid_property(msg);
453 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
456 static DBusMessage *propose_scan(DBusConnection *conn,
457 DBusMessage *msg, void *data)
459 struct connman_device *device = data;
462 DBG("conn %p", conn);
464 switch (device->mode) {
465 case CONNMAN_DEVICE_MODE_UNKNOWN:
466 return __connman_error_not_supported(msg);
467 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
468 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
472 err = __connman_device_scan(device);
474 return __connman_error_failed(msg, -err);
476 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
479 static GDBusMethodTable device_methods[] = {
480 { "GetProperties", "", "a{sv}", get_properties },
481 { "SetProperty", "sv", "", set_property,
482 G_DBUS_METHOD_FLAG_ASYNC },
483 { "ProposeScan", "", "", propose_scan },
487 static GDBusSignalTable device_signals[] = {
488 { "PropertyChanged", "sv" },
492 static int register_interface(struct connman_element *element)
494 struct connman_device *device = element->device;
496 DBG("element %p name %s", element, element->name);
498 if (g_dbus_register_interface(connection, element->path,
499 CONNMAN_DEVICE_INTERFACE,
500 device_methods, device_signals,
501 NULL, device, NULL) == FALSE) {
502 connman_error("Failed to register %s device", element->path);
506 device->registered = TRUE;
511 static void unregister_interface(struct connman_element *element)
513 struct connman_device *device = element->device;
515 DBG("element %p name %s", element, element->name);
517 device->registered = FALSE;
519 g_dbus_unregister_interface(connection, element->path,
520 CONNMAN_DEVICE_INTERFACE);
523 static int setup_device(struct connman_device *device)
527 DBG("device %p", device);
529 err = register_interface(&device->element);
531 if (device->driver->remove)
532 device->driver->remove(device);
533 device->driver = NULL;
537 __connman_technology_add_device(device);
539 switch (device->mode) {
540 case CONNMAN_DEVICE_MODE_UNKNOWN:
541 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
542 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
546 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
549 if (device->offlinemode == FALSE &&
550 device->powered_persistent == TRUE)
551 __connman_device_enable(device);
556 static void probe_driver(struct connman_element *element, gpointer user_data)
558 struct connman_device_driver *driver = user_data;
560 DBG("element %p name %s", element, element->name);
562 if (element->device == NULL)
565 if (element->device->driver != NULL)
568 if (driver->type != element->device->type)
571 if (driver->probe(element->device) < 0)
574 element->device->driver = driver;
576 setup_device(element->device);
579 static void remove_device(struct connman_device *device)
581 DBG("device %p", device);
583 __connman_device_disable(device);
585 switch (device->mode) {
586 case CONNMAN_DEVICE_MODE_UNKNOWN:
587 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
588 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
592 __connman_technology_remove_device(device);
594 unregister_interface(&device->element);
596 if (device->driver->remove)
597 device->driver->remove(device);
599 device->driver = NULL;
602 static void remove_driver(struct connman_element *element, gpointer user_data)
604 struct connman_device_driver *driver = user_data;
606 DBG("element %p name %s", element, element->name);
608 if (element->device == NULL)
611 if (element->device->driver == driver)
612 remove_device(element->device);
615 connman_bool_t __connman_device_has_driver(struct connman_device *device)
617 if (device == NULL || device->driver == NULL)
620 return device->registered;
623 static GSList *driver_list = NULL;
625 static gint compare_priority(gconstpointer a, gconstpointer b)
627 const struct connman_device_driver *driver1 = a;
628 const struct connman_device_driver *driver2 = b;
630 return driver2->priority - driver1->priority;
634 * connman_device_driver_register:
635 * @driver: device driver definition
637 * Register a new device driver
639 * Returns: %0 on success
641 int connman_device_driver_register(struct connman_device_driver *driver)
643 DBG("driver %p name %s", driver, driver->name);
645 driver_list = g_slist_insert_sorted(driver_list, driver,
648 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
649 probe_driver, driver);
655 * connman_device_driver_unregister:
656 * @driver: device driver definition
658 * Remove a previously registered device driver
660 void connman_device_driver_unregister(struct connman_device_driver *driver)
662 DBG("driver %p name %s", driver, driver->name);
664 driver_list = g_slist_remove(driver_list, driver);
666 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
667 remove_driver, driver);
670 static void unregister_network(gpointer data)
672 struct connman_network *network = data;
674 DBG("network %p", network);
676 connman_element_unregister((struct connman_element *) network);
678 connman_network_unref(network);
681 static void device_destruct(struct connman_element *element)
683 struct connman_device *device = element->device;
685 DBG("element %p name %s", element, element->name);
687 if (device->timeout > 0) {
688 g_source_remove(device->timeout);
692 clear_scan_trigger(device);
694 if (device->pending != NULL) {
695 dbus_message_unref(device->pending);
696 device->pending = NULL;
699 g_free(device->ident);
700 g_free(device->node);
701 g_free(device->name);
702 g_free(device->address);
703 g_free(device->control);
704 g_free(device->interface);
706 g_free(device->last_network);
708 g_hash_table_destroy(device->networks);
709 device->networks = NULL;
713 * connman_device_create:
714 * @node: device node name (for example an address)
717 * Allocate a new device of given #type and assign the #node name to it.
719 * Returns: a newly-allocated #connman_device structure
721 struct connman_device *connman_device_create(const char *node,
722 enum connman_device_type type)
724 struct connman_device *device;
727 DBG("node %s type %d", node, type);
729 device = g_try_new0(struct connman_device, 1);
733 DBG("device %p", device);
735 __connman_element_initialize(&device->element);
737 device->element.name = g_strdup(node);
738 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
740 device->element.device = device;
741 device->element.destruct = device_destruct;
743 str = type2string(type);
745 connman_element_set_string(&device->element,
746 CONNMAN_PROPERTY_ID_TYPE, str);
748 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
751 device->name = g_strdup(type2description(device->type));
752 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
754 device->powered_persistent = TRUE;
756 device->phyindex = -1;
759 case CONNMAN_DEVICE_TYPE_UNKNOWN:
760 case CONNMAN_DEVICE_TYPE_VENDOR:
761 device->scan_interval = 0;
763 case CONNMAN_DEVICE_TYPE_ETHERNET:
764 case CONNMAN_DEVICE_TYPE_WIFI:
765 device->scan_interval = 300;
767 case CONNMAN_DEVICE_TYPE_WIMAX:
768 device->scan_interval = 0;
770 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
771 device->scan_interval = 0;
773 case CONNMAN_DEVICE_TYPE_GPS:
774 device->scan_interval = 0;
776 case CONNMAN_DEVICE_TYPE_CELLULAR:
777 device->scan_interval = 0;
781 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
782 g_free, unregister_network);
788 * connman_device_ref:
789 * @device: device structure
791 * Increase reference counter of device
793 struct connman_device *connman_device_ref(struct connman_device *device)
795 if (connman_element_ref(&device->element) == NULL)
802 * connman_device_unref:
803 * @device: device structure
805 * Decrease reference counter of device
807 void connman_device_unref(struct connman_device *device)
809 connman_element_unref(&device->element);
812 const char *__connman_device_get_type(struct connman_device *device)
814 return type2string(device->type);
818 * connman_device_get_type:
819 * @device: device structure
823 enum connman_device_type connman_device_get_type(struct connman_device *device)
829 * connman_device_get_name:
830 * @device: device structure
832 * Get unique name of device
834 const char *connman_device_get_name(struct connman_device *device)
836 return device->element.name;
840 * connman_device_get_path:
841 * @device: device structure
843 * Get path name of device
845 const char *connman_device_get_path(struct connman_device *device)
847 return device->element.path;
851 * connman_device_set_index:
852 * @device: device structure
853 * @index: index number
855 * Set index number of device
857 void connman_device_set_index(struct connman_device *device, int index)
859 device->element.index = index;
863 * connman_device_get_index:
864 * @device: device structure
866 * Get index number of device
868 int connman_device_get_index(struct connman_device *device)
870 return device->element.index;
873 int __connman_device_get_phyindex(struct connman_device *device)
875 return device->phyindex;
878 void __connman_device_set_phyindex(struct connman_device *device,
881 device->phyindex = phyindex;
885 * connman_device_set_interface:
886 * @device: device structure
887 * @interface: interface name
888 * @control: control interface
890 * Set interface name of device
892 void connman_device_set_interface(struct connman_device *device,
893 const char *interface, const char *control)
895 g_free(device->element.devname);
896 device->element.devname = g_strdup(interface);
898 g_free(device->interface);
899 device->interface = g_strdup(interface);
901 g_free(device->control);
902 device->control = g_strdup(control);
904 if (device->name == NULL) {
905 const char *str = type2description(device->type);
906 if (str != NULL && device->interface != NULL)
907 device->name = g_strdup_printf("%s (%s)", str,
912 const char *connman_device_get_control(struct connman_device *device)
914 return device->control;
918 * connman_device_set_ident:
919 * @device: device structure
920 * @ident: unique identifier
922 * Set unique identifier of device
924 void connman_device_set_ident(struct connman_device *device,
927 g_free(device->ident);
928 device->ident = g_strdup(ident);
931 const char *__connman_device_get_ident(struct connman_device *device)
933 return device->ident;
937 * connman_device_set_mode:
938 * @device: device structure
939 * @mode: network mode
941 * Change network mode of device
943 void connman_device_set_mode(struct connman_device *device,
944 enum connman_device_mode mode)
950 * connman_device_get_mode:
951 * @device: device structure
953 * Get network mode of device
955 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
961 * connman_device_set_powered:
962 * @device: device structure
963 * @powered: powered state
965 * Change power state of device
967 int connman_device_set_powered(struct connman_device *device,
968 connman_bool_t powered)
970 DBG("driver %p powered %d", device, powered);
972 if (device->timeout > 0) {
973 g_source_remove(device->timeout);
977 if (device->pending != NULL) {
978 g_dbus_send_reply(connection, device->pending,
981 dbus_message_unref(device->pending);
982 device->pending = NULL;
985 if (device->powered == powered)
989 __connman_device_enable(device);
991 __connman_device_disable(device);
993 device->powered = powered;
994 device->powered_pending = powered;
996 if (device->powered == TRUE)
997 __connman_technology_enable_device(device);
999 __connman_technology_disable_device(device);
1001 if (device->registered == FALSE)
1004 powered_changed(device);
1006 if (powered == FALSE)
1009 reset_scan_trigger(device);
1011 if (device->driver->scan)
1012 device->driver->scan(device);
1017 int __connman_device_set_blocked(struct connman_device *device,
1018 connman_bool_t blocked)
1020 connman_bool_t powered;
1022 DBG("device %p blocked %d", device, blocked);
1024 device->blocked = blocked;
1026 if (device->offlinemode == TRUE)
1029 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1031 if (blocked == FALSE)
1032 powered = device->powered_persistent;
1036 return set_powered(device, powered);
1039 int __connman_device_scan(struct connman_device *device)
1041 if (!device->driver || !device->driver->scan)
1044 if (device->powered == FALSE)
1047 reset_scan_trigger(device);
1049 return device->driver->scan(device);
1052 int __connman_device_enable_persistent(struct connman_device *device)
1056 DBG("device %p", device);
1058 device->powered_persistent = TRUE;
1060 __connman_storage_save_device(device);
1062 err = __connman_device_enable(device);
1063 if (err == 0 || err == -EINPROGRESS) {
1064 if (__connman_profile_get_offlinemode() == TRUE)
1065 __connman_profile_set_offlinemode(FALSE, FALSE);
1072 int __connman_device_disable_persistent(struct connman_device *device)
1074 DBG("device %p", device);
1076 device->powered_persistent = FALSE;
1078 __connman_storage_save_device(device);
1080 return __connman_device_disable(device);
1083 int __connman_device_disconnect(struct connman_device *device)
1085 GHashTableIter iter;
1086 gpointer key, value;
1088 DBG("device %p", device);
1090 connman_device_set_disconnected(device, TRUE);
1092 g_hash_table_iter_init(&iter, device->networks);
1094 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1095 struct connman_network *network = value;
1097 if (__connman_network_get_connecting(network) == TRUE) {
1099 * Skip network in the process of connecting.
1100 * This is a workaround for WiFi networks serviced
1101 * by the supplicant plugin that hold a reference
1102 * to the network. If we disconnect the network
1103 * here then the referenced object will not be
1104 * registered and usage (like launching DHCP client)
1105 * will fail. There is nothing to be gained by
1106 * removing the network here anyway.
1108 connman_warn("Skipping disconnect of %s",
1109 connman_network_get_identifier(network));
1113 __connman_network_disconnect(network);
1119 static void mark_network_unavailable(gpointer key, gpointer value,
1122 struct connman_network *network = value;
1124 if (connman_network_get_connected(network) == TRUE)
1127 connman_network_set_available(network, FALSE);
1130 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1133 struct connman_network *network = value;
1135 if (connman_network_get_connected(network) == TRUE)
1138 if (connman_network_get_available(network) == TRUE)
1144 void __connman_device_cleanup_networks(struct connman_device *device)
1146 g_hash_table_foreach_remove(device->networks,
1147 remove_unavailable_network, NULL);
1150 static void scanning_changed(struct connman_device *device)
1152 connman_dbus_property_changed_basic(device->element.path,
1153 CONNMAN_DEVICE_INTERFACE, "Scanning",
1154 DBUS_TYPE_BOOLEAN, &device->scanning);
1158 * connman_device_set_scanning:
1159 * @device: device structure
1160 * @scanning: scanning state
1162 * Change scanning state of device
1164 int connman_device_set_scanning(struct connman_device *device,
1165 connman_bool_t scanning)
1167 DBG("device %p scanning %d", device, scanning);
1169 if (!device->driver || !device->driver->scan)
1172 if (device->scanning == scanning)
1175 device->scanning = scanning;
1177 scanning_changed(device);
1179 if (scanning == TRUE) {
1180 reset_scan_trigger(device);
1182 g_hash_table_foreach(device->networks,
1183 mark_network_unavailable, NULL);
1188 __connman_device_cleanup_networks(device);
1190 if (device->connections > 0)
1193 if (device->disconnected == TRUE)
1196 __connman_service_auto_connect();
1202 * connman_device_set_disconnected:
1203 * @device: device structure
1204 * @disconnected: disconnected state
1206 * Change disconnected state of device (only for device with networks)
1208 int connman_device_set_disconnected(struct connman_device *device,
1209 connman_bool_t disconnected)
1211 DBG("device %p disconnected %d", device, disconnected);
1213 switch (device->mode) {
1214 case CONNMAN_DEVICE_MODE_UNKNOWN:
1216 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1217 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1221 if (device->disconnected == disconnected)
1224 device->disconnected = disconnected;
1226 if (disconnected == TRUE)
1227 force_scan_trigger(device);
1233 * connman_device_get_disconnected:
1234 * @device: device structure
1236 * Get device disconnected state
1238 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1240 return device->disconnected;
1244 * connman_device_set_string:
1245 * @device: device structure
1246 * @key: unique identifier
1247 * @value: string value
1249 * Set string value for specific key
1251 int connman_device_set_string(struct connman_device *device,
1252 const char *key, const char *value)
1254 DBG("device %p key %s value %s", device, key, value);
1256 if (g_str_equal(key, "Address") == TRUE) {
1257 g_free(device->address);
1258 device->address = g_strdup(value);
1259 } else if (g_str_equal(key, "Name") == TRUE) {
1260 g_free(device->name);
1261 device->name = g_strdup(value);
1262 } else if (g_str_equal(key, "Node") == TRUE) {
1263 g_free(device->node);
1264 device->node = g_strdup(value);
1267 return connman_element_set_string(&device->element, key, value);
1271 * connman_device_get_string:
1272 * @device: device structure
1273 * @key: unique identifier
1275 * Get string value for specific key
1277 const char *connman_device_get_string(struct connman_device *device,
1280 DBG("device %p key %s", device, key);
1282 if (g_str_equal(key, "Address") == TRUE)
1283 return device->address;
1284 else if (g_str_equal(key, "Name") == TRUE)
1285 return device->name;
1286 else if (g_str_equal(key, "Node") == TRUE)
1287 return device->node;
1289 return connman_element_get_string(&device->element, key);
1292 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1294 struct connman_device *device = element->device;
1295 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1296 connman_bool_t powered;
1298 DBG("element %p name %s", element, element->name);
1303 device->offlinemode = offlinemode;
1305 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1307 if (device->powered == powered)
1310 if (device->powered_persistent == FALSE)
1313 set_powered(device, powered);
1316 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1318 DBG("offlinmode %d", offlinemode);
1320 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1321 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1323 __connman_notifier_offlinemode(offlinemode);
1328 void __connman_device_increase_connections(struct connman_device *device)
1330 device->connections++;
1333 void __connman_device_decrease_connections(struct connman_device *device)
1335 device->connections--;
1339 * connman_device_add_network:
1340 * @device: device structure
1341 * @network: network structure
1343 * Add new network to the device
1345 int connman_device_add_network(struct connman_device *device,
1346 struct connman_network *network)
1348 const char *identifier = connman_network_get_identifier(network);
1351 DBG("device %p network %p", device, network);
1353 if (identifier == NULL)
1356 switch (device->mode) {
1357 case CONNMAN_DEVICE_MODE_UNKNOWN:
1359 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1360 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1364 __connman_network_set_device(network, device);
1366 err = connman_element_register((struct connman_element *) network,
1369 __connman_network_set_device(network, NULL);
1373 g_hash_table_insert(device->networks, g_strdup(identifier),
1380 * connman_device_get_network:
1381 * @device: device structure
1382 * @identifier: network identifier
1384 * Get network for given identifier
1386 struct connman_network *connman_device_get_network(struct connman_device *device,
1387 const char *identifier)
1389 DBG("device %p identifier %s", device, identifier);
1391 return g_hash_table_lookup(device->networks, identifier);
1395 * connman_device_remove_network:
1396 * @device: device structure
1397 * @identifier: network identifier
1399 * Remove network for given identifier
1401 int connman_device_remove_network(struct connman_device *device,
1402 const char *identifier)
1404 DBG("device %p identifier %s", device, identifier);
1406 g_hash_table_remove(device->networks, identifier);
1411 void connman_device_remove_all_networks(struct connman_device *device)
1413 g_hash_table_remove_all(device->networks);
1416 void __connman_device_set_network(struct connman_device *device,
1417 struct connman_network *network)
1424 if (device->network == network)
1427 if (device->network != NULL)
1428 connman_network_unref(device->network);
1430 if (network != NULL) {
1431 name = connman_network_get_string(network,
1432 CONNMAN_PROPERTY_ID_NAME);
1433 g_free(device->last_network);
1434 device->last_network = g_strdup(name);
1436 device->network = connman_network_ref(network);
1438 g_free(device->last_network);
1439 device->last_network = NULL;
1441 device->network = NULL;
1445 void __connman_device_set_reconnect(struct connman_device *device,
1446 connman_bool_t reconnect)
1448 device->reconnect = reconnect;
1451 connman_bool_t __connman_device_get_reconnect(
1452 struct connman_device *device)
1454 return device->reconnect;
1458 * connman_device_register:
1459 * @device: device structure
1461 * Register device with the system
1463 int connman_device_register(struct connman_device *device)
1465 __connman_storage_load_device(device);
1467 device->offlinemode = __connman_profile_get_offlinemode();
1469 return connman_element_register(&device->element, NULL);
1473 * connman_device_unregister:
1474 * @device: device structure
1476 * Unregister device with the system
1478 void connman_device_unregister(struct connman_device *device)
1480 __connman_storage_save_device(device);
1482 connman_element_unregister(&device->element);
1486 * connman_device_get_data:
1487 * @device: device structure
1489 * Get private device data pointer
1491 void *connman_device_get_data(struct connman_device *device)
1493 return device->driver_data;
1497 * connman_device_set_data:
1498 * @device: device structure
1499 * @data: data pointer
1501 * Set private device data pointer
1503 void connman_device_set_data(struct connman_device *device, void *data)
1505 device->driver_data = data;
1508 static gboolean match_driver(struct connman_device *device,
1509 struct connman_device_driver *driver)
1511 if (device->type == driver->type ||
1512 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1518 static int device_probe(struct connman_element *element)
1520 struct connman_device *device = element->device;
1523 DBG("element %p name %s", element, element->name);
1528 if (device->driver != NULL)
1531 for (list = driver_list; list; list = list->next) {
1532 struct connman_device_driver *driver = list->data;
1534 if (match_driver(device, driver) == FALSE)
1537 DBG("driver %p name %s", driver, driver->name);
1539 if (driver->probe(device) == 0) {
1540 device->driver = driver;
1545 if (device->driver == NULL)
1548 return setup_device(device);
1551 static void device_remove(struct connman_element *element)
1553 struct connman_device *device = element->device;
1555 DBG("element %p name %s", element, element->name);
1560 if (device->driver == NULL)
1563 remove_device(device);
1566 static struct connman_driver device_driver = {
1568 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1569 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1570 .probe = device_probe,
1571 .remove = device_remove,
1574 static int device_load(struct connman_device *device)
1576 const char *ident = __connman_profile_active_ident();
1578 GError *error = NULL;
1580 connman_bool_t powered;
1583 DBG("device %p", device);
1585 keyfile = __connman_storage_open_profile(ident);
1586 if (keyfile == NULL)
1589 identifier = g_strdup_printf("device_%s", device->element.name);
1590 if (identifier == NULL)
1593 powered = g_key_file_get_boolean(keyfile, identifier,
1596 device->powered_persistent = powered;
1597 g_clear_error(&error);
1599 switch (device->mode) {
1600 case CONNMAN_DEVICE_MODE_UNKNOWN:
1602 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1603 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1604 val = g_key_file_get_integer(keyfile, identifier,
1605 "ScanInterval", &error);
1606 if (error == NULL && val > 0)
1607 device->scan_interval = val;
1608 g_clear_error(&error);
1615 __connman_storage_close_profile(ident, keyfile, FALSE);
1620 static int device_save(struct connman_device *device)
1622 const char *ident = __connman_profile_active_ident();
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 g_key_file_set_boolean(keyfile, identifier,
1637 "Powered", device->powered_persistent);
1639 switch (device->mode) {
1640 case CONNMAN_DEVICE_MODE_UNKNOWN:
1642 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1643 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1644 if (device->scan_interval > 0)
1645 g_key_file_set_integer(keyfile, identifier,
1646 "ScanInterval", device->scan_interval);
1653 __connman_storage_close_profile(ident, keyfile, TRUE);
1658 static struct connman_storage device_storage = {
1660 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1661 .device_load = device_load,
1662 .device_save = device_save,
1665 int __connman_device_init(void)
1669 connection = connman_dbus_get_connection();
1671 if (connman_storage_register(&device_storage) < 0)
1672 connman_error("Failed to register device storage");
1674 return connman_driver_register(&device_driver);
1677 void __connman_device_cleanup(void)
1681 connman_driver_unregister(&device_driver);
1683 connman_storage_unregister(&device_storage);
1685 dbus_connection_unref(connection);