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 carrier;
46 connman_bool_t scanning;
47 connman_bool_t disconnected;
48 connman_uint16_t scan_interval;
56 unsigned int connections;
58 struct connman_ipconfig *ipconfig;
60 struct connman_device_driver *driver;
63 connman_bool_t registered;
66 struct connman_network *network;
73 static gboolean device_scan_trigger(gpointer user_data)
75 struct connman_device *device = user_data;
77 DBG("device %p", device);
79 if (device->driver == NULL) {
80 device->scan_timeout = 0;
84 if (device->driver->scan)
85 device->driver->scan(device);
90 static void clear_scan_trigger(struct connman_device *device)
92 if (device->scan_timeout > 0) {
93 g_source_remove(device->scan_timeout);
94 device->scan_timeout = 0;
98 static void reset_scan_trigger(struct connman_device *device)
100 clear_scan_trigger(device);
102 if (device->scan_interval > 0) {
103 guint interval = device->scan_interval;
104 device->scan_timeout = g_timeout_add_seconds(interval,
105 device_scan_trigger, device);
109 static const char *type2description(enum connman_device_type type)
112 case CONNMAN_DEVICE_TYPE_UNKNOWN:
113 case CONNMAN_DEVICE_TYPE_VENDOR:
115 case CONNMAN_DEVICE_TYPE_ETHERNET:
117 case CONNMAN_DEVICE_TYPE_WIFI:
119 case CONNMAN_DEVICE_TYPE_WIMAX:
121 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
123 case CONNMAN_DEVICE_TYPE_GPS:
125 case CONNMAN_DEVICE_TYPE_MBM:
126 case CONNMAN_DEVICE_TYPE_HSO:
127 case CONNMAN_DEVICE_TYPE_NOZOMI:
128 case CONNMAN_DEVICE_TYPE_HUAWEI:
129 case CONNMAN_DEVICE_TYPE_NOVATEL:
136 static const char *type2string(enum connman_device_type type)
139 case CONNMAN_DEVICE_TYPE_UNKNOWN:
140 case CONNMAN_DEVICE_TYPE_VENDOR:
142 case CONNMAN_DEVICE_TYPE_ETHERNET:
144 case CONNMAN_DEVICE_TYPE_WIFI:
146 case CONNMAN_DEVICE_TYPE_WIMAX:
148 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
150 case CONNMAN_DEVICE_TYPE_GPS:
152 case CONNMAN_DEVICE_TYPE_MBM:
153 case CONNMAN_DEVICE_TYPE_HSO:
154 case CONNMAN_DEVICE_TYPE_HUAWEI:
155 case CONNMAN_DEVICE_TYPE_NOZOMI:
156 case CONNMAN_DEVICE_TYPE_NOVATEL:
163 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
165 enum connman_device_type type = connman_device_get_type(device);
168 case CONNMAN_DEVICE_TYPE_UNKNOWN:
169 case CONNMAN_DEVICE_TYPE_VENDOR:
170 case CONNMAN_DEVICE_TYPE_GPS:
171 case CONNMAN_DEVICE_TYPE_NOZOMI:
172 case CONNMAN_DEVICE_TYPE_HUAWEI:
173 case CONNMAN_DEVICE_TYPE_NOVATEL:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_MBM:
184 case CONNMAN_DEVICE_TYPE_HSO:
185 return CONNMAN_SERVICE_TYPE_CELLULAR;
188 return CONNMAN_SERVICE_TYPE_UNKNOWN;
191 static int set_connected(struct connman_device *device,
192 connman_bool_t connected)
194 if (connected == TRUE) {
195 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
196 struct connman_element *element;
198 device->disconnected = TRUE;
200 switch (device->element.ipv4.method) {
201 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
202 case CONNMAN_IPCONFIG_METHOD_OFF:
204 case CONNMAN_IPCONFIG_METHOD_STATIC:
205 type = CONNMAN_ELEMENT_TYPE_IPV4;
207 case CONNMAN_IPCONFIG_METHOD_DHCP:
208 type = CONNMAN_ELEMENT_TYPE_DHCP;
212 element = connman_element_create(NULL);
213 if (element != NULL) {
214 struct connman_service *service;
216 element->type = type;
217 element->index = device->element.index;
219 if (connman_element_register(element,
220 &device->element) < 0)
221 connman_element_unref(element);
223 device->disconnected = FALSE;
225 service = __connman_service_lookup_from_device(device);
226 __connman_service_indicate_state(service,
227 CONNMAN_SERVICE_STATE_CONFIGURATION);
230 struct connman_service *service;
232 connman_element_unregister_children(&device->element);
234 device->disconnected = TRUE;
236 service = __connman_service_lookup_from_device(device);
237 __connman_service_indicate_state(service,
238 CONNMAN_SERVICE_STATE_IDLE);
241 if (connected == TRUE) {
242 enum connman_service_type type;
244 type = __connman_device_get_service_type(device);
245 __connman_notifier_connect(type);
247 enum connman_service_type type;
249 type = __connman_device_get_service_type(device);
250 __connman_notifier_disconnect(type);
256 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
258 struct connman_service *service;
261 __connman_profile_add_device(device);
263 __connman_profile_remove_device(device);
265 service = __connman_service_lookup_from_device(device);
266 __connman_service_set_carrier(service, carrier);
268 return set_connected(device, carrier);
271 static int powered_changed(struct connman_device *device)
274 DBusMessageIter entry, value;
275 const char *key = "Powered";
277 signal = dbus_message_new_signal(device->element.path,
278 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
282 dbus_message_iter_init_append(signal, &entry);
284 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
286 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
287 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
288 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
290 dbus_message_iter_close_container(&entry, &value);
292 g_dbus_send_message(connection, signal);
297 static int set_powered(struct connman_device *device, connman_bool_t powered)
299 struct connman_device_driver *driver = device->driver;
300 enum connman_service_type type;
303 DBG("device %p powered %d", device, powered);
305 if (device->powered_pending == powered)
308 device->powered_pending = powered;
313 type = __connman_device_get_service_type(device);
315 if (powered == TRUE) {
316 if (driver->enable) {
317 err = driver->enable(device);
319 __connman_notifier_enable(type);
323 clear_scan_trigger(device);
325 g_hash_table_remove_all(device->networks);
327 set_carrier(device, FALSE);
329 if (driver->disable) {
330 err = driver->disable(device);
332 __connman_notifier_disable(type);
338 device->powered = powered;
339 powered_changed(device);
345 static void append_path(gpointer key, gpointer value, gpointer user_data)
347 struct connman_element *element = value;
348 DBusMessageIter *iter = user_data;
350 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
354 static void append_networks(struct connman_device *device,
355 DBusMessageIter *entry)
357 DBusMessageIter value, iter;
358 const char *key = "Networks";
360 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
362 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
363 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
366 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
367 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
368 g_hash_table_foreach(device->networks, append_path, &iter);
369 dbus_message_iter_close_container(&value, &iter);
371 dbus_message_iter_close_container(entry, &value);
374 static DBusMessage *get_properties(DBusConnection *conn,
375 DBusMessage *msg, void *data)
377 struct connman_device *device = data;
379 DBusMessageIter array, dict, entry;
382 DBG("conn %p", conn);
384 if (__connman_security_check_privilege(msg,
385 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
386 return __connman_error_permission_denied(msg);
388 reply = dbus_message_new_method_return(msg);
392 dbus_message_iter_init_append(reply, &array);
394 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
395 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
396 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
397 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
399 if (device->name != NULL)
400 connman_dbus_dict_append_variant(&dict, "Name",
401 DBUS_TYPE_STRING, &device->name);
403 str = type2string(device->type);
405 connman_dbus_dict_append_variant(&dict, "Type",
406 DBUS_TYPE_STRING, &str);
408 if (device->address != NULL)
409 connman_dbus_dict_append_variant(&dict, "Address",
410 DBUS_TYPE_STRING, &device->address);
412 if (device->interface != NULL)
413 connman_dbus_dict_append_variant(&dict, "Interface",
414 DBUS_TYPE_STRING, &device->interface);
416 connman_dbus_dict_append_variant(&dict, "Powered",
417 DBUS_TYPE_BOOLEAN, &device->powered);
419 if (device->driver && device->driver->scan)
420 connman_dbus_dict_append_variant(&dict, "Scanning",
421 DBUS_TYPE_BOOLEAN, &device->scanning);
423 switch (device->mode) {
424 case CONNMAN_DEVICE_MODE_UNKNOWN:
426 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
427 __connman_element_append_ipv4(&device->element, &dict);
429 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
430 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
431 if (device->scan_interval > 0)
432 connman_dbus_dict_append_variant(&dict, "ScanInterval",
433 DBUS_TYPE_UINT16, &device->scan_interval);
435 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
437 append_networks(device, &entry);
438 dbus_message_iter_close_container(&dict, &entry);
442 dbus_message_iter_close_container(&array, &dict);
447 static gboolean powered_timeout(gpointer user_data)
449 struct connman_device *device = user_data;
451 DBG("device %p", device);
455 if (device->pending != NULL) {
458 reply = __connman_error_operation_timeout(device->pending);
460 g_dbus_send_message(connection, reply);
462 dbus_message_unref(device->pending);
463 device->pending = NULL;
469 static DBusMessage *set_property(DBusConnection *conn,
470 DBusMessage *msg, void *data)
472 struct connman_device *device = data;
473 DBusMessageIter iter, value;
477 DBG("conn %p", conn);
479 if (dbus_message_iter_init(msg, &iter) == FALSE)
480 return __connman_error_invalid_arguments(msg);
482 dbus_message_iter_get_basic(&iter, &name);
483 dbus_message_iter_next(&iter);
484 dbus_message_iter_recurse(&iter, &value);
486 if (__connman_security_check_privilege(msg,
487 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
488 return __connman_error_permission_denied(msg);
490 type = dbus_message_iter_get_arg_type(&value);
492 if (g_str_equal(name, "Powered") == TRUE) {
493 connman_bool_t powered;
496 if (type != DBUS_TYPE_BOOLEAN)
497 return __connman_error_invalid_arguments(msg);
499 dbus_message_iter_get_basic(&value, &powered);
501 device->powered_persistent = powered;
503 __connman_storage_save_device(device);
505 if (device->powered == powered)
506 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
508 if (device->pending != NULL)
509 return __connman_error_in_progress(msg);
511 err = set_powered(device, powered);
513 if (err != -EINPROGRESS)
514 return __connman_error_failed(msg, -err);
516 device->pending = dbus_message_ref(msg);
518 device->timeout = g_timeout_add_seconds(15,
519 powered_timeout, device);
523 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
524 connman_uint16_t interval;
526 switch (device->mode) {
527 case CONNMAN_DEVICE_MODE_UNKNOWN:
528 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
529 return __connman_error_invalid_arguments(msg);
530 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
531 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
535 if (type != DBUS_TYPE_UINT16)
536 return __connman_error_invalid_arguments(msg);
538 dbus_message_iter_get_basic(&value, &interval);
540 if (device->scan_interval != interval) {
541 device->scan_interval = interval;
543 __connman_storage_save_device(device);
545 reset_scan_trigger(device);
547 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
550 switch (device->mode) {
551 case CONNMAN_DEVICE_MODE_UNKNOWN:
552 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
553 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
554 return __connman_error_invalid_arguments(msg);
555 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
559 err = __connman_ipconfig_set_ipv4(device->ipconfig,
562 return __connman_error_failed(msg, -err);
564 return __connman_error_invalid_property(msg);
566 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
569 static DBusMessage *propose_scan(DBusConnection *conn,
570 DBusMessage *msg, void *data)
572 struct connman_device *device = data;
575 DBG("conn %p", conn);
577 switch (device->mode) {
578 case CONNMAN_DEVICE_MODE_UNKNOWN:
579 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
580 return __connman_error_not_supported(msg);
581 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
582 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
586 err = __connman_device_scan(device);
588 return __connman_error_failed(msg, -err);
590 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
593 static GDBusMethodTable device_methods[] = {
594 { "GetProperties", "", "a{sv}", get_properties },
595 { "SetProperty", "sv", "", set_property,
596 G_DBUS_METHOD_FLAG_ASYNC },
597 { "ProposeScan", "", "", propose_scan },
601 static GDBusSignalTable device_signals[] = {
602 { "PropertyChanged", "sv" },
606 static void append_devices(DBusMessageIter *entry)
608 DBusMessageIter value, iter;
609 const char *key = "Devices";
611 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
613 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
614 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
617 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
618 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
619 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
620 dbus_message_iter_close_container(&value, &iter);
622 dbus_message_iter_close_container(entry, &value);
625 static void emit_devices_signal(void)
628 DBusMessageIter entry;
630 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
631 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
635 dbus_message_iter_init_append(signal, &entry);
637 append_devices(&entry);
639 g_dbus_send_message(connection, signal);
642 static int register_interface(struct connman_element *element)
644 struct connman_device *device = element->device;
646 DBG("element %p name %s", element, element->name);
648 if (g_dbus_register_interface(connection, element->path,
649 CONNMAN_DEVICE_INTERFACE,
650 device_methods, device_signals,
651 NULL, device, NULL) == FALSE) {
652 connman_error("Failed to register %s device", element->path);
656 device->registered = TRUE;
658 emit_devices_signal();
663 static void unregister_interface(struct connman_element *element)
665 struct connman_device *device = element->device;
667 DBG("element %p name %s", element, element->name);
669 device->registered = FALSE;
671 emit_devices_signal();
673 g_dbus_unregister_interface(connection, element->path,
674 CONNMAN_DEVICE_INTERFACE);
677 static int setup_device(struct connman_device *device)
681 DBG("device %p", device);
683 err = register_interface(&device->element);
685 if (device->driver->remove)
686 device->driver->remove(device);
687 device->driver = NULL;
691 switch (device->mode) {
692 case CONNMAN_DEVICE_MODE_UNKNOWN:
693 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
694 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
696 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
697 if (device->carrier == TRUE && device->secondary == FALSE)
698 __connman_profile_add_device(device);
702 if (device->offlinemode == FALSE &&
703 device->powered_persistent == TRUE)
704 __connman_device_enable(device);
709 static void probe_driver(struct connman_element *element, gpointer user_data)
711 struct connman_device_driver *driver = user_data;
713 DBG("element %p name %s", element, element->name);
715 if (element->device == NULL)
718 if (element->device->driver != NULL)
721 if (driver->type != element->device->type)
724 if (driver->probe(element->device) < 0)
727 element->device->driver = driver;
729 setup_device(element->device);
732 static void remove_device(struct connman_device *device)
734 DBG("device %p", device);
736 __connman_device_disable(device);
738 switch (device->mode) {
739 case CONNMAN_DEVICE_MODE_UNKNOWN:
740 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
741 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
743 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
744 if (device->secondary == FALSE)
745 __connman_profile_remove_device(device);
749 unregister_interface(&device->element);
751 if (device->driver->remove)
752 device->driver->remove(device);
754 device->driver = NULL;
757 static void remove_driver(struct connman_element *element, gpointer user_data)
759 struct connman_device_driver *driver = user_data;
761 DBG("element %p name %s", element, element->name);
763 if (element->device == NULL)
766 if (element->device->driver == driver)
767 remove_device(element->device);
770 connman_bool_t __connman_device_has_driver(struct connman_device *device)
772 if (device == NULL || device->driver == NULL)
775 return device->registered;
778 static GSList *driver_list = NULL;
780 static gint compare_priority(gconstpointer a, gconstpointer b)
782 const struct connman_device_driver *driver1 = a;
783 const struct connman_device_driver *driver2 = b;
785 return driver2->priority - driver1->priority;
789 * connman_device_driver_register:
790 * @driver: device driver definition
792 * Register a new device driver
794 * Returns: %0 on success
796 int connman_device_driver_register(struct connman_device_driver *driver)
798 DBG("driver %p name %s", driver, driver->name);
800 driver_list = g_slist_insert_sorted(driver_list, driver,
803 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
804 probe_driver, driver);
810 * connman_device_driver_unregister:
811 * @driver: device driver definition
813 * Remove a previously registered device driver
815 void connman_device_driver_unregister(struct connman_device_driver *driver)
817 DBG("driver %p name %s", driver, driver->name);
819 driver_list = g_slist_remove(driver_list, driver);
821 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
822 remove_driver, driver);
825 static void unregister_network(gpointer data)
827 struct connman_network *network = data;
829 DBG("network %p", network);
831 connman_element_unregister((struct connman_element *) network);
833 connman_network_unref(network);
836 static void device_destruct(struct connman_element *element)
838 struct connman_device *device = element->device;
840 DBG("element %p name %s", element, element->name);
842 if (device->timeout > 0) {
843 g_source_remove(device->timeout);
847 if (device->pending != NULL) {
848 dbus_message_unref(device->pending);
849 device->pending = NULL;
852 g_free(device->ident);
853 g_free(device->node);
854 g_free(device->name);
855 g_free(device->address);
856 g_free(device->control);
857 g_free(device->interface);
859 connman_ipconfig_unref(device->ipconfig);
861 g_free(device->last_network);
863 g_hash_table_destroy(device->networks);
864 device->networks = NULL;
868 * connman_device_create:
869 * @node: device node name (for example an address)
872 * Allocate a new device of given #type and assign the #node name to it.
874 * Returns: a newly-allocated #connman_device structure
876 struct connman_device *connman_device_create(const char *node,
877 enum connman_device_type type)
879 struct connman_device *device;
882 DBG("node %s type %d", node, type);
884 device = g_try_new0(struct connman_device, 1);
888 DBG("device %p", device);
890 __connman_element_initialize(&device->element);
892 device->element.name = g_strdup(node);
893 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
895 device->element.device = device;
896 device->element.destruct = device_destruct;
898 str = type2string(type);
900 connman_element_set_string(&device->element,
901 CONNMAN_PROPERTY_ID_TYPE, str);
903 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
906 device->name = g_strdup(type2description(device->type));
907 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
908 device->secondary = FALSE;
910 device->powered_persistent = TRUE;
912 device->phyindex = -1;
915 case CONNMAN_DEVICE_TYPE_UNKNOWN:
916 case CONNMAN_DEVICE_TYPE_VENDOR:
917 device->scan_interval = 0;
919 case CONNMAN_DEVICE_TYPE_ETHERNET:
920 case CONNMAN_DEVICE_TYPE_WIFI:
921 device->scan_interval = 300;
923 case CONNMAN_DEVICE_TYPE_WIMAX:
924 device->scan_interval = 0;
926 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
927 device->scan_interval = 0;
929 case CONNMAN_DEVICE_TYPE_GPS:
930 device->scan_interval = 0;
932 case CONNMAN_DEVICE_TYPE_MBM:
933 case CONNMAN_DEVICE_TYPE_HSO:
934 case CONNMAN_DEVICE_TYPE_NOZOMI:
935 case CONNMAN_DEVICE_TYPE_HUAWEI:
936 case CONNMAN_DEVICE_TYPE_NOVATEL:
937 device->scan_interval = 0;
941 device->ipconfig = connman_ipconfig_create();
942 if (device->ipconfig == NULL) {
943 connman_device_unref(device);
947 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
948 g_free, unregister_network);
954 * connman_device_ref:
955 * @device: device structure
957 * Increase reference counter of device
959 struct connman_device *connman_device_ref(struct connman_device *device)
961 if (connman_element_ref(&device->element) == NULL)
968 * connman_device_unref:
969 * @device: device structure
971 * Decrease reference counter of device
973 void connman_device_unref(struct connman_device *device)
975 connman_element_unref(&device->element);
978 const char *__connman_device_get_type(struct connman_device *device)
980 return type2string(device->type);
984 * connman_device_get_type:
985 * @device: device structure
989 enum connman_device_type connman_device_get_type(struct connman_device *device)
995 * connman_device_get_name:
996 * @device: device structure
998 * Get unique name of device
1000 const char *connman_device_get_name(struct connman_device *device)
1002 return device->element.name;
1006 * connman_device_get_path:
1007 * @device: device structure
1009 * Get path name of device
1011 const char *connman_device_get_path(struct connman_device *device)
1013 return device->element.path;
1017 * connman_device_set_index:
1018 * @device: device structure
1019 * @index: index number
1021 * Set index number of device
1023 void connman_device_set_index(struct connman_device *device, int index)
1025 device->element.index = index;
1029 * connman_device_get_index:
1030 * @device: device structure
1032 * Get index number of device
1034 int connman_device_get_index(struct connman_device *device)
1036 return device->element.index;
1039 int __connman_device_get_phyindex(struct connman_device *device)
1041 return device->phyindex;
1044 void __connman_device_set_phyindex(struct connman_device *device,
1047 device->phyindex = phyindex;
1051 * connman_device_set_interface:
1052 * @device: device structure
1053 * @interface: interface name
1054 * @control: control interface
1056 * Set interface name of device
1058 void connman_device_set_interface(struct connman_device *device,
1059 const char *interface, const char *control)
1061 g_free(device->element.devname);
1062 device->element.devname = g_strdup(interface);
1064 g_free(device->interface);
1065 device->interface = g_strdup(interface);
1067 g_free(device->control);
1068 device->control = g_strdup(control);
1070 if (device->name == NULL) {
1071 const char *str = type2description(device->type);
1072 if (str != NULL && device->interface != NULL)
1073 device->name = g_strdup_printf("%s (%s)", str,
1078 const char *connman_device_get_control(struct connman_device *device)
1080 return device->control;
1084 * connman_device_set_ident:
1085 * @device: device structure
1086 * @ident: unique identifier
1088 * Set unique identifier of device
1090 void connman_device_set_ident(struct connman_device *device,
1093 g_free(device->ident);
1094 device->ident = g_strdup(ident);
1097 const char *__connman_device_get_ident(struct connman_device *device)
1099 return device->ident;
1103 * connman_device_set_mode:
1104 * @device: device structure
1105 * @mode: network mode
1107 * Change network mode of device
1109 void connman_device_set_mode(struct connman_device *device,
1110 enum connman_device_mode mode)
1112 device->mode = mode;
1116 * connman_device_get_mode:
1117 * @device: device structure
1119 * Get network mode of device
1121 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1123 return device->mode;
1127 * connman_device_set_secondary:
1128 * @device: device structure
1129 * @secondary: secondary value
1131 * Change secondary value of device
1133 void connman_device_set_secondary(struct connman_device *device,
1134 connman_bool_t secondary)
1136 device->secondary = secondary;
1140 * connman_device_get_secondary:
1141 * @device: device structure
1143 * Get secondary value of device
1145 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1147 return device->secondary;
1151 * connman_device_set_powered:
1152 * @device: device structure
1153 * @powered: powered state
1155 * Change power state of device
1157 int connman_device_set_powered(struct connman_device *device,
1158 connman_bool_t powered)
1160 enum connman_service_type type;
1162 DBG("driver %p powered %d", device, powered);
1164 if (device->timeout > 0) {
1165 g_source_remove(device->timeout);
1166 device->timeout = 0;
1169 if (device->pending != NULL) {
1170 g_dbus_send_reply(connection, device->pending,
1173 dbus_message_unref(device->pending);
1174 device->pending = NULL;
1177 if (device->powered == powered)
1180 device->powered = powered;
1181 device->powered_pending = powered;
1183 type = __connman_device_get_service_type(device);
1185 if (device->powered == TRUE)
1186 __connman_notifier_enable(type);
1188 __connman_notifier_disable(type);
1190 if (device->registered == FALSE)
1193 powered_changed(device);
1195 if (powered == FALSE)
1198 reset_scan_trigger(device);
1200 if (device->driver->scan)
1201 device->driver->scan(device);
1206 int __connman_device_set_blocked(struct connman_device *device,
1207 connman_bool_t blocked)
1209 connman_bool_t powered;
1211 DBG("device %p blocked %d", device, blocked);
1213 device->blocked = blocked;
1215 if (device->offlinemode == TRUE)
1218 if (blocked == FALSE)
1219 powered = device->powered_persistent;
1223 return set_powered(device, powered);
1227 * connman_device_set_carrier:
1228 * @device: device structure
1229 * @carrier: carrier state
1231 * Change carrier state of device (only for device without scanning)
1233 int connman_device_set_carrier(struct connman_device *device,
1234 connman_bool_t carrier)
1236 DBG("device %p carrier %d", device, carrier);
1238 switch (device->mode) {
1239 case CONNMAN_DEVICE_MODE_UNKNOWN:
1240 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1241 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1243 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1247 if (device->carrier == carrier)
1250 device->carrier = carrier;
1252 return set_carrier(device, device->carrier);
1255 int __connman_device_scan(struct connman_device *device)
1257 if (!device->driver || !device->driver->scan)
1260 if (device->powered == FALSE)
1263 return device->driver->scan(device);
1266 int __connman_device_enable(struct connman_device *device)
1268 enum connman_service_type type;
1271 DBG("device %p", device);
1273 if (!device->driver || !device->driver->enable)
1276 if (device->powered_pending == TRUE)
1279 device->powered_pending = TRUE;
1281 err = device->driver->enable(device);
1285 device->powered = TRUE;
1287 type = __connman_device_get_service_type(device);
1288 __connman_notifier_enable(type);
1293 int __connman_device_enable_persistent(struct connman_device *device)
1295 DBG("device %p", device);
1297 device->powered_persistent = TRUE;
1299 __connman_storage_save_device(device);
1301 return __connman_device_enable(device);
1304 int __connman_device_disable(struct connman_device *device)
1306 enum connman_service_type type;
1309 DBG("device %p", device);
1311 if (!device->driver || !device->driver->disable)
1314 if (device->powered == FALSE)
1317 if (device->powered_pending == FALSE)
1320 device->powered_pending = FALSE;
1322 clear_scan_trigger(device);
1324 g_hash_table_remove_all(device->networks);
1326 err = device->driver->disable(device);
1330 device->powered = FALSE;
1332 type = __connman_device_get_service_type(device);
1333 __connman_notifier_disable(type);
1338 int __connman_device_disable_persistent(struct connman_device *device)
1340 DBG("device %p", device);
1342 device->powered_persistent = FALSE;
1344 __connman_storage_save_device(device);
1346 return __connman_device_disable(device);
1349 int __connman_device_connect(struct connman_device *device)
1351 DBG("device %p", device);
1353 if (device->disconnected == FALSE)
1356 if (device->driver && device->driver->connect)
1357 device->driver->connect(device);
1362 int __connman_device_disconnect(struct connman_device *device)
1364 GHashTableIter iter;
1365 gpointer key, value;
1367 DBG("device %p", device);
1369 connman_device_set_disconnected(device, TRUE);
1371 g_hash_table_iter_init(&iter, device->networks);
1373 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1374 struct connman_network *network = value;
1376 __connman_network_disconnect(network);
1379 if (device->driver && device->driver->disconnect)
1380 device->driver->disconnect(device);
1385 static void mark_network_unavailable(gpointer key, gpointer value,
1388 struct connman_network *network = value;
1390 if (connman_network_get_connected(network) == TRUE)
1393 connman_network_set_available(network, FALSE);
1396 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1399 struct connman_network *network = value;
1401 if (connman_network_get_connected(network) == TRUE)
1404 if (connman_network_get_available(network) == TRUE)
1410 void __connman_device_cleanup_networks(struct connman_device *device)
1412 g_hash_table_foreach_remove(device->networks,
1413 remove_unavailable_network, NULL);
1417 * connman_device_set_scanning:
1418 * @device: device structure
1419 * @scanning: scanning state
1421 * Change scanning state of device
1423 int connman_device_set_scanning(struct connman_device *device,
1424 connman_bool_t scanning)
1426 DBusMessage *signal;
1427 DBusMessageIter entry, value;
1428 const char *key = "Scanning";
1430 DBG("device %p scanning %d", device, scanning);
1432 if (!device->driver || !device->driver->scan)
1435 if (device->scanning == scanning)
1438 device->scanning = scanning;
1440 signal = dbus_message_new_signal(device->element.path,
1441 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1445 dbus_message_iter_init_append(signal, &entry);
1447 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1449 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1450 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1451 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1452 dbus_message_iter_close_container(&entry, &value);
1454 g_dbus_send_message(connection, signal);
1456 if (scanning == TRUE) {
1457 reset_scan_trigger(device);
1459 g_hash_table_foreach(device->networks,
1460 mark_network_unavailable, NULL);
1465 __connman_device_cleanup_networks(device);
1467 if (device->connections > 0)
1470 if (device->disconnected == TRUE)
1473 __connman_service_auto_connect();
1479 * connman_device_set_disconnected:
1480 * @device: device structure
1481 * @disconnected: disconnected state
1483 * Change disconnected state of device (only for device with networks)
1485 int connman_device_set_disconnected(struct connman_device *device,
1486 connman_bool_t disconnected)
1488 DBG("device %p disconnected %d", device, disconnected);
1490 switch (device->mode) {
1491 case CONNMAN_DEVICE_MODE_UNKNOWN:
1492 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1494 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1495 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1499 if (device->disconnected == disconnected)
1502 device->disconnected = disconnected;
1508 * connman_device_get_disconnected:
1509 * @device: device structure
1511 * Get device disconnected state
1513 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1515 return device->disconnected;
1519 * connman_device_set_connected:
1520 * @device: device structure
1521 * @connected: connected state
1523 * Change connected state of device (for Ethernet like devices)
1525 int connman_device_set_connected(struct connman_device *device,
1526 connman_bool_t connected)
1528 DBG("device %p connected %d", device, connected);
1530 switch (device->mode) {
1531 case CONNMAN_DEVICE_MODE_UNKNOWN:
1532 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1533 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1535 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1539 if (device->carrier == FALSE)
1542 return set_connected(device, connected);
1546 * connman_device_set_string:
1547 * @device: device structure
1548 * @key: unique identifier
1549 * @value: string value
1551 * Set string value for specific key
1553 int connman_device_set_string(struct connman_device *device,
1554 const char *key, const char *value)
1556 DBG("device %p key %s value %s", device, key, value);
1558 if (g_str_equal(key, "Address") == TRUE) {
1559 g_free(device->address);
1560 device->address = g_strdup(value);
1561 } else if (g_str_equal(key, "Name") == TRUE) {
1562 g_free(device->name);
1563 device->name = g_strdup(value);
1564 } else if (g_str_equal(key, "Node") == TRUE) {
1565 g_free(device->node);
1566 device->node = g_strdup(value);
1569 return connman_element_set_string(&device->element, key, value);
1573 * connman_device_get_string:
1574 * @device: device structure
1575 * @key: unique identifier
1577 * Get string value for specific key
1579 const char *connman_device_get_string(struct connman_device *device,
1582 DBG("device %p key %s", device, key);
1584 if (g_str_equal(key, "Address") == TRUE)
1585 return device->address;
1586 else if (g_str_equal(key, "Name") == TRUE)
1587 return device->name;
1588 else if (g_str_equal(key, "Node") == TRUE)
1589 return device->node;
1591 return connman_element_get_string(&device->element, key);
1594 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1596 struct connman_device *device = element->device;
1597 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1598 connman_bool_t powered;
1600 DBG("element %p name %s", element, element->name);
1605 device->offlinemode = offlinemode;
1607 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1609 if (device->powered == powered)
1612 if (device->powered_persistent == FALSE)
1615 set_powered(device, powered);
1618 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1620 DBG("offlinmode %d", offlinemode);
1622 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1623 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1625 __connman_notifier_offlinemode(offlinemode);
1630 void __connman_device_increase_connections(struct connman_device *device)
1632 enum connman_service_type type;
1634 device->connections++;
1636 type = __connman_device_get_service_type(device);
1637 __connman_notifier_connect(type);
1640 void __connman_device_decrease_connections(struct connman_device *device)
1642 enum connman_service_type type;
1644 device->connections--;
1646 type = __connman_device_get_service_type(device);
1647 __connman_notifier_disconnect(type);
1651 * connman_device_add_network:
1652 * @device: device structure
1653 * @network: network structure
1655 * Add new network to the device
1657 int connman_device_add_network(struct connman_device *device,
1658 struct connman_network *network)
1660 const char *identifier = connman_network_get_identifier(network);
1663 DBG("device %p network %p", device, network);
1665 switch (device->mode) {
1666 case CONNMAN_DEVICE_MODE_UNKNOWN:
1667 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1669 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1670 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1674 __connman_network_set_device(network, device);
1676 err = connman_element_register((struct connman_element *) network,
1679 __connman_network_set_device(network, NULL);
1683 g_hash_table_insert(device->networks, g_strdup(identifier),
1690 * connman_device_get_network:
1691 * @device: device structure
1692 * @identifier: network identifier
1694 * Get network for given identifier
1696 struct connman_network *connman_device_get_network(struct connman_device *device,
1697 const char *identifier)
1699 DBG("device %p identifier %s", device, identifier);
1701 return g_hash_table_lookup(device->networks, identifier);
1705 * connman_device_remove_network:
1706 * @device: device structure
1707 * @identifier: network identifier
1709 * Remove network for given identifier
1711 int connman_device_remove_network(struct connman_device *device,
1712 const char *identifier)
1714 DBG("device %p identifier %s", device, identifier);
1716 g_hash_table_remove(device->networks, identifier);
1721 void __connman_device_set_network(struct connman_device *device,
1722 struct connman_network *network)
1726 if (device->network == network)
1729 if (device->network != NULL)
1730 connman_network_unref(device->network);
1732 if (network != NULL) {
1733 name = connman_network_get_string(network,
1734 CONNMAN_PROPERTY_ID_NAME);
1735 g_free(device->last_network);
1736 device->last_network = g_strdup(name);
1738 device->network = connman_network_ref(network);
1740 g_free(device->last_network);
1741 device->last_network = NULL;
1743 device->network = NULL;
1748 * connman_device_register:
1749 * @device: device structure
1751 * Register device with the system
1753 int connman_device_register(struct connman_device *device)
1755 enum connman_service_type type;
1757 __connman_storage_load_device(device);
1759 device->offlinemode = __connman_manager_get_offlinemode();
1761 type = __connman_device_get_service_type(device);
1762 __connman_notifier_register(type);
1764 return connman_element_register(&device->element, NULL);
1768 * connman_device_unregister:
1769 * @device: device structure
1771 * Unregister device with the system
1773 void connman_device_unregister(struct connman_device *device)
1775 enum connman_service_type type;
1777 __connman_storage_save_device(device);
1779 type = __connman_device_get_service_type(device);
1780 __connman_notifier_unregister(type);
1782 connman_element_unregister(&device->element);
1786 * connman_device_get_data:
1787 * @device: device structure
1789 * Get private device data pointer
1791 void *connman_device_get_data(struct connman_device *device)
1793 return device->driver_data;
1797 * connman_device_set_data:
1798 * @device: device structure
1799 * @data: data pointer
1801 * Set private device data pointer
1803 void connman_device_set_data(struct connman_device *device, void *data)
1805 device->driver_data = data;
1808 static gboolean match_driver(struct connman_device *device,
1809 struct connman_device_driver *driver)
1811 if (device->type == driver->type ||
1812 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1818 static int device_probe(struct connman_element *element)
1820 struct connman_device *device = element->device;
1823 DBG("element %p name %s", element, element->name);
1828 if (device->driver != NULL)
1831 for (list = driver_list; list; list = list->next) {
1832 struct connman_device_driver *driver = list->data;
1834 if (match_driver(device, driver) == FALSE)
1837 DBG("driver %p name %s", driver, driver->name);
1839 if (driver->probe(device) == 0) {
1840 device->driver = driver;
1845 if (device->driver == NULL)
1848 return setup_device(device);
1851 static void device_remove(struct connman_element *element)
1853 struct connman_device *device = element->device;
1855 DBG("element %p name %s", element, element->name);
1860 if (device->driver == NULL)
1863 remove_device(device);
1866 static struct connman_driver device_driver = {
1868 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1869 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1870 .probe = device_probe,
1871 .remove = device_remove,
1874 static int device_load(struct connman_device *device)
1877 GError *error = NULL;
1879 connman_bool_t powered;
1882 DBG("device %p", device);
1884 keyfile = __connman_storage_open();
1885 if (keyfile == NULL)
1888 identifier = g_strdup_printf("device_%s", device->element.name);
1889 if (identifier == NULL)
1892 powered = g_key_file_get_boolean(keyfile, identifier,
1895 device->powered_persistent = powered;
1896 g_clear_error(&error);
1898 switch (device->mode) {
1899 case CONNMAN_DEVICE_MODE_UNKNOWN:
1900 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1902 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1903 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1904 val = g_key_file_get_integer(keyfile, identifier,
1905 "ScanInterval", &error);
1906 if (error == NULL && val > 0)
1907 device->scan_interval = val;
1908 g_clear_error(&error);
1915 __connman_storage_close(keyfile, FALSE);
1920 static int device_save(struct connman_device *device)
1925 DBG("device %p", device);
1927 keyfile = __connman_storage_open();
1928 if (keyfile == NULL)
1931 identifier = g_strdup_printf("device_%s", device->element.name);
1932 if (identifier == NULL)
1935 g_key_file_set_boolean(keyfile, identifier,
1936 "Powered", device->powered_persistent);
1938 switch (device->mode) {
1939 case CONNMAN_DEVICE_MODE_UNKNOWN:
1940 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1942 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1943 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1944 if (device->scan_interval > 0)
1945 g_key_file_set_integer(keyfile, identifier,
1946 "ScanInterval", device->scan_interval);
1953 __connman_storage_close(keyfile, TRUE);
1958 static struct connman_storage device_storage = {
1960 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1961 .device_load = device_load,
1962 .device_save = device_save,
1965 int __connman_device_init(void)
1969 connection = connman_dbus_get_connection();
1971 if (connman_storage_register(&device_storage) < 0)
1972 connman_error("Failed to register device storage");
1974 return connman_driver_register(&device_driver);
1977 void __connman_device_cleanup(void)
1981 connman_driver_unregister(&device_driver);
1983 connman_storage_unregister(&device_storage);
1985 dbus_connection_unref(connection);