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;
53 struct connman_device_driver *driver;
56 connman_bool_t registered;
59 struct connman_network *network;
66 static gboolean device_scan_trigger(gpointer user_data)
68 struct connman_device *device = user_data;
70 DBG("device %p", device);
72 if (device->driver == NULL) {
73 device->scan_timeout = 0;
77 if (device->driver->scan)
78 device->driver->scan(device);
83 static const char *type2description(enum connman_device_type type)
86 case CONNMAN_DEVICE_TYPE_UNKNOWN:
87 case CONNMAN_DEVICE_TYPE_VENDOR:
89 case CONNMAN_DEVICE_TYPE_ETHERNET:
91 case CONNMAN_DEVICE_TYPE_WIFI:
93 case CONNMAN_DEVICE_TYPE_WIMAX:
95 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
97 case CONNMAN_DEVICE_TYPE_GPS:
99 case CONNMAN_DEVICE_TYPE_HSO:
100 case CONNMAN_DEVICE_TYPE_NOZOMI:
101 case CONNMAN_DEVICE_TYPE_HUAWEI:
102 case CONNMAN_DEVICE_TYPE_NOVATEL:
109 static const char *type2string(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_HSO:
126 case CONNMAN_DEVICE_TYPE_HUAWEI:
127 case CONNMAN_DEVICE_TYPE_NOZOMI:
128 case CONNMAN_DEVICE_TYPE_NOVATEL:
135 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
137 struct connman_service *service;
139 service = __connman_service_lookup_from_device(device);
140 __connman_service_set_carrier(service, carrier);
142 if (carrier == TRUE) {
143 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
144 struct connman_element *element;
146 device->disconnected = TRUE;
148 switch (device->element.ipv4.method) {
149 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
150 case CONNMAN_IPCONFIG_METHOD_OFF:
152 case CONNMAN_IPCONFIG_METHOD_STATIC:
153 type = CONNMAN_ELEMENT_TYPE_IPV4;
155 case CONNMAN_IPCONFIG_METHOD_DHCP:
156 type = CONNMAN_ELEMENT_TYPE_DHCP;
160 element = connman_element_create(NULL);
161 if (element != NULL) {
162 element->type = type;
163 element->index = device->element.index;
165 if (connman_element_register(element,
166 &device->element) < 0)
167 connman_element_unref(element);
169 device->disconnected = FALSE;
171 __connman_service_indicate_state(service,
172 CONNMAN_SERVICE_STATE_CONFIGURATION);
175 connman_element_unregister_children(&device->element);
180 static int set_powered(struct connman_device *device, connman_bool_t powered)
182 struct connman_device_driver *driver = device->driver;
185 DBG("device %p powered %d", device, powered);
190 if (powered == TRUE) {
191 if (driver->enable) {
192 err = driver->enable(device);
193 __connman_notifier_device_type_increase(device->type);
197 g_hash_table_remove_all(device->networks);
199 set_carrier(device, FALSE);
201 if (driver->disable) {
202 err = driver->disable(device);
203 __connman_notifier_device_type_decrease(device->type);
211 static void append_path(gpointer key, gpointer value, gpointer user_data)
213 struct connman_element *element = value;
214 DBusMessageIter *iter = user_data;
216 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
220 static void append_networks(struct connman_device *device,
221 DBusMessageIter *entry)
223 DBusMessageIter value, iter;
224 const char *key = "Networks";
226 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
228 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
229 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
232 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
233 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
234 g_hash_table_foreach(device->networks, append_path, &iter);
235 dbus_message_iter_close_container(&value, &iter);
237 dbus_message_iter_close_container(entry, &value);
240 static DBusMessage *get_properties(DBusConnection *conn,
241 DBusMessage *msg, void *data)
243 struct connman_device *device = data;
245 DBusMessageIter array, dict, entry;
248 DBG("conn %p", conn);
250 if (__connman_security_check_privilege(msg,
251 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
252 return __connman_error_permission_denied(msg);
254 reply = dbus_message_new_method_return(msg);
258 dbus_message_iter_init_append(reply, &array);
260 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
261 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
262 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
263 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
265 if (device->name != NULL)
266 connman_dbus_dict_append_variant(&dict, "Name",
267 DBUS_TYPE_STRING, &device->name);
269 str = type2string(device->type);
271 connman_dbus_dict_append_variant(&dict, "Type",
272 DBUS_TYPE_STRING, &str);
274 if (device->address != NULL)
275 connman_dbus_dict_append_variant(&dict, "Address",
276 DBUS_TYPE_STRING, &device->address);
278 if (device->interface != NULL)
279 connman_dbus_dict_append_variant(&dict, "Interface",
280 DBUS_TYPE_STRING, &device->interface);
282 connman_dbus_dict_append_variant(&dict, "Powered",
283 DBUS_TYPE_BOOLEAN, &device->powered);
285 if (device->driver && device->driver->scan)
286 connman_dbus_dict_append_variant(&dict, "Scanning",
287 DBUS_TYPE_BOOLEAN, &device->scanning);
289 switch (device->mode) {
290 case CONNMAN_DEVICE_MODE_UNKNOWN:
292 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
293 __connman_element_append_ipv4(&device->element, &dict);
295 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
296 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
297 if (device->scan_interval > 0)
298 connman_dbus_dict_append_variant(&dict, "ScanInterval",
299 DBUS_TYPE_UINT16, &device->scan_interval);
301 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
303 append_networks(device, &entry);
304 dbus_message_iter_close_container(&dict, &entry);
308 dbus_message_iter_close_container(&array, &dict);
313 static gboolean powered_timeout(gpointer user_data)
315 struct connman_device *device = user_data;
317 DBG("device %p", device);
321 if (device->pending != NULL) {
324 reply = __connman_error_operation_timeout(device->pending);
326 g_dbus_send_message(connection, reply);
328 dbus_message_unref(device->pending);
329 device->pending = NULL;
335 static DBusMessage *set_property(DBusConnection *conn,
336 DBusMessage *msg, void *data)
338 struct connman_device *device = data;
339 DBusMessageIter iter, value;
343 DBG("conn %p", conn);
345 if (dbus_message_iter_init(msg, &iter) == FALSE)
346 return __connman_error_invalid_arguments(msg);
348 dbus_message_iter_get_basic(&iter, &name);
349 dbus_message_iter_next(&iter);
350 dbus_message_iter_recurse(&iter, &value);
352 if (__connman_security_check_privilege(msg,
353 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
354 return __connman_error_permission_denied(msg);
356 type = dbus_message_iter_get_arg_type(&value);
358 if (g_str_equal(name, "Powered") == TRUE) {
359 connman_bool_t powered;
362 if (type != DBUS_TYPE_BOOLEAN)
363 return __connman_error_invalid_arguments(msg);
365 dbus_message_iter_get_basic(&value, &powered);
367 if (device->powered == powered)
368 return __connman_error_invalid_arguments(msg);
370 if (device->pending != NULL)
371 return __connman_error_in_progress(msg);
373 err = set_powered(device, powered);
374 if (err < 0 && err != -EINPROGRESS)
375 return __connman_error_failed(msg, -err);
377 device->pending = dbus_message_ref(msg);
379 device->timeout = g_timeout_add_seconds(15,
380 powered_timeout, device);
383 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
384 connman_uint16_t interval;
386 switch (device->mode) {
387 case CONNMAN_DEVICE_MODE_UNKNOWN:
388 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
389 return __connman_error_invalid_arguments(msg);
390 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
391 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
395 if (type != DBUS_TYPE_UINT16)
396 return __connman_error_invalid_arguments(msg);
398 dbus_message_iter_get_basic(&value, &interval);
400 device->scan_interval = interval;
402 if (device->scan_timeout > 0) {
403 g_source_remove(device->scan_timeout);
404 device->scan_timeout = 0;
407 if (device->scan_interval > 0) {
408 guint interval = device->scan_interval;
409 device->scan_timeout = g_timeout_add_seconds(interval,
410 device_scan_trigger, device);
412 } else if (g_str_has_prefix(name, "IPv4") == TRUE) {
413 switch (device->mode) {
414 case CONNMAN_DEVICE_MODE_UNKNOWN:
415 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
416 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
417 return __connman_error_invalid_arguments(msg);
418 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
419 __connman_element_set_ipv4(&device->element,
425 __connman_storage_save_device(device);
427 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
430 static char *build_group(const unsigned char *ssid, unsigned int ssid_len,
431 const char *mode, const char *security)
436 str = g_string_sized_new((ssid_len * 2) + 24);
440 if (ssid_len > 0 && ssid[0] != '\0') {
441 for (i = 0; i < ssid_len; i++)
442 g_string_append_printf(str, "%02x", ssid[i]);
445 g_string_append_printf(str, "_%s_%s", mode, security);
447 return g_string_free(str, FALSE);
450 static DBusMessage *join_network(DBusConnection *conn,
451 DBusMessage *msg, void *data)
453 struct connman_device *device = data;
454 struct connman_network *network;
455 enum connman_network_type type;
456 unsigned int ssid_size;
457 const char *group, *mode, *security;
459 DBusMessageIter iter, array;
462 DBG("conn %p", conn);
464 if (__connman_security_check_privilege(msg,
465 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
466 return __connman_error_permission_denied(msg);
468 if (!device->driver || !device->driver->join)
469 return __connman_error_not_supported(msg);
471 dbus_message_iter_init(msg, &iter);
472 dbus_message_iter_recurse(&iter, &array);
474 switch (device->type) {
475 case CONNMAN_DEVICE_TYPE_WIFI:
476 type = CONNMAN_NETWORK_TYPE_WIFI;
479 return __connman_error_not_supported(msg);
482 network = connman_network_create("00_00_00_00_00_00", type);
484 return __connman_error_failed(msg, ENOMEM);
486 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
487 DBusMessageIter entry, value;
488 const char *key, *str;
490 dbus_message_iter_recurse(&array, &entry);
491 dbus_message_iter_get_basic(&entry, &key);
493 dbus_message_iter_next(&entry);
494 dbus_message_iter_recurse(&entry, &value);
496 switch (dbus_message_iter_get_arg_type(&value)) {
497 case DBUS_TYPE_STRING:
498 dbus_message_iter_get_basic(&value, &str);
499 if (g_str_equal(key, "WiFi.SSID") == TRUE)
500 connman_network_set_blob(network, key,
503 connman_network_set_string(network, key, str);
507 dbus_message_iter_next(&array);
510 ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_size);
511 mode = connman_network_get_string(network, "WiFi.Mode");
512 security = connman_network_get_string(network, "WiFi.Security");
513 group = build_group(ssid, ssid_size, mode, security);
515 connman_network_set_group(network, group);
517 index = connman_device_get_index(device);
518 connman_network_set_index(network, index);
520 connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
522 err = device->driver->join(device, network);
524 connman_network_unref(network);
527 return __connman_error_failed(msg, -err);
529 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
532 static DBusMessage *create_network(DBusConnection *conn,
533 DBusMessage *msg, void *data)
535 DBG("conn %p", conn);
537 if (__connman_security_check_privilege(msg,
538 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
539 return __connman_error_permission_denied(msg);
541 return __connman_error_invalid_arguments(msg);
544 static DBusMessage *remove_network(DBusConnection *conn,
545 DBusMessage *msg, void *data)
547 DBG("conn %p", conn);
549 if (__connman_security_check_privilege(msg,
550 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
551 return __connman_error_permission_denied(msg);
553 return __connman_error_invalid_arguments(msg);
556 static DBusMessage *propose_scan(DBusConnection *conn,
557 DBusMessage *msg, void *data)
559 struct connman_device *device = data;
562 DBG("conn %p", conn);
564 switch (device->mode) {
565 case CONNMAN_DEVICE_MODE_UNKNOWN:
566 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
567 return __connman_error_not_supported(msg);
568 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
569 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
573 if (!device->driver || !device->driver->scan)
574 return __connman_error_not_supported(msg);
576 if (device->powered == FALSE)
577 return __connman_error_failed(msg, EINVAL);
579 err = device->driver->scan(device);
581 return __connman_error_failed(msg, -err);
583 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
586 static GDBusMethodTable device_methods[] = {
587 { "GetProperties", "", "a{sv}", get_properties },
588 { "SetProperty", "sv", "", set_property,
589 G_DBUS_METHOD_FLAG_ASYNC },
590 { "JoinNetwork", "a{sv}", "", join_network },
591 { "CreateNetwork", "a{sv}", "o", create_network },
592 { "RemoveNetwork", "o", "", remove_network },
593 { "ProposeScan", "", "", propose_scan },
597 static GDBusSignalTable device_signals[] = {
598 { "PropertyChanged", "sv" },
602 static void append_devices(DBusMessageIter *entry)
604 DBusMessageIter value, iter;
605 const char *key = "Devices";
607 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
609 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
610 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
613 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
614 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
615 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
616 dbus_message_iter_close_container(&value, &iter);
618 dbus_message_iter_close_container(entry, &value);
621 static void emit_devices_signal(void)
624 DBusMessageIter entry;
626 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
627 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
631 dbus_message_iter_init_append(signal, &entry);
633 append_devices(&entry);
635 g_dbus_send_message(connection, signal);
638 static int register_interface(struct connman_element *element)
640 struct connman_device *device = element->device;
642 DBG("element %p name %s", element, element->name);
644 if (g_dbus_register_interface(connection, element->path,
645 CONNMAN_DEVICE_INTERFACE,
646 device_methods, device_signals,
647 NULL, device, NULL) == FALSE) {
648 connman_error("Failed to register %s device", element->path);
652 device->registered = TRUE;
654 emit_devices_signal();
659 static void unregister_interface(struct connman_element *element)
661 struct connman_device *device = element->device;
663 DBG("element %p name %s", element, element->name);
665 device->registered = FALSE;
667 emit_devices_signal();
669 g_dbus_unregister_interface(connection, element->path,
670 CONNMAN_DEVICE_INTERFACE);
673 static void device_enable(struct connman_device *device)
675 DBG("device %p", device);
677 if (device->powered == TRUE)
680 if (device->driver->enable) {
681 device->driver->enable(device);
682 __connman_notifier_device_type_increase(device->type);
686 static void device_disable(struct connman_device *device)
688 DBG("device %p", device);
690 if (device->powered == FALSE)
693 g_hash_table_remove_all(device->networks);
695 if (device->driver->disable) {
696 device->driver->disable(device);
697 __connman_notifier_device_type_decrease(device->type);
701 static int setup_device(struct connman_device *device)
705 DBG("device %p", device);
707 err = register_interface(&device->element);
709 if (device->driver->remove)
710 device->driver->remove(device);
711 device->driver = NULL;
715 switch (device->mode) {
716 case CONNMAN_DEVICE_MODE_UNKNOWN:
717 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
718 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
720 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
721 if (device->secondary == FALSE)
722 __connman_profile_add_device(device);
726 device_enable(device);
731 static void probe_driver(struct connman_element *element, gpointer user_data)
733 struct connman_device_driver *driver = user_data;
735 DBG("element %p name %s", element, element->name);
737 if (element->device == NULL)
740 if (element->device->driver != NULL)
743 if (driver->type != element->device->type)
746 if (driver->probe(element->device) < 0)
749 element->device->driver = driver;
751 setup_device(element->device);
754 static void remove_device(struct connman_device *device)
756 DBG("device %p", device);
758 device_disable(device);
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_remove_device(device);
771 unregister_interface(&device->element);
773 if (device->driver->remove)
774 device->driver->remove(device);
776 device->driver = NULL;
779 static void remove_driver(struct connman_element *element, gpointer user_data)
781 struct connman_device_driver *driver = user_data;
783 DBG("element %p name %s", element, element->name);
785 if (element->device == NULL)
788 if (element->device->driver == driver)
789 remove_device(element->device);
792 connman_bool_t __connman_device_has_driver(struct connman_device *device)
794 if (device == NULL || device->driver == NULL)
797 return device->registered;
800 static GSList *driver_list = NULL;
802 static gint compare_priority(gconstpointer a, gconstpointer b)
804 const struct connman_device_driver *driver1 = a;
805 const struct connman_device_driver *driver2 = b;
807 return driver2->priority - driver1->priority;
811 * connman_device_driver_register:
812 * @driver: device driver definition
814 * Register a new device driver
816 * Returns: %0 on success
818 int connman_device_driver_register(struct connman_device_driver *driver)
820 DBG("driver %p name %s", driver, driver->name);
822 driver_list = g_slist_insert_sorted(driver_list, driver,
825 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
826 probe_driver, driver);
832 * connman_device_driver_unregister:
833 * @driver: device driver definition
835 * Remove a previously registered device driver
837 void connman_device_driver_unregister(struct connman_device_driver *driver)
839 DBG("driver %p name %s", driver, driver->name);
841 driver_list = g_slist_remove(driver_list, driver);
843 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
844 remove_driver, driver);
847 static void unregister_network(gpointer data)
849 struct connman_network *network = data;
851 DBG("network %p", network);
853 connman_element_unregister((struct connman_element *) network);
855 connman_network_unref(network);
858 static void device_destruct(struct connman_element *element)
860 struct connman_device *device = element->device;
862 DBG("element %p name %s", element, element->name);
864 g_free(device->ident);
865 g_free(device->node);
866 g_free(device->name);
867 g_free(device->address);
868 g_free(device->interface);
870 g_free(device->last_network);
872 g_hash_table_destroy(device->networks);
873 device->networks = NULL;
877 * connman_device_create:
878 * @node: device node name (for example an address)
881 * Allocate a new device of given #type and assign the #node name to it.
883 * Returns: a newly-allocated #connman_device structure
885 struct connman_device *connman_device_create(const char *node,
886 enum connman_device_type type)
888 struct connman_device *device;
891 DBG("node %s type %d", node, type);
893 device = g_try_new0(struct connman_device, 1);
897 DBG("device %p", device);
899 __connman_element_initialize(&device->element);
901 device->element.name = g_strdup(node);
902 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
904 device->element.device = device;
905 device->element.destruct = device_destruct;
907 str = type2string(type);
909 connman_element_set_string(&device->element,
910 CONNMAN_PROPERTY_ID_TYPE, str);
912 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
915 device->name = g_strdup(type2description(device->type));
916 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
917 device->secondary = FALSE;
920 case CONNMAN_DEVICE_TYPE_UNKNOWN:
921 case CONNMAN_DEVICE_TYPE_VENDOR:
922 device->scan_interval = 0;
924 case CONNMAN_DEVICE_TYPE_ETHERNET:
925 case CONNMAN_DEVICE_TYPE_WIFI:
926 device->scan_interval = 300;
928 case CONNMAN_DEVICE_TYPE_WIMAX:
929 device->scan_interval = 0;
931 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
932 device->scan_interval = 0;
934 case CONNMAN_DEVICE_TYPE_GPS:
935 device->scan_interval = 0;
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->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
946 g_free, unregister_network);
952 * connman_device_ref:
953 * @device: device structure
955 * Increase reference counter of device
957 struct connman_device *connman_device_ref(struct connman_device *device)
959 if (connman_element_ref(&device->element) == NULL)
966 * connman_device_unref:
967 * @device: device structure
969 * Decrease reference counter of device
971 void connman_device_unref(struct connman_device *device)
973 if (device->timeout > 0) {
974 g_source_remove(device->timeout);
978 if (device->pending != NULL) {
979 dbus_message_unref(device->pending);
980 device->pending = NULL;
983 connman_element_unref(&device->element);
986 const char *__connman_device_get_type(struct connman_device *device)
988 return type2string(device->type);
992 * connman_device_get_type:
993 * @device: device structure
997 enum connman_device_type connman_device_get_type(struct connman_device *device)
1003 * connman_device_get_name:
1004 * @device: device structure
1006 * Get unique name of device
1008 const char *connman_device_get_name(struct connman_device *device)
1010 return device->element.name;
1014 * connman_device_get_path:
1015 * @device: device structure
1017 * Get path name of device
1019 const char *connman_device_get_path(struct connman_device *device)
1021 return device->element.path;
1025 * connman_device_set_index:
1026 * @device: device structure
1027 * @index: index number
1029 * Set index number of device
1031 void connman_device_set_index(struct connman_device *device, int index)
1033 device->element.index = index;
1037 * connman_device_get_index:
1038 * @device: device structure
1040 * Get index number of device
1042 int connman_device_get_index(struct connman_device *device)
1044 return device->element.index;
1048 * connman_device_set_interface:
1049 * @device: device structure
1050 * @interface: interface name
1052 * Set interface name of device
1054 void connman_device_set_interface(struct connman_device *device,
1055 const char *interface)
1057 g_free(device->element.devname);
1058 device->element.devname = g_strdup(interface);
1060 g_free(device->interface);
1061 device->interface = g_strdup(interface);
1063 if (device->name == NULL) {
1064 const char *str = type2description(device->type);
1065 if (str != NULL && device->interface != NULL)
1066 device->name = g_strdup_printf("%s (%s)", str,
1072 * connman_device_get_interface:
1073 * @device: device structure
1075 * Get interface name of device
1077 const char *connman_device_get_interface(struct connman_device *device)
1079 return device->interface;
1083 * connman_device_set_ident:
1084 * @device: device structure
1085 * @ident: unique identifier
1087 * Set unique identifier of device
1089 void connman_device_set_ident(struct connman_device *device,
1092 g_free(device->ident);
1093 device->ident = g_strdup(ident);
1096 const char *__connman_device_get_ident(struct connman_device *device)
1098 return device->ident;
1102 * connman_device_set_mode:
1103 * @device: device structure
1104 * @mode: network mode
1106 * Change network mode of device
1108 void connman_device_set_mode(struct connman_device *device,
1109 enum connman_device_mode mode)
1111 device->mode = mode;
1115 * connman_device_get_mode:
1116 * @device: device structure
1118 * Get network mode of device
1120 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1122 return device->mode;
1126 * connman_device_set_secondary:
1127 * @device: device structure
1128 * @secondary: secondary value
1130 * Change secondary value of device
1132 void connman_device_set_secondary(struct connman_device *device,
1133 connman_bool_t secondary)
1135 device->secondary = secondary;
1139 * connman_device_get_secondary:
1140 * @device: device structure
1142 * Get secondary value of device
1144 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1146 return device->secondary;
1150 * connman_device_set_powered:
1151 * @device: device structure
1152 * @powered: powered state
1154 * Change power state of device
1156 int connman_device_set_powered(struct connman_device *device,
1157 connman_bool_t powered)
1159 DBusMessage *signal;
1160 DBusMessageIter entry, value;
1161 const char *key = "Powered";
1163 DBG("driver %p powered %d", device, powered);
1165 if (device->timeout > 0) {
1166 g_source_remove(device->timeout);
1167 device->timeout = 0;
1170 if (device->pending != NULL) {
1171 g_dbus_send_reply(connection, device->pending,
1174 dbus_message_unref(device->pending);
1177 if (device->powered == powered)
1180 device->powered = powered;
1182 if (device->registered == FALSE)
1185 signal = dbus_message_new_signal(device->element.path,
1186 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1190 dbus_message_iter_init_append(signal, &entry);
1192 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1194 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1195 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1196 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &powered);
1197 dbus_message_iter_close_container(&entry, &value);
1199 g_dbus_send_message(connection, signal);
1201 if (powered == FALSE)
1204 if (device->scan_timeout > 0) {
1205 g_source_remove(device->scan_timeout);
1206 device->scan_timeout = 0;
1209 if (device->scan_interval > 0) {
1210 guint interval = device->scan_interval;
1211 device->scan_timeout = g_timeout_add_seconds(interval,
1212 device_scan_trigger, device);
1215 if (device->driver->scan)
1216 device->driver->scan(device);
1222 * connman_device_set_carrier:
1223 * @device: device structure
1224 * @carrier: carrier state
1226 * Change carrier state of device (only for device without scanning)
1228 int connman_device_set_carrier(struct connman_device *device,
1229 connman_bool_t carrier)
1231 DBG("driver %p carrier %d", device, carrier);
1233 switch (device->mode) {
1234 case CONNMAN_DEVICE_MODE_UNKNOWN:
1235 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1236 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1238 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1242 if (device->carrier == carrier)
1245 device->carrier = carrier;
1247 return set_carrier(device, device->carrier);
1250 int __connman_device_connect(struct connman_device *device)
1252 DBG("device %p", device);
1254 if (device->disconnected == FALSE)
1257 if (device->driver && device->driver->connect)
1258 device->driver->connect(device);
1263 int __connman_device_disconnect(struct connman_device *device)
1265 GHashTableIter iter;
1266 gpointer key, value;
1268 DBG("device %p", device);
1270 connman_device_set_disconnected(device, TRUE);
1272 g_hash_table_iter_init(&iter, device->networks);
1274 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1275 struct connman_network *network = value;
1277 if (connman_network_get_connected(network) == TRUE)
1278 __connman_network_disconnect(network);
1281 if (device->driver && device->driver->disconnect)
1282 device->driver->disconnect(device);
1287 static void connect_known_network(struct connman_device *device)
1289 struct connman_network *network = NULL;
1290 GHashTableIter iter;
1291 gpointer key, value;
1293 unsigned int count = 0;
1295 DBG("device %p", device);
1297 g_hash_table_iter_init(&iter, device->networks);
1299 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1300 connman_uint8_t old_strength, new_strength;
1304 if (connman_network_get_available(value) == FALSE)
1307 name = connman_network_get_string(value,
1308 CONNMAN_PROPERTY_ID_NAME);
1309 if (name != NULL && device->last_network != NULL) {
1310 if (g_str_equal(name, device->last_network) == TRUE) {
1316 if (network == NULL) {
1321 old_strength = connman_network_get_uint8(network,
1322 CONNMAN_PROPERTY_ID_STRENGTH);
1323 new_strength = connman_network_get_uint8(value,
1324 CONNMAN_PROPERTY_ID_STRENGTH);
1326 if (new_strength > old_strength)
1330 if (network != NULL) {
1333 name = connman_network_get_string(value,
1334 CONNMAN_PROPERTY_ID_NAME);
1336 err = __connman_network_connect(network);
1337 if (err == 0 || err == -EINPROGRESS)
1345 if (device->driver && device->driver->scan)
1346 device->driver->scan(device);
1349 static void mark_network_unavailable(gpointer key, gpointer value,
1352 struct connman_network *network = value;
1354 if (connman_network_get_connected(network) == TRUE)
1357 connman_network_set_available(network, FALSE);
1360 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1363 struct connman_network *network = value;
1365 if (connman_network_get_connected(network) == TRUE)
1368 if (connman_network_get_available(network) == TRUE)
1375 * connman_device_set_scanning:
1376 * @device: device structure
1377 * @scanning: scanning state
1379 * Change scanning state of device
1381 int connman_device_set_scanning(struct connman_device *device,
1382 connman_bool_t scanning)
1384 DBusMessage *signal;
1385 DBusMessageIter entry, value;
1386 const char *key = "Scanning";
1388 DBG("driver %p scanning %d", device, scanning);
1390 if (!device->driver || !device->driver->scan)
1393 if (device->scanning == scanning)
1396 device->scanning = scanning;
1398 signal = dbus_message_new_signal(device->element.path,
1399 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1403 dbus_message_iter_init_append(signal, &entry);
1405 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1407 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1408 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1409 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1410 dbus_message_iter_close_container(&entry, &value);
1412 g_dbus_send_message(connection, signal);
1414 if (scanning == TRUE) {
1415 if (device->scan_timeout > 0) {
1416 g_source_remove(device->scan_timeout);
1417 device->scan_timeout = 0;
1420 if (device->scan_interval > 0) {
1421 guint interval = device->scan_interval;
1422 device->scan_timeout = g_timeout_add_seconds(interval,
1423 device_scan_trigger, device);
1426 g_hash_table_foreach(device->networks,
1427 mark_network_unavailable, NULL);
1431 g_hash_table_foreach_remove(device->networks,
1432 remove_unavailable_network, NULL);
1434 if (device->connections > 0)
1437 if (device->disconnected == TRUE)
1440 connect_known_network(device);
1446 * connman_device_set_disconnected:
1447 * @device: device structure
1448 * @disconnected: disconnected state
1450 * Change disconnected state of device (only for device with networks)
1452 int connman_device_set_disconnected(struct connman_device *device,
1453 connman_bool_t disconnected)
1455 DBG("driver %p disconnected %d", device, disconnected);
1457 switch (device->mode) {
1458 case CONNMAN_DEVICE_MODE_UNKNOWN:
1459 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1461 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1462 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1466 if (device->disconnected == disconnected)
1469 device->disconnected = disconnected;
1475 * connman_device_set_string:
1476 * @device: device structure
1477 * @key: unique identifier
1478 * @value: string value
1480 * Set string value for specific key
1482 int connman_device_set_string(struct connman_device *device,
1483 const char *key, const char *value)
1485 DBG("device %p key %s value %s", device, key, value);
1487 if (g_str_equal(key, "Address") == TRUE) {
1488 g_free(device->address);
1489 device->address = g_strdup(value);
1490 } else if (g_str_equal(key, "Name") == TRUE) {
1491 g_free(device->name);
1492 device->name = g_strdup(value);
1493 } else if (g_str_equal(key, "Node") == TRUE) {
1494 g_free(device->node);
1495 device->node = g_strdup(value);
1498 return connman_element_set_string(&device->element, key, value);
1502 * connman_device_get_string:
1503 * @device: device structure
1504 * @key: unique identifier
1506 * Get string value for specific key
1508 const char *connman_device_get_string(struct connman_device *device,
1511 DBG("device %p key %s", device, key);
1513 if (g_str_equal(key, "Address") == TRUE)
1514 return device->address;
1515 else if (g_str_equal(key, "Name") == TRUE)
1516 return device->name;
1517 else if (g_str_equal(key, "Node") == TRUE)
1518 return device->node;
1520 return connman_element_get_string(&device->element, key);
1523 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1525 struct connman_device *device = element->device;
1526 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1527 connman_bool_t powered;
1529 DBG("element %p name %s", element, element->name);
1534 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1536 if (device->powered == powered)
1539 set_powered(device, powered);
1542 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1544 DBG("offlinmode %d", offlinemode);
1546 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1547 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1549 __connman_notifier_offline_mode(offlinemode);
1554 void __connman_device_increase_connections(struct connman_device *device)
1556 device->connections++;
1559 void __connman_device_decrease_connections(struct connman_device *device)
1561 device->connections--;
1565 * connman_device_add_network:
1566 * @device: device structure
1567 * @network: network structure
1569 * Add new network to the device
1571 int connman_device_add_network(struct connman_device *device,
1572 struct connman_network *network)
1574 const char *identifier = connman_network_get_identifier(network);
1577 DBG("device %p network %p", device, network);
1579 switch (device->mode) {
1580 case CONNMAN_DEVICE_MODE_UNKNOWN:
1581 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1583 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1584 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1588 __connman_network_set_device(network, device);
1590 __connman_storage_load_network(network);
1592 err = connman_element_register((struct connman_element *) network,
1595 __connman_network_set_device(network, NULL);
1599 g_hash_table_insert(device->networks, g_strdup(identifier),
1606 * connman_device_get_network:
1607 * @device: device structure
1608 * @identifier: network identifier
1610 * Get network for given identifier
1612 struct connman_network *connman_device_get_network(struct connman_device *device,
1613 const char *identifier)
1615 DBG("device %p identifier %s", device, identifier);
1617 return g_hash_table_lookup(device->networks, identifier);
1621 * connman_device_remove_network:
1622 * @device: device structure
1623 * @identifier: network identifier
1625 * Remove network for given identifier
1627 int connman_device_remove_network(struct connman_device *device,
1628 const char *identifier)
1630 DBG("device %p identifier %s", device, identifier);
1632 g_hash_table_remove(device->networks, identifier);
1637 void __connman_device_set_network(struct connman_device *device,
1638 struct connman_network *network)
1642 if (device->network == network)
1645 if (device->network != NULL)
1646 connman_network_unref(device->network);
1648 if (network != NULL) {
1649 name = connman_network_get_string(network,
1650 CONNMAN_PROPERTY_ID_NAME);
1651 g_free(device->last_network);
1652 device->last_network = g_strdup(name);
1654 device->network = connman_network_ref(network);
1656 g_free(device->last_network);
1657 device->last_network = NULL;
1659 device->network = NULL;
1664 * connman_device_register:
1665 * @device: device structure
1667 * Register device with the system
1669 int connman_device_register(struct connman_device *device)
1671 __connman_storage_load_device(device);
1673 switch (device->mode) {
1674 case CONNMAN_DEVICE_MODE_UNKNOWN:
1675 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1677 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1678 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1679 __connman_storage_init_network(device);
1683 return connman_element_register(&device->element, NULL);
1687 * connman_device_unregister:
1688 * @device: device structure
1690 * Unregister device with the system
1692 void connman_device_unregister(struct connman_device *device)
1694 __connman_storage_save_device(device);
1696 connman_element_unregister(&device->element);
1700 * connman_device_get_data:
1701 * @device: device structure
1703 * Get private device data pointer
1705 void *connman_device_get_data(struct connman_device *device)
1707 return device->driver_data;
1711 * connman_device_set_data:
1712 * @device: device structure
1713 * @data: data pointer
1715 * Set private device data pointer
1717 void connman_device_set_data(struct connman_device *device, void *data)
1719 device->driver_data = data;
1722 static gboolean match_driver(struct connman_device *device,
1723 struct connman_device_driver *driver)
1725 if (device->type == driver->type ||
1726 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1732 static int device_probe(struct connman_element *element)
1734 struct connman_device *device = element->device;
1737 DBG("element %p name %s", element, element->name);
1742 if (device->driver != NULL)
1745 for (list = driver_list; list; list = list->next) {
1746 struct connman_device_driver *driver = list->data;
1748 if (match_driver(device, driver) == FALSE)
1751 DBG("driver %p name %s", driver, driver->name);
1753 if (driver->probe(device) == 0) {
1754 device->driver = driver;
1759 if (device->driver == NULL)
1762 return setup_device(device);
1765 static void device_remove(struct connman_element *element)
1767 struct connman_device *device = element->device;
1769 DBG("element %p name %s", element, element->name);
1774 if (device->driver == NULL)
1777 remove_device(device);
1780 static struct connman_driver device_driver = {
1782 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1783 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1784 .probe = device_probe,
1785 .remove = device_remove,
1788 static int device_load(struct connman_device *device)
1791 gchar *pathname, *identifier, *data = NULL;
1795 DBG("device %p", device);
1797 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1798 __connman_profile_active_ident());
1799 if (pathname == NULL)
1802 keyfile = g_key_file_new();
1804 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1811 if (g_key_file_load_from_data(keyfile, data, length,
1812 0, NULL) == FALSE) {
1819 identifier = g_strdup_printf("device_%s", device->element.name);
1820 if (identifier == NULL)
1823 switch (device->mode) {
1824 case CONNMAN_DEVICE_MODE_UNKNOWN:
1825 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1827 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1828 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1829 val = g_key_file_get_integer(keyfile, identifier,
1830 "ScanInterval", NULL);
1832 device->scan_interval = val;
1837 g_key_file_free(keyfile);
1844 static int device_save(struct connman_device *device)
1847 gchar *pathname, *identifier = NULL, *data = NULL;
1850 DBG("device %p", device);
1852 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1853 __connman_profile_active_ident());
1854 if (pathname == NULL)
1857 keyfile = g_key_file_new();
1859 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1863 if (g_key_file_load_from_data(keyfile, data, length,
1871 identifier = g_strdup_printf("device_%s", device->element.name);
1872 if (identifier == NULL)
1875 switch (device->mode) {
1876 case CONNMAN_DEVICE_MODE_UNKNOWN:
1877 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1879 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1880 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1881 if (device->scan_interval > 0)
1882 g_key_file_set_integer(keyfile, identifier,
1883 "ScanInterval", device->scan_interval);
1887 data = g_key_file_to_data(keyfile, &length, NULL);
1889 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
1890 connman_error("Failed to store device information");
1895 g_key_file_free(keyfile);
1903 static struct connman_storage device_storage = {
1905 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1906 .device_load = device_load,
1907 .device_save = device_save,
1910 int __connman_device_init(void)
1914 connection = connman_dbus_get_connection();
1916 if (connman_storage_register(&device_storage) < 0)
1917 connman_error("Failed to register device storage");
1919 return connman_driver_register(&device_driver);
1922 void __connman_device_cleanup(void)
1926 connman_driver_unregister(&device_driver);
1928 connman_storage_unregister(&device_storage);
1930 dbus_connection_unref(connection);