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)
303 device->powered_pending = powered;
308 type = __connman_device_get_service_type(device);
310 if (powered == TRUE) {
311 if (driver->enable) {
312 err = driver->enable(device);
314 __connman_notifier_enable(type);
318 clear_scan_trigger(device);
320 g_hash_table_remove_all(device->networks);
322 set_carrier(device, FALSE);
324 if (driver->disable) {
325 err = driver->disable(device);
327 __connman_notifier_disable(type);
333 device->powered = powered;
335 if (device->registered == TRUE)
336 powered_changed(device);
342 static void append_path(gpointer key, gpointer value, gpointer user_data)
344 struct connman_element *element = value;
345 DBusMessageIter *iter = user_data;
347 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
351 static void append_networks(struct connman_device *device,
352 DBusMessageIter *entry)
354 DBusMessageIter value, iter;
355 const char *key = "Networks";
357 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
359 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
360 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
363 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
364 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
365 g_hash_table_foreach(device->networks, append_path, &iter);
366 dbus_message_iter_close_container(&value, &iter);
368 dbus_message_iter_close_container(entry, &value);
371 static DBusMessage *get_properties(DBusConnection *conn,
372 DBusMessage *msg, void *data)
374 struct connman_device *device = data;
376 DBusMessageIter array, dict, entry;
379 DBG("conn %p", conn);
381 if (__connman_security_check_privilege(msg,
382 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
383 return __connman_error_permission_denied(msg);
385 reply = dbus_message_new_method_return(msg);
389 dbus_message_iter_init_append(reply, &array);
391 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
392 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
393 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
394 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
396 if (device->name != NULL)
397 connman_dbus_dict_append_variant(&dict, "Name",
398 DBUS_TYPE_STRING, &device->name);
400 str = type2string(device->type);
402 connman_dbus_dict_append_variant(&dict, "Type",
403 DBUS_TYPE_STRING, &str);
405 if (device->address != NULL)
406 connman_dbus_dict_append_variant(&dict, "Address",
407 DBUS_TYPE_STRING, &device->address);
409 if (device->interface != NULL)
410 connman_dbus_dict_append_variant(&dict, "Interface",
411 DBUS_TYPE_STRING, &device->interface);
413 connman_dbus_dict_append_variant(&dict, "Powered",
414 DBUS_TYPE_BOOLEAN, &device->powered);
416 if (device->driver && device->driver->scan)
417 connman_dbus_dict_append_variant(&dict, "Scanning",
418 DBUS_TYPE_BOOLEAN, &device->scanning);
420 switch (device->mode) {
421 case CONNMAN_DEVICE_MODE_UNKNOWN:
423 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
424 __connman_element_append_ipv4(&device->element, &dict);
426 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
427 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
428 if (device->scan_interval > 0)
429 connman_dbus_dict_append_variant(&dict, "ScanInterval",
430 DBUS_TYPE_UINT16, &device->scan_interval);
432 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
434 append_networks(device, &entry);
435 dbus_message_iter_close_container(&dict, &entry);
439 dbus_message_iter_close_container(&array, &dict);
444 static gboolean powered_timeout(gpointer user_data)
446 struct connman_device *device = user_data;
448 DBG("device %p", device);
452 if (device->pending != NULL) {
455 reply = __connman_error_operation_timeout(device->pending);
457 g_dbus_send_message(connection, reply);
459 dbus_message_unref(device->pending);
460 device->pending = NULL;
466 static DBusMessage *set_property(DBusConnection *conn,
467 DBusMessage *msg, void *data)
469 struct connman_device *device = data;
470 DBusMessageIter iter, value;
474 DBG("conn %p", conn);
476 if (dbus_message_iter_init(msg, &iter) == FALSE)
477 return __connman_error_invalid_arguments(msg);
479 dbus_message_iter_get_basic(&iter, &name);
480 dbus_message_iter_next(&iter);
481 dbus_message_iter_recurse(&iter, &value);
483 if (__connman_security_check_privilege(msg,
484 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
485 return __connman_error_permission_denied(msg);
487 type = dbus_message_iter_get_arg_type(&value);
489 if (g_str_equal(name, "Powered") == TRUE) {
490 connman_bool_t powered;
493 if (type != DBUS_TYPE_BOOLEAN)
494 return __connman_error_invalid_arguments(msg);
496 dbus_message_iter_get_basic(&value, &powered);
498 device->powered_persistent = powered;
500 __connman_storage_save_device(device);
502 if (device->powered == powered)
503 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
505 if (device->pending != NULL)
506 return __connman_error_in_progress(msg);
508 err = set_powered(device, powered);
510 if (err != -EINPROGRESS)
511 return __connman_error_failed(msg, -err);
513 device->pending = dbus_message_ref(msg);
515 device->timeout = g_timeout_add_seconds(15,
516 powered_timeout, device);
520 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
521 connman_uint16_t interval;
523 switch (device->mode) {
524 case CONNMAN_DEVICE_MODE_UNKNOWN:
525 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
526 return __connman_error_invalid_arguments(msg);
527 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
528 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
532 if (type != DBUS_TYPE_UINT16)
533 return __connman_error_invalid_arguments(msg);
535 dbus_message_iter_get_basic(&value, &interval);
537 if (device->scan_interval != interval) {
538 device->scan_interval = interval;
540 __connman_storage_save_device(device);
542 reset_scan_trigger(device);
544 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
547 if (device->ipconfig == NULL)
548 return __connman_error_invalid_property(msg);
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)
679 enum connman_service_type type;
682 DBG("device %p", device);
684 err = register_interface(&device->element);
686 if (device->driver->remove)
687 device->driver->remove(device);
688 device->driver = NULL;
692 type = __connman_device_get_service_type(device);
693 __connman_notifier_register(type);
695 switch (device->mode) {
696 case CONNMAN_DEVICE_MODE_UNKNOWN:
697 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
698 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
700 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
701 if (device->carrier == TRUE && device->secondary == FALSE)
702 __connman_profile_add_device(device);
706 if (device->offlinemode == FALSE &&
707 device->powered_persistent == TRUE)
708 __connman_device_enable(device);
713 static void probe_driver(struct connman_element *element, gpointer user_data)
715 struct connman_device_driver *driver = user_data;
717 DBG("element %p name %s", element, element->name);
719 if (element->device == NULL)
722 if (element->device->driver != NULL)
725 if (driver->type != element->device->type)
728 if (driver->probe(element->device) < 0)
731 element->device->driver = driver;
733 setup_device(element->device);
736 static void remove_device(struct connman_device *device)
738 enum connman_service_type type;
740 DBG("device %p", device);
742 __connman_device_disable(device);
744 switch (device->mode) {
745 case CONNMAN_DEVICE_MODE_UNKNOWN:
746 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
747 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
749 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
750 if (device->secondary == FALSE)
751 __connman_profile_remove_device(device);
755 type = __connman_device_get_service_type(device);
756 __connman_notifier_unregister(type);
758 unregister_interface(&device->element);
760 if (device->driver->remove)
761 device->driver->remove(device);
763 device->driver = NULL;
766 static void remove_driver(struct connman_element *element, gpointer user_data)
768 struct connman_device_driver *driver = user_data;
770 DBG("element %p name %s", element, element->name);
772 if (element->device == NULL)
775 if (element->device->driver == driver)
776 remove_device(element->device);
779 connman_bool_t __connman_device_has_driver(struct connman_device *device)
781 if (device == NULL || device->driver == NULL)
784 return device->registered;
787 static GSList *driver_list = NULL;
789 static gint compare_priority(gconstpointer a, gconstpointer b)
791 const struct connman_device_driver *driver1 = a;
792 const struct connman_device_driver *driver2 = b;
794 return driver2->priority - driver1->priority;
798 * connman_device_driver_register:
799 * @driver: device driver definition
801 * Register a new device driver
803 * Returns: %0 on success
805 int connman_device_driver_register(struct connman_device_driver *driver)
807 DBG("driver %p name %s", driver, driver->name);
809 driver_list = g_slist_insert_sorted(driver_list, driver,
812 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
813 probe_driver, driver);
819 * connman_device_driver_unregister:
820 * @driver: device driver definition
822 * Remove a previously registered device driver
824 void connman_device_driver_unregister(struct connman_device_driver *driver)
826 DBG("driver %p name %s", driver, driver->name);
828 driver_list = g_slist_remove(driver_list, driver);
830 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
831 remove_driver, driver);
834 static void unregister_network(gpointer data)
836 struct connman_network *network = data;
838 DBG("network %p", network);
840 connman_element_unregister((struct connman_element *) network);
842 connman_network_unref(network);
845 static void device_destruct(struct connman_element *element)
847 struct connman_device *device = element->device;
849 DBG("element %p name %s", element, element->name);
851 if (device->timeout > 0) {
852 g_source_remove(device->timeout);
856 if (device->pending != NULL) {
857 dbus_message_unref(device->pending);
858 device->pending = NULL;
861 g_free(device->ident);
862 g_free(device->node);
863 g_free(device->name);
864 g_free(device->address);
865 g_free(device->control);
866 g_free(device->interface);
868 if (device->ipconfig != NULL) {
869 connman_ipconfig_unref(device->ipconfig);
870 device->ipconfig = NULL;
873 g_free(device->last_network);
875 g_hash_table_destroy(device->networks);
876 device->networks = NULL;
880 * connman_device_create:
881 * @node: device node name (for example an address)
884 * Allocate a new device of given #type and assign the #node name to it.
886 * Returns: a newly-allocated #connman_device structure
888 struct connman_device *connman_device_create(const char *node,
889 enum connman_device_type type)
891 struct connman_device *device;
894 DBG("node %s type %d", node, type);
896 device = g_try_new0(struct connman_device, 1);
900 DBG("device %p", device);
902 __connman_element_initialize(&device->element);
904 device->element.name = g_strdup(node);
905 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
907 device->element.device = device;
908 device->element.destruct = device_destruct;
910 str = type2string(type);
912 connman_element_set_string(&device->element,
913 CONNMAN_PROPERTY_ID_TYPE, str);
915 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
918 device->name = g_strdup(type2description(device->type));
919 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
920 device->secondary = FALSE;
922 device->powered_persistent = TRUE;
924 device->phyindex = -1;
927 case CONNMAN_DEVICE_TYPE_UNKNOWN:
928 case CONNMAN_DEVICE_TYPE_VENDOR:
929 device->scan_interval = 0;
931 case CONNMAN_DEVICE_TYPE_ETHERNET:
932 case CONNMAN_DEVICE_TYPE_WIFI:
933 device->scan_interval = 300;
935 case CONNMAN_DEVICE_TYPE_WIMAX:
936 device->scan_interval = 0;
938 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
939 device->scan_interval = 0;
941 case CONNMAN_DEVICE_TYPE_GPS:
942 device->scan_interval = 0;
944 case CONNMAN_DEVICE_TYPE_MBM:
945 case CONNMAN_DEVICE_TYPE_HSO:
946 case CONNMAN_DEVICE_TYPE_NOZOMI:
947 case CONNMAN_DEVICE_TYPE_HUAWEI:
948 case CONNMAN_DEVICE_TYPE_NOVATEL:
949 device->scan_interval = 0;
953 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
954 g_free, unregister_network);
960 * connman_device_ref:
961 * @device: device structure
963 * Increase reference counter of device
965 struct connman_device *connman_device_ref(struct connman_device *device)
967 if (connman_element_ref(&device->element) == NULL)
974 * connman_device_unref:
975 * @device: device structure
977 * Decrease reference counter of device
979 void connman_device_unref(struct connman_device *device)
981 connman_element_unref(&device->element);
984 const char *__connman_device_get_type(struct connman_device *device)
986 return type2string(device->type);
990 * connman_device_get_type:
991 * @device: device structure
995 enum connman_device_type connman_device_get_type(struct connman_device *device)
1001 * connman_device_get_name:
1002 * @device: device structure
1004 * Get unique name of device
1006 const char *connman_device_get_name(struct connman_device *device)
1008 return device->element.name;
1012 * connman_device_get_path:
1013 * @device: device structure
1015 * Get path name of device
1017 const char *connman_device_get_path(struct connman_device *device)
1019 return device->element.path;
1023 * connman_device_set_index:
1024 * @device: device structure
1025 * @index: index number
1027 * Set index number of device
1029 void connman_device_set_index(struct connman_device *device, int index)
1031 device->element.index = index;
1033 if (device->ipconfig != NULL) {
1034 if (index == connman_ipconfig_get_index(device->ipconfig))
1037 connman_ipconfig_unref(device->ipconfig);
1040 device->ipconfig = connman_ipconfig_create(index);
1044 * connman_device_get_index:
1045 * @device: device structure
1047 * Get index number of device
1049 int connman_device_get_index(struct connman_device *device)
1051 return device->element.index;
1054 int __connman_device_get_phyindex(struct connman_device *device)
1056 return device->phyindex;
1059 void __connman_device_set_phyindex(struct connman_device *device,
1062 device->phyindex = phyindex;
1066 * connman_device_set_interface:
1067 * @device: device structure
1068 * @interface: interface name
1069 * @control: control interface
1071 * Set interface name of device
1073 void connman_device_set_interface(struct connman_device *device,
1074 const char *interface, const char *control)
1076 g_free(device->element.devname);
1077 device->element.devname = g_strdup(interface);
1079 g_free(device->interface);
1080 device->interface = g_strdup(interface);
1082 g_free(device->control);
1083 device->control = g_strdup(control);
1085 if (device->name == NULL) {
1086 const char *str = type2description(device->type);
1087 if (str != NULL && device->interface != NULL)
1088 device->name = g_strdup_printf("%s (%s)", str,
1093 const char *connman_device_get_control(struct connman_device *device)
1095 return device->control;
1099 * connman_device_set_ident:
1100 * @device: device structure
1101 * @ident: unique identifier
1103 * Set unique identifier of device
1105 void connman_device_set_ident(struct connman_device *device,
1108 g_free(device->ident);
1109 device->ident = g_strdup(ident);
1112 const char *__connman_device_get_ident(struct connman_device *device)
1114 return device->ident;
1118 * connman_device_set_mode:
1119 * @device: device structure
1120 * @mode: network mode
1122 * Change network mode of device
1124 void connman_device_set_mode(struct connman_device *device,
1125 enum connman_device_mode mode)
1127 device->mode = mode;
1131 * connman_device_get_mode:
1132 * @device: device structure
1134 * Get network mode of device
1136 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1138 return device->mode;
1142 * connman_device_set_secondary:
1143 * @device: device structure
1144 * @secondary: secondary value
1146 * Change secondary value of device
1148 void connman_device_set_secondary(struct connman_device *device,
1149 connman_bool_t secondary)
1151 device->secondary = secondary;
1155 * connman_device_get_secondary:
1156 * @device: device structure
1158 * Get secondary value of device
1160 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1162 return device->secondary;
1166 * connman_device_set_powered:
1167 * @device: device structure
1168 * @powered: powered state
1170 * Change power state of device
1172 int connman_device_set_powered(struct connman_device *device,
1173 connman_bool_t powered)
1175 enum connman_service_type type;
1177 DBG("driver %p powered %d", device, powered);
1179 if (device->timeout > 0) {
1180 g_source_remove(device->timeout);
1181 device->timeout = 0;
1184 if (device->pending != NULL) {
1185 g_dbus_send_reply(connection, device->pending,
1188 dbus_message_unref(device->pending);
1189 device->pending = NULL;
1192 if (device->powered == powered)
1195 device->powered = powered;
1196 device->powered_pending = powered;
1198 type = __connman_device_get_service_type(device);
1200 if (device->powered == TRUE)
1201 __connman_notifier_enable(type);
1203 __connman_notifier_disable(type);
1205 if (device->registered == FALSE)
1208 powered_changed(device);
1210 if (powered == FALSE)
1213 reset_scan_trigger(device);
1215 if (device->driver->scan)
1216 device->driver->scan(device);
1221 int __connman_device_set_blocked(struct connman_device *device,
1222 connman_bool_t blocked)
1224 connman_bool_t powered;
1226 DBG("device %p blocked %d", device, blocked);
1228 device->blocked = blocked;
1230 if (device->offlinemode == TRUE)
1233 if (blocked == FALSE)
1234 powered = device->powered_persistent;
1238 return set_powered(device, powered);
1242 * connman_device_set_carrier:
1243 * @device: device structure
1244 * @carrier: carrier state
1246 * Change carrier state of device (only for device without scanning)
1248 int connman_device_set_carrier(struct connman_device *device,
1249 connman_bool_t carrier)
1251 DBG("device %p carrier %d", device, carrier);
1253 switch (device->mode) {
1254 case CONNMAN_DEVICE_MODE_UNKNOWN:
1255 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1256 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1258 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1262 if (device->carrier == carrier)
1265 device->carrier = carrier;
1267 return set_carrier(device, device->carrier);
1270 int __connman_device_scan(struct connman_device *device)
1272 if (!device->driver || !device->driver->scan)
1275 if (device->powered == FALSE)
1278 return device->driver->scan(device);
1281 int __connman_device_enable(struct connman_device *device)
1283 enum connman_service_type type;
1286 DBG("device %p", device);
1288 if (!device->driver || !device->driver->enable)
1291 if (device->powered_pending == TRUE)
1294 device->powered_pending = TRUE;
1296 err = device->driver->enable(device);
1300 device->powered = TRUE;
1302 type = __connman_device_get_service_type(device);
1303 __connman_notifier_enable(type);
1308 int __connman_device_enable_persistent(struct connman_device *device)
1310 DBG("device %p", device);
1312 device->powered_persistent = TRUE;
1314 __connman_storage_save_device(device);
1316 return __connman_device_enable(device);
1319 int __connman_device_disable(struct connman_device *device)
1321 enum connman_service_type type;
1324 DBG("device %p", device);
1326 if (!device->driver || !device->driver->disable)
1329 if (device->powered == FALSE)
1332 if (device->powered_pending == FALSE)
1335 device->powered_pending = FALSE;
1337 clear_scan_trigger(device);
1339 g_hash_table_remove_all(device->networks);
1341 err = device->driver->disable(device);
1345 device->powered = FALSE;
1347 type = __connman_device_get_service_type(device);
1348 __connman_notifier_disable(type);
1353 int __connman_device_disable_persistent(struct connman_device *device)
1355 DBG("device %p", device);
1357 device->powered_persistent = FALSE;
1359 __connman_storage_save_device(device);
1361 return __connman_device_disable(device);
1364 int __connman_device_connect(struct connman_device *device)
1366 DBG("device %p", device);
1368 if (device->disconnected == FALSE)
1371 if (device->driver && device->driver->connect)
1372 device->driver->connect(device);
1377 int __connman_device_disconnect(struct connman_device *device)
1379 GHashTableIter iter;
1380 gpointer key, value;
1382 DBG("device %p", device);
1384 connman_device_set_disconnected(device, TRUE);
1386 g_hash_table_iter_init(&iter, device->networks);
1388 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1389 struct connman_network *network = value;
1391 __connman_network_disconnect(network);
1394 if (device->driver && device->driver->disconnect)
1395 device->driver->disconnect(device);
1400 static void mark_network_unavailable(gpointer key, gpointer value,
1403 struct connman_network *network = value;
1405 if (connman_network_get_connected(network) == TRUE)
1408 connman_network_set_available(network, FALSE);
1411 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1414 struct connman_network *network = value;
1416 if (connman_network_get_connected(network) == TRUE)
1419 if (connman_network_get_available(network) == TRUE)
1425 void __connman_device_cleanup_networks(struct connman_device *device)
1427 g_hash_table_foreach_remove(device->networks,
1428 remove_unavailable_network, NULL);
1432 * connman_device_set_scanning:
1433 * @device: device structure
1434 * @scanning: scanning state
1436 * Change scanning state of device
1438 int connman_device_set_scanning(struct connman_device *device,
1439 connman_bool_t scanning)
1441 DBusMessage *signal;
1442 DBusMessageIter entry, value;
1443 const char *key = "Scanning";
1445 DBG("device %p scanning %d", device, scanning);
1447 if (!device->driver || !device->driver->scan)
1450 if (device->scanning == scanning)
1453 device->scanning = scanning;
1455 signal = dbus_message_new_signal(device->element.path,
1456 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1460 dbus_message_iter_init_append(signal, &entry);
1462 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1464 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1465 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1466 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1467 dbus_message_iter_close_container(&entry, &value);
1469 g_dbus_send_message(connection, signal);
1471 if (scanning == TRUE) {
1472 reset_scan_trigger(device);
1474 g_hash_table_foreach(device->networks,
1475 mark_network_unavailable, NULL);
1480 __connman_device_cleanup_networks(device);
1482 if (device->connections > 0)
1485 if (device->disconnected == TRUE)
1488 __connman_service_auto_connect();
1494 * connman_device_set_disconnected:
1495 * @device: device structure
1496 * @disconnected: disconnected state
1498 * Change disconnected state of device (only for device with networks)
1500 int connman_device_set_disconnected(struct connman_device *device,
1501 connman_bool_t disconnected)
1503 DBG("device %p disconnected %d", device, disconnected);
1505 switch (device->mode) {
1506 case CONNMAN_DEVICE_MODE_UNKNOWN:
1507 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1509 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1510 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1514 if (device->disconnected == disconnected)
1517 device->disconnected = disconnected;
1523 * connman_device_get_disconnected:
1524 * @device: device structure
1526 * Get device disconnected state
1528 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1530 return device->disconnected;
1534 * connman_device_set_connected:
1535 * @device: device structure
1536 * @connected: connected state
1538 * Change connected state of device (for Ethernet like devices)
1540 int connman_device_set_connected(struct connman_device *device,
1541 connman_bool_t connected)
1543 DBG("device %p connected %d", device, connected);
1545 switch (device->mode) {
1546 case CONNMAN_DEVICE_MODE_UNKNOWN:
1547 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1548 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1550 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1554 if (device->carrier == FALSE)
1557 return set_connected(device, connected);
1561 * connman_device_set_string:
1562 * @device: device structure
1563 * @key: unique identifier
1564 * @value: string value
1566 * Set string value for specific key
1568 int connman_device_set_string(struct connman_device *device,
1569 const char *key, const char *value)
1571 DBG("device %p key %s value %s", device, key, value);
1573 if (g_str_equal(key, "Address") == TRUE) {
1574 g_free(device->address);
1575 device->address = g_strdup(value);
1576 } else if (g_str_equal(key, "Name") == TRUE) {
1577 g_free(device->name);
1578 device->name = g_strdup(value);
1579 } else if (g_str_equal(key, "Node") == TRUE) {
1580 g_free(device->node);
1581 device->node = g_strdup(value);
1584 return connman_element_set_string(&device->element, key, value);
1588 * connman_device_get_string:
1589 * @device: device structure
1590 * @key: unique identifier
1592 * Get string value for specific key
1594 const char *connman_device_get_string(struct connman_device *device,
1597 DBG("device %p key %s", device, key);
1599 if (g_str_equal(key, "Address") == TRUE)
1600 return device->address;
1601 else if (g_str_equal(key, "Name") == TRUE)
1602 return device->name;
1603 else if (g_str_equal(key, "Node") == TRUE)
1604 return device->node;
1606 return connman_element_get_string(&device->element, key);
1609 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1611 struct connman_device *device = element->device;
1612 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1613 connman_bool_t powered;
1615 DBG("element %p name %s", element, element->name);
1620 device->offlinemode = offlinemode;
1622 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1624 if (device->powered == powered)
1627 if (device->powered_persistent == FALSE)
1630 set_powered(device, powered);
1633 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1635 DBG("offlinmode %d", offlinemode);
1637 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1638 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1640 __connman_notifier_offlinemode(offlinemode);
1645 void __connman_device_increase_connections(struct connman_device *device)
1647 device->connections++;
1650 void __connman_device_decrease_connections(struct connman_device *device)
1652 device->connections--;
1656 * connman_device_add_network:
1657 * @device: device structure
1658 * @network: network structure
1660 * Add new network to the device
1662 int connman_device_add_network(struct connman_device *device,
1663 struct connman_network *network)
1665 const char *identifier = connman_network_get_identifier(network);
1668 DBG("device %p network %p", device, network);
1670 switch (device->mode) {
1671 case CONNMAN_DEVICE_MODE_UNKNOWN:
1672 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1674 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1675 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1679 __connman_network_set_device(network, device);
1681 err = connman_element_register((struct connman_element *) network,
1684 __connman_network_set_device(network, NULL);
1688 g_hash_table_insert(device->networks, g_strdup(identifier),
1695 * connman_device_get_network:
1696 * @device: device structure
1697 * @identifier: network identifier
1699 * Get network for given identifier
1701 struct connman_network *connman_device_get_network(struct connman_device *device,
1702 const char *identifier)
1704 DBG("device %p identifier %s", device, identifier);
1706 return g_hash_table_lookup(device->networks, identifier);
1710 * connman_device_remove_network:
1711 * @device: device structure
1712 * @identifier: network identifier
1714 * Remove network for given identifier
1716 int connman_device_remove_network(struct connman_device *device,
1717 const char *identifier)
1719 DBG("device %p identifier %s", device, identifier);
1721 g_hash_table_remove(device->networks, identifier);
1726 void __connman_device_set_network(struct connman_device *device,
1727 struct connman_network *network)
1731 if (device->network == network)
1734 if (device->network != NULL)
1735 connman_network_unref(device->network);
1737 if (network != NULL) {
1738 name = connman_network_get_string(network,
1739 CONNMAN_PROPERTY_ID_NAME);
1740 g_free(device->last_network);
1741 device->last_network = g_strdup(name);
1743 device->network = connman_network_ref(network);
1745 g_free(device->last_network);
1746 device->last_network = NULL;
1748 device->network = NULL;
1753 * connman_device_register:
1754 * @device: device structure
1756 * Register device with the system
1758 int connman_device_register(struct connman_device *device)
1760 __connman_storage_load_device(device);
1762 device->offlinemode = __connman_profile_get_offlinemode();
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 __connman_storage_save_device(device);
1777 connman_element_unregister(&device->element);
1781 * connman_device_get_data:
1782 * @device: device structure
1784 * Get private device data pointer
1786 void *connman_device_get_data(struct connman_device *device)
1788 return device->driver_data;
1792 * connman_device_set_data:
1793 * @device: device structure
1794 * @data: data pointer
1796 * Set private device data pointer
1798 void connman_device_set_data(struct connman_device *device, void *data)
1800 device->driver_data = data;
1803 static gboolean match_driver(struct connman_device *device,
1804 struct connman_device_driver *driver)
1806 if (device->type == driver->type ||
1807 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1813 static int device_probe(struct connman_element *element)
1815 struct connman_device *device = element->device;
1818 DBG("element %p name %s", element, element->name);
1823 if (device->driver != NULL)
1826 for (list = driver_list; list; list = list->next) {
1827 struct connman_device_driver *driver = list->data;
1829 if (match_driver(device, driver) == FALSE)
1832 DBG("driver %p name %s", driver, driver->name);
1834 if (driver->probe(device) == 0) {
1835 device->driver = driver;
1840 if (device->driver == NULL)
1843 return setup_device(device);
1846 static void device_remove(struct connman_element *element)
1848 struct connman_device *device = element->device;
1850 DBG("element %p name %s", element, element->name);
1855 if (device->driver == NULL)
1858 remove_device(device);
1861 static struct connman_driver device_driver = {
1863 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1864 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1865 .probe = device_probe,
1866 .remove = device_remove,
1869 static int device_load(struct connman_device *device)
1871 const char *ident = __connman_profile_active_ident();
1873 GError *error = NULL;
1875 connman_bool_t powered;
1878 DBG("device %p", device);
1880 keyfile = __connman_storage_open(ident);
1881 if (keyfile == NULL)
1884 identifier = g_strdup_printf("device_%s", device->element.name);
1885 if (identifier == NULL)
1888 powered = g_key_file_get_boolean(keyfile, identifier,
1891 device->powered_persistent = powered;
1892 g_clear_error(&error);
1894 switch (device->mode) {
1895 case CONNMAN_DEVICE_MODE_UNKNOWN:
1896 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1898 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1899 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1900 val = g_key_file_get_integer(keyfile, identifier,
1901 "ScanInterval", &error);
1902 if (error == NULL && val > 0)
1903 device->scan_interval = val;
1904 g_clear_error(&error);
1911 __connman_storage_close(ident, keyfile, FALSE);
1916 static int device_save(struct connman_device *device)
1918 const char *ident = __connman_profile_active_ident();
1922 DBG("device %p", device);
1924 keyfile = __connman_storage_open(ident);
1925 if (keyfile == NULL)
1928 identifier = g_strdup_printf("device_%s", device->element.name);
1929 if (identifier == NULL)
1932 g_key_file_set_boolean(keyfile, identifier,
1933 "Powered", device->powered_persistent);
1935 switch (device->mode) {
1936 case CONNMAN_DEVICE_MODE_UNKNOWN:
1937 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1939 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1940 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1941 if (device->scan_interval > 0)
1942 g_key_file_set_integer(keyfile, identifier,
1943 "ScanInterval", device->scan_interval);
1950 __connman_storage_close(ident, keyfile, TRUE);
1955 static struct connman_storage device_storage = {
1957 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1958 .device_load = device_load,
1959 .device_save = device_save,
1962 int __connman_device_init(void)
1966 connection = connman_dbus_get_connection();
1968 if (connman_storage_register(&device_storage) < 0)
1969 connman_error("Failed to register device storage");
1971 return connman_driver_register(&device_driver);
1974 void __connman_device_cleanup(void)
1978 connman_driver_unregister(&device_driver);
1980 connman_storage_unregister(&device_storage);
1982 dbus_connection_unref(connection);