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 int set_powered(struct connman_device *device, connman_bool_t powered)
199 struct connman_device_driver *driver = device->driver;
202 DBG("device %p powered %d", device, powered);
204 if (device->powered_pending == powered)
210 if (powered == TRUE) {
211 if (driver->enable) {
212 device->powered_pending = powered;
214 err = driver->enable(device);
216 __connman_technology_enable_device(device);
220 device->powered_pending = powered;
222 device->reconnect = FALSE;
224 clear_scan_trigger(device);
226 g_hash_table_remove_all(device->networks);
228 if (driver->disable) {
229 err = driver->disable(device);
231 __connman_technology_disable_device(device);
237 device->powered = powered;
239 if (device->registered == TRUE)
240 powered_changed(device);
246 void __connman_device_list(DBusMessageIter *iter, void *user_data)
248 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
251 static void append_path(gpointer key, gpointer value, gpointer user_data)
253 struct connman_element *element = value;
254 DBusMessageIter *iter = user_data;
256 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
260 static void append_networks(DBusMessageIter *iter, void *user_data)
262 struct connman_device *device = user_data;
264 g_hash_table_foreach(device->networks, append_path, iter);
267 static DBusMessage *get_properties(DBusConnection *conn,
268 DBusMessage *msg, void *data)
270 struct connman_device *device = data;
272 DBusMessageIter array, dict;
275 DBG("conn %p", conn);
277 if (__connman_security_check_privilege(msg,
278 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
279 return __connman_error_permission_denied(msg);
281 reply = dbus_message_new_method_return(msg);
285 dbus_message_iter_init_append(reply, &array);
287 connman_dbus_dict_open(&array, &dict);
289 if (device->name != NULL)
290 connman_dbus_dict_append_basic(&dict, "Name",
291 DBUS_TYPE_STRING, &device->name);
293 str = type2string(device->type);
295 connman_dbus_dict_append_basic(&dict, "Type",
296 DBUS_TYPE_STRING, &str);
298 if (device->address != NULL)
299 connman_dbus_dict_append_basic(&dict, "Address",
300 DBUS_TYPE_STRING, &device->address);
302 if (device->interface != NULL)
303 connman_dbus_dict_append_basic(&dict, "Interface",
304 DBUS_TYPE_STRING, &device->interface);
306 connman_dbus_dict_append_basic(&dict, "Powered",
307 DBUS_TYPE_BOOLEAN, &device->powered);
309 if (device->driver && device->driver->scan)
310 connman_dbus_dict_append_basic(&dict, "Scanning",
311 DBUS_TYPE_BOOLEAN, &device->scanning);
313 switch (device->mode) {
314 case CONNMAN_DEVICE_MODE_UNKNOWN:
316 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
317 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
318 if (device->scan_interval > 0)
319 connman_dbus_dict_append_basic(&dict, "ScanInterval",
320 DBUS_TYPE_UINT16, &device->scan_interval);
322 connman_dbus_dict_append_array(&dict, "Networks",
323 DBUS_TYPE_OBJECT_PATH, append_networks, device);
327 connman_dbus_dict_close(&array, &dict);
332 static gboolean powered_timeout(gpointer user_data)
334 struct connman_device *device = user_data;
336 DBG("device %p", device);
340 if (device->pending != NULL) {
343 reply = __connman_error_operation_timeout(device->pending);
345 g_dbus_send_message(connection, reply);
347 dbus_message_unref(device->pending);
348 device->pending = NULL;
354 static DBusMessage *set_property(DBusConnection *conn,
355 DBusMessage *msg, void *data)
357 struct connman_device *device = data;
358 DBusMessageIter iter, value;
362 DBG("conn %p", conn);
364 if (dbus_message_iter_init(msg, &iter) == FALSE)
365 return __connman_error_invalid_arguments(msg);
367 dbus_message_iter_get_basic(&iter, &name);
368 dbus_message_iter_next(&iter);
369 dbus_message_iter_recurse(&iter, &value);
371 if (__connman_security_check_privilege(msg,
372 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
373 return __connman_error_permission_denied(msg);
375 type = dbus_message_iter_get_arg_type(&value);
377 if (g_str_equal(name, "Powered") == TRUE) {
378 connman_bool_t powered;
381 if (type != DBUS_TYPE_BOOLEAN)
382 return __connman_error_invalid_arguments(msg);
384 dbus_message_iter_get_basic(&value, &powered);
386 device->powered_persistent = powered;
388 __connman_storage_save_device(device);
390 if (device->powered == powered)
391 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
393 if (device->pending != NULL)
394 return __connman_error_in_progress(msg);
396 err = set_powered(device, powered);
398 if (err != -EINPROGRESS)
399 return __connman_error_failed(msg, -err);
401 device->pending = dbus_message_ref(msg);
403 device->timeout = g_timeout_add_seconds(15,
404 powered_timeout, device);
408 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
409 connman_uint16_t interval;
411 switch (device->mode) {
412 case CONNMAN_DEVICE_MODE_UNKNOWN:
413 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
414 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
418 if (type != DBUS_TYPE_UINT16)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &interval);
423 if (device->scan_interval != interval) {
424 device->scan_interval = interval;
426 __connman_storage_save_device(device);
428 reset_scan_trigger(device);
431 return __connman_error_invalid_property(msg);
433 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
436 static DBusMessage *propose_scan(DBusConnection *conn,
437 DBusMessage *msg, void *data)
439 struct connman_device *device = data;
442 DBG("conn %p", conn);
444 switch (device->mode) {
445 case CONNMAN_DEVICE_MODE_UNKNOWN:
446 return __connman_error_not_supported(msg);
447 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
448 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
452 err = __connman_device_scan(device);
454 return __connman_error_failed(msg, -err);
456 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
459 static GDBusMethodTable device_methods[] = {
460 { "GetProperties", "", "a{sv}", get_properties },
461 { "SetProperty", "sv", "", set_property,
462 G_DBUS_METHOD_FLAG_ASYNC },
463 { "ProposeScan", "", "", propose_scan },
467 static GDBusSignalTable device_signals[] = {
468 { "PropertyChanged", "sv" },
472 static int register_interface(struct connman_element *element)
474 struct connman_device *device = element->device;
476 DBG("element %p name %s", element, element->name);
478 if (g_dbus_register_interface(connection, element->path,
479 CONNMAN_DEVICE_INTERFACE,
480 device_methods, device_signals,
481 NULL, device, NULL) == FALSE) {
482 connman_error("Failed to register %s device", element->path);
486 device->registered = TRUE;
491 static void unregister_interface(struct connman_element *element)
493 struct connman_device *device = element->device;
495 DBG("element %p name %s", element, element->name);
497 device->registered = FALSE;
499 g_dbus_unregister_interface(connection, element->path,
500 CONNMAN_DEVICE_INTERFACE);
503 static int setup_device(struct connman_device *device)
507 DBG("device %p", device);
509 err = register_interface(&device->element);
511 if (device->driver->remove)
512 device->driver->remove(device);
513 device->driver = NULL;
517 __connman_technology_add_device(device);
519 switch (device->mode) {
520 case CONNMAN_DEVICE_MODE_UNKNOWN:
521 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
522 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
526 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
529 if (device->offlinemode == FALSE &&
530 device->powered_persistent == TRUE)
531 __connman_device_enable(device);
536 static void probe_driver(struct connman_element *element, gpointer user_data)
538 struct connman_device_driver *driver = user_data;
540 DBG("element %p name %s", element, element->name);
542 if (element->device == NULL)
545 if (element->device->driver != NULL)
548 if (driver->type != element->device->type)
551 if (driver->probe(element->device) < 0)
554 element->device->driver = driver;
556 setup_device(element->device);
559 static void remove_device(struct connman_device *device)
561 DBG("device %p", device);
563 __connman_device_disable(device);
565 switch (device->mode) {
566 case CONNMAN_DEVICE_MODE_UNKNOWN:
567 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
568 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
572 __connman_technology_remove_device(device);
574 unregister_interface(&device->element);
576 if (device->driver->remove)
577 device->driver->remove(device);
579 device->driver = NULL;
582 static void remove_driver(struct connman_element *element, gpointer user_data)
584 struct connman_device_driver *driver = user_data;
586 DBG("element %p name %s", element, element->name);
588 if (element->device == NULL)
591 if (element->device->driver == driver)
592 remove_device(element->device);
595 connman_bool_t __connman_device_has_driver(struct connman_device *device)
597 if (device == NULL || device->driver == NULL)
600 return device->registered;
603 static GSList *driver_list = NULL;
605 static gint compare_priority(gconstpointer a, gconstpointer b)
607 const struct connman_device_driver *driver1 = a;
608 const struct connman_device_driver *driver2 = b;
610 return driver2->priority - driver1->priority;
614 * connman_device_driver_register:
615 * @driver: device driver definition
617 * Register a new device driver
619 * Returns: %0 on success
621 int connman_device_driver_register(struct connman_device_driver *driver)
623 DBG("driver %p name %s", driver, driver->name);
625 driver_list = g_slist_insert_sorted(driver_list, driver,
628 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
629 probe_driver, driver);
635 * connman_device_driver_unregister:
636 * @driver: device driver definition
638 * Remove a previously registered device driver
640 void connman_device_driver_unregister(struct connman_device_driver *driver)
642 DBG("driver %p name %s", driver, driver->name);
644 driver_list = g_slist_remove(driver_list, driver);
646 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
647 remove_driver, driver);
650 static void unregister_network(gpointer data)
652 struct connman_network *network = data;
654 DBG("network %p", network);
656 connman_element_unregister((struct connman_element *) network);
658 connman_network_unref(network);
660 __connman_network_set_device(network, NULL);
663 static void device_destruct(struct connman_element *element)
665 struct connman_device *device = element->device;
667 DBG("element %p name %s", element, element->name);
669 if (device->timeout > 0) {
670 g_source_remove(device->timeout);
674 clear_scan_trigger(device);
676 if (device->pending != NULL) {
677 dbus_message_unref(device->pending);
678 device->pending = NULL;
681 g_free(device->ident);
682 g_free(device->node);
683 g_free(device->name);
684 g_free(device->address);
685 g_free(device->control);
686 g_free(device->interface);
688 g_free(device->last_network);
690 g_hash_table_destroy(device->networks);
691 device->networks = NULL;
695 * connman_device_create:
696 * @node: device node name (for example an address)
699 * Allocate a new device of given #type and assign the #node name to it.
701 * Returns: a newly-allocated #connman_device structure
703 struct connman_device *connman_device_create(const char *node,
704 enum connman_device_type type)
706 struct connman_device *device;
709 DBG("node %s type %d", node, type);
711 device = g_try_new0(struct connman_device, 1);
715 DBG("device %p", device);
717 __connman_element_initialize(&device->element);
719 device->element.name = g_strdup(node);
720 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
722 device->element.device = device;
723 device->element.destruct = device_destruct;
725 str = type2string(type);
727 connman_element_set_string(&device->element,
728 CONNMAN_PROPERTY_ID_TYPE, str);
730 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
733 device->name = g_strdup(type2description(device->type));
734 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
736 device->powered_persistent = TRUE;
738 device->phyindex = -1;
741 case CONNMAN_DEVICE_TYPE_UNKNOWN:
742 case CONNMAN_DEVICE_TYPE_VENDOR:
743 device->scan_interval = 0;
745 case CONNMAN_DEVICE_TYPE_ETHERNET:
746 case CONNMAN_DEVICE_TYPE_WIFI:
747 device->scan_interval = 300;
749 case CONNMAN_DEVICE_TYPE_WIMAX:
750 device->scan_interval = 0;
752 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
753 device->scan_interval = 0;
755 case CONNMAN_DEVICE_TYPE_GPS:
756 device->scan_interval = 0;
758 case CONNMAN_DEVICE_TYPE_CELLULAR:
759 device->scan_interval = 0;
763 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
764 g_free, unregister_network);
770 * connman_device_ref:
771 * @device: device structure
773 * Increase reference counter of device
775 struct connman_device *connman_device_ref(struct connman_device *device)
777 if (connman_element_ref(&device->element) == NULL)
784 * connman_device_unref:
785 * @device: device structure
787 * Decrease reference counter of device
789 void connman_device_unref(struct connman_device *device)
791 connman_element_unref(&device->element);
794 const char *__connman_device_get_type(struct connman_device *device)
796 return type2string(device->type);
800 * connman_device_get_type:
801 * @device: device structure
805 enum connman_device_type connman_device_get_type(struct connman_device *device)
811 * connman_device_get_name:
812 * @device: device structure
814 * Get unique name of device
816 const char *connman_device_get_name(struct connman_device *device)
818 return device->element.name;
822 * connman_device_get_path:
823 * @device: device structure
825 * Get path name of device
827 const char *connman_device_get_path(struct connman_device *device)
829 return device->element.path;
833 * connman_device_set_index:
834 * @device: device structure
835 * @index: index number
837 * Set index number of device
839 void connman_device_set_index(struct connman_device *device, int index)
841 device->element.index = index;
845 * connman_device_get_index:
846 * @device: device structure
848 * Get index number of device
850 int connman_device_get_index(struct connman_device *device)
852 return device->element.index;
855 int __connman_device_get_phyindex(struct connman_device *device)
857 return device->phyindex;
860 void __connman_device_set_phyindex(struct connman_device *device,
863 device->phyindex = phyindex;
867 * connman_device_set_interface:
868 * @device: device structure
869 * @interface: interface name
870 * @control: control interface
872 * Set interface name of device
874 void connman_device_set_interface(struct connman_device *device,
875 const char *interface, const char *control)
877 g_free(device->element.devname);
878 device->element.devname = g_strdup(interface);
880 g_free(device->interface);
881 device->interface = g_strdup(interface);
883 g_free(device->control);
884 device->control = g_strdup(control);
886 if (device->name == NULL) {
887 const char *str = type2description(device->type);
888 if (str != NULL && device->interface != NULL)
889 device->name = g_strdup_printf("%s (%s)", str,
894 const char *connman_device_get_control(struct connman_device *device)
896 return device->control;
900 * connman_device_set_ident:
901 * @device: device structure
902 * @ident: unique identifier
904 * Set unique identifier of device
906 void connman_device_set_ident(struct connman_device *device,
909 g_free(device->ident);
910 device->ident = g_strdup(ident);
913 const char *__connman_device_get_ident(struct connman_device *device)
915 return device->ident;
919 * connman_device_set_mode:
920 * @device: device structure
921 * @mode: network mode
923 * Change network mode of device
925 void connman_device_set_mode(struct connman_device *device,
926 enum connman_device_mode mode)
932 * connman_device_get_mode:
933 * @device: device structure
935 * Get network mode of device
937 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
943 * connman_device_set_powered:
944 * @device: device structure
945 * @powered: powered state
947 * Change power state of device
949 int connman_device_set_powered(struct connman_device *device,
950 connman_bool_t powered)
952 DBG("driver %p powered %d", device, powered);
954 if (device->timeout > 0) {
955 g_source_remove(device->timeout);
959 if (device->pending != NULL) {
960 g_dbus_send_reply(connection, device->pending,
963 dbus_message_unref(device->pending);
964 device->pending = NULL;
967 if (device->powered == powered)
970 device->powered = powered;
971 device->powered_pending = powered;
973 if (device->powered == TRUE)
974 __connman_technology_enable_device(device);
976 __connman_technology_disable_device(device);
978 if (device->registered == FALSE)
981 powered_changed(device);
983 if (powered == FALSE)
986 reset_scan_trigger(device);
988 if (device->driver->scan)
989 device->driver->scan(device);
994 int __connman_device_set_blocked(struct connman_device *device,
995 connman_bool_t blocked)
997 connman_bool_t powered;
999 DBG("device %p blocked %d", device, blocked);
1001 device->blocked = blocked;
1003 if (device->offlinemode == TRUE)
1006 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1008 if (blocked == FALSE)
1009 powered = device->powered_persistent;
1013 return set_powered(device, powered);
1016 int __connman_device_scan(struct connman_device *device)
1018 if (!device->driver || !device->driver->scan)
1021 if (device->powered == FALSE)
1024 reset_scan_trigger(device);
1026 return device->driver->scan(device);
1029 int __connman_device_enable(struct connman_device *device)
1033 DBG("device %p", device);
1035 if (!device->driver || !device->driver->enable)
1038 if (device->powered_pending == TRUE)
1041 device->powered_pending = TRUE;
1043 err = device->driver->enable(device);
1047 device->powered = TRUE;
1049 __connman_technology_enable_device(device);
1054 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 return __connman_device_enable(device);
1065 int __connman_device_disable(struct connman_device *device)
1069 DBG("device %p", device);
1071 if (!device->driver || !device->driver->disable)
1074 if (device->powered == FALSE)
1077 if (device->powered_pending == FALSE)
1080 device->powered_pending = FALSE;
1082 device->reconnect = FALSE;
1084 clear_scan_trigger(device);
1086 g_hash_table_remove_all(device->networks);
1088 err = device->driver->disable(device);
1092 device->powered = FALSE;
1094 __connman_technology_disable_device(device);
1099 int __connman_device_disable_persistent(struct connman_device *device)
1101 DBG("device %p", device);
1103 device->powered_persistent = FALSE;
1105 __connman_storage_save_device(device);
1107 return __connman_device_disable(device);
1110 int __connman_device_disconnect(struct connman_device *device)
1112 GHashTableIter iter;
1113 gpointer key, value;
1115 DBG("device %p", device);
1117 connman_device_set_disconnected(device, TRUE);
1119 g_hash_table_iter_init(&iter, device->networks);
1121 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1122 struct connman_network *network = value;
1124 if (__connman_network_get_connecting(network) == TRUE) {
1126 * Skip network in the process of connecting.
1127 * This is a workaround for WiFi networks serviced
1128 * by the supplicant plugin that hold a reference
1129 * to the network. If we disconnect the network
1130 * here then the referenced object will not be
1131 * registered and usage (like launching DHCP client)
1132 * will fail. There is nothing to be gained by
1133 * removing the network here anyway.
1135 connman_warn("Skipping disconnect of %s",
1136 connman_network_get_identifier(network));
1140 __connman_network_disconnect(network);
1146 static void mark_network_unavailable(gpointer key, gpointer value,
1149 struct connman_network *network = value;
1151 if (connman_network_get_connected(network) == TRUE)
1154 connman_network_set_available(network, FALSE);
1157 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1160 struct connman_network *network = value;
1162 if (connman_network_get_connected(network) == TRUE)
1165 if (connman_network_get_available(network) == TRUE)
1171 void __connman_device_cleanup_networks(struct connman_device *device)
1173 g_hash_table_foreach_remove(device->networks,
1174 remove_unavailable_network, NULL);
1177 static void scanning_changed(struct connman_device *device)
1179 connman_dbus_property_changed_basic(device->element.path,
1180 CONNMAN_DEVICE_INTERFACE, "Scanning",
1181 DBUS_TYPE_BOOLEAN, &device->scanning);
1184 static void mark_network_available(gpointer key, gpointer value,
1187 struct connman_network *network = value;
1189 connman_network_set_available(network, TRUE);
1192 void connman_device_cleanup_scanning(struct connman_device *device)
1194 device->scanning = FALSE;
1196 scanning_changed(device);
1198 g_hash_table_foreach(device->networks,
1199 mark_network_available, NULL);
1203 * connman_device_set_scanning:
1204 * @device: device structure
1205 * @scanning: scanning state
1207 * Change scanning state of device
1209 int connman_device_set_scanning(struct connman_device *device,
1210 connman_bool_t scanning)
1212 DBG("device %p scanning %d", device, scanning);
1214 if (!device->driver || !device->driver->scan)
1217 if (device->scanning == scanning)
1220 device->scanning = scanning;
1222 scanning_changed(device);
1224 if (scanning == TRUE) {
1225 reset_scan_trigger(device);
1227 g_hash_table_foreach(device->networks,
1228 mark_network_unavailable, NULL);
1233 __connman_device_cleanup_networks(device);
1235 if (device->connections > 0)
1238 if (device->disconnected == TRUE)
1241 __connman_service_auto_connect();
1247 * connman_device_set_disconnected:
1248 * @device: device structure
1249 * @disconnected: disconnected state
1251 * Change disconnected state of device (only for device with networks)
1253 int connman_device_set_disconnected(struct connman_device *device,
1254 connman_bool_t disconnected)
1256 DBG("device %p disconnected %d", device, disconnected);
1258 switch (device->mode) {
1259 case CONNMAN_DEVICE_MODE_UNKNOWN:
1261 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1262 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1266 if (device->disconnected == disconnected)
1269 device->disconnected = disconnected;
1271 if (disconnected == TRUE)
1272 force_scan_trigger(device);
1278 * connman_device_get_disconnected:
1279 * @device: device structure
1281 * Get device disconnected state
1283 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1285 return device->disconnected;
1289 * connman_device_set_string:
1290 * @device: device structure
1291 * @key: unique identifier
1292 * @value: string value
1294 * Set string value for specific key
1296 int connman_device_set_string(struct connman_device *device,
1297 const char *key, const char *value)
1299 DBG("device %p key %s value %s", device, key, value);
1301 if (g_str_equal(key, "Address") == TRUE) {
1302 g_free(device->address);
1303 device->address = g_strdup(value);
1304 } else if (g_str_equal(key, "Name") == TRUE) {
1305 g_free(device->name);
1306 device->name = g_strdup(value);
1307 } else if (g_str_equal(key, "Node") == TRUE) {
1308 g_free(device->node);
1309 device->node = g_strdup(value);
1312 return connman_element_set_string(&device->element, key, value);
1316 * connman_device_get_string:
1317 * @device: device structure
1318 * @key: unique identifier
1320 * Get string value for specific key
1322 const char *connman_device_get_string(struct connman_device *device,
1325 DBG("device %p key %s", device, key);
1327 if (g_str_equal(key, "Address") == TRUE)
1328 return device->address;
1329 else if (g_str_equal(key, "Name") == TRUE)
1330 return device->name;
1331 else if (g_str_equal(key, "Node") == TRUE)
1332 return device->node;
1334 return connman_element_get_string(&device->element, key);
1337 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1339 struct connman_device *device = element->device;
1340 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1341 connman_bool_t powered;
1343 DBG("element %p name %s", element, element->name);
1348 device->offlinemode = offlinemode;
1350 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1352 if (device->powered == powered)
1355 if (device->powered_persistent == FALSE)
1358 set_powered(device, powered);
1361 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1363 DBG("offlinmode %d", offlinemode);
1365 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1366 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1368 __connman_notifier_offlinemode(offlinemode);
1373 void __connman_device_increase_connections(struct connman_device *device)
1375 device->connections++;
1378 void __connman_device_decrease_connections(struct connman_device *device)
1380 device->connections--;
1384 * connman_device_add_network:
1385 * @device: device structure
1386 * @network: network structure
1388 * Add new network to the device
1390 int connman_device_add_network(struct connman_device *device,
1391 struct connman_network *network)
1393 const char *identifier = connman_network_get_identifier(network);
1396 DBG("device %p network %p", device, network);
1398 if (identifier == NULL)
1401 switch (device->mode) {
1402 case CONNMAN_DEVICE_MODE_UNKNOWN:
1404 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1405 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1409 __connman_network_set_device(network, device);
1411 err = connman_element_register((struct connman_element *) network,
1414 __connman_network_set_device(network, NULL);
1418 g_hash_table_insert(device->networks, g_strdup(identifier),
1425 * connman_device_get_network:
1426 * @device: device structure
1427 * @identifier: network identifier
1429 * Get network for given identifier
1431 struct connman_network *connman_device_get_network(struct connman_device *device,
1432 const char *identifier)
1434 DBG("device %p identifier %s", device, identifier);
1436 return g_hash_table_lookup(device->networks, identifier);
1440 * connman_device_remove_network:
1441 * @device: device structure
1442 * @identifier: network identifier
1444 * Remove network for given identifier
1446 int connman_device_remove_network(struct connman_device *device,
1447 const char *identifier)
1449 DBG("device %p identifier %s", device, identifier);
1451 g_hash_table_remove(device->networks, identifier);
1456 void connman_device_remove_all_networks(struct connman_device *device)
1458 g_hash_table_remove_all(device->networks);
1461 void __connman_device_set_network(struct connman_device *device,
1462 struct connman_network *network)
1466 if (device->network == network)
1469 if (device->network != NULL)
1470 connman_network_unref(device->network);
1472 if (network != NULL) {
1473 name = connman_network_get_string(network,
1474 CONNMAN_PROPERTY_ID_NAME);
1475 g_free(device->last_network);
1476 device->last_network = g_strdup(name);
1478 device->network = connman_network_ref(network);
1480 g_free(device->last_network);
1481 device->last_network = NULL;
1483 device->network = NULL;
1487 void __connman_device_set_reconnect(struct connman_device *device,
1488 connman_bool_t reconnect)
1490 device->reconnect = reconnect;
1493 connman_bool_t __connman_device_get_reconnect(
1494 struct connman_device *device)
1496 return device->reconnect;
1500 * connman_device_register:
1501 * @device: device structure
1503 * Register device with the system
1505 int connman_device_register(struct connman_device *device)
1507 __connman_storage_load_device(device);
1509 device->offlinemode = __connman_profile_get_offlinemode();
1511 return connman_element_register(&device->element, NULL);
1515 * connman_device_unregister:
1516 * @device: device structure
1518 * Unregister device with the system
1520 void connman_device_unregister(struct connman_device *device)
1522 __connman_storage_save_device(device);
1524 connman_element_unregister(&device->element);
1528 * connman_device_get_data:
1529 * @device: device structure
1531 * Get private device data pointer
1533 void *connman_device_get_data(struct connman_device *device)
1535 return device->driver_data;
1539 * connman_device_set_data:
1540 * @device: device structure
1541 * @data: data pointer
1543 * Set private device data pointer
1545 void connman_device_set_data(struct connman_device *device, void *data)
1547 device->driver_data = data;
1550 static gboolean match_driver(struct connman_device *device,
1551 struct connman_device_driver *driver)
1553 if (device->type == driver->type ||
1554 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1560 static int device_probe(struct connman_element *element)
1562 struct connman_device *device = element->device;
1565 DBG("element %p name %s", element, element->name);
1570 if (device->driver != NULL)
1573 for (list = driver_list; list; list = list->next) {
1574 struct connman_device_driver *driver = list->data;
1576 if (match_driver(device, driver) == FALSE)
1579 DBG("driver %p name %s", driver, driver->name);
1581 if (driver->probe(device) == 0) {
1582 device->driver = driver;
1587 if (device->driver == NULL)
1590 return setup_device(device);
1593 static void device_remove(struct connman_element *element)
1595 struct connman_device *device = element->device;
1597 DBG("element %p name %s", element, element->name);
1602 if (device->driver == NULL)
1605 remove_device(device);
1608 static struct connman_driver device_driver = {
1610 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1611 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1612 .probe = device_probe,
1613 .remove = device_remove,
1616 static int device_load(struct connman_device *device)
1618 const char *ident = __connman_profile_active_ident();
1620 GError *error = NULL;
1622 connman_bool_t powered;
1625 DBG("device %p", device);
1627 keyfile = __connman_storage_open_profile(ident);
1628 if (keyfile == NULL)
1631 identifier = g_strdup_printf("device_%s", device->element.name);
1632 if (identifier == NULL)
1635 powered = g_key_file_get_boolean(keyfile, identifier,
1638 device->powered_persistent = powered;
1639 g_clear_error(&error);
1641 switch (device->mode) {
1642 case CONNMAN_DEVICE_MODE_UNKNOWN:
1644 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1645 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1646 val = g_key_file_get_integer(keyfile, identifier,
1647 "ScanInterval", &error);
1648 if (error == NULL && val > 0)
1649 device->scan_interval = val;
1650 g_clear_error(&error);
1657 __connman_storage_close_profile(ident, keyfile, FALSE);
1662 static int device_save(struct connman_device *device)
1664 const char *ident = __connman_profile_active_ident();
1668 DBG("device %p", device);
1670 keyfile = __connman_storage_open_profile(ident);
1671 if (keyfile == NULL)
1674 identifier = g_strdup_printf("device_%s", device->element.name);
1675 if (identifier == NULL)
1678 g_key_file_set_boolean(keyfile, identifier,
1679 "Powered", device->powered_persistent);
1681 switch (device->mode) {
1682 case CONNMAN_DEVICE_MODE_UNKNOWN:
1684 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1685 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1686 if (device->scan_interval > 0)
1687 g_key_file_set_integer(keyfile, identifier,
1688 "ScanInterval", device->scan_interval);
1695 __connman_storage_close_profile(ident, keyfile, TRUE);
1700 static struct connman_storage device_storage = {
1702 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1703 .device_load = device_load,
1704 .device_save = device_save,
1707 int __connman_device_init(void)
1711 connection = connman_dbus_get_connection();
1713 if (connman_storage_register(&device_storage) < 0)
1714 connman_error("Failed to register device storage");
1716 return connman_driver_register(&device_driver);
1719 void __connman_device_cleanup(void)
1723 connman_driver_unregister(&device_driver);
1725 connman_storage_unregister(&device_storage);
1727 dbus_connection_unref(connection);