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_bool_t reconnect;
49 connman_uint16_t scan_interval;
57 unsigned int connections;
59 struct connman_ipconfig *ipconfig;
61 struct connman_device_driver *driver;
64 connman_bool_t registered;
67 struct connman_network *network;
74 static gboolean device_scan_trigger(gpointer user_data)
76 struct connman_device *device = user_data;
78 DBG("device %p", device);
80 if (device->driver == NULL) {
81 device->scan_timeout = 0;
85 if (device->driver->scan)
86 device->driver->scan(device);
91 static void clear_scan_trigger(struct connman_device *device)
93 if (device->scan_timeout > 0) {
94 g_source_remove(device->scan_timeout);
95 device->scan_timeout = 0;
99 static void reset_scan_trigger(struct connman_device *device)
101 clear_scan_trigger(device);
103 if (device->scan_interval > 0) {
104 guint interval = device->scan_interval;
105 device->scan_timeout = g_timeout_add_seconds(interval,
106 device_scan_trigger, device);
110 static void force_scan_trigger(struct connman_device *device)
112 clear_scan_trigger(device);
114 device->scan_timeout = g_timeout_add_seconds(5,
115 device_scan_trigger, device);
118 static const char *type2description(enum connman_device_type type)
121 case CONNMAN_DEVICE_TYPE_UNKNOWN:
122 case CONNMAN_DEVICE_TYPE_VENDOR:
124 case CONNMAN_DEVICE_TYPE_ETHERNET:
126 case CONNMAN_DEVICE_TYPE_WIFI:
128 case CONNMAN_DEVICE_TYPE_WIMAX:
130 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
132 case CONNMAN_DEVICE_TYPE_GPS:
134 case CONNMAN_DEVICE_TYPE_MBM:
135 case CONNMAN_DEVICE_TYPE_HSO:
136 case CONNMAN_DEVICE_TYPE_NOZOMI:
137 case CONNMAN_DEVICE_TYPE_HUAWEI:
138 case CONNMAN_DEVICE_TYPE_NOVATEL:
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_MBM:
162 case CONNMAN_DEVICE_TYPE_HSO:
163 case CONNMAN_DEVICE_TYPE_HUAWEI:
164 case CONNMAN_DEVICE_TYPE_NOZOMI:
165 case CONNMAN_DEVICE_TYPE_NOVATEL:
172 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
174 enum connman_device_type type = connman_device_get_type(device);
177 case CONNMAN_DEVICE_TYPE_UNKNOWN:
178 case CONNMAN_DEVICE_TYPE_VENDOR:
179 case CONNMAN_DEVICE_TYPE_GPS:
180 case CONNMAN_DEVICE_TYPE_NOZOMI:
181 case CONNMAN_DEVICE_TYPE_HUAWEI:
182 case CONNMAN_DEVICE_TYPE_NOVATEL:
184 case CONNMAN_DEVICE_TYPE_ETHERNET:
185 return CONNMAN_SERVICE_TYPE_ETHERNET;
186 case CONNMAN_DEVICE_TYPE_WIFI:
187 return CONNMAN_SERVICE_TYPE_WIFI;
188 case CONNMAN_DEVICE_TYPE_WIMAX:
189 return CONNMAN_SERVICE_TYPE_WIMAX;
190 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
191 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
192 case CONNMAN_DEVICE_TYPE_MBM:
193 case CONNMAN_DEVICE_TYPE_HSO:
194 return CONNMAN_SERVICE_TYPE_CELLULAR;
197 return CONNMAN_SERVICE_TYPE_UNKNOWN;
200 static int set_connected(struct connman_device *device,
201 connman_bool_t connected)
203 if (connected == TRUE) {
204 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
205 struct connman_element *element;
207 device->disconnected = TRUE;
209 switch (device->element.ipv4.method) {
210 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
211 case CONNMAN_IPCONFIG_METHOD_IGNORE:
213 case CONNMAN_IPCONFIG_METHOD_STATIC:
214 type = CONNMAN_ELEMENT_TYPE_IPV4;
216 case CONNMAN_IPCONFIG_METHOD_DHCP:
217 type = CONNMAN_ELEMENT_TYPE_DHCP;
221 element = connman_element_create(NULL);
222 if (element != NULL) {
223 struct connman_service *service;
225 element->type = type;
226 element->index = device->element.index;
228 if (connman_element_register(element,
229 &device->element) < 0)
230 connman_element_unref(element);
232 device->disconnected = FALSE;
234 service = __connman_service_lookup_from_device(device);
235 __connman_service_indicate_state(service,
236 CONNMAN_SERVICE_STATE_CONFIGURATION);
239 struct connman_service *service;
241 connman_element_unregister_children(&device->element);
243 device->disconnected = TRUE;
245 service = __connman_service_lookup_from_device(device);
246 __connman_service_indicate_state(service,
247 CONNMAN_SERVICE_STATE_IDLE);
250 if (connected == TRUE) {
251 enum connman_service_type type;
253 type = __connman_device_get_service_type(device);
254 __connman_notifier_connect(type);
256 enum connman_service_type type;
258 type = __connman_device_get_service_type(device);
259 __connman_notifier_disconnect(type);
265 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
268 __connman_profile_add_device(device);
270 __connman_profile_remove_device(device);
272 return set_connected(device, carrier);
275 static int powered_changed(struct connman_device *device)
278 DBusMessageIter entry, value;
279 const char *key = "Powered";
281 signal = dbus_message_new_signal(device->element.path,
282 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
286 dbus_message_iter_init_append(signal, &entry);
288 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
290 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
291 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
292 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
294 dbus_message_iter_close_container(&entry, &value);
296 g_dbus_send_message(connection, signal);
301 static int set_powered(struct connman_device *device, connman_bool_t powered)
303 struct connman_device_driver *driver = device->driver;
304 enum connman_service_type type;
307 DBG("device %p powered %d", device, powered);
309 if (device->powered_pending == powered)
315 type = __connman_device_get_service_type(device);
317 if (powered == TRUE) {
318 if (driver->enable) {
319 device->powered_pending = powered;
321 err = driver->enable(device);
323 __connman_notifier_enable(type);
327 device->powered_pending = powered;
329 device->reconnect = FALSE;
331 clear_scan_trigger(device);
333 g_hash_table_remove_all(device->networks);
335 set_carrier(device, FALSE);
337 if (driver->disable) {
338 err = driver->disable(device);
340 __connman_notifier_disable(type);
346 device->powered = powered;
348 if (device->registered == TRUE)
349 powered_changed(device);
355 static void append_path(gpointer key, gpointer value, gpointer user_data)
357 struct connman_element *element = value;
358 DBusMessageIter *iter = user_data;
360 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
364 static void append_networks(struct connman_device *device,
365 DBusMessageIter *entry)
367 DBusMessageIter value, iter;
368 const char *key = "Networks";
370 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
372 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
373 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
376 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
377 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
378 g_hash_table_foreach(device->networks, append_path, &iter);
379 dbus_message_iter_close_container(&value, &iter);
381 dbus_message_iter_close_container(entry, &value);
384 static DBusMessage *get_properties(DBusConnection *conn,
385 DBusMessage *msg, void *data)
387 struct connman_device *device = data;
389 DBusMessageIter array, dict, entry;
392 DBG("conn %p", conn);
394 if (__connman_security_check_privilege(msg,
395 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
396 return __connman_error_permission_denied(msg);
398 reply = dbus_message_new_method_return(msg);
402 dbus_message_iter_init_append(reply, &array);
404 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
405 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
406 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
407 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
409 if (device->name != NULL)
410 connman_dbus_dict_append_variant(&dict, "Name",
411 DBUS_TYPE_STRING, &device->name);
413 str = type2string(device->type);
415 connman_dbus_dict_append_variant(&dict, "Type",
416 DBUS_TYPE_STRING, &str);
418 if (device->address != NULL)
419 connman_dbus_dict_append_variant(&dict, "Address",
420 DBUS_TYPE_STRING, &device->address);
422 if (device->interface != NULL)
423 connman_dbus_dict_append_variant(&dict, "Interface",
424 DBUS_TYPE_STRING, &device->interface);
426 connman_dbus_dict_append_variant(&dict, "Powered",
427 DBUS_TYPE_BOOLEAN, &device->powered);
429 if (device->driver && device->driver->scan)
430 connman_dbus_dict_append_variant(&dict, "Scanning",
431 DBUS_TYPE_BOOLEAN, &device->scanning);
433 switch (device->mode) {
434 case CONNMAN_DEVICE_MODE_UNKNOWN:
436 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
437 __connman_element_append_ipv4(&device->element, &dict);
439 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
440 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
441 if (device->scan_interval > 0)
442 connman_dbus_dict_append_variant(&dict, "ScanInterval",
443 DBUS_TYPE_UINT16, &device->scan_interval);
445 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
447 append_networks(device, &entry);
448 dbus_message_iter_close_container(&dict, &entry);
452 dbus_message_iter_close_container(&array, &dict);
457 static gboolean powered_timeout(gpointer user_data)
459 struct connman_device *device = user_data;
461 DBG("device %p", device);
465 if (device->pending != NULL) {
468 reply = __connman_error_operation_timeout(device->pending);
470 g_dbus_send_message(connection, reply);
472 dbus_message_unref(device->pending);
473 device->pending = NULL;
479 static DBusMessage *set_property(DBusConnection *conn,
480 DBusMessage *msg, void *data)
482 struct connman_device *device = data;
483 DBusMessageIter iter, value;
487 DBG("conn %p", conn);
489 if (dbus_message_iter_init(msg, &iter) == FALSE)
490 return __connman_error_invalid_arguments(msg);
492 dbus_message_iter_get_basic(&iter, &name);
493 dbus_message_iter_next(&iter);
494 dbus_message_iter_recurse(&iter, &value);
496 if (__connman_security_check_privilege(msg,
497 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
498 return __connman_error_permission_denied(msg);
500 type = dbus_message_iter_get_arg_type(&value);
502 if (g_str_equal(name, "Powered") == TRUE) {
503 connman_bool_t powered;
506 if (type != DBUS_TYPE_BOOLEAN)
507 return __connman_error_invalid_arguments(msg);
509 dbus_message_iter_get_basic(&value, &powered);
511 device->powered_persistent = powered;
513 __connman_storage_save_device(device);
515 if (device->powered == powered)
516 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
518 if (device->pending != NULL)
519 return __connman_error_in_progress(msg);
521 err = set_powered(device, powered);
523 if (err != -EINPROGRESS)
524 return __connman_error_failed(msg, -err);
526 device->pending = dbus_message_ref(msg);
528 device->timeout = g_timeout_add_seconds(15,
529 powered_timeout, device);
533 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
534 connman_uint16_t interval;
536 switch (device->mode) {
537 case CONNMAN_DEVICE_MODE_UNKNOWN:
538 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
539 return __connman_error_invalid_arguments(msg);
540 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
541 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
545 if (type != DBUS_TYPE_UINT16)
546 return __connman_error_invalid_arguments(msg);
548 dbus_message_iter_get_basic(&value, &interval);
550 if (device->scan_interval != interval) {
551 device->scan_interval = interval;
553 __connman_storage_save_device(device);
555 reset_scan_trigger(device);
557 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
560 if (device->ipconfig == NULL)
561 return __connman_error_invalid_property(msg);
563 switch (device->mode) {
564 case CONNMAN_DEVICE_MODE_UNKNOWN:
565 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
566 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
567 return __connman_error_invalid_arguments(msg);
568 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
572 err = __connman_ipconfig_set_ipv4(device->ipconfig,
575 return __connman_error_failed(msg, -err);
577 return __connman_error_invalid_property(msg);
579 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
582 static DBusMessage *propose_scan(DBusConnection *conn,
583 DBusMessage *msg, void *data)
585 struct connman_device *device = data;
588 DBG("conn %p", conn);
590 switch (device->mode) {
591 case CONNMAN_DEVICE_MODE_UNKNOWN:
592 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
593 return __connman_error_not_supported(msg);
594 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
595 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
599 err = __connman_device_scan(device);
601 return __connman_error_failed(msg, -err);
603 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
606 static GDBusMethodTable device_methods[] = {
607 { "GetProperties", "", "a{sv}", get_properties },
608 { "SetProperty", "sv", "", set_property,
609 G_DBUS_METHOD_FLAG_ASYNC },
610 { "ProposeScan", "", "", propose_scan },
614 static GDBusSignalTable device_signals[] = {
615 { "PropertyChanged", "sv" },
619 static void append_devices(DBusMessageIter *entry)
621 DBusMessageIter value, iter;
622 const char *key = "Devices";
624 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
626 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
627 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
630 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
631 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
632 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
633 dbus_message_iter_close_container(&value, &iter);
635 dbus_message_iter_close_container(entry, &value);
638 static void emit_devices_signal(void)
641 DBusMessageIter entry;
643 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
644 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
648 dbus_message_iter_init_append(signal, &entry);
650 append_devices(&entry);
652 g_dbus_send_message(connection, signal);
655 static int register_interface(struct connman_element *element)
657 struct connman_device *device = element->device;
659 DBG("element %p name %s", element, element->name);
661 if (g_dbus_register_interface(connection, element->path,
662 CONNMAN_DEVICE_INTERFACE,
663 device_methods, device_signals,
664 NULL, device, NULL) == FALSE) {
665 connman_error("Failed to register %s device", element->path);
669 device->registered = TRUE;
671 emit_devices_signal();
676 static void unregister_interface(struct connman_element *element)
678 struct connman_device *device = element->device;
680 DBG("element %p name %s", element, element->name);
682 device->registered = FALSE;
684 emit_devices_signal();
686 g_dbus_unregister_interface(connection, element->path,
687 CONNMAN_DEVICE_INTERFACE);
690 static int setup_device(struct connman_device *device)
692 enum connman_service_type type;
695 DBG("device %p", device);
697 err = register_interface(&device->element);
699 if (device->driver->remove)
700 device->driver->remove(device);
701 device->driver = NULL;
705 type = __connman_device_get_service_type(device);
706 __connman_notifier_register(type);
708 switch (device->mode) {
709 case CONNMAN_DEVICE_MODE_UNKNOWN:
710 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
711 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
713 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
714 if (device->carrier == TRUE && device->secondary == FALSE)
715 __connman_profile_add_device(device);
719 if (device->offlinemode == FALSE &&
720 device->powered_persistent == TRUE)
721 __connman_device_enable(device);
726 static void probe_driver(struct connman_element *element, gpointer user_data)
728 struct connman_device_driver *driver = user_data;
730 DBG("element %p name %s", element, element->name);
732 if (element->device == NULL)
735 if (element->device->driver != NULL)
738 if (driver->type != element->device->type)
741 if (driver->probe(element->device) < 0)
744 element->device->driver = driver;
746 setup_device(element->device);
749 static void remove_device(struct connman_device *device)
751 enum connman_service_type type;
753 DBG("device %p", device);
755 __connman_device_disable(device);
757 switch (device->mode) {
758 case CONNMAN_DEVICE_MODE_UNKNOWN:
759 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
760 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
762 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
763 if (device->secondary == FALSE)
764 __connman_profile_remove_device(device);
768 type = __connman_device_get_service_type(device);
769 __connman_notifier_unregister(type);
771 unregister_interface(&device->element);
773 if (device->driver->remove)
774 device->driver->remove(device);
776 device->driver = NULL;
779 static void remove_driver(struct connman_element *element, gpointer user_data)
781 struct connman_device_driver *driver = user_data;
783 DBG("element %p name %s", element, element->name);
785 if (element->device == NULL)
788 if (element->device->driver == driver)
789 remove_device(element->device);
792 connman_bool_t __connman_device_has_driver(struct connman_device *device)
794 if (device == NULL || device->driver == NULL)
797 return device->registered;
800 static GSList *driver_list = NULL;
802 static gint compare_priority(gconstpointer a, gconstpointer b)
804 const struct connman_device_driver *driver1 = a;
805 const struct connman_device_driver *driver2 = b;
807 return driver2->priority - driver1->priority;
811 * connman_device_driver_register:
812 * @driver: device driver definition
814 * Register a new device driver
816 * Returns: %0 on success
818 int connman_device_driver_register(struct connman_device_driver *driver)
820 DBG("driver %p name %s", driver, driver->name);
822 driver_list = g_slist_insert_sorted(driver_list, driver,
825 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
826 probe_driver, driver);
832 * connman_device_driver_unregister:
833 * @driver: device driver definition
835 * Remove a previously registered device driver
837 void connman_device_driver_unregister(struct connman_device_driver *driver)
839 DBG("driver %p name %s", driver, driver->name);
841 driver_list = g_slist_remove(driver_list, driver);
843 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
844 remove_driver, driver);
847 static void unregister_network(gpointer data)
849 struct connman_network *network = data;
851 DBG("network %p", network);
853 connman_element_unregister((struct connman_element *) network);
855 connman_network_unref(network);
858 static void device_destruct(struct connman_element *element)
860 struct connman_device *device = element->device;
862 DBG("element %p name %s", element, element->name);
864 if (device->timeout > 0) {
865 g_source_remove(device->timeout);
869 if (device->pending != NULL) {
870 dbus_message_unref(device->pending);
871 device->pending = NULL;
874 g_free(device->ident);
875 g_free(device->node);
876 g_free(device->name);
877 g_free(device->address);
878 g_free(device->control);
879 g_free(device->interface);
881 if (device->ipconfig != NULL) {
882 connman_ipconfig_unref(device->ipconfig);
883 device->ipconfig = NULL;
886 g_free(device->last_network);
888 g_hash_table_destroy(device->networks);
889 device->networks = NULL;
893 * connman_device_create:
894 * @node: device node name (for example an address)
897 * Allocate a new device of given #type and assign the #node name to it.
899 * Returns: a newly-allocated #connman_device structure
901 struct connman_device *connman_device_create(const char *node,
902 enum connman_device_type type)
904 struct connman_device *device;
907 DBG("node %s type %d", node, type);
909 device = g_try_new0(struct connman_device, 1);
913 DBG("device %p", device);
915 __connman_element_initialize(&device->element);
917 device->element.name = g_strdup(node);
918 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
920 device->element.device = device;
921 device->element.destruct = device_destruct;
923 str = type2string(type);
925 connman_element_set_string(&device->element,
926 CONNMAN_PROPERTY_ID_TYPE, str);
928 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
931 device->name = g_strdup(type2description(device->type));
932 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
933 device->secondary = FALSE;
935 device->powered_persistent = TRUE;
937 device->phyindex = -1;
940 case CONNMAN_DEVICE_TYPE_UNKNOWN:
941 case CONNMAN_DEVICE_TYPE_VENDOR:
942 device->scan_interval = 0;
944 case CONNMAN_DEVICE_TYPE_ETHERNET:
945 case CONNMAN_DEVICE_TYPE_WIFI:
946 device->scan_interval = 300;
948 case CONNMAN_DEVICE_TYPE_WIMAX:
949 device->scan_interval = 0;
951 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
952 device->scan_interval = 0;
954 case CONNMAN_DEVICE_TYPE_GPS:
955 device->scan_interval = 0;
957 case CONNMAN_DEVICE_TYPE_MBM:
958 case CONNMAN_DEVICE_TYPE_HSO:
959 case CONNMAN_DEVICE_TYPE_NOZOMI:
960 case CONNMAN_DEVICE_TYPE_HUAWEI:
961 case CONNMAN_DEVICE_TYPE_NOVATEL:
962 device->scan_interval = 0;
966 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
967 g_free, unregister_network);
973 * connman_device_ref:
974 * @device: device structure
976 * Increase reference counter of device
978 struct connman_device *connman_device_ref(struct connman_device *device)
980 if (connman_element_ref(&device->element) == NULL)
987 * connman_device_unref:
988 * @device: device structure
990 * Decrease reference counter of device
992 void connman_device_unref(struct connman_device *device)
994 connman_element_unref(&device->element);
997 const char *__connman_device_get_type(struct connman_device *device)
999 return type2string(device->type);
1003 * connman_device_get_type:
1004 * @device: device structure
1006 * Get type of device
1008 enum connman_device_type connman_device_get_type(struct connman_device *device)
1010 return device->type;
1014 * connman_device_get_name:
1015 * @device: device structure
1017 * Get unique name of device
1019 const char *connman_device_get_name(struct connman_device *device)
1021 return device->element.name;
1025 * connman_device_get_path:
1026 * @device: device structure
1028 * Get path name of device
1030 const char *connman_device_get_path(struct connman_device *device)
1032 return device->element.path;
1036 * connman_device_set_index:
1037 * @device: device structure
1038 * @index: index number
1040 * Set index number of device
1042 void connman_device_set_index(struct connman_device *device, int index)
1044 device->element.index = index;
1046 if (device->ipconfig != NULL) {
1047 if (index == connman_ipconfig_get_index(device->ipconfig))
1050 connman_ipconfig_unref(device->ipconfig);
1053 device->ipconfig = connman_ipconfig_create(index);
1057 * connman_device_get_index:
1058 * @device: device structure
1060 * Get index number of device
1062 int connman_device_get_index(struct connman_device *device)
1064 return device->element.index;
1067 int __connman_device_get_phyindex(struct connman_device *device)
1069 return device->phyindex;
1072 void __connman_device_set_phyindex(struct connman_device *device,
1075 device->phyindex = phyindex;
1079 * connman_device_set_interface:
1080 * @device: device structure
1081 * @interface: interface name
1082 * @control: control interface
1084 * Set interface name of device
1086 void connman_device_set_interface(struct connman_device *device,
1087 const char *interface, const char *control)
1089 g_free(device->element.devname);
1090 device->element.devname = g_strdup(interface);
1092 g_free(device->interface);
1093 device->interface = g_strdup(interface);
1095 g_free(device->control);
1096 device->control = g_strdup(control);
1098 if (device->name == NULL) {
1099 const char *str = type2description(device->type);
1100 if (str != NULL && device->interface != NULL)
1101 device->name = g_strdup_printf("%s (%s)", str,
1106 const char *connman_device_get_control(struct connman_device *device)
1108 return device->control;
1112 * connman_device_set_ident:
1113 * @device: device structure
1114 * @ident: unique identifier
1116 * Set unique identifier of device
1118 void connman_device_set_ident(struct connman_device *device,
1121 g_free(device->ident);
1122 device->ident = g_strdup(ident);
1125 const char *__connman_device_get_ident(struct connman_device *device)
1127 return device->ident;
1131 * connman_device_set_mode:
1132 * @device: device structure
1133 * @mode: network mode
1135 * Change network mode of device
1137 void connman_device_set_mode(struct connman_device *device,
1138 enum connman_device_mode mode)
1140 device->mode = mode;
1144 * connman_device_get_mode:
1145 * @device: device structure
1147 * Get network mode of device
1149 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1151 return device->mode;
1155 * connman_device_set_secondary:
1156 * @device: device structure
1157 * @secondary: secondary value
1159 * Change secondary value of device
1161 void connman_device_set_secondary(struct connman_device *device,
1162 connman_bool_t secondary)
1164 device->secondary = secondary;
1168 * connman_device_get_secondary:
1169 * @device: device structure
1171 * Get secondary value of device
1173 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1175 return device->secondary;
1179 * connman_device_set_powered:
1180 * @device: device structure
1181 * @powered: powered state
1183 * Change power state of device
1185 int connman_device_set_powered(struct connman_device *device,
1186 connman_bool_t powered)
1188 enum connman_service_type type;
1190 DBG("driver %p powered %d", device, powered);
1192 if (device->timeout > 0) {
1193 g_source_remove(device->timeout);
1194 device->timeout = 0;
1197 if (device->pending != NULL) {
1198 g_dbus_send_reply(connection, device->pending,
1201 dbus_message_unref(device->pending);
1202 device->pending = NULL;
1205 if (device->powered == powered)
1208 device->powered = powered;
1209 device->powered_pending = powered;
1211 type = __connman_device_get_service_type(device);
1213 if (device->powered == TRUE)
1214 __connman_notifier_enable(type);
1216 __connman_notifier_disable(type);
1218 if (device->registered == FALSE)
1221 powered_changed(device);
1223 if (powered == FALSE)
1226 reset_scan_trigger(device);
1228 if (device->driver->scan)
1229 device->driver->scan(device);
1234 int __connman_device_set_blocked(struct connman_device *device,
1235 connman_bool_t blocked)
1237 connman_bool_t powered;
1239 DBG("device %p blocked %d", device, blocked);
1241 device->blocked = blocked;
1243 if (device->offlinemode == TRUE)
1246 connman_info("%s {rfkill} blocked %d", device->interface, blocked);
1248 if (blocked == FALSE)
1249 powered = device->powered_persistent;
1253 return set_powered(device, powered);
1257 * connman_device_set_carrier:
1258 * @device: device structure
1259 * @carrier: carrier state
1261 * Change carrier state of device (only for device without scanning)
1263 int connman_device_set_carrier(struct connman_device *device,
1264 connman_bool_t carrier)
1266 DBG("device %p carrier %d", device, carrier);
1268 switch (device->mode) {
1269 case CONNMAN_DEVICE_MODE_UNKNOWN:
1270 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1271 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1273 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1277 if (device->carrier == carrier)
1280 device->carrier = carrier;
1282 return set_carrier(device, device->carrier);
1285 int __connman_device_scan(struct connman_device *device)
1287 if (!device->driver || !device->driver->scan)
1290 if (device->powered == FALSE)
1293 reset_scan_trigger(device);
1295 return device->driver->scan(device);
1298 int __connman_device_enable(struct connman_device *device)
1300 enum connman_service_type type;
1303 DBG("device %p", device);
1305 if (!device->driver || !device->driver->enable)
1308 if (device->powered_pending == TRUE)
1311 device->powered_pending = TRUE;
1313 err = device->driver->enable(device);
1317 device->powered = TRUE;
1319 type = __connman_device_get_service_type(device);
1320 __connman_notifier_enable(type);
1325 int __connman_device_enable_persistent(struct connman_device *device)
1327 DBG("device %p", device);
1329 device->powered_persistent = TRUE;
1331 __connman_storage_save_device(device);
1333 return __connman_device_enable(device);
1336 int __connman_device_disable(struct connman_device *device)
1338 enum connman_service_type type;
1341 DBG("device %p", device);
1343 if (!device->driver || !device->driver->disable)
1346 if (device->powered == FALSE)
1349 if (device->powered_pending == FALSE)
1352 device->powered_pending = FALSE;
1354 device->reconnect = FALSE;
1356 clear_scan_trigger(device);
1358 g_hash_table_remove_all(device->networks);
1360 err = device->driver->disable(device);
1364 device->powered = FALSE;
1366 type = __connman_device_get_service_type(device);
1367 __connman_notifier_disable(type);
1372 int __connman_device_disable_persistent(struct connman_device *device)
1374 DBG("device %p", device);
1376 device->powered_persistent = FALSE;
1378 __connman_storage_save_device(device);
1380 return __connman_device_disable(device);
1383 int __connman_device_connect(struct connman_device *device)
1385 DBG("device %p", device);
1387 if (device->disconnected == FALSE)
1390 if (device->driver && device->driver->connect)
1391 device->driver->connect(device);
1396 int __connman_device_disconnect(struct connman_device *device)
1398 GHashTableIter iter;
1399 gpointer key, value;
1401 DBG("device %p", device);
1403 connman_device_set_disconnected(device, TRUE);
1405 g_hash_table_iter_init(&iter, device->networks);
1407 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1408 struct connman_network *network = value;
1410 __connman_network_disconnect(network);
1413 if (device->driver && device->driver->disconnect)
1414 device->driver->disconnect(device);
1419 static void mark_network_unavailable(gpointer key, gpointer value,
1422 struct connman_network *network = value;
1424 if (connman_network_get_connected(network) == TRUE)
1427 connman_network_set_available(network, FALSE);
1430 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1433 struct connman_network *network = value;
1435 if (connman_network_get_connected(network) == TRUE)
1438 if (connman_network_get_available(network) == TRUE)
1444 void __connman_device_cleanup_networks(struct connman_device *device)
1446 g_hash_table_foreach_remove(device->networks,
1447 remove_unavailable_network, NULL);
1450 static void scanning_changed(struct connman_device *device)
1452 DBusMessage *signal;
1453 DBusMessageIter entry, value;
1454 const char *key = "Scanning";
1456 signal = dbus_message_new_signal(device->element.path,
1457 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1461 dbus_message_iter_init_append(signal, &entry);
1463 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1465 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1466 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1467 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
1469 dbus_message_iter_close_container(&entry, &value);
1471 g_dbus_send_message(connection, signal);
1474 static void mark_network_available(gpointer key, gpointer value,
1477 struct connman_network *network = value;
1479 connman_network_set_available(network, TRUE);
1482 void connman_device_cleanup_scanning(struct connman_device *device)
1484 device->scanning = FALSE;
1486 scanning_changed(device);
1488 g_hash_table_foreach(device->networks,
1489 mark_network_available, NULL);
1493 * connman_device_set_scanning:
1494 * @device: device structure
1495 * @scanning: scanning state
1497 * Change scanning state of device
1499 int connman_device_set_scanning(struct connman_device *device,
1500 connman_bool_t scanning)
1502 DBG("device %p scanning %d", device, scanning);
1504 if (!device->driver || !device->driver->scan)
1507 if (device->scanning == scanning)
1510 device->scanning = scanning;
1512 scanning_changed(device);
1514 if (scanning == TRUE) {
1515 reset_scan_trigger(device);
1517 g_hash_table_foreach(device->networks,
1518 mark_network_unavailable, NULL);
1523 __connman_device_cleanup_networks(device);
1525 if (device->connections > 0)
1528 if (device->disconnected == TRUE)
1531 __connman_service_auto_connect();
1537 * connman_device_set_disconnected:
1538 * @device: device structure
1539 * @disconnected: disconnected state
1541 * Change disconnected state of device (only for device with networks)
1543 int connman_device_set_disconnected(struct connman_device *device,
1544 connman_bool_t disconnected)
1546 DBG("device %p disconnected %d", device, disconnected);
1548 switch (device->mode) {
1549 case CONNMAN_DEVICE_MODE_UNKNOWN:
1550 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1552 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1553 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1557 if (device->disconnected == disconnected)
1560 device->disconnected = disconnected;
1562 if (disconnected == TRUE)
1563 force_scan_trigger(device);
1569 * connman_device_get_disconnected:
1570 * @device: device structure
1572 * Get device disconnected state
1574 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1576 return device->disconnected;
1580 * connman_device_set_connected:
1581 * @device: device structure
1582 * @connected: connected state
1584 * Change connected state of device (for Ethernet like devices)
1586 int connman_device_set_connected(struct connman_device *device,
1587 connman_bool_t connected)
1589 DBG("device %p connected %d", device, connected);
1591 switch (device->mode) {
1592 case CONNMAN_DEVICE_MODE_UNKNOWN:
1593 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1594 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1596 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1600 if (device->carrier == FALSE)
1603 return set_connected(device, connected);
1607 * connman_device_set_string:
1608 * @device: device structure
1609 * @key: unique identifier
1610 * @value: string value
1612 * Set string value for specific key
1614 int connman_device_set_string(struct connman_device *device,
1615 const char *key, const char *value)
1617 DBG("device %p key %s value %s", device, key, value);
1619 if (g_str_equal(key, "Address") == TRUE) {
1620 g_free(device->address);
1621 device->address = g_strdup(value);
1622 } else if (g_str_equal(key, "Name") == TRUE) {
1623 g_free(device->name);
1624 device->name = g_strdup(value);
1625 } else if (g_str_equal(key, "Node") == TRUE) {
1626 g_free(device->node);
1627 device->node = g_strdup(value);
1630 return connman_element_set_string(&device->element, key, value);
1634 * connman_device_get_string:
1635 * @device: device structure
1636 * @key: unique identifier
1638 * Get string value for specific key
1640 const char *connman_device_get_string(struct connman_device *device,
1643 DBG("device %p key %s", device, key);
1645 if (g_str_equal(key, "Address") == TRUE)
1646 return device->address;
1647 else if (g_str_equal(key, "Name") == TRUE)
1648 return device->name;
1649 else if (g_str_equal(key, "Node") == TRUE)
1650 return device->node;
1652 return connman_element_get_string(&device->element, key);
1655 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1657 struct connman_device *device = element->device;
1658 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1659 connman_bool_t powered;
1661 DBG("element %p name %s", element, element->name);
1666 device->offlinemode = offlinemode;
1668 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1670 if (device->powered == powered)
1673 if (device->powered_persistent == FALSE)
1676 set_powered(device, powered);
1679 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1681 DBG("offlinmode %d", offlinemode);
1683 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1684 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1686 __connman_notifier_offlinemode(offlinemode);
1691 void __connman_device_increase_connections(struct connman_device *device)
1693 device->connections++;
1696 void __connman_device_decrease_connections(struct connman_device *device)
1698 device->connections--;
1702 * connman_device_add_network:
1703 * @device: device structure
1704 * @network: network structure
1706 * Add new network to the device
1708 int connman_device_add_network(struct connman_device *device,
1709 struct connman_network *network)
1711 const char *identifier = connman_network_get_identifier(network);
1714 DBG("device %p network %p", device, network);
1716 switch (device->mode) {
1717 case CONNMAN_DEVICE_MODE_UNKNOWN:
1718 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1720 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1721 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1725 __connman_network_set_device(network, device);
1727 err = connman_element_register((struct connman_element *) network,
1730 __connman_network_set_device(network, NULL);
1734 g_hash_table_insert(device->networks, g_strdup(identifier),
1741 * connman_device_get_network:
1742 * @device: device structure
1743 * @identifier: network identifier
1745 * Get network for given identifier
1747 struct connman_network *connman_device_get_network(struct connman_device *device,
1748 const char *identifier)
1750 DBG("device %p identifier %s", device, identifier);
1752 return g_hash_table_lookup(device->networks, identifier);
1756 * connman_device_remove_network:
1757 * @device: device structure
1758 * @identifier: network identifier
1760 * Remove network for given identifier
1762 int connman_device_remove_network(struct connman_device *device,
1763 const char *identifier)
1765 DBG("device %p identifier %s", device, identifier);
1767 g_hash_table_remove(device->networks, identifier);
1772 void __connman_device_set_network(struct connman_device *device,
1773 struct connman_network *network)
1777 if (device->network == network)
1780 if (device->network != NULL)
1781 connman_network_unref(device->network);
1783 if (network != NULL) {
1784 name = connman_network_get_string(network,
1785 CONNMAN_PROPERTY_ID_NAME);
1786 g_free(device->last_network);
1787 device->last_network = g_strdup(name);
1789 device->network = connman_network_ref(network);
1791 g_free(device->last_network);
1792 device->last_network = NULL;
1794 device->network = NULL;
1798 void __connman_device_set_reconnect(struct connman_device *device,
1799 connman_bool_t reconnect)
1801 device->reconnect = reconnect;
1804 connman_bool_t __connman_device_get_reconnect(
1805 struct connman_device *device)
1807 return device->reconnect;
1811 * connman_device_register:
1812 * @device: device structure
1814 * Register device with the system
1816 int connman_device_register(struct connman_device *device)
1818 __connman_storage_load_device(device);
1820 device->offlinemode = __connman_profile_get_offlinemode();
1822 return connman_element_register(&device->element, NULL);
1826 * connman_device_unregister:
1827 * @device: device structure
1829 * Unregister device with the system
1831 void connman_device_unregister(struct connman_device *device)
1833 __connman_storage_save_device(device);
1835 connman_element_unregister(&device->element);
1839 * connman_device_get_data:
1840 * @device: device structure
1842 * Get private device data pointer
1844 void *connman_device_get_data(struct connman_device *device)
1846 return device->driver_data;
1850 * connman_device_set_data:
1851 * @device: device structure
1852 * @data: data pointer
1854 * Set private device data pointer
1856 void connman_device_set_data(struct connman_device *device, void *data)
1858 device->driver_data = data;
1861 static gboolean match_driver(struct connman_device *device,
1862 struct connman_device_driver *driver)
1864 if (device->type == driver->type ||
1865 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1871 static int device_probe(struct connman_element *element)
1873 struct connman_device *device = element->device;
1876 DBG("element %p name %s", element, element->name);
1881 if (device->driver != NULL)
1884 for (list = driver_list; list; list = list->next) {
1885 struct connman_device_driver *driver = list->data;
1887 if (match_driver(device, driver) == FALSE)
1890 DBG("driver %p name %s", driver, driver->name);
1892 if (driver->probe(device) == 0) {
1893 device->driver = driver;
1898 if (device->driver == NULL)
1901 return setup_device(device);
1904 static void device_remove(struct connman_element *element)
1906 struct connman_device *device = element->device;
1908 DBG("element %p name %s", element, element->name);
1913 if (device->driver == NULL)
1916 remove_device(device);
1919 static struct connman_driver device_driver = {
1921 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1922 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1923 .probe = device_probe,
1924 .remove = device_remove,
1927 static int device_load(struct connman_device *device)
1929 const char *ident = __connman_profile_active_ident();
1931 GError *error = NULL;
1933 connman_bool_t powered;
1936 DBG("device %p", device);
1938 keyfile = __connman_storage_open(ident);
1939 if (keyfile == NULL)
1942 identifier = g_strdup_printf("device_%s", device->element.name);
1943 if (identifier == NULL)
1946 powered = g_key_file_get_boolean(keyfile, identifier,
1949 device->powered_persistent = powered;
1950 g_clear_error(&error);
1952 switch (device->mode) {
1953 case CONNMAN_DEVICE_MODE_UNKNOWN:
1954 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1956 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1957 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1958 val = g_key_file_get_integer(keyfile, identifier,
1959 "ScanInterval", &error);
1960 if (error == NULL && val > 0)
1961 device->scan_interval = val;
1962 g_clear_error(&error);
1969 __connman_storage_close(ident, keyfile, FALSE);
1974 static int device_save(struct connman_device *device)
1976 const char *ident = __connman_profile_active_ident();
1980 DBG("device %p", device);
1982 keyfile = __connman_storage_open(ident);
1983 if (keyfile == NULL)
1986 identifier = g_strdup_printf("device_%s", device->element.name);
1987 if (identifier == NULL)
1990 g_key_file_set_boolean(keyfile, identifier,
1991 "Powered", device->powered_persistent);
1993 switch (device->mode) {
1994 case CONNMAN_DEVICE_MODE_UNKNOWN:
1995 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1997 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1998 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1999 if (device->scan_interval > 0)
2000 g_key_file_set_integer(keyfile, identifier,
2001 "ScanInterval", device->scan_interval);
2008 __connman_storage_close(ident, keyfile, TRUE);
2013 static struct connman_storage device_storage = {
2015 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
2016 .device_load = device_load,
2017 .device_save = device_save,
2020 int __connman_device_init(void)
2024 connection = connman_dbus_get_connection();
2026 if (connman_storage_register(&device_storage) < 0)
2027 connman_error("Failed to register device storage");
2029 return connman_driver_register(&device_driver);
2032 void __connman_device_cleanup(void)
2036 connman_driver_unregister(&device_driver);
2038 connman_storage_unregister(&device_storage);
2040 dbus_connection_unref(connection);