5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 static DBusConnection *connection = NULL;
35 struct connman_device {
36 struct connman_element element;
37 enum connman_device_type type;
38 enum connman_device_mode mode;
39 connman_bool_t secondary;
40 connman_bool_t offlinemode;
41 connman_bool_t blocked;
42 connman_bool_t powered;
43 connman_bool_t powered_pending;
44 connman_bool_t powered_persistent;
45 connman_bool_t carrier;
46 connman_bool_t scanning;
47 connman_bool_t disconnected;
48 connman_uint16_t scan_interval;
56 unsigned int connections;
58 struct connman_ipconfig *ipconfig;
60 struct connman_device_driver *driver;
63 connman_bool_t registered;
66 struct connman_network *network;
73 static gboolean device_scan_trigger(gpointer user_data)
75 struct connman_device *device = user_data;
77 DBG("device %p", device);
79 if (device->driver == NULL) {
80 device->scan_timeout = 0;
84 if (device->driver->scan)
85 device->driver->scan(device);
90 static void clear_scan_trigger(struct connman_device *device)
92 if (device->scan_timeout > 0) {
93 g_source_remove(device->scan_timeout);
94 device->scan_timeout = 0;
98 static void reset_scan_trigger(struct connman_device *device)
100 clear_scan_trigger(device);
102 if (device->scan_interval > 0) {
103 guint interval = device->scan_interval;
104 device->scan_timeout = g_timeout_add_seconds(interval,
105 device_scan_trigger, device);
109 static void force_scan_trigger(struct connman_device *device)
111 clear_scan_trigger(device);
113 device->scan_timeout = g_timeout_add_seconds(5,
114 device_scan_trigger, device);
117 static const char *type2description(enum connman_device_type type)
120 case CONNMAN_DEVICE_TYPE_UNKNOWN:
121 case CONNMAN_DEVICE_TYPE_VENDOR:
123 case CONNMAN_DEVICE_TYPE_ETHERNET:
125 case CONNMAN_DEVICE_TYPE_WIFI:
127 case CONNMAN_DEVICE_TYPE_WIMAX:
129 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
131 case CONNMAN_DEVICE_TYPE_GPS:
133 case CONNMAN_DEVICE_TYPE_MBM:
134 case CONNMAN_DEVICE_TYPE_HSO:
135 case CONNMAN_DEVICE_TYPE_NOZOMI:
136 case CONNMAN_DEVICE_TYPE_HUAWEI:
137 case CONNMAN_DEVICE_TYPE_NOVATEL:
144 static const char *type2string(enum connman_device_type type)
147 case CONNMAN_DEVICE_TYPE_UNKNOWN:
148 case CONNMAN_DEVICE_TYPE_VENDOR:
150 case CONNMAN_DEVICE_TYPE_ETHERNET:
152 case CONNMAN_DEVICE_TYPE_WIFI:
154 case CONNMAN_DEVICE_TYPE_WIMAX:
156 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
158 case CONNMAN_DEVICE_TYPE_GPS:
160 case CONNMAN_DEVICE_TYPE_MBM:
161 case CONNMAN_DEVICE_TYPE_HSO:
162 case CONNMAN_DEVICE_TYPE_HUAWEI:
163 case CONNMAN_DEVICE_TYPE_NOZOMI:
164 case CONNMAN_DEVICE_TYPE_NOVATEL:
171 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
173 enum connman_device_type type = connman_device_get_type(device);
176 case CONNMAN_DEVICE_TYPE_UNKNOWN:
177 case CONNMAN_DEVICE_TYPE_VENDOR:
178 case CONNMAN_DEVICE_TYPE_GPS:
179 case CONNMAN_DEVICE_TYPE_NOZOMI:
180 case CONNMAN_DEVICE_TYPE_HUAWEI:
181 case CONNMAN_DEVICE_TYPE_NOVATEL:
183 case CONNMAN_DEVICE_TYPE_ETHERNET:
184 return CONNMAN_SERVICE_TYPE_ETHERNET;
185 case CONNMAN_DEVICE_TYPE_WIFI:
186 return CONNMAN_SERVICE_TYPE_WIFI;
187 case CONNMAN_DEVICE_TYPE_WIMAX:
188 return CONNMAN_SERVICE_TYPE_WIMAX;
189 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
190 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
191 case CONNMAN_DEVICE_TYPE_MBM:
192 case CONNMAN_DEVICE_TYPE_HSO:
193 return CONNMAN_SERVICE_TYPE_CELLULAR;
196 return CONNMAN_SERVICE_TYPE_UNKNOWN;
199 static int set_connected(struct connman_device *device,
200 connman_bool_t connected)
202 if (connected == TRUE) {
203 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
204 struct connman_element *element;
206 device->disconnected = TRUE;
208 switch (device->element.ipv4.method) {
209 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
210 case CONNMAN_IPCONFIG_METHOD_IGNORE:
212 case CONNMAN_IPCONFIG_METHOD_STATIC:
213 type = CONNMAN_ELEMENT_TYPE_IPV4;
215 case CONNMAN_IPCONFIG_METHOD_DHCP:
216 type = CONNMAN_ELEMENT_TYPE_DHCP;
220 element = connman_element_create(NULL);
221 if (element != NULL) {
222 struct connman_service *service;
224 element->type = type;
225 element->index = device->element.index;
227 if (connman_element_register(element,
228 &device->element) < 0)
229 connman_element_unref(element);
231 device->disconnected = FALSE;
233 service = __connman_service_lookup_from_device(device);
234 __connman_service_indicate_state(service,
235 CONNMAN_SERVICE_STATE_CONFIGURATION);
238 struct connman_service *service;
240 connman_element_unregister_children(&device->element);
242 device->disconnected = TRUE;
244 service = __connman_service_lookup_from_device(device);
245 __connman_service_indicate_state(service,
246 CONNMAN_SERVICE_STATE_IDLE);
249 if (connected == TRUE) {
250 enum connman_service_type type;
252 type = __connman_device_get_service_type(device);
253 __connman_notifier_connect(type);
255 enum connman_service_type type;
257 type = __connman_device_get_service_type(device);
258 __connman_notifier_disconnect(type);
264 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
267 __connman_profile_add_device(device);
269 __connman_profile_remove_device(device);
271 return set_connected(device, carrier);
274 static int powered_changed(struct connman_device *device)
277 DBusMessageIter entry, value;
278 const char *key = "Powered";
280 signal = dbus_message_new_signal(device->element.path,
281 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
285 dbus_message_iter_init_append(signal, &entry);
287 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
289 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
290 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
291 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
293 dbus_message_iter_close_container(&entry, &value);
295 g_dbus_send_message(connection, signal);
300 static int set_powered(struct connman_device *device, connman_bool_t powered)
302 struct connman_device_driver *driver = device->driver;
303 enum connman_service_type type;
306 DBG("device %p powered %d", device, powered);
308 if (device->powered_pending == powered)
314 type = __connman_device_get_service_type(device);
316 if (powered == TRUE) {
317 if (driver->enable) {
318 device->powered_pending = powered;
320 err = driver->enable(device);
322 __connman_notifier_enable(type);
326 device->powered_pending = powered;
328 clear_scan_trigger(device);
330 g_hash_table_remove_all(device->networks);
332 set_carrier(device, FALSE);
334 if (driver->disable) {
335 err = driver->disable(device);
337 __connman_notifier_disable(type);
343 device->powered = powered;
345 if (device->registered == TRUE)
346 powered_changed(device);
352 static void append_path(gpointer key, gpointer value, gpointer user_data)
354 struct connman_element *element = value;
355 DBusMessageIter *iter = user_data;
357 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
361 static void append_networks(struct connman_device *device,
362 DBusMessageIter *entry)
364 DBusMessageIter value, iter;
365 const char *key = "Networks";
367 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
369 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
370 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
373 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
374 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
375 g_hash_table_foreach(device->networks, append_path, &iter);
376 dbus_message_iter_close_container(&value, &iter);
378 dbus_message_iter_close_container(entry, &value);
381 static DBusMessage *get_properties(DBusConnection *conn,
382 DBusMessage *msg, void *data)
384 struct connman_device *device = data;
386 DBusMessageIter array, dict, entry;
389 DBG("conn %p", conn);
391 if (__connman_security_check_privilege(msg,
392 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
393 return __connman_error_permission_denied(msg);
395 reply = dbus_message_new_method_return(msg);
399 dbus_message_iter_init_append(reply, &array);
401 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
402 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
403 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
404 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
406 if (device->name != NULL)
407 connman_dbus_dict_append_variant(&dict, "Name",
408 DBUS_TYPE_STRING, &device->name);
410 str = type2string(device->type);
412 connman_dbus_dict_append_variant(&dict, "Type",
413 DBUS_TYPE_STRING, &str);
415 if (device->address != NULL)
416 connman_dbus_dict_append_variant(&dict, "Address",
417 DBUS_TYPE_STRING, &device->address);
419 if (device->interface != NULL)
420 connman_dbus_dict_append_variant(&dict, "Interface",
421 DBUS_TYPE_STRING, &device->interface);
423 connman_dbus_dict_append_variant(&dict, "Powered",
424 DBUS_TYPE_BOOLEAN, &device->powered);
426 if (device->driver && device->driver->scan)
427 connman_dbus_dict_append_variant(&dict, "Scanning",
428 DBUS_TYPE_BOOLEAN, &device->scanning);
430 switch (device->mode) {
431 case CONNMAN_DEVICE_MODE_UNKNOWN:
433 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
434 __connman_element_append_ipv4(&device->element, &dict);
436 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
437 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
438 if (device->scan_interval > 0)
439 connman_dbus_dict_append_variant(&dict, "ScanInterval",
440 DBUS_TYPE_UINT16, &device->scan_interval);
442 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
444 append_networks(device, &entry);
445 dbus_message_iter_close_container(&dict, &entry);
449 dbus_message_iter_close_container(&array, &dict);
454 static gboolean powered_timeout(gpointer user_data)
456 struct connman_device *device = user_data;
458 DBG("device %p", device);
462 if (device->pending != NULL) {
465 reply = __connman_error_operation_timeout(device->pending);
467 g_dbus_send_message(connection, reply);
469 dbus_message_unref(device->pending);
470 device->pending = NULL;
476 static DBusMessage *set_property(DBusConnection *conn,
477 DBusMessage *msg, void *data)
479 struct connman_device *device = data;
480 DBusMessageIter iter, value;
484 DBG("conn %p", conn);
486 if (dbus_message_iter_init(msg, &iter) == FALSE)
487 return __connman_error_invalid_arguments(msg);
489 dbus_message_iter_get_basic(&iter, &name);
490 dbus_message_iter_next(&iter);
491 dbus_message_iter_recurse(&iter, &value);
493 if (__connman_security_check_privilege(msg,
494 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
495 return __connman_error_permission_denied(msg);
497 type = dbus_message_iter_get_arg_type(&value);
499 if (g_str_equal(name, "Powered") == TRUE) {
500 connman_bool_t powered;
503 if (type != DBUS_TYPE_BOOLEAN)
504 return __connman_error_invalid_arguments(msg);
506 dbus_message_iter_get_basic(&value, &powered);
508 device->powered_persistent = powered;
510 __connman_storage_save_device(device);
512 if (device->powered == powered)
513 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
515 if (device->pending != NULL)
516 return __connman_error_in_progress(msg);
518 err = set_powered(device, powered);
520 if (err != -EINPROGRESS)
521 return __connman_error_failed(msg, -err);
523 device->pending = dbus_message_ref(msg);
525 device->timeout = g_timeout_add_seconds(15,
526 powered_timeout, device);
530 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
531 connman_uint16_t interval;
533 switch (device->mode) {
534 case CONNMAN_DEVICE_MODE_UNKNOWN:
535 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
536 return __connman_error_invalid_arguments(msg);
537 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
538 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
542 if (type != DBUS_TYPE_UINT16)
543 return __connman_error_invalid_arguments(msg);
545 dbus_message_iter_get_basic(&value, &interval);
547 if (device->scan_interval != interval) {
548 device->scan_interval = interval;
550 __connman_storage_save_device(device);
552 reset_scan_trigger(device);
554 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
557 if (device->ipconfig == NULL)
558 return __connman_error_invalid_property(msg);
560 switch (device->mode) {
561 case CONNMAN_DEVICE_MODE_UNKNOWN:
562 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
563 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
564 return __connman_error_invalid_arguments(msg);
565 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
569 err = __connman_ipconfig_set_ipv4(device->ipconfig,
572 return __connman_error_failed(msg, -err);
574 return __connman_error_invalid_property(msg);
576 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
579 static DBusMessage *propose_scan(DBusConnection *conn,
580 DBusMessage *msg, void *data)
582 struct connman_device *device = data;
585 DBG("conn %p", conn);
587 switch (device->mode) {
588 case CONNMAN_DEVICE_MODE_UNKNOWN:
589 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
590 return __connman_error_not_supported(msg);
591 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
592 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
596 err = __connman_device_scan(device);
598 return __connman_error_failed(msg, -err);
600 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
603 static GDBusMethodTable device_methods[] = {
604 { "GetProperties", "", "a{sv}", get_properties },
605 { "SetProperty", "sv", "", set_property,
606 G_DBUS_METHOD_FLAG_ASYNC },
607 { "ProposeScan", "", "", propose_scan },
611 static GDBusSignalTable device_signals[] = {
612 { "PropertyChanged", "sv" },
616 static void append_devices(DBusMessageIter *entry)
618 DBusMessageIter value, iter;
619 const char *key = "Devices";
621 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
623 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
624 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
627 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
628 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
629 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
630 dbus_message_iter_close_container(&value, &iter);
632 dbus_message_iter_close_container(entry, &value);
635 static void emit_devices_signal(void)
638 DBusMessageIter entry;
640 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
641 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
645 dbus_message_iter_init_append(signal, &entry);
647 append_devices(&entry);
649 g_dbus_send_message(connection, signal);
652 static int register_interface(struct connman_element *element)
654 struct connman_device *device = element->device;
656 DBG("element %p name %s", element, element->name);
658 if (g_dbus_register_interface(connection, element->path,
659 CONNMAN_DEVICE_INTERFACE,
660 device_methods, device_signals,
661 NULL, device, NULL) == FALSE) {
662 connman_error("Failed to register %s device", element->path);
666 device->registered = TRUE;
668 emit_devices_signal();
673 static void unregister_interface(struct connman_element *element)
675 struct connman_device *device = element->device;
677 DBG("element %p name %s", element, element->name);
679 device->registered = FALSE;
681 emit_devices_signal();
683 g_dbus_unregister_interface(connection, element->path,
684 CONNMAN_DEVICE_INTERFACE);
687 static int setup_device(struct connman_device *device)
689 enum connman_service_type type;
692 DBG("device %p", device);
694 err = register_interface(&device->element);
696 if (device->driver->remove)
697 device->driver->remove(device);
698 device->driver = NULL;
702 type = __connman_device_get_service_type(device);
703 __connman_notifier_register(type);
705 switch (device->mode) {
706 case CONNMAN_DEVICE_MODE_UNKNOWN:
707 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
708 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
710 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
711 if (device->carrier == TRUE && device->secondary == FALSE)
712 __connman_profile_add_device(device);
716 if (device->offlinemode == FALSE &&
717 device->powered_persistent == TRUE)
718 __connman_device_enable(device);
723 static void probe_driver(struct connman_element *element, gpointer user_data)
725 struct connman_device_driver *driver = user_data;
727 DBG("element %p name %s", element, element->name);
729 if (element->device == NULL)
732 if (element->device->driver != NULL)
735 if (driver->type != element->device->type)
738 if (driver->probe(element->device) < 0)
741 element->device->driver = driver;
743 setup_device(element->device);
746 static void remove_device(struct connman_device *device)
748 enum connman_service_type type;
750 DBG("device %p", device);
752 __connman_device_disable(device);
754 switch (device->mode) {
755 case CONNMAN_DEVICE_MODE_UNKNOWN:
756 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
757 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
759 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
760 if (device->secondary == FALSE)
761 __connman_profile_remove_device(device);
765 type = __connman_device_get_service_type(device);
766 __connman_notifier_unregister(type);
768 unregister_interface(&device->element);
770 if (device->driver->remove)
771 device->driver->remove(device);
773 device->driver = NULL;
776 static void remove_driver(struct connman_element *element, gpointer user_data)
778 struct connman_device_driver *driver = user_data;
780 DBG("element %p name %s", element, element->name);
782 if (element->device == NULL)
785 if (element->device->driver == driver)
786 remove_device(element->device);
789 connman_bool_t __connman_device_has_driver(struct connman_device *device)
791 if (device == NULL || device->driver == NULL)
794 return device->registered;
797 static GSList *driver_list = NULL;
799 static gint compare_priority(gconstpointer a, gconstpointer b)
801 const struct connman_device_driver *driver1 = a;
802 const struct connman_device_driver *driver2 = b;
804 return driver2->priority - driver1->priority;
808 * connman_device_driver_register:
809 * @driver: device driver definition
811 * Register a new device driver
813 * Returns: %0 on success
815 int connman_device_driver_register(struct connman_device_driver *driver)
817 DBG("driver %p name %s", driver, driver->name);
819 driver_list = g_slist_insert_sorted(driver_list, driver,
822 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
823 probe_driver, driver);
829 * connman_device_driver_unregister:
830 * @driver: device driver definition
832 * Remove a previously registered device driver
834 void connman_device_driver_unregister(struct connman_device_driver *driver)
836 DBG("driver %p name %s", driver, driver->name);
838 driver_list = g_slist_remove(driver_list, driver);
840 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
841 remove_driver, driver);
844 static void unregister_network(gpointer data)
846 struct connman_network *network = data;
848 DBG("network %p", network);
850 connman_element_unregister((struct connman_element *) network);
852 connman_network_unref(network);
855 static void device_destruct(struct connman_element *element)
857 struct connman_device *device = element->device;
859 DBG("element %p name %s", element, element->name);
861 if (device->timeout > 0) {
862 g_source_remove(device->timeout);
866 if (device->pending != NULL) {
867 dbus_message_unref(device->pending);
868 device->pending = NULL;
871 g_free(device->ident);
872 g_free(device->node);
873 g_free(device->name);
874 g_free(device->address);
875 g_free(device->control);
876 g_free(device->interface);
878 if (device->ipconfig != NULL) {
879 connman_ipconfig_unref(device->ipconfig);
880 device->ipconfig = NULL;
883 g_free(device->last_network);
885 g_hash_table_destroy(device->networks);
886 device->networks = NULL;
890 * connman_device_create:
891 * @node: device node name (for example an address)
894 * Allocate a new device of given #type and assign the #node name to it.
896 * Returns: a newly-allocated #connman_device structure
898 struct connman_device *connman_device_create(const char *node,
899 enum connman_device_type type)
901 struct connman_device *device;
904 DBG("node %s type %d", node, type);
906 device = g_try_new0(struct connman_device, 1);
910 DBG("device %p", device);
912 __connman_element_initialize(&device->element);
914 device->element.name = g_strdup(node);
915 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
917 device->element.device = device;
918 device->element.destruct = device_destruct;
920 str = type2string(type);
922 connman_element_set_string(&device->element,
923 CONNMAN_PROPERTY_ID_TYPE, str);
925 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
928 device->name = g_strdup(type2description(device->type));
929 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
930 device->secondary = FALSE;
932 device->powered_persistent = TRUE;
934 device->phyindex = -1;
937 case CONNMAN_DEVICE_TYPE_UNKNOWN:
938 case CONNMAN_DEVICE_TYPE_VENDOR:
939 device->scan_interval = 0;
941 case CONNMAN_DEVICE_TYPE_ETHERNET:
942 case CONNMAN_DEVICE_TYPE_WIFI:
943 device->scan_interval = 300;
945 case CONNMAN_DEVICE_TYPE_WIMAX:
946 device->scan_interval = 0;
948 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
949 device->scan_interval = 0;
951 case CONNMAN_DEVICE_TYPE_GPS:
952 device->scan_interval = 0;
954 case CONNMAN_DEVICE_TYPE_MBM:
955 case CONNMAN_DEVICE_TYPE_HSO:
956 case CONNMAN_DEVICE_TYPE_NOZOMI:
957 case CONNMAN_DEVICE_TYPE_HUAWEI:
958 case CONNMAN_DEVICE_TYPE_NOVATEL:
959 device->scan_interval = 0;
963 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
964 g_free, unregister_network);
970 * connman_device_ref:
971 * @device: device structure
973 * Increase reference counter of device
975 struct connman_device *connman_device_ref(struct connman_device *device)
977 if (connman_element_ref(&device->element) == NULL)
984 * connman_device_unref:
985 * @device: device structure
987 * Decrease reference counter of device
989 void connman_device_unref(struct connman_device *device)
991 connman_element_unref(&device->element);
994 const char *__connman_device_get_type(struct connman_device *device)
996 return type2string(device->type);
1000 * connman_device_get_type:
1001 * @device: device structure
1003 * Get type of device
1005 enum connman_device_type connman_device_get_type(struct connman_device *device)
1007 return device->type;
1011 * connman_device_get_name:
1012 * @device: device structure
1014 * Get unique name of device
1016 const char *connman_device_get_name(struct connman_device *device)
1018 return device->element.name;
1022 * connman_device_get_path:
1023 * @device: device structure
1025 * Get path name of device
1027 const char *connman_device_get_path(struct connman_device *device)
1029 return device->element.path;
1033 * connman_device_set_index:
1034 * @device: device structure
1035 * @index: index number
1037 * Set index number of device
1039 void connman_device_set_index(struct connman_device *device, int index)
1041 device->element.index = index;
1043 if (device->ipconfig != NULL) {
1044 if (index == connman_ipconfig_get_index(device->ipconfig))
1047 connman_ipconfig_unref(device->ipconfig);
1050 device->ipconfig = connman_ipconfig_create(index);
1054 * connman_device_get_index:
1055 * @device: device structure
1057 * Get index number of device
1059 int connman_device_get_index(struct connman_device *device)
1061 return device->element.index;
1064 int __connman_device_get_phyindex(struct connman_device *device)
1066 return device->phyindex;
1069 void __connman_device_set_phyindex(struct connman_device *device,
1072 device->phyindex = phyindex;
1076 * connman_device_set_interface:
1077 * @device: device structure
1078 * @interface: interface name
1079 * @control: control interface
1081 * Set interface name of device
1083 void connman_device_set_interface(struct connman_device *device,
1084 const char *interface, const char *control)
1086 g_free(device->element.devname);
1087 device->element.devname = g_strdup(interface);
1089 g_free(device->interface);
1090 device->interface = g_strdup(interface);
1092 g_free(device->control);
1093 device->control = g_strdup(control);
1095 if (device->name == NULL) {
1096 const char *str = type2description(device->type);
1097 if (str != NULL && device->interface != NULL)
1098 device->name = g_strdup_printf("%s (%s)", str,
1103 const char *connman_device_get_control(struct connman_device *device)
1105 return device->control;
1109 * connman_device_set_ident:
1110 * @device: device structure
1111 * @ident: unique identifier
1113 * Set unique identifier of device
1115 void connman_device_set_ident(struct connman_device *device,
1118 g_free(device->ident);
1119 device->ident = g_strdup(ident);
1122 const char *__connman_device_get_ident(struct connman_device *device)
1124 return device->ident;
1128 * connman_device_set_mode:
1129 * @device: device structure
1130 * @mode: network mode
1132 * Change network mode of device
1134 void connman_device_set_mode(struct connman_device *device,
1135 enum connman_device_mode mode)
1137 device->mode = mode;
1141 * connman_device_get_mode:
1142 * @device: device structure
1144 * Get network mode of device
1146 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1148 return device->mode;
1152 * connman_device_set_secondary:
1153 * @device: device structure
1154 * @secondary: secondary value
1156 * Change secondary value of device
1158 void connman_device_set_secondary(struct connman_device *device,
1159 connman_bool_t secondary)
1161 device->secondary = secondary;
1165 * connman_device_get_secondary:
1166 * @device: device structure
1168 * Get secondary value of device
1170 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1172 return device->secondary;
1176 * connman_device_set_powered:
1177 * @device: device structure
1178 * @powered: powered state
1180 * Change power state of device
1182 int connman_device_set_powered(struct connman_device *device,
1183 connman_bool_t powered)
1185 enum connman_service_type type;
1187 DBG("driver %p powered %d", device, powered);
1189 if (device->timeout > 0) {
1190 g_source_remove(device->timeout);
1191 device->timeout = 0;
1194 if (device->pending != NULL) {
1195 g_dbus_send_reply(connection, device->pending,
1198 dbus_message_unref(device->pending);
1199 device->pending = NULL;
1202 if (device->powered == powered)
1205 device->powered = powered;
1206 device->powered_pending = powered;
1208 type = __connman_device_get_service_type(device);
1210 if (device->powered == TRUE)
1211 __connman_notifier_enable(type);
1213 __connman_notifier_disable(type);
1215 if (device->registered == FALSE)
1218 powered_changed(device);
1220 if (powered == FALSE)
1223 reset_scan_trigger(device);
1225 if (device->driver->scan)
1226 device->driver->scan(device);
1231 int __connman_device_set_blocked(struct connman_device *device,
1232 connman_bool_t blocked)
1234 connman_bool_t powered;
1236 DBG("device %p blocked %d", device, blocked);
1238 device->blocked = blocked;
1240 if (device->offlinemode == TRUE)
1243 if (blocked == FALSE)
1244 powered = device->powered_persistent;
1248 return set_powered(device, powered);
1252 * connman_device_set_carrier:
1253 * @device: device structure
1254 * @carrier: carrier state
1256 * Change carrier state of device (only for device without scanning)
1258 int connman_device_set_carrier(struct connman_device *device,
1259 connman_bool_t carrier)
1261 DBG("device %p carrier %d", device, carrier);
1263 switch (device->mode) {
1264 case CONNMAN_DEVICE_MODE_UNKNOWN:
1265 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1266 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1268 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1272 if (device->carrier == carrier)
1275 device->carrier = carrier;
1277 return set_carrier(device, device->carrier);
1280 int __connman_device_scan(struct connman_device *device)
1282 if (!device->driver || !device->driver->scan)
1285 if (device->powered == FALSE)
1288 reset_scan_trigger(device);
1290 return device->driver->scan(device);
1293 int __connman_device_enable(struct connman_device *device)
1295 enum connman_service_type type;
1298 DBG("device %p", device);
1300 if (!device->driver || !device->driver->enable)
1303 if (device->powered_pending == TRUE)
1306 device->powered_pending = TRUE;
1308 err = device->driver->enable(device);
1312 device->powered = TRUE;
1314 type = __connman_device_get_service_type(device);
1315 __connman_notifier_enable(type);
1320 int __connman_device_enable_persistent(struct connman_device *device)
1322 DBG("device %p", device);
1324 device->powered_persistent = TRUE;
1326 __connman_storage_save_device(device);
1328 return __connman_device_enable(device);
1331 int __connman_device_disable(struct connman_device *device)
1333 enum connman_service_type type;
1336 DBG("device %p", device);
1338 if (!device->driver || !device->driver->disable)
1341 if (device->powered == FALSE)
1344 if (device->powered_pending == FALSE)
1347 device->powered_pending = FALSE;
1349 clear_scan_trigger(device);
1351 g_hash_table_remove_all(device->networks);
1353 err = device->driver->disable(device);
1357 device->powered = FALSE;
1359 type = __connman_device_get_service_type(device);
1360 __connman_notifier_disable(type);
1365 int __connman_device_disable_persistent(struct connman_device *device)
1367 DBG("device %p", device);
1369 device->powered_persistent = FALSE;
1371 __connman_storage_save_device(device);
1373 return __connman_device_disable(device);
1376 int __connman_device_connect(struct connman_device *device)
1378 DBG("device %p", device);
1380 if (device->disconnected == FALSE)
1383 if (device->driver && device->driver->connect)
1384 device->driver->connect(device);
1389 int __connman_device_disconnect(struct connman_device *device)
1391 GHashTableIter iter;
1392 gpointer key, value;
1394 DBG("device %p", device);
1396 connman_device_set_disconnected(device, TRUE);
1398 g_hash_table_iter_init(&iter, device->networks);
1400 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1401 struct connman_network *network = value;
1403 __connman_network_disconnect(network);
1406 if (device->driver && device->driver->disconnect)
1407 device->driver->disconnect(device);
1412 static void mark_network_unavailable(gpointer key, gpointer value,
1415 struct connman_network *network = value;
1417 if (connman_network_get_connected(network) == TRUE)
1420 connman_network_set_available(network, FALSE);
1423 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1426 struct connman_network *network = value;
1428 if (connman_network_get_connected(network) == TRUE)
1431 if (connman_network_get_available(network) == TRUE)
1437 void __connman_device_cleanup_networks(struct connman_device *device)
1439 g_hash_table_foreach_remove(device->networks,
1440 remove_unavailable_network, NULL);
1444 * connman_device_set_scanning:
1445 * @device: device structure
1446 * @scanning: scanning state
1448 * Change scanning state of device
1450 int connman_device_set_scanning(struct connman_device *device,
1451 connman_bool_t scanning)
1453 DBusMessage *signal;
1454 DBusMessageIter entry, value;
1455 const char *key = "Scanning";
1457 DBG("device %p scanning %d", device, scanning);
1459 if (!device->driver || !device->driver->scan)
1462 if (device->scanning == scanning)
1465 device->scanning = scanning;
1467 signal = dbus_message_new_signal(device->element.path,
1468 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1472 dbus_message_iter_init_append(signal, &entry);
1474 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1476 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1477 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1478 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1479 dbus_message_iter_close_container(&entry, &value);
1481 g_dbus_send_message(connection, signal);
1483 if (scanning == TRUE) {
1484 reset_scan_trigger(device);
1486 g_hash_table_foreach(device->networks,
1487 mark_network_unavailable, NULL);
1492 __connman_device_cleanup_networks(device);
1494 if (device->connections > 0)
1497 if (device->disconnected == TRUE)
1500 __connman_service_auto_connect();
1506 * connman_device_set_disconnected:
1507 * @device: device structure
1508 * @disconnected: disconnected state
1510 * Change disconnected state of device (only for device with networks)
1512 int connman_device_set_disconnected(struct connman_device *device,
1513 connman_bool_t disconnected)
1515 DBG("device %p disconnected %d", device, disconnected);
1517 switch (device->mode) {
1518 case CONNMAN_DEVICE_MODE_UNKNOWN:
1519 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1521 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1522 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1526 if (device->disconnected == disconnected)
1529 device->disconnected = disconnected;
1531 if (disconnected == TRUE)
1532 force_scan_trigger(device);
1538 * connman_device_get_disconnected:
1539 * @device: device structure
1541 * Get device disconnected state
1543 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1545 return device->disconnected;
1549 * connman_device_set_connected:
1550 * @device: device structure
1551 * @connected: connected state
1553 * Change connected state of device (for Ethernet like devices)
1555 int connman_device_set_connected(struct connman_device *device,
1556 connman_bool_t connected)
1558 DBG("device %p connected %d", device, connected);
1560 switch (device->mode) {
1561 case CONNMAN_DEVICE_MODE_UNKNOWN:
1562 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1563 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1565 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1569 if (device->carrier == FALSE)
1572 return set_connected(device, connected);
1576 * connman_device_set_string:
1577 * @device: device structure
1578 * @key: unique identifier
1579 * @value: string value
1581 * Set string value for specific key
1583 int connman_device_set_string(struct connman_device *device,
1584 const char *key, const char *value)
1586 DBG("device %p key %s value %s", device, key, value);
1588 if (g_str_equal(key, "Address") == TRUE) {
1589 g_free(device->address);
1590 device->address = g_strdup(value);
1591 } else if (g_str_equal(key, "Name") == TRUE) {
1592 g_free(device->name);
1593 device->name = g_strdup(value);
1594 } else if (g_str_equal(key, "Node") == TRUE) {
1595 g_free(device->node);
1596 device->node = g_strdup(value);
1599 return connman_element_set_string(&device->element, key, value);
1603 * connman_device_get_string:
1604 * @device: device structure
1605 * @key: unique identifier
1607 * Get string value for specific key
1609 const char *connman_device_get_string(struct connman_device *device,
1612 DBG("device %p key %s", device, key);
1614 if (g_str_equal(key, "Address") == TRUE)
1615 return device->address;
1616 else if (g_str_equal(key, "Name") == TRUE)
1617 return device->name;
1618 else if (g_str_equal(key, "Node") == TRUE)
1619 return device->node;
1621 return connman_element_get_string(&device->element, key);
1624 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1626 struct connman_device *device = element->device;
1627 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1628 connman_bool_t powered;
1630 DBG("element %p name %s", element, element->name);
1635 device->offlinemode = offlinemode;
1637 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1639 if (device->powered == powered)
1642 if (device->powered_persistent == FALSE)
1645 set_powered(device, powered);
1648 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1650 DBG("offlinmode %d", offlinemode);
1652 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1653 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1655 __connman_notifier_offlinemode(offlinemode);
1660 void __connman_device_increase_connections(struct connman_device *device)
1662 device->connections++;
1665 void __connman_device_decrease_connections(struct connman_device *device)
1667 device->connections--;
1671 * connman_device_add_network:
1672 * @device: device structure
1673 * @network: network structure
1675 * Add new network to the device
1677 int connman_device_add_network(struct connman_device *device,
1678 struct connman_network *network)
1680 const char *identifier = connman_network_get_identifier(network);
1683 DBG("device %p network %p", device, network);
1685 switch (device->mode) {
1686 case CONNMAN_DEVICE_MODE_UNKNOWN:
1687 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1689 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1690 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1694 __connman_network_set_device(network, device);
1696 err = connman_element_register((struct connman_element *) network,
1699 __connman_network_set_device(network, NULL);
1703 g_hash_table_insert(device->networks, g_strdup(identifier),
1710 * connman_device_get_network:
1711 * @device: device structure
1712 * @identifier: network identifier
1714 * Get network for given identifier
1716 struct connman_network *connman_device_get_network(struct connman_device *device,
1717 const char *identifier)
1719 DBG("device %p identifier %s", device, identifier);
1721 return g_hash_table_lookup(device->networks, identifier);
1725 * connman_device_remove_network:
1726 * @device: device structure
1727 * @identifier: network identifier
1729 * Remove network for given identifier
1731 int connman_device_remove_network(struct connman_device *device,
1732 const char *identifier)
1734 DBG("device %p identifier %s", device, identifier);
1736 g_hash_table_remove(device->networks, identifier);
1741 void __connman_device_set_network(struct connman_device *device,
1742 struct connman_network *network)
1746 if (device->network == network)
1749 if (device->network != NULL)
1750 connman_network_unref(device->network);
1752 if (network != NULL) {
1753 name = connman_network_get_string(network,
1754 CONNMAN_PROPERTY_ID_NAME);
1755 g_free(device->last_network);
1756 device->last_network = g_strdup(name);
1758 device->network = connman_network_ref(network);
1760 g_free(device->last_network);
1761 device->last_network = NULL;
1763 device->network = NULL;
1768 * connman_device_register:
1769 * @device: device structure
1771 * Register device with the system
1773 int connman_device_register(struct connman_device *device)
1775 __connman_storage_load_device(device);
1777 device->offlinemode = __connman_profile_get_offlinemode();
1779 return connman_element_register(&device->element, NULL);
1783 * connman_device_unregister:
1784 * @device: device structure
1786 * Unregister device with the system
1788 void connman_device_unregister(struct connman_device *device)
1790 __connman_storage_save_device(device);
1792 connman_element_unregister(&device->element);
1796 * connman_device_get_data:
1797 * @device: device structure
1799 * Get private device data pointer
1801 void *connman_device_get_data(struct connman_device *device)
1803 return device->driver_data;
1807 * connman_device_set_data:
1808 * @device: device structure
1809 * @data: data pointer
1811 * Set private device data pointer
1813 void connman_device_set_data(struct connman_device *device, void *data)
1815 device->driver_data = data;
1818 static gboolean match_driver(struct connman_device *device,
1819 struct connman_device_driver *driver)
1821 if (device->type == driver->type ||
1822 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1828 static int device_probe(struct connman_element *element)
1830 struct connman_device *device = element->device;
1833 DBG("element %p name %s", element, element->name);
1838 if (device->driver != NULL)
1841 for (list = driver_list; list; list = list->next) {
1842 struct connman_device_driver *driver = list->data;
1844 if (match_driver(device, driver) == FALSE)
1847 DBG("driver %p name %s", driver, driver->name);
1849 if (driver->probe(device) == 0) {
1850 device->driver = driver;
1855 if (device->driver == NULL)
1858 return setup_device(device);
1861 static void device_remove(struct connman_element *element)
1863 struct connman_device *device = element->device;
1865 DBG("element %p name %s", element, element->name);
1870 if (device->driver == NULL)
1873 remove_device(device);
1876 static struct connman_driver device_driver = {
1878 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1879 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1880 .probe = device_probe,
1881 .remove = device_remove,
1884 static int device_load(struct connman_device *device)
1886 const char *ident = __connman_profile_active_ident();
1888 GError *error = NULL;
1890 connman_bool_t powered;
1893 DBG("device %p", device);
1895 keyfile = __connman_storage_open(ident);
1896 if (keyfile == NULL)
1899 identifier = g_strdup_printf("device_%s", device->element.name);
1900 if (identifier == NULL)
1903 powered = g_key_file_get_boolean(keyfile, identifier,
1906 device->powered_persistent = powered;
1907 g_clear_error(&error);
1909 switch (device->mode) {
1910 case CONNMAN_DEVICE_MODE_UNKNOWN:
1911 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1913 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1914 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1915 val = g_key_file_get_integer(keyfile, identifier,
1916 "ScanInterval", &error);
1917 if (error == NULL && val > 0)
1918 device->scan_interval = val;
1919 g_clear_error(&error);
1926 __connman_storage_close(ident, keyfile, FALSE);
1931 static int device_save(struct connman_device *device)
1933 const char *ident = __connman_profile_active_ident();
1937 DBG("device %p", device);
1939 keyfile = __connman_storage_open(ident);
1940 if (keyfile == NULL)
1943 identifier = g_strdup_printf("device_%s", device->element.name);
1944 if (identifier == NULL)
1947 g_key_file_set_boolean(keyfile, identifier,
1948 "Powered", device->powered_persistent);
1950 switch (device->mode) {
1951 case CONNMAN_DEVICE_MODE_UNKNOWN:
1952 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1954 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1955 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1956 if (device->scan_interval > 0)
1957 g_key_file_set_integer(keyfile, identifier,
1958 "ScanInterval", device->scan_interval);
1965 __connman_storage_close(ident, keyfile, TRUE);
1970 static struct connman_storage device_storage = {
1972 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1973 .device_load = device_load,
1974 .device_save = device_save,
1977 int __connman_device_init(void)
1981 connection = connman_dbus_get_connection();
1983 if (connman_storage_register(&device_storage) < 0)
1984 connman_error("Failed to register device storage");
1986 return connman_driver_register(&device_driver);
1989 void __connman_device_cleanup(void)
1993 connman_driver_unregister(&device_driver);
1995 connman_storage_unregister(&device_storage);
1997 dbus_connection_unref(connection);