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 powered;
41 connman_bool_t carrier;
42 connman_bool_t scanning;
43 connman_bool_t disconnected;
44 connman_uint16_t scan_interval;
50 unsigned int connections;
52 struct connman_ipconfig *ipconfig;
54 struct connman_device_driver *driver;
57 connman_bool_t registered;
60 struct connman_network *network;
67 static gboolean device_scan_trigger(gpointer user_data)
69 struct connman_device *device = user_data;
71 DBG("device %p", device);
73 if (device->driver == NULL) {
74 device->scan_timeout = 0;
78 if (device->driver->scan)
79 device->driver->scan(device);
84 static const char *type2description(enum connman_device_type type)
87 case CONNMAN_DEVICE_TYPE_UNKNOWN:
88 case CONNMAN_DEVICE_TYPE_VENDOR:
90 case CONNMAN_DEVICE_TYPE_ETHERNET:
92 case CONNMAN_DEVICE_TYPE_WIFI:
94 case CONNMAN_DEVICE_TYPE_WIMAX:
96 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
98 case CONNMAN_DEVICE_TYPE_GPS:
100 case CONNMAN_DEVICE_TYPE_HSO:
101 case CONNMAN_DEVICE_TYPE_NOZOMI:
102 case CONNMAN_DEVICE_TYPE_HUAWEI:
103 case CONNMAN_DEVICE_TYPE_NOVATEL:
110 static const char *type2string(enum connman_device_type type)
113 case CONNMAN_DEVICE_TYPE_UNKNOWN:
114 case CONNMAN_DEVICE_TYPE_VENDOR:
116 case CONNMAN_DEVICE_TYPE_ETHERNET:
118 case CONNMAN_DEVICE_TYPE_WIFI:
120 case CONNMAN_DEVICE_TYPE_WIMAX:
122 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
124 case CONNMAN_DEVICE_TYPE_GPS:
126 case CONNMAN_DEVICE_TYPE_HSO:
127 case CONNMAN_DEVICE_TYPE_HUAWEI:
128 case CONNMAN_DEVICE_TYPE_NOZOMI:
129 case CONNMAN_DEVICE_TYPE_NOVATEL:
136 static int set_connected(struct connman_device *device,
137 connman_bool_t connected)
139 if (connected == TRUE) {
140 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
141 struct connman_element *element;
143 device->disconnected = TRUE;
145 switch (device->element.ipv4.method) {
146 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
147 case CONNMAN_IPCONFIG_METHOD_OFF:
149 case CONNMAN_IPCONFIG_METHOD_STATIC:
150 type = CONNMAN_ELEMENT_TYPE_IPV4;
152 case CONNMAN_IPCONFIG_METHOD_DHCP:
153 type = CONNMAN_ELEMENT_TYPE_DHCP;
157 element = connman_element_create(NULL);
158 if (element != NULL) {
159 struct connman_service *service;
161 element->type = type;
162 element->index = device->element.index;
164 if (connman_element_register(element,
165 &device->element) < 0)
166 connman_element_unref(element);
168 device->disconnected = FALSE;
170 service = __connman_service_lookup_from_device(device);
171 __connman_service_indicate_state(service,
172 CONNMAN_SERVICE_STATE_CONFIGURATION);
175 connman_element_unregister_children(&device->element);
177 device->disconnected = TRUE;
183 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
185 struct connman_service *service;
187 service = __connman_service_lookup_from_device(device);
188 __connman_service_set_carrier(service, carrier);
190 return set_connected(device, carrier);
193 static int set_powered(struct connman_device *device, connman_bool_t powered)
195 struct connman_device_driver *driver = device->driver;
198 DBG("device %p powered %d", device, powered);
203 if (powered == TRUE) {
204 if (driver->enable) {
205 err = driver->enable(device);
206 __connman_notifier_device_type_increase(device->type);
210 g_hash_table_remove_all(device->networks);
212 set_carrier(device, FALSE);
214 if (driver->disable) {
215 err = driver->disable(device);
216 __connman_notifier_device_type_decrease(device->type);
224 static void append_path(gpointer key, gpointer value, gpointer user_data)
226 struct connman_element *element = value;
227 DBusMessageIter *iter = user_data;
229 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
233 static void append_networks(struct connman_device *device,
234 DBusMessageIter *entry)
236 DBusMessageIter value, iter;
237 const char *key = "Networks";
239 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
241 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
242 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
245 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
246 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
247 g_hash_table_foreach(device->networks, append_path, &iter);
248 dbus_message_iter_close_container(&value, &iter);
250 dbus_message_iter_close_container(entry, &value);
253 static DBusMessage *get_properties(DBusConnection *conn,
254 DBusMessage *msg, void *data)
256 struct connman_device *device = data;
258 DBusMessageIter array, dict, entry;
261 DBG("conn %p", conn);
263 if (__connman_security_check_privilege(msg,
264 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
265 return __connman_error_permission_denied(msg);
267 reply = dbus_message_new_method_return(msg);
271 dbus_message_iter_init_append(reply, &array);
273 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
274 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
275 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
276 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
278 if (device->name != NULL)
279 connman_dbus_dict_append_variant(&dict, "Name",
280 DBUS_TYPE_STRING, &device->name);
282 str = type2string(device->type);
284 connman_dbus_dict_append_variant(&dict, "Type",
285 DBUS_TYPE_STRING, &str);
287 if (device->address != NULL)
288 connman_dbus_dict_append_variant(&dict, "Address",
289 DBUS_TYPE_STRING, &device->address);
291 if (device->interface != NULL)
292 connman_dbus_dict_append_variant(&dict, "Interface",
293 DBUS_TYPE_STRING, &device->interface);
295 connman_dbus_dict_append_variant(&dict, "Powered",
296 DBUS_TYPE_BOOLEAN, &device->powered);
298 if (device->driver && device->driver->scan)
299 connman_dbus_dict_append_variant(&dict, "Scanning",
300 DBUS_TYPE_BOOLEAN, &device->scanning);
302 switch (device->mode) {
303 case CONNMAN_DEVICE_MODE_UNKNOWN:
305 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
306 __connman_element_append_ipv4(&device->element, &dict);
308 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
309 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
310 if (device->scan_interval > 0)
311 connman_dbus_dict_append_variant(&dict, "ScanInterval",
312 DBUS_TYPE_UINT16, &device->scan_interval);
314 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
316 append_networks(device, &entry);
317 dbus_message_iter_close_container(&dict, &entry);
321 dbus_message_iter_close_container(&array, &dict);
326 static gboolean powered_timeout(gpointer user_data)
328 struct connman_device *device = user_data;
330 DBG("device %p", device);
334 if (device->pending != NULL) {
337 reply = __connman_error_operation_timeout(device->pending);
339 g_dbus_send_message(connection, reply);
341 dbus_message_unref(device->pending);
342 device->pending = NULL;
348 static DBusMessage *set_property(DBusConnection *conn,
349 DBusMessage *msg, void *data)
351 struct connman_device *device = data;
352 DBusMessageIter iter, value;
356 DBG("conn %p", conn);
358 if (dbus_message_iter_init(msg, &iter) == FALSE)
359 return __connman_error_invalid_arguments(msg);
361 dbus_message_iter_get_basic(&iter, &name);
362 dbus_message_iter_next(&iter);
363 dbus_message_iter_recurse(&iter, &value);
365 if (__connman_security_check_privilege(msg,
366 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
367 return __connman_error_permission_denied(msg);
369 type = dbus_message_iter_get_arg_type(&value);
371 if (g_str_equal(name, "Powered") == TRUE) {
372 connman_bool_t powered;
375 if (type != DBUS_TYPE_BOOLEAN)
376 return __connman_error_invalid_arguments(msg);
378 dbus_message_iter_get_basic(&value, &powered);
380 if (device->powered == powered)
381 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
383 if (device->pending != NULL)
384 return __connman_error_in_progress(msg);
386 err = set_powered(device, powered);
388 if (err != -EINPROGRESS)
389 return __connman_error_failed(msg, -err);
391 device->pending = dbus_message_ref(msg);
393 device->timeout = g_timeout_add_seconds(15,
394 powered_timeout, device);
398 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
399 connman_uint16_t interval;
401 switch (device->mode) {
402 case CONNMAN_DEVICE_MODE_UNKNOWN:
403 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
404 return __connman_error_invalid_arguments(msg);
405 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
406 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
410 if (type != DBUS_TYPE_UINT16)
411 return __connman_error_invalid_arguments(msg);
413 dbus_message_iter_get_basic(&value, &interval);
415 device->scan_interval = interval;
417 if (device->scan_timeout > 0) {
418 g_source_remove(device->scan_timeout);
419 device->scan_timeout = 0;
422 if (device->scan_interval > 0) {
423 guint interval = device->scan_interval;
424 device->scan_timeout = g_timeout_add_seconds(interval,
425 device_scan_trigger, device);
427 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
430 switch (device->mode) {
431 case CONNMAN_DEVICE_MODE_UNKNOWN:
432 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
433 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
434 return __connman_error_invalid_arguments(msg);
435 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
439 err = __connman_ipconfig_set_ipv4(device->ipconfig,
442 return __connman_error_failed(msg, -err);
444 return __connman_error_invalid_property(msg);
446 __connman_storage_save_device(device);
448 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
451 static char *build_group(const unsigned char *ssid, unsigned int ssid_len,
452 const char *mode, const char *security)
457 str = g_string_sized_new((ssid_len * 2) + 24);
461 if (ssid_len > 0 && ssid[0] != '\0') {
462 for (i = 0; i < ssid_len; i++)
463 g_string_append_printf(str, "%02x", ssid[i]);
466 g_string_append_printf(str, "_%s_%s", mode, security);
468 return g_string_free(str, FALSE);
471 static void convert_name(const char *ssid, char *name,
472 unsigned int ssid_len)
476 for (i = 0; i < ssid_len; i++) {
477 if (g_ascii_isprint(ssid[i]))
484 static DBusMessage *join_network(DBusConnection *conn,
485 DBusMessage *msg, void *data)
487 struct connman_device *device = data;
488 struct connman_network *network;
489 enum connman_network_type type;
490 unsigned int ssid_size;
491 const char *group, *mode, *security;
493 DBusMessageIter iter, array;
496 DBG("conn %p", conn);
498 if (__connman_security_check_privilege(msg,
499 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
500 return __connman_error_permission_denied(msg);
502 if (!device->driver || !device->driver->join)
503 return __connman_error_not_supported(msg);
505 dbus_message_iter_init(msg, &iter);
506 dbus_message_iter_recurse(&iter, &array);
508 switch (device->type) {
509 case CONNMAN_DEVICE_TYPE_WIFI:
510 type = CONNMAN_NETWORK_TYPE_WIFI;
513 return __connman_error_not_supported(msg);
516 network = connman_network_create("00_00_00_00_00_00", type);
518 return __connman_error_failed(msg, ENOMEM);
520 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
521 DBusMessageIter entry, value;
522 const char *key, *str;
524 dbus_message_iter_recurse(&array, &entry);
525 dbus_message_iter_get_basic(&entry, &key);
527 dbus_message_iter_next(&entry);
528 dbus_message_iter_recurse(&entry, &value);
530 switch (dbus_message_iter_get_arg_type(&value)) {
531 case DBUS_TYPE_STRING:
532 dbus_message_iter_get_basic(&value, &str);
533 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
536 connman_network_set_blob(network, key,
538 name = g_try_malloc0(strlen(str) + 1);
540 return __connman_error_failed(msg,
543 convert_name((char *) str, name, strlen(str));
544 connman_network_set_name(network, name);
548 connman_network_set_string(network, key, str);
552 dbus_message_iter_next(&array);
555 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_size);
556 mode = connman_network_get_string(network, "WiFi.Mode");
557 security = connman_network_get_string(network, "WiFi.Security");
558 group = build_group(ssid, ssid_size, mode, security);
560 connman_network_set_group(network, group);
562 index = connman_device_get_index(device);
563 connman_network_set_index(network, index);
565 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
567 err = device->driver->join(device, network);
569 connman_network_unref(network);
572 return __connman_error_failed(msg, -err);
574 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
577 static DBusMessage *create_network(DBusConnection *conn,
578 DBusMessage *msg, void *data)
580 DBG("conn %p", conn);
582 if (__connman_security_check_privilege(msg,
583 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
584 return __connman_error_permission_denied(msg);
586 return __connman_error_invalid_arguments(msg);
589 static DBusMessage *remove_network(DBusConnection *conn,
590 DBusMessage *msg, void *data)
592 DBG("conn %p", conn);
594 if (__connman_security_check_privilege(msg,
595 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
596 return __connman_error_permission_denied(msg);
598 return __connman_error_invalid_arguments(msg);
601 static DBusMessage *propose_scan(DBusConnection *conn,
602 DBusMessage *msg, void *data)
604 struct connman_device *device = data;
607 DBG("conn %p", conn);
609 switch (device->mode) {
610 case CONNMAN_DEVICE_MODE_UNKNOWN:
611 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
612 return __connman_error_not_supported(msg);
613 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
614 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
618 if (!device->driver || !device->driver->scan)
619 return __connman_error_not_supported(msg);
621 if (device->powered == FALSE)
622 return __connman_error_failed(msg, EINVAL);
624 err = device->driver->scan(device);
626 return __connman_error_failed(msg, -err);
628 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
631 static GDBusMethodTable device_methods[] = {
632 { "GetProperties", "", "a{sv}", get_properties },
633 { "SetProperty", "sv", "", set_property,
634 G_DBUS_METHOD_FLAG_ASYNC },
635 { "JoinNetwork", "a{sv}", "", join_network },
636 { "CreateNetwork", "a{sv}", "o", create_network },
637 { "RemoveNetwork", "o", "", remove_network },
638 { "ProposeScan", "", "", propose_scan },
642 static GDBusSignalTable device_signals[] = {
643 { "PropertyChanged", "sv" },
647 static void append_devices(DBusMessageIter *entry)
649 DBusMessageIter value, iter;
650 const char *key = "Devices";
652 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
654 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
655 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
658 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
659 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
660 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
661 dbus_message_iter_close_container(&value, &iter);
663 dbus_message_iter_close_container(entry, &value);
666 static void emit_devices_signal(void)
669 DBusMessageIter entry;
671 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
672 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
676 dbus_message_iter_init_append(signal, &entry);
678 append_devices(&entry);
680 g_dbus_send_message(connection, signal);
683 static int register_interface(struct connman_element *element)
685 struct connman_device *device = element->device;
687 DBG("element %p name %s", element, element->name);
689 if (g_dbus_register_interface(connection, element->path,
690 CONNMAN_DEVICE_INTERFACE,
691 device_methods, device_signals,
692 NULL, device, NULL) == FALSE) {
693 connman_error("Failed to register %s device", element->path);
697 device->registered = TRUE;
699 emit_devices_signal();
704 static void unregister_interface(struct connman_element *element)
706 struct connman_device *device = element->device;
708 DBG("element %p name %s", element, element->name);
710 device->registered = FALSE;
712 emit_devices_signal();
714 g_dbus_unregister_interface(connection, element->path,
715 CONNMAN_DEVICE_INTERFACE);
718 static void device_enable(struct connman_device *device)
720 DBG("device %p", device);
722 if (device->powered == TRUE)
725 if (device->driver->enable) {
726 device->driver->enable(device);
727 __connman_notifier_device_type_increase(device->type);
731 static void device_disable(struct connman_device *device)
733 DBG("device %p", device);
735 if (device->powered == FALSE)
738 g_hash_table_remove_all(device->networks);
740 if (device->driver->disable) {
741 device->driver->disable(device);
742 __connman_notifier_device_type_decrease(device->type);
746 static int setup_device(struct connman_device *device)
750 DBG("device %p", device);
752 err = register_interface(&device->element);
754 if (device->driver->remove)
755 device->driver->remove(device);
756 device->driver = NULL;
760 switch (device->mode) {
761 case CONNMAN_DEVICE_MODE_UNKNOWN:
762 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
763 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
765 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
766 if (device->secondary == FALSE)
767 __connman_profile_add_device(device);
771 device_enable(device);
776 static void probe_driver(struct connman_element *element, gpointer user_data)
778 struct connman_device_driver *driver = user_data;
780 DBG("element %p name %s", element, element->name);
782 if (element->device == NULL)
785 if (element->device->driver != NULL)
788 if (driver->type != element->device->type)
791 if (driver->probe(element->device) < 0)
794 element->device->driver = driver;
796 setup_device(element->device);
799 static void remove_device(struct connman_device *device)
801 DBG("device %p", device);
803 device_disable(device);
805 switch (device->mode) {
806 case CONNMAN_DEVICE_MODE_UNKNOWN:
807 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
808 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
810 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
811 if (device->secondary == FALSE)
812 __connman_profile_remove_device(device);
816 unregister_interface(&device->element);
818 if (device->driver->remove)
819 device->driver->remove(device);
821 device->driver = NULL;
824 static void remove_driver(struct connman_element *element, gpointer user_data)
826 struct connman_device_driver *driver = user_data;
828 DBG("element %p name %s", element, element->name);
830 if (element->device == NULL)
833 if (element->device->driver == driver)
834 remove_device(element->device);
837 connman_bool_t __connman_device_has_driver(struct connman_device *device)
839 if (device == NULL || device->driver == NULL)
842 return device->registered;
845 static GSList *driver_list = NULL;
847 static gint compare_priority(gconstpointer a, gconstpointer b)
849 const struct connman_device_driver *driver1 = a;
850 const struct connman_device_driver *driver2 = b;
852 return driver2->priority - driver1->priority;
856 * connman_device_driver_register:
857 * @driver: device driver definition
859 * Register a new device driver
861 * Returns: %0 on success
863 int connman_device_driver_register(struct connman_device_driver *driver)
865 DBG("driver %p name %s", driver, driver->name);
867 driver_list = g_slist_insert_sorted(driver_list, driver,
870 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
871 probe_driver, driver);
877 * connman_device_driver_unregister:
878 * @driver: device driver definition
880 * Remove a previously registered device driver
882 void connman_device_driver_unregister(struct connman_device_driver *driver)
884 DBG("driver %p name %s", driver, driver->name);
886 driver_list = g_slist_remove(driver_list, driver);
888 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
889 remove_driver, driver);
892 static void unregister_network(gpointer data)
894 struct connman_network *network = data;
896 DBG("network %p", network);
898 connman_element_unregister((struct connman_element *) network);
900 connman_network_unref(network);
903 static void device_destruct(struct connman_element *element)
905 struct connman_device *device = element->device;
907 DBG("element %p name %s", element, element->name);
909 if (device->timeout > 0) {
910 g_source_remove(device->timeout);
914 if (device->pending != NULL) {
915 dbus_message_unref(device->pending);
916 device->pending = NULL;
919 g_free(device->ident);
920 g_free(device->node);
921 g_free(device->name);
922 g_free(device->address);
923 g_free(device->interface);
925 connman_ipconfig_unref(device->ipconfig);
927 g_free(device->last_network);
929 g_hash_table_destroy(device->networks);
930 device->networks = NULL;
934 * connman_device_create:
935 * @node: device node name (for example an address)
938 * Allocate a new device of given #type and assign the #node name to it.
940 * Returns: a newly-allocated #connman_device structure
942 struct connman_device *connman_device_create(const char *node,
943 enum connman_device_type type)
945 struct connman_device *device;
948 DBG("node %s type %d", node, type);
950 device = g_try_new0(struct connman_device, 1);
954 DBG("device %p", device);
956 __connman_element_initialize(&device->element);
958 device->element.name = g_strdup(node);
959 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
961 device->element.device = device;
962 device->element.destruct = device_destruct;
964 str = type2string(type);
966 connman_element_set_string(&device->element,
967 CONNMAN_PROPERTY_ID_TYPE, str);
969 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
972 device->name = g_strdup(type2description(device->type));
973 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
974 device->secondary = FALSE;
977 case CONNMAN_DEVICE_TYPE_UNKNOWN:
978 case CONNMAN_DEVICE_TYPE_VENDOR:
979 device->scan_interval = 0;
981 case CONNMAN_DEVICE_TYPE_ETHERNET:
982 case CONNMAN_DEVICE_TYPE_WIFI:
983 device->scan_interval = 300;
985 case CONNMAN_DEVICE_TYPE_WIMAX:
986 device->scan_interval = 0;
988 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
989 device->scan_interval = 0;
991 case CONNMAN_DEVICE_TYPE_GPS:
992 device->scan_interval = 0;
994 case CONNMAN_DEVICE_TYPE_HSO:
995 case CONNMAN_DEVICE_TYPE_NOZOMI:
996 case CONNMAN_DEVICE_TYPE_HUAWEI:
997 case CONNMAN_DEVICE_TYPE_NOVATEL:
998 device->scan_interval = 0;
1002 device->ipconfig = connman_ipconfig_create();
1003 if (device->ipconfig == NULL) {
1004 connman_device_unref(device);
1008 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
1009 g_free, unregister_network);
1015 * connman_device_ref:
1016 * @device: device structure
1018 * Increase reference counter of device
1020 struct connman_device *connman_device_ref(struct connman_device *device)
1022 if (connman_element_ref(&device->element) == NULL)
1029 * connman_device_unref:
1030 * @device: device structure
1032 * Decrease reference counter of device
1034 void connman_device_unref(struct connman_device *device)
1036 connman_element_unref(&device->element);
1039 const char *__connman_device_get_type(struct connman_device *device)
1041 return type2string(device->type);
1045 * connman_device_get_type:
1046 * @device: device structure
1048 * Get type of device
1050 enum connman_device_type connman_device_get_type(struct connman_device *device)
1052 return device->type;
1056 * connman_device_get_name:
1057 * @device: device structure
1059 * Get unique name of device
1061 const char *connman_device_get_name(struct connman_device *device)
1063 return device->element.name;
1067 * connman_device_get_path:
1068 * @device: device structure
1070 * Get path name of device
1072 const char *connman_device_get_path(struct connman_device *device)
1074 return device->element.path;
1078 * connman_device_set_index:
1079 * @device: device structure
1080 * @index: index number
1082 * Set index number of device
1084 void connman_device_set_index(struct connman_device *device, int index)
1086 device->element.index = index;
1090 * connman_device_get_index:
1091 * @device: device structure
1093 * Get index number of device
1095 int connman_device_get_index(struct connman_device *device)
1097 return device->element.index;
1101 * connman_device_set_interface:
1102 * @device: device structure
1103 * @interface: interface name
1105 * Set interface name of device
1107 void connman_device_set_interface(struct connman_device *device,
1108 const char *interface)
1110 g_free(device->element.devname);
1111 device->element.devname = g_strdup(interface);
1113 g_free(device->interface);
1114 device->interface = g_strdup(interface);
1116 if (device->name == NULL) {
1117 const char *str = type2description(device->type);
1118 if (str != NULL && device->interface != NULL)
1119 device->name = g_strdup_printf("%s (%s)", str,
1125 * connman_device_get_interface:
1126 * @device: device structure
1128 * Get interface name of device
1130 const char *connman_device_get_interface(struct connman_device *device)
1132 return device->interface;
1136 * connman_device_set_ident:
1137 * @device: device structure
1138 * @ident: unique identifier
1140 * Set unique identifier of device
1142 void connman_device_set_ident(struct connman_device *device,
1145 g_free(device->ident);
1146 device->ident = g_strdup(ident);
1149 const char *__connman_device_get_ident(struct connman_device *device)
1151 return device->ident;
1155 * connman_device_set_mode:
1156 * @device: device structure
1157 * @mode: network mode
1159 * Change network mode of device
1161 void connman_device_set_mode(struct connman_device *device,
1162 enum connman_device_mode mode)
1164 device->mode = mode;
1168 * connman_device_get_mode:
1169 * @device: device structure
1171 * Get network mode of device
1173 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1175 return device->mode;
1179 * connman_device_set_secondary:
1180 * @device: device structure
1181 * @secondary: secondary value
1183 * Change secondary value of device
1185 void connman_device_set_secondary(struct connman_device *device,
1186 connman_bool_t secondary)
1188 device->secondary = secondary;
1192 * connman_device_get_secondary:
1193 * @device: device structure
1195 * Get secondary value of device
1197 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1199 return device->secondary;
1203 * connman_device_set_powered:
1204 * @device: device structure
1205 * @powered: powered state
1207 * Change power state of device
1209 int connman_device_set_powered(struct connman_device *device,
1210 connman_bool_t powered)
1212 DBusMessage *signal;
1213 DBusMessageIter entry, value;
1214 const char *key = "Powered";
1216 DBG("driver %p powered %d", device, powered);
1218 if (device->timeout > 0) {
1219 g_source_remove(device->timeout);
1220 device->timeout = 0;
1223 if (device->pending != NULL) {
1224 g_dbus_send_reply(connection, device->pending,
1227 dbus_message_unref(device->pending);
1228 device->pending = NULL;
1231 if (device->powered == powered)
1234 device->powered = powered;
1236 if (device->registered == FALSE)
1239 signal = dbus_message_new_signal(device->element.path,
1240 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1244 dbus_message_iter_init_append(signal, &entry);
1246 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1248 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1249 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1250 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &powered);
1251 dbus_message_iter_close_container(&entry, &value);
1253 g_dbus_send_message(connection, signal);
1255 if (powered == FALSE)
1258 if (device->scan_timeout > 0) {
1259 g_source_remove(device->scan_timeout);
1260 device->scan_timeout = 0;
1263 if (device->scan_interval > 0) {
1264 guint interval = device->scan_interval;
1265 device->scan_timeout = g_timeout_add_seconds(interval,
1266 device_scan_trigger, device);
1269 if (device->driver->scan)
1270 device->driver->scan(device);
1276 * connman_device_set_carrier:
1277 * @device: device structure
1278 * @carrier: carrier state
1280 * Change carrier state of device (only for device without scanning)
1282 int connman_device_set_carrier(struct connman_device *device,
1283 connman_bool_t carrier)
1285 DBG("device %p carrier %d", device, carrier);
1287 switch (device->mode) {
1288 case CONNMAN_DEVICE_MODE_UNKNOWN:
1289 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1290 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1292 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1296 if (device->carrier == carrier)
1299 device->carrier = carrier;
1301 return set_carrier(device, device->carrier);
1304 int __connman_device_connect(struct connman_device *device)
1306 DBG("device %p", device);
1308 if (device->disconnected == FALSE)
1311 if (device->driver && device->driver->connect)
1312 device->driver->connect(device);
1317 int __connman_device_disconnect(struct connman_device *device)
1319 GHashTableIter iter;
1320 gpointer key, value;
1322 DBG("device %p", device);
1324 connman_device_set_disconnected(device, TRUE);
1326 g_hash_table_iter_init(&iter, device->networks);
1328 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1329 struct connman_network *network = value;
1331 if (connman_network_get_connected(network) == TRUE)
1332 __connman_network_disconnect(network);
1335 if (device->driver && device->driver->disconnect)
1336 device->driver->disconnect(device);
1341 static void connect_known_network(struct connman_device *device)
1343 struct connman_network *network = NULL;
1344 GHashTableIter iter;
1345 gpointer key, value;
1347 unsigned int count = 0;
1349 DBG("device %p", device);
1351 g_hash_table_iter_init(&iter, device->networks);
1353 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1354 connman_uint8_t old_strength, new_strength;
1358 if (connman_network_get_available(value) == FALSE)
1361 name = connman_network_get_string(value,
1362 CONNMAN_PROPERTY_ID_NAME);
1363 if (name != NULL && device->last_network != NULL) {
1364 if (g_str_equal(name, device->last_network) == TRUE) {
1370 if (network == NULL) {
1375 old_strength = connman_network_get_uint8(network,
1376 CONNMAN_PROPERTY_ID_STRENGTH);
1377 new_strength = connman_network_get_uint8(value,
1378 CONNMAN_PROPERTY_ID_STRENGTH);
1380 if (new_strength > old_strength)
1384 if (network != NULL) {
1387 name = connman_network_get_string(value,
1388 CONNMAN_PROPERTY_ID_NAME);
1390 err = __connman_network_connect(network);
1391 if (err == 0 || err == -EINPROGRESS)
1399 if (device->driver && device->driver->scan)
1400 device->driver->scan(device);
1403 static void mark_network_unavailable(gpointer key, gpointer value,
1406 struct connman_network *network = value;
1408 if (connman_network_get_connected(network) == TRUE)
1411 connman_network_set_available(network, FALSE);
1414 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1417 struct connman_network *network = value;
1419 if (connman_network_get_connected(network) == TRUE)
1422 if (connman_network_get_available(network) == TRUE)
1429 * connman_device_set_scanning:
1430 * @device: device structure
1431 * @scanning: scanning state
1433 * Change scanning state of device
1435 int connman_device_set_scanning(struct connman_device *device,
1436 connman_bool_t scanning)
1438 DBusMessage *signal;
1439 DBusMessageIter entry, value;
1440 const char *key = "Scanning";
1442 DBG("device %p scanning %d", device, scanning);
1444 if (!device->driver || !device->driver->scan)
1447 if (device->scanning == scanning)
1450 device->scanning = scanning;
1452 signal = dbus_message_new_signal(device->element.path,
1453 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1457 dbus_message_iter_init_append(signal, &entry);
1459 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1461 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1462 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1463 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1464 dbus_message_iter_close_container(&entry, &value);
1466 g_dbus_send_message(connection, signal);
1468 if (scanning == TRUE) {
1469 if (device->scan_timeout > 0) {
1470 g_source_remove(device->scan_timeout);
1471 device->scan_timeout = 0;
1474 if (device->scan_interval > 0) {
1475 guint interval = device->scan_interval;
1476 device->scan_timeout = g_timeout_add_seconds(interval,
1477 device_scan_trigger, device);
1480 g_hash_table_foreach(device->networks,
1481 mark_network_unavailable, NULL);
1485 g_hash_table_foreach_remove(device->networks,
1486 remove_unavailable_network, NULL);
1488 if (device->connections > 0)
1491 if (device->disconnected == TRUE)
1494 connect_known_network(device);
1500 * connman_device_set_disconnected:
1501 * @device: device structure
1502 * @disconnected: disconnected state
1504 * Change disconnected state of device (only for device with networks)
1506 int connman_device_set_disconnected(struct connman_device *device,
1507 connman_bool_t disconnected)
1509 DBG("device %p disconnected %d", device, disconnected);
1511 switch (device->mode) {
1512 case CONNMAN_DEVICE_MODE_UNKNOWN:
1513 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1515 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1516 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1520 if (device->disconnected == disconnected)
1523 device->disconnected = disconnected;
1529 * connman_device_set_connected:
1530 * @device: device structure
1531 * @connected: connected state
1533 * Change connected state of device (for Ethernet like devices)
1535 int connman_device_set_connected(struct connman_device *device,
1536 connman_bool_t connected)
1538 DBG("device %p connected %d", device, connected);
1540 switch (device->mode) {
1541 case CONNMAN_DEVICE_MODE_UNKNOWN:
1542 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1543 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1545 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1549 if (device->carrier == FALSE)
1552 return set_connected(device, connected);
1556 * connman_device_set_string:
1557 * @device: device structure
1558 * @key: unique identifier
1559 * @value: string value
1561 * Set string value for specific key
1563 int connman_device_set_string(struct connman_device *device,
1564 const char *key, const char *value)
1566 DBG("device %p key %s value %s", device, key, value);
1568 if (g_str_equal(key, "Address") == TRUE) {
1569 g_free(device->address);
1570 device->address = g_strdup(value);
1571 } else if (g_str_equal(key, "Name") == TRUE) {
1572 g_free(device->name);
1573 device->name = g_strdup(value);
1574 } else if (g_str_equal(key, "Node") == TRUE) {
1575 g_free(device->node);
1576 device->node = g_strdup(value);
1579 return connman_element_set_string(&device->element, key, value);
1583 * connman_device_get_string:
1584 * @device: device structure
1585 * @key: unique identifier
1587 * Get string value for specific key
1589 const char *connman_device_get_string(struct connman_device *device,
1592 DBG("device %p key %s", device, key);
1594 if (g_str_equal(key, "Address") == TRUE)
1595 return device->address;
1596 else if (g_str_equal(key, "Name") == TRUE)
1597 return device->name;
1598 else if (g_str_equal(key, "Node") == TRUE)
1599 return device->node;
1601 return connman_element_get_string(&device->element, key);
1604 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1606 struct connman_device *device = element->device;
1607 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1608 connman_bool_t powered;
1610 DBG("element %p name %s", element, element->name);
1615 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1617 if (device->powered == powered)
1620 set_powered(device, powered);
1623 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1625 DBG("offlinmode %d", offlinemode);
1627 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1628 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1630 __connman_notifier_offline_mode(offlinemode);
1635 void __connman_device_increase_connections(struct connman_device *device)
1637 device->connections++;
1640 void __connman_device_decrease_connections(struct connman_device *device)
1642 device->connections--;
1646 * connman_device_add_network:
1647 * @device: device structure
1648 * @network: network structure
1650 * Add new network to the device
1652 int connman_device_add_network(struct connman_device *device,
1653 struct connman_network *network)
1655 const char *identifier = connman_network_get_identifier(network);
1658 DBG("device %p network %p", device, network);
1660 switch (device->mode) {
1661 case CONNMAN_DEVICE_MODE_UNKNOWN:
1662 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1664 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1665 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1669 __connman_network_set_device(network, device);
1671 __connman_storage_load_network(network);
1673 err = connman_element_register((struct connman_element *) network,
1676 __connman_network_set_device(network, NULL);
1680 g_hash_table_insert(device->networks, g_strdup(identifier),
1687 * connman_device_get_network:
1688 * @device: device structure
1689 * @identifier: network identifier
1691 * Get network for given identifier
1693 struct connman_network *connman_device_get_network(struct connman_device *device,
1694 const char *identifier)
1696 DBG("device %p identifier %s", device, identifier);
1698 return g_hash_table_lookup(device->networks, identifier);
1702 * connman_device_remove_network:
1703 * @device: device structure
1704 * @identifier: network identifier
1706 * Remove network for given identifier
1708 int connman_device_remove_network(struct connman_device *device,
1709 const char *identifier)
1711 DBG("device %p identifier %s", device, identifier);
1713 g_hash_table_remove(device->networks, identifier);
1718 void __connman_device_set_network(struct connman_device *device,
1719 struct connman_network *network)
1723 if (device->network == network)
1726 if (device->network != NULL)
1727 connman_network_unref(device->network);
1729 if (network != NULL) {
1730 name = connman_network_get_string(network,
1731 CONNMAN_PROPERTY_ID_NAME);
1732 g_free(device->last_network);
1733 device->last_network = g_strdup(name);
1735 device->network = connman_network_ref(network);
1737 g_free(device->last_network);
1738 device->last_network = NULL;
1740 device->network = NULL;
1745 * connman_device_register:
1746 * @device: device structure
1748 * Register device with the system
1750 int connman_device_register(struct connman_device *device)
1752 __connman_storage_load_device(device);
1754 switch (device->mode) {
1755 case CONNMAN_DEVICE_MODE_UNKNOWN:
1756 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1758 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1759 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1760 __connman_storage_init_network(device);
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)
1872 gchar *pathname, *identifier, *data = NULL;
1876 DBG("device %p", device);
1878 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1879 __connman_profile_active_ident());
1880 if (pathname == NULL)
1883 keyfile = g_key_file_new();
1885 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1892 if (g_key_file_load_from_data(keyfile, data, length,
1893 0, NULL) == FALSE) {
1900 identifier = g_strdup_printf("device_%s", device->element.name);
1901 if (identifier == NULL)
1904 switch (device->mode) {
1905 case CONNMAN_DEVICE_MODE_UNKNOWN:
1906 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1908 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1909 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1910 val = g_key_file_get_integer(keyfile, identifier,
1911 "ScanInterval", NULL);
1913 device->scan_interval = val;
1918 g_key_file_free(keyfile);
1925 static int device_save(struct connman_device *device)
1928 gchar *pathname, *identifier = NULL, *data = NULL;
1931 DBG("device %p", device);
1933 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1934 __connman_profile_active_ident());
1935 if (pathname == NULL)
1938 keyfile = g_key_file_new();
1940 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1944 if (g_key_file_load_from_data(keyfile, data, length,
1952 identifier = g_strdup_printf("device_%s", device->element.name);
1953 if (identifier == NULL)
1956 switch (device->mode) {
1957 case CONNMAN_DEVICE_MODE_UNKNOWN:
1958 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1960 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1961 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1962 if (device->scan_interval > 0)
1963 g_key_file_set_integer(keyfile, identifier,
1964 "ScanInterval", device->scan_interval);
1968 data = g_key_file_to_data(keyfile, &length, NULL);
1970 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1971 connman_error("Failed to store device information");
1976 g_key_file_free(keyfile);
1984 static struct connman_storage device_storage = {
1986 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1987 .device_load = device_load,
1988 .device_save = device_save,
1991 int __connman_device_init(void)
1995 connection = connman_dbus_get_connection();
1997 if (connman_storage_register(&device_storage) < 0)
1998 connman_error("Failed to register device storage");
2000 return connman_driver_register(&device_driver);
2003 void __connman_device_cleanup(void)
2007 connman_driver_unregister(&device_driver);
2009 connman_storage_unregister(&device_storage);
2011 dbus_connection_unref(connection);