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
35 static DBusConnection *connection;
37 static GNode *element_root = NULL;
38 static GSList *driver_list = NULL;
39 static gchar *device_filter = NULL;
41 static gboolean started = FALSE;
43 static const char *type2string(enum connman_element_type type)
46 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
48 case CONNMAN_ELEMENT_TYPE_ROOT:
50 case CONNMAN_ELEMENT_TYPE_PROFILE:
52 case CONNMAN_ELEMENT_TYPE_DEVICE:
54 case CONNMAN_ELEMENT_TYPE_NETWORK:
56 case CONNMAN_ELEMENT_TYPE_SERVICE:
58 case CONNMAN_ELEMENT_TYPE_PPP:
60 case CONNMAN_ELEMENT_TYPE_IPV4:
62 case CONNMAN_ELEMENT_TYPE_IPV6:
64 case CONNMAN_ELEMENT_TYPE_DHCP:
66 case CONNMAN_ELEMENT_TYPE_BOOTP:
68 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
70 case CONNMAN_ELEMENT_TYPE_CONNECTION:
72 case CONNMAN_ELEMENT_TYPE_VENDOR:
79 const char *__connman_ipv4_method2string(enum connman_ipv4_method method)
82 case CONNMAN_IPV4_METHOD_UNKNOWN:
84 case CONNMAN_IPV4_METHOD_OFF:
86 case CONNMAN_IPV4_METHOD_STATIC:
88 case CONNMAN_IPV4_METHOD_DHCP:
95 enum connman_ipv4_method __connman_ipv4_string2method(const char *method)
97 if (strcasecmp(method, "off") == 0)
98 return CONNMAN_IPV4_METHOD_OFF;
99 else if (strcasecmp(method, "static") == 0)
100 return CONNMAN_IPV4_METHOD_STATIC;
101 else if (strcasecmp(method, "dhcp") == 0)
102 return CONNMAN_IPV4_METHOD_DHCP;
104 return CONNMAN_IPV4_METHOD_UNKNOWN;
107 static void emit_element_signal(DBusConnection *conn, const char *member,
108 struct connman_element *element)
112 if (__connman_debug_enabled() == FALSE)
115 DBG("conn %p member %s", conn, member);
120 signal = dbus_message_new_signal(element->path,
121 CONNMAN_DEBUG_INTERFACE, member);
125 g_dbus_send_message(conn, signal);
128 struct foreach_data {
129 enum connman_element_type type;
130 element_cb_t callback;
134 static gboolean foreach_callback(GNode *node, gpointer user_data)
136 struct connman_element *element = node->data;
137 struct foreach_data *data = user_data;
139 DBG("element %p name %s", element, element->name);
141 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
144 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
145 data->type != element->type)
149 data->callback(element, data->user_data);
154 void __connman_element_foreach(struct connman_element *element,
155 enum connman_element_type type,
156 element_cb_t callback, gpointer user_data)
158 struct foreach_data data = { type, callback, user_data };
163 if (element != NULL) {
164 node = g_node_find(element_root, G_PRE_ORDER,
165 G_TRAVERSE_ALL, element);
171 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
172 foreach_callback, &data);
175 struct append_filter {
176 enum connman_element_type type;
177 DBusMessageIter *iter;
180 static gboolean append_path(GNode *node, gpointer user_data)
182 struct connman_element *element = node->data;
183 struct append_filter *filter = user_data;
185 DBG("element %p name %s", element, element->name);
187 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
190 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
191 filter->type != element->type)
194 if (filter->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
195 __connman_device_has_driver(element->device) == FALSE)
198 if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
199 __connman_network_has_driver(element->network) == FALSE)
202 dbus_message_iter_append_basic(filter->iter,
203 DBUS_TYPE_OBJECT_PATH, &element->path);
208 void __connman_element_list(struct connman_element *element,
209 enum connman_element_type type,
210 DBusMessageIter *iter)
212 struct append_filter filter = { type, iter };
217 if (element != NULL) {
218 node = g_node_find(element_root, G_PRE_ORDER,
219 G_TRAVERSE_ALL, element);
225 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
226 append_path, &filter);
230 enum connman_element_type type;
234 static gboolean count_element(GNode *node, gpointer user_data)
236 struct connman_element *element = node->data;
237 struct count_data *data = user_data;
239 DBG("element %p name %s", element, element->name);
241 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
244 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
245 data->type != element->type)
253 int __connman_element_count(struct connman_element *element,
254 enum connman_element_type type)
256 struct count_data data = { type, 0 };
261 if (element != NULL) {
262 node = g_node_find(element_root, G_PRE_ORDER,
263 G_TRAVERSE_ALL, element);
269 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
270 count_element, &data);
275 static struct connman_network *__connman_element_get_network(struct connman_element *element)
277 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
278 element->network != NULL)
279 return element->network;
281 if (element->parent == NULL)
284 return __connman_element_get_network(element->parent);
287 struct connman_service *__connman_element_get_service(struct connman_element *element)
289 struct connman_service *service = NULL;
290 struct connman_network *network;
291 struct connman_device *device;
292 enum connman_device_type type;
294 device = __connman_element_get_device(element);
298 type = connman_device_get_type(device);
301 case CONNMAN_DEVICE_TYPE_UNKNOWN:
302 case CONNMAN_DEVICE_TYPE_VENDOR:
303 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
304 case CONNMAN_DEVICE_TYPE_GPS:
305 case CONNMAN_DEVICE_TYPE_HSO:
306 case CONNMAN_DEVICE_TYPE_NOZOMI:
307 case CONNMAN_DEVICE_TYPE_HUAWEI:
308 case CONNMAN_DEVICE_TYPE_NOVATEL:
310 case CONNMAN_DEVICE_TYPE_ETHERNET:
311 service = __connman_service_lookup_from_device(device);
313 case CONNMAN_DEVICE_TYPE_WIFI:
314 case CONNMAN_DEVICE_TYPE_WIMAX:
315 network = __connman_element_get_network(element);
318 service = __connman_service_lookup_from_network(network);
325 struct connman_device *__connman_element_get_device(struct connman_element *element)
327 if (element->type == CONNMAN_ELEMENT_TYPE_DEVICE &&
328 element->device != NULL)
329 return element->device;
331 if (element->parent == NULL)
334 return __connman_element_get_device(element->parent);
337 const char *__connman_element_get_device_path(struct connman_element *element)
339 struct connman_device *device;
341 device = __connman_element_get_device(element);
345 return connman_device_get_path(device);
348 const char *__connman_element_get_network_path(struct connman_element *element)
350 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
351 element->network != NULL)
352 return element->path;
354 if (element->parent == NULL)
357 return __connman_element_get_network_path(element->parent);
360 static gint compare_priority(gconstpointer a, gconstpointer b)
362 const struct connman_driver *driver1 = a;
363 const struct connman_driver *driver2 = b;
365 return driver2->priority - driver1->priority;
368 static gboolean match_driver(struct connman_element *element,
369 struct connman_driver *driver)
371 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
374 if (element->type == driver->type ||
375 driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
381 static gboolean probe_driver(GNode *node, gpointer data)
383 struct connman_element *element = node->data;
384 struct connman_driver *driver = data;
386 DBG("element %p name %s", element, element->name);
388 if (!element->driver && match_driver(element, driver) == TRUE) {
389 if (driver->probe(element) < 0)
392 __connman_element_lock(element);
393 element->driver = driver;
394 __connman_element_unlock(element);
400 void __connman_driver_rescan(struct connman_driver *driver)
402 DBG("driver %p name %s", driver, driver->name);
407 if (element_root != NULL)
408 g_node_traverse(element_root, G_PRE_ORDER,
409 G_TRAVERSE_ALL, -1, probe_driver, driver);
413 * connman_driver_register:
414 * @driver: driver definition
416 * Register a new driver
418 * Returns: %0 on success
420 int connman_driver_register(struct connman_driver *driver)
422 DBG("driver %p name %s", driver, driver->name);
424 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
430 driver_list = g_slist_insert_sorted(driver_list, driver,
433 if (started == FALSE)
436 if (element_root != NULL)
437 g_node_traverse(element_root, G_PRE_ORDER,
438 G_TRAVERSE_ALL, -1, probe_driver, driver);
443 static gboolean remove_driver(GNode *node, gpointer data)
445 struct connman_element *element = node->data;
446 struct connman_driver *driver = data;
448 DBG("element %p name %s", element, element->name);
450 if (element->driver == driver) {
452 driver->remove(element);
454 __connman_element_lock(element);
455 element->driver = NULL;
456 __connman_element_unlock(element);
463 * connman_driver_unregister:
464 * @driver: driver definition
466 * Remove a previously registered driver
468 void connman_driver_unregister(struct connman_driver *driver)
470 DBG("driver %p name %s", driver, driver->name);
472 driver_list = g_slist_remove(driver_list, driver);
474 if (element_root != NULL)
475 g_node_traverse(element_root, G_POST_ORDER,
476 G_TRAVERSE_ALL, -1, remove_driver, driver);
479 static void unregister_property(gpointer data)
481 struct connman_property *property = data;
483 DBG("property %p", property);
485 g_free(property->value);
489 void __connman_element_initialize(struct connman_element *element)
491 DBG("element %p", element);
493 element->refcount = 1;
495 element->name = NULL;
496 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
497 element->state = CONNMAN_ELEMENT_STATE_UNKNOWN;
498 element->error = CONNMAN_ELEMENT_ERROR_UNKNOWN;
500 element->enabled = FALSE;
502 element->configuring = FALSE;
504 element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
505 g_free, unregister_property);
509 * connman_element_create:
510 * @name: element name
512 * Allocate a new element and assign the given #name to it. If the name
513 * is #NULL, it will be later on created based on the element type.
515 * Returns: a newly-allocated #connman_element structure
517 struct connman_element *connman_element_create(const char *name)
519 struct connman_element *element;
521 element = g_try_new0(struct connman_element, 1);
525 DBG("element %p", element);
527 __connman_element_initialize(element);
532 struct connman_element *connman_element_ref(struct connman_element *element)
534 DBG("element %p name %s refcount %d", element, element->name,
535 g_atomic_int_get(&element->refcount) + 1);
537 g_atomic_int_inc(&element->refcount);
542 static void free_properties(struct connman_element *element)
544 DBG("element %p name %s", element, element->name);
546 __connman_element_lock(element);
548 g_hash_table_destroy(element->properties);
549 element->properties = NULL;
551 __connman_element_unlock(element);
554 void connman_element_unref(struct connman_element *element)
556 DBG("element %p name %s refcount %d", element, element->name,
557 g_atomic_int_get(&element->refcount) - 1);
559 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
560 if (element->destruct)
561 element->destruct(element);
562 free_properties(element);
563 g_free(element->ipv4.address);
564 g_free(element->ipv4.netmask);
565 g_free(element->ipv4.gateway);
566 g_free(element->ipv4.network);
567 g_free(element->ipv4.broadcast);
568 g_free(element->ipv4.nameserver);
569 g_free(element->devname);
570 g_free(element->path);
571 g_free(element->name);
576 static int set_static_property(struct connman_element *element,
577 const char *name, int type, const void *value)
579 struct connman_property *property;
581 DBG("element %p name %s", element, element->name);
583 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
586 property = g_try_new0(struct connman_property, 1);
587 if (property == NULL)
590 property->id = CONNMAN_PROPERTY_ID_INVALID;
591 property->type = type;
593 DBG("name %s type %d value %p", name, type, value);
596 case DBUS_TYPE_STRING:
597 property->value = g_strdup(*((const char **) value));
600 property->value = g_try_malloc(1);
601 if (property->value != NULL)
602 memcpy(property->value, value, 1);
606 __connman_element_lock(element);
608 g_hash_table_replace(element->properties, g_strdup(name), property);
610 __connman_element_unlock(element);
615 static int set_static_array_property(struct connman_element *element,
616 const char *name, int type, const void *value, int len)
618 struct connman_property *property;
620 DBG("element %p name %s", element, element->name);
622 if (type != DBUS_TYPE_BYTE)
625 property = g_try_new0(struct connman_property, 1);
626 if (property == NULL)
629 property->id = CONNMAN_PROPERTY_ID_INVALID;
630 property->type = DBUS_TYPE_ARRAY;
631 property->subtype = type;
633 DBG("name %s type %d value %p", name, type, value);
637 property->value = g_try_malloc(len);
638 if (property->value != NULL) {
639 memcpy(property->value,
640 *((const unsigned char **) value), len);
641 property->size = len;
646 __connman_element_lock(element);
648 g_hash_table_replace(element->properties, g_strdup(name), property);
650 __connman_element_unlock(element);
656 static int set_property(struct connman_element *element,
657 enum connman_property_id id, const void *value)
660 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
661 __connman_element_lock(element);
662 g_free(element->ipv4.address);
663 element->ipv4.address = g_strdup(*((const char **) value));
664 __connman_element_unlock(element);
666 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
667 __connman_element_lock(element);
668 g_free(element->ipv4.netmask);
669 element->ipv4.netmask = g_strdup(*((const char **) value));
670 __connman_element_unlock(element);
672 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
673 __connman_element_lock(element);
674 g_free(element->ipv4.gateway);
675 element->ipv4.gateway = g_strdup(*((const char **) value));
676 __connman_element_unlock(element);
678 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
679 __connman_element_lock(element);
680 g_free(element->ipv4.broadcast);
681 element->ipv4.broadcast = g_strdup(*((const char **) value));
682 __connman_element_unlock(element);
684 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
685 __connman_element_lock(element);
686 g_free(element->ipv4.nameserver);
687 element->ipv4.nameserver = g_strdup(*((const char **) value));
688 __connman_element_unlock(element);
698 int connman_element_get_value(struct connman_element *element,
699 enum connman_property_id id, void *value)
701 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
705 case CONNMAN_PROPERTY_ID_IPV4_METHOD:
706 if (element->ipv4.method == CONNMAN_IPV4_METHOD_UNKNOWN)
707 return connman_element_get_value(element->parent,
709 __connman_element_lock(element);
710 *((const char **) value) = __connman_ipv4_method2string(element->ipv4.method);
711 __connman_element_unlock(element);
713 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
714 if (element->ipv4.address == NULL)
715 return connman_element_get_value(element->parent,
717 __connman_element_lock(element);
718 *((char **) value) = element->ipv4.address;
719 __connman_element_unlock(element);
721 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
722 if (element->ipv4.netmask == NULL)
723 return connman_element_get_value(element->parent,
725 __connman_element_lock(element);
726 *((char **) value) = element->ipv4.netmask;
727 __connman_element_unlock(element);
729 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
730 if (element->ipv4.gateway == NULL)
731 return connman_element_get_value(element->parent,
733 __connman_element_lock(element);
734 *((char **) value) = element->ipv4.gateway;
735 __connman_element_unlock(element);
737 case CONNMAN_PROPERTY_ID_IPV4_BROADCAST:
738 if (element->ipv4.broadcast == NULL)
739 return connman_element_get_value(element->parent,
741 __connman_element_lock(element);
742 *((char **) value) = element->ipv4.broadcast;
743 __connman_element_unlock(element);
745 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
746 if (element->ipv4.nameserver == NULL)
747 return connman_element_get_value(element->parent,
749 __connman_element_lock(element);
750 *((char **) value) = element->ipv4.nameserver;
751 __connman_element_unlock(element);
760 static gboolean get_static_property(struct connman_element *element,
761 const char *name, void *value)
763 struct connman_property *property;
764 gboolean found = FALSE;
766 DBG("element %p name %s", element, element->name);
768 __connman_element_lock(element);
770 property = g_hash_table_lookup(element->properties, name);
771 if (property != NULL) {
772 switch (property->type) {
773 case DBUS_TYPE_STRING:
774 *((char **) value) = property->value;
778 memcpy(value, property->value, 1);
784 __connman_element_unlock(element);
786 if (found == FALSE && element->parent != NULL)
787 return get_static_property(element->parent, name, value);
792 static gboolean get_static_array_property(struct connman_element *element,
793 const char *name, void *value, unsigned int *len)
795 struct connman_property *property;
796 gboolean found = FALSE;
798 DBG("element %p name %s", element, element->name);
800 __connman_element_lock(element);
802 property = g_hash_table_lookup(element->properties, name);
803 if (property != NULL) {
804 *((char **) value) = property->value;
805 *len = property->size;
809 __connman_element_unlock(element);
815 static gboolean match_static_property(struct connman_element *element,
816 const char *name, const void *value)
818 struct connman_property *property;
819 gboolean result = FALSE;
821 DBG("element %p name %s", element, element->name);
823 __connman_element_lock(element);
825 property = g_hash_table_lookup(element->properties, name);
826 if (property != NULL) {
827 if (property->type == DBUS_TYPE_STRING)
828 result = g_str_equal(property->value,
829 *((const char **) value));
832 __connman_element_unlock(element);
839 * connman_element_set_string:
840 * @element: element structure
841 * @key: unique identifier
842 * @value: string value
844 * Set string value for specific key
846 int connman_element_set_string(struct connman_element *element,
847 const char *key, const char *value)
849 return set_static_property(element, key, DBUS_TYPE_STRING, &value);
853 * connman_element_get_string:
854 * @element: element structure
855 * @key: unique identifier
857 * Get string value for specific key
859 const char *connman_element_get_string(struct connman_element *element,
864 if (get_static_property(element, key, &value) == FALSE)
871 * connman_element_set_uint8:
872 * @element: element structure
873 * @key: unique identifier
874 * @value: integer value
876 * Set integer value for specific key
878 int connman_element_set_uint8(struct connman_element *element,
879 const char *key, connman_uint8_t value)
881 return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
885 * connman_element_get_uint8:
886 * @element: element structure
887 * @key: unique identifier
889 * Get integer value for specific key
891 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
894 connman_uint8_t value;
896 if (get_static_property(element, key, &value) == FALSE)
903 * connman_element_set_blob:
904 * @element: element structure
905 * @key: unique identifier
909 * Set binary blob value for specific key
911 int connman_element_set_blob(struct connman_element *element,
912 const char *key, const void *data, unsigned int size)
914 return set_static_array_property(element, key,
915 DBUS_TYPE_BYTE, &data, size);
919 * connman_element_get_blob:
920 * @element: element structure
921 * @key: unique identifier
922 * @size: pointer to blob size
924 * Get binary blob value for specific key
926 const void *connman_element_get_blob(struct connman_element *element,
927 const char *key, unsigned int *size)
931 if (get_static_array_property(element, key, &value, size) == FALSE)
937 int __connman_element_append_ipv4(struct connman_element *element,
938 DBusMessageIter *dict)
940 const char *method = NULL;
941 const char *address = NULL, *netmask = NULL, *gateway = NULL;
943 connman_element_get_value(element,
944 CONNMAN_PROPERTY_ID_IPV4_METHOD, &method);
946 connman_element_get_value(element,
947 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &address);
948 connman_element_get_value(element,
949 CONNMAN_PROPERTY_ID_IPV4_NETMASK, &netmask);
950 connman_element_get_value(element,
951 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
954 connman_dbus_dict_append_variant(dict, "IPv4.Method",
955 DBUS_TYPE_STRING, &method);
958 connman_dbus_dict_append_variant(dict, "IPv4.Address",
959 DBUS_TYPE_STRING, &address);
962 connman_dbus_dict_append_variant(dict, "IPv4.Netmask",
963 DBUS_TYPE_STRING, &netmask);
966 connman_dbus_dict_append_variant(dict, "IPv4.Gateway",
967 DBUS_TYPE_STRING, &gateway);
972 int __connman_element_set_ipv4(struct connman_element *element,
973 const char *name, DBusMessageIter *value)
977 type = dbus_message_iter_get_arg_type(value);
979 if (g_str_equal(name, "IPv4.Method") == TRUE) {
980 enum connman_ipv4_method method;
983 if (type != DBUS_TYPE_STRING)
986 dbus_message_iter_get_basic(value, &str);
987 method = __connman_ipv4_string2method(str);
988 if (method == CONNMAN_IPV4_METHOD_UNKNOWN)
991 if (method == element->ipv4.method)
994 element->ipv4.method = method;
996 connman_element_update(element);
997 } else if (g_str_equal(name, "IPv4.Address") == TRUE) {
1000 if (type != DBUS_TYPE_STRING)
1003 dbus_message_iter_get_basic(value, &address);
1005 g_free(element->ipv4.address);
1006 element->ipv4.address = g_strdup(address);
1008 connman_element_update(element);
1009 } else if (g_str_equal(name, "IPv4.Netmask") == TRUE) {
1010 const char *netmask;
1012 if (type != DBUS_TYPE_STRING)
1015 dbus_message_iter_get_basic(value, &netmask);
1017 g_free(element->ipv4.netmask);
1018 element->ipv4.netmask = g_strdup(netmask);
1020 connman_element_update(element);
1021 } else if (g_str_equal(name, "IPv4.Gateway") == TRUE) {
1022 const char *gateway;
1024 if (type != DBUS_TYPE_STRING)
1027 dbus_message_iter_get_basic(value, &gateway);
1029 g_free(element->ipv4.gateway);
1030 element->ipv4.gateway = g_strdup(gateway);
1032 connman_element_update(element);
1038 static void append_state(DBusMessageIter *entry, const char *state)
1040 DBusMessageIter value;
1041 const char *key = "State";
1043 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
1045 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
1046 DBUS_TYPE_STRING_AS_STRING, &value);
1047 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &state);
1048 dbus_message_iter_close_container(entry, &value);
1051 static void emit_state_change(DBusConnection *conn, const char *state)
1053 DBusMessage *signal;
1054 DBusMessageIter entry;
1056 DBG("conn %p", conn);
1058 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
1059 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
1063 dbus_message_iter_init_append(signal, &entry);
1065 append_state(&entry, state);
1067 g_dbus_send_message(conn, signal);
1070 static void probe_element(struct connman_element *element)
1074 DBG("element %p name %s", element, element->name);
1076 for (list = driver_list; list; list = list->next) {
1077 struct connman_driver *driver = list->data;
1079 if (match_driver(element, driver) == FALSE)
1082 DBG("driver %p name %s", driver, driver->name);
1084 if (driver->probe(element) == 0) {
1085 __connman_element_lock(element);
1086 element->driver = driver;
1087 __connman_element_unlock(element);
1093 static void register_element(gpointer data, gpointer user_data)
1095 struct connman_element *element = data;
1096 const gchar *basepath;
1099 __connman_element_lock(element);
1101 if (element->parent) {
1102 node = g_node_find(element_root, G_PRE_ORDER,
1103 G_TRAVERSE_ALL, element->parent);
1104 basepath = element->parent->path;
1106 element->parent = element_root->data;
1108 node = element_root;
1112 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1114 __connman_element_unlock(element);
1116 DBG("element %p path %s", element, element->path);
1118 g_node_append_data(node, element);
1120 if (element->type == CONNMAN_ELEMENT_TYPE_DHCP) {
1121 element->parent->configuring = TRUE;
1123 if (__connman_element_count(NULL,
1124 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1125 emit_state_change(connection, "connecting");
1128 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1129 struct connman_element *parent = element->parent;
1132 parent->configuring = FALSE;
1133 parent = parent->parent;
1136 if (__connman_element_count(NULL,
1137 CONNMAN_ELEMENT_TYPE_CONNECTION) == 1)
1138 emit_state_change(connection, "online");
1141 emit_element_signal(connection, "ElementAdded", element);
1143 if (started == FALSE)
1146 probe_element(element);
1150 * connman_element_register:
1151 * @element: the element to register
1152 * @parent: the parent to register the element with
1154 * Register an element with the core. It will be register under the given
1155 * parent of if %NULL is provided under the root element.
1157 * Returns: %0 on success
1159 int connman_element_register(struct connman_element *element,
1160 struct connman_element *parent)
1162 DBG("element %p name %s parent %p", element, element->name, parent);
1164 if (element->devname == NULL)
1165 element->devname = g_strdup(element->name);
1167 if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE) {
1168 if (g_pattern_match_simple(device_filter,
1169 element->devname) == FALSE) {
1170 DBG("ignoring %s [%s] device", element->name,
1176 if (connman_element_ref(element) == NULL)
1179 __connman_element_lock(element);
1181 if (element->name == NULL) {
1182 element->name = g_strdup(type2string(element->type));
1183 if (element->name == NULL) {
1184 __connman_element_unlock(element);
1189 if (element->type == CONNMAN_ELEMENT_TYPE_DHCP)
1190 element->ipv4.method = CONNMAN_IPV4_METHOD_DHCP;
1192 element->parent = parent;
1194 __connman_element_unlock(element);
1196 register_element(element, NULL);
1201 static gboolean remove_element(GNode *node, gpointer user_data)
1203 struct connman_element *element = node->data;
1204 struct connman_element *root = user_data;
1206 DBG("element %p name %s", element, element->name);
1208 if (element == root)
1212 g_node_unlink(node);
1214 if (element->driver) {
1215 if (element->driver->remove)
1216 element->driver->remove(element);
1218 __connman_element_lock(element);
1219 element->driver = NULL;
1220 __connman_element_unlock(element);
1224 g_node_destroy(node);
1226 if (element->type == CONNMAN_ELEMENT_TYPE_CONNECTION) {
1227 if (__connman_element_count(NULL,
1228 CONNMAN_ELEMENT_TYPE_CONNECTION) == 0)
1229 emit_state_change(connection, "offline");
1232 emit_element_signal(connection, "ElementRemoved", element);
1234 connman_element_unref(element);
1239 void connman_element_unregister(struct connman_element *element)
1243 DBG("element %p name %s", element, element->name);
1245 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1248 g_node_traverse(node, G_POST_ORDER,
1249 G_TRAVERSE_ALL, -1, remove_element, NULL);
1252 void connman_element_unregister_children(struct connman_element *element)
1256 DBG("element %p name %s", element, element->name);
1258 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1261 g_node_traverse(node, G_POST_ORDER,
1262 G_TRAVERSE_ALL, -1, remove_element, element);
1265 static gboolean update_element(GNode *node, gpointer user_data)
1267 struct connman_element *element = node->data;
1269 DBG("element %p name %s", element, element->name);
1271 if (element->driver && element->driver->update)
1272 element->driver->update(element);
1274 emit_element_signal(connection, "ElementUpdated", element);
1279 void connman_element_update(struct connman_element *element)
1283 DBG("element %p name %s", element, element->name);
1285 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1288 g_node_traverse(node, G_PRE_ORDER,
1289 G_TRAVERSE_ALL, -1, update_element, element);
1292 int connman_element_set_enabled(struct connman_element *element,
1295 if (element->enabled == enabled)
1298 element->enabled = enabled;
1300 connman_element_update(element);
1306 * connman_element_set_error:
1307 * @element: element structure
1308 * @error: error identifier
1310 * Set error state and specific error identifier
1312 void connman_element_set_error(struct connman_element *element,
1313 enum connman_element_error error)
1315 struct connman_service *service;
1317 DBG("element %p error %d", element, error);
1319 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1322 element->state = CONNMAN_ELEMENT_STATE_ERROR;
1323 element->error = error;
1325 if (element->driver && element->driver->change)
1326 element->driver->change(element);
1328 service = __connman_element_get_service(element);
1329 __connman_service_indicate_state(service,
1330 CONNMAN_SERVICE_STATE_FAILURE);
1333 int __connman_element_init(DBusConnection *conn, const char *device,
1334 const char *nodevice)
1336 struct connman_element *element;
1338 DBG("conn %p", conn);
1340 connection = dbus_connection_ref(conn);
1341 if (connection == NULL)
1344 device_filter = g_strdup(device);
1346 element = connman_element_create("root");
1348 element->path = g_strdup("/");
1349 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1351 element_root = g_node_new(element);
1353 __connman_notifier_init();
1354 __connman_service_init();
1355 __connman_network_init();
1356 __connman_device_init();
1361 static gboolean probe_node(GNode *node, gpointer data)
1363 struct connman_element *element = node->data;
1365 DBG("element %p name %s", element, element->name);
1367 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
1370 if (element->driver)
1373 probe_element(element);
1378 void __connman_element_start(void)
1382 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
1387 __connman_storage_init_device();
1389 __connman_connection_init();
1390 __connman_ipv4_init();
1391 __connman_detect_init();
1394 void __connman_element_stop(void)
1398 __connman_detect_cleanup();
1399 __connman_ipv4_cleanup();
1400 __connman_connection_cleanup();
1403 static gboolean free_driver(GNode *node, gpointer data)
1405 struct connman_element *element = node->data;
1407 DBG("element %p name %s", element, element->name);
1409 if (element->driver) {
1410 if (element->driver->remove)
1411 element->driver->remove(element);
1413 __connman_element_lock(element);
1414 element->driver = NULL;
1415 __connman_element_unlock(element);
1421 static gboolean free_node(GNode *node, gpointer data)
1423 struct connman_element *element = node->data;
1425 DBG("element %p name %s", element, element->name);
1427 if (g_node_depth(node) > 1)
1428 connman_element_unregister(element);
1433 void __connman_element_cleanup(void)
1437 __connman_device_cleanup();
1438 __connman_network_cleanup();
1439 __connman_service_cleanup();
1440 __connman_notifier_cleanup();
1442 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1445 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1448 g_node_destroy(element_root);
1449 element_root = NULL;
1451 g_free(device_filter);
1453 dbus_connection_unref(connection);