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_MBM:
101 case CONNMAN_DEVICE_TYPE_HSO:
102 case CONNMAN_DEVICE_TYPE_NOZOMI:
103 case CONNMAN_DEVICE_TYPE_HUAWEI:
104 case CONNMAN_DEVICE_TYPE_NOVATEL:
111 static const char *type2string(enum connman_device_type type)
114 case CONNMAN_DEVICE_TYPE_UNKNOWN:
115 case CONNMAN_DEVICE_TYPE_VENDOR:
117 case CONNMAN_DEVICE_TYPE_ETHERNET:
119 case CONNMAN_DEVICE_TYPE_WIFI:
121 case CONNMAN_DEVICE_TYPE_WIMAX:
123 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
125 case CONNMAN_DEVICE_TYPE_GPS:
127 case CONNMAN_DEVICE_TYPE_MBM:
128 case CONNMAN_DEVICE_TYPE_HSO:
129 case CONNMAN_DEVICE_TYPE_HUAWEI:
130 case CONNMAN_DEVICE_TYPE_NOZOMI:
131 case CONNMAN_DEVICE_TYPE_NOVATEL:
138 static int set_connected(struct connman_device *device,
139 connman_bool_t connected)
141 if (connected == TRUE) {
142 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
143 struct connman_element *element;
145 device->disconnected = TRUE;
147 switch (device->element.ipv4.method) {
148 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
149 case CONNMAN_IPCONFIG_METHOD_OFF:
151 case CONNMAN_IPCONFIG_METHOD_STATIC:
152 type = CONNMAN_ELEMENT_TYPE_IPV4;
154 case CONNMAN_IPCONFIG_METHOD_DHCP:
155 type = CONNMAN_ELEMENT_TYPE_DHCP;
159 element = connman_element_create(NULL);
160 if (element != NULL) {
161 struct connman_service *service;
163 element->type = type;
164 element->index = device->element.index;
166 if (connman_element_register(element,
167 &device->element) < 0)
168 connman_element_unref(element);
170 device->disconnected = FALSE;
172 service = __connman_service_lookup_from_device(device);
173 __connman_service_indicate_state(service,
174 CONNMAN_SERVICE_STATE_CONFIGURATION);
177 connman_element_unregister_children(&device->element);
179 device->disconnected = TRUE;
185 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
187 struct connman_service *service;
189 service = __connman_service_lookup_from_device(device);
190 __connman_service_set_carrier(service, carrier);
192 return set_connected(device, carrier);
195 static int set_powered(struct connman_device *device, connman_bool_t powered)
197 struct connman_device_driver *driver = device->driver;
200 DBG("device %p powered %d", device, powered);
205 if (powered == TRUE) {
206 if (driver->enable) {
207 err = driver->enable(device);
208 __connman_notifier_device_type_increase(device->type);
212 g_hash_table_remove_all(device->networks);
214 set_carrier(device, FALSE);
216 if (driver->disable) {
217 err = driver->disable(device);
218 __connman_notifier_device_type_decrease(device->type);
226 static void append_path(gpointer key, gpointer value, gpointer user_data)
228 struct connman_element *element = value;
229 DBusMessageIter *iter = user_data;
231 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
235 static void append_networks(struct connman_device *device,
236 DBusMessageIter *entry)
238 DBusMessageIter value, iter;
239 const char *key = "Networks";
241 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
243 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
244 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
247 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
248 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
249 g_hash_table_foreach(device->networks, append_path, &iter);
250 dbus_message_iter_close_container(&value, &iter);
252 dbus_message_iter_close_container(entry, &value);
255 static DBusMessage *get_properties(DBusConnection *conn,
256 DBusMessage *msg, void *data)
258 struct connman_device *device = data;
260 DBusMessageIter array, dict, entry;
263 DBG("conn %p", conn);
265 if (__connman_security_check_privilege(msg,
266 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
267 return __connman_error_permission_denied(msg);
269 reply = dbus_message_new_method_return(msg);
273 dbus_message_iter_init_append(reply, &array);
275 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
276 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
277 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
278 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
280 if (device->name != NULL)
281 connman_dbus_dict_append_variant(&dict, "Name",
282 DBUS_TYPE_STRING, &device->name);
284 str = type2string(device->type);
286 connman_dbus_dict_append_variant(&dict, "Type",
287 DBUS_TYPE_STRING, &str);
289 if (device->address != NULL)
290 connman_dbus_dict_append_variant(&dict, "Address",
291 DBUS_TYPE_STRING, &device->address);
293 if (device->interface != NULL)
294 connman_dbus_dict_append_variant(&dict, "Interface",
295 DBUS_TYPE_STRING, &device->interface);
297 connman_dbus_dict_append_variant(&dict, "Powered",
298 DBUS_TYPE_BOOLEAN, &device->powered);
300 if (device->driver && device->driver->scan)
301 connman_dbus_dict_append_variant(&dict, "Scanning",
302 DBUS_TYPE_BOOLEAN, &device->scanning);
304 switch (device->mode) {
305 case CONNMAN_DEVICE_MODE_UNKNOWN:
307 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
308 __connman_element_append_ipv4(&device->element, &dict);
310 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
311 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
312 if (device->scan_interval > 0)
313 connman_dbus_dict_append_variant(&dict, "ScanInterval",
314 DBUS_TYPE_UINT16, &device->scan_interval);
316 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
318 append_networks(device, &entry);
319 dbus_message_iter_close_container(&dict, &entry);
323 dbus_message_iter_close_container(&array, &dict);
328 static gboolean powered_timeout(gpointer user_data)
330 struct connman_device *device = user_data;
332 DBG("device %p", device);
336 if (device->pending != NULL) {
339 reply = __connman_error_operation_timeout(device->pending);
341 g_dbus_send_message(connection, reply);
343 dbus_message_unref(device->pending);
344 device->pending = NULL;
350 static DBusMessage *set_property(DBusConnection *conn,
351 DBusMessage *msg, void *data)
353 struct connman_device *device = data;
354 DBusMessageIter iter, value;
358 DBG("conn %p", conn);
360 if (dbus_message_iter_init(msg, &iter) == FALSE)
361 return __connman_error_invalid_arguments(msg);
363 dbus_message_iter_get_basic(&iter, &name);
364 dbus_message_iter_next(&iter);
365 dbus_message_iter_recurse(&iter, &value);
367 if (__connman_security_check_privilege(msg,
368 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
369 return __connman_error_permission_denied(msg);
371 type = dbus_message_iter_get_arg_type(&value);
373 if (g_str_equal(name, "Powered") == TRUE) {
374 connman_bool_t powered;
377 if (type != DBUS_TYPE_BOOLEAN)
378 return __connman_error_invalid_arguments(msg);
380 dbus_message_iter_get_basic(&value, &powered);
382 if (device->powered == powered)
383 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
385 if (device->pending != NULL)
386 return __connman_error_in_progress(msg);
388 err = set_powered(device, powered);
390 if (err != -EINPROGRESS)
391 return __connman_error_failed(msg, -err);
393 device->pending = dbus_message_ref(msg);
395 device->timeout = g_timeout_add_seconds(15,
396 powered_timeout, device);
400 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
401 connman_uint16_t interval;
403 switch (device->mode) {
404 case CONNMAN_DEVICE_MODE_UNKNOWN:
405 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
406 return __connman_error_invalid_arguments(msg);
407 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
408 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
412 if (type != DBUS_TYPE_UINT16)
413 return __connman_error_invalid_arguments(msg);
415 dbus_message_iter_get_basic(&value, &interval);
417 device->scan_interval = interval;
419 if (device->scan_timeout > 0) {
420 g_source_remove(device->scan_timeout);
421 device->scan_timeout = 0;
424 if (device->scan_interval > 0) {
425 guint interval = device->scan_interval;
426 device->scan_timeout = g_timeout_add_seconds(interval,
427 device_scan_trigger, device);
429 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
432 switch (device->mode) {
433 case CONNMAN_DEVICE_MODE_UNKNOWN:
434 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
435 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
436 return __connman_error_invalid_arguments(msg);
437 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
441 err = __connman_ipconfig_set_ipv4(device->ipconfig,
444 return __connman_error_failed(msg, -err);
446 return __connman_error_invalid_property(msg);
448 __connman_storage_save_device(device);
450 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
453 static char *build_group(const unsigned char *ssid, unsigned int ssid_len,
454 const char *mode, const char *security)
459 str = g_string_sized_new((ssid_len * 2) + 24);
463 if (ssid_len > 0 && ssid[0] != '\0') {
464 for (i = 0; i < ssid_len; i++)
465 g_string_append_printf(str, "%02x", ssid[i]);
468 g_string_append_printf(str, "_%s_%s", mode, security);
470 return g_string_free(str, FALSE);
473 static void convert_name(const char *ssid, char *name,
474 unsigned int ssid_len)
478 for (i = 0; i < ssid_len; i++) {
479 if (g_ascii_isprint(ssid[i]))
486 static DBusMessage *join_network(DBusConnection *conn,
487 DBusMessage *msg, void *data)
489 struct connman_device *device = data;
490 struct connman_network *network;
491 enum connman_network_type type;
492 unsigned int ssid_size;
493 const char *group, *mode, *security;
495 DBusMessageIter iter, array;
498 DBG("conn %p", conn);
500 if (__connman_security_check_privilege(msg,
501 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
502 return __connman_error_permission_denied(msg);
504 if (!device->driver || !device->driver->join)
505 return __connman_error_not_supported(msg);
507 dbus_message_iter_init(msg, &iter);
508 dbus_message_iter_recurse(&iter, &array);
510 switch (device->type) {
511 case CONNMAN_DEVICE_TYPE_WIFI:
512 type = CONNMAN_NETWORK_TYPE_WIFI;
515 return __connman_error_not_supported(msg);
518 network = connman_network_create("00_00_00_00_00_00", type);
520 return __connman_error_failed(msg, ENOMEM);
522 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
523 DBusMessageIter entry, value;
524 const char *key, *str;
526 dbus_message_iter_recurse(&array, &entry);
527 dbus_message_iter_get_basic(&entry, &key);
529 dbus_message_iter_next(&entry);
530 dbus_message_iter_recurse(&entry, &value);
532 switch (dbus_message_iter_get_arg_type(&value)) {
533 case DBUS_TYPE_STRING:
534 dbus_message_iter_get_basic(&value, &str);
535 if (g_str_equal(key, "WiFi.SSID") == TRUE) {
538 connman_network_set_blob(network, key,
540 name = g_try_malloc0(strlen(str) + 1);
542 return __connman_error_failed(msg,
545 convert_name((char *) str, name, strlen(str));
546 connman_network_set_name(network, name);
550 connman_network_set_string(network, key, str);
554 dbus_message_iter_next(&array);
557 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_size);
558 mode = connman_network_get_string(network, "WiFi.Mode");
559 security = connman_network_get_string(network, "WiFi.Security");
560 group = build_group(ssid, ssid_size, mode, security);
562 connman_network_set_group(network, group);
564 index = connman_device_get_index(device);
565 connman_network_set_index(network, index);
567 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
569 err = device->driver->join(device, network);
571 connman_network_unref(network);
574 return __connman_error_failed(msg, -err);
576 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
579 static DBusMessage *create_network(DBusConnection *conn,
580 DBusMessage *msg, void *data)
582 DBG("conn %p", conn);
584 if (__connman_security_check_privilege(msg,
585 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
586 return __connman_error_permission_denied(msg);
588 return __connman_error_invalid_arguments(msg);
591 static DBusMessage *remove_network(DBusConnection *conn,
592 DBusMessage *msg, void *data)
594 DBG("conn %p", conn);
596 if (__connman_security_check_privilege(msg,
597 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
598 return __connman_error_permission_denied(msg);
600 return __connman_error_invalid_arguments(msg);
603 static DBusMessage *propose_scan(DBusConnection *conn,
604 DBusMessage *msg, void *data)
606 struct connman_device *device = data;
609 DBG("conn %p", conn);
611 switch (device->mode) {
612 case CONNMAN_DEVICE_MODE_UNKNOWN:
613 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
614 return __connman_error_not_supported(msg);
615 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
616 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
620 if (!device->driver || !device->driver->scan)
621 return __connman_error_not_supported(msg);
623 if (device->powered == FALSE)
624 return __connman_error_failed(msg, EINVAL);
626 err = device->driver->scan(device);
628 return __connman_error_failed(msg, -err);
630 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
633 static GDBusMethodTable device_methods[] = {
634 { "GetProperties", "", "a{sv}", get_properties },
635 { "SetProperty", "sv", "", set_property,
636 G_DBUS_METHOD_FLAG_ASYNC },
637 { "JoinNetwork", "a{sv}", "", join_network },
638 { "CreateNetwork", "a{sv}", "o", create_network },
639 { "RemoveNetwork", "o", "", remove_network },
640 { "ProposeScan", "", "", propose_scan },
644 static GDBusSignalTable device_signals[] = {
645 { "PropertyChanged", "sv" },
649 static void append_devices(DBusMessageIter *entry)
651 DBusMessageIter value, iter;
652 const char *key = "Devices";
654 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
656 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
657 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
660 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
661 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
662 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
663 dbus_message_iter_close_container(&value, &iter);
665 dbus_message_iter_close_container(entry, &value);
668 static void emit_devices_signal(void)
671 DBusMessageIter entry;
673 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
674 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
678 dbus_message_iter_init_append(signal, &entry);
680 append_devices(&entry);
682 g_dbus_send_message(connection, signal);
685 static int register_interface(struct connman_element *element)
687 struct connman_device *device = element->device;
689 DBG("element %p name %s", element, element->name);
691 if (g_dbus_register_interface(connection, element->path,
692 CONNMAN_DEVICE_INTERFACE,
693 device_methods, device_signals,
694 NULL, device, NULL) == FALSE) {
695 connman_error("Failed to register %s device", element->path);
699 device->registered = TRUE;
701 emit_devices_signal();
706 static void unregister_interface(struct connman_element *element)
708 struct connman_device *device = element->device;
710 DBG("element %p name %s", element, element->name);
712 device->registered = FALSE;
714 emit_devices_signal();
716 g_dbus_unregister_interface(connection, element->path,
717 CONNMAN_DEVICE_INTERFACE);
720 static void device_enable(struct connman_device *device)
722 DBG("device %p", device);
724 if (device->powered == TRUE)
727 if (device->driver->enable) {
728 device->driver->enable(device);
729 __connman_notifier_device_type_increase(device->type);
733 static void device_disable(struct connman_device *device)
735 DBG("device %p", device);
737 if (device->powered == FALSE)
740 g_hash_table_remove_all(device->networks);
742 if (device->driver->disable) {
743 device->driver->disable(device);
744 __connman_notifier_device_type_decrease(device->type);
748 static int setup_device(struct connman_device *device)
752 DBG("device %p", device);
754 err = register_interface(&device->element);
756 if (device->driver->remove)
757 device->driver->remove(device);
758 device->driver = NULL;
762 switch (device->mode) {
763 case CONNMAN_DEVICE_MODE_UNKNOWN:
764 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
765 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
767 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
768 if (device->secondary == FALSE)
769 __connman_profile_add_device(device);
773 device_enable(device);
778 static void probe_driver(struct connman_element *element, gpointer user_data)
780 struct connman_device_driver *driver = user_data;
782 DBG("element %p name %s", element, element->name);
784 if (element->device == NULL)
787 if (element->device->driver != NULL)
790 if (driver->type != element->device->type)
793 if (driver->probe(element->device) < 0)
796 element->device->driver = driver;
798 setup_device(element->device);
801 static void remove_device(struct connman_device *device)
803 DBG("device %p", device);
805 device_disable(device);
807 switch (device->mode) {
808 case CONNMAN_DEVICE_MODE_UNKNOWN:
809 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
810 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
812 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
813 if (device->secondary == FALSE)
814 __connman_profile_remove_device(device);
818 unregister_interface(&device->element);
820 if (device->driver->remove)
821 device->driver->remove(device);
823 device->driver = NULL;
826 static void remove_driver(struct connman_element *element, gpointer user_data)
828 struct connman_device_driver *driver = user_data;
830 DBG("element %p name %s", element, element->name);
832 if (element->device == NULL)
835 if (element->device->driver == driver)
836 remove_device(element->device);
839 connman_bool_t __connman_device_has_driver(struct connman_device *device)
841 if (device == NULL || device->driver == NULL)
844 return device->registered;
847 static GSList *driver_list = NULL;
849 static gint compare_priority(gconstpointer a, gconstpointer b)
851 const struct connman_device_driver *driver1 = a;
852 const struct connman_device_driver *driver2 = b;
854 return driver2->priority - driver1->priority;
858 * connman_device_driver_register:
859 * @driver: device driver definition
861 * Register a new device driver
863 * Returns: %0 on success
865 int connman_device_driver_register(struct connman_device_driver *driver)
867 DBG("driver %p name %s", driver, driver->name);
869 driver_list = g_slist_insert_sorted(driver_list, driver,
872 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
873 probe_driver, driver);
879 * connman_device_driver_unregister:
880 * @driver: device driver definition
882 * Remove a previously registered device driver
884 void connman_device_driver_unregister(struct connman_device_driver *driver)
886 DBG("driver %p name %s", driver, driver->name);
888 driver_list = g_slist_remove(driver_list, driver);
890 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
891 remove_driver, driver);
894 static void unregister_network(gpointer data)
896 struct connman_network *network = data;
898 DBG("network %p", network);
900 connman_element_unregister((struct connman_element *) network);
902 connman_network_unref(network);
905 static void device_destruct(struct connman_element *element)
907 struct connman_device *device = element->device;
909 DBG("element %p name %s", element, element->name);
911 if (device->timeout > 0) {
912 g_source_remove(device->timeout);
916 if (device->pending != NULL) {
917 dbus_message_unref(device->pending);
918 device->pending = NULL;
921 g_free(device->ident);
922 g_free(device->node);
923 g_free(device->name);
924 g_free(device->address);
925 g_free(device->interface);
927 connman_ipconfig_unref(device->ipconfig);
929 g_free(device->last_network);
931 g_hash_table_destroy(device->networks);
932 device->networks = NULL;
936 * connman_device_create:
937 * @node: device node name (for example an address)
940 * Allocate a new device of given #type and assign the #node name to it.
942 * Returns: a newly-allocated #connman_device structure
944 struct connman_device *connman_device_create(const char *node,
945 enum connman_device_type type)
947 struct connman_device *device;
950 DBG("node %s type %d", node, type);
952 device = g_try_new0(struct connman_device, 1);
956 DBG("device %p", device);
958 __connman_element_initialize(&device->element);
960 device->element.name = g_strdup(node);
961 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
963 device->element.device = device;
964 device->element.destruct = device_destruct;
966 str = type2string(type);
968 connman_element_set_string(&device->element,
969 CONNMAN_PROPERTY_ID_TYPE, str);
971 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
974 device->name = g_strdup(type2description(device->type));
975 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
976 device->secondary = FALSE;
979 case CONNMAN_DEVICE_TYPE_UNKNOWN:
980 case CONNMAN_DEVICE_TYPE_VENDOR:
981 device->scan_interval = 0;
983 case CONNMAN_DEVICE_TYPE_ETHERNET:
984 case CONNMAN_DEVICE_TYPE_WIFI:
985 device->scan_interval = 300;
987 case CONNMAN_DEVICE_TYPE_WIMAX:
988 device->scan_interval = 0;
990 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
991 device->scan_interval = 0;
993 case CONNMAN_DEVICE_TYPE_GPS:
994 device->scan_interval = 0;
996 case CONNMAN_DEVICE_TYPE_MBM:
997 case CONNMAN_DEVICE_TYPE_HSO:
998 case CONNMAN_DEVICE_TYPE_NOZOMI:
999 case CONNMAN_DEVICE_TYPE_HUAWEI:
1000 case CONNMAN_DEVICE_TYPE_NOVATEL:
1001 device->scan_interval = 0;
1005 device->ipconfig = connman_ipconfig_create();
1006 if (device->ipconfig == NULL) {
1007 connman_device_unref(device);
1011 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
1012 g_free, unregister_network);
1018 * connman_device_ref:
1019 * @device: device structure
1021 * Increase reference counter of device
1023 struct connman_device *connman_device_ref(struct connman_device *device)
1025 if (connman_element_ref(&device->element) == NULL)
1032 * connman_device_unref:
1033 * @device: device structure
1035 * Decrease reference counter of device
1037 void connman_device_unref(struct connman_device *device)
1039 connman_element_unref(&device->element);
1042 const char *__connman_device_get_type(struct connman_device *device)
1044 return type2string(device->type);
1048 * connman_device_get_type:
1049 * @device: device structure
1051 * Get type of device
1053 enum connman_device_type connman_device_get_type(struct connman_device *device)
1055 return device->type;
1059 * connman_device_get_name:
1060 * @device: device structure
1062 * Get unique name of device
1064 const char *connman_device_get_name(struct connman_device *device)
1066 return device->element.name;
1070 * connman_device_get_path:
1071 * @device: device structure
1073 * Get path name of device
1075 const char *connman_device_get_path(struct connman_device *device)
1077 return device->element.path;
1081 * connman_device_set_index:
1082 * @device: device structure
1083 * @index: index number
1085 * Set index number of device
1087 void connman_device_set_index(struct connman_device *device, int index)
1089 device->element.index = index;
1093 * connman_device_get_index:
1094 * @device: device structure
1096 * Get index number of device
1098 int connman_device_get_index(struct connman_device *device)
1100 return device->element.index;
1104 * connman_device_set_interface:
1105 * @device: device structure
1106 * @interface: interface name
1108 * Set interface name of device
1110 void connman_device_set_interface(struct connman_device *device,
1111 const char *interface)
1113 g_free(device->element.devname);
1114 device->element.devname = g_strdup(interface);
1116 g_free(device->interface);
1117 device->interface = g_strdup(interface);
1119 if (device->name == NULL) {
1120 const char *str = type2description(device->type);
1121 if (str != NULL && device->interface != NULL)
1122 device->name = g_strdup_printf("%s (%s)", str,
1128 * connman_device_get_interface:
1129 * @device: device structure
1131 * Get interface name of device
1133 const char *connman_device_get_interface(struct connman_device *device)
1135 return device->interface;
1139 * connman_device_set_ident:
1140 * @device: device structure
1141 * @ident: unique identifier
1143 * Set unique identifier of device
1145 void connman_device_set_ident(struct connman_device *device,
1148 g_free(device->ident);
1149 device->ident = g_strdup(ident);
1152 const char *__connman_device_get_ident(struct connman_device *device)
1154 return device->ident;
1158 * connman_device_set_mode:
1159 * @device: device structure
1160 * @mode: network mode
1162 * Change network mode of device
1164 void connman_device_set_mode(struct connman_device *device,
1165 enum connman_device_mode mode)
1167 device->mode = mode;
1171 * connman_device_get_mode:
1172 * @device: device structure
1174 * Get network mode of device
1176 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1178 return device->mode;
1182 * connman_device_set_secondary:
1183 * @device: device structure
1184 * @secondary: secondary value
1186 * Change secondary value of device
1188 void connman_device_set_secondary(struct connman_device *device,
1189 connman_bool_t secondary)
1191 device->secondary = secondary;
1195 * connman_device_get_secondary:
1196 * @device: device structure
1198 * Get secondary value of device
1200 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1202 return device->secondary;
1206 * connman_device_set_powered:
1207 * @device: device structure
1208 * @powered: powered state
1210 * Change power state of device
1212 int connman_device_set_powered(struct connman_device *device,
1213 connman_bool_t powered)
1215 DBusMessage *signal;
1216 DBusMessageIter entry, value;
1217 const char *key = "Powered";
1219 DBG("driver %p powered %d", device, powered);
1221 if (device->timeout > 0) {
1222 g_source_remove(device->timeout);
1223 device->timeout = 0;
1226 if (device->pending != NULL) {
1227 g_dbus_send_reply(connection, device->pending,
1230 dbus_message_unref(device->pending);
1231 device->pending = NULL;
1234 if (device->powered == powered)
1237 device->powered = powered;
1239 if (device->registered == FALSE)
1242 signal = dbus_message_new_signal(device->element.path,
1243 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1247 dbus_message_iter_init_append(signal, &entry);
1249 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1251 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1252 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1253 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &powered);
1254 dbus_message_iter_close_container(&entry, &value);
1256 g_dbus_send_message(connection, signal);
1258 if (powered == FALSE)
1261 if (device->scan_timeout > 0) {
1262 g_source_remove(device->scan_timeout);
1263 device->scan_timeout = 0;
1266 if (device->scan_interval > 0) {
1267 guint interval = device->scan_interval;
1268 device->scan_timeout = g_timeout_add_seconds(interval,
1269 device_scan_trigger, device);
1272 if (device->driver->scan)
1273 device->driver->scan(device);
1279 * connman_device_set_carrier:
1280 * @device: device structure
1281 * @carrier: carrier state
1283 * Change carrier state of device (only for device without scanning)
1285 int connman_device_set_carrier(struct connman_device *device,
1286 connman_bool_t carrier)
1288 DBG("device %p carrier %d", device, carrier);
1290 switch (device->mode) {
1291 case CONNMAN_DEVICE_MODE_UNKNOWN:
1292 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1293 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1295 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1299 if (device->carrier == carrier)
1302 device->carrier = carrier;
1304 return set_carrier(device, device->carrier);
1307 int __connman_device_connect(struct connman_device *device)
1309 DBG("device %p", device);
1311 if (device->disconnected == FALSE)
1314 if (device->driver && device->driver->connect)
1315 device->driver->connect(device);
1320 int __connman_device_disconnect(struct connman_device *device)
1322 GHashTableIter iter;
1323 gpointer key, value;
1325 DBG("device %p", device);
1327 connman_device_set_disconnected(device, TRUE);
1329 g_hash_table_iter_init(&iter, device->networks);
1331 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1332 struct connman_network *network = value;
1334 if (connman_network_get_connected(network) == TRUE)
1335 __connman_network_disconnect(network);
1338 if (device->driver && device->driver->disconnect)
1339 device->driver->disconnect(device);
1344 static void connect_known_network(struct connman_device *device)
1346 struct connman_network *network = NULL;
1347 GHashTableIter iter;
1348 gpointer key, value;
1350 unsigned int count = 0;
1352 DBG("device %p", device);
1354 g_hash_table_iter_init(&iter, device->networks);
1356 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1357 connman_uint8_t old_strength, new_strength;
1361 if (connman_network_get_available(value) == FALSE)
1364 name = connman_network_get_string(value,
1365 CONNMAN_PROPERTY_ID_NAME);
1366 if (name != NULL && device->last_network != NULL) {
1367 if (g_str_equal(name, device->last_network) == TRUE) {
1373 if (network == NULL) {
1378 old_strength = connman_network_get_uint8(network,
1379 CONNMAN_PROPERTY_ID_STRENGTH);
1380 new_strength = connman_network_get_uint8(value,
1381 CONNMAN_PROPERTY_ID_STRENGTH);
1383 if (new_strength > old_strength)
1387 if (network != NULL) {
1390 name = connman_network_get_string(value,
1391 CONNMAN_PROPERTY_ID_NAME);
1393 err = __connman_network_connect(network);
1394 if (err == 0 || err == -EINPROGRESS)
1402 if (device->driver && device->driver->scan)
1403 device->driver->scan(device);
1406 static void mark_network_unavailable(gpointer key, gpointer value,
1409 struct connman_network *network = value;
1411 if (connman_network_get_connected(network) == TRUE)
1414 connman_network_set_available(network, FALSE);
1417 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1420 struct connman_network *network = value;
1422 if (connman_network_get_connected(network) == TRUE)
1425 if (connman_network_get_available(network) == TRUE)
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 if (device->scan_timeout > 0) {
1473 g_source_remove(device->scan_timeout);
1474 device->scan_timeout = 0;
1477 if (device->scan_interval > 0) {
1478 guint interval = device->scan_interval;
1479 device->scan_timeout = g_timeout_add_seconds(interval,
1480 device_scan_trigger, device);
1483 g_hash_table_foreach(device->networks,
1484 mark_network_unavailable, NULL);
1488 g_hash_table_foreach_remove(device->networks,
1489 remove_unavailable_network, NULL);
1491 if (device->connections > 0)
1494 if (device->disconnected == TRUE)
1497 connect_known_network(device);
1503 * connman_device_set_disconnected:
1504 * @device: device structure
1505 * @disconnected: disconnected state
1507 * Change disconnected state of device (only for device with networks)
1509 int connman_device_set_disconnected(struct connman_device *device,
1510 connman_bool_t disconnected)
1512 DBG("device %p disconnected %d", device, disconnected);
1514 switch (device->mode) {
1515 case CONNMAN_DEVICE_MODE_UNKNOWN:
1516 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1518 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1519 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1523 if (device->disconnected == disconnected)
1526 device->disconnected = disconnected;
1532 * connman_device_set_connected:
1533 * @device: device structure
1534 * @connected: connected state
1536 * Change connected state of device (for Ethernet like devices)
1538 int connman_device_set_connected(struct connman_device *device,
1539 connman_bool_t connected)
1541 DBG("device %p connected %d", device, connected);
1543 switch (device->mode) {
1544 case CONNMAN_DEVICE_MODE_UNKNOWN:
1545 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1546 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1548 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1552 if (device->carrier == FALSE)
1555 return set_connected(device, connected);
1559 * connman_device_set_string:
1560 * @device: device structure
1561 * @key: unique identifier
1562 * @value: string value
1564 * Set string value for specific key
1566 int connman_device_set_string(struct connman_device *device,
1567 const char *key, const char *value)
1569 DBG("device %p key %s value %s", device, key, value);
1571 if (g_str_equal(key, "Address") == TRUE) {
1572 g_free(device->address);
1573 device->address = g_strdup(value);
1574 } else if (g_str_equal(key, "Name") == TRUE) {
1575 g_free(device->name);
1576 device->name = g_strdup(value);
1577 } else if (g_str_equal(key, "Node") == TRUE) {
1578 g_free(device->node);
1579 device->node = g_strdup(value);
1582 return connman_element_set_string(&device->element, key, value);
1586 * connman_device_get_string:
1587 * @device: device structure
1588 * @key: unique identifier
1590 * Get string value for specific key
1592 const char *connman_device_get_string(struct connman_device *device,
1595 DBG("device %p key %s", device, key);
1597 if (g_str_equal(key, "Address") == TRUE)
1598 return device->address;
1599 else if (g_str_equal(key, "Name") == TRUE)
1600 return device->name;
1601 else if (g_str_equal(key, "Node") == TRUE)
1602 return device->node;
1604 return connman_element_get_string(&device->element, key);
1607 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1609 struct connman_device *device = element->device;
1610 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1611 connman_bool_t powered;
1613 DBG("element %p name %s", element, element->name);
1618 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1620 if (device->powered == powered)
1623 set_powered(device, powered);
1626 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1628 DBG("offlinmode %d", offlinemode);
1630 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1631 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1633 __connman_notifier_offline_mode(offlinemode);
1638 void __connman_device_increase_connections(struct connman_device *device)
1640 device->connections++;
1643 void __connman_device_decrease_connections(struct connman_device *device)
1645 device->connections--;
1649 * connman_device_add_network:
1650 * @device: device structure
1651 * @network: network structure
1653 * Add new network to the device
1655 int connman_device_add_network(struct connman_device *device,
1656 struct connman_network *network)
1658 const char *identifier = connman_network_get_identifier(network);
1661 DBG("device %p network %p", device, network);
1663 switch (device->mode) {
1664 case CONNMAN_DEVICE_MODE_UNKNOWN:
1665 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1667 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1668 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1672 __connman_network_set_device(network, device);
1674 __connman_storage_load_network(network);
1676 err = connman_element_register((struct connman_element *) network,
1679 __connman_network_set_device(network, NULL);
1683 g_hash_table_insert(device->networks, g_strdup(identifier),
1690 * connman_device_get_network:
1691 * @device: device structure
1692 * @identifier: network identifier
1694 * Get network for given identifier
1696 struct connman_network *connman_device_get_network(struct connman_device *device,
1697 const char *identifier)
1699 DBG("device %p identifier %s", device, identifier);
1701 return g_hash_table_lookup(device->networks, identifier);
1705 * connman_device_remove_network:
1706 * @device: device structure
1707 * @identifier: network identifier
1709 * Remove network for given identifier
1711 int connman_device_remove_network(struct connman_device *device,
1712 const char *identifier)
1714 DBG("device %p identifier %s", device, identifier);
1716 g_hash_table_remove(device->networks, identifier);
1721 void __connman_device_set_network(struct connman_device *device,
1722 struct connman_network *network)
1726 if (device->network == network)
1729 if (device->network != NULL)
1730 connman_network_unref(device->network);
1732 if (network != NULL) {
1733 name = connman_network_get_string(network,
1734 CONNMAN_PROPERTY_ID_NAME);
1735 g_free(device->last_network);
1736 device->last_network = g_strdup(name);
1738 device->network = connman_network_ref(network);
1740 g_free(device->last_network);
1741 device->last_network = NULL;
1743 device->network = NULL;
1748 * connman_device_register:
1749 * @device: device structure
1751 * Register device with the system
1753 int connman_device_register(struct connman_device *device)
1755 __connman_storage_load_device(device);
1757 switch (device->mode) {
1758 case CONNMAN_DEVICE_MODE_UNKNOWN:
1759 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1761 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1762 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1763 __connman_storage_init_network(device);
1767 return connman_element_register(&device->element, NULL);
1771 * connman_device_unregister:
1772 * @device: device structure
1774 * Unregister device with the system
1776 void connman_device_unregister(struct connman_device *device)
1778 __connman_storage_save_device(device);
1780 connman_element_unregister(&device->element);
1784 * connman_device_get_data:
1785 * @device: device structure
1787 * Get private device data pointer
1789 void *connman_device_get_data(struct connman_device *device)
1791 return device->driver_data;
1795 * connman_device_set_data:
1796 * @device: device structure
1797 * @data: data pointer
1799 * Set private device data pointer
1801 void connman_device_set_data(struct connman_device *device, void *data)
1803 device->driver_data = data;
1806 static gboolean match_driver(struct connman_device *device,
1807 struct connman_device_driver *driver)
1809 if (device->type == driver->type ||
1810 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1816 static int device_probe(struct connman_element *element)
1818 struct connman_device *device = element->device;
1821 DBG("element %p name %s", element, element->name);
1826 if (device->driver != NULL)
1829 for (list = driver_list; list; list = list->next) {
1830 struct connman_device_driver *driver = list->data;
1832 if (match_driver(device, driver) == FALSE)
1835 DBG("driver %p name %s", driver, driver->name);
1837 if (driver->probe(device) == 0) {
1838 device->driver = driver;
1843 if (device->driver == NULL)
1846 return setup_device(device);
1849 static void device_remove(struct connman_element *element)
1851 struct connman_device *device = element->device;
1853 DBG("element %p name %s", element, element->name);
1858 if (device->driver == NULL)
1861 remove_device(device);
1864 static struct connman_driver device_driver = {
1866 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1867 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1868 .probe = device_probe,
1869 .remove = device_remove,
1872 static int device_load(struct connman_device *device)
1875 gchar *pathname, *identifier, *data = NULL;
1879 DBG("device %p", device);
1881 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1882 __connman_profile_active_ident());
1883 if (pathname == NULL)
1886 keyfile = g_key_file_new();
1888 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1895 if (g_key_file_load_from_data(keyfile, data, length,
1896 0, NULL) == FALSE) {
1903 identifier = g_strdup_printf("device_%s", device->element.name);
1904 if (identifier == NULL)
1907 switch (device->mode) {
1908 case CONNMAN_DEVICE_MODE_UNKNOWN:
1909 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1911 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1912 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1913 val = g_key_file_get_integer(keyfile, identifier,
1914 "ScanInterval", NULL);
1916 device->scan_interval = val;
1921 g_key_file_free(keyfile);
1928 static int device_save(struct connman_device *device)
1931 gchar *pathname, *identifier = NULL, *data = NULL;
1934 DBG("device %p", device);
1936 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1937 __connman_profile_active_ident());
1938 if (pathname == NULL)
1941 keyfile = g_key_file_new();
1943 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1947 if (g_key_file_load_from_data(keyfile, data, length,
1955 identifier = g_strdup_printf("device_%s", device->element.name);
1956 if (identifier == NULL)
1959 switch (device->mode) {
1960 case CONNMAN_DEVICE_MODE_UNKNOWN:
1961 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1963 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1964 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1965 if (device->scan_interval > 0)
1966 g_key_file_set_integer(keyfile, identifier,
1967 "ScanInterval", device->scan_interval);
1971 data = g_key_file_to_data(keyfile, &length, NULL);
1973 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1974 connman_error("Failed to store device information");
1979 g_key_file_free(keyfile);
1987 static struct connman_storage device_storage = {
1989 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1990 .device_load = device_load,
1991 .device_save = device_save,
1994 int __connman_device_init(void)
1998 connection = connman_dbus_get_connection();
2000 if (connman_storage_register(&device_storage) < 0)
2001 connman_error("Failed to register device storage");
2003 return connman_driver_register(&device_driver);
2006 void __connman_device_cleanup(void)
2010 connman_driver_unregister(&device_driver);
2012 connman_storage_unregister(&device_storage);
2014 dbus_connection_unref(connection);