5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t offlinemode;
40 connman_bool_t blocked;
41 connman_bool_t powered;
42 connman_bool_t powered_pending;
43 connman_bool_t powered_persistent;
44 connman_bool_t scanning;
45 connman_bool_t disconnected;
46 connman_bool_t reconnect;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
58 struct connman_device_driver *driver;
61 connman_bool_t registered;
64 struct connman_network *network;
71 static gboolean device_scan_trigger(gpointer user_data)
73 struct connman_device *device = user_data;
75 DBG("device %p", device);
77 if (device->driver == NULL) {
78 device->scan_timeout = 0;
82 if (device->driver->scan)
83 device->driver->scan(device);
88 static void clear_scan_trigger(struct connman_device *device)
90 if (device->scan_timeout > 0) {
91 g_source_remove(device->scan_timeout);
92 device->scan_timeout = 0;
96 static void reset_scan_trigger(struct connman_device *device)
98 clear_scan_trigger(device);
100 if (device->scan_interval > 0) {
101 guint interval = device->scan_interval;
102 device->scan_timeout = g_timeout_add_seconds(interval,
103 device_scan_trigger, device);
107 static void force_scan_trigger(struct connman_device *device)
109 clear_scan_trigger(device);
111 device->scan_timeout = g_timeout_add_seconds(5,
112 device_scan_trigger, device);
115 void connman_device_schedule_scan(struct connman_device *device)
117 reset_scan_trigger(device);
120 static const char *type2description(enum connman_device_type type)
123 case CONNMAN_DEVICE_TYPE_UNKNOWN:
124 case CONNMAN_DEVICE_TYPE_VENDOR:
126 case CONNMAN_DEVICE_TYPE_ETHERNET:
128 case CONNMAN_DEVICE_TYPE_WIFI:
130 case CONNMAN_DEVICE_TYPE_WIMAX:
132 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
134 case CONNMAN_DEVICE_TYPE_GPS:
136 case CONNMAN_DEVICE_TYPE_CELLULAR:
137 case CONNMAN_DEVICE_TYPE_MBM:
138 case CONNMAN_DEVICE_TYPE_HSO:
145 static const char *type2string(enum connman_device_type type)
148 case CONNMAN_DEVICE_TYPE_UNKNOWN:
149 case CONNMAN_DEVICE_TYPE_VENDOR:
151 case CONNMAN_DEVICE_TYPE_ETHERNET:
153 case CONNMAN_DEVICE_TYPE_WIFI:
155 case CONNMAN_DEVICE_TYPE_WIMAX:
157 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
159 case CONNMAN_DEVICE_TYPE_GPS:
161 case CONNMAN_DEVICE_TYPE_CELLULAR:
162 case CONNMAN_DEVICE_TYPE_MBM:
163 case CONNMAN_DEVICE_TYPE_HSO:
170 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
172 enum connman_device_type type = connman_device_get_type(device);
175 case CONNMAN_DEVICE_TYPE_UNKNOWN:
176 case CONNMAN_DEVICE_TYPE_VENDOR:
177 case CONNMAN_DEVICE_TYPE_GPS:
179 case CONNMAN_DEVICE_TYPE_ETHERNET:
180 return CONNMAN_SERVICE_TYPE_ETHERNET;
181 case CONNMAN_DEVICE_TYPE_WIFI:
182 return CONNMAN_SERVICE_TYPE_WIFI;
183 case CONNMAN_DEVICE_TYPE_WIMAX:
184 return CONNMAN_SERVICE_TYPE_WIMAX;
185 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
186 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
187 case CONNMAN_DEVICE_TYPE_CELLULAR:
188 case CONNMAN_DEVICE_TYPE_MBM:
189 case CONNMAN_DEVICE_TYPE_HSO:
190 return CONNMAN_SERVICE_TYPE_CELLULAR;
193 return CONNMAN_SERVICE_TYPE_UNKNOWN;
196 static void powered_changed(struct connman_device *device)
198 connman_dbus_property_changed_basic(device->element.path,
199 CONNMAN_DEVICE_INTERFACE, "Powered",
200 DBUS_TYPE_BOOLEAN, &device->powered);
203 static int set_powered(struct connman_device *device, connman_bool_t powered)
205 struct connman_device_driver *driver = device->driver;
206 enum connman_service_type type;
209 DBG("device %p powered %d", device, powered);
211 if (device->powered_pending == powered)
217 type = __connman_device_get_service_type(device);
219 if (powered == TRUE) {
220 if (driver->enable) {
221 device->powered_pending = powered;
223 err = driver->enable(device);
225 __connman_notifier_enable(type);
229 device->powered_pending = powered;
231 device->reconnect = FALSE;
233 clear_scan_trigger(device);
235 g_hash_table_remove_all(device->networks);
237 if (driver->disable) {
238 err = driver->disable(device);
240 __connman_notifier_disable(type);
246 device->powered = powered;
248 if (device->registered == TRUE)
249 powered_changed(device);
255 void __connman_device_list(DBusMessageIter *iter, 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_variable_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)
484 DBusMessageIter iter;
486 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
487 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
491 dbus_message_iter_init_append(signal, &iter);
492 connman_dbus_property_append_variable_array(&iter, "Devices",
493 DBUS_TYPE_OBJECT_PATH, __connman_device_list, NULL);
495 g_dbus_send_message(connection, signal);
498 static int register_interface(struct connman_element *element)
500 struct connman_device *device = element->device;
502 DBG("element %p name %s", element, element->name);
504 if (g_dbus_register_interface(connection, element->path,
505 CONNMAN_DEVICE_INTERFACE,
506 device_methods, device_signals,
507 NULL, device, NULL) == FALSE) {
508 connman_error("Failed to register %s device", element->path);
512 device->registered = TRUE;
514 emit_devices_signal();
519 static void unregister_interface(struct connman_element *element)
521 struct connman_device *device = element->device;
523 DBG("element %p name %s", element, element->name);
525 device->registered = FALSE;
527 emit_devices_signal();
529 g_dbus_unregister_interface(connection, element->path,
530 CONNMAN_DEVICE_INTERFACE);
533 static int setup_device(struct connman_device *device)
535 enum connman_service_type type;
538 DBG("device %p", device);
540 err = register_interface(&device->element);
542 if (device->driver->remove)
543 device->driver->remove(device);
544 device->driver = NULL;
548 type = __connman_device_get_service_type(device);
549 __connman_notifier_register(type);
551 switch (device->mode) {
552 case CONNMAN_DEVICE_MODE_UNKNOWN:
553 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
554 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
558 if (device->offlinemode == FALSE &&
559 device->powered_persistent == TRUE)
560 __connman_device_enable(device);
565 static void probe_driver(struct connman_element *element, gpointer user_data)
567 struct connman_device_driver *driver = user_data;
569 DBG("element %p name %s", element, element->name);
571 if (element->device == NULL)
574 if (element->device->driver != NULL)
577 if (driver->type != element->device->type)
580 if (driver->probe(element->device) < 0)
583 element->device->driver = driver;
585 setup_device(element->device);
588 static void remove_device(struct connman_device *device)
590 enum connman_service_type type;
592 DBG("device %p", device);
594 __connman_device_disable(device);
596 switch (device->mode) {
597 case CONNMAN_DEVICE_MODE_UNKNOWN:
598 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
599 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
603 type = __connman_device_get_service_type(device);
604 __connman_notifier_unregister(type);
606 unregister_interface(&device->element);
608 if (device->driver->remove)
609 device->driver->remove(device);
611 device->driver = NULL;
614 static void remove_driver(struct connman_element *element, gpointer user_data)
616 struct connman_device_driver *driver = user_data;
618 DBG("element %p name %s", element, element->name);
620 if (element->device == NULL)
623 if (element->device->driver == driver)
624 remove_device(element->device);
627 connman_bool_t __connman_device_has_driver(struct connman_device *device)
629 if (device == NULL || device->driver == NULL)
632 return device->registered;
635 static GSList *driver_list = NULL;
637 static gint compare_priority(gconstpointer a, gconstpointer b)
639 const struct connman_device_driver *driver1 = a;
640 const struct connman_device_driver *driver2 = b;
642 return driver2->priority - driver1->priority;
646 * connman_device_driver_register:
647 * @driver: device driver definition
649 * Register a new device driver
651 * Returns: %0 on success
653 int connman_device_driver_register(struct connman_device_driver *driver)
655 DBG("driver %p name %s", driver, driver->name);
657 driver_list = g_slist_insert_sorted(driver_list, driver,
660 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
661 probe_driver, driver);
667 * connman_device_driver_unregister:
668 * @driver: device driver definition
670 * Remove a previously registered device driver
672 void connman_device_driver_unregister(struct connman_device_driver *driver)
674 DBG("driver %p name %s", driver, driver->name);
676 driver_list = g_slist_remove(driver_list, driver);
678 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
679 remove_driver, driver);
682 static void unregister_network(gpointer data)
684 struct connman_network *network = data;
686 DBG("network %p", network);
688 connman_element_unregister((struct connman_element *) network);
690 connman_network_unref(network);
693 static void device_destruct(struct connman_element *element)
695 struct connman_device *device = element->device;
697 DBG("element %p name %s", element, element->name);
699 if (device->timeout > 0) {
700 g_source_remove(device->timeout);
704 clear_scan_trigger(device);
706 if (device->pending != NULL) {
707 dbus_message_unref(device->pending);
708 device->pending = NULL;
711 g_free(device->ident);
712 g_free(device->node);
713 g_free(device->name);
714 g_free(device->address);
715 g_free(device->control);
716 g_free(device->interface);
718 g_free(device->last_network);
720 g_hash_table_destroy(device->networks);
721 device->networks = NULL;
725 * connman_device_create:
726 * @node: device node name (for example an address)
729 * Allocate a new device of given #type and assign the #node name to it.
731 * Returns: a newly-allocated #connman_device structure
733 struct connman_device *connman_device_create(const char *node,
734 enum connman_device_type type)
736 struct connman_device *device;
739 DBG("node %s type %d", node, type);
741 device = g_try_new0(struct connman_device, 1);
745 DBG("device %p", device);
747 __connman_element_initialize(&device->element);
749 device->element.name = g_strdup(node);
750 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
752 device->element.device = device;
753 device->element.destruct = device_destruct;
755 str = type2string(type);
757 connman_element_set_string(&device->element,
758 CONNMAN_PROPERTY_ID_TYPE, str);
760 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
763 device->name = g_strdup(type2description(device->type));
764 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
766 device->powered_persistent = TRUE;
768 device->phyindex = -1;
771 case CONNMAN_DEVICE_TYPE_UNKNOWN:
772 case CONNMAN_DEVICE_TYPE_VENDOR:
773 device->scan_interval = 0;
775 case CONNMAN_DEVICE_TYPE_ETHERNET:
776 case CONNMAN_DEVICE_TYPE_WIFI:
777 device->scan_interval = 300;
779 case CONNMAN_DEVICE_TYPE_WIMAX:
780 device->scan_interval = 0;
782 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
783 device->scan_interval = 0;
785 case CONNMAN_DEVICE_TYPE_GPS:
786 device->scan_interval = 0;
788 case CONNMAN_DEVICE_TYPE_CELLULAR:
789 case CONNMAN_DEVICE_TYPE_MBM:
790 case CONNMAN_DEVICE_TYPE_HSO:
791 device->scan_interval = 0;
795 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
796 g_free, unregister_network);
802 * connman_device_ref:
803 * @device: device structure
805 * Increase reference counter of device
807 struct connman_device *connman_device_ref(struct connman_device *device)
809 if (connman_element_ref(&device->element) == NULL)
816 * connman_device_unref:
817 * @device: device structure
819 * Decrease reference counter of device
821 void connman_device_unref(struct connman_device *device)
823 connman_element_unref(&device->element);
826 const char *__connman_device_get_type(struct connman_device *device)
828 return type2string(device->type);
832 * connman_device_get_type:
833 * @device: device structure
837 enum connman_device_type connman_device_get_type(struct connman_device *device)
843 * connman_device_get_name:
844 * @device: device structure
846 * Get unique name of device
848 const char *connman_device_get_name(struct connman_device *device)
850 return device->element.name;
854 * connman_device_get_path:
855 * @device: device structure
857 * Get path name of device
859 const char *connman_device_get_path(struct connman_device *device)
861 return device->element.path;
865 * connman_device_set_index:
866 * @device: device structure
867 * @index: index number
869 * Set index number of device
871 void connman_device_set_index(struct connman_device *device, int index)
873 device->element.index = index;
877 * connman_device_get_index:
878 * @device: device structure
880 * Get index number of device
882 int connman_device_get_index(struct connman_device *device)
884 return device->element.index;
887 int __connman_device_get_phyindex(struct connman_device *device)
889 return device->phyindex;
892 void __connman_device_set_phyindex(struct connman_device *device,
895 device->phyindex = phyindex;
899 * connman_device_set_interface:
900 * @device: device structure
901 * @interface: interface name
902 * @control: control interface
904 * Set interface name of device
906 void connman_device_set_interface(struct connman_device *device,
907 const char *interface, const char *control)
909 g_free(device->element.devname);
910 device->element.devname = g_strdup(interface);
912 g_free(device->interface);
913 device->interface = g_strdup(interface);
915 g_free(device->control);
916 device->control = g_strdup(control);
918 if (device->name == NULL) {
919 const char *str = type2description(device->type);
920 if (str != NULL && device->interface != NULL)
921 device->name = g_strdup_printf("%s (%s)", str,
926 const char *connman_device_get_control(struct connman_device *device)
928 return device->control;
932 * connman_device_set_ident:
933 * @device: device structure
934 * @ident: unique identifier
936 * Set unique identifier of device
938 void connman_device_set_ident(struct connman_device *device,
941 g_free(device->ident);
942 device->ident = g_strdup(ident);
945 const char *__connman_device_get_ident(struct connman_device *device)
947 return device->ident;
951 * connman_device_set_mode:
952 * @device: device structure
953 * @mode: network mode
955 * Change network mode of device
957 void connman_device_set_mode(struct connman_device *device,
958 enum connman_device_mode mode)
964 * connman_device_get_mode:
965 * @device: device structure
967 * Get network mode of device
969 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
975 * connman_device_set_powered:
976 * @device: device structure
977 * @powered: powered state
979 * Change power state of device
981 int connman_device_set_powered(struct connman_device *device,
982 connman_bool_t powered)
984 enum connman_service_type type;
986 DBG("driver %p powered %d", device, powered);
988 if (device->timeout > 0) {
989 g_source_remove(device->timeout);
993 if (device->pending != NULL) {
994 g_dbus_send_reply(connection, device->pending,
997 dbus_message_unref(device->pending);
998 device->pending = NULL;
1001 if (device->powered == powered)
1004 device->powered = powered;
1005 device->powered_pending = powered;
1007 type = __connman_device_get_service_type(device);
1009 if (device->powered == TRUE)
1010 __connman_notifier_enable(type);
1012 __connman_notifier_disable(type);
1014 if (device->registered == FALSE)
1017 powered_changed(device);
1019 if (powered == FALSE)
1022 reset_scan_trigger(device);
1024 if (device->driver->scan)
1025 device->driver->scan(device);
1030 int __connman_device_set_blocked(struct connman_device *device,
1031 connman_bool_t blocked)
1033 connman_bool_t powered;
1035 DBG("device %p blocked %d", device, blocked);
1037 device->blocked = blocked;
1039 if (device->offlinemode == TRUE)
1042 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1044 if (blocked == FALSE)
1045 powered = device->powered_persistent;
1049 return set_powered(device, powered);
1052 int __connman_device_scan(struct connman_device *device)
1054 if (!device->driver || !device->driver->scan)
1057 if (device->powered == FALSE)
1060 reset_scan_trigger(device);
1062 return device->driver->scan(device);
1065 int __connman_device_enable(struct connman_device *device)
1067 enum connman_service_type type;
1070 DBG("device %p", device);
1072 if (!device->driver || !device->driver->enable)
1075 if (device->powered_pending == TRUE)
1078 device->powered_pending = TRUE;
1080 err = device->driver->enable(device);
1084 device->powered = TRUE;
1086 type = __connman_device_get_service_type(device);
1087 __connman_notifier_enable(type);
1092 int __connman_device_enable_persistent(struct connman_device *device)
1094 DBG("device %p", device);
1096 device->powered_persistent = TRUE;
1098 __connman_storage_save_device(device);
1100 return __connman_device_enable(device);
1103 int __connman_device_disable(struct connman_device *device)
1105 enum connman_service_type type;
1108 DBG("device %p", device);
1110 if (!device->driver || !device->driver->disable)
1113 if (device->powered == FALSE)
1116 if (device->powered_pending == FALSE)
1119 device->powered_pending = FALSE;
1121 device->reconnect = FALSE;
1123 clear_scan_trigger(device);
1125 g_hash_table_remove_all(device->networks);
1127 err = device->driver->disable(device);
1131 device->powered = FALSE;
1133 type = __connman_device_get_service_type(device);
1134 __connman_notifier_disable(type);
1139 int __connman_device_disable_persistent(struct connman_device *device)
1141 DBG("device %p", device);
1143 device->powered_persistent = FALSE;
1145 __connman_storage_save_device(device);
1147 return __connman_device_disable(device);
1150 int __connman_device_disconnect(struct connman_device *device)
1152 GHashTableIter iter;
1153 gpointer key, value;
1155 DBG("device %p", device);
1157 connman_device_set_disconnected(device, TRUE);
1159 g_hash_table_iter_init(&iter, device->networks);
1161 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1162 struct connman_network *network = value;
1164 if (__connman_network_get_connecting(network) == TRUE) {
1166 * Skip network in the process of connecting.
1167 * This is a workaround for WiFi networks serviced
1168 * by the supplicant plugin that hold a reference
1169 * to the network. If we disconnect the network
1170 * here then the referenced object will not be
1171 * registered and usage (like launching DHCP client)
1172 * will fail. There is nothing to be gained by
1173 * removing the network here anyway.
1175 connman_warn("Skipping disconnect of %s",
1176 connman_network_get_identifier(network));
1180 __connman_network_disconnect(network);
1186 static void mark_network_unavailable(gpointer key, gpointer value,
1189 struct connman_network *network = value;
1191 if (connman_network_get_connected(network) == TRUE)
1194 connman_network_set_available(network, FALSE);
1197 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1200 struct connman_network *network = value;
1202 if (connman_network_get_connected(network) == TRUE)
1205 if (connman_network_get_available(network) == TRUE)
1211 void __connman_device_cleanup_networks(struct connman_device *device)
1213 g_hash_table_foreach_remove(device->networks,
1214 remove_unavailable_network, NULL);
1217 static void scanning_changed(struct connman_device *device)
1219 connman_dbus_property_changed_basic(device->element.path,
1220 CONNMAN_DEVICE_INTERFACE, "Scanning",
1221 DBUS_TYPE_BOOLEAN, &device->scanning);
1224 static void mark_network_available(gpointer key, gpointer value,
1227 struct connman_network *network = value;
1229 connman_network_set_available(network, TRUE);
1232 void connman_device_cleanup_scanning(struct connman_device *device)
1234 device->scanning = FALSE;
1236 scanning_changed(device);
1238 g_hash_table_foreach(device->networks,
1239 mark_network_available, NULL);
1243 * connman_device_set_scanning:
1244 * @device: device structure
1245 * @scanning: scanning state
1247 * Change scanning state of device
1249 int connman_device_set_scanning(struct connman_device *device,
1250 connman_bool_t scanning)
1252 DBG("device %p scanning %d", device, scanning);
1254 if (!device->driver || !device->driver->scan)
1257 if (device->scanning == scanning)
1260 device->scanning = scanning;
1262 scanning_changed(device);
1264 if (scanning == TRUE) {
1265 reset_scan_trigger(device);
1267 g_hash_table_foreach(device->networks,
1268 mark_network_unavailable, NULL);
1273 __connman_device_cleanup_networks(device);
1275 if (device->connections > 0)
1278 if (device->disconnected == TRUE)
1281 __connman_service_auto_connect();
1287 * connman_device_set_disconnected:
1288 * @device: device structure
1289 * @disconnected: disconnected state
1291 * Change disconnected state of device (only for device with networks)
1293 int connman_device_set_disconnected(struct connman_device *device,
1294 connman_bool_t disconnected)
1296 DBG("device %p disconnected %d", device, disconnected);
1298 switch (device->mode) {
1299 case CONNMAN_DEVICE_MODE_UNKNOWN:
1301 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1302 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1306 if (device->disconnected == disconnected)
1309 device->disconnected = disconnected;
1311 if (disconnected == TRUE)
1312 force_scan_trigger(device);
1318 * connman_device_get_disconnected:
1319 * @device: device structure
1321 * Get device disconnected state
1323 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1325 return device->disconnected;
1329 * connman_device_set_string:
1330 * @device: device structure
1331 * @key: unique identifier
1332 * @value: string value
1334 * Set string value for specific key
1336 int connman_device_set_string(struct connman_device *device,
1337 const char *key, const char *value)
1339 DBG("device %p key %s value %s", device, key, value);
1341 if (g_str_equal(key, "Address") == TRUE) {
1342 g_free(device->address);
1343 device->address = g_strdup(value);
1344 } else if (g_str_equal(key, "Name") == TRUE) {
1345 g_free(device->name);
1346 device->name = g_strdup(value);
1347 } else if (g_str_equal(key, "Node") == TRUE) {
1348 g_free(device->node);
1349 device->node = g_strdup(value);
1352 return connman_element_set_string(&device->element, key, value);
1356 * connman_device_get_string:
1357 * @device: device structure
1358 * @key: unique identifier
1360 * Get string value for specific key
1362 const char *connman_device_get_string(struct connman_device *device,
1365 DBG("device %p key %s", device, key);
1367 if (g_str_equal(key, "Address") == TRUE)
1368 return device->address;
1369 else if (g_str_equal(key, "Name") == TRUE)
1370 return device->name;
1371 else if (g_str_equal(key, "Node") == TRUE)
1372 return device->node;
1374 return connman_element_get_string(&device->element, key);
1377 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1379 struct connman_device *device = element->device;
1380 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1381 connman_bool_t powered;
1383 DBG("element %p name %s", element, element->name);
1388 device->offlinemode = offlinemode;
1390 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1392 if (device->powered == powered)
1395 if (device->powered_persistent == FALSE)
1398 set_powered(device, powered);
1401 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1403 DBG("offlinmode %d", offlinemode);
1405 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1406 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1408 __connman_notifier_offlinemode(offlinemode);
1413 void __connman_device_increase_connections(struct connman_device *device)
1415 device->connections++;
1418 void __connman_device_decrease_connections(struct connman_device *device)
1420 device->connections--;
1424 * connman_device_add_network:
1425 * @device: device structure
1426 * @network: network structure
1428 * Add new network to the device
1430 int connman_device_add_network(struct connman_device *device,
1431 struct connman_network *network)
1433 const char *identifier = connman_network_get_identifier(network);
1436 DBG("device %p network %p", device, network);
1438 if (identifier == NULL)
1441 switch (device->mode) {
1442 case CONNMAN_DEVICE_MODE_UNKNOWN:
1444 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1445 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1449 __connman_network_set_device(network, device);
1451 err = connman_element_register((struct connman_element *) network,
1454 __connman_network_set_device(network, NULL);
1458 g_hash_table_insert(device->networks, g_strdup(identifier),
1465 * connman_device_get_network:
1466 * @device: device structure
1467 * @identifier: network identifier
1469 * Get network for given identifier
1471 struct connman_network *connman_device_get_network(struct connman_device *device,
1472 const char *identifier)
1474 DBG("device %p identifier %s", device, identifier);
1476 return g_hash_table_lookup(device->networks, identifier);
1480 * connman_device_remove_network:
1481 * @device: device structure
1482 * @identifier: network identifier
1484 * Remove network for given identifier
1486 int connman_device_remove_network(struct connman_device *device,
1487 const char *identifier)
1489 DBG("device %p identifier %s", device, identifier);
1491 g_hash_table_remove(device->networks, identifier);
1496 void connman_device_remove_all_networks(struct connman_device *device)
1498 g_hash_table_remove_all(device->networks);
1501 void __connman_device_set_network(struct connman_device *device,
1502 struct connman_network *network)
1506 if (device->network == network)
1509 if (device->network != NULL)
1510 connman_network_unref(device->network);
1512 if (network != NULL) {
1513 name = connman_network_get_string(network,
1514 CONNMAN_PROPERTY_ID_NAME);
1515 g_free(device->last_network);
1516 device->last_network = g_strdup(name);
1518 device->network = connman_network_ref(network);
1520 g_free(device->last_network);
1521 device->last_network = NULL;
1523 device->network = NULL;
1527 void __connman_device_set_reconnect(struct connman_device *device,
1528 connman_bool_t reconnect)
1530 device->reconnect = reconnect;
1533 connman_bool_t __connman_device_get_reconnect(
1534 struct connman_device *device)
1536 return device->reconnect;
1540 * connman_device_register:
1541 * @device: device structure
1543 * Register device with the system
1545 int connman_device_register(struct connman_device *device)
1547 __connman_storage_load_device(device);
1549 device->offlinemode = __connman_profile_get_offlinemode();
1551 return connman_element_register(&device->element, NULL);
1555 * connman_device_unregister:
1556 * @device: device structure
1558 * Unregister device with the system
1560 void connman_device_unregister(struct connman_device *device)
1562 __connman_storage_save_device(device);
1564 connman_element_unregister(&device->element);
1568 * connman_device_get_data:
1569 * @device: device structure
1571 * Get private device data pointer
1573 void *connman_device_get_data(struct connman_device *device)
1575 return device->driver_data;
1579 * connman_device_set_data:
1580 * @device: device structure
1581 * @data: data pointer
1583 * Set private device data pointer
1585 void connman_device_set_data(struct connman_device *device, void *data)
1587 device->driver_data = data;
1590 static gboolean match_driver(struct connman_device *device,
1591 struct connman_device_driver *driver)
1593 if (device->type == driver->type ||
1594 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1600 static int device_probe(struct connman_element *element)
1602 struct connman_device *device = element->device;
1605 DBG("element %p name %s", element, element->name);
1610 if (device->driver != NULL)
1613 for (list = driver_list; list; list = list->next) {
1614 struct connman_device_driver *driver = list->data;
1616 if (match_driver(device, driver) == FALSE)
1619 DBG("driver %p name %s", driver, driver->name);
1621 if (driver->probe(device) == 0) {
1622 device->driver = driver;
1627 if (device->driver == NULL)
1630 return setup_device(device);
1633 static void device_remove(struct connman_element *element)
1635 struct connman_device *device = element->device;
1637 DBG("element %p name %s", element, element->name);
1642 if (device->driver == NULL)
1645 remove_device(device);
1648 static struct connman_driver device_driver = {
1650 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1651 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1652 .probe = device_probe,
1653 .remove = device_remove,
1656 static int device_load(struct connman_device *device)
1658 const char *ident = __connman_profile_active_ident();
1660 GError *error = NULL;
1662 connman_bool_t powered;
1665 DBG("device %p", device);
1667 keyfile = __connman_storage_open_profile(ident);
1668 if (keyfile == NULL)
1671 identifier = g_strdup_printf("device_%s", device->element.name);
1672 if (identifier == NULL)
1675 powered = g_key_file_get_boolean(keyfile, identifier,
1678 device->powered_persistent = powered;
1679 g_clear_error(&error);
1681 switch (device->mode) {
1682 case CONNMAN_DEVICE_MODE_UNKNOWN:
1684 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1685 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1686 val = g_key_file_get_integer(keyfile, identifier,
1687 "ScanInterval", &error);
1688 if (error == NULL && val > 0)
1689 device->scan_interval = val;
1690 g_clear_error(&error);
1697 __connman_storage_close_profile(ident, keyfile, FALSE);
1702 static int device_save(struct connman_device *device)
1704 const char *ident = __connman_profile_active_ident();
1708 DBG("device %p", device);
1710 keyfile = __connman_storage_open_profile(ident);
1711 if (keyfile == NULL)
1714 identifier = g_strdup_printf("device_%s", device->element.name);
1715 if (identifier == NULL)
1718 g_key_file_set_boolean(keyfile, identifier,
1719 "Powered", device->powered_persistent);
1721 switch (device->mode) {
1722 case CONNMAN_DEVICE_MODE_UNKNOWN:
1724 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1725 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1726 if (device->scan_interval > 0)
1727 g_key_file_set_integer(keyfile, identifier,
1728 "ScanInterval", device->scan_interval);
1735 __connman_storage_close_profile(ident, keyfile, TRUE);
1740 static struct connman_storage device_storage = {
1742 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1743 .device_load = device_load,
1744 .device_save = device_save,
1747 int __connman_device_init(void)
1751 connection = connman_dbus_get_connection();
1753 if (connman_storage_register(&device_storage) < 0)
1754 connman_error("Failed to register device storage");
1756 return connman_driver_register(&device_driver);
1759 void __connman_device_cleanup(void)
1763 connman_driver_unregister(&device_driver);
1765 connman_storage_unregister(&device_storage);
1767 dbus_connection_unref(connection);