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_persistent;
44 connman_bool_t carrier;
45 connman_bool_t scanning;
46 connman_bool_t disconnected;
47 connman_uint16_t scan_interval;
55 unsigned int connections;
57 struct connman_ipconfig *ipconfig;
59 struct connman_device_driver *driver;
62 connman_bool_t registered;
65 struct connman_network *network;
72 static gboolean device_scan_trigger(gpointer user_data)
74 struct connman_device *device = user_data;
76 DBG("device %p", device);
78 if (device->driver == NULL) {
79 device->scan_timeout = 0;
83 if (device->driver->scan)
84 device->driver->scan(device);
89 static void reset_scan_trigger(struct connman_device *device)
91 if (device->scan_timeout > 0) {
92 g_source_remove(device->scan_timeout);
93 device->scan_timeout = 0;
96 if (device->scan_interval > 0) {
97 guint interval = device->scan_interval;
98 device->scan_timeout = g_timeout_add_seconds(interval,
99 device_scan_trigger, device);
103 static const char *type2description(enum connman_device_type type)
106 case CONNMAN_DEVICE_TYPE_UNKNOWN:
107 case CONNMAN_DEVICE_TYPE_VENDOR:
109 case CONNMAN_DEVICE_TYPE_ETHERNET:
111 case CONNMAN_DEVICE_TYPE_WIFI:
113 case CONNMAN_DEVICE_TYPE_WIMAX:
115 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
117 case CONNMAN_DEVICE_TYPE_GPS:
119 case CONNMAN_DEVICE_TYPE_MBM:
120 case CONNMAN_DEVICE_TYPE_HSO:
121 case CONNMAN_DEVICE_TYPE_NOZOMI:
122 case CONNMAN_DEVICE_TYPE_HUAWEI:
123 case CONNMAN_DEVICE_TYPE_NOVATEL:
130 static const char *type2string(enum connman_device_type type)
133 case CONNMAN_DEVICE_TYPE_UNKNOWN:
134 case CONNMAN_DEVICE_TYPE_VENDOR:
136 case CONNMAN_DEVICE_TYPE_ETHERNET:
138 case CONNMAN_DEVICE_TYPE_WIFI:
140 case CONNMAN_DEVICE_TYPE_WIMAX:
142 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
144 case CONNMAN_DEVICE_TYPE_GPS:
146 case CONNMAN_DEVICE_TYPE_MBM:
147 case CONNMAN_DEVICE_TYPE_HSO:
148 case CONNMAN_DEVICE_TYPE_HUAWEI:
149 case CONNMAN_DEVICE_TYPE_NOZOMI:
150 case CONNMAN_DEVICE_TYPE_NOVATEL:
157 enum connman_service_type __connman_device_get_service_type(struct connman_device *device)
159 enum connman_device_type type = connman_device_get_type(device);
162 case CONNMAN_DEVICE_TYPE_UNKNOWN:
163 case CONNMAN_DEVICE_TYPE_VENDOR:
164 case CONNMAN_DEVICE_TYPE_GPS:
165 case CONNMAN_DEVICE_TYPE_NOZOMI:
166 case CONNMAN_DEVICE_TYPE_HUAWEI:
167 case CONNMAN_DEVICE_TYPE_NOVATEL:
169 case CONNMAN_DEVICE_TYPE_ETHERNET:
170 return CONNMAN_SERVICE_TYPE_ETHERNET;
171 case CONNMAN_DEVICE_TYPE_WIFI:
172 return CONNMAN_SERVICE_TYPE_WIFI;
173 case CONNMAN_DEVICE_TYPE_WIMAX:
174 return CONNMAN_SERVICE_TYPE_WIMAX;
175 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
176 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
177 case CONNMAN_DEVICE_TYPE_MBM:
178 case CONNMAN_DEVICE_TYPE_HSO:
179 return CONNMAN_SERVICE_TYPE_CELLULAR;
182 return CONNMAN_SERVICE_TYPE_UNKNOWN;
185 static int set_connected(struct connman_device *device,
186 connman_bool_t connected)
188 if (connected == TRUE) {
189 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
190 struct connman_element *element;
192 device->disconnected = TRUE;
194 switch (device->element.ipv4.method) {
195 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
196 case CONNMAN_IPCONFIG_METHOD_OFF:
198 case CONNMAN_IPCONFIG_METHOD_STATIC:
199 type = CONNMAN_ELEMENT_TYPE_IPV4;
201 case CONNMAN_IPCONFIG_METHOD_DHCP:
202 type = CONNMAN_ELEMENT_TYPE_DHCP;
206 element = connman_element_create(NULL);
207 if (element != NULL) {
208 struct connman_service *service;
210 element->type = type;
211 element->index = device->element.index;
213 if (connman_element_register(element,
214 &device->element) < 0)
215 connman_element_unref(element);
217 device->disconnected = FALSE;
219 service = __connman_service_lookup_from_device(device);
220 __connman_service_indicate_state(service,
221 CONNMAN_SERVICE_STATE_CONFIGURATION);
224 struct connman_service *service;
226 connman_element_unregister_children(&device->element);
228 device->disconnected = TRUE;
230 service = __connman_service_lookup_from_device(device);
231 __connman_service_indicate_state(service,
232 CONNMAN_SERVICE_STATE_IDLE);
235 if (connected == TRUE) {
236 enum connman_service_type type;
238 type = __connman_device_get_service_type(device);
239 __connman_notifier_connect(type);
241 enum connman_service_type type;
243 type = __connman_device_get_service_type(device);
244 __connman_notifier_disconnect(type);
250 static int set_carrier(struct connman_device *device, connman_bool_t carrier)
252 struct connman_service *service;
255 __connman_profile_add_device(device);
257 __connman_profile_remove_device(device);
259 service = __connman_service_lookup_from_device(device);
260 __connman_service_set_carrier(service, carrier);
262 return set_connected(device, carrier);
265 static int powered_changed(struct connman_device *device)
268 DBusMessageIter entry, value;
269 const char *key = "Powered";
271 signal = dbus_message_new_signal(device->element.path,
272 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
276 dbus_message_iter_init_append(signal, &entry);
278 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
280 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
281 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
282 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
284 dbus_message_iter_close_container(&entry, &value);
286 g_dbus_send_message(connection, signal);
291 static int set_powered(struct connman_device *device, connman_bool_t powered)
293 struct connman_device_driver *driver = device->driver;
294 enum connman_service_type type;
297 DBG("device %p powered %d", device, powered);
299 if (device->powered == powered)
305 type = __connman_device_get_service_type(device);
307 if (powered == TRUE) {
308 if (driver->enable) {
309 err = driver->enable(device);
311 __connman_notifier_enable(type);
315 g_hash_table_remove_all(device->networks);
317 set_carrier(device, FALSE);
319 if (driver->disable) {
320 err = driver->disable(device);
322 __connman_notifier_disable(type);
328 device->powered = powered;
329 powered_changed(device);
335 static void append_path(gpointer key, gpointer value, gpointer user_data)
337 struct connman_element *element = value;
338 DBusMessageIter *iter = user_data;
340 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
344 static void append_networks(struct connman_device *device,
345 DBusMessageIter *entry)
347 DBusMessageIter value, iter;
348 const char *key = "Networks";
350 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
352 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
353 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
356 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
357 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
358 g_hash_table_foreach(device->networks, append_path, &iter);
359 dbus_message_iter_close_container(&value, &iter);
361 dbus_message_iter_close_container(entry, &value);
364 static DBusMessage *get_properties(DBusConnection *conn,
365 DBusMessage *msg, void *data)
367 struct connman_device *device = data;
369 DBusMessageIter array, dict, entry;
372 DBG("conn %p", conn);
374 if (__connman_security_check_privilege(msg,
375 CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
376 return __connman_error_permission_denied(msg);
378 reply = dbus_message_new_method_return(msg);
382 dbus_message_iter_init_append(reply, &array);
384 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
385 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
386 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
387 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
389 if (device->name != NULL)
390 connman_dbus_dict_append_variant(&dict, "Name",
391 DBUS_TYPE_STRING, &device->name);
393 str = type2string(device->type);
395 connman_dbus_dict_append_variant(&dict, "Type",
396 DBUS_TYPE_STRING, &str);
398 if (device->address != NULL)
399 connman_dbus_dict_append_variant(&dict, "Address",
400 DBUS_TYPE_STRING, &device->address);
402 if (device->interface != NULL)
403 connman_dbus_dict_append_variant(&dict, "Interface",
404 DBUS_TYPE_STRING, &device->interface);
406 connman_dbus_dict_append_variant(&dict, "Powered",
407 DBUS_TYPE_BOOLEAN, &device->powered);
409 if (device->driver && device->driver->scan)
410 connman_dbus_dict_append_variant(&dict, "Scanning",
411 DBUS_TYPE_BOOLEAN, &device->scanning);
413 switch (device->mode) {
414 case CONNMAN_DEVICE_MODE_UNKNOWN:
416 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
417 __connman_element_append_ipv4(&device->element, &dict);
419 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
420 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
421 if (device->scan_interval > 0)
422 connman_dbus_dict_append_variant(&dict, "ScanInterval",
423 DBUS_TYPE_UINT16, &device->scan_interval);
425 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
427 append_networks(device, &entry);
428 dbus_message_iter_close_container(&dict, &entry);
432 dbus_message_iter_close_container(&array, &dict);
437 static gboolean powered_timeout(gpointer user_data)
439 struct connman_device *device = user_data;
441 DBG("device %p", device);
445 if (device->pending != NULL) {
448 reply = __connman_error_operation_timeout(device->pending);
450 g_dbus_send_message(connection, reply);
452 dbus_message_unref(device->pending);
453 device->pending = NULL;
459 static DBusMessage *set_property(DBusConnection *conn,
460 DBusMessage *msg, void *data)
462 struct connman_device *device = data;
463 DBusMessageIter iter, value;
467 DBG("conn %p", conn);
469 if (dbus_message_iter_init(msg, &iter) == FALSE)
470 return __connman_error_invalid_arguments(msg);
472 dbus_message_iter_get_basic(&iter, &name);
473 dbus_message_iter_next(&iter);
474 dbus_message_iter_recurse(&iter, &value);
476 if (__connman_security_check_privilege(msg,
477 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
478 return __connman_error_permission_denied(msg);
480 type = dbus_message_iter_get_arg_type(&value);
482 if (g_str_equal(name, "Powered") == TRUE) {
483 connman_bool_t powered;
486 if (type != DBUS_TYPE_BOOLEAN)
487 return __connman_error_invalid_arguments(msg);
489 dbus_message_iter_get_basic(&value, &powered);
491 device->powered_persistent = powered;
493 __connman_storage_save_device(device);
495 if (device->powered == powered)
496 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
498 if (device->pending != NULL)
499 return __connman_error_in_progress(msg);
501 err = set_powered(device, powered);
503 if (err != -EINPROGRESS)
504 return __connman_error_failed(msg, -err);
506 device->pending = dbus_message_ref(msg);
508 device->timeout = g_timeout_add_seconds(15,
509 powered_timeout, device);
513 } else if (g_str_equal(name, "ScanInterval") == TRUE) {
514 connman_uint16_t interval;
516 switch (device->mode) {
517 case CONNMAN_DEVICE_MODE_UNKNOWN:
518 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
519 return __connman_error_invalid_arguments(msg);
520 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
521 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
525 if (type != DBUS_TYPE_UINT16)
526 return __connman_error_invalid_arguments(msg);
528 dbus_message_iter_get_basic(&value, &interval);
530 if (device->scan_interval != interval) {
531 device->scan_interval = interval;
533 __connman_storage_save_device(device);
535 reset_scan_trigger(device);
537 } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
540 switch (device->mode) {
541 case CONNMAN_DEVICE_MODE_UNKNOWN:
542 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
543 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
544 return __connman_error_invalid_arguments(msg);
545 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
549 err = __connman_ipconfig_set_ipv4(device->ipconfig,
552 return __connman_error_failed(msg, -err);
554 return __connman_error_invalid_property(msg);
556 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
559 static DBusMessage *propose_scan(DBusConnection *conn,
560 DBusMessage *msg, void *data)
562 struct connman_device *device = data;
565 DBG("conn %p", conn);
567 switch (device->mode) {
568 case CONNMAN_DEVICE_MODE_UNKNOWN:
569 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
570 return __connman_error_not_supported(msg);
571 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
572 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
576 err = __connman_device_scan(device);
578 return __connman_error_failed(msg, -err);
580 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
583 static GDBusMethodTable device_methods[] = {
584 { "GetProperties", "", "a{sv}", get_properties },
585 { "SetProperty", "sv", "", set_property,
586 G_DBUS_METHOD_FLAG_ASYNC },
587 { "ProposeScan", "", "", propose_scan },
591 static GDBusSignalTable device_signals[] = {
592 { "PropertyChanged", "sv" },
596 static void append_devices(DBusMessageIter *entry)
598 DBusMessageIter value, iter;
599 const char *key = "Devices";
601 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
603 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
604 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
607 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
608 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
609 __connman_element_list(NULL, CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
610 dbus_message_iter_close_container(&value, &iter);
612 dbus_message_iter_close_container(entry, &value);
615 static void emit_devices_signal(void)
618 DBusMessageIter entry;
620 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
621 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
625 dbus_message_iter_init_append(signal, &entry);
627 append_devices(&entry);
629 g_dbus_send_message(connection, signal);
632 static int register_interface(struct connman_element *element)
634 struct connman_device *device = element->device;
636 DBG("element %p name %s", element, element->name);
638 if (g_dbus_register_interface(connection, element->path,
639 CONNMAN_DEVICE_INTERFACE,
640 device_methods, device_signals,
641 NULL, device, NULL) == FALSE) {
642 connman_error("Failed to register %s device", element->path);
646 device->registered = TRUE;
648 emit_devices_signal();
653 static void unregister_interface(struct connman_element *element)
655 struct connman_device *device = element->device;
657 DBG("element %p name %s", element, element->name);
659 device->registered = FALSE;
661 emit_devices_signal();
663 g_dbus_unregister_interface(connection, element->path,
664 CONNMAN_DEVICE_INTERFACE);
667 static int setup_device(struct connman_device *device)
671 DBG("device %p", device);
673 err = register_interface(&device->element);
675 if (device->driver->remove)
676 device->driver->remove(device);
677 device->driver = NULL;
681 switch (device->mode) {
682 case CONNMAN_DEVICE_MODE_UNKNOWN:
683 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
684 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
686 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
687 if (device->carrier == TRUE && device->secondary == FALSE)
688 __connman_profile_add_device(device);
692 if (device->offlinemode == FALSE &&
693 device->powered_persistent == TRUE)
694 __connman_device_enable(device);
699 static void probe_driver(struct connman_element *element, gpointer user_data)
701 struct connman_device_driver *driver = user_data;
703 DBG("element %p name %s", element, element->name);
705 if (element->device == NULL)
708 if (element->device->driver != NULL)
711 if (driver->type != element->device->type)
714 if (driver->probe(element->device) < 0)
717 element->device->driver = driver;
719 setup_device(element->device);
722 static void remove_device(struct connman_device *device)
724 DBG("device %p", device);
726 __connman_device_disable(device);
728 switch (device->mode) {
729 case CONNMAN_DEVICE_MODE_UNKNOWN:
730 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
731 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
733 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
734 if (device->secondary == FALSE)
735 __connman_profile_remove_device(device);
739 unregister_interface(&device->element);
741 if (device->driver->remove)
742 device->driver->remove(device);
744 device->driver = NULL;
747 static void remove_driver(struct connman_element *element, gpointer user_data)
749 struct connman_device_driver *driver = user_data;
751 DBG("element %p name %s", element, element->name);
753 if (element->device == NULL)
756 if (element->device->driver == driver)
757 remove_device(element->device);
760 connman_bool_t __connman_device_has_driver(struct connman_device *device)
762 if (device == NULL || device->driver == NULL)
765 return device->registered;
768 static GSList *driver_list = NULL;
770 static gint compare_priority(gconstpointer a, gconstpointer b)
772 const struct connman_device_driver *driver1 = a;
773 const struct connman_device_driver *driver2 = b;
775 return driver2->priority - driver1->priority;
779 * connman_device_driver_register:
780 * @driver: device driver definition
782 * Register a new device driver
784 * Returns: %0 on success
786 int connman_device_driver_register(struct connman_device_driver *driver)
788 DBG("driver %p name %s", driver, driver->name);
790 driver_list = g_slist_insert_sorted(driver_list, driver,
793 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
794 probe_driver, driver);
800 * connman_device_driver_unregister:
801 * @driver: device driver definition
803 * Remove a previously registered device driver
805 void connman_device_driver_unregister(struct connman_device_driver *driver)
807 DBG("driver %p name %s", driver, driver->name);
809 driver_list = g_slist_remove(driver_list, driver);
811 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
812 remove_driver, driver);
815 static void unregister_network(gpointer data)
817 struct connman_network *network = data;
819 DBG("network %p", network);
821 connman_element_unregister((struct connman_element *) network);
823 connman_network_unref(network);
826 static void device_destruct(struct connman_element *element)
828 struct connman_device *device = element->device;
830 DBG("element %p name %s", element, element->name);
832 if (device->timeout > 0) {
833 g_source_remove(device->timeout);
837 if (device->pending != NULL) {
838 dbus_message_unref(device->pending);
839 device->pending = NULL;
842 g_free(device->ident);
843 g_free(device->node);
844 g_free(device->name);
845 g_free(device->address);
846 g_free(device->control);
847 g_free(device->interface);
849 connman_ipconfig_unref(device->ipconfig);
851 g_free(device->last_network);
853 g_hash_table_destroy(device->networks);
854 device->networks = NULL;
858 * connman_device_create:
859 * @node: device node name (for example an address)
862 * Allocate a new device of given #type and assign the #node name to it.
864 * Returns: a newly-allocated #connman_device structure
866 struct connman_device *connman_device_create(const char *node,
867 enum connman_device_type type)
869 struct connman_device *device;
872 DBG("node %s type %d", node, type);
874 device = g_try_new0(struct connman_device, 1);
878 DBG("device %p", device);
880 __connman_element_initialize(&device->element);
882 device->element.name = g_strdup(node);
883 device->element.type = CONNMAN_ELEMENT_TYPE_DEVICE;
885 device->element.device = device;
886 device->element.destruct = device_destruct;
888 str = type2string(type);
890 connman_element_set_string(&device->element,
891 CONNMAN_PROPERTY_ID_TYPE, str);
893 device->element.ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
896 device->name = g_strdup(type2description(device->type));
897 device->mode = CONNMAN_DEVICE_MODE_UNKNOWN;
898 device->secondary = FALSE;
900 device->powered_persistent = TRUE;
902 device->phyindex = -1;
905 case CONNMAN_DEVICE_TYPE_UNKNOWN:
906 case CONNMAN_DEVICE_TYPE_VENDOR:
907 device->scan_interval = 0;
909 case CONNMAN_DEVICE_TYPE_ETHERNET:
910 case CONNMAN_DEVICE_TYPE_WIFI:
911 device->scan_interval = 300;
913 case CONNMAN_DEVICE_TYPE_WIMAX:
914 device->scan_interval = 0;
916 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
917 device->scan_interval = 0;
919 case CONNMAN_DEVICE_TYPE_GPS:
920 device->scan_interval = 0;
922 case CONNMAN_DEVICE_TYPE_MBM:
923 case CONNMAN_DEVICE_TYPE_HSO:
924 case CONNMAN_DEVICE_TYPE_NOZOMI:
925 case CONNMAN_DEVICE_TYPE_HUAWEI:
926 case CONNMAN_DEVICE_TYPE_NOVATEL:
927 device->scan_interval = 0;
931 device->ipconfig = connman_ipconfig_create();
932 if (device->ipconfig == NULL) {
933 connman_device_unref(device);
937 device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
938 g_free, unregister_network);
944 * connman_device_ref:
945 * @device: device structure
947 * Increase reference counter of device
949 struct connman_device *connman_device_ref(struct connman_device *device)
951 if (connman_element_ref(&device->element) == NULL)
958 * connman_device_unref:
959 * @device: device structure
961 * Decrease reference counter of device
963 void connman_device_unref(struct connman_device *device)
965 connman_element_unref(&device->element);
968 const char *__connman_device_get_type(struct connman_device *device)
970 return type2string(device->type);
974 * connman_device_get_type:
975 * @device: device structure
979 enum connman_device_type connman_device_get_type(struct connman_device *device)
985 * connman_device_get_name:
986 * @device: device structure
988 * Get unique name of device
990 const char *connman_device_get_name(struct connman_device *device)
992 return device->element.name;
996 * connman_device_get_path:
997 * @device: device structure
999 * Get path name of device
1001 const char *connman_device_get_path(struct connman_device *device)
1003 return device->element.path;
1007 * connman_device_set_index:
1008 * @device: device structure
1009 * @index: index number
1011 * Set index number of device
1013 void connman_device_set_index(struct connman_device *device, int index)
1015 device->element.index = index;
1019 * connman_device_get_index:
1020 * @device: device structure
1022 * Get index number of device
1024 int connman_device_get_index(struct connman_device *device)
1026 return device->element.index;
1029 int __connman_device_get_phyindex(struct connman_device *device)
1031 return device->phyindex;
1034 void __connman_device_set_phyindex(struct connman_device *device,
1037 device->phyindex = phyindex;
1041 * connman_device_set_interface:
1042 * @device: device structure
1043 * @interface: interface name
1044 * @control: control interface
1046 * Set interface name of device
1048 void connman_device_set_interface(struct connman_device *device,
1049 const char *interface, const char *control)
1051 g_free(device->element.devname);
1052 device->element.devname = g_strdup(interface);
1054 g_free(device->interface);
1055 device->interface = g_strdup(interface);
1057 g_free(device->control);
1058 device->control = g_strdup(control);
1060 if (device->name == NULL) {
1061 const char *str = type2description(device->type);
1062 if (str != NULL && device->interface != NULL)
1063 device->name = g_strdup_printf("%s (%s)", str,
1068 const char *connman_device_get_control(struct connman_device *device)
1070 return device->control;
1074 * connman_device_set_ident:
1075 * @device: device structure
1076 * @ident: unique identifier
1078 * Set unique identifier of device
1080 void connman_device_set_ident(struct connman_device *device,
1083 g_free(device->ident);
1084 device->ident = g_strdup(ident);
1087 const char *__connman_device_get_ident(struct connman_device *device)
1089 return device->ident;
1093 * connman_device_set_mode:
1094 * @device: device structure
1095 * @mode: network mode
1097 * Change network mode of device
1099 void connman_device_set_mode(struct connman_device *device,
1100 enum connman_device_mode mode)
1102 device->mode = mode;
1106 * connman_device_get_mode:
1107 * @device: device structure
1109 * Get network mode of device
1111 enum connman_device_mode connman_device_get_mode(struct connman_device *device)
1113 return device->mode;
1117 * connman_device_set_secondary:
1118 * @device: device structure
1119 * @secondary: secondary value
1121 * Change secondary value of device
1123 void connman_device_set_secondary(struct connman_device *device,
1124 connman_bool_t secondary)
1126 device->secondary = secondary;
1130 * connman_device_get_secondary:
1131 * @device: device structure
1133 * Get secondary value of device
1135 connman_bool_t connman_device_get_secondary(struct connman_device *device)
1137 return device->secondary;
1141 * connman_device_set_powered:
1142 * @device: device structure
1143 * @powered: powered state
1145 * Change power state of device
1147 int connman_device_set_powered(struct connman_device *device,
1148 connman_bool_t powered)
1150 enum connman_service_type type;
1152 DBG("driver %p powered %d", device, powered);
1154 if (device->timeout > 0) {
1155 g_source_remove(device->timeout);
1156 device->timeout = 0;
1159 if (device->pending != NULL) {
1160 g_dbus_send_reply(connection, device->pending,
1163 dbus_message_unref(device->pending);
1164 device->pending = NULL;
1167 if (device->powered == powered)
1170 device->powered = powered;
1172 type = __connman_device_get_service_type(device);
1174 if (device->powered == TRUE)
1175 __connman_notifier_enable(type);
1177 __connman_notifier_disable(type);
1179 if (device->registered == FALSE)
1182 powered_changed(device);
1184 if (powered == FALSE)
1187 reset_scan_trigger(device);
1189 if (device->driver->scan)
1190 device->driver->scan(device);
1195 int __connman_device_set_blocked(struct connman_device *device,
1196 connman_bool_t blocked)
1198 connman_bool_t powered;
1200 DBG("device %p blocked %d", device, blocked);
1202 if (device->blocked == blocked)
1205 device->blocked = blocked;
1207 if (device->offlinemode == TRUE)
1210 if (blocked == FALSE)
1211 powered = device->powered_persistent;
1215 return set_powered(device, powered);
1219 * connman_device_set_carrier:
1220 * @device: device structure
1221 * @carrier: carrier state
1223 * Change carrier state of device (only for device without scanning)
1225 int connman_device_set_carrier(struct connman_device *device,
1226 connman_bool_t carrier)
1228 DBG("device %p carrier %d", device, carrier);
1230 switch (device->mode) {
1231 case CONNMAN_DEVICE_MODE_UNKNOWN:
1232 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1233 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1235 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1239 if (device->carrier == carrier)
1242 device->carrier = carrier;
1244 return set_carrier(device, device->carrier);
1247 int __connman_device_scan(struct connman_device *device)
1249 if (!device->driver || !device->driver->scan)
1252 if (device->powered == FALSE)
1255 return device->driver->scan(device);
1258 int __connman_device_enable(struct connman_device *device)
1260 enum connman_service_type type;
1263 DBG("device %p", device);
1265 if (!device->driver || !device->driver->enable)
1268 if (device->powered == TRUE)
1271 err = device->driver->enable(device);
1275 device->powered = TRUE;
1277 type = __connman_device_get_service_type(device);
1278 __connman_notifier_enable(type);
1283 int __connman_device_enable_persistent(struct connman_device *device)
1285 DBG("device %p", device);
1287 device->powered_persistent = TRUE;
1289 __connman_storage_save_device(device);
1291 return __connman_device_enable(device);
1294 int __connman_device_disable(struct connman_device *device)
1296 enum connman_service_type type;
1299 DBG("device %p", device);
1301 if (!device->driver || !device->driver->disable)
1304 if (device->powered == FALSE)
1307 g_hash_table_remove_all(device->networks);
1309 err = device->driver->disable(device);
1313 device->powered = FALSE;
1315 type = __connman_device_get_service_type(device);
1316 __connman_notifier_disable(type);
1321 int __connman_device_disable_persistent(struct connman_device *device)
1323 DBG("device %p", device);
1325 device->powered_persistent = FALSE;
1327 __connman_storage_save_device(device);
1329 return __connman_device_disable(device);
1332 int __connman_device_connect(struct connman_device *device)
1334 DBG("device %p", device);
1336 if (device->disconnected == FALSE)
1339 if (device->driver && device->driver->connect)
1340 device->driver->connect(device);
1345 int __connman_device_disconnect(struct connman_device *device)
1347 GHashTableIter iter;
1348 gpointer key, value;
1350 DBG("device %p", device);
1352 connman_device_set_disconnected(device, TRUE);
1354 g_hash_table_iter_init(&iter, device->networks);
1356 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1357 struct connman_network *network = value;
1359 __connman_network_disconnect(network);
1362 if (device->driver && device->driver->disconnect)
1363 device->driver->disconnect(device);
1368 static void mark_network_unavailable(gpointer key, gpointer value,
1371 struct connman_network *network = value;
1373 if (connman_network_get_connected(network) == TRUE)
1376 connman_network_set_available(network, FALSE);
1379 static gboolean remove_unavailable_network(gpointer key, gpointer value,
1382 struct connman_network *network = value;
1384 if (connman_network_get_connected(network) == TRUE)
1387 if (connman_network_get_available(network) == TRUE)
1393 void __connman_device_cleanup_networks(struct connman_device *device)
1395 g_hash_table_foreach_remove(device->networks,
1396 remove_unavailable_network, NULL);
1400 * connman_device_set_scanning:
1401 * @device: device structure
1402 * @scanning: scanning state
1404 * Change scanning state of device
1406 int connman_device_set_scanning(struct connman_device *device,
1407 connman_bool_t scanning)
1409 DBusMessage *signal;
1410 DBusMessageIter entry, value;
1411 const char *key = "Scanning";
1413 DBG("device %p scanning %d", device, scanning);
1415 if (!device->driver || !device->driver->scan)
1418 if (device->scanning == scanning)
1421 device->scanning = scanning;
1423 signal = dbus_message_new_signal(device->element.path,
1424 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
1428 dbus_message_iter_init_append(signal, &entry);
1430 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
1432 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
1433 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
1434 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &scanning);
1435 dbus_message_iter_close_container(&entry, &value);
1437 g_dbus_send_message(connection, signal);
1439 if (scanning == TRUE) {
1440 reset_scan_trigger(device);
1442 g_hash_table_foreach(device->networks,
1443 mark_network_unavailable, NULL);
1448 __connman_device_cleanup_networks(device);
1450 if (device->connections > 0)
1453 if (device->disconnected == TRUE)
1456 __connman_service_auto_connect();
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("device %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_get_disconnected:
1492 * @device: device structure
1494 * Get device disconnected state
1496 connman_bool_t connman_device_get_disconnected(struct connman_device *device)
1498 return device->disconnected;
1502 * connman_device_set_connected:
1503 * @device: device structure
1504 * @connected: connected state
1506 * Change connected state of device (for Ethernet like devices)
1508 int connman_device_set_connected(struct connman_device *device,
1509 connman_bool_t connected)
1511 DBG("device %p connected %d", device, connected);
1513 switch (device->mode) {
1514 case CONNMAN_DEVICE_MODE_UNKNOWN:
1515 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1516 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1518 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1522 if (device->carrier == FALSE)
1525 return set_connected(device, connected);
1529 * connman_device_set_string:
1530 * @device: device structure
1531 * @key: unique identifier
1532 * @value: string value
1534 * Set string value for specific key
1536 int connman_device_set_string(struct connman_device *device,
1537 const char *key, const char *value)
1539 DBG("device %p key %s value %s", device, key, value);
1541 if (g_str_equal(key, "Address") == TRUE) {
1542 g_free(device->address);
1543 device->address = g_strdup(value);
1544 } else if (g_str_equal(key, "Name") == TRUE) {
1545 g_free(device->name);
1546 device->name = g_strdup(value);
1547 } else if (g_str_equal(key, "Node") == TRUE) {
1548 g_free(device->node);
1549 device->node = g_strdup(value);
1552 return connman_element_set_string(&device->element, key, value);
1556 * connman_device_get_string:
1557 * @device: device structure
1558 * @key: unique identifier
1560 * Get string value for specific key
1562 const char *connman_device_get_string(struct connman_device *device,
1565 DBG("device %p key %s", device, key);
1567 if (g_str_equal(key, "Address") == TRUE)
1568 return device->address;
1569 else if (g_str_equal(key, "Name") == TRUE)
1570 return device->name;
1571 else if (g_str_equal(key, "Node") == TRUE)
1572 return device->node;
1574 return connman_element_get_string(&device->element, key);
1577 static void set_offlinemode(struct connman_element *element, gpointer user_data)
1579 struct connman_device *device = element->device;
1580 connman_bool_t offlinemode = GPOINTER_TO_UINT(user_data);
1581 connman_bool_t powered;
1583 DBG("element %p name %s", element, element->name);
1588 device->offlinemode = offlinemode;
1590 powered = (offlinemode == TRUE) ? FALSE : TRUE;
1592 if (device->powered == powered)
1595 if (device->powered_persistent == FALSE)
1598 set_powered(device, powered);
1601 int __connman_device_set_offlinemode(connman_bool_t offlinemode)
1603 DBG("offlinmode %d", offlinemode);
1605 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_DEVICE,
1606 set_offlinemode, GUINT_TO_POINTER(offlinemode));
1608 __connman_notifier_offlinemode(offlinemode);
1613 void __connman_device_increase_connections(struct connman_device *device)
1615 enum connman_service_type type;
1617 device->connections++;
1619 type = __connman_device_get_service_type(device);
1620 __connman_notifier_connect(type);
1623 void __connman_device_decrease_connections(struct connman_device *device)
1625 enum connman_service_type type;
1627 device->connections--;
1629 type = __connman_device_get_service_type(device);
1630 __connman_notifier_disconnect(type);
1634 * connman_device_add_network:
1635 * @device: device structure
1636 * @network: network structure
1638 * Add new network to the device
1640 int connman_device_add_network(struct connman_device *device,
1641 struct connman_network *network)
1643 const char *identifier = connman_network_get_identifier(network);
1646 DBG("device %p network %p", device, network);
1648 switch (device->mode) {
1649 case CONNMAN_DEVICE_MODE_UNKNOWN:
1650 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1652 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1653 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1657 __connman_network_set_device(network, device);
1659 err = connman_element_register((struct connman_element *) network,
1662 __connman_network_set_device(network, NULL);
1666 g_hash_table_insert(device->networks, g_strdup(identifier),
1673 * connman_device_get_network:
1674 * @device: device structure
1675 * @identifier: network identifier
1677 * Get network for given identifier
1679 struct connman_network *connman_device_get_network(struct connman_device *device,
1680 const char *identifier)
1682 DBG("device %p identifier %s", device, identifier);
1684 return g_hash_table_lookup(device->networks, identifier);
1688 * connman_device_remove_network:
1689 * @device: device structure
1690 * @identifier: network identifier
1692 * Remove network for given identifier
1694 int connman_device_remove_network(struct connman_device *device,
1695 const char *identifier)
1697 DBG("device %p identifier %s", device, identifier);
1699 g_hash_table_remove(device->networks, identifier);
1704 void __connman_device_set_network(struct connman_device *device,
1705 struct connman_network *network)
1709 if (device->network == network)
1712 if (device->network != NULL)
1713 connman_network_unref(device->network);
1715 if (network != NULL) {
1716 name = connman_network_get_string(network,
1717 CONNMAN_PROPERTY_ID_NAME);
1718 g_free(device->last_network);
1719 device->last_network = g_strdup(name);
1721 device->network = connman_network_ref(network);
1723 g_free(device->last_network);
1724 device->last_network = NULL;
1726 device->network = NULL;
1731 * connman_device_register:
1732 * @device: device structure
1734 * Register device with the system
1736 int connman_device_register(struct connman_device *device)
1738 enum connman_service_type type;
1740 __connman_storage_load_device(device);
1742 device->offlinemode = __connman_manager_get_offlinemode();
1744 type = __connman_device_get_service_type(device);
1745 __connman_notifier_register(type);
1747 return connman_element_register(&device->element, NULL);
1751 * connman_device_unregister:
1752 * @device: device structure
1754 * Unregister device with the system
1756 void connman_device_unregister(struct connman_device *device)
1758 enum connman_service_type type;
1760 __connman_storage_save_device(device);
1762 type = __connman_device_get_service_type(device);
1763 __connman_notifier_unregister(type);
1765 connman_element_unregister(&device->element);
1769 * connman_device_get_data:
1770 * @device: device structure
1772 * Get private device data pointer
1774 void *connman_device_get_data(struct connman_device *device)
1776 return device->driver_data;
1780 * connman_device_set_data:
1781 * @device: device structure
1782 * @data: data pointer
1784 * Set private device data pointer
1786 void connman_device_set_data(struct connman_device *device, void *data)
1788 device->driver_data = data;
1791 static gboolean match_driver(struct connman_device *device,
1792 struct connman_device_driver *driver)
1794 if (device->type == driver->type ||
1795 driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
1801 static int device_probe(struct connman_element *element)
1803 struct connman_device *device = element->device;
1806 DBG("element %p name %s", element, element->name);
1811 if (device->driver != NULL)
1814 for (list = driver_list; list; list = list->next) {
1815 struct connman_device_driver *driver = list->data;
1817 if (match_driver(device, driver) == FALSE)
1820 DBG("driver %p name %s", driver, driver->name);
1822 if (driver->probe(device) == 0) {
1823 device->driver = driver;
1828 if (device->driver == NULL)
1831 return setup_device(device);
1834 static void device_remove(struct connman_element *element)
1836 struct connman_device *device = element->device;
1838 DBG("element %p name %s", element, element->name);
1843 if (device->driver == NULL)
1846 remove_device(device);
1849 static struct connman_driver device_driver = {
1851 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
1852 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
1853 .probe = device_probe,
1854 .remove = device_remove,
1857 static int device_load(struct connman_device *device)
1860 GError *error = NULL;
1862 connman_bool_t powered;
1865 DBG("device %p", device);
1867 keyfile = __connman_storage_open();
1868 if (keyfile == NULL)
1871 identifier = g_strdup_printf("device_%s", device->element.name);
1872 if (identifier == NULL)
1875 powered = g_key_file_get_boolean(keyfile, identifier,
1878 device->powered_persistent = powered;
1879 g_clear_error(&error);
1881 switch (device->mode) {
1882 case CONNMAN_DEVICE_MODE_UNKNOWN:
1883 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1885 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1886 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1887 val = g_key_file_get_integer(keyfile, identifier,
1888 "ScanInterval", &error);
1889 if (error == NULL && val > 0)
1890 device->scan_interval = val;
1891 g_clear_error(&error);
1898 __connman_storage_close(keyfile, FALSE);
1903 static int device_save(struct connman_device *device)
1908 DBG("device %p", device);
1910 keyfile = __connman_storage_open();
1911 if (keyfile == NULL)
1914 identifier = g_strdup_printf("device_%s", device->element.name);
1915 if (identifier == NULL)
1918 g_key_file_set_boolean(keyfile, identifier,
1919 "Powered", device->powered_persistent);
1921 switch (device->mode) {
1922 case CONNMAN_DEVICE_MODE_UNKNOWN:
1923 case CONNMAN_DEVICE_MODE_TRANSPORT_IP:
1925 case CONNMAN_DEVICE_MODE_NETWORK_SINGLE:
1926 case CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE:
1927 if (device->scan_interval > 0)
1928 g_key_file_set_integer(keyfile, identifier,
1929 "ScanInterval", device->scan_interval);
1936 __connman_storage_close(keyfile, TRUE);
1941 static struct connman_storage device_storage = {
1943 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
1944 .device_load = device_load,
1945 .device_save = device_save,
1948 int __connman_device_init(void)
1952 connection = connman_dbus_get_connection();
1954 if (connman_storage_register(&device_storage) < 0)
1955 connman_error("Failed to register device storage");
1957 return connman_driver_register(&device_driver);
1960 void __connman_device_cleanup(void)
1964 connman_driver_unregister(&device_driver);
1966 connman_storage_unregister(&device_storage);
1968 dbus_connection_unref(connection);