5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
143 static const char *type2string(enum connman_device_type type)
146 case CONNMAN_DEVICE_TYPE_UNKNOWN:
147 case CONNMAN_DEVICE_TYPE_VENDOR:
149 case CONNMAN_DEVICE_TYPE_ETHERNET:
151 case CONNMAN_DEVICE_TYPE_WIFI:
153 case CONNMAN_DEVICE_TYPE_WIMAX:
155 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
157 case CONNMAN_DEVICE_TYPE_GPS:
159 case CONNMAN_DEVICE_TYPE_CELLULAR:
166 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
168 enum connman_device_type type = connman_device_get_type(device);
171 case CONNMAN_DEVICE_TYPE_UNKNOWN:
172 case CONNMAN_DEVICE_TYPE_VENDOR:
173 case CONNMAN_DEVICE_TYPE_GPS:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_CELLULAR:
184 return CONNMAN_SERVICE_TYPE_CELLULAR;
187 return CONNMAN_SERVICE_TYPE_UNKNOWN;
190 static void powered_changed(struct connman_device *device)
192 connman_dbus_property_changed_basic(device->element.path,
193 CONNMAN_DEVICE_INTERFACE, "Powered",
194 DBUS_TYPE_BOOLEAN, &device->powered);
197 static void blocked_changed(struct connman_device *device)
199 connman_dbus_property_changed_basic(device->element.path,
200 CONNMAN_DEVICE_INTERFACE, "Blocked",
201 DBUS_TYPE_BOOLEAN, &device->blocked);
204 int __connman_device_enable(struct connman_device *device)
208 DBG("device %p", device);
210 if (!device->driver || !device->driver->enable)
213 if (device->powered_pending == TRUE)
216 if (device->blocked == TRUE)
219 err = device->driver->enable(device);
221 if (err == -EINPROGRESS)
222 device->powered_pending = TRUE;
226 device->powered_pending = TRUE;
227 device->powered = TRUE;
229 __connman_technology_enable_device(device);
234 int __connman_device_disable(struct connman_device *device)
238 DBG("device %p", device);
240 if (!device->driver || !device->driver->disable)
243 if (device->powered == FALSE)
246 if (device->powered_pending == FALSE)
249 device->reconnect = FALSE;
251 clear_scan_trigger(device);
253 g_hash_table_remove_all(device->networks);
255 err = device->driver->disable(device);
257 if (err == -EINPROGRESS)
258 device->powered_pending = FALSE;
262 device->powered_pending = FALSE;
263 device->powered = FALSE;
265 __connman_technology_disable_device(device);
270 static int set_powered(struct connman_device *device, connman_bool_t powered)
272 DBG("device %p powered %d", device, powered);
275 return __connman_device_enable(device);
277 return __connman_device_disable(device);
280 void __connman_device_list(DBusMessageIter *iter, void *user_data)
282 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, iter);
285 static void append_path(gpointer key, gpointer value, gpointer user_data)
287 struct connman_element *element = value;
288 DBusMessageIter *iter = user_data;
290 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
294 static void append_networks(DBusMessageIter *iter, void *user_data)
296 struct connman_device *device = user_data;
298 g_hash_table_foreach(device->networks, append_path, iter);
301 static DBusMessage *get_properties(DBusConnection *conn,
302 DBusMessage *msg, void *data)
304 struct connman_device *device = data;
306 DBusMessageIter array, dict;
309 DBG("conn %p", conn);
311 if (__connman_security_check_privilege(msg,
312 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
313 return __connman_error_permission_denied(msg);
315 reply = dbus_message_new_method_return(msg);
319 dbus_message_iter_init_append(reply, &array);
321 connman_dbus_dict_open(&array, &dict);
323 if (device->name != NULL)
324 connman_dbus_dict_append_basic(&dict, "Name",
325 DBUS_TYPE_STRING, &device->name);
327 str = type2string(device->type);
329 connman_dbus_dict_append_basic(&dict, "Type",
330 DBUS_TYPE_STRING, &str);
332 if (device->address != NULL)
333 connman_dbus_dict_append_basic(&dict, "Address",
334 DBUS_TYPE_STRING, &device->address);
336 if (device->interface != NULL)
337 connman_dbus_dict_append_basic(&dict, "Interface",
338 DBUS_TYPE_STRING, &device->interface);
340 connman_dbus_dict_append_basic(&dict, "Powered",
341 DBUS_TYPE_BOOLEAN, &device->powered);
343 connman_dbus_dict_append_basic(&dict, "Blocked",
344 DBUS_TYPE_BOOLEAN, &device->blocked);
346 if (device->driver && device->driver->scan)
347 connman_dbus_dict_append_basic(&dict, "Scanning",
348 DBUS_TYPE_BOOLEAN, &device->scanning);
350 switch (device->mode) {
351 case CONNMAN_DEVICE_MODE_UNKNOWN:
353 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
354 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
355 if (device->scan_interval > 0)
356 connman_dbus_dict_append_basic(&dict, "ScanInterval",
357 DBUS_TYPE_UINT16, &device->scan_interval);
359 connman_dbus_dict_append_array(&dict, "Networks",
360 DBUS_TYPE_OBJECT_PATH, append_networks, device);
364 connman_dbus_dict_close(&array, &dict);
369 static gboolean powered_timeout(gpointer user_data)
371 struct connman_device *device = user_data;
373 DBG("device %p", device);
377 if (device->pending != NULL) {
380 reply = __connman_error_operation_timeout(device->pending);
382 g_dbus_send_message(connection, reply);
384 dbus_message_unref(device->pending);
385 device->pending = NULL;
391 static DBusMessage *set_property(DBusConnection *conn,
392 DBusMessage *msg, void *data)
394 struct connman_device *device = data;
395 DBusMessageIter iter, value;
399 DBG("conn %p", conn);
401 if (dbus_message_iter_init(msg, &iter) == FALSE)
402 return __connman_error_invalid_arguments(msg);
404 dbus_message_iter_get_basic(&iter, &name);
405 dbus_message_iter_next(&iter);
406 dbus_message_iter_recurse(&iter, &value);
408 if (__connman_security_check_privilege(msg,
409 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
410 return __connman_error_permission_denied(msg);
412 type = dbus_message_iter_get_arg_type(&value);
414 if (g_str_equal(name, "Powered") == TRUE) {
415 connman_bool_t powered;
418 if (type != DBUS_TYPE_BOOLEAN)
419 return __connman_error_invalid_arguments(msg);
421 dbus_message_iter_get_basic(&value, &powered);
423 device->powered_persistent = powered;
425 __connman_storage_save_device(device);
427 if (device->powered == powered)
428 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
430 if (device->pending != NULL)
431 return __connman_error_in_progress(msg);
433 err = set_powered(device, powered);
435 if (err != -EINPROGRESS)
436 return __connman_error_failed(msg, -err);
438 device->pending = dbus_message_ref(msg);
440 device->timeout = g_timeout_add_seconds(15,
441 powered_timeout, device);
445 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
446 connman_uint16_t interval;
448 switch (device->mode) {
449 case CONNMAN_DEVICE_MODE_UNKNOWN:
450 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
451 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
455 if (type != DBUS_TYPE_UINT16)
456 return __connman_error_invalid_arguments(msg);
458 dbus_message_iter_get_basic(&value, &interval);
460 if (device->scan_interval != interval) {
461 device->scan_interval = interval;
463 __connman_storage_save_device(device);
465 reset_scan_trigger(device);
468 return __connman_error_invalid_property(msg);
470 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
473 static DBusMessage *propose_scan(DBusConnection *conn,
474 DBusMessage *msg, void *data)
476 struct connman_device *device = data;
479 DBG("conn %p", conn);
481 switch (device->mode) {
482 case CONNMAN_DEVICE_MODE_UNKNOWN:
483 return __connman_error_not_supported(msg);
484 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
485 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
489 err = __connman_device_scan(device);
491 return __connman_error_failed(msg, -err);
493 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
496 static GDBusMethodTable device_methods[] = {
497 { "GetProperties", "", "a{sv}", get_properties },
498 { "SetProperty", "sv", "", set_property,
499 G_DBUS_METHOD_FLAG_ASYNC },
500 { "ProposeScan", "", "", propose_scan },
504 static GDBusSignalTable device_signals[] = {
505 { "PropertyChanged", "sv" },
509 static int register_interface(struct connman_element *element)
511 struct connman_device *device = element->device;
513 DBG("element %p name %s", element, element->name);
515 if (g_dbus_register_interface(connection, element->path,
516 CONNMAN_DEVICE_INTERFACE,
517 device_methods, device_signals,
518 NULL, device, NULL) == FALSE) {
519 connman_error("Failed to register %s device", element->path);
523 device->registered = TRUE;
528 static void unregister_interface(struct connman_element *element)
530 struct connman_device *device = element->device;
532 DBG("element %p name %s", element, element->name);
534 device->registered = FALSE;
536 g_dbus_unregister_interface(connection, element->path,
537 CONNMAN_DEVICE_INTERFACE);
540 static int setup_device(struct connman_device *device)
544 DBG("device %p", device);
546 err = register_interface(&device->element);
548 if (device->driver->remove)
549 device->driver->remove(device);
550 device->driver = NULL;
554 __connman_technology_add_device(device);
556 switch (device->mode) {
557 case CONNMAN_DEVICE_MODE_UNKNOWN:
558 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
559 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
563 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
566 if (device->offlinemode == FALSE &&
567 device->powered_persistent == TRUE)
568 __connman_device_enable(device);
573 static void probe_driver(struct connman_element *element, gpointer user_data)
575 struct connman_device_driver *driver = user_data;
577 DBG("element %p name %s", element, element->name);
579 if (element->device == NULL)
582 if (element->device->driver != NULL)
585 if (driver->type != element->device->type)
588 if (driver->probe(element->device) < 0)
591 element->device->driver = driver;
593 setup_device(element->device);
596 static void remove_device(struct connman_device *device)
600 DBG("device %p", device);
602 err = __connman_device_disable(device);
603 if (err < 0 && err == -EINPROGRESS)
604 __connman_technology_disable_device(device);
606 switch (device->mode) {
607 case CONNMAN_DEVICE_MODE_UNKNOWN:
608 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
609 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
613 __connman_technology_remove_device(device);
615 unregister_interface(&device->element);
617 if (device->driver->remove)
618 device->driver->remove(device);
620 device->driver = NULL;
623 static void remove_driver(struct connman_element *element, gpointer user_data)
625 struct connman_device_driver *driver = user_data;
627 DBG("element %p name %s", element, element->name);
629 if (element->device == NULL)
632 if (element->device->driver == driver)
633 remove_device(element->device);
636 connman_bool_t __connman_device_has_driver(struct connman_device *device)
638 if (device == NULL || device->driver == NULL)
641 return device->registered;
644 static GSList *driver_list = NULL;
646 static gint compare_priority(gconstpointer a, gconstpointer b)
648 const struct connman_device_driver *driver1 = a;
649 const struct connman_device_driver *driver2 = b;
651 return driver2->priority - driver1->priority;
655 * connman_device_driver_register:
656 * @driver: device driver definition
658 * Register a new device driver
660 * Returns: %0 on success
662 int connman_device_driver_register(struct connman_device_driver *driver)
664 DBG("driver %p name %s", driver, driver->name);
666 driver_list = g_slist_insert_sorted(driver_list, driver,
669 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
670 probe_driver, driver);
676 * connman_device_driver_unregister:
677 * @driver: device driver definition
679 * Remove a previously registered device driver
681 void connman_device_driver_unregister(struct connman_device_driver *driver)
683 DBG("driver %p name %s", driver, driver->name);
685 driver_list = g_slist_remove(driver_list, driver);
687 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
688 remove_driver, driver);
691 static void unregister_network(gpointer data)
693 struct connman_network *network = data;
695 DBG("network %p", network);
697 connman_element_unregister((struct connman_element *) network);
699 connman_network_unref(network);
702 static void device_destruct(struct connman_element *element)
704 struct connman_device *device = element->device;
706 DBG("element %p name %s", element, element->name);
708 if (device->timeout > 0) {
709 g_source_remove(device->timeout);
713 clear_scan_trigger(device);
715 if (device->pending != NULL) {
716 dbus_message_unref(device->pending);
717 device->pending = NULL;
720 g_free(device->ident);
721 g_free(device->node);
722 g_free(device->name);
723 g_free(device->address);
724 g_free(device->control);
725 g_free(device->interface);
727 g_free(device->last_network);
729 g_hash_table_destroy(device->networks);
730 device->networks = NULL;
734 * connman_device_create:
735 * @node: device node name (for example an address)
738 * Allocate a new device of given #type and assign the #node name to it.
740 * Returns: a newly-allocated #connman_device structure
742 struct connman_device *connman_device_create(const char *node,
743 enum connman_device_type type)
745 struct connman_device *device;
748 DBG("node %s type %d", node, type);
750 device = g_try_new0(struct connman_device, 1);
754 DBG("device %p", device);
756 __connman_element_initialize(&device->element);
758 device->element.name = g_strdup(node);
759 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
761 device->element.device = device;
762 device->element.destruct = device_destruct;
764 str = type2string(type);
766 connman_element_set_string(&device->element,
767 CONNMAN_PROPERTY_ID_TYPE, str);
769 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
772 device->name = g_strdup(type2description(device->type));
773 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
775 device->powered_persistent = TRUE;
777 device->phyindex = -1;
780 case CONNMAN_DEVICE_TYPE_UNKNOWN:
781 case CONNMAN_DEVICE_TYPE_VENDOR:
782 device->scan_interval = 0;
784 case CONNMAN_DEVICE_TYPE_ETHERNET:
785 case CONNMAN_DEVICE_TYPE_WIFI:
786 device->scan_interval = 300;
788 case CONNMAN_DEVICE_TYPE_WIMAX:
789 device->scan_interval = 0;
791 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
792 device->scan_interval = 0;
794 case CONNMAN_DEVICE_TYPE_GPS:
795 device->scan_interval = 0;
797 case CONNMAN_DEVICE_TYPE_CELLULAR:
798 device->scan_interval = 0;
802 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
803 g_free, unregister_network);
809 * connman_device_ref:
810 * @device: device structure
812 * Increase reference counter of device
814 struct connman_device *connman_device_ref(struct connman_device *device)
816 if (connman_element_ref(&device->element) == NULL)
823 * connman_device_unref:
824 * @device: device structure
826 * Decrease reference counter of device
828 void connman_device_unref(struct connman_device *device)
830 connman_element_unref(&device->element);
833 const char *__connman_device_get_type(struct connman_device *device)
835 return type2string(device->type);
839 * connman_device_get_type:
840 * @device: device structure
844 enum connman_device_type connman_device_get_type(struct connman_device *device)
850 * connman_device_get_name:
851 * @device: device structure
853 * Get unique name of device
855 const char *connman_device_get_name(struct connman_device *device)
857 return device->element.name;
861 * connman_device_get_path:
862 * @device: device structure
864 * Get path name of device
866 const char *connman_device_get_path(struct connman_device *device)
868 return device->element.path;
872 * connman_device_set_index:
873 * @device: device structure
874 * @index: index number
876 * Set index number of device
878 void connman_device_set_index(struct connman_device *device, int index)
880 device->element.index = index;
884 * connman_device_get_index:
885 * @device: device structure
887 * Get index number of device
889 int connman_device_get_index(struct connman_device *device)
891 return device->element.index;
894 int __connman_device_get_phyindex(struct connman_device *device)
896 return device->phyindex;
899 void __connman_device_set_phyindex(struct connman_device *device,
902 device->phyindex = phyindex;
906 * connman_device_set_interface:
907 * @device: device structure
908 * @interface: interface name
909 * @control: control interface
911 * Set interface name of device
913 void connman_device_set_interface(struct connman_device *device,
914 const char *interface, const char *control)
916 g_free(device->element.devname);
917 device->element.devname = g_strdup(interface);
919 g_free(device->interface);
920 device->interface = g_strdup(interface);
922 g_free(device->control);
923 device->control = g_strdup(control);
925 if (device->name == NULL) {
926 const char *str = type2description(device->type);
927 if (str != NULL && device->interface != NULL)
928 device->name = g_strdup_printf("%s (%s)", str,
933 const char *connman_device_get_control(struct connman_device *device)
935 return device->control;
939 * connman_device_set_ident:
940 * @device: device structure
941 * @ident: unique identifier
943 * Set unique identifier of device
945 void connman_device_set_ident(struct connman_device *device,
948 g_free(device->ident);
949 device->ident = g_strdup(ident);
952 const char *__connman_device_get_ident(struct connman_device *device)
954 return device->ident;
958 * connman_device_set_mode:
959 * @device: device structure
960 * @mode: network mode
962 * Change network mode of device
964 void connman_device_set_mode(struct connman_device *device,
965 enum connman_device_mode mode)
971 * connman_device_get_mode:
972 * @device: device structure
974 * Get network mode of device
976 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
982 * connman_device_set_powered:
983 * @device: device structure
984 * @powered: powered state
986 * Change power state of device
988 int connman_device_set_powered(struct connman_device *device,
989 connman_bool_t powered)
991 DBG("driver %p powered %d", device, powered);
993 if (device->timeout > 0) {
994 g_source_remove(device->timeout);
998 if (device->pending != NULL) {
999 g_dbus_send_reply(connection, device->pending,
1002 dbus_message_unref(device->pending);
1003 device->pending = NULL;
1006 if (device->powered == powered) {
1007 device->powered_pending = powered;
1011 if (powered == TRUE)
1012 __connman_device_enable(device);
1014 __connman_device_disable(device);
1016 device->powered = powered;
1017 device->powered_pending = powered;
1019 if (device->powered == TRUE)
1020 __connman_technology_enable_device(device);
1022 __connman_technology_disable_device(device);
1024 if (device->offlinemode == TRUE && powered == TRUE) {
1025 powered_changed(device);
1026 return connman_device_set_powered(device, FALSE);
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 blocked_changed(device);
1056 if (device->offlinemode == TRUE)
1059 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1061 if (blocked == FALSE)
1062 powered = device->powered_persistent;
1066 return set_powered(device, powered);
1069 connman_bool_t __connman_device_get_blocked(struct connman_device *device)
1071 return device->blocked;
1074 int __connman_device_scan(struct connman_device *device)
1076 if (!device->driver || !device->driver->scan)
1079 if (device->powered == FALSE)
1082 reset_scan_trigger(device);
1084 return device->driver->scan(device);
1087 int __connman_device_enable_persistent(struct connman_device *device)
1091 DBG("device %p", device);
1093 device->powered_persistent = TRUE;
1095 __connman_storage_save_device(device);
1097 err = __connman_device_enable(device);
1098 if (err == 0 || err == -EINPROGRESS) {
1099 device->offlinemode = FALSE;
1100 if (__connman_profile_get_offlinemode() == TRUE) {
1101 __connman_profile_set_offlinemode(FALSE, FALSE);
1103 __connman_profile_save_default();
1110 int __connman_device_disable_persistent(struct connman_device *device)
1112 DBG("device %p", device);
1114 device->powered_persistent = FALSE;
1116 __connman_storage_save_device(device);
1118 return __connman_device_disable(device);
1121 int __connman_device_disconnect(struct connman_device *device)
1123 GHashTableIter iter;
1124 gpointer key, value;
1126 DBG("device %p", device);
1128 connman_device_set_disconnected(device, TRUE);
1130 g_hash_table_iter_init(&iter, device->networks);
1132 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1133 struct connman_network *network = value;
1135 if (__connman_network_get_connecting(network) == TRUE) {
1137 * Skip network in the process of connecting.
1138 * This is a workaround for WiFi networks serviced
1139 * by the supplicant plugin that hold a reference
1140 * to the network. If we disconnect the network
1141 * here then the referenced object will not be
1142 * registered and usage (like launching DHCP client)
1143 * will fail. There is nothing to be gained by
1144 * removing the network here anyway.
1146 connman_warn("Skipping disconnect of %s",
1147 connman_network_get_identifier(network));
1151 __connman_network_disconnect(network);
1157 static void mark_network_unavailable(gpointer key, gpointer value,
1160 struct connman_network *network = value;
1162 if (connman_network_get_connected(network) == TRUE)
1165 connman_network_set_available(network, FALSE);
1168 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1171 struct connman_network *network = value;
1173 if (connman_network_get_connected(network) == TRUE)
1176 if (connman_network_get_available(network) == TRUE)
1182 void __connman_device_cleanup_networks(struct connman_device *device)
1184 g_hash_table_foreach_remove(device->networks,
1185 remove_unavailable_network, NULL);
1188 static void scanning_changed(struct connman_device *device)
1190 connman_dbus_property_changed_basic(device->element.path,
1191 CONNMAN_DEVICE_INTERFACE, "Scanning",
1192 DBUS_TYPE_BOOLEAN, &device->scanning);
1196 * connman_device_set_scanning:
1197 * @device: device structure
1198 * @scanning: scanning state
1200 * Change scanning state of device
1202 int connman_device_set_scanning(struct connman_device *device,
1203 connman_bool_t scanning)
1205 DBG("device %p scanning %d", device, scanning);
1207 if (!device->driver || !device->driver->scan)
1210 if (device->scanning == scanning)
1213 device->scanning = scanning;
1215 scanning_changed(device);
1217 if (scanning == TRUE) {
1218 reset_scan_trigger(device);
1220 g_hash_table_foreach(device->networks,
1221 mark_network_unavailable, NULL);
1226 __connman_device_cleanup_networks(device);
1228 if (device->connections > 0)
1231 if (device->disconnected == TRUE)
1234 __connman_service_auto_connect();
1240 * connman_device_set_disconnected:
1241 * @device: device structure
1242 * @disconnected: disconnected state
1244 * Change disconnected state of device (only for device with networks)
1246 int connman_device_set_disconnected(struct connman_device *device,
1247 connman_bool_t disconnected)
1249 DBG("device %p disconnected %d", device, disconnected);
1251 switch (device->mode) {
1252 case CONNMAN_DEVICE_MODE_UNKNOWN:
1254 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1255 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1259 if (device->disconnected == disconnected)
1262 device->disconnected = disconnected;
1264 if (disconnected == TRUE)
1265 force_scan_trigger(device);
1271 * connman_device_get_disconnected:
1272 * @device: device structure
1274 * Get device disconnected state
1276 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1278 return device->disconnected;
1282 * connman_device_set_string:
1283 * @device: device structure
1284 * @key: unique identifier
1285 * @value: string value
1287 * Set string value for specific key
1289 int connman_device_set_string(struct connman_device *device,
1290 const char *key, const char *value)
1292 DBG("device %p key %s value %s", device, key, value);
1294 if (g_str_equal(key, "Address") == TRUE) {
1295 g_free(device->address);
1296 device->address = g_strdup(value);
1297 } else if (g_str_equal(key, "Name") == TRUE) {
1298 g_free(device->name);
1299 device->name = g_strdup(value);
1300 } else if (g_str_equal(key, "Node") == TRUE) {
1301 g_free(device->node);
1302 device->node = g_strdup(value);
1305 return connman_element_set_string(&device->element, key, value);
1309 * connman_device_get_string:
1310 * @device: device structure
1311 * @key: unique identifier
1313 * Get string value for specific key
1315 const char *connman_device_get_string(struct connman_device *device,
1318 DBG("device %p key %s", device, key);
1320 if (g_str_equal(key, "Address") == TRUE)
1321 return device->address;
1322 else if (g_str_equal(key, "Name") == TRUE)
1323 return device->name;
1324 else if (g_str_equal(key, "Node") == TRUE)
1325 return device->node;
1327 return connman_element_get_string(&device->element, key);
1330 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1332 struct connman_device *device = element->device;
1333 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1334 connman_bool_t powered;
1336 DBG("element %p name %s", element, element->name);
1341 device->offlinemode = offlinemode;
1343 if (device->blocked == TRUE)
1346 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1348 if (device->powered == powered)
1351 if (device->powered_persistent == FALSE)
1354 set_powered(device, powered);
1357 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1359 DBG("offlinmode %d", offlinemode);
1361 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1362 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1364 __connman_notifier_offlinemode(offlinemode);
1369 void __connman_device_increase_connections(struct connman_device *device)
1371 device->connections++;
1374 void __connman_device_decrease_connections(struct connman_device *device)
1376 device->connections--;
1380 * connman_device_add_network:
1381 * @device: device structure
1382 * @network: network structure
1384 * Add new network to the device
1386 int connman_device_add_network(struct connman_device *device,
1387 struct connman_network *network)
1389 const char *identifier = connman_network_get_identifier(network);
1392 DBG("device %p network %p", device, network);
1394 if (identifier == NULL)
1397 switch (device->mode) {
1398 case CONNMAN_DEVICE_MODE_UNKNOWN:
1400 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1401 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1405 __connman_network_set_device(network, device);
1407 err = connman_element_register((struct connman_element *) network,
1410 __connman_network_set_device(network, NULL);
1414 g_hash_table_insert(device->networks, g_strdup(identifier),
1421 * connman_device_get_network:
1422 * @device: device structure
1423 * @identifier: network identifier
1425 * Get network for given identifier
1427 struct connman_network *connman_device_get_network(struct connman_device *device,
1428 const char *identifier)
1430 DBG("device %p identifier %s", device, identifier);
1432 return g_hash_table_lookup(device->networks, identifier);
1436 * connman_device_remove_network:
1437 * @device: device structure
1438 * @identifier: network identifier
1440 * Remove network for given identifier
1442 int connman_device_remove_network(struct connman_device *device,
1443 const char *identifier)
1445 DBG("device %p identifier %s", device, identifier);
1447 g_hash_table_remove(device->networks, identifier);
1452 void connman_device_remove_all_networks(struct connman_device *device)
1454 g_hash_table_remove_all(device->networks);
1457 void __connman_device_set_network(struct connman_device *device,
1458 struct connman_network *network)
1465 if (device->network == network)
1468 if (device->network != NULL)
1469 connman_network_unref(device->network);
1471 if (network != NULL) {
1472 name = connman_network_get_string(network,
1473 CONNMAN_PROPERTY_ID_NAME);
1474 g_free(device->last_network);
1475 device->last_network = g_strdup(name);
1477 device->network = connman_network_ref(network);
1479 g_free(device->last_network);
1480 device->last_network = NULL;
1482 device->network = NULL;
1486 void __connman_device_set_reconnect(struct connman_device *device,
1487 connman_bool_t reconnect)
1489 device->reconnect = reconnect;
1492 connman_bool_t __connman_device_get_reconnect(
1493 struct connman_device *device)
1495 return device->reconnect;
1499 * connman_device_register:
1500 * @device: device structure
1502 * Register device with the system
1504 int connman_device_register(struct connman_device *device)
1506 __connman_storage_load_device(device);
1508 device->offlinemode = __connman_profile_get_offlinemode();
1510 return connman_element_register(&device->element, NULL);
1514 * connman_device_unregister:
1515 * @device: device structure
1517 * Unregister device with the system
1519 void connman_device_unregister(struct connman_device *device)
1521 __connman_storage_save_device(device);
1523 connman_element_unregister(&device->element);
1527 * connman_device_get_data:
1528 * @device: device structure
1530 * Get private device data pointer
1532 void *connman_device_get_data(struct connman_device *device)
1534 return device->driver_data;
1538 * connman_device_set_data:
1539 * @device: device structure
1540 * @data: data pointer
1542 * Set private device data pointer
1544 void connman_device_set_data(struct connman_device *device, void *data)
1546 device->driver_data = data;
1549 static gboolean match_driver(struct connman_device *device,
1550 struct connman_device_driver *driver)
1552 if (device->type == driver->type ||
1553 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1559 static int device_probe(struct connman_element *element)
1561 struct connman_device *device = element->device;
1564 DBG("element %p name %s", element, element->name);
1569 if (device->driver != NULL)
1572 for (list = driver_list; list; list = list->next) {
1573 struct connman_device_driver *driver = list->data;
1575 if (match_driver(device, driver) == FALSE)
1578 DBG("driver %p name %s", driver, driver->name);
1580 if (driver->probe(device) == 0) {
1581 device->driver = driver;
1586 if (device->driver == NULL)
1589 return setup_device(device);
1592 static void device_remove(struct connman_element *element)
1594 struct connman_device *device = element->device;
1596 DBG("element %p name %s", element, element->name);
1601 if (device->driver == NULL)
1604 remove_device(device);
1607 static struct connman_driver device_driver = {
1609 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1610 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1611 .probe = device_probe,
1612 .remove = device_remove,
1615 static int device_load(struct connman_device *device)
1617 const char *ident = __connman_profile_active_ident();
1619 GError *error = NULL;
1621 connman_bool_t powered;
1624 DBG("device %p", device);
1626 keyfile = __connman_storage_open_profile(ident);
1627 if (keyfile == NULL)
1630 identifier = g_strdup_printf("device_%s", device->element.name);
1631 if (identifier == NULL)
1634 powered = g_key_file_get_boolean(keyfile, identifier,
1637 device->powered_persistent = powered;
1638 g_clear_error(&error);
1640 switch (device->mode) {
1641 case CONNMAN_DEVICE_MODE_UNKNOWN:
1643 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1644 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1645 val = g_key_file_get_integer(keyfile, identifier,
1646 "ScanInterval", &error);
1647 if (error == NULL && val > 0)
1648 device->scan_interval = val;
1649 g_clear_error(&error);
1656 __connman_storage_close_profile(ident, keyfile, FALSE);
1661 static int device_save(struct connman_device *device)
1663 const char *ident = __connman_profile_active_ident();
1667 DBG("device %p", device);
1669 keyfile = __connman_storage_open_profile(ident);
1670 if (keyfile == NULL)
1673 identifier = g_strdup_printf("device_%s", device->element.name);
1674 if (identifier == NULL)
1677 g_key_file_set_boolean(keyfile, identifier,
1678 "Powered", device->powered_persistent);
1680 switch (device->mode) {
1681 case CONNMAN_DEVICE_MODE_UNKNOWN:
1683 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1684 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1685 if (device->scan_interval > 0)
1686 g_key_file_set_integer(keyfile, identifier,
1687 "ScanInterval", device->scan_interval);
1694 __connman_storage_close_profile(ident, keyfile, TRUE);
1699 static struct connman_storage device_storage = {
1701 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1702 .device_load = device_load,
1703 .device_save = device_save,
1706 int __connman_device_init(void)
1710 connection = connman_dbus_get_connection();
1712 if (connman_storage_register(&device_storage) < 0)
1713 connman_error("Failed to register device storage");
1715 return connman_driver_register(&device_driver);
1718 void __connman_device_cleanup(void)
1722 connman_driver_unregister(&device_driver);
1724 connman_storage_unregister(&device_storage);
1726 dbus_connection_unref(connection);