5 * Copyright (C) 2007-2009 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;
206 enum connman_service_type type;
209 DBG("device %p powered %d", device, powered);
211 if (device->powered_pending == powered)
217 type = __connman_device_get_service_type(device);
219 if (powered == TRUE) {
220 if (driver->enable) {
221 device->powered_pending = powered;
223 err = driver->enable(device);
225 __connman_notifier_enable(type);
229 device->powered_pending = powered;
231 device->reconnect = FALSE;
233 clear_scan_trigger(device);
235 g_hash_table_remove_all(device->networks);
237 if (driver->disable) {
238 err = driver->disable(device);
240 __connman_notifier_disable(type);
246 device->powered = powered;
248 if (device->registered == TRUE)
249 powered_changed(device);
255 void __connman_device_list(DBusMessageIter *iter)
257 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
260 static void append_path(gpointer key, gpointer value, gpointer user_data)
262 struct connman_element *element = value;
263 DBusMessageIter *iter = user_data;
265 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
269 static void append_networks(struct connman_device *device,
270 DBusMessageIter *entry)
272 DBusMessageIter value, iter;
273 const char *key = "Networks";
275 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
277 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
278 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
281 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
282 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
283 g_hash_table_foreach(device->networks, append_path, &iter);
284 dbus_message_iter_close_container(&value, &iter);
286 dbus_message_iter_close_container(entry, &value);
289 static DBusMessage *get_properties(DBusConnection *conn,
290 DBusMessage *msg, void *data)
292 struct connman_device *device = data;
294 DBusMessageIter array, dict, entry;
297 DBG("conn %p", conn);
299 if (__connman_security_check_privilege(msg,
300 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
301 return __connman_error_permission_denied(msg);
303 reply = dbus_message_new_method_return(msg);
307 dbus_message_iter_init_append(reply, &array);
309 connman_dbus_dict_open(&array, &dict);
311 if (device->name != NULL)
312 connman_dbus_dict_append_variant(&dict, "Name",
313 DBUS_TYPE_STRING, &device->name);
315 str = type2string(device->type);
317 connman_dbus_dict_append_variant(&dict, "Type",
318 DBUS_TYPE_STRING, &str);
320 if (device->address != NULL)
321 connman_dbus_dict_append_variant(&dict, "Address",
322 DBUS_TYPE_STRING, &device->address);
324 if (device->interface != NULL)
325 connman_dbus_dict_append_variant(&dict, "Interface",
326 DBUS_TYPE_STRING, &device->interface);
328 connman_dbus_dict_append_variant(&dict, "Powered",
329 DBUS_TYPE_BOOLEAN, &device->powered);
331 if (device->driver && device->driver->scan)
332 connman_dbus_dict_append_variant(&dict, "Scanning",
333 DBUS_TYPE_BOOLEAN, &device->scanning);
335 switch (device->mode) {
336 case CONNMAN_DEVICE_MODE_UNKNOWN:
338 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
339 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
340 if (device->scan_interval > 0)
341 connman_dbus_dict_append_variant(&dict, "ScanInterval",
342 DBUS_TYPE_UINT16, &device->scan_interval);
344 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
346 append_networks(device, &entry);
347 dbus_message_iter_close_container(&dict, &entry);
351 connman_dbus_dict_close(&array, &dict);
356 static gboolean powered_timeout(gpointer user_data)
358 struct connman_device *device = user_data;
360 DBG("device %p", device);
364 if (device->pending != NULL) {
367 reply = __connman_error_operation_timeout(device->pending);
369 g_dbus_send_message(connection, reply);
371 dbus_message_unref(device->pending);
372 device->pending = NULL;
378 static DBusMessage *set_property(DBusConnection *conn,
379 DBusMessage *msg, void *data)
381 struct connman_device *device = data;
382 DBusMessageIter iter, value;
386 DBG("conn %p", conn);
388 if (dbus_message_iter_init(msg, &iter) == FALSE)
389 return __connman_error_invalid_arguments(msg);
391 dbus_message_iter_get_basic(&iter, &name);
392 dbus_message_iter_next(&iter);
393 dbus_message_iter_recurse(&iter, &value);
395 if (__connman_security_check_privilege(msg,
396 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
397 return __connman_error_permission_denied(msg);
399 type = dbus_message_iter_get_arg_type(&value);
401 if (g_str_equal(name, "Powered") == TRUE) {
402 connman_bool_t powered;
405 if (type != DBUS_TYPE_BOOLEAN)
406 return __connman_error_invalid_arguments(msg);
408 dbus_message_iter_get_basic(&value, &powered);
410 device->powered_persistent = powered;
412 __connman_storage_save_device(device);
414 if (device->powered == powered)
415 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
417 if (device->pending != NULL)
418 return __connman_error_in_progress(msg);
420 err = set_powered(device, powered);
422 if (err != -EINPROGRESS)
423 return __connman_error_failed(msg, -err);
425 device->pending = dbus_message_ref(msg);
427 device->timeout = g_timeout_add_seconds(15,
428 powered_timeout, device);
432 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
433 connman_uint16_t interval;
435 switch (device->mode) {
436 case CONNMAN_DEVICE_MODE_UNKNOWN:
437 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
438 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
442 if (type != DBUS_TYPE_UINT16)
443 return __connman_error_invalid_arguments(msg);
445 dbus_message_iter_get_basic(&value, &interval);
447 if (device->scan_interval != interval) {
448 device->scan_interval = interval;
450 __connman_storage_save_device(device);
452 reset_scan_trigger(device);
455 return __connman_error_invalid_property(msg);
457 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
460 static DBusMessage *propose_scan(DBusConnection *conn,
461 DBusMessage *msg, void *data)
463 struct connman_device *device = data;
466 DBG("conn %p", conn);
468 switch (device->mode) {
469 case CONNMAN_DEVICE_MODE_UNKNOWN:
470 return __connman_error_not_supported(msg);
471 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
472 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
476 err = __connman_device_scan(device);
478 return __connman_error_failed(msg, -err);
480 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
483 static GDBusMethodTable device_methods[] = {
484 { "GetProperties", "", "a{sv}", get_properties },
485 { "SetProperty", "sv", "", set_property,
486 G_DBUS_METHOD_FLAG_ASYNC },
487 { "ProposeScan", "", "", propose_scan },
491 static GDBusSignalTable device_signals[] = {
492 { "PropertyChanged", "sv" },
496 static void emit_devices_signal(void)
499 DBusMessageIter iter;
501 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
502 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
506 dbus_message_iter_init_append(signal, &iter);
507 connman_dbus_property_append_variable_array(&iter, "Devices",
508 DBUS_TYPE_OBJECT_PATH, __connman_device_list);
510 g_dbus_send_message(connection, signal);
513 static int register_interface(struct connman_element *element)
515 struct connman_device *device = element->device;
517 DBG("element %p name %s", element, element->name);
519 if (g_dbus_register_interface(connection, element->path,
520 CONNMAN_DEVICE_INTERFACE,
521 device_methods, device_signals,
522 NULL, device, NULL) == FALSE) {
523 connman_error("Failed to register %s device", element->path);
527 device->registered = TRUE;
529 emit_devices_signal();
534 static void unregister_interface(struct connman_element *element)
536 struct connman_device *device = element->device;
538 DBG("element %p name %s", element, element->name);
540 device->registered = FALSE;
542 emit_devices_signal();
544 g_dbus_unregister_interface(connection, element->path,
545 CONNMAN_DEVICE_INTERFACE);
548 static int setup_device(struct connman_device *device)
550 enum connman_service_type type;
553 DBG("device %p", device);
555 err = register_interface(&device->element);
557 if (device->driver->remove)
558 device->driver->remove(device);
559 device->driver = NULL;
563 type = __connman_device_get_service_type(device);
564 __connman_notifier_register(type);
566 switch (device->mode) {
567 case CONNMAN_DEVICE_MODE_UNKNOWN:
568 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
569 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
573 if (device->offlinemode == FALSE &&
574 device->powered_persistent == TRUE)
575 __connman_device_enable(device);
580 static void probe_driver(struct connman_element *element, gpointer user_data)
582 struct connman_device_driver *driver = user_data;
584 DBG("element %p name %s", element, element->name);
586 if (element->device == NULL)
589 if (element->device->driver != NULL)
592 if (driver->type != element->device->type)
595 if (driver->probe(element->device) < 0)
598 element->device->driver = driver;
600 setup_device(element->device);
603 static void remove_device(struct connman_device *device)
605 enum connman_service_type type;
607 DBG("device %p", device);
609 __connman_device_disable(device);
611 switch (device->mode) {
612 case CONNMAN_DEVICE_MODE_UNKNOWN:
613 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
614 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
618 type = __connman_device_get_service_type(device);
619 __connman_notifier_unregister(type);
621 unregister_interface(&device->element);
623 if (device->driver->remove)
624 device->driver->remove(device);
626 device->driver = NULL;
629 static void remove_driver(struct connman_element *element, gpointer user_data)
631 struct connman_device_driver *driver = user_data;
633 DBG("element %p name %s", element, element->name);
635 if (element->device == NULL)
638 if (element->device->driver == driver)
639 remove_device(element->device);
642 connman_bool_t __connman_device_has_driver(struct connman_device *device)
644 if (device == NULL || device->driver == NULL)
647 return device->registered;
650 static GSList *driver_list = NULL;
652 static gint compare_priority(gconstpointer a, gconstpointer b)
654 const struct connman_device_driver *driver1 = a;
655 const struct connman_device_driver *driver2 = b;
657 return driver2->priority - driver1->priority;
661 * connman_device_driver_register:
662 * @driver: device driver definition
664 * Register a new device driver
666 * Returns: %0 on success
668 int connman_device_driver_register(struct connman_device_driver *driver)
670 DBG("driver %p name %s", driver, driver->name);
672 driver_list = g_slist_insert_sorted(driver_list, driver,
675 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
676 probe_driver, driver);
682 * connman_device_driver_unregister:
683 * @driver: device driver definition
685 * Remove a previously registered device driver
687 void connman_device_driver_unregister(struct connman_device_driver *driver)
689 DBG("driver %p name %s", driver, driver->name);
691 driver_list = g_slist_remove(driver_list, driver);
693 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
694 remove_driver, driver);
697 static void unregister_network(gpointer data)
699 struct connman_network *network = data;
701 DBG("network %p", network);
703 connman_element_unregister((struct connman_element *) network);
705 connman_network_unref(network);
708 static void device_destruct(struct connman_element *element)
710 struct connman_device *device = element->device;
712 DBG("element %p name %s", element, element->name);
714 if (device->timeout > 0) {
715 g_source_remove(device->timeout);
719 clear_scan_trigger(device);
721 if (device->pending != NULL) {
722 dbus_message_unref(device->pending);
723 device->pending = NULL;
726 g_free(device->ident);
727 g_free(device->node);
728 g_free(device->name);
729 g_free(device->address);
730 g_free(device->control);
731 g_free(device->interface);
733 g_free(device->last_network);
735 g_hash_table_destroy(device->networks);
736 device->networks = NULL;
740 * connman_device_create:
741 * @node: device node name (for example an address)
744 * Allocate a new device of given #type and assign the #node name to it.
746 * Returns: a newly-allocated #connman_device structure
748 struct connman_device *connman_device_create(const char *node,
749 enum connman_device_type type)
751 struct connman_device *device;
754 DBG("node %s type %d", node, type);
756 device = g_try_new0(struct connman_device, 1);
760 DBG("device %p", device);
762 __connman_element_initialize(&device->element);
764 device->element.name = g_strdup(node);
765 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
767 device->element.device = device;
768 device->element.destruct = device_destruct;
770 str = type2string(type);
772 connman_element_set_string(&device->element,
773 CONNMAN_PROPERTY_ID_TYPE, str);
775 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
778 device->name = g_strdup(type2description(device->type));
779 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
781 device->powered_persistent = TRUE;
783 device->phyindex = -1;
786 case CONNMAN_DEVICE_TYPE_UNKNOWN:
787 case CONNMAN_DEVICE_TYPE_VENDOR:
788 device->scan_interval = 0;
790 case CONNMAN_DEVICE_TYPE_ETHERNET:
791 case CONNMAN_DEVICE_TYPE_WIFI:
792 device->scan_interval = 300;
794 case CONNMAN_DEVICE_TYPE_WIMAX:
795 device->scan_interval = 0;
797 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
798 device->scan_interval = 0;
800 case CONNMAN_DEVICE_TYPE_GPS:
801 device->scan_interval = 0;
803 case CONNMAN_DEVICE_TYPE_CELLULAR:
804 case CONNMAN_DEVICE_TYPE_MBM:
805 case CONNMAN_DEVICE_TYPE_HSO:
806 device->scan_interval = 0;
810 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
811 g_free, unregister_network);
817 * connman_device_ref:
818 * @device: device structure
820 * Increase reference counter of device
822 struct connman_device *connman_device_ref(struct connman_device *device)
824 if (connman_element_ref(&device->element) == NULL)
831 * connman_device_unref:
832 * @device: device structure
834 * Decrease reference counter of device
836 void connman_device_unref(struct connman_device *device)
838 connman_element_unref(&device->element);
841 const char *__connman_device_get_type(struct connman_device *device)
843 return type2string(device->type);
847 * connman_device_get_type:
848 * @device: device structure
852 enum connman_device_type connman_device_get_type(struct connman_device *device)
858 * connman_device_get_name:
859 * @device: device structure
861 * Get unique name of device
863 const char *connman_device_get_name(struct connman_device *device)
865 return device->element.name;
869 * connman_device_get_path:
870 * @device: device structure
872 * Get path name of device
874 const char *connman_device_get_path(struct connman_device *device)
876 return device->element.path;
880 * connman_device_set_index:
881 * @device: device structure
882 * @index: index number
884 * Set index number of device
886 void connman_device_set_index(struct connman_device *device, int index)
888 device->element.index = index;
892 * connman_device_get_index:
893 * @device: device structure
895 * Get index number of device
897 int connman_device_get_index(struct connman_device *device)
899 return device->element.index;
902 int __connman_device_get_phyindex(struct connman_device *device)
904 return device->phyindex;
907 void __connman_device_set_phyindex(struct connman_device *device,
910 device->phyindex = phyindex;
914 * connman_device_set_interface:
915 * @device: device structure
916 * @interface: interface name
917 * @control: control interface
919 * Set interface name of device
921 void connman_device_set_interface(struct connman_device *device,
922 const char *interface, const char *control)
924 g_free(device->element.devname);
925 device->element.devname = g_strdup(interface);
927 g_free(device->interface);
928 device->interface = g_strdup(interface);
930 g_free(device->control);
931 device->control = g_strdup(control);
933 if (device->name == NULL) {
934 const char *str = type2description(device->type);
935 if (str != NULL && device->interface != NULL)
936 device->name = g_strdup_printf("%s (%s)", str,
941 const char *connman_device_get_control(struct connman_device *device)
943 return device->control;
947 * connman_device_set_ident:
948 * @device: device structure
949 * @ident: unique identifier
951 * Set unique identifier of device
953 void connman_device_set_ident(struct connman_device *device,
956 g_free(device->ident);
957 device->ident = g_strdup(ident);
960 const char *__connman_device_get_ident(struct connman_device *device)
962 return device->ident;
966 * connman_device_set_mode:
967 * @device: device structure
968 * @mode: network mode
970 * Change network mode of device
972 void connman_device_set_mode(struct connman_device *device,
973 enum connman_device_mode mode)
979 * connman_device_get_mode:
980 * @device: device structure
982 * Get network mode of device
984 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
990 * connman_device_set_powered:
991 * @device: device structure
992 * @powered: powered state
994 * Change power state of device
996 int connman_device_set_powered(struct connman_device *device,
997 connman_bool_t powered)
999 enum connman_service_type type;
1001 DBG("driver %p powered %d", device, powered);
1003 if (device->timeout > 0) {
1004 g_source_remove(device->timeout);
1005 device->timeout = 0;
1008 if (device->pending != NULL) {
1009 g_dbus_send_reply(connection, device->pending,
1012 dbus_message_unref(device->pending);
1013 device->pending = NULL;
1016 if (device->powered == powered)
1019 device->powered = powered;
1020 device->powered_pending = powered;
1022 type = __connman_device_get_service_type(device);
1024 if (device->powered == TRUE)
1025 __connman_notifier_enable(type);
1027 __connman_notifier_disable(type);
1029 if (device->registered == FALSE)
1032 powered_changed(device);
1034 if (powered == FALSE)
1037 reset_scan_trigger(device);
1039 if (device->driver->scan)
1040 device->driver->scan(device);
1045 int __connman_device_set_blocked(struct connman_device *device,
1046 connman_bool_t blocked)
1048 connman_bool_t powered;
1050 DBG("device %p blocked %d", device, blocked);
1052 device->blocked = blocked;
1054 if (device->offlinemode == TRUE)
1057 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1059 if (blocked == FALSE)
1060 powered = device->powered_persistent;
1064 return set_powered(device, powered);
1067 int __connman_device_scan(struct connman_device *device)
1069 if (!device->driver || !device->driver->scan)
1072 if (device->powered == FALSE)
1075 reset_scan_trigger(device);
1077 return device->driver->scan(device);
1080 int __connman_device_enable(struct connman_device *device)
1082 enum connman_service_type type;
1085 DBG("device %p", device);
1087 if (!device->driver || !device->driver->enable)
1090 if (device->powered_pending == TRUE)
1093 device->powered_pending = TRUE;
1095 err = device->driver->enable(device);
1099 device->powered = TRUE;
1101 type = __connman_device_get_service_type(device);
1102 __connman_notifier_enable(type);
1107 int __connman_device_enable_persistent(struct connman_device *device)
1109 DBG("device %p", device);
1111 device->powered_persistent = TRUE;
1113 __connman_storage_save_device(device);
1115 return __connman_device_enable(device);
1118 int __connman_device_disable(struct connman_device *device)
1120 enum connman_service_type type;
1123 DBG("device %p", device);
1125 if (!device->driver || !device->driver->disable)
1128 if (device->powered == FALSE)
1131 if (device->powered_pending == FALSE)
1134 device->powered_pending = FALSE;
1136 device->reconnect = FALSE;
1138 clear_scan_trigger(device);
1140 g_hash_table_remove_all(device->networks);
1142 err = device->driver->disable(device);
1146 device->powered = FALSE;
1148 type = __connman_device_get_service_type(device);
1149 __connman_notifier_disable(type);
1154 int __connman_device_disable_persistent(struct connman_device *device)
1156 DBG("device %p", device);
1158 device->powered_persistent = FALSE;
1160 __connman_storage_save_device(device);
1162 return __connman_device_disable(device);
1165 int __connman_device_disconnect(struct connman_device *device)
1167 GHashTableIter iter;
1168 gpointer key, value;
1170 DBG("device %p", device);
1172 connman_device_set_disconnected(device, TRUE);
1174 g_hash_table_iter_init(&iter, device->networks);
1176 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1177 struct connman_network *network = value;
1179 if (__connman_network_get_connecting(network) == TRUE) {
1181 * Skip network in the process of connecting.
1182 * This is a workaround for WiFi networks serviced
1183 * by the supplicant plugin that hold a reference
1184 * to the network. If we disconnect the network
1185 * here then the referenced object will not be
1186 * registered and usage (like launching DHCP client)
1187 * will fail. There is nothing to be gained by
1188 * removing the network here anyway.
1190 connman_warn("Skipping disconnect of %s",
1191 connman_network_get_identifier(network));
1195 __connman_network_disconnect(network);
1201 static void mark_network_unavailable(gpointer key, gpointer value,
1204 struct connman_network *network = value;
1206 if (connman_network_get_connected(network) == TRUE)
1209 connman_network_set_available(network, FALSE);
1212 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1215 struct connman_network *network = value;
1217 if (connman_network_get_connected(network) == TRUE)
1220 if (connman_network_get_available(network) == TRUE)
1226 void __connman_device_cleanup_networks(struct connman_device *device)
1228 g_hash_table_foreach_remove(device->networks,
1229 remove_unavailable_network, NULL);
1232 static void scanning_changed(struct connman_device *device)
1234 connman_dbus_property_changed_basic(device->element.path,
1235 CONNMAN_DEVICE_INTERFACE, "Scanning",
1236 DBUS_TYPE_BOOLEAN, &device->scanning);
1239 static void mark_network_available(gpointer key, gpointer value,
1242 struct connman_network *network = value;
1244 connman_network_set_available(network, TRUE);
1247 void connman_device_cleanup_scanning(struct connman_device *device)
1249 device->scanning = FALSE;
1251 scanning_changed(device);
1253 g_hash_table_foreach(device->networks,
1254 mark_network_available, NULL);
1258 * connman_device_set_scanning:
1259 * @device: device structure
1260 * @scanning: scanning state
1262 * Change scanning state of device
1264 int connman_device_set_scanning(struct connman_device *device,
1265 connman_bool_t scanning)
1267 DBG("device %p scanning %d", device, scanning);
1269 if (!device->driver || !device->driver->scan)
1272 if (device->scanning == scanning)
1275 device->scanning = scanning;
1277 scanning_changed(device);
1279 if (scanning == TRUE) {
1280 reset_scan_trigger(device);
1282 g_hash_table_foreach(device->networks,
1283 mark_network_unavailable, NULL);
1288 __connman_device_cleanup_networks(device);
1290 if (device->connections > 0)
1293 if (device->disconnected == TRUE)
1296 __connman_service_auto_connect();
1302 * connman_device_set_disconnected:
1303 * @device: device structure
1304 * @disconnected: disconnected state
1306 * Change disconnected state of device (only for device with networks)
1308 int connman_device_set_disconnected(struct connman_device *device,
1309 connman_bool_t disconnected)
1311 DBG("device %p disconnected %d", device, disconnected);
1313 switch (device->mode) {
1314 case CONNMAN_DEVICE_MODE_UNKNOWN:
1316 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1317 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1321 if (device->disconnected == disconnected)
1324 device->disconnected = disconnected;
1326 if (disconnected == TRUE)
1327 force_scan_trigger(device);
1333 * connman_device_get_disconnected:
1334 * @device: device structure
1336 * Get device disconnected state
1338 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1340 return device->disconnected;
1344 * connman_device_set_string:
1345 * @device: device structure
1346 * @key: unique identifier
1347 * @value: string value
1349 * Set string value for specific key
1351 int connman_device_set_string(struct connman_device *device,
1352 const char *key, const char *value)
1354 DBG("device %p key %s value %s", device, key, value);
1356 if (g_str_equal(key, "Address") == TRUE) {
1357 g_free(device->address);
1358 device->address = g_strdup(value);
1359 } else if (g_str_equal(key, "Name") == TRUE) {
1360 g_free(device->name);
1361 device->name = g_strdup(value);
1362 } else if (g_str_equal(key, "Node") == TRUE) {
1363 g_free(device->node);
1364 device->node = g_strdup(value);
1367 return connman_element_set_string(&device->element, key, value);
1371 * connman_device_get_string:
1372 * @device: device structure
1373 * @key: unique identifier
1375 * Get string value for specific key
1377 const char *connman_device_get_string(struct connman_device *device,
1380 DBG("device %p key %s", device, key);
1382 if (g_str_equal(key, "Address") == TRUE)
1383 return device->address;
1384 else if (g_str_equal(key, "Name") == TRUE)
1385 return device->name;
1386 else if (g_str_equal(key, "Node") == TRUE)
1387 return device->node;
1389 return connman_element_get_string(&device->element, key);
1392 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1394 struct connman_device *device = element->device;
1395 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1396 connman_bool_t powered;
1398 DBG("element %p name %s", element, element->name);
1403 device->offlinemode = offlinemode;
1405 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1407 if (device->powered == powered)
1410 if (device->powered_persistent == FALSE)
1413 set_powered(device, powered);
1416 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1418 DBG("offlinmode %d", offlinemode);
1420 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1421 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1423 __connman_notifier_offlinemode(offlinemode);
1428 void __connman_device_increase_connections(struct connman_device *device)
1430 device->connections++;
1433 void __connman_device_decrease_connections(struct connman_device *device)
1435 device->connections--;
1439 * connman_device_add_network:
1440 * @device: device structure
1441 * @network: network structure
1443 * Add new network to the device
1445 int connman_device_add_network(struct connman_device *device,
1446 struct connman_network *network)
1448 const char *identifier = connman_network_get_identifier(network);
1451 DBG("device %p network %p", device, network);
1453 if (identifier == NULL)
1456 switch (device->mode) {
1457 case CONNMAN_DEVICE_MODE_UNKNOWN:
1459 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1460 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1464 __connman_network_set_device(network, device);
1466 err = connman_element_register((struct connman_element *) network,
1469 __connman_network_set_device(network, NULL);
1473 g_hash_table_insert(device->networks, g_strdup(identifier),
1480 * connman_device_get_network:
1481 * @device: device structure
1482 * @identifier: network identifier
1484 * Get network for given identifier
1486 struct connman_network *connman_device_get_network(struct connman_device *device,
1487 const char *identifier)
1489 DBG("device %p identifier %s", device, identifier);
1491 return g_hash_table_lookup(device->networks, identifier);
1495 * connman_device_remove_network:
1496 * @device: device structure
1497 * @identifier: network identifier
1499 * Remove network for given identifier
1501 int connman_device_remove_network(struct connman_device *device,
1502 const char *identifier)
1504 DBG("device %p identifier %s", device, identifier);
1506 g_hash_table_remove(device->networks, identifier);
1511 void connman_device_remove_all_networks(struct connman_device *device)
1513 g_hash_table_remove_all(device->networks);
1516 void __connman_device_set_network(struct connman_device *device,
1517 struct connman_network *network)
1521 if (device->network == network)
1524 if (device->network != NULL)
1525 connman_network_unref(device->network);
1527 if (network != NULL) {
1528 name = connman_network_get_string(network,
1529 CONNMAN_PROPERTY_ID_NAME);
1530 g_free(device->last_network);
1531 device->last_network = g_strdup(name);
1533 device->network = connman_network_ref(network);
1535 g_free(device->last_network);
1536 device->last_network = NULL;
1538 device->network = NULL;
1542 void __connman_device_set_reconnect(struct connman_device *device,
1543 connman_bool_t reconnect)
1545 device->reconnect = reconnect;
1548 connman_bool_t __connman_device_get_reconnect(
1549 struct connman_device *device)
1551 return device->reconnect;
1555 * connman_device_register:
1556 * @device: device structure
1558 * Register device with the system
1560 int connman_device_register(struct connman_device *device)
1562 __connman_storage_load_device(device);
1564 device->offlinemode = __connman_profile_get_offlinemode();
1566 return connman_element_register(&device->element, NULL);
1570 * connman_device_unregister:
1571 * @device: device structure
1573 * Unregister device with the system
1575 void connman_device_unregister(struct connman_device *device)
1577 __connman_storage_save_device(device);
1579 connman_element_unregister(&device->element);
1583 * connman_device_get_data:
1584 * @device: device structure
1586 * Get private device data pointer
1588 void *connman_device_get_data(struct connman_device *device)
1590 return device->driver_data;
1594 * connman_device_set_data:
1595 * @device: device structure
1596 * @data: data pointer
1598 * Set private device data pointer
1600 void connman_device_set_data(struct connman_device *device, void *data)
1602 device->driver_data = data;
1605 static gboolean match_driver(struct connman_device *device,
1606 struct connman_device_driver *driver)
1608 if (device->type == driver->type ||
1609 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1615 static int device_probe(struct connman_element *element)
1617 struct connman_device *device = element->device;
1620 DBG("element %p name %s", element, element->name);
1625 if (device->driver != NULL)
1628 for (list = driver_list; list; list = list->next) {
1629 struct connman_device_driver *driver = list->data;
1631 if (match_driver(device, driver) == FALSE)
1634 DBG("driver %p name %s", driver, driver->name);
1636 if (driver->probe(device) == 0) {
1637 device->driver = driver;
1642 if (device->driver == NULL)
1645 return setup_device(device);
1648 static void device_remove(struct connman_element *element)
1650 struct connman_device *device = element->device;
1652 DBG("element %p name %s", element, element->name);
1657 if (device->driver == NULL)
1660 remove_device(device);
1663 static struct connman_driver device_driver = {
1665 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1666 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1667 .probe = device_probe,
1668 .remove = device_remove,
1671 static int device_load(struct connman_device *device)
1673 const char *ident = __connman_profile_active_ident();
1675 GError *error = NULL;
1677 connman_bool_t powered;
1680 DBG("device %p", device);
1682 keyfile = __connman_storage_open_profile(ident);
1683 if (keyfile == NULL)
1686 identifier = g_strdup_printf("device_%s", device->element.name);
1687 if (identifier == NULL)
1690 powered = g_key_file_get_boolean(keyfile, identifier,
1693 device->powered_persistent = powered;
1694 g_clear_error(&error);
1696 switch (device->mode) {
1697 case CONNMAN_DEVICE_MODE_UNKNOWN:
1699 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1700 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1701 val = g_key_file_get_integer(keyfile, identifier,
1702 "ScanInterval", &error);
1703 if (error == NULL && val > 0)
1704 device->scan_interval = val;
1705 g_clear_error(&error);
1712 __connman_storage_close_profile(ident, keyfile, FALSE);
1717 static int device_save(struct connman_device *device)
1719 const char *ident = __connman_profile_active_ident();
1723 DBG("device %p", device);
1725 keyfile = __connman_storage_open_profile(ident);
1726 if (keyfile == NULL)
1729 identifier = g_strdup_printf("device_%s", device->element.name);
1730 if (identifier == NULL)
1733 g_key_file_set_boolean(keyfile, identifier,
1734 "Powered", device->powered_persistent);
1736 switch (device->mode) {
1737 case CONNMAN_DEVICE_MODE_UNKNOWN:
1739 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1740 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1741 if (device->scan_interval > 0)
1742 g_key_file_set_integer(keyfile, identifier,
1743 "ScanInterval", device->scan_interval);
1750 __connman_storage_close_profile(ident, keyfile, TRUE);
1755 static struct connman_storage device_storage = {
1757 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1758 .device_load = device_load,
1759 .device_save = device_save,
1762 int __connman_device_init(void)
1766 connection = connman_dbus_get_connection();
1768 if (connman_storage_register(&device_storage) < 0)
1769 connman_error("Failed to register device storage");
1771 return connman_driver_register(&device_driver);
1774 void __connman_device_cleanup(void)
1778 connman_driver_unregister(&device_driver);
1780 connman_storage_unregister(&device_storage);
1782 dbus_connection_unref(connection);