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 const char *type2description(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_MBM:
126 case CONNMAN_DEVICE_TYPE_HSO:
127 case CONNMAN_DEVICE_TYPE_NOZOMI:
128 case CONNMAN_DEVICE_TYPE_HUAWEI:
129 case CONNMAN_DEVICE_TYPE_NOVATEL:
136 static const char *type2string(enum connman_device_type type)
139 case CONNMAN_DEVICE_TYPE_UNKNOWN:
140 case CONNMAN_DEVICE_TYPE_VENDOR:
142 case CONNMAN_DEVICE_TYPE_ETHERNET:
144 case CONNMAN_DEVICE_TYPE_WIFI:
146 case CONNMAN_DEVICE_TYPE_WIMAX:
148 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
150 case CONNMAN_DEVICE_TYPE_GPS:
152 case CONNMAN_DEVICE_TYPE_MBM:
153 case CONNMAN_DEVICE_TYPE_HSO:
154 case CONNMAN_DEVICE_TYPE_HUAWEI:
155 case CONNMAN_DEVICE_TYPE_NOZOMI:
156 case CONNMAN_DEVICE_TYPE_NOVATEL:
163 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
165 enum connman_device_type type = connman_device_get_type(device);
168 case CONNMAN_DEVICE_TYPE_UNKNOWN:
169 case CONNMAN_DEVICE_TYPE_VENDOR:
170 case CONNMAN_DEVICE_TYPE_GPS:
171 case CONNMAN_DEVICE_TYPE_NOZOMI:
172 case CONNMAN_DEVICE_TYPE_HUAWEI:
173 case CONNMAN_DEVICE_TYPE_NOVATEL:
175 case CONNMAN_DEVICE_TYPE_ETHERNET:
176 return CONNMAN_SERVICE_TYPE_ETHERNET;
177 case CONNMAN_DEVICE_TYPE_WIFI:
178 return CONNMAN_SERVICE_TYPE_WIFI;
179 case CONNMAN_DEVICE_TYPE_WIMAX:
180 return CONNMAN_SERVICE_TYPE_WIMAX;
181 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
182 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
183 case CONNMAN_DEVICE_TYPE_MBM:
184 case CONNMAN_DEVICE_TYPE_HSO:
185 return CONNMAN_SERVICE_TYPE_CELLULAR;
188 return CONNMAN_SERVICE_TYPE_UNKNOWN;
191 static int set_connected(struct connman_device *device,
192 connman_bool_t connected)
194 if (connected == TRUE) {
195 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
196 struct connman_element *element;
198 device->disconnected = TRUE;
200 switch (device->element.ipv4.method) {
201 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
202 case CONNMAN_IPCONFIG_METHOD_OFF:
204 case CONNMAN_IPCONFIG_METHOD_STATIC:
205 type = CONNMAN_ELEMENT_TYPE_IPV4;
207 case CONNMAN_IPCONFIG_METHOD_DHCP:
208 type = CONNMAN_ELEMENT_TYPE_DHCP;
212 element = connman_element_create(NULL);
213 if (element != NULL) {
214 struct connman_service *service;
216 element->type = type;
217 element->index = device->element.index;
219 if (connman_element_register(element,
220 &device->element) < 0)
221 connman_element_unref(element);
223 device->disconnected = FALSE;
225 service = __connman_service_lookup_from_device(device);
226 __connman_service_indicate_state(service,
227 CONNMAN_SERVICE_STATE_CONFIGURATION);
230 struct connman_service *service;
232 connman_element_unregister_children(&device->element);
234 device->disconnected = TRUE;
236 service = __connman_service_lookup_from_device(device);
237 __connman_service_indicate_state(service,
238 CONNMAN_SERVICE_STATE_IDLE);
241 if (connected == TRUE) {
242 enum connman_service_type type;
244 type = __connman_device_get_service_type(device);
245 __connman_notifier_connect(type);
247 enum connman_service_type type;
249 type = __connman_device_get_service_type(device);
250 __connman_notifier_disconnect(type);
256 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
259 __connman_profile_add_device(device);
261 __connman_profile_remove_device(device);
263 return set_connected(device, carrier);
266 static int powered_changed(struct connman_device *device)
269 DBusMessageIter entry, value;
270 const char *key = "Powered";
272 signal = dbus_message_new_signal(device->element.path,
273 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
277 dbus_message_iter_init_append(signal, &entry);
279 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
281 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
282 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
283 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
285 dbus_message_iter_close_container(&entry, &value);
287 g_dbus_send_message(connection, signal);
292 static int set_powered(struct connman_device *device, connman_bool_t powered)
294 struct connman_device_driver *driver = device->driver;
295 enum connman_service_type type;
298 DBG("device %p powered %d", device, powered);
300 if (device->powered_pending == powered)
303 device->powered_pending = powered;
308 type = __connman_device_get_service_type(device);
310 if (powered == TRUE) {
311 if (driver->enable) {
312 err = driver->enable(device);
314 __connman_notifier_enable(type);
318 clear_scan_trigger(device);
320 g_hash_table_remove_all(device->networks);
322 set_carrier(device, FALSE);
324 if (driver->disable) {
325 err = driver->disable(device);
327 __connman_notifier_disable(type);
333 device->powered = powered;
334 powered_changed(device);
340 static void append_path(gpointer key, gpointer value, gpointer user_data)
342 struct connman_element *element = value;
343 DBusMessageIter *iter = user_data;
345 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
349 static void append_networks(struct connman_device *device,
350 DBusMessageIter *entry)
352 DBusMessageIter value, iter;
353 const char *key = "Networks";
355 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
357 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
358 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
361 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
362 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
363 g_hash_table_foreach(device->networks, append_path, &iter);
364 dbus_message_iter_close_container(&value, &iter);
366 dbus_message_iter_close_container(entry, &value);
369 static DBusMessage *get_properties(DBusConnection *conn,
370 DBusMessage *msg, void *data)
372 struct connman_device *device = data;
374 DBusMessageIter array, dict, entry;
377 DBG("conn %p", conn);
379 if (__connman_security_check_privilege(msg,
380 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
381 return __connman_error_permission_denied(msg);
383 reply = dbus_message_new_method_return(msg);
387 dbus_message_iter_init_append(reply, &array);
389 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
390 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
391 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
392 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
394 if (device->name != NULL)
395 connman_dbus_dict_append_variant(&dict, "Name",
396 DBUS_TYPE_STRING, &device->name);
398 str = type2string(device->type);
400 connman_dbus_dict_append_variant(&dict, "Type",
401 DBUS_TYPE_STRING, &str);
403 if (device->address != NULL)
404 connman_dbus_dict_append_variant(&dict, "Address",
405 DBUS_TYPE_STRING, &device->address);
407 if (device->interface != NULL)
408 connman_dbus_dict_append_variant(&dict, "Interface",
409 DBUS_TYPE_STRING, &device->interface);
411 connman_dbus_dict_append_variant(&dict, "Powered",
412 DBUS_TYPE_BOOLEAN, &device->powered);
414 if (device->driver && device->driver->scan)
415 connman_dbus_dict_append_variant(&dict, "Scanning",
416 DBUS_TYPE_BOOLEAN, &device->scanning);
418 switch (device->mode) {
419 case CONNMAN_DEVICE_MODE_UNKNOWN:
421 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
422 __connman_element_append_ipv4(&device->element, &dict);
424 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
425 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
426 if (device->scan_interval > 0)
427 connman_dbus_dict_append_variant(&dict, "ScanInterval",
428 DBUS_TYPE_UINT16, &device->scan_interval);
430 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
432 append_networks(device, &entry);
433 dbus_message_iter_close_container(&dict, &entry);
437 dbus_message_iter_close_container(&array, &dict);
442 static gboolean powered_timeout(gpointer user_data)
444 struct connman_device *device = user_data;
446 DBG("device %p", device);
450 if (device->pending != NULL) {
453 reply = __connman_error_operation_timeout(device->pending);
455 g_dbus_send_message(connection, reply);
457 dbus_message_unref(device->pending);
458 device->pending = NULL;
464 static DBusMessage *set_property(DBusConnection *conn,
465 DBusMessage *msg, void *data)
467 struct connman_device *device = data;
468 DBusMessageIter iter, value;
472 DBG("conn %p", conn);
474 if (dbus_message_iter_init(msg, &iter) == FALSE)
475 return __connman_error_invalid_arguments(msg);
477 dbus_message_iter_get_basic(&iter, &name);
478 dbus_message_iter_next(&iter);
479 dbus_message_iter_recurse(&iter, &value);
481 if (__connman_security_check_privilege(msg,
482 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
483 return __connman_error_permission_denied(msg);
485 type = dbus_message_iter_get_arg_type(&value);
487 if (g_str_equal(name, "Powered") == TRUE) {
488 connman_bool_t powered;
491 if (type != DBUS_TYPE_BOOLEAN)
492 return __connman_error_invalid_arguments(msg);
494 dbus_message_iter_get_basic(&value, &powered);
496 device->powered_persistent = powered;
498 __connman_storage_save_device(device);
500 if (device->powered == powered)
501 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
503 if (device->pending != NULL)
504 return __connman_error_in_progress(msg);
506 err = set_powered(device, powered);
508 if (err != -EINPROGRESS)
509 return __connman_error_failed(msg, -err);
511 device->pending = dbus_message_ref(msg);
513 device->timeout = g_timeout_add_seconds(15,
514 powered_timeout, device);
518 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
519 connman_uint16_t interval;
521 switch (device->mode) {
522 case CONNMAN_DEVICE_MODE_UNKNOWN:
523 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
524 return __connman_error_invalid_arguments(msg);
525 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
526 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
530 if (type != DBUS_TYPE_UINT16)
531 return __connman_error_invalid_arguments(msg);
533 dbus_message_iter_get_basic(&value, &interval);
535 if (device->scan_interval != interval) {
536 device->scan_interval = interval;
538 __connman_storage_save_device(device);
540 reset_scan_trigger(device);
542 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
545 switch (device->mode) {
546 case CONNMAN_DEVICE_MODE_UNKNOWN:
547 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
548 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
549 return __connman_error_invalid_arguments(msg);
550 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
554 err = __connman_ipconfig_set_ipv4(device->ipconfig,
557 return __connman_error_failed(msg, -err);
559 return __connman_error_invalid_property(msg);
561 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
564 static DBusMessage *propose_scan(DBusConnection *conn,
565 DBusMessage *msg, void *data)
567 struct connman_device *device = data;
570 DBG("conn %p", conn);
572 switch (device->mode) {
573 case CONNMAN_DEVICE_MODE_UNKNOWN:
574 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
575 return __connman_error_not_supported(msg);
576 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
577 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
581 err = __connman_device_scan(device);
583 return __connman_error_failed(msg, -err);
585 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
588 static GDBusMethodTable device_methods[] = {
589 { "GetProperties", "", "a{sv}", get_properties },
590 { "SetProperty", "sv", "", set_property,
591 G_DBUS_METHOD_FLAG_ASYNC },
592 { "ProposeScan", "", "", propose_scan },
596 static GDBusSignalTable device_signals[] = {
597 { "PropertyChanged", "sv" },
601 static void append_devices(DBusMessageIter *entry)
603 DBusMessageIter value, iter;
604 const char *key = "Devices";
606 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
608 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
609 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
612 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
613 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
614 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
615 dbus_message_iter_close_container(&value, &iter);
617 dbus_message_iter_close_container(entry, &value);
620 static void emit_devices_signal(void)
623 DBusMessageIter entry;
625 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
626 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
630 dbus_message_iter_init_append(signal, &entry);
632 append_devices(&entry);
634 g_dbus_send_message(connection, signal);
637 static int register_interface(struct connman_element *element)
639 struct connman_device *device = element->device;
641 DBG("element %p name %s", element, element->name);
643 if (g_dbus_register_interface(connection, element->path,
644 CONNMAN_DEVICE_INTERFACE,
645 device_methods, device_signals,
646 NULL, device, NULL) == FALSE) {
647 connman_error("Failed to register %s device", element->path);
651 device->registered = TRUE;
653 emit_devices_signal();
658 static void unregister_interface(struct connman_element *element)
660 struct connman_device *device = element->device;
662 DBG("element %p name %s", element, element->name);
664 device->registered = FALSE;
666 emit_devices_signal();
668 g_dbus_unregister_interface(connection, element->path,
669 CONNMAN_DEVICE_INTERFACE);
672 static int setup_device(struct connman_device *device)
676 DBG("device %p", device);
678 err = register_interface(&device->element);
680 if (device->driver->remove)
681 device->driver->remove(device);
682 device->driver = NULL;
686 switch (device->mode) {
687 case CONNMAN_DEVICE_MODE_UNKNOWN:
688 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
689 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
691 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
692 if (device->carrier == TRUE && device->secondary == FALSE)
693 __connman_profile_add_device(device);
697 if (device->offlinemode == FALSE &&
698 device->powered_persistent == TRUE)
699 __connman_device_enable(device);
704 static void probe_driver(struct connman_element *element, gpointer user_data)
706 struct connman_device_driver *driver = user_data;
708 DBG("element %p name %s", element, element->name);
710 if (element->device == NULL)
713 if (element->device->driver != NULL)
716 if (driver->type != element->device->type)
719 if (driver->probe(element->device) < 0)
722 element->device->driver = driver;
724 setup_device(element->device);
727 static void remove_device(struct connman_device *device)
729 DBG("device %p", device);
731 __connman_device_disable(device);
733 switch (device->mode) {
734 case CONNMAN_DEVICE_MODE_UNKNOWN:
735 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
736 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
738 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
739 if (device->secondary == FALSE)
740 __connman_profile_remove_device(device);
744 unregister_interface(&device->element);
746 if (device->driver->remove)
747 device->driver->remove(device);
749 device->driver = NULL;
752 static void remove_driver(struct connman_element *element, gpointer user_data)
754 struct connman_device_driver *driver = user_data;
756 DBG("element %p name %s", element, element->name);
758 if (element->device == NULL)
761 if (element->device->driver == driver)
762 remove_device(element->device);
765 connman_bool_t __connman_device_has_driver(struct connman_device *device)
767 if (device == NULL || device->driver == NULL)
770 return device->registered;
773 static GSList *driver_list = NULL;
775 static gint compare_priority(gconstpointer a, gconstpointer b)
777 const struct connman_device_driver *driver1 = a;
778 const struct connman_device_driver *driver2 = b;
780 return driver2->priority - driver1->priority;
784 * connman_device_driver_register:
785 * @driver: device driver definition
787 * Register a new device driver
789 * Returns: %0 on success
791 int connman_device_driver_register(struct connman_device_driver *driver)
793 DBG("driver %p name %s", driver, driver->name);
795 driver_list = g_slist_insert_sorted(driver_list, driver,
798 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
799 probe_driver, driver);
805 * connman_device_driver_unregister:
806 * @driver: device driver definition
808 * Remove a previously registered device driver
810 void connman_device_driver_unregister(struct connman_device_driver *driver)
812 DBG("driver %p name %s", driver, driver->name);
814 driver_list = g_slist_remove(driver_list, driver);
816 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
817 remove_driver, driver);
820 static void unregister_network(gpointer data)
822 struct connman_network *network = data;
824 DBG("network %p", network);
826 connman_element_unregister((struct connman_element *) network);
828 connman_network_unref(network);
831 static void device_destruct(struct connman_element *element)
833 struct connman_device *device = element->device;
835 DBG("element %p name %s", element, element->name);
837 if (device->timeout > 0) {
838 g_source_remove(device->timeout);
842 if (device->pending != NULL) {
843 dbus_message_unref(device->pending);
844 device->pending = NULL;
847 g_free(device->ident);
848 g_free(device->node);
849 g_free(device->name);
850 g_free(device->address);
851 g_free(device->control);
852 g_free(device->interface);
854 connman_ipconfig_unref(device->ipconfig);
856 g_free(device->last_network);
858 g_hash_table_destroy(device->networks);
859 device->networks = NULL;
863 * connman_device_create:
864 * @node: device node name (for example an address)
867 * Allocate a new device of given #type and assign the #node name to it.
869 * Returns: a newly-allocated #connman_device structure
871 struct connman_device *connman_device_create(const char *node,
872 enum connman_device_type type)
874 struct connman_device *device;
877 DBG("node %s type %d", node, type);
879 device = g_try_new0(struct connman_device, 1);
883 DBG("device %p", device);
885 __connman_element_initialize(&device->element);
887 device->element.name = g_strdup(node);
888 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
890 device->element.device = device;
891 device->element.destruct = device_destruct;
893 str = type2string(type);
895 connman_element_set_string(&device->element,
896 CONNMAN_PROPERTY_ID_TYPE, str);
898 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
901 device->name = g_strdup(type2description(device->type));
902 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
903 device->secondary = FALSE;
905 device->powered_persistent = TRUE;
907 device->phyindex = -1;
910 case CONNMAN_DEVICE_TYPE_UNKNOWN:
911 case CONNMAN_DEVICE_TYPE_VENDOR:
912 device->scan_interval = 0;
914 case CONNMAN_DEVICE_TYPE_ETHERNET:
915 case CONNMAN_DEVICE_TYPE_WIFI:
916 device->scan_interval = 300;
918 case CONNMAN_DEVICE_TYPE_WIMAX:
919 device->scan_interval = 0;
921 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
922 device->scan_interval = 0;
924 case CONNMAN_DEVICE_TYPE_GPS:
925 device->scan_interval = 0;
927 case CONNMAN_DEVICE_TYPE_MBM:
928 case CONNMAN_DEVICE_TYPE_HSO:
929 case CONNMAN_DEVICE_TYPE_NOZOMI:
930 case CONNMAN_DEVICE_TYPE_HUAWEI:
931 case CONNMAN_DEVICE_TYPE_NOVATEL:
932 device->scan_interval = 0;
936 device->ipconfig = connman_ipconfig_create();
937 if (device->ipconfig == NULL) {
938 connman_device_unref(device);
942 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
943 g_free, unregister_network);
949 * connman_device_ref:
950 * @device: device structure
952 * Increase reference counter of device
954 struct connman_device *connman_device_ref(struct connman_device *device)
956 if (connman_element_ref(&device->element) == NULL)
963 * connman_device_unref:
964 * @device: device structure
966 * Decrease reference counter of device
968 void connman_device_unref(struct connman_device *device)
970 connman_element_unref(&device->element);
973 const char *__connman_device_get_type(struct connman_device *device)
975 return type2string(device->type);
979 * connman_device_get_type:
980 * @device: device structure
984 enum connman_device_type connman_device_get_type(struct connman_device *device)
990 * connman_device_get_name:
991 * @device: device structure
993 * Get unique name of device
995 const char *connman_device_get_name(struct connman_device *device)
997 return device->element.name;
1001 * connman_device_get_path:
1002 * @device: device structure
1004 * Get path name of device
1006 const char *connman_device_get_path(struct connman_device *device)
1008 return device->element.path;
1012 * connman_device_set_index:
1013 * @device: device structure
1014 * @index: index number
1016 * Set index number of device
1018 void connman_device_set_index(struct connman_device *device, int index)
1020 device->element.index = index;
1024 * connman_device_get_index:
1025 * @device: device structure
1027 * Get index number of device
1029 int connman_device_get_index(struct connman_device *device)
1031 return device->element.index;
1034 int __connman_device_get_phyindex(struct connman_device *device)
1036 return device->phyindex;
1039 void __connman_device_set_phyindex(struct connman_device *device,
1042 device->phyindex = phyindex;
1046 * connman_device_set_interface:
1047 * @device: device structure
1048 * @interface: interface name
1049 * @control: control interface
1051 * Set interface name of device
1053 void connman_device_set_interface(struct connman_device *device,
1054 const char *interface, const char *control)
1056 g_free(device->element.devname);
1057 device->element.devname = g_strdup(interface);
1059 g_free(device->interface);
1060 device->interface = g_strdup(interface);
1062 g_free(device->control);
1063 device->control = g_strdup(control);
1065 if (device->name == NULL) {
1066 const char *str = type2description(device->type);
1067 if (str != NULL && device->interface != NULL)
1068 device->name = g_strdup_printf("%s (%s)", str,
1073 const char *connman_device_get_control(struct connman_device *device)
1075 return device->control;
1079 * connman_device_set_ident:
1080 * @device: device structure
1081 * @ident: unique identifier
1083 * Set unique identifier of device
1085 void connman_device_set_ident(struct connman_device *device,
1088 g_free(device->ident);
1089 device->ident = g_strdup(ident);
1092 const char *__connman_device_get_ident(struct connman_device *device)
1094 return device->ident;
1098 * connman_device_set_mode:
1099 * @device: device structure
1100 * @mode: network mode
1102 * Change network mode of device
1104 void connman_device_set_mode(struct connman_device *device,
1105 enum connman_device_mode mode)
1107 device->mode = mode;
1111 * connman_device_get_mode:
1112 * @device: device structure
1114 * Get network mode of device
1116 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1118 return device->mode;
1122 * connman_device_set_secondary:
1123 * @device: device structure
1124 * @secondary: secondary value
1126 * Change secondary value of device
1128 void connman_device_set_secondary(struct connman_device *device,
1129 connman_bool_t secondary)
1131 device->secondary = secondary;
1135 * connman_device_get_secondary:
1136 * @device: device structure
1138 * Get secondary value of device
1140 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1142 return device->secondary;
1146 * connman_device_set_powered:
1147 * @device: device structure
1148 * @powered: powered state
1150 * Change power state of device
1152 int connman_device_set_powered(struct connman_device *device,
1153 connman_bool_t powered)
1155 enum connman_service_type type;
1157 DBG("driver %p powered %d", device, powered);
1159 if (device->timeout > 0) {
1160 g_source_remove(device->timeout);
1161 device->timeout = 0;
1164 if (device->pending != NULL) {
1165 g_dbus_send_reply(connection, device->pending,
1168 dbus_message_unref(device->pending);
1169 device->pending = NULL;
1172 if (device->powered == powered)
1175 device->powered = powered;
1176 device->powered_pending = powered;
1178 type = __connman_device_get_service_type(device);
1180 if (device->powered == TRUE)
1181 __connman_notifier_enable(type);
1183 __connman_notifier_disable(type);
1185 if (device->registered == FALSE)
1188 powered_changed(device);
1190 if (powered == FALSE)
1193 reset_scan_trigger(device);
1195 if (device->driver->scan)
1196 device->driver->scan(device);
1201 int __connman_device_set_blocked(struct connman_device *device,
1202 connman_bool_t blocked)
1204 connman_bool_t powered;
1206 DBG("device %p blocked %d", device, blocked);
1208 device->blocked = blocked;
1210 if (device->offlinemode == TRUE)
1213 if (blocked == FALSE)
1214 powered = device->powered_persistent;
1218 return set_powered(device, powered);
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("device %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_scan(struct connman_device *device)
1252 if (!device->driver || !device->driver->scan)
1255 if (device->powered == FALSE)
1258 return device->driver->scan(device);
1261 int __connman_device_enable(struct connman_device *device)
1263 enum connman_service_type type;
1266 DBG("device %p", device);
1268 if (!device->driver || !device->driver->enable)
1271 if (device->powered_pending == TRUE)
1274 device->powered_pending = TRUE;
1276 err = device->driver->enable(device);
1280 device->powered = TRUE;
1282 type = __connman_device_get_service_type(device);
1283 __connman_notifier_enable(type);
1288 int __connman_device_enable_persistent(struct connman_device *device)
1290 DBG("device %p", device);
1292 device->powered_persistent = TRUE;
1294 __connman_storage_save_device(device);
1296 return __connman_device_enable(device);
1299 int __connman_device_disable(struct connman_device *device)
1301 enum connman_service_type type;
1304 DBG("device %p", device);
1306 if (!device->driver || !device->driver->disable)
1309 if (device->powered == FALSE)
1312 if (device->powered_pending == FALSE)
1315 device->powered_pending = FALSE;
1317 clear_scan_trigger(device);
1319 g_hash_table_remove_all(device->networks);
1321 err = device->driver->disable(device);
1325 device->powered = FALSE;
1327 type = __connman_device_get_service_type(device);
1328 __connman_notifier_disable(type);
1333 int __connman_device_disable_persistent(struct connman_device *device)
1335 DBG("device %p", device);
1337 device->powered_persistent = FALSE;
1339 __connman_storage_save_device(device);
1341 return __connman_device_disable(device);
1344 int __connman_device_connect(struct connman_device *device)
1346 DBG("device %p", device);
1348 if (device->disconnected == FALSE)
1351 if (device->driver && device->driver->connect)
1352 device->driver->connect(device);
1357 int __connman_device_disconnect(struct connman_device *device)
1359 GHashTableIter iter;
1360 gpointer key, value;
1362 DBG("device %p", device);
1364 connman_device_set_disconnected(device, TRUE);
1366 g_hash_table_iter_init(&iter, device->networks);
1368 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1369 struct connman_network *network = value;
1371 __connman_network_disconnect(network);
1374 if (device->driver && device->driver->disconnect)
1375 device->driver->disconnect(device);
1380 static void mark_network_unavailable(gpointer key, gpointer value,
1383 struct connman_network *network = value;
1385 if (connman_network_get_connected(network) == TRUE)
1388 connman_network_set_available(network, FALSE);
1391 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1394 struct connman_network *network = value;
1396 if (connman_network_get_connected(network) == TRUE)
1399 if (connman_network_get_available(network) == TRUE)
1405 void __connman_device_cleanup_networks(struct connman_device *device)
1407 g_hash_table_foreach_remove(device->networks,
1408 remove_unavailable_network, NULL);
1412 * connman_device_set_scanning:
1413 * @device: device structure
1414 * @scanning: scanning state
1416 * Change scanning state of device
1418 int connman_device_set_scanning(struct connman_device *device,
1419 connman_bool_t scanning)
1421 DBusMessage *signal;
1422 DBusMessageIter entry, value;
1423 const char *key = "Scanning";
1425 DBG("device %p scanning %d", device, scanning);
1427 if (!device->driver || !device->driver->scan)
1430 if (device->scanning == scanning)
1433 device->scanning = scanning;
1435 signal = dbus_message_new_signal(device->element.path,
1436 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1440 dbus_message_iter_init_append(signal, &entry);
1442 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1444 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1445 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1446 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1447 dbus_message_iter_close_container(&entry, &value);
1449 g_dbus_send_message(connection, signal);
1451 if (scanning == TRUE) {
1452 reset_scan_trigger(device);
1454 g_hash_table_foreach(device->networks,
1455 mark_network_unavailable, NULL);
1460 __connman_device_cleanup_networks(device);
1462 if (device->connections > 0)
1465 if (device->disconnected == TRUE)
1468 __connman_service_auto_connect();
1474 * connman_device_set_disconnected:
1475 * @device: device structure
1476 * @disconnected: disconnected state
1478 * Change disconnected state of device (only for device with networks)
1480 int connman_device_set_disconnected(struct connman_device *device,
1481 connman_bool_t disconnected)
1483 DBG("device %p disconnected %d", device, disconnected);
1485 switch (device->mode) {
1486 case CONNMAN_DEVICE_MODE_UNKNOWN:
1487 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1489 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1490 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1494 if (device->disconnected == disconnected)
1497 device->disconnected = disconnected;
1503 * connman_device_get_disconnected:
1504 * @device: device structure
1506 * Get device disconnected state
1508 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1510 return device->disconnected;
1514 * connman_device_set_connected:
1515 * @device: device structure
1516 * @connected: connected state
1518 * Change connected state of device (for Ethernet like devices)
1520 int connman_device_set_connected(struct connman_device *device,
1521 connman_bool_t connected)
1523 DBG("device %p connected %d", device, connected);
1525 switch (device->mode) {
1526 case CONNMAN_DEVICE_MODE_UNKNOWN:
1527 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1528 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1530 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1534 if (device->carrier == FALSE)
1537 return set_connected(device, connected);
1541 * connman_device_set_string:
1542 * @device: device structure
1543 * @key: unique identifier
1544 * @value: string value
1546 * Set string value for specific key
1548 int connman_device_set_string(struct connman_device *device,
1549 const char *key, const char *value)
1551 DBG("device %p key %s value %s", device, key, value);
1553 if (g_str_equal(key, "Address") == TRUE) {
1554 g_free(device->address);
1555 device->address = g_strdup(value);
1556 } else if (g_str_equal(key, "Name") == TRUE) {
1557 g_free(device->name);
1558 device->name = g_strdup(value);
1559 } else if (g_str_equal(key, "Node") == TRUE) {
1560 g_free(device->node);
1561 device->node = g_strdup(value);
1564 return connman_element_set_string(&device->element, key, value);
1568 * connman_device_get_string:
1569 * @device: device structure
1570 * @key: unique identifier
1572 * Get string value for specific key
1574 const char *connman_device_get_string(struct connman_device *device,
1577 DBG("device %p key %s", device, key);
1579 if (g_str_equal(key, "Address") == TRUE)
1580 return device->address;
1581 else if (g_str_equal(key, "Name") == TRUE)
1582 return device->name;
1583 else if (g_str_equal(key, "Node") == TRUE)
1584 return device->node;
1586 return connman_element_get_string(&device->element, key);
1589 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1591 struct connman_device *device = element->device;
1592 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1593 connman_bool_t powered;
1595 DBG("element %p name %s", element, element->name);
1600 device->offlinemode = offlinemode;
1602 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1604 if (device->powered == powered)
1607 if (device->powered_persistent == FALSE)
1610 set_powered(device, powered);
1613 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1615 DBG("offlinmode %d", offlinemode);
1617 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1618 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1620 __connman_notifier_offlinemode(offlinemode);
1625 void __connman_device_increase_connections(struct connman_device *device)
1627 device->connections++;
1630 void __connman_device_decrease_connections(struct connman_device *device)
1632 device->connections--;
1636 * connman_device_add_network:
1637 * @device: device structure
1638 * @network: network structure
1640 * Add new network to the device
1642 int connman_device_add_network(struct connman_device *device,
1643 struct connman_network *network)
1645 const char *identifier = connman_network_get_identifier(network);
1648 DBG("device %p network %p", device, network);
1650 switch (device->mode) {
1651 case CONNMAN_DEVICE_MODE_UNKNOWN:
1652 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1654 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1655 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1659 __connman_network_set_device(network, device);
1661 err = connman_element_register((struct connman_element *) network,
1664 __connman_network_set_device(network, NULL);
1668 g_hash_table_insert(device->networks, g_strdup(identifier),
1675 * connman_device_get_network:
1676 * @device: device structure
1677 * @identifier: network identifier
1679 * Get network for given identifier
1681 struct connman_network *connman_device_get_network(struct connman_device *device,
1682 const char *identifier)
1684 DBG("device %p identifier %s", device, identifier);
1686 return g_hash_table_lookup(device->networks, identifier);
1690 * connman_device_remove_network:
1691 * @device: device structure
1692 * @identifier: network identifier
1694 * Remove network for given identifier
1696 int connman_device_remove_network(struct connman_device *device,
1697 const char *identifier)
1699 DBG("device %p identifier %s", device, identifier);
1701 g_hash_table_remove(device->networks, identifier);
1706 void __connman_device_set_network(struct connman_device *device,
1707 struct connman_network *network)
1711 if (device->network == network)
1714 if (device->network != NULL)
1715 connman_network_unref(device->network);
1717 if (network != NULL) {
1718 name = connman_network_get_string(network,
1719 CONNMAN_PROPERTY_ID_NAME);
1720 g_free(device->last_network);
1721 device->last_network = g_strdup(name);
1723 device->network = connman_network_ref(network);
1725 g_free(device->last_network);
1726 device->last_network = NULL;
1728 device->network = NULL;
1733 * connman_device_register:
1734 * @device: device structure
1736 * Register device with the system
1738 int connman_device_register(struct connman_device *device)
1740 enum connman_service_type type;
1742 __connman_storage_load_device(device);
1744 device->offlinemode = __connman_manager_get_offlinemode();
1746 type = __connman_device_get_service_type(device);
1747 __connman_notifier_register(type);
1749 return connman_element_register(&device->element, NULL);
1753 * connman_device_unregister:
1754 * @device: device structure
1756 * Unregister device with the system
1758 void connman_device_unregister(struct connman_device *device)
1760 enum connman_service_type type;
1762 __connman_storage_save_device(device);
1764 type = __connman_device_get_service_type(device);
1765 __connman_notifier_unregister(type);
1767 connman_element_unregister(&device->element);
1771 * connman_device_get_data:
1772 * @device: device structure
1774 * Get private device data pointer
1776 void *connman_device_get_data(struct connman_device *device)
1778 return device->driver_data;
1782 * connman_device_set_data:
1783 * @device: device structure
1784 * @data: data pointer
1786 * Set private device data pointer
1788 void connman_device_set_data(struct connman_device *device, void *data)
1790 device->driver_data = data;
1793 static gboolean match_driver(struct connman_device *device,
1794 struct connman_device_driver *driver)
1796 if (device->type == driver->type ||
1797 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1803 static int device_probe(struct connman_element *element)
1805 struct connman_device *device = element->device;
1808 DBG("element %p name %s", element, element->name);
1813 if (device->driver != NULL)
1816 for (list = driver_list; list; list = list->next) {
1817 struct connman_device_driver *driver = list->data;
1819 if (match_driver(device, driver) == FALSE)
1822 DBG("driver %p name %s", driver, driver->name);
1824 if (driver->probe(device) == 0) {
1825 device->driver = driver;
1830 if (device->driver == NULL)
1833 return setup_device(device);
1836 static void device_remove(struct connman_element *element)
1838 struct connman_device *device = element->device;
1840 DBG("element %p name %s", element, element->name);
1845 if (device->driver == NULL)
1848 remove_device(device);
1851 static struct connman_driver device_driver = {
1853 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1854 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1855 .probe = device_probe,
1856 .remove = device_remove,
1859 static int device_load(struct connman_device *device)
1862 GError *error = NULL;
1864 connman_bool_t powered;
1867 DBG("device %p", device);
1869 keyfile = __connman_storage_open();
1870 if (keyfile == NULL)
1873 identifier = g_strdup_printf("device_%s", device->element.name);
1874 if (identifier == NULL)
1877 powered = g_key_file_get_boolean(keyfile, identifier,
1880 device->powered_persistent = powered;
1881 g_clear_error(&error);
1883 switch (device->mode) {
1884 case CONNMAN_DEVICE_MODE_UNKNOWN:
1885 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1887 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1888 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1889 val = g_key_file_get_integer(keyfile, identifier,
1890 "ScanInterval", &error);
1891 if (error == NULL && val > 0)
1892 device->scan_interval = val;
1893 g_clear_error(&error);
1900 __connman_storage_close(keyfile, FALSE);
1905 static int device_save(struct connman_device *device)
1910 DBG("device %p", device);
1912 keyfile = __connman_storage_open();
1913 if (keyfile == NULL)
1916 identifier = g_strdup_printf("device_%s", device->element.name);
1917 if (identifier == NULL)
1920 g_key_file_set_boolean(keyfile, identifier,
1921 "Powered", device->powered_persistent);
1923 switch (device->mode) {
1924 case CONNMAN_DEVICE_MODE_UNKNOWN:
1925 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1927 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1928 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1929 if (device->scan_interval > 0)
1930 g_key_file_set_integer(keyfile, identifier,
1931 "ScanInterval", device->scan_interval);
1938 __connman_storage_close(keyfile, TRUE);
1943 static struct connman_storage device_storage = {
1945 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1946 .device_load = device_load,
1947 .device_save = device_save,
1950 int __connman_device_init(void)
1954 connection = connman_dbus_get_connection();
1956 if (connman_storage_register(&device_storage) < 0)
1957 connman_error("Failed to register device storage");
1959 return connman_driver_register(&device_driver);
1962 void __connman_device_cleanup(void)
1966 connman_driver_unregister(&device_driver);
1968 connman_storage_unregister(&device_storage);
1970 dbus_connection_unref(connection);