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 secondary;
40 connman_bool_t offlinemode;
41 connman_bool_t blocked;
42 connman_bool_t powered;
43 connman_bool_t powered_pending;
44 connman_bool_t powered_persistent;
45 connman_bool_t scanning;
46 connman_bool_t disconnected;
47 connman_bool_t reconnect;
48 connman_uint16_t scan_interval;
56 unsigned int connections;
59 struct connman_device_driver *driver;
62 connman_bool_t registered;
65 struct connman_network *network;
72 static gboolean device_scan_trigger(gpointer user_data)
74 struct connman_device *device = user_data;
76 DBG("device %p", device);
78 if (device->driver == NULL) {
79 device->scan_timeout = 0;
83 if (device->driver->scan)
84 device->driver->scan(device);
89 static void clear_scan_trigger(struct connman_device *device)
91 if (device->scan_timeout > 0) {
92 g_source_remove(device->scan_timeout);
93 device->scan_timeout = 0;
97 static void reset_scan_trigger(struct connman_device *device)
99 clear_scan_trigger(device);
101 if (device->scan_interval > 0) {
102 guint interval = device->scan_interval;
103 device->scan_timeout = g_timeout_add_seconds(interval,
104 device_scan_trigger, device);
108 static void force_scan_trigger(struct connman_device *device)
110 clear_scan_trigger(device);
112 device->scan_timeout = g_timeout_add_seconds(5,
113 device_scan_trigger, device);
116 void connman_device_schedule_scan(struct connman_device *device)
118 reset_scan_trigger(device);
121 static const char *type2description(enum connman_device_type type)
124 case CONNMAN_DEVICE_TYPE_UNKNOWN:
125 case CONNMAN_DEVICE_TYPE_VENDOR:
127 case CONNMAN_DEVICE_TYPE_ETHERNET:
129 case CONNMAN_DEVICE_TYPE_WIFI:
131 case CONNMAN_DEVICE_TYPE_WIMAX:
133 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
135 case CONNMAN_DEVICE_TYPE_GPS:
137 case CONNMAN_DEVICE_TYPE_CELLULAR:
138 case CONNMAN_DEVICE_TYPE_MBM:
139 case CONNMAN_DEVICE_TYPE_HSO:
146 static const char *type2string(enum connman_device_type type)
149 case CONNMAN_DEVICE_TYPE_UNKNOWN:
150 case CONNMAN_DEVICE_TYPE_VENDOR:
152 case CONNMAN_DEVICE_TYPE_ETHERNET:
154 case CONNMAN_DEVICE_TYPE_WIFI:
156 case CONNMAN_DEVICE_TYPE_WIMAX:
158 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
160 case CONNMAN_DEVICE_TYPE_GPS:
162 case CONNMAN_DEVICE_TYPE_CELLULAR:
163 case CONNMAN_DEVICE_TYPE_MBM:
164 case CONNMAN_DEVICE_TYPE_HSO:
171 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
173 enum connman_device_type type = connman_device_get_type(device);
176 case CONNMAN_DEVICE_TYPE_UNKNOWN:
177 case CONNMAN_DEVICE_TYPE_VENDOR:
178 case CONNMAN_DEVICE_TYPE_GPS:
180 case CONNMAN_DEVICE_TYPE_ETHERNET:
181 return CONNMAN_SERVICE_TYPE_ETHERNET;
182 case CONNMAN_DEVICE_TYPE_WIFI:
183 return CONNMAN_SERVICE_TYPE_WIFI;
184 case CONNMAN_DEVICE_TYPE_WIMAX:
185 return CONNMAN_SERVICE_TYPE_WIMAX;
186 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
187 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
188 case CONNMAN_DEVICE_TYPE_CELLULAR:
189 case CONNMAN_DEVICE_TYPE_MBM:
190 case CONNMAN_DEVICE_TYPE_HSO:
191 return CONNMAN_SERVICE_TYPE_CELLULAR;
194 return CONNMAN_SERVICE_TYPE_UNKNOWN;
197 static int powered_changed(struct connman_device *device)
200 DBusMessageIter entry, value;
201 const char *key = "Powered";
203 signal = dbus_message_new_signal(device->element.path,
204 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
208 dbus_message_iter_init_append(signal, &entry);
210 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
212 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
213 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
214 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
216 dbus_message_iter_close_container(&entry, &value);
218 g_dbus_send_message(connection, signal);
223 static int set_powered(struct connman_device *device, connman_bool_t powered)
225 struct connman_device_driver *driver = device->driver;
226 enum connman_service_type type;
229 DBG("device %p powered %d", device, powered);
231 if (device->powered_pending == powered)
237 type = __connman_device_get_service_type(device);
239 if (powered == TRUE) {
240 if (driver->enable) {
241 device->powered_pending = powered;
243 err = driver->enable(device);
245 __connman_notifier_enable(type);
249 device->powered_pending = powered;
251 device->reconnect = FALSE;
253 clear_scan_trigger(device);
255 g_hash_table_remove_all(device->networks);
257 if (driver->disable) {
258 err = driver->disable(device);
260 __connman_notifier_disable(type);
266 device->powered = powered;
268 if (device->registered == TRUE)
269 powered_changed(device);
275 static void append_path(gpointer key, gpointer value, gpointer user_data)
277 struct connman_element *element = value;
278 DBusMessageIter *iter = user_data;
280 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
284 static void append_networks(struct connman_device *device,
285 DBusMessageIter *entry)
287 DBusMessageIter value, iter;
288 const char *key = "Networks";
290 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
292 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
293 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
296 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
297 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
298 g_hash_table_foreach(device->networks, append_path, &iter);
299 dbus_message_iter_close_container(&value, &iter);
301 dbus_message_iter_close_container(entry, &value);
304 static DBusMessage *get_properties(DBusConnection *conn,
305 DBusMessage *msg, void *data)
307 struct connman_device *device = data;
309 DBusMessageIter array, dict, entry;
312 DBG("conn %p", conn);
314 if (__connman_security_check_privilege(msg,
315 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
316 return __connman_error_permission_denied(msg);
318 reply = dbus_message_new_method_return(msg);
322 dbus_message_iter_init_append(reply, &array);
324 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
325 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
326 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
327 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
329 if (device->name != NULL)
330 connman_dbus_dict_append_variant(&dict, "Name",
331 DBUS_TYPE_STRING, &device->name);
333 str = type2string(device->type);
335 connman_dbus_dict_append_variant(&dict, "Type",
336 DBUS_TYPE_STRING, &str);
338 if (device->address != NULL)
339 connman_dbus_dict_append_variant(&dict, "Address",
340 DBUS_TYPE_STRING, &device->address);
342 if (device->interface != NULL)
343 connman_dbus_dict_append_variant(&dict, "Interface",
344 DBUS_TYPE_STRING, &device->interface);
346 connman_dbus_dict_append_variant(&dict, "Powered",
347 DBUS_TYPE_BOOLEAN, &device->powered);
349 if (device->driver && device->driver->scan)
350 connman_dbus_dict_append_variant(&dict, "Scanning",
351 DBUS_TYPE_BOOLEAN, &device->scanning);
353 switch (device->mode) {
354 case CONNMAN_DEVICE_MODE_UNKNOWN:
356 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
357 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
358 if (device->scan_interval > 0)
359 connman_dbus_dict_append_variant(&dict, "ScanInterval",
360 DBUS_TYPE_UINT16, &device->scan_interval);
362 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
364 append_networks(device, &entry);
365 dbus_message_iter_close_container(&dict, &entry);
369 dbus_message_iter_close_container(&array, &dict);
374 static gboolean powered_timeout(gpointer user_data)
376 struct connman_device *device = user_data;
378 DBG("device %p", device);
382 if (device->pending != NULL) {
385 reply = __connman_error_operation_timeout(device->pending);
387 g_dbus_send_message(connection, reply);
389 dbus_message_unref(device->pending);
390 device->pending = NULL;
396 static DBusMessage *set_property(DBusConnection *conn,
397 DBusMessage *msg, void *data)
399 struct connman_device *device = data;
400 DBusMessageIter iter, value;
404 DBG("conn %p", conn);
406 if (dbus_message_iter_init(msg, &iter) == FALSE)
407 return __connman_error_invalid_arguments(msg);
409 dbus_message_iter_get_basic(&iter, &name);
410 dbus_message_iter_next(&iter);
411 dbus_message_iter_recurse(&iter, &value);
413 if (__connman_security_check_privilege(msg,
414 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
415 return __connman_error_permission_denied(msg);
417 type = dbus_message_iter_get_arg_type(&value);
419 if (g_str_equal(name, "Powered") == TRUE) {
420 connman_bool_t powered;
423 if (type != DBUS_TYPE_BOOLEAN)
424 return __connman_error_invalid_arguments(msg);
426 dbus_message_iter_get_basic(&value, &powered);
428 device->powered_persistent = powered;
430 __connman_storage_save_device(device);
432 if (device->powered == powered)
433 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
435 if (device->pending != NULL)
436 return __connman_error_in_progress(msg);
438 err = set_powered(device, powered);
440 if (err != -EINPROGRESS)
441 return __connman_error_failed(msg, -err);
443 device->pending = dbus_message_ref(msg);
445 device->timeout = g_timeout_add_seconds(15,
446 powered_timeout, device);
450 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
451 connman_uint16_t interval;
453 switch (device->mode) {
454 case CONNMAN_DEVICE_MODE_UNKNOWN:
455 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
456 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
460 if (type != DBUS_TYPE_UINT16)
461 return __connman_error_invalid_arguments(msg);
463 dbus_message_iter_get_basic(&value, &interval);
465 if (device->scan_interval != interval) {
466 device->scan_interval = interval;
468 __connman_storage_save_device(device);
470 reset_scan_trigger(device);
473 return __connman_error_invalid_property(msg);
475 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
478 static DBusMessage *propose_scan(DBusConnection *conn,
479 DBusMessage *msg, void *data)
481 struct connman_device *device = data;
484 DBG("conn %p", conn);
486 switch (device->mode) {
487 case CONNMAN_DEVICE_MODE_UNKNOWN:
488 return __connman_error_not_supported(msg);
489 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
490 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
494 err = __connman_device_scan(device);
496 return __connman_error_failed(msg, -err);
498 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
501 static GDBusMethodTable device_methods[] = {
502 { "GetProperties", "", "a{sv}", get_properties },
503 { "SetProperty", "sv", "", set_property,
504 G_DBUS_METHOD_FLAG_ASYNC },
505 { "ProposeScan", "", "", propose_scan },
509 static GDBusSignalTable device_signals[] = {
510 { "PropertyChanged", "sv" },
514 static void append_devices(DBusMessageIter *entry)
516 DBusMessageIter value, iter;
517 const char *key = "Devices";
519 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
521 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
522 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
525 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
526 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
527 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
528 dbus_message_iter_close_container(&value, &iter);
530 dbus_message_iter_close_container(entry, &value);
533 static void emit_devices_signal(void)
536 DBusMessageIter entry;
538 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
539 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
543 dbus_message_iter_init_append(signal, &entry);
545 append_devices(&entry);
547 g_dbus_send_message(connection, signal);
550 static int register_interface(struct connman_element *element)
552 struct connman_device *device = element->device;
554 DBG("element %p name %s", element, element->name);
556 if (g_dbus_register_interface(connection, element->path,
557 CONNMAN_DEVICE_INTERFACE,
558 device_methods, device_signals,
559 NULL, device, NULL) == FALSE) {
560 connman_error("Failed to register %s device", element->path);
564 device->registered = TRUE;
566 emit_devices_signal();
571 static void unregister_interface(struct connman_element *element)
573 struct connman_device *device = element->device;
575 DBG("element %p name %s", element, element->name);
577 device->registered = FALSE;
579 emit_devices_signal();
581 g_dbus_unregister_interface(connection, element->path,
582 CONNMAN_DEVICE_INTERFACE);
585 static int setup_device(struct connman_device *device)
587 enum connman_service_type type;
590 DBG("device %p", device);
592 err = register_interface(&device->element);
594 if (device->driver->remove)
595 device->driver->remove(device);
596 device->driver = NULL;
600 type = __connman_device_get_service_type(device);
601 __connman_notifier_register(type);
603 switch (device->mode) {
604 case CONNMAN_DEVICE_MODE_UNKNOWN:
605 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
606 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
610 if (device->offlinemode == FALSE &&
611 device->powered_persistent == TRUE)
612 __connman_device_enable(device);
617 static void probe_driver(struct connman_element *element, gpointer user_data)
619 struct connman_device_driver *driver = user_data;
621 DBG("element %p name %s", element, element->name);
623 if (element->device == NULL)
626 if (element->device->driver != NULL)
629 if (driver->type != element->device->type)
632 if (driver->probe(element->device) < 0)
635 element->device->driver = driver;
637 setup_device(element->device);
640 static void remove_device(struct connman_device *device)
642 enum connman_service_type type;
644 DBG("device %p", device);
646 __connman_device_disable(device);
648 switch (device->mode) {
649 case CONNMAN_DEVICE_MODE_UNKNOWN:
650 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
651 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
655 type = __connman_device_get_service_type(device);
656 __connman_notifier_unregister(type);
658 unregister_interface(&device->element);
660 if (device->driver->remove)
661 device->driver->remove(device);
663 device->driver = NULL;
666 static void remove_driver(struct connman_element *element, gpointer user_data)
668 struct connman_device_driver *driver = user_data;
670 DBG("element %p name %s", element, element->name);
672 if (element->device == NULL)
675 if (element->device->driver == driver)
676 remove_device(element->device);
679 connman_bool_t __connman_device_has_driver(struct connman_device *device)
681 if (device == NULL || device->driver == NULL)
684 return device->registered;
687 static GSList *driver_list = NULL;
689 static gint compare_priority(gconstpointer a, gconstpointer b)
691 const struct connman_device_driver *driver1 = a;
692 const struct connman_device_driver *driver2 = b;
694 return driver2->priority - driver1->priority;
698 * connman_device_driver_register:
699 * @driver: device driver definition
701 * Register a new device driver
703 * Returns: %0 on success
705 int connman_device_driver_register(struct connman_device_driver *driver)
707 DBG("driver %p name %s", driver, driver->name);
709 driver_list = g_slist_insert_sorted(driver_list, driver,
712 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
713 probe_driver, driver);
719 * connman_device_driver_unregister:
720 * @driver: device driver definition
722 * Remove a previously registered device driver
724 void connman_device_driver_unregister(struct connman_device_driver *driver)
726 DBG("driver %p name %s", driver, driver->name);
728 driver_list = g_slist_remove(driver_list, driver);
730 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
731 remove_driver, driver);
734 static void unregister_network(gpointer data)
736 struct connman_network *network = data;
738 DBG("network %p", network);
740 connman_element_unregister((struct connman_element *) network);
742 connman_network_unref(network);
745 static void device_destruct(struct connman_element *element)
747 struct connman_device *device = element->device;
749 DBG("element %p name %s", element, element->name);
751 if (device->timeout > 0) {
752 g_source_remove(device->timeout);
756 clear_scan_trigger(device);
758 if (device->pending != NULL) {
759 dbus_message_unref(device->pending);
760 device->pending = NULL;
763 g_free(device->ident);
764 g_free(device->node);
765 g_free(device->name);
766 g_free(device->address);
767 g_free(device->control);
768 g_free(device->interface);
770 g_free(device->last_network);
772 g_hash_table_destroy(device->networks);
773 device->networks = NULL;
777 * connman_device_create:
778 * @node: device node name (for example an address)
781 * Allocate a new device of given #type and assign the #node name to it.
783 * Returns: a newly-allocated #connman_device structure
785 struct connman_device *connman_device_create(const char *node,
786 enum connman_device_type type)
788 struct connman_device *device;
791 DBG("node %s type %d", node, type);
793 device = g_try_new0(struct connman_device, 1);
797 DBG("device %p", device);
799 __connman_element_initialize(&device->element);
801 device->element.name = g_strdup(node);
802 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
804 device->element.device = device;
805 device->element.destruct = device_destruct;
807 str = type2string(type);
809 connman_element_set_string(&device->element,
810 CONNMAN_PROPERTY_ID_TYPE, str);
812 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
815 device->name = g_strdup(type2description(device->type));
816 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
817 device->secondary = FALSE;
819 device->powered_persistent = TRUE;
821 device->phyindex = -1;
824 case CONNMAN_DEVICE_TYPE_UNKNOWN:
825 case CONNMAN_DEVICE_TYPE_VENDOR:
826 device->scan_interval = 0;
828 case CONNMAN_DEVICE_TYPE_ETHERNET:
829 case CONNMAN_DEVICE_TYPE_WIFI:
830 device->scan_interval = 300;
832 case CONNMAN_DEVICE_TYPE_WIMAX:
833 device->scan_interval = 0;
835 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
836 device->scan_interval = 0;
838 case CONNMAN_DEVICE_TYPE_GPS:
839 device->scan_interval = 0;
841 case CONNMAN_DEVICE_TYPE_CELLULAR:
842 case CONNMAN_DEVICE_TYPE_MBM:
843 case CONNMAN_DEVICE_TYPE_HSO:
844 device->scan_interval = 0;
848 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
849 g_free, unregister_network);
855 * connman_device_ref:
856 * @device: device structure
858 * Increase reference counter of device
860 struct connman_device *connman_device_ref(struct connman_device *device)
862 if (connman_element_ref(&device->element) == NULL)
869 * connman_device_unref:
870 * @device: device structure
872 * Decrease reference counter of device
874 void connman_device_unref(struct connman_device *device)
876 connman_element_unref(&device->element);
879 const char *__connman_device_get_type(struct connman_device *device)
881 return type2string(device->type);
885 * connman_device_get_type:
886 * @device: device structure
890 enum connman_device_type connman_device_get_type(struct connman_device *device)
896 * connman_device_get_name:
897 * @device: device structure
899 * Get unique name of device
901 const char *connman_device_get_name(struct connman_device *device)
903 return device->element.name;
907 * connman_device_get_path:
908 * @device: device structure
910 * Get path name of device
912 const char *connman_device_get_path(struct connman_device *device)
914 return device->element.path;
918 * connman_device_set_index:
919 * @device: device structure
920 * @index: index number
922 * Set index number of device
924 void connman_device_set_index(struct connman_device *device, int index)
926 device->element.index = index;
930 * connman_device_get_index:
931 * @device: device structure
933 * Get index number of device
935 int connman_device_get_index(struct connman_device *device)
937 return device->element.index;
940 int __connman_device_get_phyindex(struct connman_device *device)
942 return device->phyindex;
945 void __connman_device_set_phyindex(struct connman_device *device,
948 device->phyindex = phyindex;
952 * connman_device_set_interface:
953 * @device: device structure
954 * @interface: interface name
955 * @control: control interface
957 * Set interface name of device
959 void connman_device_set_interface(struct connman_device *device,
960 const char *interface, const char *control)
962 g_free(device->element.devname);
963 device->element.devname = g_strdup(interface);
965 g_free(device->interface);
966 device->interface = g_strdup(interface);
968 g_free(device->control);
969 device->control = g_strdup(control);
971 if (device->name == NULL) {
972 const char *str = type2description(device->type);
973 if (str != NULL && device->interface != NULL)
974 device->name = g_strdup_printf("%s (%s)", str,
979 const char *connman_device_get_control(struct connman_device *device)
981 return device->control;
985 * connman_device_set_ident:
986 * @device: device structure
987 * @ident: unique identifier
989 * Set unique identifier of device
991 void connman_device_set_ident(struct connman_device *device,
994 g_free(device->ident);
995 device->ident = g_strdup(ident);
998 const char *__connman_device_get_ident(struct connman_device *device)
1000 return device->ident;
1004 * connman_device_set_mode:
1005 * @device: device structure
1006 * @mode: network mode
1008 * Change network mode of device
1010 void connman_device_set_mode(struct connman_device *device,
1011 enum connman_device_mode mode)
1013 device->mode = mode;
1017 * connman_device_get_mode:
1018 * @device: device structure
1020 * Get network mode of device
1022 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1024 return device->mode;
1028 * connman_device_set_secondary:
1029 * @device: device structure
1030 * @secondary: secondary value
1032 * Change secondary value of device
1034 void connman_device_set_secondary(struct connman_device *device,
1035 connman_bool_t secondary)
1037 device->secondary = secondary;
1041 * connman_device_get_secondary:
1042 * @device: device structure
1044 * Get secondary value of device
1046 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1048 return device->secondary;
1052 * connman_device_set_powered:
1053 * @device: device structure
1054 * @powered: powered state
1056 * Change power state of device
1058 int connman_device_set_powered(struct connman_device *device,
1059 connman_bool_t powered)
1061 enum connman_service_type type;
1063 DBG("driver %p powered %d", device, powered);
1065 if (device->timeout > 0) {
1066 g_source_remove(device->timeout);
1067 device->timeout = 0;
1070 if (device->pending != NULL) {
1071 g_dbus_send_reply(connection, device->pending,
1074 dbus_message_unref(device->pending);
1075 device->pending = NULL;
1078 if (device->powered == powered)
1081 device->powered = powered;
1082 device->powered_pending = powered;
1084 type = __connman_device_get_service_type(device);
1086 if (device->powered == TRUE)
1087 __connman_notifier_enable(type);
1089 __connman_notifier_disable(type);
1091 if (device->registered == FALSE)
1094 powered_changed(device);
1096 if (powered == FALSE)
1099 reset_scan_trigger(device);
1101 if (device->driver->scan)
1102 device->driver->scan(device);
1107 int __connman_device_set_blocked(struct connman_device *device,
1108 connman_bool_t blocked)
1110 connman_bool_t powered;
1112 DBG("device %p blocked %d", device, blocked);
1114 device->blocked = blocked;
1116 if (device->offlinemode == TRUE)
1119 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1121 if (blocked == FALSE)
1122 powered = device->powered_persistent;
1126 return set_powered(device, powered);
1129 int __connman_device_scan(struct connman_device *device)
1131 if (!device->driver || !device->driver->scan)
1134 if (device->powered == FALSE)
1137 reset_scan_trigger(device);
1139 return device->driver->scan(device);
1142 int __connman_device_enable(struct connman_device *device)
1144 enum connman_service_type type;
1147 DBG("device %p", device);
1149 if (!device->driver || !device->driver->enable)
1152 if (device->powered_pending == TRUE)
1155 device->powered_pending = TRUE;
1157 err = device->driver->enable(device);
1161 device->powered = TRUE;
1163 type = __connman_device_get_service_type(device);
1164 __connman_notifier_enable(type);
1169 int __connman_device_enable_persistent(struct connman_device *device)
1171 DBG("device %p", device);
1173 device->powered_persistent = TRUE;
1175 __connman_storage_save_device(device);
1177 return __connman_device_enable(device);
1180 int __connman_device_disable(struct connman_device *device)
1182 enum connman_service_type type;
1185 DBG("device %p", device);
1187 if (!device->driver || !device->driver->disable)
1190 if (device->powered == FALSE)
1193 if (device->powered_pending == FALSE)
1196 device->powered_pending = FALSE;
1198 device->reconnect = FALSE;
1200 clear_scan_trigger(device);
1202 g_hash_table_remove_all(device->networks);
1204 err = device->driver->disable(device);
1208 device->powered = FALSE;
1210 type = __connman_device_get_service_type(device);
1211 __connman_notifier_disable(type);
1216 int __connman_device_disable_persistent(struct connman_device *device)
1218 DBG("device %p", device);
1220 device->powered_persistent = FALSE;
1222 __connman_storage_save_device(device);
1224 return __connman_device_disable(device);
1227 int __connman_device_disconnect(struct connman_device *device)
1229 GHashTableIter iter;
1230 gpointer key, value;
1232 DBG("device %p", device);
1234 connman_device_set_disconnected(device, TRUE);
1236 g_hash_table_iter_init(&iter, device->networks);
1238 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1239 struct connman_network *network = value;
1241 if (__connman_network_get_connecting(network) == TRUE) {
1243 * Skip network in the process of connecting.
1244 * This is a workaround for WiFi networks serviced
1245 * by the supplicant plugin that hold a reference
1246 * to the network. If we disconnect the network
1247 * here then the referenced object will not be
1248 * registered and usage (like launching DHCP client)
1249 * will fail. There is nothing to be gained by
1250 * removing the network here anyway.
1252 connman_warn("Skipping disconnect of %s",
1253 connman_network_get_identifier(network));
1257 __connman_network_disconnect(network);
1263 static void mark_network_unavailable(gpointer key, gpointer value,
1266 struct connman_network *network = value;
1268 if (connman_network_get_connected(network) == TRUE)
1271 connman_network_set_available(network, FALSE);
1274 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1277 struct connman_network *network = value;
1279 if (connman_network_get_connected(network) == TRUE)
1282 if (connman_network_get_available(network) == TRUE)
1288 void __connman_device_cleanup_networks(struct connman_device *device)
1290 g_hash_table_foreach_remove(device->networks,
1291 remove_unavailable_network, NULL);
1294 static void scanning_changed(struct connman_device *device)
1296 DBusMessage *signal;
1297 DBusMessageIter entry, value;
1298 const char *key = "Scanning";
1300 signal = dbus_message_new_signal(device->element.path,
1301 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1305 dbus_message_iter_init_append(signal, &entry);
1307 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1309 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1310 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1311 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
1313 dbus_message_iter_close_container(&entry, &value);
1315 g_dbus_send_message(connection, signal);
1318 static void mark_network_available(gpointer key, gpointer value,
1321 struct connman_network *network = value;
1323 connman_network_set_available(network, TRUE);
1326 void connman_device_cleanup_scanning(struct connman_device *device)
1328 device->scanning = FALSE;
1330 scanning_changed(device);
1332 g_hash_table_foreach(device->networks,
1333 mark_network_available, NULL);
1337 * connman_device_set_scanning:
1338 * @device: device structure
1339 * @scanning: scanning state
1341 * Change scanning state of device
1343 int connman_device_set_scanning(struct connman_device *device,
1344 connman_bool_t scanning)
1346 DBG("device %p scanning %d", device, scanning);
1348 if (!device->driver || !device->driver->scan)
1351 if (device->scanning == scanning)
1354 device->scanning = scanning;
1356 scanning_changed(device);
1358 if (scanning == TRUE) {
1359 reset_scan_trigger(device);
1361 g_hash_table_foreach(device->networks,
1362 mark_network_unavailable, NULL);
1367 __connman_device_cleanup_networks(device);
1369 if (device->connections > 0)
1372 if (device->disconnected == TRUE)
1375 __connman_service_auto_connect();
1381 * connman_device_set_disconnected:
1382 * @device: device structure
1383 * @disconnected: disconnected state
1385 * Change disconnected state of device (only for device with networks)
1387 int connman_device_set_disconnected(struct connman_device *device,
1388 connman_bool_t disconnected)
1390 DBG("device %p disconnected %d", device, disconnected);
1392 switch (device->mode) {
1393 case CONNMAN_DEVICE_MODE_UNKNOWN:
1395 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1396 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1400 if (device->disconnected == disconnected)
1403 device->disconnected = disconnected;
1405 if (disconnected == TRUE)
1406 force_scan_trigger(device);
1412 * connman_device_get_disconnected:
1413 * @device: device structure
1415 * Get device disconnected state
1417 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1419 return device->disconnected;
1423 * connman_device_set_string:
1424 * @device: device structure
1425 * @key: unique identifier
1426 * @value: string value
1428 * Set string value for specific key
1430 int connman_device_set_string(struct connman_device *device,
1431 const char *key, const char *value)
1433 DBG("device %p key %s value %s", device, key, value);
1435 if (g_str_equal(key, "Address") == TRUE) {
1436 g_free(device->address);
1437 device->address = g_strdup(value);
1438 } else if (g_str_equal(key, "Name") == TRUE) {
1439 g_free(device->name);
1440 device->name = g_strdup(value);
1441 } else if (g_str_equal(key, "Node") == TRUE) {
1442 g_free(device->node);
1443 device->node = g_strdup(value);
1446 return connman_element_set_string(&device->element, key, value);
1450 * connman_device_get_string:
1451 * @device: device structure
1452 * @key: unique identifier
1454 * Get string value for specific key
1456 const char *connman_device_get_string(struct connman_device *device,
1459 DBG("device %p key %s", device, key);
1461 if (g_str_equal(key, "Address") == TRUE)
1462 return device->address;
1463 else if (g_str_equal(key, "Name") == TRUE)
1464 return device->name;
1465 else if (g_str_equal(key, "Node") == TRUE)
1466 return device->node;
1468 return connman_element_get_string(&device->element, key);
1471 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1473 struct connman_device *device = element->device;
1474 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1475 connman_bool_t powered;
1477 DBG("element %p name %s", element, element->name);
1482 device->offlinemode = offlinemode;
1484 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1486 if (device->powered == powered)
1489 if (device->powered_persistent == FALSE)
1492 set_powered(device, powered);
1495 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1497 DBG("offlinmode %d", offlinemode);
1499 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1500 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1502 __connman_notifier_offlinemode(offlinemode);
1507 void __connman_device_increase_connections(struct connman_device *device)
1509 device->connections++;
1512 void __connman_device_decrease_connections(struct connman_device *device)
1514 device->connections--;
1518 * connman_device_add_network:
1519 * @device: device structure
1520 * @network: network structure
1522 * Add new network to the device
1524 int connman_device_add_network(struct connman_device *device,
1525 struct connman_network *network)
1527 const char *identifier = connman_network_get_identifier(network);
1530 DBG("device %p network %p", device, network);
1532 if (identifier == NULL)
1535 switch (device->mode) {
1536 case CONNMAN_DEVICE_MODE_UNKNOWN:
1538 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1539 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1543 __connman_network_set_device(network, device);
1545 err = connman_element_register((struct connman_element *) network,
1548 __connman_network_set_device(network, NULL);
1552 g_hash_table_insert(device->networks, g_strdup(identifier),
1559 * connman_device_get_network:
1560 * @device: device structure
1561 * @identifier: network identifier
1563 * Get network for given identifier
1565 struct connman_network *connman_device_get_network(struct connman_device *device,
1566 const char *identifier)
1568 DBG("device %p identifier %s", device, identifier);
1570 return g_hash_table_lookup(device->networks, identifier);
1574 * connman_device_remove_network:
1575 * @device: device structure
1576 * @identifier: network identifier
1578 * Remove network for given identifier
1580 int connman_device_remove_network(struct connman_device *device,
1581 const char *identifier)
1583 DBG("device %p identifier %s", device, identifier);
1585 g_hash_table_remove(device->networks, identifier);
1590 void connman_device_remove_all_networks(struct connman_device *device)
1592 g_hash_table_remove_all(device->networks);
1595 void __connman_device_set_network(struct connman_device *device,
1596 struct connman_network *network)
1600 if (device->network == network)
1603 if (device->network != NULL)
1604 connman_network_unref(device->network);
1606 if (network != NULL) {
1607 name = connman_network_get_string(network,
1608 CONNMAN_PROPERTY_ID_NAME);
1609 g_free(device->last_network);
1610 device->last_network = g_strdup(name);
1612 device->network = connman_network_ref(network);
1614 g_free(device->last_network);
1615 device->last_network = NULL;
1617 device->network = NULL;
1621 void __connman_device_set_reconnect(struct connman_device *device,
1622 connman_bool_t reconnect)
1624 device->reconnect = reconnect;
1627 connman_bool_t __connman_device_get_reconnect(
1628 struct connman_device *device)
1630 return device->reconnect;
1634 * connman_device_register:
1635 * @device: device structure
1637 * Register device with the system
1639 int connman_device_register(struct connman_device *device)
1641 __connman_storage_load_device(device);
1643 device->offlinemode = __connman_profile_get_offlinemode();
1645 return connman_element_register(&device->element, NULL);
1649 * connman_device_unregister:
1650 * @device: device structure
1652 * Unregister device with the system
1654 void connman_device_unregister(struct connman_device *device)
1656 __connman_storage_save_device(device);
1658 connman_element_unregister(&device->element);
1662 * connman_device_get_data:
1663 * @device: device structure
1665 * Get private device data pointer
1667 void *connman_device_get_data(struct connman_device *device)
1669 return device->driver_data;
1673 * connman_device_set_data:
1674 * @device: device structure
1675 * @data: data pointer
1677 * Set private device data pointer
1679 void connman_device_set_data(struct connman_device *device, void *data)
1681 device->driver_data = data;
1684 static gboolean match_driver(struct connman_device *device,
1685 struct connman_device_driver *driver)
1687 if (device->type == driver->type ||
1688 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1694 static int device_probe(struct connman_element *element)
1696 struct connman_device *device = element->device;
1699 DBG("element %p name %s", element, element->name);
1704 if (device->driver != NULL)
1707 for (list = driver_list; list; list = list->next) {
1708 struct connman_device_driver *driver = list->data;
1710 if (match_driver(device, driver) == FALSE)
1713 DBG("driver %p name %s", driver, driver->name);
1715 if (driver->probe(device) == 0) {
1716 device->driver = driver;
1721 if (device->driver == NULL)
1724 return setup_device(device);
1727 static void device_remove(struct connman_element *element)
1729 struct connman_device *device = element->device;
1731 DBG("element %p name %s", element, element->name);
1736 if (device->driver == NULL)
1739 remove_device(device);
1742 static struct connman_driver device_driver = {
1744 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1745 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1746 .probe = device_probe,
1747 .remove = device_remove,
1750 static int device_load(struct connman_device *device)
1752 const char *ident = __connman_profile_active_ident();
1754 GError *error = NULL;
1756 connman_bool_t powered;
1759 DBG("device %p", device);
1761 keyfile = __connman_storage_open(ident);
1762 if (keyfile == NULL)
1765 identifier = g_strdup_printf("device_%s", device->element.name);
1766 if (identifier == NULL)
1769 powered = g_key_file_get_boolean(keyfile, identifier,
1772 device->powered_persistent = powered;
1773 g_clear_error(&error);
1775 switch (device->mode) {
1776 case CONNMAN_DEVICE_MODE_UNKNOWN:
1778 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1779 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1780 val = g_key_file_get_integer(keyfile, identifier,
1781 "ScanInterval", &error);
1782 if (error == NULL && val > 0)
1783 device->scan_interval = val;
1784 g_clear_error(&error);
1791 __connman_storage_close(ident, keyfile, FALSE);
1796 static int device_save(struct connman_device *device)
1798 const char *ident = __connman_profile_active_ident();
1802 DBG("device %p", device);
1804 keyfile = __connman_storage_open(ident);
1805 if (keyfile == NULL)
1808 identifier = g_strdup_printf("device_%s", device->element.name);
1809 if (identifier == NULL)
1812 g_key_file_set_boolean(keyfile, identifier,
1813 "Powered", device->powered_persistent);
1815 switch (device->mode) {
1816 case CONNMAN_DEVICE_MODE_UNKNOWN:
1818 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1819 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1820 if (device->scan_interval > 0)
1821 g_key_file_set_integer(keyfile, identifier,
1822 "ScanInterval", device->scan_interval);
1829 __connman_storage_close(ident, keyfile, TRUE);
1834 static struct connman_storage device_storage = {
1836 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1837 .device_load = device_load,
1838 .device_save = device_save,
1841 int __connman_device_init(void)
1845 connection = connman_dbus_get_connection();
1847 if (connman_storage_register(&device_storage) < 0)
1848 connman_error("Failed to register device storage");
1850 return connman_driver_register(&device_driver);
1853 void __connman_device_cleanup(void)
1857 connman_driver_unregister(&device_driver);
1859 connman_storage_unregister(&device_storage);
1861 dbus_connection_unref(connection);