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:
137 case CONNMAN_DEVICE_TYPE_MBM:
138 case CONNMAN_DEVICE_TYPE_HSO:
145 static const char *type2string(enum connman_device_type type)
148 case CONNMAN_DEVICE_TYPE_UNKNOWN:
149 case CONNMAN_DEVICE_TYPE_VENDOR:
151 case CONNMAN_DEVICE_TYPE_ETHERNET:
153 case CONNMAN_DEVICE_TYPE_WIFI:
155 case CONNMAN_DEVICE_TYPE_WIMAX:
157 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
159 case CONNMAN_DEVICE_TYPE_GPS:
161 case CONNMAN_DEVICE_TYPE_CELLULAR:
162 case CONNMAN_DEVICE_TYPE_MBM:
163 case CONNMAN_DEVICE_TYPE_HSO:
170 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
172 enum connman_device_type type = connman_device_get_type(device);
175 case CONNMAN_DEVICE_TYPE_UNKNOWN:
176 case CONNMAN_DEVICE_TYPE_VENDOR:
177 case CONNMAN_DEVICE_TYPE_GPS:
179 case CONNMAN_DEVICE_TYPE_ETHERNET:
180 return CONNMAN_SERVICE_TYPE_ETHERNET;
181 case CONNMAN_DEVICE_TYPE_WIFI:
182 return CONNMAN_SERVICE_TYPE_WIFI;
183 case CONNMAN_DEVICE_TYPE_WIMAX:
184 return CONNMAN_SERVICE_TYPE_WIMAX;
185 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
186 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
187 case CONNMAN_DEVICE_TYPE_CELLULAR:
188 case CONNMAN_DEVICE_TYPE_MBM:
189 case CONNMAN_DEVICE_TYPE_HSO:
190 return CONNMAN_SERVICE_TYPE_CELLULAR;
193 return CONNMAN_SERVICE_TYPE_UNKNOWN;
196 static void powered_changed(struct connman_device *device)
198 connman_dbus_property_changed_basic(device->element.path,
199 CONNMAN_DEVICE_INTERFACE, "Powered",
200 DBUS_TYPE_BOOLEAN, &device->powered);
203 static int set_powered(struct connman_device *device, connman_bool_t powered)
205 struct connman_device_driver *driver = device->driver;
208 DBG("device %p powered %d", device, powered);
210 if (device->powered_pending == powered)
216 if (powered == TRUE) {
217 if (driver->enable) {
218 device->powered_pending = powered;
220 err = driver->enable(device);
222 __connman_technology_enable_device(device);
226 device->powered_pending = powered;
228 device->reconnect = FALSE;
230 clear_scan_trigger(device);
232 g_hash_table_remove_all(device->networks);
234 if (driver->disable) {
235 err = driver->disable(device);
237 __connman_technology_disable_device(device);
243 device->powered = powered;
245 if (device->registered == TRUE)
246 powered_changed(device);
252 void __connman_device_list(DBusMessageIter *iter, void *user_data)
254 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
257 static void append_path(gpointer key, gpointer value, gpointer user_data)
259 struct connman_element *element = value;
260 DBusMessageIter *iter = user_data;
262 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
266 static void append_networks(DBusMessageIter *iter, void *user_data)
268 struct connman_device *device = user_data;
270 g_hash_table_foreach(device->networks, append_path, iter);
273 static DBusMessage *get_properties(DBusConnection *conn,
274 DBusMessage *msg, void *data)
276 struct connman_device *device = data;
278 DBusMessageIter array, dict;
281 DBG("conn %p", conn);
283 if (__connman_security_check_privilege(msg,
284 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
285 return __connman_error_permission_denied(msg);
287 reply = dbus_message_new_method_return(msg);
291 dbus_message_iter_init_append(reply, &array);
293 connman_dbus_dict_open(&array, &dict);
295 if (device->name != NULL)
296 connman_dbus_dict_append_basic(&dict, "Name",
297 DBUS_TYPE_STRING, &device->name);
299 str = type2string(device->type);
301 connman_dbus_dict_append_basic(&dict, "Type",
302 DBUS_TYPE_STRING, &str);
304 if (device->address != NULL)
305 connman_dbus_dict_append_basic(&dict, "Address",
306 DBUS_TYPE_STRING, &device->address);
308 if (device->interface != NULL)
309 connman_dbus_dict_append_basic(&dict, "Interface",
310 DBUS_TYPE_STRING, &device->interface);
312 connman_dbus_dict_append_basic(&dict, "Powered",
313 DBUS_TYPE_BOOLEAN, &device->powered);
315 if (device->driver && device->driver->scan)
316 connman_dbus_dict_append_basic(&dict, "Scanning",
317 DBUS_TYPE_BOOLEAN, &device->scanning);
319 switch (device->mode) {
320 case CONNMAN_DEVICE_MODE_UNKNOWN:
322 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
323 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
324 if (device->scan_interval > 0)
325 connman_dbus_dict_append_basic(&dict, "ScanInterval",
326 DBUS_TYPE_UINT16, &device->scan_interval);
328 connman_dbus_dict_append_array(&dict, "Networks",
329 DBUS_TYPE_OBJECT_PATH, append_networks, device);
333 connman_dbus_dict_close(&array, &dict);
338 static gboolean powered_timeout(gpointer user_data)
340 struct connman_device *device = user_data;
342 DBG("device %p", device);
346 if (device->pending != NULL) {
349 reply = __connman_error_operation_timeout(device->pending);
351 g_dbus_send_message(connection, reply);
353 dbus_message_unref(device->pending);
354 device->pending = NULL;
360 static DBusMessage *set_property(DBusConnection *conn,
361 DBusMessage *msg, void *data)
363 struct connman_device *device = data;
364 DBusMessageIter iter, value;
368 DBG("conn %p", conn);
370 if (dbus_message_iter_init(msg, &iter) == FALSE)
371 return __connman_error_invalid_arguments(msg);
373 dbus_message_iter_get_basic(&iter, &name);
374 dbus_message_iter_next(&iter);
375 dbus_message_iter_recurse(&iter, &value);
377 if (__connman_security_check_privilege(msg,
378 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
379 return __connman_error_permission_denied(msg);
381 type = dbus_message_iter_get_arg_type(&value);
383 if (g_str_equal(name, "Powered") == TRUE) {
384 connman_bool_t powered;
387 if (type != DBUS_TYPE_BOOLEAN)
388 return __connman_error_invalid_arguments(msg);
390 dbus_message_iter_get_basic(&value, &powered);
392 device->powered_persistent = powered;
394 __connman_storage_save_device(device);
396 if (device->powered == powered)
397 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
399 if (device->pending != NULL)
400 return __connman_error_in_progress(msg);
402 err = set_powered(device, powered);
404 if (err != -EINPROGRESS)
405 return __connman_error_failed(msg, -err);
407 device->pending = dbus_message_ref(msg);
409 device->timeout = g_timeout_add_seconds(15,
410 powered_timeout, device);
414 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
415 connman_uint16_t interval;
417 switch (device->mode) {
418 case CONNMAN_DEVICE_MODE_UNKNOWN:
419 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
420 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
424 if (type != DBUS_TYPE_UINT16)
425 return __connman_error_invalid_arguments(msg);
427 dbus_message_iter_get_basic(&value, &interval);
429 if (device->scan_interval != interval) {
430 device->scan_interval = interval;
432 __connman_storage_save_device(device);
434 reset_scan_trigger(device);
437 return __connman_error_invalid_property(msg);
439 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
442 static DBusMessage *propose_scan(DBusConnection *conn,
443 DBusMessage *msg, void *data)
445 struct connman_device *device = data;
448 DBG("conn %p", conn);
450 switch (device->mode) {
451 case CONNMAN_DEVICE_MODE_UNKNOWN:
452 return __connman_error_not_supported(msg);
453 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
454 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
458 err = __connman_device_scan(device);
460 return __connman_error_failed(msg, -err);
462 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
465 static GDBusMethodTable device_methods[] = {
466 { "GetProperties", "", "a{sv}", get_properties },
467 { "SetProperty", "sv", "", set_property,
468 G_DBUS_METHOD_FLAG_ASYNC },
469 { "ProposeScan", "", "", propose_scan },
473 static GDBusSignalTable device_signals[] = {
474 { "PropertyChanged", "sv" },
478 static int register_interface(struct connman_element *element)
480 struct connman_device *device = element->device;
482 DBG("element %p name %s", element, element->name);
484 if (g_dbus_register_interface(connection, element->path,
485 CONNMAN_DEVICE_INTERFACE,
486 device_methods, device_signals,
487 NULL, device, NULL) == FALSE) {
488 connman_error("Failed to register %s device", element->path);
492 device->registered = TRUE;
497 static void unregister_interface(struct connman_element *element)
499 struct connman_device *device = element->device;
501 DBG("element %p name %s", element, element->name);
503 device->registered = FALSE;
505 g_dbus_unregister_interface(connection, element->path,
506 CONNMAN_DEVICE_INTERFACE);
509 static int setup_device(struct connman_device *device)
513 DBG("device %p", device);
515 err = register_interface(&device->element);
517 if (device->driver->remove)
518 device->driver->remove(device);
519 device->driver = NULL;
523 __connman_technology_add_device(device);
525 switch (device->mode) {
526 case CONNMAN_DEVICE_MODE_UNKNOWN:
527 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
528 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
532 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
535 if (device->offlinemode == FALSE &&
536 device->powered_persistent == TRUE)
537 __connman_device_enable(device);
542 static void probe_driver(struct connman_element *element, gpointer user_data)
544 struct connman_device_driver *driver = user_data;
546 DBG("element %p name %s", element, element->name);
548 if (element->device == NULL)
551 if (element->device->driver != NULL)
554 if (driver->type != element->device->type)
557 if (driver->probe(element->device) < 0)
560 element->device->driver = driver;
562 setup_device(element->device);
565 static void remove_device(struct connman_device *device)
567 DBG("device %p", device);
569 __connman_device_disable(device);
571 switch (device->mode) {
572 case CONNMAN_DEVICE_MODE_UNKNOWN:
573 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
574 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
578 __connman_technology_remove_device(device);
580 unregister_interface(&device->element);
582 if (device->driver->remove)
583 device->driver->remove(device);
585 device->driver = NULL;
588 static void remove_driver(struct connman_element *element, gpointer user_data)
590 struct connman_device_driver *driver = user_data;
592 DBG("element %p name %s", element, element->name);
594 if (element->device == NULL)
597 if (element->device->driver == driver)
598 remove_device(element->device);
601 connman_bool_t __connman_device_has_driver(struct connman_device *device)
603 if (device == NULL || device->driver == NULL)
606 return device->registered;
609 static GSList *driver_list = NULL;
611 static gint compare_priority(gconstpointer a, gconstpointer b)
613 const struct connman_device_driver *driver1 = a;
614 const struct connman_device_driver *driver2 = b;
616 return driver2->priority - driver1->priority;
620 * connman_device_driver_register:
621 * @driver: device driver definition
623 * Register a new device driver
625 * Returns: %0 on success
627 int connman_device_driver_register(struct connman_device_driver *driver)
629 DBG("driver %p name %s", driver, driver->name);
631 driver_list = g_slist_insert_sorted(driver_list, driver,
634 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
635 probe_driver, driver);
641 * connman_device_driver_unregister:
642 * @driver: device driver definition
644 * Remove a previously registered device driver
646 void connman_device_driver_unregister(struct connman_device_driver *driver)
648 DBG("driver %p name %s", driver, driver->name);
650 driver_list = g_slist_remove(driver_list, driver);
652 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
653 remove_driver, driver);
656 static void unregister_network(gpointer data)
658 struct connman_network *network = data;
660 DBG("network %p", network);
662 connman_element_unregister((struct connman_element *) network);
664 connman_network_unref(network);
667 static void device_destruct(struct connman_element *element)
669 struct connman_device *device = element->device;
671 DBG("element %p name %s", element, element->name);
673 if (device->timeout > 0) {
674 g_source_remove(device->timeout);
678 clear_scan_trigger(device);
680 if (device->pending != NULL) {
681 dbus_message_unref(device->pending);
682 device->pending = NULL;
685 g_free(device->ident);
686 g_free(device->node);
687 g_free(device->name);
688 g_free(device->address);
689 g_free(device->control);
690 g_free(device->interface);
692 g_free(device->last_network);
694 g_hash_table_destroy(device->networks);
695 device->networks = NULL;
699 * connman_device_create:
700 * @node: device node name (for example an address)
703 * Allocate a new device of given #type and assign the #node name to it.
705 * Returns: a newly-allocated #connman_device structure
707 struct connman_device *connman_device_create(const char *node,
708 enum connman_device_type type)
710 struct connman_device *device;
713 DBG("node %s type %d", node, type);
715 device = g_try_new0(struct connman_device, 1);
719 DBG("device %p", device);
721 __connman_element_initialize(&device->element);
723 device->element.name = g_strdup(node);
724 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
726 device->element.device = device;
727 device->element.destruct = device_destruct;
729 str = type2string(type);
731 connman_element_set_string(&device->element,
732 CONNMAN_PROPERTY_ID_TYPE, str);
734 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
737 device->name = g_strdup(type2description(device->type));
738 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
740 device->powered_persistent = TRUE;
742 device->phyindex = -1;
745 case CONNMAN_DEVICE_TYPE_UNKNOWN:
746 case CONNMAN_DEVICE_TYPE_VENDOR:
747 device->scan_interval = 0;
749 case CONNMAN_DEVICE_TYPE_ETHERNET:
750 case CONNMAN_DEVICE_TYPE_WIFI:
751 device->scan_interval = 300;
753 case CONNMAN_DEVICE_TYPE_WIMAX:
754 device->scan_interval = 0;
756 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
757 device->scan_interval = 0;
759 case CONNMAN_DEVICE_TYPE_GPS:
760 device->scan_interval = 0;
762 case CONNMAN_DEVICE_TYPE_CELLULAR:
763 case CONNMAN_DEVICE_TYPE_MBM:
764 case CONNMAN_DEVICE_TYPE_HSO:
765 device->scan_interval = 0;
769 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
770 g_free, unregister_network);
776 * connman_device_ref:
777 * @device: device structure
779 * Increase reference counter of device
781 struct connman_device *connman_device_ref(struct connman_device *device)
783 if (connman_element_ref(&device->element) == NULL)
790 * connman_device_unref:
791 * @device: device structure
793 * Decrease reference counter of device
795 void connman_device_unref(struct connman_device *device)
797 connman_element_unref(&device->element);
800 const char *__connman_device_get_type(struct connman_device *device)
802 return type2string(device->type);
806 * connman_device_get_type:
807 * @device: device structure
811 enum connman_device_type connman_device_get_type(struct connman_device *device)
817 * connman_device_get_name:
818 * @device: device structure
820 * Get unique name of device
822 const char *connman_device_get_name(struct connman_device *device)
824 return device->element.name;
828 * connman_device_get_path:
829 * @device: device structure
831 * Get path name of device
833 const char *connman_device_get_path(struct connman_device *device)
835 return device->element.path;
839 * connman_device_set_index:
840 * @device: device structure
841 * @index: index number
843 * Set index number of device
845 void connman_device_set_index(struct connman_device *device, int index)
847 device->element.index = index;
851 * connman_device_get_index:
852 * @device: device structure
854 * Get index number of device
856 int connman_device_get_index(struct connman_device *device)
858 return device->element.index;
861 int __connman_device_get_phyindex(struct connman_device *device)
863 return device->phyindex;
866 void __connman_device_set_phyindex(struct connman_device *device,
869 device->phyindex = phyindex;
873 * connman_device_set_interface:
874 * @device: device structure
875 * @interface: interface name
876 * @control: control interface
878 * Set interface name of device
880 void connman_device_set_interface(struct connman_device *device,
881 const char *interface, const char *control)
883 g_free(device->element.devname);
884 device->element.devname = g_strdup(interface);
886 g_free(device->interface);
887 device->interface = g_strdup(interface);
889 g_free(device->control);
890 device->control = g_strdup(control);
892 if (device->name == NULL) {
893 const char *str = type2description(device->type);
894 if (str != NULL && device->interface != NULL)
895 device->name = g_strdup_printf("%s (%s)", str,
900 const char *connman_device_get_control(struct connman_device *device)
902 return device->control;
906 * connman_device_set_ident:
907 * @device: device structure
908 * @ident: unique identifier
910 * Set unique identifier of device
912 void connman_device_set_ident(struct connman_device *device,
915 g_free(device->ident);
916 device->ident = g_strdup(ident);
919 const char *__connman_device_get_ident(struct connman_device *device)
921 return device->ident;
925 * connman_device_set_mode:
926 * @device: device structure
927 * @mode: network mode
929 * Change network mode of device
931 void connman_device_set_mode(struct connman_device *device,
932 enum connman_device_mode mode)
938 * connman_device_get_mode:
939 * @device: device structure
941 * Get network mode of device
943 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
949 * connman_device_set_powered:
950 * @device: device structure
951 * @powered: powered state
953 * Change power state of device
955 int connman_device_set_powered(struct connman_device *device,
956 connman_bool_t powered)
958 DBG("driver %p powered %d", device, powered);
960 if (device->timeout > 0) {
961 g_source_remove(device->timeout);
965 if (device->pending != NULL) {
966 g_dbus_send_reply(connection, device->pending,
969 dbus_message_unref(device->pending);
970 device->pending = NULL;
973 if (device->powered == powered)
976 device->powered = powered;
977 device->powered_pending = powered;
979 if (device->powered == TRUE)
980 __connman_technology_enable_device(device);
982 __connman_technology_disable_device(device);
984 if (device->registered == FALSE)
987 powered_changed(device);
989 if (powered == FALSE)
992 reset_scan_trigger(device);
994 if (device->driver->scan)
995 device->driver->scan(device);
1000 int __connman_device_set_blocked(struct connman_device *device,
1001 connman_bool_t blocked)
1003 connman_bool_t powered;
1005 DBG("device %p blocked %d", device, blocked);
1007 device->blocked = blocked;
1009 if (device->offlinemode == TRUE)
1012 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1014 if (blocked == FALSE)
1015 powered = device->powered_persistent;
1019 return set_powered(device, powered);
1022 int __connman_device_scan(struct connman_device *device)
1024 if (!device->driver || !device->driver->scan)
1027 if (device->powered == FALSE)
1030 reset_scan_trigger(device);
1032 return device->driver->scan(device);
1035 int __connman_device_enable(struct connman_device *device)
1039 DBG("device %p", device);
1041 if (!device->driver || !device->driver->enable)
1044 if (device->powered_pending == TRUE)
1047 device->powered_pending = TRUE;
1049 err = device->driver->enable(device);
1053 device->powered = TRUE;
1055 __connman_technology_enable_device(device);
1060 int __connman_device_enable_persistent(struct connman_device *device)
1062 DBG("device %p", device);
1064 device->powered_persistent = TRUE;
1066 __connman_storage_save_device(device);
1068 return __connman_device_enable(device);
1071 int __connman_device_disable(struct connman_device *device)
1075 DBG("device %p", device);
1077 if (!device->driver || !device->driver->disable)
1080 if (device->powered == FALSE)
1083 if (device->powered_pending == FALSE)
1086 device->powered_pending = FALSE;
1088 device->reconnect = FALSE;
1090 clear_scan_trigger(device);
1092 g_hash_table_remove_all(device->networks);
1094 err = device->driver->disable(device);
1098 device->powered = FALSE;
1100 __connman_technology_disable_device(device);
1105 int __connman_device_disable_persistent(struct connman_device *device)
1107 DBG("device %p", device);
1109 device->powered_persistent = FALSE;
1111 __connman_storage_save_device(device);
1113 return __connman_device_disable(device);
1116 int __connman_device_disconnect(struct connman_device *device)
1118 GHashTableIter iter;
1119 gpointer key, value;
1121 DBG("device %p", device);
1123 connman_device_set_disconnected(device, TRUE);
1125 g_hash_table_iter_init(&iter, device->networks);
1127 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1128 struct connman_network *network = value;
1130 if (__connman_network_get_connecting(network) == TRUE) {
1132 * Skip network in the process of connecting.
1133 * This is a workaround for WiFi networks serviced
1134 * by the supplicant plugin that hold a reference
1135 * to the network. If we disconnect the network
1136 * here then the referenced object will not be
1137 * registered and usage (like launching DHCP client)
1138 * will fail. There is nothing to be gained by
1139 * removing the network here anyway.
1141 connman_warn("Skipping disconnect of %s",
1142 connman_network_get_identifier(network));
1146 __connman_network_disconnect(network);
1152 static void mark_network_unavailable(gpointer key, gpointer value,
1155 struct connman_network *network = value;
1157 if (connman_network_get_connected(network) == TRUE)
1160 connman_network_set_available(network, FALSE);
1163 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1166 struct connman_network *network = value;
1168 if (connman_network_get_connected(network) == TRUE)
1171 if (connman_network_get_available(network) == TRUE)
1177 void __connman_device_cleanup_networks(struct connman_device *device)
1179 g_hash_table_foreach_remove(device->networks,
1180 remove_unavailable_network, NULL);
1183 static void scanning_changed(struct connman_device *device)
1185 connman_dbus_property_changed_basic(device->element.path,
1186 CONNMAN_DEVICE_INTERFACE, "Scanning",
1187 DBUS_TYPE_BOOLEAN, &device->scanning);
1190 static void mark_network_available(gpointer key, gpointer value,
1193 struct connman_network *network = value;
1195 connman_network_set_available(network, TRUE);
1198 void connman_device_cleanup_scanning(struct connman_device *device)
1200 device->scanning = FALSE;
1202 scanning_changed(device);
1204 g_hash_table_foreach(device->networks,
1205 mark_network_available, NULL);
1209 * connman_device_set_scanning:
1210 * @device: device structure
1211 * @scanning: scanning state
1213 * Change scanning state of device
1215 int connman_device_set_scanning(struct connman_device *device,
1216 connman_bool_t scanning)
1218 DBG("device %p scanning %d", device, scanning);
1220 if (!device->driver || !device->driver->scan)
1223 if (device->scanning == scanning)
1226 device->scanning = scanning;
1228 scanning_changed(device);
1230 if (scanning == TRUE) {
1231 reset_scan_trigger(device);
1233 g_hash_table_foreach(device->networks,
1234 mark_network_unavailable, NULL);
1239 __connman_device_cleanup_networks(device);
1241 if (device->connections > 0)
1244 if (device->disconnected == TRUE)
1247 __connman_service_auto_connect();
1253 * connman_device_set_disconnected:
1254 * @device: device structure
1255 * @disconnected: disconnected state
1257 * Change disconnected state of device (only for device with networks)
1259 int connman_device_set_disconnected(struct connman_device *device,
1260 connman_bool_t disconnected)
1262 DBG("device %p disconnected %d", device, disconnected);
1264 switch (device->mode) {
1265 case CONNMAN_DEVICE_MODE_UNKNOWN:
1267 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1268 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1272 if (device->disconnected == disconnected)
1275 device->disconnected = disconnected;
1277 if (disconnected == TRUE)
1278 force_scan_trigger(device);
1284 * connman_device_get_disconnected:
1285 * @device: device structure
1287 * Get device disconnected state
1289 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1291 return device->disconnected;
1295 * connman_device_set_string:
1296 * @device: device structure
1297 * @key: unique identifier
1298 * @value: string value
1300 * Set string value for specific key
1302 int connman_device_set_string(struct connman_device *device,
1303 const char *key, const char *value)
1305 DBG("device %p key %s value %s", device, key, value);
1307 if (g_str_equal(key, "Address") == TRUE) {
1308 g_free(device->address);
1309 device->address = g_strdup(value);
1310 } else if (g_str_equal(key, "Name") == TRUE) {
1311 g_free(device->name);
1312 device->name = g_strdup(value);
1313 } else if (g_str_equal(key, "Node") == TRUE) {
1314 g_free(device->node);
1315 device->node = g_strdup(value);
1318 return connman_element_set_string(&device->element, key, value);
1322 * connman_device_get_string:
1323 * @device: device structure
1324 * @key: unique identifier
1326 * Get string value for specific key
1328 const char *connman_device_get_string(struct connman_device *device,
1331 DBG("device %p key %s", device, key);
1333 if (g_str_equal(key, "Address") == TRUE)
1334 return device->address;
1335 else if (g_str_equal(key, "Name") == TRUE)
1336 return device->name;
1337 else if (g_str_equal(key, "Node") == TRUE)
1338 return device->node;
1340 return connman_element_get_string(&device->element, key);
1343 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1345 struct connman_device *device = element->device;
1346 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1347 connman_bool_t powered;
1349 DBG("element %p name %s", element, element->name);
1354 device->offlinemode = offlinemode;
1356 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1358 if (device->powered == powered)
1361 if (device->powered_persistent == FALSE)
1364 set_powered(device, powered);
1367 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1369 DBG("offlinmode %d", offlinemode);
1371 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1372 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1374 __connman_notifier_offlinemode(offlinemode);
1379 void __connman_device_increase_connections(struct connman_device *device)
1381 device->connections++;
1384 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)
1472 if (device->network == network)
1475 if (device->network != NULL)
1476 connman_network_unref(device->network);
1478 if (network != NULL) {
1479 name = connman_network_get_string(network,
1480 CONNMAN_PROPERTY_ID_NAME);
1481 g_free(device->last_network);
1482 device->last_network = g_strdup(name);
1484 device->network = connman_network_ref(network);
1486 g_free(device->last_network);
1487 device->last_network = NULL;
1489 device->network = NULL;
1493 void __connman_device_set_reconnect(struct connman_device *device,
1494 connman_bool_t reconnect)
1496 device->reconnect = reconnect;
1499 connman_bool_t __connman_device_get_reconnect(
1500 struct connman_device *device)
1502 return device->reconnect;
1506 * connman_device_register:
1507 * @device: device structure
1509 * Register device with the system
1511 int connman_device_register(struct connman_device *device)
1513 __connman_storage_load_device(device);
1515 device->offlinemode = __connman_profile_get_offlinemode();
1517 return connman_element_register(&device->element, NULL);
1521 * connman_device_unregister:
1522 * @device: device structure
1524 * Unregister device with the system
1526 void connman_device_unregister(struct connman_device *device)
1528 __connman_storage_save_device(device);
1530 connman_element_unregister(&device->element);
1534 * connman_device_get_data:
1535 * @device: device structure
1537 * Get private device data pointer
1539 void *connman_device_get_data(struct connman_device *device)
1541 return device->driver_data;
1545 * connman_device_set_data:
1546 * @device: device structure
1547 * @data: data pointer
1549 * Set private device data pointer
1551 void connman_device_set_data(struct connman_device *device, void *data)
1553 device->driver_data = data;
1556 static gboolean match_driver(struct connman_device *device,
1557 struct connman_device_driver *driver)
1559 if (device->type == driver->type ||
1560 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1566 static int device_probe(struct connman_element *element)
1568 struct connman_device *device = element->device;
1571 DBG("element %p name %s", element, element->name);
1576 if (device->driver != NULL)
1579 for (list = driver_list; list; list = list->next) {
1580 struct connman_device_driver *driver = list->data;
1582 if (match_driver(device, driver) == FALSE)
1585 DBG("driver %p name %s", driver, driver->name);
1587 if (driver->probe(device) == 0) {
1588 device->driver = driver;
1593 if (device->driver == NULL)
1596 return setup_device(device);
1599 static void device_remove(struct connman_element *element)
1601 struct connman_device *device = element->device;
1603 DBG("element %p name %s", element, element->name);
1608 if (device->driver == NULL)
1611 remove_device(device);
1614 static struct connman_driver device_driver = {
1616 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1617 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1618 .probe = device_probe,
1619 .remove = device_remove,
1622 static int device_load(struct connman_device *device)
1624 const char *ident = __connman_profile_active_ident();
1626 GError *error = NULL;
1628 connman_bool_t powered;
1631 DBG("device %p", device);
1633 keyfile = __connman_storage_open_profile(ident);
1634 if (keyfile == NULL)
1637 identifier = g_strdup_printf("device_%s", device->element.name);
1638 if (identifier == NULL)
1641 powered = g_key_file_get_boolean(keyfile, identifier,
1644 device->powered_persistent = powered;
1645 g_clear_error(&error);
1647 switch (device->mode) {
1648 case CONNMAN_DEVICE_MODE_UNKNOWN:
1650 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1651 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1652 val = g_key_file_get_integer(keyfile, identifier,
1653 "ScanInterval", &error);
1654 if (error == NULL && val > 0)
1655 device->scan_interval = val;
1656 g_clear_error(&error);
1663 __connman_storage_close_profile(ident, keyfile, FALSE);
1668 static int device_save(struct connman_device *device)
1670 const char *ident = __connman_profile_active_ident();
1674 DBG("device %p", device);
1676 keyfile = __connman_storage_open_profile(ident);
1677 if (keyfile == NULL)
1680 identifier = g_strdup_printf("device_%s", device->element.name);
1681 if (identifier == NULL)
1684 g_key_file_set_boolean(keyfile, identifier,
1685 "Powered", device->powered_persistent);
1687 switch (device->mode) {
1688 case CONNMAN_DEVICE_MODE_UNKNOWN:
1690 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1691 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1692 if (device->scan_interval > 0)
1693 g_key_file_set_integer(keyfile, identifier,
1694 "ScanInterval", device->scan_interval);
1701 __connman_storage_close_profile(ident, keyfile, TRUE);
1706 static struct connman_storage device_storage = {
1708 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1709 .device_load = device_load,
1710 .device_save = device_save,
1713 int __connman_device_init(void)
1717 connection = connman_dbus_get_connection();
1719 if (connman_storage_register(&device_storage) < 0)
1720 connman_error("Failed to register device storage");
1722 return connman_driver_register(&device_driver);
1725 void __connman_device_cleanup(void)
1729 connman_driver_unregister(&device_driver);
1731 connman_storage_unregister(&device_storage);
1733 dbus_connection_unref(connection);