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 struct connman_device {
34 struct connman_element element;
35 enum connman_device_type type;
36 enum connman_device_mode mode;
37 enum connman_device_policy policy;
38 connman_bool_t powered;
39 connman_bool_t carrier;
40 connman_bool_t scanning;
41 connman_bool_t disconnected;
42 connman_uint8_t priority;
43 connman_uint16_t scan_interval;
48 unsigned int connections;
51 struct connman_device_driver *driver;
54 connman_bool_t registered;
57 struct connman_network *network;
61 static gboolean device_scan_trigger(gpointer user_data)
63 struct connman_device *device = user_data;
65 DBG("device %p", device);
67 if (device->driver == NULL) {
68 device->scan_timeout = 0;
72 if (device->driver->scan)
73 device->driver->scan(device);
78 static const char *type2description(enum connman_device_type type)
81 case CONNMAN_DEVICE_TYPE_UNKNOWN:
82 case CONNMAN_DEVICE_TYPE_VENDOR:
84 case CONNMAN_DEVICE_TYPE_ETHERNET:
86 case CONNMAN_DEVICE_TYPE_WIFI:
88 case CONNMAN_DEVICE_TYPE_WIMAX:
90 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
92 case CONNMAN_DEVICE_TYPE_GPS:
94 case CONNMAN_DEVICE_TYPE_HSO:
95 case CONNMAN_DEVICE_TYPE_NOZOMI:
96 case CONNMAN_DEVICE_TYPE_HUAWEI:
97 case CONNMAN_DEVICE_TYPE_NOVATEL:
104 static const char *type2string(enum connman_device_type type)
107 case CONNMAN_DEVICE_TYPE_UNKNOWN:
108 case CONNMAN_DEVICE_TYPE_VENDOR:
110 case CONNMAN_DEVICE_TYPE_ETHERNET:
112 case CONNMAN_DEVICE_TYPE_WIFI:
114 case CONNMAN_DEVICE_TYPE_WIMAX:
116 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
118 case CONNMAN_DEVICE_TYPE_GPS:
120 case CONNMAN_DEVICE_TYPE_HSO:
121 case CONNMAN_DEVICE_TYPE_HUAWEI:
122 case CONNMAN_DEVICE_TYPE_NOZOMI:
123 case CONNMAN_DEVICE_TYPE_NOVATEL:
130 static const char *policy2string(enum connman_device_policy policy)
133 case CONNMAN_DEVICE_POLICY_UNKNOWN:
135 case CONNMAN_DEVICE_POLICY_IGNORE:
137 case CONNMAN_DEVICE_POLICY_OFF:
139 case CONNMAN_DEVICE_POLICY_AUTO:
141 case CONNMAN_DEVICE_POLICY_MANUAL:
148 static enum connman_device_policy string2policy(const char *policy)
150 if (g_str_equal(policy, "ignore") == TRUE)
151 return CONNMAN_DEVICE_POLICY_IGNORE;
152 else if (g_str_equal(policy, "off") == TRUE)
153 return CONNMAN_DEVICE_POLICY_OFF;
154 else if (g_str_equal(policy, "auto") == TRUE)
155 return CONNMAN_DEVICE_POLICY_AUTO;
156 else if (g_str_equal(policy, "manual") == TRUE)
157 return CONNMAN_DEVICE_POLICY_MANUAL;
159 return CONNMAN_DEVICE_POLICY_UNKNOWN;
162 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
164 if (carrier == TRUE) {
165 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
166 struct connman_element *element;
168 switch (device->element.ipv4.method) {
169 case CONNMAN_IPV4_METHOD_UNKNOWN:
170 case CONNMAN_IPV4_METHOD_OFF:
172 case CONNMAN_IPV4_METHOD_STATIC:
173 type = CONNMAN_ELEMENT_TYPE_IPV4;
175 case CONNMAN_IPV4_METHOD_DHCP:
176 type = CONNMAN_ELEMENT_TYPE_DHCP;
180 element = connman_element_create(NULL);
181 if (element != NULL) {
182 element->type = type;
183 element->index = device->element.index;
185 if (connman_element_register(element,
186 &device->element) < 0)
187 connman_element_unref(element);
190 connman_element_unregister_children(&device->element);
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 int set_policy(DBusConnection *connection,
227 struct connman_device *device,
228 enum connman_device_policy policy)
231 DBusMessageIter entry, value;
232 const char *str, *key = "Policy";
235 DBG("device %p policy %d", device, policy);
237 if (device->policy == policy)
241 case CONNMAN_DEVICE_POLICY_UNKNOWN:
243 case CONNMAN_DEVICE_POLICY_IGNORE:
245 case CONNMAN_DEVICE_POLICY_OFF:
246 if (device->powered == TRUE)
247 err = set_powered(device, FALSE);
249 case CONNMAN_DEVICE_POLICY_AUTO:
250 case CONNMAN_DEVICE_POLICY_MANUAL:
251 if (device->powered == FALSE)
252 err = set_powered(device, TRUE);
254 err = set_carrier(device, device->carrier);
261 device->policy = policy;
263 signal = dbus_message_new_signal(device->element.path,
264 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
268 dbus_message_iter_init_append(signal, &entry);
270 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
272 str = policy2string(policy);
274 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
275 DBUS_TYPE_STRING_AS_STRING, &value);
276 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &str);
277 dbus_message_iter_close_container(&entry, &value);
279 g_dbus_send_message(connection, signal);
284 static void append_path(gpointer key, gpointer value, gpointer user_data)
286 struct connman_element *element = value;
287 DBusMessageIter *iter = user_data;
289 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
293 static void append_networks(struct connman_device *device,
294 DBusMessageIter *entry)
296 DBusMessageIter value, iter;
297 const char *key = "Networks";
299 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
301 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
302 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
305 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
306 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
307 g_hash_table_foreach(device->networks, append_path, &iter);
308 dbus_message_iter_close_container(&value, &iter);
310 dbus_message_iter_close_container(entry, &value);
313 static DBusMessage *get_properties(DBusConnection *conn,
314 DBusMessage *msg, void *data)
316 struct connman_device *device = data;
318 DBusMessageIter array, dict, entry;
321 DBG("conn %p", conn);
323 if (__connman_security_check_privilege(msg,
324 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
325 return __connman_error_permission_denied(msg);
327 reply = dbus_message_new_method_return(msg);
331 dbus_message_iter_init_append(reply, &array);
333 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
334 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
335 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
336 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
338 if (device->name != NULL)
339 connman_dbus_dict_append_variant(&dict, "Name",
340 DBUS_TYPE_STRING, &device->name);
342 str = type2string(device->type);
344 connman_dbus_dict_append_variant(&dict, "Type",
345 DBUS_TYPE_STRING, &str);
347 if (device->address != NULL)
348 connman_dbus_dict_append_variant(&dict, "Address",
349 DBUS_TYPE_STRING, &device->address);
351 if (device->interface != NULL)
352 connman_dbus_dict_append_variant(&dict, "Interface",
353 DBUS_TYPE_STRING, &device->interface);
355 str = policy2string(device->policy);
357 connman_dbus_dict_append_variant(&dict, "Policy",
358 DBUS_TYPE_STRING, &str);
360 if (device->priority > 0)
361 connman_dbus_dict_append_variant(&dict, "Priority",
362 DBUS_TYPE_BYTE, &device->priority);
364 connman_dbus_dict_append_variant(&dict, "Powered",
365 DBUS_TYPE_BOOLEAN, &device->powered);
367 if (device->driver && device->driver->scan)
368 connman_dbus_dict_append_variant(&dict, "Scanning",
369 DBUS_TYPE_BOOLEAN, &device->scanning);
371 switch (device->mode) {
372 case CONNMAN_DEVICE_MODE_UNKNOWN:
374 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
375 __connman_element_append_ipv4(&device->element, &dict);
377 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
378 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
379 if (device->scan_interval > 0)
380 connman_dbus_dict_append_variant(&dict, "ScanInterval",
381 DBUS_TYPE_UINT16, &device->scan_interval);
383 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
385 append_networks(device, &entry);
386 dbus_message_iter_close_container(&dict, &entry);
390 dbus_message_iter_close_container(&array, &dict);
395 static DBusMessage *set_property(DBusConnection *conn,
396 DBusMessage *msg, void *data)
398 struct connman_device *device = data;
399 DBusMessageIter iter, value;
403 DBG("conn %p", conn);
405 if (dbus_message_iter_init(msg, &iter) == FALSE)
406 return __connman_error_invalid_arguments(msg);
408 dbus_message_iter_get_basic(&iter, &name);
409 dbus_message_iter_next(&iter);
410 dbus_message_iter_recurse(&iter, &value);
412 if (__connman_security_check_privilege(msg,
413 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
414 return __connman_error_permission_denied(msg);
416 type = dbus_message_iter_get_arg_type(&value);
418 if (g_str_equal(name, "Powered") == TRUE) {
419 connman_bool_t powered;
422 if (type != DBUS_TYPE_BOOLEAN)
423 return __connman_error_invalid_arguments(msg);
425 dbus_message_iter_get_basic(&value, &powered);
427 if (device->powered == powered)
428 return __connman_error_invalid_arguments(msg);
430 err = set_powered(device, powered);
431 if (err < 0 && err != -EINPROGRESS)
432 return __connman_error_failed(msg);
433 } else if (g_str_equal(name, "Policy") == TRUE) {
434 enum connman_device_policy policy;
438 if (type != DBUS_TYPE_STRING)
439 return __connman_error_invalid_arguments(msg);
441 dbus_message_iter_get_basic(&value, &str);
442 policy = string2policy(str);
443 if (policy == CONNMAN_DEVICE_POLICY_UNKNOWN)
444 return __connman_error_invalid_arguments(msg);
446 err = set_policy(conn, device, policy);
448 return __connman_error_failed(msg);
449 } else if (g_str_equal(name, "Priority") == TRUE) {
450 connman_uint8_t priority;
452 if (type != DBUS_TYPE_BYTE)
453 return __connman_error_invalid_arguments(msg);
455 dbus_message_iter_get_basic(&value, &priority);
457 device->priority = priority;
458 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
459 connman_uint16_t interval;
461 switch (device->mode) {
462 case CONNMAN_DEVICE_MODE_UNKNOWN:
463 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
464 return __connman_error_invalid_arguments(msg);
465 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
466 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
470 if (type != DBUS_TYPE_UINT16)
471 return __connman_error_invalid_arguments(msg);
473 dbus_message_iter_get_basic(&value, &interval);
475 device->scan_interval = interval;
477 if (device->scan_timeout > 0) {
478 g_source_remove(device->scan_timeout);
479 device->scan_timeout = 0;
482 if (device->scan_interval > 0) {
483 guint interval = device->scan_interval;
484 device->scan_timeout = g_timeout_add_seconds(interval,
485 device_scan_trigger, device);
487 } else if (g_str_has_prefix(name, "IPv4") == TRUE) {
488 switch (device->mode) {
489 case CONNMAN_DEVICE_MODE_UNKNOWN:
490 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
491 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
492 return __connman_error_invalid_arguments(msg);
493 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
494 __connman_element_set_ipv4(&device->element,
500 __connman_storage_save_device(device);
502 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
505 static DBusMessage *join_network(DBusConnection *conn,
506 DBusMessage *msg, void *data)
508 struct connman_device *device = data;
509 struct connman_network *network;
510 enum connman_network_type type;
511 DBusMessageIter iter, array;
514 DBG("conn %p", conn);
516 if (__connman_security_check_privilege(msg,
517 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
518 return __connman_error_permission_denied(msg);
520 if (!device->driver || !device->driver->join)
521 return __connman_error_not_supported(msg);
523 dbus_message_iter_init(msg, &iter);
524 dbus_message_iter_recurse(&iter, &array);
526 switch (device->type) {
527 case CONNMAN_DEVICE_TYPE_WIFI:
528 type = CONNMAN_NETWORK_TYPE_WIFI;
531 return __connman_error_not_supported(msg);
534 network = connman_network_create("00_00_00_00_00_00", type);
536 return __connman_error_failed(msg);
538 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
539 DBusMessageIter entry, value;
540 const char *key, *str;
542 dbus_message_iter_recurse(&array, &entry);
543 dbus_message_iter_get_basic(&entry, &key);
545 dbus_message_iter_next(&entry);
546 dbus_message_iter_recurse(&entry, &value);
548 switch (dbus_message_iter_get_arg_type(&value)) {
549 case DBUS_TYPE_STRING:
550 dbus_message_iter_get_basic(&value, &str);
551 if (g_str_equal(key, "WiFi.SSID") == TRUE)
552 connman_network_set_blob(network, key,
555 connman_network_set_string(network, key, str);
559 dbus_message_iter_next(&array);
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);
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);
624 err = device->driver->scan(device);
626 return __connman_error_failed(msg);
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 { "JoinNetwork", "a{sv}", "", join_network },
635 { "CreateNetwork", "a{sv}", "o", create_network },
636 { "RemoveNetwork", "o", "", remove_network },
637 { "ProposeScan", "", "", propose_scan },
641 static GDBusSignalTable device_signals[] = {
642 { "PropertyChanged", "sv" },
646 static DBusConnection *connection;
648 static void append_devices(DBusMessageIter *entry)
650 DBusMessageIter value, iter;
651 const char *key = "Devices";
653 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
655 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
656 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
659 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
660 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
661 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
662 dbus_message_iter_close_container(&value, &iter);
664 dbus_message_iter_close_container(entry, &value);
667 static void emit_devices_signal(void)
670 DBusMessageIter entry;
672 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
673 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
677 dbus_message_iter_init_append(signal, &entry);
679 append_devices(&entry);
681 g_dbus_send_message(connection, signal);
684 static int register_interface(struct connman_element *element)
686 struct connman_device *device = element->device;
688 DBG("element %p name %s", element, element->name);
690 if (g_dbus_register_interface(connection, element->path,
691 CONNMAN_DEVICE_INTERFACE,
692 device_methods, device_signals,
693 NULL, device, NULL) == FALSE) {
694 connman_error("Failed to register %s device", element->path);
698 device->registered = TRUE;
700 emit_devices_signal();
705 static void unregister_interface(struct connman_element *element)
707 struct connman_device *device = element->device;
709 DBG("element %p name %s", element, element->name);
711 device->registered = FALSE;
713 emit_devices_signal();
715 g_dbus_unregister_interface(connection, element->path,
716 CONNMAN_DEVICE_INTERFACE);
719 static void device_enable(struct connman_device *device)
721 DBG("device %p", device);
723 if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE ||
724 device->policy == CONNMAN_DEVICE_POLICY_OFF)
727 if (device->powered == TRUE)
730 if (device->driver->enable) {
731 device->driver->enable(device);
732 __connman_notifier_device_type_increase(device->type);
736 static void device_disable(struct connman_device *device)
738 DBG("device %p", device);
740 if (device->policy == CONNMAN_DEVICE_POLICY_IGNORE)
743 if (device->powered == FALSE)
746 g_hash_table_remove_all(device->networks);
748 if (device->driver->disable) {
749 device->driver->disable(device);
750 __connman_notifier_device_type_decrease(device->type);
754 static int setup_device(struct connman_device *device)
758 DBG("device %p", device);
760 err = register_interface(&device->element);
762 if (device->driver->remove)
763 device->driver->remove(device);
764 device->driver = NULL;
768 switch (device->mode) {
769 case CONNMAN_DEVICE_MODE_UNKNOWN:
770 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
771 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
773 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
774 __connman_profile_add_device(device);
778 device_enable(device);
783 static void probe_driver(struct connman_element *element, gpointer user_data)
785 struct connman_device_driver *driver = user_data;
787 DBG("element %p name %s", element, element->name);
789 if (element->device == NULL)
792 if (element->device->driver != NULL)
795 if (driver->type != element->device->type)
798 if (driver->probe(element->device) < 0)
801 element->device->driver = driver;
803 setup_device(element->device);
806 static void remove_device(struct connman_device *device)
808 DBG("device %p", device);
810 device_disable(device);
812 switch (device->mode) {
813 case CONNMAN_DEVICE_MODE_UNKNOWN:
814 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
815 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
817 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
818 __connman_profile_remove_device(device);
822 unregister_interface(&device->element);
824 if (device->driver->remove)
825 device->driver->remove(device);
827 device->driver = NULL;
830 static void remove_driver(struct connman_element *element, gpointer user_data)
832 struct connman_device_driver *driver = user_data;
834 DBG("element %p name %s", element, element->name);
836 if (element->device == NULL)
839 if (element->device->driver == driver)
840 remove_device(element->device);
843 connman_bool_t __connman_device_has_driver(struct connman_device *device)
845 if (device == NULL || device->driver == NULL)
848 return device->registered;
851 static GSList *driver_list = NULL;
853 static gint compare_priority(gconstpointer a, gconstpointer b)
855 const struct connman_device_driver *driver1 = a;
856 const struct connman_device_driver *driver2 = b;
858 return driver2->priority - driver1->priority;
862 * connman_device_driver_register:
863 * @driver: device driver definition
865 * Register a new device driver
867 * Returns: %0 on success
869 int connman_device_driver_register(struct connman_device_driver *driver)
871 DBG("driver %p name %s", driver, driver->name);
873 driver_list = g_slist_insert_sorted(driver_list, driver,
876 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
877 probe_driver, driver);
883 * connman_device_driver_unregister:
884 * @driver: device driver definition
886 * Remove a previously registered device driver
888 void connman_device_driver_unregister(struct connman_device_driver *driver)
890 DBG("driver %p name %s", driver, driver->name);
892 driver_list = g_slist_remove(driver_list, driver);
894 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
895 remove_driver, driver);
898 static void unregister_network(gpointer data)
900 struct connman_network *network = data;
902 DBG("network %p", network);
904 connman_element_unregister((struct connman_element *) network);
906 connman_network_unref(network);
909 static void device_destruct(struct connman_element *element)
911 struct connman_device *device = element->device;
913 DBG("element %p name %s", element, element->name);
915 g_free(device->node);
916 g_free(device->name);
917 g_free(device->address);
918 g_free(device->interface);
920 g_free(device->last_network);
922 g_hash_table_destroy(device->networks);
923 device->networks = NULL;
927 * connman_device_create:
928 * @node: device node name (for example an address)
931 * Allocate a new device of given #type and assign the #node name to it.
933 * Returns: a newly-allocated #connman_device structure
935 struct connman_device *connman_device_create(const char *node,
936 enum connman_device_type type)
938 struct connman_device *device;
941 DBG("node %s type %d", node, type);
943 device = g_try_new0(struct connman_device, 1);
947 DBG("device %p", device);
949 __connman_element_initialize(&device->element);
951 device->element.name = g_strdup(node);
952 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
954 device->element.device = device;
955 device->element.destruct = device_destruct;
957 str = type2string(type);
959 connman_element_set_string(&device->element,
960 CONNMAN_PROPERTY_ID_TYPE, str);
962 device->element.ipv4.method = CONNMAN_IPV4_METHOD_DHCP;
965 device->name = g_strdup(type2description(device->type));
966 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
967 device->policy = CONNMAN_DEVICE_POLICY_AUTO;
970 case CONNMAN_DEVICE_TYPE_UNKNOWN:
971 case CONNMAN_DEVICE_TYPE_VENDOR:
972 device->priority = 0;
973 device->scan_interval = 0;
975 case CONNMAN_DEVICE_TYPE_ETHERNET:
976 case CONNMAN_DEVICE_TYPE_WIFI:
977 device->priority = 100;
978 device->scan_interval = 300;
980 case CONNMAN_DEVICE_TYPE_WIMAX:
981 device->priority = 20;
982 device->scan_interval = 0;
984 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
985 device->priority = 50;
986 device->scan_interval = 0;
988 case CONNMAN_DEVICE_TYPE_GPS:
989 device->priority = 0;
990 device->scan_interval = 0;
992 case CONNMAN_DEVICE_TYPE_HSO:
993 case CONNMAN_DEVICE_TYPE_NOZOMI:
994 case CONNMAN_DEVICE_TYPE_HUAWEI:
995 case CONNMAN_DEVICE_TYPE_NOVATEL:
996 device->priority = 60;
997 device->scan_interval = 0;
1001 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
1002 g_free, unregister_network);
1008 * connman_device_ref:
1009 * @device: device structure
1011 * Increase reference counter of device
1013 struct connman_device *connman_device_ref(struct connman_device *device)
1015 if (connman_element_ref(&device->element) == NULL)
1022 * connman_device_unref:
1023 * @device: device structure
1025 * Decrease reference counter of device
1027 void connman_device_unref(struct connman_device *device)
1029 connman_element_unref(&device->element);
1032 const char *__connman_device_get_type(struct connman_device *device)
1034 return type2string(device->type);
1038 * connman_device_get_type:
1039 * @device: device structure
1041 * Get type of device
1043 enum connman_device_type connman_device_get_type(struct connman_device *device)
1045 return device->type;
1049 * connman_device_get_name:
1050 * @device: device structure
1052 * Get unique name of device
1054 const char *connman_device_get_name(struct connman_device *device)
1056 return device->element.name;
1060 * connman_device_get_path:
1061 * @device: device structure
1063 * Get path name of device
1065 const char *connman_device_get_path(struct connman_device *device)
1067 return device->element.path;
1071 * connman_device_set_index:
1072 * @device: device structure
1073 * @index: index number
1075 * Set index number of device
1077 void connman_device_set_index(struct connman_device *device, int index)
1079 device->element.index = index;
1083 * connman_device_get_index:
1084 * @device: device structure
1086 * Get index number of device
1088 int connman_device_get_index(struct connman_device *device)
1090 return device->element.index;
1094 * connman_device_set_interface:
1095 * @device: device structure
1096 * @interface: interface name
1098 * Set interface name of device
1100 void connman_device_set_interface(struct connman_device *device,
1101 const char *interface)
1103 g_free(device->element.devname);
1104 device->element.devname = g_strdup(interface);
1106 g_free(device->interface);
1107 device->interface = g_strdup(interface);
1109 if (device->name == NULL) {
1110 const char *str = type2description(device->type);
1111 if (str != NULL && device->interface != NULL)
1112 device->name = g_strdup_printf("%s (%s)", str,
1118 * connman_device_get_interface:
1119 * @device: device structure
1121 * Get interface name of device
1123 const char *connman_device_get_interface(struct connman_device *device)
1125 return device->interface;
1129 * connman_device_set_policy:
1130 * @device: device structure
1131 * @policy: power and connection policy
1133 * Change power and connection policy of device
1135 void connman_device_set_policy(struct connman_device *device,
1136 enum connman_device_policy policy)
1138 device->policy = policy;
1142 * connman_device_set_mode:
1143 * @device: device structure
1144 * @mode: network mode
1146 * Change network mode of device
1148 void connman_device_set_mode(struct connman_device *device,
1149 enum connman_device_mode mode)
1151 device->mode = mode;
1155 * connman_device_get_mode:
1156 * @device: device structure
1158 * Get network mode of device
1160 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1162 return device->mode;
1166 * connman_device_set_powered:
1167 * @device: device structure
1168 * @powered: powered state
1170 * Change power state of device
1172 int connman_device_set_powered(struct connman_device *device,
1173 connman_bool_t powered)
1175 DBusMessage *signal;
1176 DBusMessageIter entry, value;
1177 const char *key = "Powered";
1179 DBG("driver %p powered %d", device, powered);
1181 if (device->powered == powered)
1184 device->powered = powered;
1186 if (device->registered == FALSE)
1189 signal = dbus_message_new_signal(device->element.path,
1190 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1194 dbus_message_iter_init_append(signal, &entry);
1196 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1198 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1199 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1200 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &powered);
1201 dbus_message_iter_close_container(&entry, &value);
1203 g_dbus_send_message(connection, signal);
1205 if (powered == FALSE)
1208 if (device->policy != CONNMAN_DEVICE_POLICY_AUTO)
1211 if (device->scan_timeout > 0) {
1212 g_source_remove(device->scan_timeout);
1213 device->scan_timeout = 0;
1216 if (device->scan_interval > 0) {
1217 guint interval = device->scan_interval;
1218 device->scan_timeout = g_timeout_add_seconds(interval,
1219 device_scan_trigger, device);
1222 if (device->driver->scan)
1223 device->driver->scan(device);
1229 * connman_device_set_carrier:
1230 * @device: device structure
1231 * @carrier: carrier state
1233 * Change carrier state of device (only for device without scanning)
1235 int connman_device_set_carrier(struct connman_device *device,
1236 connman_bool_t carrier)
1238 DBG("driver %p carrier %d", device, carrier);
1240 switch (device->mode) {
1241 case CONNMAN_DEVICE_MODE_UNKNOWN:
1242 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1243 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1245 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1249 if (device->carrier == carrier)
1252 device->carrier = carrier;
1254 switch (device->policy) {
1255 case CONNMAN_DEVICE_POLICY_UNKNOWN:
1256 case CONNMAN_DEVICE_POLICY_IGNORE:
1257 case CONNMAN_DEVICE_POLICY_OFF:
1259 case CONNMAN_DEVICE_POLICY_AUTO:
1260 case CONNMAN_DEVICE_POLICY_MANUAL:
1264 return set_carrier(device, device->carrier);
1267 void __connman_device_disconnect(struct connman_device *device)
1269 GHashTableIter iter;
1270 gpointer key, value;
1272 DBG("device %p", device);
1274 connman_device_set_disconnected(device, TRUE);
1276 g_hash_table_iter_init(&iter, device->networks);
1278 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1279 struct connman_network *network = value;
1281 if (connman_network_get_connected(network) == TRUE)
1282 __connman_network_disconnect(network);
1286 static void connect_known_network(struct connman_device *device)
1288 struct connman_network *network = NULL;
1289 GHashTableIter iter;
1290 gpointer key, value;
1291 unsigned int count = 0;
1293 DBG("device %p", device);
1295 g_hash_table_iter_init(&iter, device->networks);
1297 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1298 connman_uint8_t old_priority, new_priority;
1299 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 (connman_network_get_remember(value) == FALSE)
1319 if (network == NULL) {
1324 old_priority = connman_network_get_uint8(network,
1325 CONNMAN_PROPERTY_ID_PRIORITY);
1326 new_priority = connman_network_get_uint8(value,
1327 CONNMAN_PROPERTY_ID_PRIORITY);
1329 if (new_priority != old_priority) {
1330 if (new_priority > old_priority)
1335 old_strength = connman_network_get_uint8(network,
1336 CONNMAN_PROPERTY_ID_STRENGTH);
1337 new_strength = connman_network_get_uint8(value,
1338 CONNMAN_PROPERTY_ID_STRENGTH);
1340 if (new_strength > old_strength)
1344 if (network != NULL) {
1347 err = connman_network_connect(network);
1348 if (err == 0 || err == -EINPROGRESS)
1355 if (device->driver && device->driver->scan)
1356 device->driver->scan(device);
1359 static void mark_network_unavailable(gpointer key, gpointer value,
1362 struct connman_network *network = value;
1364 if (connman_network_get_connected(network) == TRUE)
1367 connman_network_set_available(network, FALSE);
1370 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1373 struct connman_network *network = value;
1375 if (connman_network_get_connected(network) == TRUE)
1378 if (connman_network_get_remember(network) == TRUE)
1381 if (connman_network_get_available(network) == TRUE)
1388 * connman_device_set_scanning:
1389 * @device: device structure
1390 * @scanning: scanning state
1392 * Change scanning state of device
1394 int connman_device_set_scanning(struct connman_device *device,
1395 connman_bool_t scanning)
1397 DBusMessage *signal;
1398 DBusMessageIter entry, value;
1399 const char *key = "Scanning";
1401 DBG("driver %p scanning %d", device, scanning);
1403 if (!device->driver || !device->driver->scan)
1406 if (device->scanning == scanning)
1409 device->scanning = scanning;
1411 signal = dbus_message_new_signal(device->element.path,
1412 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1416 dbus_message_iter_init_append(signal, &entry);
1418 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1420 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1421 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1422 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1423 dbus_message_iter_close_container(&entry, &value);
1425 g_dbus_send_message(connection, signal);
1427 if (scanning == TRUE) {
1428 if (device->scan_timeout > 0) {
1429 g_source_remove(device->scan_timeout);
1430 device->scan_timeout = 0;
1433 if (device->scan_interval > 0) {
1434 guint interval = device->scan_interval;
1435 device->scan_timeout = g_timeout_add_seconds(interval,
1436 device_scan_trigger, device);
1439 g_hash_table_foreach(device->networks,
1440 mark_network_unavailable, NULL);
1444 g_hash_table_foreach_remove(device->networks,
1445 remove_unavailable_network, NULL);
1447 if (device->connections > 0)
1450 if (device->disconnected == TRUE)
1453 if (device->policy != CONNMAN_DEVICE_POLICY_AUTO)
1456 connect_known_network(device);
1462 * connman_device_set_disconnected:
1463 * @device: device structure
1464 * @disconnected: disconnected state
1466 * Change disconnected state of device (only for device with networks)
1468 int connman_device_set_disconnected(struct connman_device *device,
1469 connman_bool_t disconnected)
1471 DBG("driver %p disconnected %d", device, disconnected);
1473 switch (device->mode) {
1474 case CONNMAN_DEVICE_MODE_UNKNOWN:
1475 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1477 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1478 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1482 if (device->disconnected == disconnected)
1485 device->disconnected = disconnected;
1491 * connman_device_set_string:
1492 * @device: device structure
1493 * @key: unique identifier
1494 * @value: string value
1496 * Set string value for specific key
1498 int connman_device_set_string(struct connman_device *device,
1499 const char *key, const char *value)
1501 DBG("device %p key %s value %s", device, key, value);
1503 if (g_str_equal(key, "Address") == TRUE) {
1504 g_free(device->address);
1505 device->address = g_strdup(value);
1506 } else if (g_str_equal(key, "Name") == TRUE) {
1507 g_free(device->name);
1508 device->name = g_strdup(value);
1509 } else if (g_str_equal(key, "Node") == TRUE) {
1510 g_free(device->node);
1511 device->node = g_strdup(value);
1514 return connman_element_set_string(&device->element, key, value);
1518 * connman_device_get_string:
1519 * @device: device structure
1520 * @key: unique identifier
1522 * Get string value for specific key
1524 const char *connman_device_get_string(struct connman_device *device,
1527 DBG("device %p key %s", device, key);
1529 if (g_str_equal(key, "Address") == TRUE)
1530 return device->address;
1531 else if (g_str_equal(key, "Name") == TRUE)
1532 return device->name;
1533 else if (g_str_equal(key, "Node") == TRUE)
1534 return device->node;
1536 return connman_element_get_string(&device->element, key);
1539 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1541 struct connman_device *device = element->device;
1542 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1543 connman_bool_t powered;
1545 DBG("element %p name %s", element, element->name);
1550 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1552 if (device->powered == powered)
1555 set_powered(device, powered);
1558 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1560 DBG("offlinmode %d", offlinemode);
1562 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1563 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1565 __connman_notifier_offline_mode(offlinemode);
1570 void __connman_device_increase_connections(struct connman_device *device)
1572 device->connections++;
1575 void __connman_device_decrease_connections(struct connman_device *device)
1577 device->connections--;
1581 * connman_device_add_network:
1582 * @device: device structure
1583 * @network: network structure
1585 * Add new network to the device
1587 int connman_device_add_network(struct connman_device *device,
1588 struct connman_network *network)
1590 const char *identifier = connman_network_get_identifier(network);
1593 DBG("device %p network %p", device, network);
1595 switch (device->mode) {
1596 case CONNMAN_DEVICE_MODE_UNKNOWN:
1597 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1599 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1600 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1604 __connman_network_set_device(network, device);
1606 __connman_storage_load_network(network);
1608 err = connman_element_register((struct connman_element *) network,
1611 __connman_network_set_device(network, NULL);
1615 g_hash_table_insert(device->networks, g_strdup(identifier),
1622 * connman_device_get_network:
1623 * @device: device structure
1624 * @identifier: network identifier
1626 * Get network for given identifier
1628 struct connman_network *connman_device_get_network(struct connman_device *device,
1629 const char *identifier)
1631 DBG("device %p identifier %s", device, identifier);
1633 return g_hash_table_lookup(device->networks, identifier);
1637 * connman_device_remove_network:
1638 * @device: device structure
1639 * @identifier: network identifier
1641 * Remove network for given identifier
1643 int connman_device_remove_network(struct connman_device *device,
1644 const char *identifier)
1646 DBG("device %p identifier %s", device, identifier);
1648 g_hash_table_remove(device->networks, identifier);
1653 void __connman_device_set_network(struct connman_device *device,
1654 struct connman_network *network)
1658 if (network != NULL) {
1659 name = connman_network_get_string(network,
1660 CONNMAN_PROPERTY_ID_NAME);
1661 device->last_network = g_strdup(name);
1664 device->network = network;
1668 * connman_device_register:
1669 * @device: device structure
1671 * Register device with the system
1673 int connman_device_register(struct connman_device *device)
1675 __connman_storage_load_device(device);
1677 switch (device->mode) {
1678 case CONNMAN_DEVICE_MODE_UNKNOWN:
1679 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1681 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1682 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1683 __connman_storage_init_network(device);
1687 return connman_element_register(&device->element, NULL);
1691 * connman_device_unregister:
1692 * @device: device structure
1694 * Unregister device with the system
1696 void connman_device_unregister(struct connman_device *device)
1698 __connman_storage_save_device(device);
1700 connman_element_unregister(&device->element);
1704 * connman_device_get_data:
1705 * @device: device structure
1707 * Get private device data pointer
1709 void *connman_device_get_data(struct connman_device *device)
1711 return device->driver_data;
1715 * connman_device_set_data:
1716 * @device: device structure
1717 * @data: data pointer
1719 * Set private device data pointer
1721 void connman_device_set_data(struct connman_device *device, void *data)
1723 device->driver_data = data;
1726 static gboolean match_driver(struct connman_device *device,
1727 struct connman_device_driver *driver)
1729 if (device->type == driver->type ||
1730 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1736 static int device_probe(struct connman_element *element)
1738 struct connman_device *device = element->device;
1741 DBG("element %p name %s", element, element->name);
1746 if (device->driver != NULL)
1749 for (list = driver_list; list; list = list->next) {
1750 struct connman_device_driver *driver = list->data;
1752 if (match_driver(device, driver) == FALSE)
1755 DBG("driver %p name %s", driver, driver->name);
1757 if (driver->probe(device) == 0) {
1758 device->driver = driver;
1763 if (device->driver == NULL)
1766 return setup_device(device);
1769 static void device_remove(struct connman_element *element)
1771 struct connman_device *device = element->device;
1773 DBG("element %p name %s", element, element->name);
1778 if (device->driver == NULL)
1781 remove_device(device);
1784 static struct connman_driver device_driver = {
1786 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1787 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1788 .probe = device_probe,
1789 .remove = device_remove,
1792 static int device_load(struct connman_device *device)
1795 gchar *pathname, *data = NULL;
1800 DBG("device %p", device);
1802 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1803 device->element.name);
1804 if (pathname == NULL)
1807 keyfile = g_key_file_new();
1809 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
1816 if (g_key_file_load_from_data(keyfile, data, length,
1817 0, NULL) == FALSE) {
1824 str = g_key_file_get_string(keyfile, "Configuration", "Policy", NULL);
1826 device->policy = string2policy(str);
1830 val = g_key_file_get_integer(keyfile, "Configuration",
1833 device->priority = val;
1835 switch (device->mode) {
1836 case CONNMAN_DEVICE_MODE_UNKNOWN:
1837 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1839 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1840 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1841 val = g_key_file_get_integer(keyfile, "Configuration",
1842 "ScanInterval", NULL);
1844 device->scan_interval = val;
1849 str = g_key_file_get_string(keyfile, "Configuration",
1850 "LastNetwork", NULL);
1852 device->last_network = str;
1855 g_key_file_free(keyfile);
1860 static int device_save(struct connman_device *device)
1863 gchar *pathname, *data = NULL;
1867 DBG("device %p", device);
1869 pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
1870 device->element.name);
1871 if (pathname == NULL)
1874 keyfile = g_key_file_new();
1876 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
1880 if (g_key_file_load_from_data(keyfile, data, length,
1888 str = policy2string(device->policy);
1890 g_key_file_set_string(keyfile, "Configuration", "Policy", str);
1892 if (device->priority > 0)
1893 g_key_file_set_integer(keyfile, "Configuration",
1894 "Priority", device->priority);
1896 switch (device->mode) {
1897 case CONNMAN_DEVICE_MODE_UNKNOWN:
1898 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1900 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1901 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1902 if (device->scan_interval > 0)
1903 g_key_file_set_integer(keyfile, "Configuration",
1904 "ScanInterval", device->scan_interval);
1908 if (device->last_network != NULL)
1909 g_key_file_set_string(keyfile, "Configuration",
1910 "LastNetwork", device->last_network);
1912 data = g_key_file_to_data(keyfile, &length, NULL);
1914 g_file_set_contents(pathname, data, length, NULL);
1919 g_key_file_free(keyfile);
1923 if (device->network != NULL)
1924 __connman_storage_save_network(device->network);
1929 static struct connman_storage device_storage = {
1931 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1932 .device_load = device_load,
1933 .device_save = device_save,
1936 int __connman_device_init(void)
1940 connection = connman_dbus_get_connection();
1942 if (connman_storage_register(&device_storage) < 0)
1943 connman_error("Failed to register device storage");
1945 return connman_driver_register(&device_driver);
1948 void __connman_device_cleanup(void)
1952 connman_driver_unregister(&device_driver);
1954 connman_storage_unregister(&device_storage);
1956 dbus_connection_unref(connection);