5 * Copyright (C) 2007-2010 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
35 static DBusConnection *connection;
37 static GNode *element_root = NULL;
38 static GSList *driver_list = NULL;
39 static gchar **device_filter = NULL;
40 static gchar **nodevice_filter = NULL;
42 static gboolean started = FALSE;
44 static const char *type2string(enum connman_element_type type)
47 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
49 case CONNMAN_ELEMENT_TYPE_ROOT:
51 case CONNMAN_ELEMENT_TYPE_DEVICE:
53 case CONNMAN_ELEMENT_TYPE_NETWORK:
55 case CONNMAN_ELEMENT_TYPE_SERVICE:
57 case CONNMAN_ELEMENT_TYPE_BOOTP:
59 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
61 case CONNMAN_ELEMENT_TYPE_VENDOR:
69 enum connman_element_type type;
70 element_cb_t callback;
74 static gboolean foreach_callback(GNode *node, gpointer user_data)
76 struct connman_element *element = node->data;
77 struct foreach_data *data = user_data;
79 DBG("element %p name %s", element, element->name);
81 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
84 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
85 data->type != element->type)
89 data->callback(element, data->user_data);
94 void __connman_element_foreach(struct connman_element *element,
95 enum connman_element_type type,
96 element_cb_t callback, gpointer user_data)
98 struct foreach_data data = { type, callback, user_data };
103 if (element != NULL) {
104 node = g_node_find(element_root, G_PRE_ORDER,
105 G_TRAVERSE_ALL, element);
111 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
112 foreach_callback, &data);
115 struct append_filter {
116 enum connman_element_type type;
117 DBusMessageIter *iter;
120 static gboolean append_path(GNode *node, gpointer user_data)
122 struct connman_element *element = node->data;
123 struct append_filter *filter = user_data;
125 DBG("element %p name %s", element, element->name);
127 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
130 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
131 filter->type != element->type)
134 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
135 __connman_device_has_driver(element->device) == FALSE)
138 if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
139 __connman_network_has_driver(element->network) == FALSE)
142 dbus_message_iter_append_basic(filter->iter,
143 DBUS_TYPE_OBJECT_PATH, &element->path);
148 void __connman_element_list(struct connman_element *element,
149 enum connman_element_type type,
150 DBusMessageIter *iter)
152 struct append_filter filter = { type, iter };
157 if (element != NULL) {
158 node = g_node_find(element_root, G_PRE_ORDER,
159 G_TRAVERSE_ALL, element);
165 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
166 append_path, &filter);
169 static struct connman_network *get_network(struct connman_element *element)
171 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
172 element->network != NULL)
173 return element->network;
175 if (element->parent == NULL)
178 return get_network(element->parent);
181 struct connman_service *__connman_element_get_service(struct connman_element *element)
183 struct connman_service *service = NULL;
184 struct connman_network *network;
185 struct connman_device *device;
186 enum connman_device_type type;
188 device = __connman_element_get_device(element);
189 if (device == NULL) {
190 /* Workaround for the connection removal. */
191 service = __connman_service_lookup_from_index(element->index);
195 type = connman_device_get_type(device);
198 case CONNMAN_DEVICE_TYPE_UNKNOWN:
199 case CONNMAN_DEVICE_TYPE_VENDOR:
200 case CONNMAN_DEVICE_TYPE_GPS:
201 case CONNMAN_DEVICE_TYPE_GADGET:
203 case CONNMAN_DEVICE_TYPE_ETHERNET:
204 case CONNMAN_DEVICE_TYPE_WIFI:
205 case CONNMAN_DEVICE_TYPE_WIMAX:
206 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
207 case CONNMAN_DEVICE_TYPE_CELLULAR:
208 network = get_network(element);
211 service = __connman_service_lookup_from_network(network);
218 struct connman_device *__connman_element_get_device(struct connman_element *element)
220 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
221 element->device != NULL)
222 return element->device;
224 if (element->parent == NULL)
227 return __connman_element_get_device(element->parent);
231 enum connman_service_type type;
232 struct connman_device *device;
233 connman_bool_t error;
236 static gboolean find_device(GNode *node, gpointer user_data)
238 struct connman_element *element = node->data;
239 struct find_data *data = user_data;
241 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
244 if (element->device == NULL)
247 if (data->type != __connman_device_get_service_type(element->device))
250 data->device = element->device;
255 struct connman_device *__connman_element_find_device(enum connman_service_type type)
257 struct find_data data = { .type = type, .device = NULL };
259 g_node_traverse(element_root, G_PRE_ORDER,
260 G_TRAVERSE_ALL, -1, find_device, &data);
265 static gboolean request_scan(GNode *node, gpointer user_data)
267 struct connman_element *element = node->data;
268 struct find_data *data = user_data;
269 enum connman_service_type type;
271 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
274 if (element->device == NULL)
277 type = __connman_device_get_service_type(element->device);
280 case CONNMAN_SERVICE_TYPE_UNKNOWN:
281 case CONNMAN_SERVICE_TYPE_SYSTEM:
282 case CONNMAN_SERVICE_TYPE_ETHERNET:
283 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
284 case CONNMAN_SERVICE_TYPE_CELLULAR:
285 case CONNMAN_SERVICE_TYPE_GPS:
286 case CONNMAN_SERVICE_TYPE_VPN:
287 case CONNMAN_SERVICE_TYPE_GADGET:
289 case CONNMAN_SERVICE_TYPE_WIFI:
290 case CONNMAN_SERVICE_TYPE_WIMAX:
291 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
297 __connman_device_scan(element->device);
302 int __connman_element_request_scan(enum connman_service_type type)
304 struct find_data data = { .type = type, .device = NULL };
306 g_node_traverse(element_root, G_PRE_ORDER,
307 G_TRAVERSE_ALL, -1, request_scan, &data);
312 static gboolean enable_technology(GNode *node, gpointer user_data)
314 struct connman_element *element = node->data;
315 struct find_data *data = user_data;
316 enum connman_service_type type;
319 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
322 if (element->device == NULL)
325 type = __connman_device_get_service_type(element->device);
328 case CONNMAN_SERVICE_TYPE_UNKNOWN:
329 case CONNMAN_SERVICE_TYPE_SYSTEM:
330 case CONNMAN_SERVICE_TYPE_GPS:
331 case CONNMAN_SERVICE_TYPE_VPN:
332 case CONNMAN_SERVICE_TYPE_GADGET:
334 case CONNMAN_SERVICE_TYPE_ETHERNET:
335 case CONNMAN_SERVICE_TYPE_WIFI:
336 case CONNMAN_SERVICE_TYPE_WIMAX:
337 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
338 case CONNMAN_SERVICE_TYPE_CELLULAR:
339 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
345 err = __connman_device_enable_persistent(element->device);
346 if (err == 0 || (err < 0 && err == -EINPROGRESS))
352 int __connman_element_enable_technology(enum connman_service_type type)
354 struct find_data data = { .type = type, .device = NULL, .error = TRUE };
356 g_node_traverse(element_root, G_PRE_ORDER,
357 G_TRAVERSE_ALL, -1, enable_technology, &data);
359 if (data.error == TRUE)
365 static gboolean disable_technology(GNode *node, gpointer user_data)
367 struct connman_element *element = node->data;
368 struct find_data *data = user_data;
369 enum connman_service_type type;
372 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
375 if (element->device == NULL)
378 type = __connman_device_get_service_type(element->device);
381 case CONNMAN_SERVICE_TYPE_UNKNOWN:
382 case CONNMAN_SERVICE_TYPE_SYSTEM:
383 case CONNMAN_SERVICE_TYPE_GPS:
384 case CONNMAN_SERVICE_TYPE_VPN:
385 case CONNMAN_SERVICE_TYPE_GADGET:
387 case CONNMAN_SERVICE_TYPE_ETHERNET:
388 case CONNMAN_SERVICE_TYPE_WIFI:
389 case CONNMAN_SERVICE_TYPE_WIMAX:
390 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
391 case CONNMAN_SERVICE_TYPE_CELLULAR:
392 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
398 err = __connman_device_disable_persistent(element->device);
399 if (err == 0 || (err < 0 && err == -EINPROGRESS))
405 int __connman_element_disable_technology(enum connman_service_type type)
407 struct find_data data = { .type = type, .device = NULL, .error = TRUE };
409 g_node_traverse(element_root, G_PRE_ORDER,
410 G_TRAVERSE_ALL, -1, disable_technology, &data);
412 if (data.error == TRUE)
418 static gint compare_priority(gconstpointer a, gconstpointer b)
420 const struct connman_driver *driver1 = a;
421 const struct connman_driver *driver2 = b;
423 return driver2->priority - driver1->priority;
426 static gboolean match_driver(struct connman_element *element,
427 struct connman_driver *driver)
429 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
432 if (element->type == driver->type ||
433 driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
439 static gboolean probe_driver(GNode *node, gpointer data)
441 struct connman_element *element = node->data;
442 struct connman_driver *driver = data;
444 DBG("element %p name %s", element, element->name);
446 if (!element->driver && match_driver(element, driver) == TRUE) {
447 if (driver->probe(element) < 0)
450 element->driver = driver;
456 void __connman_driver_rescan(struct connman_driver *driver)
458 DBG("driver %p name %s", driver, driver->name);
463 if (element_root != NULL)
464 g_node_traverse(element_root, G_PRE_ORDER,
465 G_TRAVERSE_ALL, -1, probe_driver, driver);
469 * connman_driver_register:
470 * @driver: driver definition
472 * Register a new driver
474 * Returns: %0 on success
476 int connman_driver_register(struct connman_driver *driver)
478 DBG("driver %p name %s", driver, driver->name);
480 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
486 driver_list = g_slist_insert_sorted(driver_list, driver,
489 if (started == FALSE)
492 if (element_root != NULL)
493 g_node_traverse(element_root, G_PRE_ORDER,
494 G_TRAVERSE_ALL, -1, probe_driver, driver);
499 static gboolean remove_driver(GNode *node, gpointer data)
501 struct connman_element *element = node->data;
502 struct connman_driver *driver = data;
504 DBG("element %p name %s", element, element->name);
506 if (element->driver == driver) {
508 driver->remove(element);
510 element->driver = NULL;
517 * connman_driver_unregister:
518 * @driver: driver definition
520 * Remove a previously registered driver
522 void connman_driver_unregister(struct connman_driver *driver)
524 DBG("driver %p name %s", driver, driver->name);
526 driver_list = g_slist_remove(driver_list, driver);
528 if (element_root != NULL)
529 g_node_traverse(element_root, G_POST_ORDER,
530 G_TRAVERSE_ALL, -1, remove_driver, driver);
533 static void unregister_property(gpointer data)
535 struct connman_property *property = data;
537 DBG("property %p", property);
539 g_free(property->value);
543 static void unregister_child(gpointer data)
545 struct connman_element *element = data;
547 DBG("element %p", element);
549 connman_element_unref(element);
552 void __connman_element_initialize(struct connman_element *element)
554 DBG("element %p", element);
556 element->refcount = 1;
558 element->name = NULL;
559 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
560 element->state = CONNMAN_ELEMENT_STATE_UNKNOWN;
561 element->error = CONNMAN_ELEMENT_ERROR_UNKNOWN;
563 element->enabled = FALSE;
565 element->children = g_hash_table_new_full(g_str_hash, g_str_equal,
566 NULL, unregister_child);
568 element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
569 g_free, unregister_property);
573 * connman_element_create:
574 * @name: element name
576 * Allocate a new element and assign the given #name to it. If the name
577 * is #NULL, it will be later on created based on the element type.
579 * Returns: a newly-allocated #connman_element structure
581 struct connman_element *connman_element_create(const char *name)
583 struct connman_element *element;
585 element = g_try_new0(struct connman_element, 1);
589 DBG("element %p", element);
591 __connman_element_initialize(element);
593 element->name = g_strdup(name);
598 struct connman_element *connman_element_ref(struct connman_element *element)
600 DBG("element %p name %s refcount %d", element, element->name,
601 g_atomic_int_get(&element->refcount) + 1);
603 g_atomic_int_inc(&element->refcount);
608 static void free_properties(struct connman_element *element)
610 DBG("element %p name %s", element, element->name);
612 g_hash_table_destroy(element->properties);
613 element->properties = NULL;
616 static void free_children(struct connman_element *element)
618 DBG("element %p name %s", element, element->name);
620 g_hash_table_destroy(element->children);
621 element->children = NULL;
624 void connman_element_unref(struct connman_element *element)
626 DBG("element %p name %s refcount %d", element, element->name,
627 g_atomic_int_get(&element->refcount) - 1);
629 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
630 if (element->destruct)
631 element->destruct(element);
632 free_children(element);
633 free_properties(element);
634 g_free(element->hostname);
635 g_free(element->domainname);
636 g_free(element->ipv6.address);
637 g_free(element->ipv6.network);
638 g_free(element->devname);
639 g_free(element->path);
640 g_free(element->name);
645 static int set_static_property(struct connman_element *element,
646 const char *name, int type, const void *value)
648 struct connman_property *property;
650 DBG("element %p name %s", element, element->name);
652 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
655 property = g_try_new0(struct connman_property, 1);
656 if (property == NULL)
659 property->id = CONNMAN_PROPERTY_ID_INVALID;
660 property->type = type;
662 DBG("name %s type %d value %p", name, type, value);
665 case DBUS_TYPE_STRING:
666 property->value = g_strdup(*((const char **) value));
668 case DBUS_TYPE_BOOLEAN:
670 property->value = g_try_malloc(1);
671 if (property->value != NULL)
672 memcpy(property->value, value, 1);
676 g_hash_table_replace(element->properties, g_strdup(name), property);
681 static int set_static_array_property(struct connman_element *element,
682 const char *name, int type, const void *value, int len)
684 struct connman_property *property;
686 DBG("element %p name %s", element, element->name);
688 if (type != DBUS_TYPE_BYTE)
691 property = g_try_new0(struct connman_property, 1);
692 if (property == NULL)
695 property->id = CONNMAN_PROPERTY_ID_INVALID;
696 property->type = DBUS_TYPE_ARRAY;
697 property->subtype = type;
699 DBG("name %s type %d value %p", name, type, value);
703 property->value = g_try_malloc(len);
704 if (property->value != NULL) {
705 memcpy(property->value,
706 *((const unsigned char **) value), len);
707 property->size = len;
712 g_hash_table_replace(element->properties, g_strdup(name), property);
717 int connman_element_get_value(struct connman_element *element,
718 enum connman_property_id id, void *value)
720 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
724 case CONNMAN_PROPERTY_ID_HOSTNAME:
725 if (element->hostname == NULL)
726 return connman_element_get_value(element->parent,
728 *((char **) value) = element->hostname;
730 case CONNMAN_PROPERTY_ID_DOMAINNAME:
731 if (element->domainname == NULL)
732 return connman_element_get_value(element->parent,
734 *((char **) value) = element->domainname;
736 case CONNMAN_PROPERTY_ID_IPV6_GATEWAY:
737 if (element->ipv6.gateway == NULL)
738 return connman_element_get_value(element->parent,
740 *((char **) value) = element->ipv6.gateway;
750 static gboolean get_static_property(struct connman_element *element,
751 const char *name, void *value)
753 struct connman_property *property;
754 gboolean found = FALSE;
756 DBG("element %p name %s", element, element->name);
758 property = g_hash_table_lookup(element->properties, name);
759 if (property != NULL) {
760 switch (property->type) {
761 case DBUS_TYPE_STRING:
762 *((char **) value) = property->value;
765 case DBUS_TYPE_BOOLEAN:
767 memcpy(value, property->value, 1);
773 if (found == FALSE && element->parent != NULL)
774 return get_static_property(element->parent, name, value);
779 static gboolean get_static_array_property(struct connman_element *element,
780 const char *name, void *value, unsigned int *len)
782 struct connman_property *property;
783 gboolean found = FALSE;
785 DBG("element %p name %s", element, element->name);
787 property = g_hash_table_lookup(element->properties, name);
788 if (property != NULL) {
789 *((void **) value) = property->value;
790 *len = property->size;
798 * connman_element_set_string:
799 * @element: element structure
800 * @key: unique identifier
801 * @value: string value
803 * Set string value for specific key
805 int connman_element_set_string(struct connman_element *element,
806 const char *key, const char *value)
808 return set_static_property(element, key, DBUS_TYPE_STRING, &value);
812 * connman_element_get_string:
813 * @element: element structure
814 * @key: unique identifier
816 * Get string value for specific key
818 const char *connman_element_get_string(struct connman_element *element,
823 if (get_static_property(element, key, &value) == FALSE)
830 * connman_element_set_bool:
831 * @element: element structure
832 * @key: unique identifier
833 * @value: boolean value
835 * Set boolean value for specific key
837 int connman_element_set_bool(struct connman_element *element,
838 const char *key, connman_bool_t value)
840 return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
844 * connman_element_get_bool:
845 * @element: element structure
846 * @key: unique identifier
848 * Get boolean value for specific key
850 connman_bool_t connman_element_get_bool(struct connman_element *element,
853 connman_bool_t value;
855 if (get_static_property(element, key, &value) == FALSE)
862 * connman_element_set_uint8:
863 * @element: element structure
864 * @key: unique identifier
865 * @value: integer value
867 * Set integer value for specific key
869 int connman_element_set_uint8(struct connman_element *element,
870 const char *key, connman_uint8_t value)
872 return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
876 * connman_element_get_uint8:
877 * @element: element structure
878 * @key: unique identifier
880 * Get integer value for specific key
882 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
885 connman_uint8_t value;
887 if (get_static_property(element, key, &value) == FALSE)
894 * connman_element_set_blob:
895 * @element: element structure
896 * @key: unique identifier
900 * Set binary blob value for specific key
902 int connman_element_set_blob(struct connman_element *element,
903 const char *key, const void *data, unsigned int size)
905 return set_static_array_property(element, key,
906 DBUS_TYPE_BYTE, &data, size);
910 * connman_element_get_blob:
911 * @element: element structure
912 * @key: unique identifier
913 * @size: pointer to blob size
915 * Get binary blob value for specific key
917 const void *connman_element_get_blob(struct connman_element *element,
918 const char *key, unsigned int *size)
922 if (get_static_array_property(element, key, &value, size) == FALSE)
928 static void probe_element(struct connman_element *element)
932 DBG("element %p name %s", element, element->name);
934 for (list = driver_list; list; list = list->next) {
935 struct connman_driver *driver = list->data;
937 if (match_driver(element, driver) == FALSE)
940 DBG("driver %p name %s", driver, driver->name);
942 if (driver->probe(element) == 0) {
943 element->driver = driver;
949 static void register_element(gpointer data, gpointer user_data)
951 struct connman_element *element = data;
952 const gchar *basepath;
955 if (element->parent) {
956 node = g_node_find(element_root, G_PRE_ORDER,
957 G_TRAVERSE_ALL, element->parent);
958 basepath = element->parent->path;
960 element->parent = element_root->data;
963 basepath = CONNMAN_PATH "/device";
966 element->path = g_strdup_printf("%s/%s", basepath, element->name);
969 connman_error("Element registration for %s failed",
974 DBG("element %p path %s", element, element->path);
976 g_node_append_data(node, element);
978 if (started == FALSE)
981 probe_element(element);
984 gboolean __connman_element_device_isfiltered(const char *devname)
988 if (device_filter == NULL)
991 for (pattern = device_filter; *pattern; pattern++) {
992 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
993 DBG("ignoring device %s (match)", devname);
999 if (nodevice_filter == NULL)
1002 for (pattern = nodevice_filter; *pattern; pattern++) {
1003 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1004 DBG("ignoring device %s (no match)", devname);
1013 * connman_element_register:
1014 * @element: the element to register
1015 * @parent: the parent to register the element with
1017 * Register an element with the core. It will be register under the given
1018 * parent of if %NULL is provided under the root element.
1020 * Returns: %0 on success
1022 int connman_element_register(struct connman_element *element,
1023 struct connman_element *parent)
1025 DBG("element %p name %s parent %p", element, element->name, parent);
1027 if (element->devname == NULL)
1028 element->devname = g_strdup(element->name);
1030 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
1033 if (__connman_element_device_isfiltered(element->devname) == TRUE)
1037 if (connman_element_ref(element) == NULL)
1040 if (element->name == NULL) {
1041 element->name = g_strdup(type2string(element->type));
1042 if (element->name == NULL) {
1047 element->parent = parent;
1049 register_element(element, NULL);
1054 static gboolean remove_element(GNode *node, gpointer user_data)
1056 struct connman_element *element = node->data;
1057 struct connman_element *root = user_data;
1059 DBG("element %p name %s", element, element->name);
1061 if (element == root)
1064 g_node_unlink(node);
1066 if (element->driver) {
1067 if (element->driver->remove)
1068 element->driver->remove(element);
1070 element->driver = NULL;
1073 g_node_destroy(node);
1075 connman_element_unref(element);
1080 struct unregister_type {
1081 struct connman_element *root;
1082 enum connman_element_type type;
1085 static gboolean remove_element_type(GNode *node, gpointer user_data)
1087 struct unregister_type *children_type = user_data;
1088 struct connman_element *root = children_type->root;
1089 struct connman_element *element = node->data;
1090 enum connman_element_type type = children_type->type;
1092 DBG("element %p name %s", element, element->name);
1094 if (element == root)
1097 if(element->type != type)
1100 g_node_unlink(node);
1102 if (element->driver) {
1103 if (element->driver->remove)
1104 element->driver->remove(element);
1106 element->driver = NULL;
1109 g_node_destroy(node);
1111 connman_element_unref(element);
1117 void connman_element_unregister(struct connman_element *element)
1121 DBG("element %p name %s", element, element->name);
1123 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1126 g_node_traverse(node, G_POST_ORDER,
1127 G_TRAVERSE_ALL, -1, remove_element, NULL);
1130 void connman_element_unregister_children(struct connman_element *element)
1134 DBG("element %p name %s", element, element->name);
1136 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1139 g_node_traverse(node, G_POST_ORDER,
1140 G_TRAVERSE_ALL, -1, remove_element, element);
1143 void connman_element_unregister_children_type(struct connman_element *element, enum connman_element_type type)
1147 DBG("element %p name %s", element, element->name);
1149 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1152 struct unregister_type children_type;
1154 children_type.root = element;
1155 children_type.type = type;
1156 g_node_traverse(node, G_POST_ORDER,
1157 G_TRAVERSE_ALL, -1, remove_element_type, &children_type);
1162 static gboolean update_element(GNode *node, gpointer user_data)
1164 struct connman_element *element = node->data;
1166 DBG("element %p name %s", element, element->name);
1168 if (element->driver && element->driver->update)
1169 element->driver->update(element);
1174 void connman_element_update(struct connman_element *element)
1178 DBG("element %p name %s", element, element->name);
1180 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1183 g_node_traverse(node, G_PRE_ORDER,
1184 G_TRAVERSE_ALL, -1, update_element, element);
1187 int connman_element_set_enabled(struct connman_element *element,
1190 if (element->enabled == enabled)
1193 element->enabled = enabled;
1195 connman_element_update(element);
1200 static enum connman_service_error convert_error(enum connman_element_error error)
1203 case CONNMAN_ELEMENT_ERROR_UNKNOWN:
1204 case CONNMAN_ELEMENT_ERROR_FAILED:
1206 case CONNMAN_ELEMENT_ERROR_DHCP_FAILED:
1207 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
1208 case CONNMAN_ELEMENT_ERROR_CONNECT_FAILED:
1209 return CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
1212 return CONNMAN_SERVICE_ERROR_UNKNOWN;
1216 * connman_element_set_error:
1217 * @element: element structure
1218 * @error: error identifier
1220 * Set error state and specific error identifier
1222 void connman_element_set_error(struct connman_element *element,
1223 enum connman_element_error error)
1225 struct connman_service *service;
1227 DBG("element %p error %d", element, error);
1229 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1232 element->state = CONNMAN_ELEMENT_STATE_ERROR;
1233 element->error = error;
1235 if (element->driver && element->driver->change)
1236 element->driver->change(element);
1238 service = __connman_element_get_service(element);
1239 __connman_service_indicate_error(service, convert_error(error));
1242 void __connman_element_set_driver(struct connman_element *element)
1246 DBG("element %p name %s driver %p", element, element->name,
1249 if (element->driver)
1252 for (list = driver_list; list; list = list->next) {
1253 struct connman_driver *driver = list->data;
1255 if (match_driver(element, driver) == FALSE)
1258 element->driver = driver;
1264 int __connman_element_init(const char *device, const char *nodevice)
1266 struct connman_element *element;
1270 connection = connman_dbus_get_connection();
1271 if (connection == NULL)
1275 device_filter = g_strsplit(device, ",", -1);
1278 nodevice_filter = g_strsplit(nodevice, ",", -1);
1280 element = connman_element_create("root");
1282 element->path = g_strdup("/");
1283 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1285 element_root = g_node_new(element);
1287 __connman_technology_init();
1288 __connman_notifier_init();
1289 __connman_location_init();
1290 __connman_service_init();
1291 __connman_provider_init();
1292 __connman_network_init();
1293 __connman_device_init();
1298 static gboolean probe_node(GNode *node, gpointer data)
1300 struct connman_element *element = node->data;
1302 DBG("element %p name %s", element, element->name);
1304 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1307 if (element->driver)
1310 probe_element(element);
1315 void __connman_element_start(void)
1319 __connman_storage_init_profile();
1321 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1326 __connman_rtnl_start();
1328 __connman_dhcp_init();
1329 __connman_wpad_init();
1330 __connman_wispr_init();
1332 __connman_rfkill_init();
1335 void __connman_element_stop(void)
1339 __connman_rfkill_cleanup();
1341 __connman_wispr_cleanup();
1342 __connman_wpad_cleanup();
1343 __connman_dhcp_cleanup();
1344 __connman_provider_cleanup();
1347 static gboolean free_driver(GNode *node, gpointer data)
1349 struct connman_element *element = node->data;
1351 DBG("element %p name %s", element, element->name);
1353 if (element->driver) {
1354 if (element->driver->remove)
1355 element->driver->remove(element);
1357 element->driver = NULL;
1363 static gboolean free_node(GNode *node, gpointer data)
1365 struct connman_element *element = node->data;
1367 DBG("element %p name %s", element, element->name);
1369 if (g_node_depth(node) > 1)
1370 connman_element_unregister(element);
1375 void __connman_element_cleanup(void)
1379 __connman_device_cleanup();
1380 __connman_network_cleanup();
1381 __connman_service_cleanup();
1382 __connman_location_cleanup();
1383 __connman_notifier_cleanup();
1384 __connman_technology_cleanup();
1386 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1389 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1392 connman_element_unref(element_root->data);
1394 g_node_destroy(element_root);
1395 element_root = NULL;
1397 g_strfreev(nodevice_filter);
1398 g_strfreev(device_filter);
1400 if (connection == NULL)
1403 dbus_connection_unref(connection);