5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t secondary;
40 connman_bool_t offlinemode;
41 connman_bool_t blocked;
42 connman_bool_t powered;
43 connman_bool_t powered_pending;
44 connman_bool_t powered_persistent;
45 connman_bool_t carrier;
46 connman_bool_t scanning;
47 connman_bool_t disconnected;
48 connman_uint16_t scan_interval;
56 unsigned int connections;
58 struct connman_ipconfig *ipconfig;
60 struct connman_device_driver *driver;
63 connman_bool_t registered;
66 struct connman_network *network;
73 static gboolean device_scan_trigger(gpointer user_data)
75 struct connman_device *device = user_data;
77 DBG("device %p", device);
79 if (device->driver == NULL) {
80 device->scan_timeout = 0;
84 if (device->driver->scan)
85 device->driver->scan(device);
90 static void clear_scan_trigger(struct connman_device *device)
92 if (device->scan_timeout > 0) {
93 g_source_remove(device->scan_timeout);
94 device->scan_timeout = 0;
98 static void reset_scan_trigger(struct connman_device *device)
100 clear_scan_trigger(device);
102 if (device->scan_interval > 0) {
103 guint interval = device->scan_interval;
104 device->scan_timeout = g_timeout_add_seconds(interval,
105 device_scan_trigger, device);
109 static const char *type2description(enum connman_device_type type)
112 case CONNMAN_DEVICE_TYPE_UNKNOWN:
113 case CONNMAN_DEVICE_TYPE_VENDOR:
115 case CONNMAN_DEVICE_TYPE_ETHERNET:
117 case CONNMAN_DEVICE_TYPE_WIFI:
119 case CONNMAN_DEVICE_TYPE_WIMAX:
121 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
123 case CONNMAN_DEVICE_TYPE_GPS:
125 case CONNMAN_DEVICE_TYPE_MBM:
126 case CONNMAN_DEVICE_TYPE_HSO:
127 case CONNMAN_DEVICE_TYPE_NOZOMI:
128 case CONNMAN_DEVICE_TYPE_HUAWEI:
129 case CONNMAN_DEVICE_TYPE_NOVATEL:
136 static const char *type2string(enum connman_device_type type)
139 case CONNMAN_DEVICE_TYPE_UNKNOWN:
140 case CONNMAN_DEVICE_TYPE_VENDOR:
142 case CONNMAN_DEVICE_TYPE_ETHERNET:
144 case CONNMAN_DEVICE_TYPE_WIFI:
146 case CONNMAN_DEVICE_TYPE_WIMAX:
148 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
150 case CONNMAN_DEVICE_TYPE_GPS:
152 case CONNMAN_DEVICE_TYPE_MBM:
153 case CONNMAN_DEVICE_TYPE_HSO:
154 case CONNMAN_DEVICE_TYPE_HUAWEI:
155 case CONNMAN_DEVICE_TYPE_NOZOMI:
156 case CONNMAN_DEVICE_TYPE_NOVATEL:
163 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
165 enum connman_device_type type = connman_device_get_type(device);
168 case CONNMAN_DEVICE_TYPE_UNKNOWN:
169 case CONNMAN_DEVICE_TYPE_VENDOR:
170 case CONNMAN_DEVICE_TYPE_GPS:
171 case CONNMAN_DEVICE_TYPE_NOZOMI:
172 case CONNMAN_DEVICE_TYPE_HUAWEI:
173 case CONNMAN_DEVICE_TYPE_NOVATEL:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_MBM:
184 case CONNMAN_DEVICE_TYPE_HSO:
185 return CONNMAN_SERVICE_TYPE_CELLULAR;
188 return CONNMAN_SERVICE_TYPE_UNKNOWN;
191 static int set_connected(struct connman_device *device,
192 connman_bool_t connected)
194 if (connected == TRUE) {
195 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
196 struct connman_element *element;
198 device->disconnected = TRUE;
200 switch (device->element.ipv4.method) {
201 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
202 case CONNMAN_IPCONFIG_METHOD_OFF:
204 case CONNMAN_IPCONFIG_METHOD_STATIC:
205 type = CONNMAN_ELEMENT_TYPE_IPV4;
207 case CONNMAN_IPCONFIG_METHOD_DHCP:
208 type = CONNMAN_ELEMENT_TYPE_DHCP;
212 element = connman_element_create(NULL);
213 if (element != NULL) {
214 struct connman_service *service;
216 element->type = type;
217 element->index = device->element.index;
219 if (connman_element_register(element,
220 &device->element) < 0)
221 connman_element_unref(element);
223 device->disconnected = FALSE;
225 service = __connman_service_lookup_from_device(device);
226 __connman_service_indicate_state(service,
227 CONNMAN_SERVICE_STATE_CONFIGURATION);
230 struct connman_service *service;
232 connman_element_unregister_children(&device->element);
234 device->disconnected = TRUE;
236 service = __connman_service_lookup_from_device(device);
237 __connman_service_indicate_state(service,
238 CONNMAN_SERVICE_STATE_IDLE);
241 if (connected == TRUE) {
242 enum connman_service_type type;
244 type = __connman_device_get_service_type(device);
245 __connman_notifier_connect(type);
247 enum connman_service_type type;
249 type = __connman_device_get_service_type(device);
250 __connman_notifier_disconnect(type);
256 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
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;
334 powered_changed(device);
340 static void append_path(gpointer key, gpointer value, gpointer user_data)
342 struct connman_element *element = value;
343 DBusMessageIter *iter = user_data;
345 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
349 static void append_networks(struct connman_device *device,
350 DBusMessageIter *entry)
352 DBusMessageIter value, iter;
353 const char *key = "Networks";
355 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
357 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
358 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
361 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
362 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
363 g_hash_table_foreach(device->networks, append_path, &iter);
364 dbus_message_iter_close_container(&value, &iter);
366 dbus_message_iter_close_container(entry, &value);
369 static DBusMessage *get_properties(DBusConnection *conn,
370 DBusMessage *msg, void *data)
372 struct connman_device *device = data;
374 DBusMessageIter array, dict, entry;
377 DBG("conn %p", conn);
379 if (__connman_security_check_privilege(msg,
380 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
381 return __connman_error_permission_denied(msg);
383 reply = dbus_message_new_method_return(msg);
387 dbus_message_iter_init_append(reply, &array);
389 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
390 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
391 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
392 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
394 if (device->name != NULL)
395 connman_dbus_dict_append_variant(&dict, "Name",
396 DBUS_TYPE_STRING, &device->name);
398 str = type2string(device->type);
400 connman_dbus_dict_append_variant(&dict, "Type",
401 DBUS_TYPE_STRING, &str);
403 if (device->address != NULL)
404 connman_dbus_dict_append_variant(&dict, "Address",
405 DBUS_TYPE_STRING, &device->address);
407 if (device->interface != NULL)
408 connman_dbus_dict_append_variant(&dict, "Interface",
409 DBUS_TYPE_STRING, &device->interface);
411 connman_dbus_dict_append_variant(&dict, "Powered",
412 DBUS_TYPE_BOOLEAN, &device->powered);
414 if (device->driver && device->driver->scan)
415 connman_dbus_dict_append_variant(&dict, "Scanning",
416 DBUS_TYPE_BOOLEAN, &device->scanning);
418 switch (device->mode) {
419 case CONNMAN_DEVICE_MODE_UNKNOWN:
421 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
422 __connman_element_append_ipv4(&device->element, &dict);
424 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
425 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
426 if (device->scan_interval > 0)
427 connman_dbus_dict_append_variant(&dict, "ScanInterval",
428 DBUS_TYPE_UINT16, &device->scan_interval);
430 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
432 append_networks(device, &entry);
433 dbus_message_iter_close_container(&dict, &entry);
437 dbus_message_iter_close_container(&array, &dict);
442 static gboolean powered_timeout(gpointer user_data)
444 struct connman_device *device = user_data;
446 DBG("device %p", device);
450 if (device->pending != NULL) {
453 reply = __connman_error_operation_timeout(device->pending);
455 g_dbus_send_message(connection, reply);
457 dbus_message_unref(device->pending);
458 device->pending = NULL;
464 static DBusMessage *set_property(DBusConnection *conn,
465 DBusMessage *msg, void *data)
467 struct connman_device *device = data;
468 DBusMessageIter iter, value;
472 DBG("conn %p", conn);
474 if (dbus_message_iter_init(msg, &iter) == FALSE)
475 return __connman_error_invalid_arguments(msg);
477 dbus_message_iter_get_basic(&iter, &name);
478 dbus_message_iter_next(&iter);
479 dbus_message_iter_recurse(&iter, &value);
481 if (__connman_security_check_privilege(msg,
482 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
483 return __connman_error_permission_denied(msg);
485 type = dbus_message_iter_get_arg_type(&value);
487 if (g_str_equal(name, "Powered") == TRUE) {
488 connman_bool_t powered;
491 if (type != DBUS_TYPE_BOOLEAN)
492 return __connman_error_invalid_arguments(msg);
494 dbus_message_iter_get_basic(&value, &powered);
496 device->powered_persistent = powered;
498 __connman_storage_save_device(device);
500 if (device->powered == powered)
501 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
503 if (device->pending != NULL)
504 return __connman_error_in_progress(msg);
506 err = set_powered(device, powered);
508 if (err != -EINPROGRESS)
509 return __connman_error_failed(msg, -err);
511 device->pending = dbus_message_ref(msg);
513 device->timeout = g_timeout_add_seconds(15,
514 powered_timeout, device);
518 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
519 connman_uint16_t interval;
521 switch (device->mode) {
522 case CONNMAN_DEVICE_MODE_UNKNOWN:
523 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
524 return __connman_error_invalid_arguments(msg);
525 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
526 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
530 if (type != DBUS_TYPE_UINT16)
531 return __connman_error_invalid_arguments(msg);
533 dbus_message_iter_get_basic(&value, &interval);
535 if (device->scan_interval != interval) {
536 device->scan_interval = interval;
538 __connman_storage_save_device(device);
540 reset_scan_trigger(device);
542 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
545 switch (device->mode) {
546 case CONNMAN_DEVICE_MODE_UNKNOWN:
547 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
548 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
549 return __connman_error_invalid_arguments(msg);
550 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
554 err = __connman_ipconfig_set_ipv4(device->ipconfig,
557 return __connman_error_failed(msg, -err);
559 return __connman_error_invalid_property(msg);
561 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
564 static DBusMessage *propose_scan(DBusConnection *conn,
565 DBusMessage *msg, void *data)
567 struct connman_device *device = data;
570 DBG("conn %p", conn);
572 switch (device->mode) {
573 case CONNMAN_DEVICE_MODE_UNKNOWN:
574 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
575 return __connman_error_not_supported(msg);
576 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
577 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
581 err = __connman_device_scan(device);
583 return __connman_error_failed(msg, -err);
585 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
588 static GDBusMethodTable device_methods[] = {
589 { "GetProperties", "", "a{sv}", get_properties },
590 { "SetProperty", "sv", "", set_property,
591 G_DBUS_METHOD_FLAG_ASYNC },
592 { "ProposeScan", "", "", propose_scan },
596 static GDBusSignalTable device_signals[] = {
597 { "PropertyChanged", "sv" },
601 static void append_devices(DBusMessageIter *entry)
603 DBusMessageIter value, iter;
604 const char *key = "Devices";
606 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
608 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
609 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
612 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
613 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
614 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
615 dbus_message_iter_close_container(&value, &iter);
617 dbus_message_iter_close_container(entry, &value);
620 static void emit_devices_signal(void)
623 DBusMessageIter entry;
625 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
626 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
630 dbus_message_iter_init_append(signal, &entry);
632 append_devices(&entry);
634 g_dbus_send_message(connection, signal);
637 static int register_interface(struct connman_element *element)
639 struct connman_device *device = element->device;
641 DBG("element %p name %s", element, element->name);
643 if (g_dbus_register_interface(connection, element->path,
644 CONNMAN_DEVICE_INTERFACE,
645 device_methods, device_signals,
646 NULL, device, NULL) == FALSE) {
647 connman_error("Failed to register %s device", element->path);
651 device->registered = TRUE;
653 emit_devices_signal();
658 static void unregister_interface(struct connman_element *element)
660 struct connman_device *device = element->device;
662 DBG("element %p name %s", element, element->name);
664 device->registered = FALSE;
666 emit_devices_signal();
668 g_dbus_unregister_interface(connection, element->path,
669 CONNMAN_DEVICE_INTERFACE);
672 static int setup_device(struct connman_device *device)
674 enum connman_service_type type;
677 DBG("device %p", device);
679 err = register_interface(&device->element);
681 if (device->driver->remove)
682 device->driver->remove(device);
683 device->driver = NULL;
687 type = __connman_device_get_service_type(device);
688 __connman_notifier_register(type);
690 switch (device->mode) {
691 case CONNMAN_DEVICE_MODE_UNKNOWN:
692 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
693 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
695 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
696 if (device->carrier == TRUE && device->secondary == FALSE)
697 __connman_profile_add_device(device);
701 if (device->offlinemode == FALSE &&
702 device->powered_persistent == TRUE)
703 __connman_device_enable(device);
708 static void probe_driver(struct connman_element *element, gpointer user_data)
710 struct connman_device_driver *driver = user_data;
712 DBG("element %p name %s", element, element->name);
714 if (element->device == NULL)
717 if (element->device->driver != NULL)
720 if (driver->type != element->device->type)
723 if (driver->probe(element->device) < 0)
726 element->device->driver = driver;
728 setup_device(element->device);
731 static void remove_device(struct connman_device *device)
733 enum connman_service_type type;
735 DBG("device %p", device);
737 __connman_device_disable(device);
739 switch (device->mode) {
740 case CONNMAN_DEVICE_MODE_UNKNOWN:
741 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
742 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
744 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
745 if (device->secondary == FALSE)
746 __connman_profile_remove_device(device);
750 type = __connman_device_get_service_type(device);
751 __connman_notifier_unregister(type);
753 unregister_interface(&device->element);
755 if (device->driver->remove)
756 device->driver->remove(device);
758 device->driver = NULL;
761 static void remove_driver(struct connman_element *element, gpointer user_data)
763 struct connman_device_driver *driver = user_data;
765 DBG("element %p name %s", element, element->name);
767 if (element->device == NULL)
770 if (element->device->driver == driver)
771 remove_device(element->device);
774 connman_bool_t __connman_device_has_driver(struct connman_device *device)
776 if (device == NULL || device->driver == NULL)
779 return device->registered;
782 static GSList *driver_list = NULL;
784 static gint compare_priority(gconstpointer a, gconstpointer b)
786 const struct connman_device_driver *driver1 = a;
787 const struct connman_device_driver *driver2 = b;
789 return driver2->priority - driver1->priority;
793 * connman_device_driver_register:
794 * @driver: device driver definition
796 * Register a new device driver
798 * Returns: %0 on success
800 int connman_device_driver_register(struct connman_device_driver *driver)
802 DBG("driver %p name %s", driver, driver->name);
804 driver_list = g_slist_insert_sorted(driver_list, driver,
807 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
808 probe_driver, driver);
814 * connman_device_driver_unregister:
815 * @driver: device driver definition
817 * Remove a previously registered device driver
819 void connman_device_driver_unregister(struct connman_device_driver *driver)
821 DBG("driver %p name %s", driver, driver->name);
823 driver_list = g_slist_remove(driver_list, driver);
825 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
826 remove_driver, driver);
829 static void unregister_network(gpointer data)
831 struct connman_network *network = data;
833 DBG("network %p", network);
835 connman_element_unregister((struct connman_element *) network);
837 connman_network_unref(network);
840 static void device_destruct(struct connman_element *element)
842 struct connman_device *device = element->device;
844 DBG("element %p name %s", element, element->name);
846 if (device->timeout > 0) {
847 g_source_remove(device->timeout);
851 if (device->pending != NULL) {
852 dbus_message_unref(device->pending);
853 device->pending = NULL;
856 g_free(device->ident);
857 g_free(device->node);
858 g_free(device->name);
859 g_free(device->address);
860 g_free(device->control);
861 g_free(device->interface);
863 connman_ipconfig_unref(device->ipconfig);
865 g_free(device->last_network);
867 g_hash_table_destroy(device->networks);
868 device->networks = NULL;
872 * connman_device_create:
873 * @node: device node name (for example an address)
876 * Allocate a new device of given #type and assign the #node name to it.
878 * Returns: a newly-allocated #connman_device structure
880 struct connman_device *connman_device_create(const char *node,
881 enum connman_device_type type)
883 struct connman_device *device;
886 DBG("node %s type %d", node, type);
888 device = g_try_new0(struct connman_device, 1);
892 DBG("device %p", device);
894 __connman_element_initialize(&device->element);
896 device->element.name = g_strdup(node);
897 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
899 device->element.device = device;
900 device->element.destruct = device_destruct;
902 str = type2string(type);
904 connman_element_set_string(&device->element,
905 CONNMAN_PROPERTY_ID_TYPE, str);
907 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
910 device->name = g_strdup(type2description(device->type));
911 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
912 device->secondary = FALSE;
914 device->powered_persistent = TRUE;
916 device->phyindex = -1;
919 case CONNMAN_DEVICE_TYPE_UNKNOWN:
920 case CONNMAN_DEVICE_TYPE_VENDOR:
921 device->scan_interval = 0;
923 case CONNMAN_DEVICE_TYPE_ETHERNET:
924 case CONNMAN_DEVICE_TYPE_WIFI:
925 device->scan_interval = 300;
927 case CONNMAN_DEVICE_TYPE_WIMAX:
928 device->scan_interval = 0;
930 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
931 device->scan_interval = 0;
933 case CONNMAN_DEVICE_TYPE_GPS:
934 device->scan_interval = 0;
936 case CONNMAN_DEVICE_TYPE_MBM:
937 case CONNMAN_DEVICE_TYPE_HSO:
938 case CONNMAN_DEVICE_TYPE_NOZOMI:
939 case CONNMAN_DEVICE_TYPE_HUAWEI:
940 case CONNMAN_DEVICE_TYPE_NOVATEL:
941 device->scan_interval = 0;
945 device->ipconfig = connman_ipconfig_create();
946 if (device->ipconfig == NULL) {
947 connman_device_unref(device);
951 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
952 g_free, unregister_network);
958 * connman_device_ref:
959 * @device: device structure
961 * Increase reference counter of device
963 struct connman_device *connman_device_ref(struct connman_device *device)
965 if (connman_element_ref(&device->element) == NULL)
972 * connman_device_unref:
973 * @device: device structure
975 * Decrease reference counter of device
977 void connman_device_unref(struct connman_device *device)
979 connman_element_unref(&device->element);
982 const char *__connman_device_get_type(struct connman_device *device)
984 return type2string(device->type);
988 * connman_device_get_type:
989 * @device: device structure
993 enum connman_device_type connman_device_get_type(struct connman_device *device)
999 * connman_device_get_name:
1000 * @device: device structure
1002 * Get unique name of device
1004 const char *connman_device_get_name(struct connman_device *device)
1006 return device->element.name;
1010 * connman_device_get_path:
1011 * @device: device structure
1013 * Get path name of device
1015 const char *connman_device_get_path(struct connman_device *device)
1017 return device->element.path;
1021 * connman_device_set_index:
1022 * @device: device structure
1023 * @index: index number
1025 * Set index number of device
1027 void connman_device_set_index(struct connman_device *device, int index)
1029 device->element.index = index;
1033 * connman_device_get_index:
1034 * @device: device structure
1036 * Get index number of device
1038 int connman_device_get_index(struct connman_device *device)
1040 return device->element.index;
1043 int __connman_device_get_phyindex(struct connman_device *device)
1045 return device->phyindex;
1048 void __connman_device_set_phyindex(struct connman_device *device,
1051 device->phyindex = phyindex;
1055 * connman_device_set_interface:
1056 * @device: device structure
1057 * @interface: interface name
1058 * @control: control interface
1060 * Set interface name of device
1062 void connman_device_set_interface(struct connman_device *device,
1063 const char *interface, const char *control)
1065 g_free(device->element.devname);
1066 device->element.devname = g_strdup(interface);
1068 g_free(device->interface);
1069 device->interface = g_strdup(interface);
1071 g_free(device->control);
1072 device->control = g_strdup(control);
1074 if (device->name == NULL) {
1075 const char *str = type2description(device->type);
1076 if (str != NULL && device->interface != NULL)
1077 device->name = g_strdup_printf("%s (%s)", str,
1082 const char *connman_device_get_control(struct connman_device *device)
1084 return device->control;
1088 * connman_device_set_ident:
1089 * @device: device structure
1090 * @ident: unique identifier
1092 * Set unique identifier of device
1094 void connman_device_set_ident(struct connman_device *device,
1097 g_free(device->ident);
1098 device->ident = g_strdup(ident);
1101 const char *__connman_device_get_ident(struct connman_device *device)
1103 return device->ident;
1107 * connman_device_set_mode:
1108 * @device: device structure
1109 * @mode: network mode
1111 * Change network mode of device
1113 void connman_device_set_mode(struct connman_device *device,
1114 enum connman_device_mode mode)
1116 device->mode = mode;
1120 * connman_device_get_mode:
1121 * @device: device structure
1123 * Get network mode of device
1125 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1127 return device->mode;
1131 * connman_device_set_secondary:
1132 * @device: device structure
1133 * @secondary: secondary value
1135 * Change secondary value of device
1137 void connman_device_set_secondary(struct connman_device *device,
1138 connman_bool_t secondary)
1140 device->secondary = secondary;
1144 * connman_device_get_secondary:
1145 * @device: device structure
1147 * Get secondary value of device
1149 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1151 return device->secondary;
1155 * connman_device_set_powered:
1156 * @device: device structure
1157 * @powered: powered state
1159 * Change power state of device
1161 int connman_device_set_powered(struct connman_device *device,
1162 connman_bool_t powered)
1164 enum connman_service_type type;
1166 DBG("driver %p powered %d", device, powered);
1168 if (device->timeout > 0) {
1169 g_source_remove(device->timeout);
1170 device->timeout = 0;
1173 if (device->pending != NULL) {
1174 g_dbus_send_reply(connection, device->pending,
1177 dbus_message_unref(device->pending);
1178 device->pending = NULL;
1181 if (device->powered == powered)
1184 device->powered = powered;
1185 device->powered_pending = powered;
1187 type = __connman_device_get_service_type(device);
1189 if (device->powered == TRUE)
1190 __connman_notifier_enable(type);
1192 __connman_notifier_disable(type);
1194 if (device->registered == FALSE)
1197 powered_changed(device);
1199 if (powered == FALSE)
1202 reset_scan_trigger(device);
1204 if (device->driver->scan)
1205 device->driver->scan(device);
1210 int __connman_device_set_blocked(struct connman_device *device,
1211 connman_bool_t blocked)
1213 connman_bool_t powered;
1215 DBG("device %p blocked %d", device, blocked);
1217 device->blocked = blocked;
1219 if (device->offlinemode == TRUE)
1222 if (blocked == FALSE)
1223 powered = device->powered_persistent;
1227 return set_powered(device, powered);
1231 * connman_device_set_carrier:
1232 * @device: device structure
1233 * @carrier: carrier state
1235 * Change carrier state of device (only for device without scanning)
1237 int connman_device_set_carrier(struct connman_device *device,
1238 connman_bool_t carrier)
1240 DBG("device %p carrier %d", device, carrier);
1242 switch (device->mode) {
1243 case CONNMAN_DEVICE_MODE_UNKNOWN:
1244 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1245 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1247 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1251 if (device->carrier == carrier)
1254 device->carrier = carrier;
1256 return set_carrier(device, device->carrier);
1259 int __connman_device_scan(struct connman_device *device)
1261 if (!device->driver || !device->driver->scan)
1264 if (device->powered == FALSE)
1267 return device->driver->scan(device);
1270 int __connman_device_enable(struct connman_device *device)
1272 enum connman_service_type type;
1275 DBG("device %p", device);
1277 if (!device->driver || !device->driver->enable)
1280 if (device->powered_pending == TRUE)
1283 device->powered_pending = TRUE;
1285 err = device->driver->enable(device);
1289 device->powered = TRUE;
1291 type = __connman_device_get_service_type(device);
1292 __connman_notifier_enable(type);
1297 int __connman_device_enable_persistent(struct connman_device *device)
1299 DBG("device %p", device);
1301 device->powered_persistent = TRUE;
1303 __connman_storage_save_device(device);
1305 return __connman_device_enable(device);
1308 int __connman_device_disable(struct connman_device *device)
1310 enum connman_service_type type;
1313 DBG("device %p", device);
1315 if (!device->driver || !device->driver->disable)
1318 if (device->powered == FALSE)
1321 if (device->powered_pending == FALSE)
1324 device->powered_pending = FALSE;
1326 clear_scan_trigger(device);
1328 g_hash_table_remove_all(device->networks);
1330 err = device->driver->disable(device);
1334 device->powered = FALSE;
1336 type = __connman_device_get_service_type(device);
1337 __connman_notifier_disable(type);
1342 int __connman_device_disable_persistent(struct connman_device *device)
1344 DBG("device %p", device);
1346 device->powered_persistent = FALSE;
1348 __connman_storage_save_device(device);
1350 return __connman_device_disable(device);
1353 int __connman_device_connect(struct connman_device *device)
1355 DBG("device %p", device);
1357 if (device->disconnected == FALSE)
1360 if (device->driver && device->driver->connect)
1361 device->driver->connect(device);
1366 int __connman_device_disconnect(struct connman_device *device)
1368 GHashTableIter iter;
1369 gpointer key, value;
1371 DBG("device %p", device);
1373 connman_device_set_disconnected(device, TRUE);
1375 g_hash_table_iter_init(&iter, device->networks);
1377 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1378 struct connman_network *network = value;
1380 __connman_network_disconnect(network);
1383 if (device->driver && device->driver->disconnect)
1384 device->driver->disconnect(device);
1389 static void mark_network_unavailable(gpointer key, gpointer value,
1392 struct connman_network *network = value;
1394 if (connman_network_get_connected(network) == TRUE)
1397 connman_network_set_available(network, FALSE);
1400 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1403 struct connman_network *network = value;
1405 if (connman_network_get_connected(network) == TRUE)
1408 if (connman_network_get_available(network) == TRUE)
1414 void __connman_device_cleanup_networks(struct connman_device *device)
1416 g_hash_table_foreach_remove(device->networks,
1417 remove_unavailable_network, NULL);
1421 * connman_device_set_scanning:
1422 * @device: device structure
1423 * @scanning: scanning state
1425 * Change scanning state of device
1427 int connman_device_set_scanning(struct connman_device *device,
1428 connman_bool_t scanning)
1430 DBusMessage *signal;
1431 DBusMessageIter entry, value;
1432 const char *key = "Scanning";
1434 DBG("device %p scanning %d", device, scanning);
1436 if (!device->driver || !device->driver->scan)
1439 if (device->scanning == scanning)
1442 device->scanning = scanning;
1444 signal = dbus_message_new_signal(device->element.path,
1445 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1449 dbus_message_iter_init_append(signal, &entry);
1451 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1453 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1454 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1455 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1456 dbus_message_iter_close_container(&entry, &value);
1458 g_dbus_send_message(connection, signal);
1460 if (scanning == TRUE) {
1461 reset_scan_trigger(device);
1463 g_hash_table_foreach(device->networks,
1464 mark_network_unavailable, NULL);
1469 __connman_device_cleanup_networks(device);
1471 if (device->connections > 0)
1474 if (device->disconnected == TRUE)
1477 __connman_service_auto_connect();
1483 * connman_device_set_disconnected:
1484 * @device: device structure
1485 * @disconnected: disconnected state
1487 * Change disconnected state of device (only for device with networks)
1489 int connman_device_set_disconnected(struct connman_device *device,
1490 connman_bool_t disconnected)
1492 DBG("device %p disconnected %d", device, disconnected);
1494 switch (device->mode) {
1495 case CONNMAN_DEVICE_MODE_UNKNOWN:
1496 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1498 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1499 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1503 if (device->disconnected == disconnected)
1506 device->disconnected = disconnected;
1512 * connman_device_get_disconnected:
1513 * @device: device structure
1515 * Get device disconnected state
1517 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1519 return device->disconnected;
1523 * connman_device_set_connected:
1524 * @device: device structure
1525 * @connected: connected state
1527 * Change connected state of device (for Ethernet like devices)
1529 int connman_device_set_connected(struct connman_device *device,
1530 connman_bool_t connected)
1532 DBG("device %p connected %d", device, connected);
1534 switch (device->mode) {
1535 case CONNMAN_DEVICE_MODE_UNKNOWN:
1536 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1537 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1539 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1543 if (device->carrier == FALSE)
1546 return set_connected(device, connected);
1550 * connman_device_set_string:
1551 * @device: device structure
1552 * @key: unique identifier
1553 * @value: string value
1555 * Set string value for specific key
1557 int connman_device_set_string(struct connman_device *device,
1558 const char *key, const char *value)
1560 DBG("device %p key %s value %s", device, key, value);
1562 if (g_str_equal(key, "Address") == TRUE) {
1563 g_free(device->address);
1564 device->address = g_strdup(value);
1565 } else if (g_str_equal(key, "Name") == TRUE) {
1566 g_free(device->name);
1567 device->name = g_strdup(value);
1568 } else if (g_str_equal(key, "Node") == TRUE) {
1569 g_free(device->node);
1570 device->node = g_strdup(value);
1573 return connman_element_set_string(&device->element, key, value);
1577 * connman_device_get_string:
1578 * @device: device structure
1579 * @key: unique identifier
1581 * Get string value for specific key
1583 const char *connman_device_get_string(struct connman_device *device,
1586 DBG("device %p key %s", device, key);
1588 if (g_str_equal(key, "Address") == TRUE)
1589 return device->address;
1590 else if (g_str_equal(key, "Name") == TRUE)
1591 return device->name;
1592 else if (g_str_equal(key, "Node") == TRUE)
1593 return device->node;
1595 return connman_element_get_string(&device->element, key);
1598 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1600 struct connman_device *device = element->device;
1601 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1602 connman_bool_t powered;
1604 DBG("element %p name %s", element, element->name);
1609 device->offlinemode = offlinemode;
1611 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1613 if (device->powered == powered)
1616 if (device->powered_persistent == FALSE)
1619 set_powered(device, powered);
1622 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1624 DBG("offlinmode %d", offlinemode);
1626 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1627 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1629 __connman_notifier_offlinemode(offlinemode);
1634 void __connman_device_increase_connections(struct connman_device *device)
1636 device->connections++;
1639 void __connman_device_decrease_connections(struct connman_device *device)
1641 device->connections--;
1645 * connman_device_add_network:
1646 * @device: device structure
1647 * @network: network structure
1649 * Add new network to the device
1651 int connman_device_add_network(struct connman_device *device,
1652 struct connman_network *network)
1654 const char *identifier = connman_network_get_identifier(network);
1657 DBG("device %p network %p", device, network);
1659 switch (device->mode) {
1660 case CONNMAN_DEVICE_MODE_UNKNOWN:
1661 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1663 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1664 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1668 __connman_network_set_device(network, device);
1670 err = connman_element_register((struct connman_element *) network,
1673 __connman_network_set_device(network, NULL);
1677 g_hash_table_insert(device->networks, g_strdup(identifier),
1684 * connman_device_get_network:
1685 * @device: device structure
1686 * @identifier: network identifier
1688 * Get network for given identifier
1690 struct connman_network *connman_device_get_network(struct connman_device *device,
1691 const char *identifier)
1693 DBG("device %p identifier %s", device, identifier);
1695 return g_hash_table_lookup(device->networks, identifier);
1699 * connman_device_remove_network:
1700 * @device: device structure
1701 * @identifier: network identifier
1703 * Remove network for given identifier
1705 int connman_device_remove_network(struct connman_device *device,
1706 const char *identifier)
1708 DBG("device %p identifier %s", device, identifier);
1710 g_hash_table_remove(device->networks, identifier);
1715 void __connman_device_set_network(struct connman_device *device,
1716 struct connman_network *network)
1720 if (device->network == network)
1723 if (device->network != NULL)
1724 connman_network_unref(device->network);
1726 if (network != NULL) {
1727 name = connman_network_get_string(network,
1728 CONNMAN_PROPERTY_ID_NAME);
1729 g_free(device->last_network);
1730 device->last_network = g_strdup(name);
1732 device->network = connman_network_ref(network);
1734 g_free(device->last_network);
1735 device->last_network = NULL;
1737 device->network = NULL;
1742 * connman_device_register:
1743 * @device: device structure
1745 * Register device with the system
1747 int connman_device_register(struct connman_device *device)
1749 __connman_storage_load_device(device);
1751 device->offlinemode = __connman_manager_get_offlinemode();
1753 return connman_element_register(&device->element, NULL);
1757 * connman_device_unregister:
1758 * @device: device structure
1760 * Unregister device with the system
1762 void connman_device_unregister(struct connman_device *device)
1764 __connman_storage_save_device(device);
1766 connman_element_unregister(&device->element);
1770 * connman_device_get_data:
1771 * @device: device structure
1773 * Get private device data pointer
1775 void *connman_device_get_data(struct connman_device *device)
1777 return device->driver_data;
1781 * connman_device_set_data:
1782 * @device: device structure
1783 * @data: data pointer
1785 * Set private device data pointer
1787 void connman_device_set_data(struct connman_device *device, void *data)
1789 device->driver_data = data;
1792 static gboolean match_driver(struct connman_device *device,
1793 struct connman_device_driver *driver)
1795 if (device->type == driver->type ||
1796 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1802 static int device_probe(struct connman_element *element)
1804 struct connman_device *device = element->device;
1807 DBG("element %p name %s", element, element->name);
1812 if (device->driver != NULL)
1815 for (list = driver_list; list; list = list->next) {
1816 struct connman_device_driver *driver = list->data;
1818 if (match_driver(device, driver) == FALSE)
1821 DBG("driver %p name %s", driver, driver->name);
1823 if (driver->probe(device) == 0) {
1824 device->driver = driver;
1829 if (device->driver == NULL)
1832 return setup_device(device);
1835 static void device_remove(struct connman_element *element)
1837 struct connman_device *device = element->device;
1839 DBG("element %p name %s", element, element->name);
1844 if (device->driver == NULL)
1847 remove_device(device);
1850 static struct connman_driver device_driver = {
1852 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1853 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1854 .probe = device_probe,
1855 .remove = device_remove,
1858 static int device_load(struct connman_device *device)
1861 GError *error = NULL;
1863 connman_bool_t powered;
1866 DBG("device %p", device);
1868 keyfile = __connman_storage_open();
1869 if (keyfile == NULL)
1872 identifier = g_strdup_printf("device_%s", device->element.name);
1873 if (identifier == NULL)
1876 powered = g_key_file_get_boolean(keyfile, identifier,
1879 device->powered_persistent = powered;
1880 g_clear_error(&error);
1882 switch (device->mode) {
1883 case CONNMAN_DEVICE_MODE_UNKNOWN:
1884 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1886 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1887 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1888 val = g_key_file_get_integer(keyfile, identifier,
1889 "ScanInterval", &error);
1890 if (error == NULL && val > 0)
1891 device->scan_interval = val;
1892 g_clear_error(&error);
1899 __connman_storage_close(keyfile, FALSE);
1904 static int device_save(struct connman_device *device)
1909 DBG("device %p", device);
1911 keyfile = __connman_storage_open();
1912 if (keyfile == NULL)
1915 identifier = g_strdup_printf("device_%s", device->element.name);
1916 if (identifier == NULL)
1919 g_key_file_set_boolean(keyfile, identifier,
1920 "Powered", device->powered_persistent);
1922 switch (device->mode) {
1923 case CONNMAN_DEVICE_MODE_UNKNOWN:
1924 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1926 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1927 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1928 if (device->scan_interval > 0)
1929 g_key_file_set_integer(keyfile, identifier,
1930 "ScanInterval", device->scan_interval);
1937 __connman_storage_close(keyfile, TRUE);
1942 static struct connman_storage device_storage = {
1944 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1945 .device_load = device_load,
1946 .device_save = device_save,
1949 int __connman_device_init(void)
1953 connection = connman_dbus_get_connection();
1955 if (connman_storage_register(&device_storage) < 0)
1956 connman_error("Failed to register device storage");
1958 return connman_driver_register(&device_driver);
1961 void __connman_device_cleanup(void)
1965 connman_driver_unregister(&device_driver);
1967 connman_storage_unregister(&device_storage);
1969 dbus_connection_unref(connection);