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_PROFILE:
53 case CONNMAN_ELEMENT_TYPE_DEVICE:
55 case CONNMAN_ELEMENT_TYPE_NETWORK:
57 case CONNMAN_ELEMENT_TYPE_SERVICE:
59 case CONNMAN_ELEMENT_TYPE_IPV4:
61 case CONNMAN_ELEMENT_TYPE_IPV6:
63 case CONNMAN_ELEMENT_TYPE_DHCP:
65 case CONNMAN_ELEMENT_TYPE_BOOTP:
67 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
69 case CONNMAN_ELEMENT_TYPE_CONNECTION:
71 case CONNMAN_ELEMENT_TYPE_VENDOR:
79 enum connman_element_type type;
80 element_cb_t callback;
84 static gboolean foreach_callback(GNode *node, gpointer user_data)
86 struct connman_element *element = node->data;
87 struct foreach_data *data = user_data;
89 DBG("element %p name %s", element, element->name);
91 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
94 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
95 data->type != element->type)
99 data->callback(element, data->user_data);
104 void __connman_element_foreach(struct connman_element *element,
105 enum connman_element_type type,
106 element_cb_t callback, gpointer user_data)
108 struct foreach_data data = { type, callback, user_data };
113 if (element != NULL) {
114 node = g_node_find(element_root, G_PRE_ORDER,
115 G_TRAVERSE_ALL, element);
121 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
122 foreach_callback, &data);
125 struct append_filter {
126 enum connman_element_type type;
127 DBusMessageIter *iter;
130 static gboolean append_path(GNode *node, gpointer user_data)
132 struct connman_element *element = node->data;
133 struct append_filter *filter = user_data;
135 DBG("element %p name %s", element, element->name);
137 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
140 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
141 filter->type != element->type)
144 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
145 __connman_device_has_driver(element->device) == FALSE)
148 if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
149 __connman_network_has_driver(element->network) == FALSE)
152 dbus_message_iter_append_basic(filter->iter,
153 DBUS_TYPE_OBJECT_PATH, &element->path);
158 void __connman_element_list(struct connman_element *element,
159 enum connman_element_type type,
160 DBusMessageIter *iter)
162 struct append_filter filter = { type, iter };
167 if (element != NULL) {
168 node = g_node_find(element_root, G_PRE_ORDER,
169 G_TRAVERSE_ALL, element);
175 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
176 append_path, &filter);
179 static struct connman_network *get_network(struct connman_element *element)
181 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
182 element->network != NULL)
183 return element->network;
185 if (element->parent == NULL)
188 return get_network(element->parent);
191 struct connman_service *__connman_element_get_service(struct connman_element *element)
193 struct connman_service *service = NULL;
194 struct connman_network *network;
195 struct connman_device *device;
196 enum connman_device_type type;
198 device = __connman_element_get_device(element);
202 type = connman_device_get_type(device);
205 case CONNMAN_DEVICE_TYPE_UNKNOWN:
206 case CONNMAN_DEVICE_TYPE_VENDOR:
207 case CONNMAN_DEVICE_TYPE_GPS:
208 case CONNMAN_DEVICE_TYPE_GADGET:
210 case CONNMAN_DEVICE_TYPE_ETHERNET:
211 case CONNMAN_DEVICE_TYPE_WIFI:
212 case CONNMAN_DEVICE_TYPE_WIMAX:
213 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
214 case CONNMAN_DEVICE_TYPE_CELLULAR:
215 network = get_network(element);
218 service = __connman_service_lookup_from_network(network);
225 struct connman_device *__connman_element_get_device(struct connman_element *element)
227 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
228 element->device != NULL)
229 return element->device;
231 if (element->parent == NULL)
234 return __connman_element_get_device(element->parent);
238 enum connman_service_type type;
239 struct connman_device *device;
240 connman_bool_t error;
243 static gboolean find_device(GNode *node, gpointer user_data)
245 struct connman_element *element = node->data;
246 struct find_data *data = user_data;
248 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
251 if (element->device == NULL)
254 if (data->type != __connman_device_get_service_type(element->device))
257 data->device = element->device;
262 struct connman_device *__connman_element_find_device(enum connman_service_type type)
264 struct find_data data = { .type = type, .device = NULL };
266 g_node_traverse(element_root, G_PRE_ORDER,
267 G_TRAVERSE_ALL, -1, find_device, &data);
272 static gboolean request_scan(GNode *node, gpointer user_data)
274 struct connman_element *element = node->data;
275 struct find_data *data = user_data;
276 enum connman_service_type type;
278 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
281 if (element->device == NULL)
284 type = __connman_device_get_service_type(element->device);
287 case CONNMAN_SERVICE_TYPE_UNKNOWN:
288 case CONNMAN_SERVICE_TYPE_SYSTEM:
289 case CONNMAN_SERVICE_TYPE_ETHERNET:
290 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
291 case CONNMAN_SERVICE_TYPE_CELLULAR:
292 case CONNMAN_SERVICE_TYPE_GPS:
293 case CONNMAN_SERVICE_TYPE_VPN:
294 case CONNMAN_SERVICE_TYPE_GADGET:
296 case CONNMAN_SERVICE_TYPE_WIFI:
297 case CONNMAN_SERVICE_TYPE_WIMAX:
298 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
304 __connman_device_scan(element->device);
309 int __connman_element_request_scan(enum connman_service_type type)
311 struct find_data data = { .type = type, .device = NULL };
313 g_node_traverse(element_root, G_PRE_ORDER,
314 G_TRAVERSE_ALL, -1, request_scan, &data);
319 static gboolean enable_technology(GNode *node, gpointer user_data)
321 struct connman_element *element = node->data;
322 struct find_data *data = user_data;
323 enum connman_service_type type;
326 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
329 if (element->device == NULL)
332 type = __connman_device_get_service_type(element->device);
335 case CONNMAN_SERVICE_TYPE_UNKNOWN:
336 case CONNMAN_SERVICE_TYPE_SYSTEM:
337 case CONNMAN_SERVICE_TYPE_GPS:
338 case CONNMAN_SERVICE_TYPE_VPN:
339 case CONNMAN_SERVICE_TYPE_GADGET:
341 case CONNMAN_SERVICE_TYPE_ETHERNET:
342 case CONNMAN_SERVICE_TYPE_WIFI:
343 case CONNMAN_SERVICE_TYPE_WIMAX:
344 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
345 case CONNMAN_SERVICE_TYPE_CELLULAR:
346 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
352 err = __connman_device_enable_persistent(element->device);
353 if (err == 0 || (err < 0 && err == -EINPROGRESS))
359 int __connman_element_enable_technology(enum connman_service_type type)
361 struct find_data data = { .type = type, .device = NULL, .error = TRUE };
363 g_node_traverse(element_root, G_PRE_ORDER,
364 G_TRAVERSE_ALL, -1, enable_technology, &data);
366 if (data.error == TRUE)
372 static gboolean disable_technology(GNode *node, gpointer user_data)
374 struct connman_element *element = node->data;
375 struct find_data *data = user_data;
376 enum connman_service_type type;
379 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
382 if (element->device == NULL)
385 type = __connman_device_get_service_type(element->device);
388 case CONNMAN_SERVICE_TYPE_UNKNOWN:
389 case CONNMAN_SERVICE_TYPE_SYSTEM:
390 case CONNMAN_SERVICE_TYPE_GPS:
391 case CONNMAN_SERVICE_TYPE_VPN:
392 case CONNMAN_SERVICE_TYPE_GADGET:
394 case CONNMAN_SERVICE_TYPE_ETHERNET:
395 case CONNMAN_SERVICE_TYPE_WIFI:
396 case CONNMAN_SERVICE_TYPE_WIMAX:
397 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
398 case CONNMAN_SERVICE_TYPE_CELLULAR:
399 if (data->type != CONNMAN_SERVICE_TYPE_UNKNOWN &&
405 err = __connman_device_disable_persistent(element->device);
406 if (err == 0 || (err < 0 && err == -EINPROGRESS))
412 int __connman_element_disable_technology(enum connman_service_type type)
414 struct find_data data = { .type = type, .device = NULL, .error = TRUE };
416 g_node_traverse(element_root, G_PRE_ORDER,
417 G_TRAVERSE_ALL, -1, disable_technology, &data);
419 if (data.error == TRUE)
425 static gint compare_priority(gconstpointer a, gconstpointer b)
427 const struct connman_driver *driver1 = a;
428 const struct connman_driver *driver2 = b;
430 return driver2->priority - driver1->priority;
433 static gboolean match_driver(struct connman_element *element,
434 struct connman_driver *driver)
436 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
439 if (element->type == driver->type ||
440 driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
446 static gboolean probe_driver(GNode *node, gpointer data)
448 struct connman_element *element = node->data;
449 struct connman_driver *driver = data;
451 DBG("element %p name %s", element, element->name);
453 if (!element->driver && match_driver(element, driver) == TRUE) {
454 if (driver->probe(element) < 0)
457 element->driver = driver;
463 void __connman_driver_rescan(struct connman_driver *driver)
465 DBG("driver %p name %s", driver, driver->name);
470 if (element_root != NULL)
471 g_node_traverse(element_root, G_PRE_ORDER,
472 G_TRAVERSE_ALL, -1, probe_driver, driver);
476 * connman_driver_register:
477 * @driver: driver definition
479 * Register a new driver
481 * Returns: %0 on success
483 int connman_driver_register(struct connman_driver *driver)
485 DBG("driver %p name %s", driver, driver->name);
487 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
493 driver_list = g_slist_insert_sorted(driver_list, driver,
496 if (started == FALSE)
499 if (element_root != NULL)
500 g_node_traverse(element_root, G_PRE_ORDER,
501 G_TRAVERSE_ALL, -1, probe_driver, driver);
506 static gboolean remove_driver(GNode *node, gpointer data)
508 struct connman_element *element = node->data;
509 struct connman_driver *driver = data;
511 DBG("element %p name %s", element, element->name);
513 if (element->driver == driver) {
515 driver->remove(element);
517 element->driver = NULL;
524 * connman_driver_unregister:
525 * @driver: driver definition
527 * Remove a previously registered driver
529 void connman_driver_unregister(struct connman_driver *driver)
531 DBG("driver %p name %s", driver, driver->name);
533 driver_list = g_slist_remove(driver_list, driver);
535 if (element_root != NULL)
536 g_node_traverse(element_root, G_POST_ORDER,
537 G_TRAVERSE_ALL, -1, remove_driver, driver);
540 static void unregister_property(gpointer data)
542 struct connman_property *property = data;
544 DBG("property %p", property);
546 g_free(property->value);
550 static void unregister_child(gpointer data)
552 struct connman_element *element = data;
554 DBG("element %p", element);
556 connman_element_unref(element);
559 void __connman_element_initialize(struct connman_element *element)
561 DBG("element %p", element);
563 element->refcount = 1;
565 element->name = NULL;
566 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
567 element->state = CONNMAN_ELEMENT_STATE_UNKNOWN;
568 element->error = CONNMAN_ELEMENT_ERROR_UNKNOWN;
570 element->enabled = FALSE;
572 element->children = g_hash_table_new_full(g_str_hash, g_str_equal,
573 NULL, unregister_child);
575 element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
576 g_free, unregister_property);
580 * connman_element_create:
581 * @name: element name
583 * Allocate a new element and assign the given #name to it. If the name
584 * is #NULL, it will be later on created based on the element type.
586 * Returns: a newly-allocated #connman_element structure
588 struct connman_element *connman_element_create(const char *name)
590 struct connman_element *element;
592 element = g_try_new0(struct connman_element, 1);
596 DBG("element %p", element);
598 __connman_element_initialize(element);
600 element->name = g_strdup(name);
605 struct connman_element *connman_element_ref(struct connman_element *element)
607 DBG("element %p name %s refcount %d", element, element->name,
608 g_atomic_int_get(&element->refcount) + 1);
610 g_atomic_int_inc(&element->refcount);
615 static void free_properties(struct connman_element *element)
617 DBG("element %p name %s", element, element->name);
619 g_hash_table_destroy(element->properties);
620 element->properties = NULL;
623 static void free_children(struct connman_element *element)
625 DBG("element %p name %s", element, element->name);
627 g_hash_table_destroy(element->children);
628 element->children = NULL;
631 void connman_element_unref(struct connman_element *element)
633 DBG("element %p name %s refcount %d", element, element->name,
634 g_atomic_int_get(&element->refcount) - 1);
636 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
637 if (element->destruct)
638 element->destruct(element);
639 free_children(element);
640 free_properties(element);
641 g_free(element->hostname);
642 g_free(element->domainname);
643 g_free(element->ipv4.address);
644 g_free(element->ipv4.netmask);
645 g_free(element->ipv4.gateway);
646 g_free(element->ipv4.network);
647 g_free(element->ipv4.broadcast);
648 g_free(element->ipv4.nameserver);
649 g_free(element->ipv4.timeserver);
650 g_free(element->ipv4.pac);
651 g_free(element->ipv6.address);
652 g_free(element->ipv6.network);
653 g_free(element->devname);
654 g_free(element->path);
655 g_free(element->name);
660 static int set_static_property(struct connman_element *element,
661 const char *name, int type, const void *value)
663 struct connman_property *property;
665 DBG("element %p name %s", element, element->name);
667 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
670 property = g_try_new0(struct connman_property, 1);
671 if (property == NULL)
674 property->id = CONNMAN_PROPERTY_ID_INVALID;
675 property->type = type;
677 DBG("name %s type %d value %p", name, type, value);
680 case DBUS_TYPE_STRING:
681 property->value = g_strdup(*((const char **) value));
683 case DBUS_TYPE_BOOLEAN:
685 property->value = g_try_malloc(1);
686 if (property->value != NULL)
687 memcpy(property->value, value, 1);
691 g_hash_table_replace(element->properties, g_strdup(name), property);
696 static int set_static_array_property(struct connman_element *element,
697 const char *name, int type, const void *value, int len)
699 struct connman_property *property;
701 DBG("element %p name %s", element, element->name);
703 if (type != DBUS_TYPE_BYTE)
706 property = g_try_new0(struct connman_property, 1);
707 if (property == NULL)
710 property->id = CONNMAN_PROPERTY_ID_INVALID;
711 property->type = DBUS_TYPE_ARRAY;
712 property->subtype = type;
714 DBG("name %s type %d value %p", name, type, value);
718 property->value = g_try_malloc(len);
719 if (property->value != NULL) {
720 memcpy(property->value,
721 *((const unsigned char **) value), len);
722 property->size = len;
727 g_hash_table_replace(element->properties, g_strdup(name), property);
732 int connman_element_get_value(struct connman_element *element,
733 enum connman_property_id id, void *value)
735 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
739 case CONNMAN_PROPERTY_ID_HOSTNAME:
740 if (element->hostname == NULL)
741 return connman_element_get_value(element->parent,
743 *((char **) value) = element->hostname;
745 case CONNMAN_PROPERTY_ID_DOMAINNAME:
746 if (element->domainname == NULL)
747 return connman_element_get_value(element->parent,
749 *((char **) value) = element->domainname;
751 case CONNMAN_PROPERTY_ID_IPV4_METHOD:
752 if (element->ipv4.method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
753 return connman_element_get_value(element->parent,
755 *((const char **) value) = __connman_ipconfig_method2string(element->ipv4.method);
757 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
758 if (element->ipv4.address == NULL)
759 return connman_element_get_value(element->parent,
761 *((char **) value) = element->ipv4.address;
763 case CONNMAN_PROPERTY_ID_IPV4_PEER:
764 if (element->ipv4.peer == NULL)
765 return connman_element_get_value(element->parent,
767 *((char **) value) = element->ipv4.peer;
769 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
770 if (element->ipv4.netmask == NULL)
771 return connman_element_get_value(element->parent,
773 *((char **) value) = element->ipv4.netmask;
775 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
776 if (element->ipv4.gateway == NULL)
777 return connman_element_get_value(element->parent,
779 *((char **) value) = element->ipv4.gateway;
781 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
782 if (element->ipv4.broadcast == NULL)
783 return connman_element_get_value(element->parent,
785 *((char **) value) = element->ipv4.broadcast;
787 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
788 if (element->ipv4.nameserver == NULL)
789 return connman_element_get_value(element->parent,
791 *((char **) value) = element->ipv4.nameserver;
793 case CONNMAN_PROPERTY_ID_IPV4_TIMESERVER:
794 if (element->ipv4.timeserver == NULL)
795 return connman_element_get_value(element->parent,
797 *((char **) value) = element->ipv4.timeserver;
799 case CONNMAN_PROPERTY_ID_IPV4_PAC:
800 if (element->ipv4.pac == NULL)
801 return connman_element_get_value(element->parent,
803 *((char **) value) = element->ipv4.pac;
805 case CONNMAN_PROPERTY_ID_IPV6_GATEWAY:
806 if (element->ipv6.gateway == NULL)
807 return connman_element_get_value(element->parent,
809 *((char **) value) = element->ipv6.gateway;
819 static gboolean get_static_property(struct connman_element *element,
820 const char *name, void *value)
822 struct connman_property *property;
823 gboolean found = FALSE;
825 DBG("element %p name %s", element, element->name);
827 property = g_hash_table_lookup(element->properties, name);
828 if (property != NULL) {
829 switch (property->type) {
830 case DBUS_TYPE_STRING:
831 *((char **) value) = property->value;
834 case DBUS_TYPE_BOOLEAN:
836 memcpy(value, property->value, 1);
842 if (found == FALSE && element->parent != NULL)
843 return get_static_property(element->parent, name, value);
848 static gboolean get_static_array_property(struct connman_element *element,
849 const char *name, void *value, unsigned int *len)
851 struct connman_property *property;
852 gboolean found = FALSE;
854 DBG("element %p name %s", element, element->name);
856 property = g_hash_table_lookup(element->properties, name);
857 if (property != NULL) {
858 *((void **) value) = property->value;
859 *len = property->size;
867 * connman_element_set_string:
868 * @element: element structure
869 * @key: unique identifier
870 * @value: string value
872 * Set string value for specific key
874 int connman_element_set_string(struct connman_element *element,
875 const char *key, const char *value)
877 return set_static_property(element, key, DBUS_TYPE_STRING, &value);
881 * connman_element_get_string:
882 * @element: element structure
883 * @key: unique identifier
885 * Get string value for specific key
887 const char *connman_element_get_string(struct connman_element *element,
892 if (get_static_property(element, key, &value) == FALSE)
899 * connman_element_set_bool:
900 * @element: element structure
901 * @key: unique identifier
902 * @value: boolean value
904 * Set boolean value for specific key
906 int connman_element_set_bool(struct connman_element *element,
907 const char *key, connman_bool_t value)
909 return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
913 * connman_element_get_bool:
914 * @element: element structure
915 * @key: unique identifier
917 * Get boolean value for specific key
919 connman_bool_t connman_element_get_bool(struct connman_element *element,
922 connman_bool_t value;
924 if (get_static_property(element, key, &value) == FALSE)
931 * connman_element_set_uint8:
932 * @element: element structure
933 * @key: unique identifier
934 * @value: integer value
936 * Set integer value for specific key
938 int connman_element_set_uint8(struct connman_element *element,
939 const char *key, connman_uint8_t value)
941 return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
945 * connman_element_get_uint8:
946 * @element: element structure
947 * @key: unique identifier
949 * Get integer value for specific key
951 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
954 connman_uint8_t value;
956 if (get_static_property(element, key, &value) == FALSE)
963 * connman_element_set_blob:
964 * @element: element structure
965 * @key: unique identifier
969 * Set binary blob value for specific key
971 int connman_element_set_blob(struct connman_element *element,
972 const char *key, const void *data, unsigned int size)
974 return set_static_array_property(element, key,
975 DBUS_TYPE_BYTE, &data, size);
979 * connman_element_get_blob:
980 * @element: element structure
981 * @key: unique identifier
982 * @size: pointer to blob size
984 * Get binary blob value for specific key
986 const void *connman_element_get_blob(struct connman_element *element,
987 const char *key, unsigned int *size)
991 if (get_static_array_property(element, key, &value, size) == FALSE)
997 static void probe_element(struct connman_element *element)
1001 DBG("element %p name %s", element, element->name);
1003 for (list = driver_list; list; list = list->next) {
1004 struct connman_driver *driver = list->data;
1006 if (match_driver(element, driver) == FALSE)
1009 DBG("driver %p name %s", driver, driver->name);
1011 if (driver->probe(element) == 0) {
1012 element->driver = driver;
1018 static void register_element(gpointer data, gpointer user_data)
1020 struct connman_element *element = data;
1021 const gchar *basepath;
1024 if (element->parent) {
1025 node = g_node_find(element_root, G_PRE_ORDER,
1026 G_TRAVERSE_ALL, element->parent);
1027 basepath = element->parent->path;
1029 element->parent = element_root->data;
1031 node = element_root;
1032 basepath = CONNMAN_PATH "/device";
1035 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1038 connman_error("Element registration for %s failed",
1043 DBG("element %p path %s", element, element->path);
1045 g_node_append_data(node, element);
1047 if (started == FALSE)
1050 probe_element(element);
1053 gboolean __connman_element_device_isfiltered(const char *devname)
1057 if (device_filter == NULL)
1060 for (pattern = device_filter; *pattern; pattern++) {
1061 if (g_pattern_match_simple(*pattern, devname) == FALSE) {
1062 DBG("ignoring device %s (match)", devname);
1068 if (nodevice_filter == NULL)
1071 for (pattern = nodevice_filter; *pattern; pattern++) {
1072 if (g_pattern_match_simple(*pattern, devname) == TRUE) {
1073 DBG("ignoring device %s (no match)", devname);
1082 * connman_element_register:
1083 * @element: the element to register
1084 * @parent: the parent to register the element with
1086 * Register an element with the core. It will be register under the given
1087 * parent of if %NULL is provided under the root element.
1089 * Returns: %0 on success
1091 int connman_element_register(struct connman_element *element,
1092 struct connman_element *parent)
1094 DBG("element %p name %s parent %p", element, element->name, parent);
1096 if (element->devname == NULL)
1097 element->devname = g_strdup(element->name);
1099 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
1102 if (__connman_element_device_isfiltered(element->devname) == TRUE)
1106 if (connman_element_ref(element) == NULL)
1109 if (element->name == NULL) {
1110 element->name = g_strdup(type2string(element->type));
1111 if (element->name == NULL) {
1116 if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1117 element->ipv4.method = CONNMAN_IPCONFIG_METHOD_DHCP;
1119 element->parent = parent;
1121 register_element(element, NULL);
1126 static gboolean remove_element(GNode *node, gpointer user_data)
1128 struct connman_element *element = node->data;
1129 struct connman_element *root = user_data;
1131 DBG("element %p name %s", element, element->name);
1133 if (element == root)
1136 g_node_unlink(node);
1138 if (element->driver) {
1139 if (element->driver->remove)
1140 element->driver->remove(element);
1142 element->driver = NULL;
1145 g_node_destroy(node);
1147 connman_element_unref(element);
1152 struct unregister_type {
1153 struct connman_element *root;
1154 enum connman_element_type type;
1157 static gboolean remove_element_type(GNode *node, gpointer user_data)
1159 struct unregister_type *children_type = user_data;
1160 struct connman_element *root = children_type->root;
1161 struct connman_element *element = node->data;
1162 enum connman_element_type type = children_type->type;
1164 DBG("element %p name %s", element, element->name);
1166 if (element == root)
1169 if(element->type != type)
1172 g_node_unlink(node);
1174 if (element->driver) {
1175 if (element->driver->remove)
1176 element->driver->remove(element);
1178 element->driver = NULL;
1181 g_node_destroy(node);
1183 connman_element_unref(element);
1189 void connman_element_unregister(struct connman_element *element)
1193 DBG("element %p name %s", element, element->name);
1195 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1198 g_node_traverse(node, G_POST_ORDER,
1199 G_TRAVERSE_ALL, -1, remove_element, NULL);
1202 void connman_element_unregister_children(struct connman_element *element)
1206 DBG("element %p name %s", element, element->name);
1208 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1211 g_node_traverse(node, G_POST_ORDER,
1212 G_TRAVERSE_ALL, -1, remove_element, element);
1215 void connman_element_unregister_children_type(struct connman_element *element, enum connman_element_type type)
1219 DBG("element %p name %s", element, element->name);
1221 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1224 struct unregister_type children_type;
1226 children_type.root = element;
1227 children_type.type = type;
1228 g_node_traverse(node, G_POST_ORDER,
1229 G_TRAVERSE_ALL, -1, remove_element_type, &children_type);
1234 static gboolean update_element(GNode *node, gpointer user_data)
1236 struct connman_element *element = node->data;
1238 DBG("element %p name %s", element, element->name);
1240 if (element->driver && element->driver->update)
1241 element->driver->update(element);
1246 void connman_element_update(struct connman_element *element)
1250 DBG("element %p name %s", element, element->name);
1252 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1255 g_node_traverse(node, G_PRE_ORDER,
1256 G_TRAVERSE_ALL, -1, update_element, element);
1259 int connman_element_set_enabled(struct connman_element *element,
1262 if (element->enabled == enabled)
1265 element->enabled = enabled;
1267 connman_element_update(element);
1272 static enum connman_service_error convert_error(enum connman_element_error error)
1275 case CONNMAN_ELEMENT_ERROR_UNKNOWN:
1276 case CONNMAN_ELEMENT_ERROR_FAILED:
1278 case CONNMAN_ELEMENT_ERROR_DHCP_FAILED:
1279 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
1280 case CONNMAN_ELEMENT_ERROR_CONNECT_FAILED:
1281 return CONNMAN_SERVICE_ERROR_CONNECT_FAILED;
1284 return CONNMAN_SERVICE_ERROR_UNKNOWN;
1288 * connman_element_set_error:
1289 * @element: element structure
1290 * @error: error identifier
1292 * Set error state and specific error identifier
1294 void connman_element_set_error(struct connman_element *element,
1295 enum connman_element_error error)
1297 struct connman_service *service;
1299 DBG("element %p error %d", element, error);
1301 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1304 element->state = CONNMAN_ELEMENT_STATE_ERROR;
1305 element->error = error;
1307 if (element->driver && element->driver->change)
1308 element->driver->change(element);
1310 service = __connman_element_get_service(element);
1311 __connman_service_indicate_error(service, convert_error(error));
1314 void __connman_element_set_driver(struct connman_element *element)
1318 DBG("element %p name %s driver %p", element, element->name,
1321 if (element->driver)
1324 for (list = driver_list; list; list = list->next) {
1325 struct connman_driver *driver = list->data;
1327 if (match_driver(element, driver) == FALSE)
1330 element->driver = driver;
1336 int __connman_element_init(const char *device, const char *nodevice)
1338 struct connman_element *element;
1342 connection = connman_dbus_get_connection();
1343 if (connection == NULL)
1347 device_filter = g_strsplit(device, ",", -1);
1350 nodevice_filter = g_strsplit(nodevice, ",", -1);
1352 element = connman_element_create("root");
1354 element->path = g_strdup("/");
1355 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1357 element_root = g_node_new(element);
1359 __connman_technology_init();
1360 __connman_notifier_init();
1361 __connman_location_init();
1362 __connman_service_init();
1363 __connman_provider_init();
1364 __connman_network_init();
1365 __connman_device_init();
1370 static gboolean probe_node(GNode *node, gpointer data)
1372 struct connman_element *element = node->data;
1374 DBG("element %p name %s", element, element->name);
1376 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1379 if (element->driver)
1382 probe_element(element);
1387 void __connman_element_start(void)
1391 __connman_storage_init_profile();
1393 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1398 __connman_rtnl_start();
1400 __connman_connection_init();
1401 __connman_ipv4_init();
1402 __connman_dhcp_init();
1403 __connman_wpad_init();
1404 __connman_wispr_init();
1406 __connman_rfkill_init();
1409 void __connman_element_stop(void)
1413 __connman_rfkill_cleanup();
1415 __connman_wispr_cleanup();
1416 __connman_wpad_cleanup();
1417 __connman_dhcp_cleanup();
1418 __connman_ipv4_cleanup();
1419 __connman_provider_cleanup();
1420 __connman_connection_cleanup();
1423 static gboolean free_driver(GNode *node, gpointer data)
1425 struct connman_element *element = node->data;
1427 DBG("element %p name %s", element, element->name);
1429 if (element->driver) {
1430 if (element->driver->remove)
1431 element->driver->remove(element);
1433 element->driver = NULL;
1439 static gboolean free_node(GNode *node, gpointer data)
1441 struct connman_element *element = node->data;
1443 DBG("element %p name %s", element, element->name);
1445 if (g_node_depth(node) > 1)
1446 connman_element_unregister(element);
1451 void __connman_element_cleanup(void)
1455 __connman_device_cleanup();
1456 __connman_network_cleanup();
1457 __connman_service_cleanup();
1458 __connman_location_cleanup();
1459 __connman_notifier_cleanup();
1460 __connman_technology_cleanup();
1462 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1465 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1468 connman_element_unref(element_root->data);
1470 g_node_destroy(element_root);
1471 element_root = NULL;
1473 g_strfreev(nodevice_filter);
1474 g_strfreev(device_filter);
1476 if (connection == NULL)
1479 dbus_connection_unref(connection);