5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
137 case CONNMAN_DEVICE_TYPE_MBM:
138 case CONNMAN_DEVICE_TYPE_HSO:
145 static const char *type2string(enum connman_device_type type)
148 case CONNMAN_DEVICE_TYPE_UNKNOWN:
149 case CONNMAN_DEVICE_TYPE_VENDOR:
151 case CONNMAN_DEVICE_TYPE_ETHERNET:
153 case CONNMAN_DEVICE_TYPE_WIFI:
155 case CONNMAN_DEVICE_TYPE_WIMAX:
157 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
159 case CONNMAN_DEVICE_TYPE_GPS:
161 case CONNMAN_DEVICE_TYPE_CELLULAR:
162 case CONNMAN_DEVICE_TYPE_MBM:
163 case CONNMAN_DEVICE_TYPE_HSO:
170 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
172 enum connman_device_type type = connman_device_get_type(device);
175 case CONNMAN_DEVICE_TYPE_UNKNOWN:
176 case CONNMAN_DEVICE_TYPE_VENDOR:
177 case CONNMAN_DEVICE_TYPE_GPS:
179 case CONNMAN_DEVICE_TYPE_ETHERNET:
180 return CONNMAN_SERVICE_TYPE_ETHERNET;
181 case CONNMAN_DEVICE_TYPE_WIFI:
182 return CONNMAN_SERVICE_TYPE_WIFI;
183 case CONNMAN_DEVICE_TYPE_WIMAX:
184 return CONNMAN_SERVICE_TYPE_WIMAX;
185 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
186 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
187 case CONNMAN_DEVICE_TYPE_CELLULAR:
188 case CONNMAN_DEVICE_TYPE_MBM:
189 case CONNMAN_DEVICE_TYPE_HSO:
190 return CONNMAN_SERVICE_TYPE_CELLULAR;
193 return CONNMAN_SERVICE_TYPE_UNKNOWN;
196 static void powered_changed(struct connman_device *device)
198 connman_dbus_property_changed_basic(device->element.path,
199 CONNMAN_DEVICE_INTERFACE, "Powered",
200 DBUS_TYPE_BOOLEAN, &device->powered);
203 static int set_powered(struct connman_device *device, connman_bool_t powered)
205 struct connman_device_driver *driver = device->driver;
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, void *user_data)
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(DBusMessageIter *iter, void *user_data)
271 struct connman_device *device = user_data;
273 g_hash_table_foreach(device->networks, append_path, iter);
276 static DBusMessage *get_properties(DBusConnection *conn,
277 DBusMessage *msg, void *data)
279 struct connman_device *device = data;
281 DBusMessageIter array, dict;
284 DBG("conn %p", conn);
286 if (__connman_security_check_privilege(msg,
287 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
288 return __connman_error_permission_denied(msg);
290 reply = dbus_message_new_method_return(msg);
294 dbus_message_iter_init_append(reply, &array);
296 connman_dbus_dict_open(&array, &dict);
298 if (device->name != NULL)
299 connman_dbus_dict_append_basic(&dict, "Name",
300 DBUS_TYPE_STRING, &device->name);
302 str = type2string(device->type);
304 connman_dbus_dict_append_basic(&dict, "Type",
305 DBUS_TYPE_STRING, &str);
307 if (device->address != NULL)
308 connman_dbus_dict_append_basic(&dict, "Address",
309 DBUS_TYPE_STRING, &device->address);
311 if (device->interface != NULL)
312 connman_dbus_dict_append_basic(&dict, "Interface",
313 DBUS_TYPE_STRING, &device->interface);
315 connman_dbus_dict_append_basic(&dict, "Powered",
316 DBUS_TYPE_BOOLEAN, &device->powered);
318 if (device->driver && device->driver->scan)
319 connman_dbus_dict_append_basic(&dict, "Scanning",
320 DBUS_TYPE_BOOLEAN, &device->scanning);
322 switch (device->mode) {
323 case CONNMAN_DEVICE_MODE_UNKNOWN:
325 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
326 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
327 if (device->scan_interval > 0)
328 connman_dbus_dict_append_basic(&dict, "ScanInterval",
329 DBUS_TYPE_UINT16, &device->scan_interval);
331 connman_dbus_dict_append_array(&dict, "Networks",
332 DBUS_TYPE_OBJECT_PATH, append_networks, device);
336 connman_dbus_dict_close(&array, &dict);
341 static gboolean powered_timeout(gpointer user_data)
343 struct connman_device *device = user_data;
345 DBG("device %p", device);
349 if (device->pending != NULL) {
352 reply = __connman_error_operation_timeout(device->pending);
354 g_dbus_send_message(connection, reply);
356 dbus_message_unref(device->pending);
357 device->pending = NULL;
363 static DBusMessage *set_property(DBusConnection *conn,
364 DBusMessage *msg, void *data)
366 struct connman_device *device = data;
367 DBusMessageIter iter, value;
371 DBG("conn %p", conn);
373 if (dbus_message_iter_init(msg, &iter) == FALSE)
374 return __connman_error_invalid_arguments(msg);
376 dbus_message_iter_get_basic(&iter, &name);
377 dbus_message_iter_next(&iter);
378 dbus_message_iter_recurse(&iter, &value);
380 if (__connman_security_check_privilege(msg,
381 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
382 return __connman_error_permission_denied(msg);
384 type = dbus_message_iter_get_arg_type(&value);
386 if (g_str_equal(name, "Powered") == TRUE) {
387 connman_bool_t powered;
390 if (type != DBUS_TYPE_BOOLEAN)
391 return __connman_error_invalid_arguments(msg);
393 dbus_message_iter_get_basic(&value, &powered);
395 device->powered_persistent = powered;
397 __connman_storage_save_device(device);
399 if (device->powered == powered)
400 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
402 if (device->pending != NULL)
403 return __connman_error_in_progress(msg);
405 err = set_powered(device, powered);
407 if (err != -EINPROGRESS)
408 return __connman_error_failed(msg, -err);
410 device->pending = dbus_message_ref(msg);
412 device->timeout = g_timeout_add_seconds(15,
413 powered_timeout, device);
417 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
418 connman_uint16_t interval;
420 switch (device->mode) {
421 case CONNMAN_DEVICE_MODE_UNKNOWN:
422 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
423 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
427 if (type != DBUS_TYPE_UINT16)
428 return __connman_error_invalid_arguments(msg);
430 dbus_message_iter_get_basic(&value, &interval);
432 if (device->scan_interval != interval) {
433 device->scan_interval = interval;
435 __connman_storage_save_device(device);
437 reset_scan_trigger(device);
440 return __connman_error_invalid_property(msg);
442 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
445 static DBusMessage *propose_scan(DBusConnection *conn,
446 DBusMessage *msg, void *data)
448 struct connman_device *device = data;
451 DBG("conn %p", conn);
453 switch (device->mode) {
454 case CONNMAN_DEVICE_MODE_UNKNOWN:
455 return __connman_error_not_supported(msg);
456 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
457 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
461 err = __connman_device_scan(device);
463 return __connman_error_failed(msg, -err);
465 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
468 static GDBusMethodTable device_methods[] = {
469 { "GetProperties", "", "a{sv}", get_properties },
470 { "SetProperty", "sv", "", set_property,
471 G_DBUS_METHOD_FLAG_ASYNC },
472 { "ProposeScan", "", "", propose_scan },
476 static GDBusSignalTable device_signals[] = {
477 { "PropertyChanged", "sv" },
481 static void emit_devices_signal(void)
483 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
484 CONNMAN_MANAGER_INTERFACE, "Devices",
485 DBUS_TYPE_OBJECT_PATH, __connman_device_list, NULL);
488 static int register_interface(struct connman_element *element)
490 struct connman_device *device = element->device;
492 DBG("element %p name %s", element, element->name);
494 if (g_dbus_register_interface(connection, element->path,
495 CONNMAN_DEVICE_INTERFACE,
496 device_methods, device_signals,
497 NULL, device, NULL) == FALSE) {
498 connman_error("Failed to register %s device", element->path);
502 device->registered = TRUE;
504 emit_devices_signal();
509 static void unregister_interface(struct connman_element *element)
511 struct connman_device *device = element->device;
513 DBG("element %p name %s", element, element->name);
515 device->registered = FALSE;
517 emit_devices_signal();
519 g_dbus_unregister_interface(connection, element->path,
520 CONNMAN_DEVICE_INTERFACE);
523 static int setup_device(struct connman_device *device)
525 enum connman_service_type type;
528 DBG("device %p", device);
530 err = register_interface(&device->element);
532 if (device->driver->remove)
533 device->driver->remove(device);
534 device->driver = NULL;
538 type = __connman_device_get_service_type(device);
539 __connman_notifier_register(type);
541 switch (device->mode) {
542 case CONNMAN_DEVICE_MODE_UNKNOWN:
543 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
544 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
548 if (__connman_udev_get_blocked(device->phyindex) == TRUE)
551 if (device->offlinemode == FALSE &&
552 device->powered_persistent == TRUE)
553 __connman_device_enable(device);
558 static void probe_driver(struct connman_element *element, gpointer user_data)
560 struct connman_device_driver *driver = user_data;
562 DBG("element %p name %s", element, element->name);
564 if (element->device == NULL)
567 if (element->device->driver != NULL)
570 if (driver->type != element->device->type)
573 if (driver->probe(element->device) < 0)
576 element->device->driver = driver;
578 setup_device(element->device);
581 static void remove_device(struct connman_device *device)
583 enum connman_service_type type;
585 DBG("device %p", device);
587 __connman_device_disable(device);
589 switch (device->mode) {
590 case CONNMAN_DEVICE_MODE_UNKNOWN:
591 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
592 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
596 type = __connman_device_get_service_type(device);
597 __connman_notifier_unregister(type);
599 unregister_interface(&device->element);
601 if (device->driver->remove)
602 device->driver->remove(device);
604 device->driver = NULL;
607 static void remove_driver(struct connman_element *element, gpointer user_data)
609 struct connman_device_driver *driver = user_data;
611 DBG("element %p name %s", element, element->name);
613 if (element->device == NULL)
616 if (element->device->driver == driver)
617 remove_device(element->device);
620 connman_bool_t __connman_device_has_driver(struct connman_device *device)
622 if (device == NULL || device->driver == NULL)
625 return device->registered;
628 static GSList *driver_list = NULL;
630 static gint compare_priority(gconstpointer a, gconstpointer b)
632 const struct connman_device_driver *driver1 = a;
633 const struct connman_device_driver *driver2 = b;
635 return driver2->priority - driver1->priority;
639 * connman_device_driver_register:
640 * @driver: device driver definition
642 * Register a new device driver
644 * Returns: %0 on success
646 int connman_device_driver_register(struct connman_device_driver *driver)
648 DBG("driver %p name %s", driver, driver->name);
650 driver_list = g_slist_insert_sorted(driver_list, driver,
653 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
654 probe_driver, driver);
660 * connman_device_driver_unregister:
661 * @driver: device driver definition
663 * Remove a previously registered device driver
665 void connman_device_driver_unregister(struct connman_device_driver *driver)
667 DBG("driver %p name %s", driver, driver->name);
669 driver_list = g_slist_remove(driver_list, driver);
671 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
672 remove_driver, driver);
675 static void unregister_network(gpointer data)
677 struct connman_network *network = data;
679 DBG("network %p", network);
681 connman_element_unregister((struct connman_element *) network);
683 connman_network_unref(network);
686 static void device_destruct(struct connman_element *element)
688 struct connman_device *device = element->device;
690 DBG("element %p name %s", element, element->name);
692 if (device->timeout > 0) {
693 g_source_remove(device->timeout);
697 clear_scan_trigger(device);
699 if (device->pending != NULL) {
700 dbus_message_unref(device->pending);
701 device->pending = NULL;
704 g_free(device->ident);
705 g_free(device->node);
706 g_free(device->name);
707 g_free(device->address);
708 g_free(device->control);
709 g_free(device->interface);
711 g_free(device->last_network);
713 g_hash_table_destroy(device->networks);
714 device->networks = NULL;
718 * connman_device_create:
719 * @node: device node name (for example an address)
722 * Allocate a new device of given #type and assign the #node name to it.
724 * Returns: a newly-allocated #connman_device structure
726 struct connman_device *connman_device_create(const char *node,
727 enum connman_device_type type)
729 struct connman_device *device;
732 DBG("node %s type %d", node, type);
734 device = g_try_new0(struct connman_device, 1);
738 DBG("device %p", device);
740 __connman_element_initialize(&device->element);
742 device->element.name = g_strdup(node);
743 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
745 device->element.device = device;
746 device->element.destruct = device_destruct;
748 str = type2string(type);
750 connman_element_set_string(&device->element,
751 CONNMAN_PROPERTY_ID_TYPE, str);
753 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
756 device->name = g_strdup(type2description(device->type));
757 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
759 device->powered_persistent = TRUE;
761 device->phyindex = -1;
764 case CONNMAN_DEVICE_TYPE_UNKNOWN:
765 case CONNMAN_DEVICE_TYPE_VENDOR:
766 device->scan_interval = 0;
768 case CONNMAN_DEVICE_TYPE_ETHERNET:
769 case CONNMAN_DEVICE_TYPE_WIFI:
770 device->scan_interval = 300;
772 case CONNMAN_DEVICE_TYPE_WIMAX:
773 device->scan_interval = 0;
775 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
776 device->scan_interval = 0;
778 case CONNMAN_DEVICE_TYPE_GPS:
779 device->scan_interval = 0;
781 case CONNMAN_DEVICE_TYPE_CELLULAR:
782 case CONNMAN_DEVICE_TYPE_MBM:
783 case CONNMAN_DEVICE_TYPE_HSO:
784 device->scan_interval = 0;
788 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
789 g_free, unregister_network);
795 * connman_device_ref:
796 * @device: device structure
798 * Increase reference counter of device
800 struct connman_device *connman_device_ref(struct connman_device *device)
802 if (connman_element_ref(&device->element) == NULL)
809 * connman_device_unref:
810 * @device: device structure
812 * Decrease reference counter of device
814 void connman_device_unref(struct connman_device *device)
816 connman_element_unref(&device->element);
819 const char *__connman_device_get_type(struct connman_device *device)
821 return type2string(device->type);
825 * connman_device_get_type:
826 * @device: device structure
830 enum connman_device_type connman_device_get_type(struct connman_device *device)
836 * connman_device_get_name:
837 * @device: device structure
839 * Get unique name of device
841 const char *connman_device_get_name(struct connman_device *device)
843 return device->element.name;
847 * connman_device_get_path:
848 * @device: device structure
850 * Get path name of device
852 const char *connman_device_get_path(struct connman_device *device)
854 return device->element.path;
858 * connman_device_set_index:
859 * @device: device structure
860 * @index: index number
862 * Set index number of device
864 void connman_device_set_index(struct connman_device *device, int index)
866 device->element.index = index;
870 * connman_device_get_index:
871 * @device: device structure
873 * Get index number of device
875 int connman_device_get_index(struct connman_device *device)
877 return device->element.index;
880 int __connman_device_get_phyindex(struct connman_device *device)
882 return device->phyindex;
885 void __connman_device_set_phyindex(struct connman_device *device,
888 device->phyindex = phyindex;
892 * connman_device_set_interface:
893 * @device: device structure
894 * @interface: interface name
895 * @control: control interface
897 * Set interface name of device
899 void connman_device_set_interface(struct connman_device *device,
900 const char *interface, const char *control)
902 g_free(device->element.devname);
903 device->element.devname = g_strdup(interface);
905 g_free(device->interface);
906 device->interface = g_strdup(interface);
908 g_free(device->control);
909 device->control = g_strdup(control);
911 if (device->name == NULL) {
912 const char *str = type2description(device->type);
913 if (str != NULL && device->interface != NULL)
914 device->name = g_strdup_printf("%s (%s)", str,
919 const char *connman_device_get_control(struct connman_device *device)
921 return device->control;
925 * connman_device_set_ident:
926 * @device: device structure
927 * @ident: unique identifier
929 * Set unique identifier of device
931 void connman_device_set_ident(struct connman_device *device,
934 g_free(device->ident);
935 device->ident = g_strdup(ident);
938 const char *__connman_device_get_ident(struct connman_device *device)
940 return device->ident;
944 * connman_device_set_mode:
945 * @device: device structure
946 * @mode: network mode
948 * Change network mode of device
950 void connman_device_set_mode(struct connman_device *device,
951 enum connman_device_mode mode)
957 * connman_device_get_mode:
958 * @device: device structure
960 * Get network mode of device
962 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
968 * connman_device_set_powered:
969 * @device: device structure
970 * @powered: powered state
972 * Change power state of device
974 int connman_device_set_powered(struct connman_device *device,
975 connman_bool_t powered)
977 enum connman_service_type type;
979 DBG("driver %p powered %d", device, powered);
981 if (device->timeout > 0) {
982 g_source_remove(device->timeout);
986 if (device->pending != NULL) {
987 g_dbus_send_reply(connection, device->pending,
990 dbus_message_unref(device->pending);
991 device->pending = NULL;
994 if (device->powered == powered)
997 device->powered = powered;
998 device->powered_pending = powered;
1000 type = __connman_device_get_service_type(device);
1002 if (device->powered == TRUE)
1003 __connman_notifier_enable(type);
1005 __connman_notifier_disable(type);
1007 if (device->registered == FALSE)
1010 powered_changed(device);
1012 if (powered == FALSE)
1015 reset_scan_trigger(device);
1017 if (device->driver->scan)
1018 device->driver->scan(device);
1023 int __connman_device_set_blocked(struct connman_device *device,
1024 connman_bool_t blocked)
1026 connman_bool_t powered;
1028 DBG("device %p blocked %d", device, blocked);
1030 device->blocked = blocked;
1032 if (device->offlinemode == TRUE)
1035 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1037 if (blocked == FALSE)
1038 powered = device->powered_persistent;
1042 return set_powered(device, powered);
1045 int __connman_device_scan(struct connman_device *device)
1047 if (!device->driver || !device->driver->scan)
1050 if (device->powered == FALSE)
1053 reset_scan_trigger(device);
1055 return device->driver->scan(device);
1058 int __connman_device_enable(struct connman_device *device)
1060 enum connman_service_type type;
1063 DBG("device %p", device);
1065 if (!device->driver || !device->driver->enable)
1068 if (device->powered_pending == TRUE)
1071 device->powered_pending = TRUE;
1073 err = device->driver->enable(device);
1077 device->powered = TRUE;
1079 type = __connman_device_get_service_type(device);
1080 __connman_notifier_enable(type);
1085 int __connman_device_enable_persistent(struct connman_device *device)
1087 DBG("device %p", device);
1089 device->powered_persistent = TRUE;
1091 __connman_storage_save_device(device);
1093 return __connman_device_enable(device);
1096 int __connman_device_disable(struct connman_device *device)
1098 enum connman_service_type type;
1101 DBG("device %p", device);
1103 if (!device->driver || !device->driver->disable)
1106 if (device->powered == FALSE)
1109 if (device->powered_pending == FALSE)
1112 device->powered_pending = FALSE;
1114 device->reconnect = FALSE;
1116 clear_scan_trigger(device);
1118 g_hash_table_remove_all(device->networks);
1120 err = device->driver->disable(device);
1124 device->powered = FALSE;
1126 type = __connman_device_get_service_type(device);
1127 __connman_notifier_disable(type);
1132 int __connman_device_disable_persistent(struct connman_device *device)
1134 DBG("device %p", device);
1136 device->powered_persistent = FALSE;
1138 __connman_storage_save_device(device);
1140 return __connman_device_disable(device);
1143 int __connman_device_disconnect(struct connman_device *device)
1145 GHashTableIter iter;
1146 gpointer key, value;
1148 DBG("device %p", device);
1150 connman_device_set_disconnected(device, TRUE);
1152 g_hash_table_iter_init(&iter, device->networks);
1154 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1155 struct connman_network *network = value;
1157 if (__connman_network_get_connecting(network) == TRUE) {
1159 * Skip network in the process of connecting.
1160 * This is a workaround for WiFi networks serviced
1161 * by the supplicant plugin that hold a reference
1162 * to the network. If we disconnect the network
1163 * here then the referenced object will not be
1164 * registered and usage (like launching DHCP client)
1165 * will fail. There is nothing to be gained by
1166 * removing the network here anyway.
1168 connman_warn("Skipping disconnect of %s",
1169 connman_network_get_identifier(network));
1173 __connman_network_disconnect(network);
1179 static void mark_network_unavailable(gpointer key, gpointer value,
1182 struct connman_network *network = value;
1184 if (connman_network_get_connected(network) == TRUE)
1187 connman_network_set_available(network, FALSE);
1190 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1193 struct connman_network *network = value;
1195 if (connman_network_get_connected(network) == TRUE)
1198 if (connman_network_get_available(network) == TRUE)
1204 void __connman_device_cleanup_networks(struct connman_device *device)
1206 g_hash_table_foreach_remove(device->networks,
1207 remove_unavailable_network, NULL);
1210 static void scanning_changed(struct connman_device *device)
1212 connman_dbus_property_changed_basic(device->element.path,
1213 CONNMAN_DEVICE_INTERFACE, "Scanning",
1214 DBUS_TYPE_BOOLEAN, &device->scanning);
1217 static void mark_network_available(gpointer key, gpointer value,
1220 struct connman_network *network = value;
1222 connman_network_set_available(network, TRUE);
1225 void connman_device_cleanup_scanning(struct connman_device *device)
1227 device->scanning = FALSE;
1229 scanning_changed(device);
1231 g_hash_table_foreach(device->networks,
1232 mark_network_available, NULL);
1236 * connman_device_set_scanning:
1237 * @device: device structure
1238 * @scanning: scanning state
1240 * Change scanning state of device
1242 int connman_device_set_scanning(struct connman_device *device,
1243 connman_bool_t scanning)
1245 DBG("device %p scanning %d", device, scanning);
1247 if (!device->driver || !device->driver->scan)
1250 if (device->scanning == scanning)
1253 device->scanning = scanning;
1255 scanning_changed(device);
1257 if (scanning == TRUE) {
1258 reset_scan_trigger(device);
1260 g_hash_table_foreach(device->networks,
1261 mark_network_unavailable, NULL);
1266 __connman_device_cleanup_networks(device);
1268 if (device->connections > 0)
1271 if (device->disconnected == TRUE)
1274 __connman_service_auto_connect();
1280 * connman_device_set_disconnected:
1281 * @device: device structure
1282 * @disconnected: disconnected state
1284 * Change disconnected state of device (only for device with networks)
1286 int connman_device_set_disconnected(struct connman_device *device,
1287 connman_bool_t disconnected)
1289 DBG("device %p disconnected %d", device, disconnected);
1291 switch (device->mode) {
1292 case CONNMAN_DEVICE_MODE_UNKNOWN:
1294 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1295 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1299 if (device->disconnected == disconnected)
1302 device->disconnected = disconnected;
1304 if (disconnected == TRUE)
1305 force_scan_trigger(device);
1311 * connman_device_get_disconnected:
1312 * @device: device structure
1314 * Get device disconnected state
1316 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1318 return device->disconnected;
1322 * connman_device_set_string:
1323 * @device: device structure
1324 * @key: unique identifier
1325 * @value: string value
1327 * Set string value for specific key
1329 int connman_device_set_string(struct connman_device *device,
1330 const char *key, const char *value)
1332 DBG("device %p key %s value %s", device, key, value);
1334 if (g_str_equal(key, "Address") == TRUE) {
1335 g_free(device->address);
1336 device->address = g_strdup(value);
1337 } else if (g_str_equal(key, "Name") == TRUE) {
1338 g_free(device->name);
1339 device->name = g_strdup(value);
1340 } else if (g_str_equal(key, "Node") == TRUE) {
1341 g_free(device->node);
1342 device->node = g_strdup(value);
1345 return connman_element_set_string(&device->element, key, value);
1349 * connman_device_get_string:
1350 * @device: device structure
1351 * @key: unique identifier
1353 * Get string value for specific key
1355 const char *connman_device_get_string(struct connman_device *device,
1358 DBG("device %p key %s", device, key);
1360 if (g_str_equal(key, "Address") == TRUE)
1361 return device->address;
1362 else if (g_str_equal(key, "Name") == TRUE)
1363 return device->name;
1364 else if (g_str_equal(key, "Node") == TRUE)
1365 return device->node;
1367 return connman_element_get_string(&device->element, key);
1370 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1372 struct connman_device *device = element->device;
1373 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1374 connman_bool_t powered;
1376 DBG("element %p name %s", element, element->name);
1381 device->offlinemode = offlinemode;
1383 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1385 if (device->powered == powered)
1388 if (device->powered_persistent == FALSE)
1391 set_powered(device, powered);
1394 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1396 DBG("offlinmode %d", offlinemode);
1398 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1399 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1401 __connman_notifier_offlinemode(offlinemode);
1406 void __connman_device_increase_connections(struct connman_device *device)
1408 device->connections++;
1411 void __connman_device_decrease_connections(struct connman_device *device)
1413 device->connections--;
1417 * connman_device_add_network:
1418 * @device: device structure
1419 * @network: network structure
1421 * Add new network to the device
1423 int connman_device_add_network(struct connman_device *device,
1424 struct connman_network *network)
1426 const char *identifier = connman_network_get_identifier(network);
1429 DBG("device %p network %p", device, network);
1431 if (identifier == NULL)
1434 switch (device->mode) {
1435 case CONNMAN_DEVICE_MODE_UNKNOWN:
1437 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1438 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1442 __connman_network_set_device(network, device);
1444 err = connman_element_register((struct connman_element *) network,
1447 __connman_network_set_device(network, NULL);
1451 g_hash_table_insert(device->networks, g_strdup(identifier),
1458 * connman_device_get_network:
1459 * @device: device structure
1460 * @identifier: network identifier
1462 * Get network for given identifier
1464 struct connman_network *connman_device_get_network(struct connman_device *device,
1465 const char *identifier)
1467 DBG("device %p identifier %s", device, identifier);
1469 return g_hash_table_lookup(device->networks, identifier);
1473 * connman_device_remove_network:
1474 * @device: device structure
1475 * @identifier: network identifier
1477 * Remove network for given identifier
1479 int connman_device_remove_network(struct connman_device *device,
1480 const char *identifier)
1482 DBG("device %p identifier %s", device, identifier);
1484 g_hash_table_remove(device->networks, identifier);
1489 void connman_device_remove_all_networks(struct connman_device *device)
1491 g_hash_table_remove_all(device->networks);
1494 void __connman_device_set_network(struct connman_device *device,
1495 struct connman_network *network)
1499 if (device->network == network)
1502 if (device->network != NULL)
1503 connman_network_unref(device->network);
1505 if (network != NULL) {
1506 name = connman_network_get_string(network,
1507 CONNMAN_PROPERTY_ID_NAME);
1508 g_free(device->last_network);
1509 device->last_network = g_strdup(name);
1511 device->network = connman_network_ref(network);
1513 g_free(device->last_network);
1514 device->last_network = NULL;
1516 device->network = NULL;
1520 void __connman_device_set_reconnect(struct connman_device *device,
1521 connman_bool_t reconnect)
1523 device->reconnect = reconnect;
1526 connman_bool_t __connman_device_get_reconnect(
1527 struct connman_device *device)
1529 return device->reconnect;
1533 * connman_device_register:
1534 * @device: device structure
1536 * Register device with the system
1538 int connman_device_register(struct connman_device *device)
1540 __connman_storage_load_device(device);
1542 device->offlinemode = __connman_profile_get_offlinemode();
1544 return connman_element_register(&device->element, NULL);
1548 * connman_device_unregister:
1549 * @device: device structure
1551 * Unregister device with the system
1553 void connman_device_unregister(struct connman_device *device)
1555 __connman_storage_save_device(device);
1557 connman_element_unregister(&device->element);
1561 * connman_device_get_data:
1562 * @device: device structure
1564 * Get private device data pointer
1566 void *connman_device_get_data(struct connman_device *device)
1568 return device->driver_data;
1572 * connman_device_set_data:
1573 * @device: device structure
1574 * @data: data pointer
1576 * Set private device data pointer
1578 void connman_device_set_data(struct connman_device *device, void *data)
1580 device->driver_data = data;
1583 static gboolean match_driver(struct connman_device *device,
1584 struct connman_device_driver *driver)
1586 if (device->type == driver->type ||
1587 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1593 static int device_probe(struct connman_element *element)
1595 struct connman_device *device = element->device;
1598 DBG("element %p name %s", element, element->name);
1603 if (device->driver != NULL)
1606 for (list = driver_list; list; list = list->next) {
1607 struct connman_device_driver *driver = list->data;
1609 if (match_driver(device, driver) == FALSE)
1612 DBG("driver %p name %s", driver, driver->name);
1614 if (driver->probe(device) == 0) {
1615 device->driver = driver;
1620 if (device->driver == NULL)
1623 return setup_device(device);
1626 static void device_remove(struct connman_element *element)
1628 struct connman_device *device = element->device;
1630 DBG("element %p name %s", element, element->name);
1635 if (device->driver == NULL)
1638 remove_device(device);
1641 static struct connman_driver device_driver = {
1643 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1644 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1645 .probe = device_probe,
1646 .remove = device_remove,
1649 static int device_load(struct connman_device *device)
1651 const char *ident = __connman_profile_active_ident();
1653 GError *error = NULL;
1655 connman_bool_t powered;
1658 DBG("device %p", device);
1660 keyfile = __connman_storage_open_profile(ident);
1661 if (keyfile == NULL)
1664 identifier = g_strdup_printf("device_%s", device->element.name);
1665 if (identifier == NULL)
1668 powered = g_key_file_get_boolean(keyfile, identifier,
1671 device->powered_persistent = powered;
1672 g_clear_error(&error);
1674 switch (device->mode) {
1675 case CONNMAN_DEVICE_MODE_UNKNOWN:
1677 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1678 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1679 val = g_key_file_get_integer(keyfile, identifier,
1680 "ScanInterval", &error);
1681 if (error == NULL && val > 0)
1682 device->scan_interval = val;
1683 g_clear_error(&error);
1690 __connman_storage_close_profile(ident, keyfile, FALSE);
1695 static int device_save(struct connman_device *device)
1697 const char *ident = __connman_profile_active_ident();
1701 DBG("device %p", device);
1703 keyfile = __connman_storage_open_profile(ident);
1704 if (keyfile == NULL)
1707 identifier = g_strdup_printf("device_%s", device->element.name);
1708 if (identifier == NULL)
1711 g_key_file_set_boolean(keyfile, identifier,
1712 "Powered", device->powered_persistent);
1714 switch (device->mode) {
1715 case CONNMAN_DEVICE_MODE_UNKNOWN:
1717 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1718 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1719 if (device->scan_interval > 0)
1720 g_key_file_set_integer(keyfile, identifier,
1721 "ScanInterval", device->scan_interval);
1728 __connman_storage_close_profile(ident, keyfile, TRUE);
1733 static struct connman_storage device_storage = {
1735 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1736 .device_load = device_load,
1737 .device_save = device_save,
1740 int __connman_device_init(void)
1744 connection = connman_dbus_get_connection();
1746 if (connman_storage_register(&device_storage) < 0)
1747 connman_error("Failed to register device storage");
1749 return connman_driver_register(&device_driver);
1752 void __connman_device_cleanup(void)
1756 connman_driver_unregister(&device_driver);
1758 connman_storage_unregister(&device_storage);
1760 dbus_connection_unref(connection);