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_IGNORE:
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)
259 __connman_profile_add_device(device);
261 __connman_profile_remove_device(device);
263 return set_connected(device, carrier);
266 static int powered_changed(struct connman_device *device)
269 DBusMessageIter entry, value;
270 const char *key = "Powered";
272 signal = dbus_message_new_signal(device->element.path,
273 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
277 dbus_message_iter_init_append(signal, &entry);
279 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
281 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
282 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
283 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
285 dbus_message_iter_close_container(&entry, &value);
287 g_dbus_send_message(connection, signal);
292 static int set_powered(struct connman_device *device, connman_bool_t powered)
294 struct connman_device_driver *driver = device->driver;
295 enum connman_service_type type;
298 DBG("device %p powered %d", device, powered);
300 if (device->powered_pending == powered)
306 type = __connman_device_get_service_type(device);
308 if (powered == TRUE) {
309 if (driver->enable) {
310 device->powered_pending = powered;
312 err = driver->enable(device);
314 __connman_notifier_enable(type);
318 device->powered_pending = powered;
320 clear_scan_trigger(device);
322 g_hash_table_remove_all(device->networks);
324 set_carrier(device, FALSE);
326 if (driver->disable) {
327 err = driver->disable(device);
329 __connman_notifier_disable(type);
335 device->powered = powered;
337 if (device->registered == TRUE)
338 powered_changed(device);
344 static void append_path(gpointer key, gpointer value, gpointer user_data)
346 struct connman_element *element = value;
347 DBusMessageIter *iter = user_data;
349 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
353 static void append_networks(struct connman_device *device,
354 DBusMessageIter *entry)
356 DBusMessageIter value, iter;
357 const char *key = "Networks";
359 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
361 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
362 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
365 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
366 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
367 g_hash_table_foreach(device->networks, append_path, &iter);
368 dbus_message_iter_close_container(&value, &iter);
370 dbus_message_iter_close_container(entry, &value);
373 static DBusMessage *get_properties(DBusConnection *conn,
374 DBusMessage *msg, void *data)
376 struct connman_device *device = data;
378 DBusMessageIter array, dict, entry;
381 DBG("conn %p", conn);
383 if (__connman_security_check_privilege(msg,
384 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
385 return __connman_error_permission_denied(msg);
387 reply = dbus_message_new_method_return(msg);
391 dbus_message_iter_init_append(reply, &array);
393 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
394 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
395 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
396 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
398 if (device->name != NULL)
399 connman_dbus_dict_append_variant(&dict, "Name",
400 DBUS_TYPE_STRING, &device->name);
402 str = type2string(device->type);
404 connman_dbus_dict_append_variant(&dict, "Type",
405 DBUS_TYPE_STRING, &str);
407 if (device->address != NULL)
408 connman_dbus_dict_append_variant(&dict, "Address",
409 DBUS_TYPE_STRING, &device->address);
411 if (device->interface != NULL)
412 connman_dbus_dict_append_variant(&dict, "Interface",
413 DBUS_TYPE_STRING, &device->interface);
415 connman_dbus_dict_append_variant(&dict, "Powered",
416 DBUS_TYPE_BOOLEAN, &device->powered);
418 if (device->driver && device->driver->scan)
419 connman_dbus_dict_append_variant(&dict, "Scanning",
420 DBUS_TYPE_BOOLEAN, &device->scanning);
422 switch (device->mode) {
423 case CONNMAN_DEVICE_MODE_UNKNOWN:
425 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
426 __connman_element_append_ipv4(&device->element, &dict);
428 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
429 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
430 if (device->scan_interval > 0)
431 connman_dbus_dict_append_variant(&dict, "ScanInterval",
432 DBUS_TYPE_UINT16, &device->scan_interval);
434 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
436 append_networks(device, &entry);
437 dbus_message_iter_close_container(&dict, &entry);
441 dbus_message_iter_close_container(&array, &dict);
446 static gboolean powered_timeout(gpointer user_data)
448 struct connman_device *device = user_data;
450 DBG("device %p", device);
454 if (device->pending != NULL) {
457 reply = __connman_error_operation_timeout(device->pending);
459 g_dbus_send_message(connection, reply);
461 dbus_message_unref(device->pending);
462 device->pending = NULL;
468 static DBusMessage *set_property(DBusConnection *conn,
469 DBusMessage *msg, void *data)
471 struct connman_device *device = data;
472 DBusMessageIter iter, value;
476 DBG("conn %p", conn);
478 if (dbus_message_iter_init(msg, &iter) == FALSE)
479 return __connman_error_invalid_arguments(msg);
481 dbus_message_iter_get_basic(&iter, &name);
482 dbus_message_iter_next(&iter);
483 dbus_message_iter_recurse(&iter, &value);
485 if (__connman_security_check_privilege(msg,
486 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
487 return __connman_error_permission_denied(msg);
489 type = dbus_message_iter_get_arg_type(&value);
491 if (g_str_equal(name, "Powered") == TRUE) {
492 connman_bool_t powered;
495 if (type != DBUS_TYPE_BOOLEAN)
496 return __connman_error_invalid_arguments(msg);
498 dbus_message_iter_get_basic(&value, &powered);
500 device->powered_persistent = powered;
502 __connman_storage_save_device(device);
504 if (device->powered == powered)
505 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
507 if (device->pending != NULL)
508 return __connman_error_in_progress(msg);
510 err = set_powered(device, powered);
512 if (err != -EINPROGRESS)
513 return __connman_error_failed(msg, -err);
515 device->pending = dbus_message_ref(msg);
517 device->timeout = g_timeout_add_seconds(15,
518 powered_timeout, device);
522 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
523 connman_uint16_t interval;
525 switch (device->mode) {
526 case CONNMAN_DEVICE_MODE_UNKNOWN:
527 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
528 return __connman_error_invalid_arguments(msg);
529 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
530 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
534 if (type != DBUS_TYPE_UINT16)
535 return __connman_error_invalid_arguments(msg);
537 dbus_message_iter_get_basic(&value, &interval);
539 if (device->scan_interval != interval) {
540 device->scan_interval = interval;
542 __connman_storage_save_device(device);
544 reset_scan_trigger(device);
546 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
549 if (device->ipconfig == NULL)
550 return __connman_error_invalid_property(msg);
552 switch (device->mode) {
553 case CONNMAN_DEVICE_MODE_UNKNOWN:
554 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
555 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
556 return __connman_error_invalid_arguments(msg);
557 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
561 err = __connman_ipconfig_set_ipv4(device->ipconfig,
564 return __connman_error_failed(msg, -err);
566 return __connman_error_invalid_property(msg);
568 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
571 static DBusMessage *propose_scan(DBusConnection *conn,
572 DBusMessage *msg, void *data)
574 struct connman_device *device = data;
577 DBG("conn %p", conn);
579 switch (device->mode) {
580 case CONNMAN_DEVICE_MODE_UNKNOWN:
581 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
582 return __connman_error_not_supported(msg);
583 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
584 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
588 err = __connman_device_scan(device);
590 return __connman_error_failed(msg, -err);
592 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
595 static GDBusMethodTable device_methods[] = {
596 { "GetProperties", "", "a{sv}", get_properties },
597 { "SetProperty", "sv", "", set_property,
598 G_DBUS_METHOD_FLAG_ASYNC },
599 { "ProposeScan", "", "", propose_scan },
603 static GDBusSignalTable device_signals[] = {
604 { "PropertyChanged", "sv" },
608 static void append_devices(DBusMessageIter *entry)
610 DBusMessageIter value, iter;
611 const char *key = "Devices";
613 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
615 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
616 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
619 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
620 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
621 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
622 dbus_message_iter_close_container(&value, &iter);
624 dbus_message_iter_close_container(entry, &value);
627 static void emit_devices_signal(void)
630 DBusMessageIter entry;
632 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
633 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
637 dbus_message_iter_init_append(signal, &entry);
639 append_devices(&entry);
641 g_dbus_send_message(connection, signal);
644 static int register_interface(struct connman_element *element)
646 struct connman_device *device = element->device;
648 DBG("element %p name %s", element, element->name);
650 if (g_dbus_register_interface(connection, element->path,
651 CONNMAN_DEVICE_INTERFACE,
652 device_methods, device_signals,
653 NULL, device, NULL) == FALSE) {
654 connman_error("Failed to register %s device", element->path);
658 device->registered = TRUE;
660 emit_devices_signal();
665 static void unregister_interface(struct connman_element *element)
667 struct connman_device *device = element->device;
669 DBG("element %p name %s", element, element->name);
671 device->registered = FALSE;
673 emit_devices_signal();
675 g_dbus_unregister_interface(connection, element->path,
676 CONNMAN_DEVICE_INTERFACE);
679 static int setup_device(struct connman_device *device)
681 enum connman_service_type type;
684 DBG("device %p", device);
686 err = register_interface(&device->element);
688 if (device->driver->remove)
689 device->driver->remove(device);
690 device->driver = NULL;
694 type = __connman_device_get_service_type(device);
695 __connman_notifier_register(type);
697 switch (device->mode) {
698 case CONNMAN_DEVICE_MODE_UNKNOWN:
699 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
700 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
702 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
703 if (device->carrier == TRUE && device->secondary == FALSE)
704 __connman_profile_add_device(device);
708 if (device->offlinemode == FALSE &&
709 device->powered_persistent == TRUE)
710 __connman_device_enable(device);
715 static void probe_driver(struct connman_element *element, gpointer user_data)
717 struct connman_device_driver *driver = user_data;
719 DBG("element %p name %s", element, element->name);
721 if (element->device == NULL)
724 if (element->device->driver != NULL)
727 if (driver->type != element->device->type)
730 if (driver->probe(element->device) < 0)
733 element->device->driver = driver;
735 setup_device(element->device);
738 static void remove_device(struct connman_device *device)
740 enum connman_service_type type;
742 DBG("device %p", device);
744 __connman_device_disable(device);
746 switch (device->mode) {
747 case CONNMAN_DEVICE_MODE_UNKNOWN:
748 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
749 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
751 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
752 if (device->secondary == FALSE)
753 __connman_profile_remove_device(device);
757 type = __connman_device_get_service_type(device);
758 __connman_notifier_unregister(type);
760 unregister_interface(&device->element);
762 if (device->driver->remove)
763 device->driver->remove(device);
765 device->driver = NULL;
768 static void remove_driver(struct connman_element *element, gpointer user_data)
770 struct connman_device_driver *driver = user_data;
772 DBG("element %p name %s", element, element->name);
774 if (element->device == NULL)
777 if (element->device->driver == driver)
778 remove_device(element->device);
781 connman_bool_t __connman_device_has_driver(struct connman_device *device)
783 if (device == NULL || device->driver == NULL)
786 return device->registered;
789 static GSList *driver_list = NULL;
791 static gint compare_priority(gconstpointer a, gconstpointer b)
793 const struct connman_device_driver *driver1 = a;
794 const struct connman_device_driver *driver2 = b;
796 return driver2->priority - driver1->priority;
800 * connman_device_driver_register:
801 * @driver: device driver definition
803 * Register a new device driver
805 * Returns: %0 on success
807 int connman_device_driver_register(struct connman_device_driver *driver)
809 DBG("driver %p name %s", driver, driver->name);
811 driver_list = g_slist_insert_sorted(driver_list, driver,
814 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
815 probe_driver, driver);
821 * connman_device_driver_unregister:
822 * @driver: device driver definition
824 * Remove a previously registered device driver
826 void connman_device_driver_unregister(struct connman_device_driver *driver)
828 DBG("driver %p name %s", driver, driver->name);
830 driver_list = g_slist_remove(driver_list, driver);
832 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
833 remove_driver, driver);
836 static void unregister_network(gpointer data)
838 struct connman_network *network = data;
840 DBG("network %p", network);
842 connman_element_unregister((struct connman_element *) network);
844 connman_network_unref(network);
847 static void device_destruct(struct connman_element *element)
849 struct connman_device *device = element->device;
851 DBG("element %p name %s", element, element->name);
853 if (device->timeout > 0) {
854 g_source_remove(device->timeout);
858 if (device->pending != NULL) {
859 dbus_message_unref(device->pending);
860 device->pending = NULL;
863 g_free(device->ident);
864 g_free(device->node);
865 g_free(device->name);
866 g_free(device->address);
867 g_free(device->control);
868 g_free(device->interface);
870 if (device->ipconfig != NULL) {
871 connman_ipconfig_unref(device->ipconfig);
872 device->ipconfig = NULL;
875 g_free(device->last_network);
877 g_hash_table_destroy(device->networks);
878 device->networks = NULL;
882 * connman_device_create:
883 * @node: device node name (for example an address)
886 * Allocate a new device of given #type and assign the #node name to it.
888 * Returns: a newly-allocated #connman_device structure
890 struct connman_device *connman_device_create(const char *node,
891 enum connman_device_type type)
893 struct connman_device *device;
896 DBG("node %s type %d", node, type);
898 device = g_try_new0(struct connman_device, 1);
902 DBG("device %p", device);
904 __connman_element_initialize(&device->element);
906 device->element.name = g_strdup(node);
907 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
909 device->element.device = device;
910 device->element.destruct = device_destruct;
912 str = type2string(type);
914 connman_element_set_string(&device->element,
915 CONNMAN_PROPERTY_ID_TYPE, str);
917 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
920 device->name = g_strdup(type2description(device->type));
921 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
922 device->secondary = FALSE;
924 device->powered_persistent = TRUE;
926 device->phyindex = -1;
929 case CONNMAN_DEVICE_TYPE_UNKNOWN:
930 case CONNMAN_DEVICE_TYPE_VENDOR:
931 device->scan_interval = 0;
933 case CONNMAN_DEVICE_TYPE_ETHERNET:
934 case CONNMAN_DEVICE_TYPE_WIFI:
935 device->scan_interval = 300;
937 case CONNMAN_DEVICE_TYPE_WIMAX:
938 device->scan_interval = 0;
940 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
941 device->scan_interval = 0;
943 case CONNMAN_DEVICE_TYPE_GPS:
944 device->scan_interval = 0;
946 case CONNMAN_DEVICE_TYPE_MBM:
947 case CONNMAN_DEVICE_TYPE_HSO:
948 case CONNMAN_DEVICE_TYPE_NOZOMI:
949 case CONNMAN_DEVICE_TYPE_HUAWEI:
950 case CONNMAN_DEVICE_TYPE_NOVATEL:
951 device->scan_interval = 0;
955 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
956 g_free, unregister_network);
962 * connman_device_ref:
963 * @device: device structure
965 * Increase reference counter of device
967 struct connman_device *connman_device_ref(struct connman_device *device)
969 if (connman_element_ref(&device->element) == NULL)
976 * connman_device_unref:
977 * @device: device structure
979 * Decrease reference counter of device
981 void connman_device_unref(struct connman_device *device)
983 connman_element_unref(&device->element);
986 const char *__connman_device_get_type(struct connman_device *device)
988 return type2string(device->type);
992 * connman_device_get_type:
993 * @device: device structure
997 enum connman_device_type connman_device_get_type(struct connman_device *device)
1003 * connman_device_get_name:
1004 * @device: device structure
1006 * Get unique name of device
1008 const char *connman_device_get_name(struct connman_device *device)
1010 return device->element.name;
1014 * connman_device_get_path:
1015 * @device: device structure
1017 * Get path name of device
1019 const char *connman_device_get_path(struct connman_device *device)
1021 return device->element.path;
1025 * connman_device_set_index:
1026 * @device: device structure
1027 * @index: index number
1029 * Set index number of device
1031 void connman_device_set_index(struct connman_device *device, int index)
1033 device->element.index = index;
1035 if (device->ipconfig != NULL) {
1036 if (index == connman_ipconfig_get_index(device->ipconfig))
1039 connman_ipconfig_unref(device->ipconfig);
1042 device->ipconfig = connman_ipconfig_create(index);
1046 * connman_device_get_index:
1047 * @device: device structure
1049 * Get index number of device
1051 int connman_device_get_index(struct connman_device *device)
1053 return device->element.index;
1056 int __connman_device_get_phyindex(struct connman_device *device)
1058 return device->phyindex;
1061 void __connman_device_set_phyindex(struct connman_device *device,
1064 device->phyindex = phyindex;
1068 * connman_device_set_interface:
1069 * @device: device structure
1070 * @interface: interface name
1071 * @control: control interface
1073 * Set interface name of device
1075 void connman_device_set_interface(struct connman_device *device,
1076 const char *interface, const char *control)
1078 g_free(device->element.devname);
1079 device->element.devname = g_strdup(interface);
1081 g_free(device->interface);
1082 device->interface = g_strdup(interface);
1084 g_free(device->control);
1085 device->control = g_strdup(control);
1087 if (device->name == NULL) {
1088 const char *str = type2description(device->type);
1089 if (str != NULL && device->interface != NULL)
1090 device->name = g_strdup_printf("%s (%s)", str,
1095 const char *connman_device_get_control(struct connman_device *device)
1097 return device->control;
1101 * connman_device_set_ident:
1102 * @device: device structure
1103 * @ident: unique identifier
1105 * Set unique identifier of device
1107 void connman_device_set_ident(struct connman_device *device,
1110 g_free(device->ident);
1111 device->ident = g_strdup(ident);
1114 const char *__connman_device_get_ident(struct connman_device *device)
1116 return device->ident;
1120 * connman_device_set_mode:
1121 * @device: device structure
1122 * @mode: network mode
1124 * Change network mode of device
1126 void connman_device_set_mode(struct connman_device *device,
1127 enum connman_device_mode mode)
1129 device->mode = mode;
1133 * connman_device_get_mode:
1134 * @device: device structure
1136 * Get network mode of device
1138 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1140 return device->mode;
1144 * connman_device_set_secondary:
1145 * @device: device structure
1146 * @secondary: secondary value
1148 * Change secondary value of device
1150 void connman_device_set_secondary(struct connman_device *device,
1151 connman_bool_t secondary)
1153 device->secondary = secondary;
1157 * connman_device_get_secondary:
1158 * @device: device structure
1160 * Get secondary value of device
1162 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1164 return device->secondary;
1168 * connman_device_set_powered:
1169 * @device: device structure
1170 * @powered: powered state
1172 * Change power state of device
1174 int connman_device_set_powered(struct connman_device *device,
1175 connman_bool_t powered)
1177 enum connman_service_type type;
1179 DBG("driver %p powered %d", device, powered);
1181 if (device->timeout > 0) {
1182 g_source_remove(device->timeout);
1183 device->timeout = 0;
1186 if (device->pending != NULL) {
1187 g_dbus_send_reply(connection, device->pending,
1190 dbus_message_unref(device->pending);
1191 device->pending = NULL;
1194 if (device->powered == powered)
1197 device->powered = powered;
1198 device->powered_pending = powered;
1200 type = __connman_device_get_service_type(device);
1202 if (device->powered == TRUE)
1203 __connman_notifier_enable(type);
1205 __connman_notifier_disable(type);
1207 if (device->registered == FALSE)
1210 powered_changed(device);
1212 if (powered == FALSE)
1215 reset_scan_trigger(device);
1217 if (device->driver->scan)
1218 device->driver->scan(device);
1223 int __connman_device_set_blocked(struct connman_device *device,
1224 connman_bool_t blocked)
1226 connman_bool_t powered;
1228 DBG("device %p blocked %d", device, blocked);
1230 device->blocked = blocked;
1232 if (device->offlinemode == TRUE)
1235 if (blocked == FALSE)
1236 powered = device->powered_persistent;
1240 return set_powered(device, powered);
1244 * connman_device_set_carrier:
1245 * @device: device structure
1246 * @carrier: carrier state
1248 * Change carrier state of device (only for device without scanning)
1250 int connman_device_set_carrier(struct connman_device *device,
1251 connman_bool_t carrier)
1253 DBG("device %p carrier %d", device, carrier);
1255 switch (device->mode) {
1256 case CONNMAN_DEVICE_MODE_UNKNOWN:
1257 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1258 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1260 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1264 if (device->carrier == carrier)
1267 device->carrier = carrier;
1269 return set_carrier(device, device->carrier);
1272 int __connman_device_scan(struct connman_device *device)
1274 if (!device->driver || !device->driver->scan)
1277 if (device->powered == FALSE)
1280 return device->driver->scan(device);
1283 int __connman_device_enable(struct connman_device *device)
1285 enum connman_service_type type;
1288 DBG("device %p", device);
1290 if (!device->driver || !device->driver->enable)
1293 if (device->powered_pending == TRUE)
1296 device->powered_pending = TRUE;
1298 err = device->driver->enable(device);
1302 device->powered = TRUE;
1304 type = __connman_device_get_service_type(device);
1305 __connman_notifier_enable(type);
1310 int __connman_device_enable_persistent(struct connman_device *device)
1312 DBG("device %p", device);
1314 device->powered_persistent = TRUE;
1316 __connman_storage_save_device(device);
1318 return __connman_device_enable(device);
1321 int __connman_device_disable(struct connman_device *device)
1323 enum connman_service_type type;
1326 DBG("device %p", device);
1328 if (!device->driver || !device->driver->disable)
1331 if (device->powered == FALSE)
1334 if (device->powered_pending == FALSE)
1337 device->powered_pending = FALSE;
1339 clear_scan_trigger(device);
1341 g_hash_table_remove_all(device->networks);
1343 err = device->driver->disable(device);
1347 device->powered = FALSE;
1349 type = __connman_device_get_service_type(device);
1350 __connman_notifier_disable(type);
1355 int __connman_device_disable_persistent(struct connman_device *device)
1357 DBG("device %p", device);
1359 device->powered_persistent = FALSE;
1361 __connman_storage_save_device(device);
1363 return __connman_device_disable(device);
1366 int __connman_device_connect(struct connman_device *device)
1368 DBG("device %p", device);
1370 if (device->disconnected == FALSE)
1373 if (device->driver && device->driver->connect)
1374 device->driver->connect(device);
1379 int __connman_device_disconnect(struct connman_device *device)
1381 GHashTableIter iter;
1382 gpointer key, value;
1384 DBG("device %p", device);
1386 connman_device_set_disconnected(device, TRUE);
1388 g_hash_table_iter_init(&iter, device->networks);
1390 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1391 struct connman_network *network = value;
1393 __connman_network_disconnect(network);
1396 if (device->driver && device->driver->disconnect)
1397 device->driver->disconnect(device);
1402 static void mark_network_unavailable(gpointer key, gpointer value,
1405 struct connman_network *network = value;
1407 if (connman_network_get_connected(network) == TRUE)
1410 connman_network_set_available(network, FALSE);
1413 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1416 struct connman_network *network = value;
1418 if (connman_network_get_connected(network) == TRUE)
1421 if (connman_network_get_available(network) == TRUE)
1427 void __connman_device_cleanup_networks(struct connman_device *device)
1429 g_hash_table_foreach_remove(device->networks,
1430 remove_unavailable_network, NULL);
1434 * connman_device_set_scanning:
1435 * @device: device structure
1436 * @scanning: scanning state
1438 * Change scanning state of device
1440 int connman_device_set_scanning(struct connman_device *device,
1441 connman_bool_t scanning)
1443 DBusMessage *signal;
1444 DBusMessageIter entry, value;
1445 const char *key = "Scanning";
1447 DBG("device %p scanning %d", device, scanning);
1449 if (!device->driver || !device->driver->scan)
1452 if (device->scanning == scanning)
1455 device->scanning = scanning;
1457 signal = dbus_message_new_signal(device->element.path,
1458 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1462 dbus_message_iter_init_append(signal, &entry);
1464 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1466 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1467 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1468 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1469 dbus_message_iter_close_container(&entry, &value);
1471 g_dbus_send_message(connection, signal);
1473 if (scanning == TRUE) {
1474 reset_scan_trigger(device);
1476 g_hash_table_foreach(device->networks,
1477 mark_network_unavailable, NULL);
1482 __connman_device_cleanup_networks(device);
1484 if (device->connections > 0)
1487 if (device->disconnected == TRUE)
1490 __connman_service_auto_connect();
1496 * connman_device_set_disconnected:
1497 * @device: device structure
1498 * @disconnected: disconnected state
1500 * Change disconnected state of device (only for device with networks)
1502 int connman_device_set_disconnected(struct connman_device *device,
1503 connman_bool_t disconnected)
1505 DBG("device %p disconnected %d", device, disconnected);
1507 switch (device->mode) {
1508 case CONNMAN_DEVICE_MODE_UNKNOWN:
1509 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1511 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1512 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1516 if (device->disconnected == disconnected)
1519 device->disconnected = disconnected;
1525 * connman_device_get_disconnected:
1526 * @device: device structure
1528 * Get device disconnected state
1530 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1532 return device->disconnected;
1536 * connman_device_set_connected:
1537 * @device: device structure
1538 * @connected: connected state
1540 * Change connected state of device (for Ethernet like devices)
1542 int connman_device_set_connected(struct connman_device *device,
1543 connman_bool_t connected)
1545 DBG("device %p connected %d", device, connected);
1547 switch (device->mode) {
1548 case CONNMAN_DEVICE_MODE_UNKNOWN:
1549 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1550 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1552 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1556 if (device->carrier == FALSE)
1559 return set_connected(device, connected);
1563 * connman_device_set_string:
1564 * @device: device structure
1565 * @key: unique identifier
1566 * @value: string value
1568 * Set string value for specific key
1570 int connman_device_set_string(struct connman_device *device,
1571 const char *key, const char *value)
1573 DBG("device %p key %s value %s", device, key, value);
1575 if (g_str_equal(key, "Address") == TRUE) {
1576 g_free(device->address);
1577 device->address = g_strdup(value);
1578 } else if (g_str_equal(key, "Name") == TRUE) {
1579 g_free(device->name);
1580 device->name = g_strdup(value);
1581 } else if (g_str_equal(key, "Node") == TRUE) {
1582 g_free(device->node);
1583 device->node = g_strdup(value);
1586 return connman_element_set_string(&device->element, key, value);
1590 * connman_device_get_string:
1591 * @device: device structure
1592 * @key: unique identifier
1594 * Get string value for specific key
1596 const char *connman_device_get_string(struct connman_device *device,
1599 DBG("device %p key %s", device, key);
1601 if (g_str_equal(key, "Address") == TRUE)
1602 return device->address;
1603 else if (g_str_equal(key, "Name") == TRUE)
1604 return device->name;
1605 else if (g_str_equal(key, "Node") == TRUE)
1606 return device->node;
1608 return connman_element_get_string(&device->element, key);
1611 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1613 struct connman_device *device = element->device;
1614 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1615 connman_bool_t powered;
1617 DBG("element %p name %s", element, element->name);
1622 device->offlinemode = offlinemode;
1624 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1626 if (device->powered == powered)
1629 if (device->powered_persistent == FALSE)
1632 set_powered(device, powered);
1635 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1637 DBG("offlinmode %d", offlinemode);
1639 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1640 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1642 __connman_notifier_offlinemode(offlinemode);
1647 void __connman_device_increase_connections(struct connman_device *device)
1649 device->connections++;
1652 void __connman_device_decrease_connections(struct connman_device *device)
1654 device->connections--;
1658 * connman_device_add_network:
1659 * @device: device structure
1660 * @network: network structure
1662 * Add new network to the device
1664 int connman_device_add_network(struct connman_device *device,
1665 struct connman_network *network)
1667 const char *identifier = connman_network_get_identifier(network);
1670 DBG("device %p network %p", device, network);
1672 switch (device->mode) {
1673 case CONNMAN_DEVICE_MODE_UNKNOWN:
1674 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1676 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1677 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1681 __connman_network_set_device(network, device);
1683 err = connman_element_register((struct connman_element *) network,
1686 __connman_network_set_device(network, NULL);
1690 g_hash_table_insert(device->networks, g_strdup(identifier),
1697 * connman_device_get_network:
1698 * @device: device structure
1699 * @identifier: network identifier
1701 * Get network for given identifier
1703 struct connman_network *connman_device_get_network(struct connman_device *device,
1704 const char *identifier)
1706 DBG("device %p identifier %s", device, identifier);
1708 return g_hash_table_lookup(device->networks, identifier);
1712 * connman_device_remove_network:
1713 * @device: device structure
1714 * @identifier: network identifier
1716 * Remove network for given identifier
1718 int connman_device_remove_network(struct connman_device *device,
1719 const char *identifier)
1721 DBG("device %p identifier %s", device, identifier);
1723 g_hash_table_remove(device->networks, identifier);
1728 void __connman_device_set_network(struct connman_device *device,
1729 struct connman_network *network)
1733 if (device->network == network)
1736 if (device->network != NULL)
1737 connman_network_unref(device->network);
1739 if (network != NULL) {
1740 name = connman_network_get_string(network,
1741 CONNMAN_PROPERTY_ID_NAME);
1742 g_free(device->last_network);
1743 device->last_network = g_strdup(name);
1745 device->network = connman_network_ref(network);
1747 g_free(device->last_network);
1748 device->last_network = NULL;
1750 device->network = NULL;
1755 * connman_device_register:
1756 * @device: device structure
1758 * Register device with the system
1760 int connman_device_register(struct connman_device *device)
1762 __connman_storage_load_device(device);
1764 device->offlinemode = __connman_profile_get_offlinemode();
1766 return connman_element_register(&device->element, NULL);
1770 * connman_device_unregister:
1771 * @device: device structure
1773 * Unregister device with the system
1775 void connman_device_unregister(struct connman_device *device)
1777 __connman_storage_save_device(device);
1779 connman_element_unregister(&device->element);
1783 * connman_device_get_data:
1784 * @device: device structure
1786 * Get private device data pointer
1788 void *connman_device_get_data(struct connman_device *device)
1790 return device->driver_data;
1794 * connman_device_set_data:
1795 * @device: device structure
1796 * @data: data pointer
1798 * Set private device data pointer
1800 void connman_device_set_data(struct connman_device *device, void *data)
1802 device->driver_data = data;
1805 static gboolean match_driver(struct connman_device *device,
1806 struct connman_device_driver *driver)
1808 if (device->type == driver->type ||
1809 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1815 static int device_probe(struct connman_element *element)
1817 struct connman_device *device = element->device;
1820 DBG("element %p name %s", element, element->name);
1825 if (device->driver != NULL)
1828 for (list = driver_list; list; list = list->next) {
1829 struct connman_device_driver *driver = list->data;
1831 if (match_driver(device, driver) == FALSE)
1834 DBG("driver %p name %s", driver, driver->name);
1836 if (driver->probe(device) == 0) {
1837 device->driver = driver;
1842 if (device->driver == NULL)
1845 return setup_device(device);
1848 static void device_remove(struct connman_element *element)
1850 struct connman_device *device = element->device;
1852 DBG("element %p name %s", element, element->name);
1857 if (device->driver == NULL)
1860 remove_device(device);
1863 static struct connman_driver device_driver = {
1865 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1866 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1867 .probe = device_probe,
1868 .remove = device_remove,
1871 static int device_load(struct connman_device *device)
1873 const char *ident = __connman_profile_active_ident();
1875 GError *error = NULL;
1877 connman_bool_t powered;
1880 DBG("device %p", device);
1882 keyfile = __connman_storage_open(ident);
1883 if (keyfile == NULL)
1886 identifier = g_strdup_printf("device_%s", device->element.name);
1887 if (identifier == NULL)
1890 powered = g_key_file_get_boolean(keyfile, identifier,
1893 device->powered_persistent = powered;
1894 g_clear_error(&error);
1896 switch (device->mode) {
1897 case CONNMAN_DEVICE_MODE_UNKNOWN:
1898 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1900 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1901 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1902 val = g_key_file_get_integer(keyfile, identifier,
1903 "ScanInterval", &error);
1904 if (error == NULL && val > 0)
1905 device->scan_interval = val;
1906 g_clear_error(&error);
1913 __connman_storage_close(ident, keyfile, FALSE);
1918 static int device_save(struct connman_device *device)
1920 const char *ident = __connman_profile_active_ident();
1924 DBG("device %p", device);
1926 keyfile = __connman_storage_open(ident);
1927 if (keyfile == NULL)
1930 identifier = g_strdup_printf("device_%s", device->element.name);
1931 if (identifier == NULL)
1934 g_key_file_set_boolean(keyfile, identifier,
1935 "Powered", device->powered_persistent);
1937 switch (device->mode) {
1938 case CONNMAN_DEVICE_MODE_UNKNOWN:
1939 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1941 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1942 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1943 if (device->scan_interval > 0)
1944 g_key_file_set_integer(keyfile, identifier,
1945 "ScanInterval", device->scan_interval);
1952 __connman_storage_close(ident, keyfile, TRUE);
1957 static struct connman_storage device_storage = {
1959 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1960 .device_load = device_load,
1961 .device_save = device_save,
1964 int __connman_device_init(void)
1968 connection = connman_dbus_get_connection();
1970 if (connman_storage_register(&device_storage) < 0)
1971 connman_error("Failed to register device storage");
1973 return connman_driver_register(&device_driver);
1976 void __connman_device_cleanup(void)
1980 connman_driver_unregister(&device_driver);
1982 connman_storage_unregister(&device_storage);
1984 dbus_connection_unref(connection);