5 * Copyright (C) 2007-2008 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
34 static DBusConnection *connection;
36 static GStaticRWLock element_lock = G_STATIC_RW_LOCK_INIT;
37 static GNode *element_root = NULL;
39 static GSList *driver_list = NULL;
41 static GThreadPool *thread_register = NULL;
42 static GThreadPool *thread_unregister = NULL;
43 static GThreadPool *thread_unregister_children = NULL;
45 static gchar *device_filter = NULL;
48 enum connman_property_id id;
53 { CONNMAN_PROPERTY_ID_IPV4_METHOD,
54 DBUS_TYPE_STRING, "IPv4.Method", "dhcp" },
55 { CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
56 DBUS_TYPE_STRING, "IPv4.Address" },
57 { CONNMAN_PROPERTY_ID_IPV4_NETMASK,
58 DBUS_TYPE_STRING, "IPv4.Netmask" },
59 { CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
60 DBUS_TYPE_STRING, "IPv4.Gateway" },
61 { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
62 DBUS_TYPE_STRING, "IPv4.Nameserver" },
66 static int propid2type(enum connman_property_id id)
70 for (i = 0; propid_table[i].name; i++) {
71 if (propid_table[i].id == id)
72 return propid_table[i].type;
75 return DBUS_TYPE_INVALID;
78 static const char *propid2name(enum connman_property_id id)
82 for (i = 0; propid_table[i].name; i++) {
83 if (propid_table[i].id == id)
84 return propid_table[i].name;
90 static const char *type2string(enum connman_element_type type)
93 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
95 case CONNMAN_ELEMENT_TYPE_ROOT:
97 case CONNMAN_ELEMENT_TYPE_DEVICE:
99 case CONNMAN_ELEMENT_TYPE_NETWORK:
101 case CONNMAN_ELEMENT_TYPE_IPV4:
103 case CONNMAN_ELEMENT_TYPE_IPV6:
105 case CONNMAN_ELEMENT_TYPE_DHCP:
107 case CONNMAN_ELEMENT_TYPE_BOOTP:
109 case CONNMAN_ELEMENT_TYPE_ZEROCONF:
111 case CONNMAN_ELEMENT_TYPE_RESOLVER:
113 case CONNMAN_ELEMENT_TYPE_INTERNET:
120 static const char *subtype2string(enum connman_element_subtype type)
123 case CONNMAN_ELEMENT_SUBTYPE_UNKNOWN:
125 case CONNMAN_ELEMENT_SUBTYPE_NETWORK:
127 case CONNMAN_ELEMENT_SUBTYPE_ETHERNET:
129 case CONNMAN_ELEMENT_SUBTYPE_WIFI:
131 case CONNMAN_ELEMENT_SUBTYPE_WIMAX:
133 case CONNMAN_ELEMENT_SUBTYPE_MODEM:
135 case CONNMAN_ELEMENT_SUBTYPE_BLUETOOTH:
142 static void append_entry(DBusMessageIter *dict,
143 const char *key, int type, void *val)
145 DBusMessageIter entry, value;
146 const char *signature;
148 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
151 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
154 case DBUS_TYPE_BOOLEAN:
155 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
157 case DBUS_TYPE_STRING:
158 signature = DBUS_TYPE_STRING_AS_STRING;
160 case DBUS_TYPE_UINT16:
161 signature = DBUS_TYPE_UINT16_AS_STRING;
163 case DBUS_TYPE_UINT32:
164 signature = DBUS_TYPE_UINT32_AS_STRING;
166 case DBUS_TYPE_OBJECT_PATH:
167 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
170 signature = DBUS_TYPE_VARIANT_AS_STRING;
174 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
176 dbus_message_iter_append_basic(&value, type, val);
177 dbus_message_iter_close_container(&entry, &value);
179 dbus_message_iter_close_container(dict, &entry);
182 static void append_property(DBusMessageIter *dict,
183 struct connman_property *property)
185 if (property->value == NULL)
188 append_entry(dict, property->name, property->type, &property->value);
191 static DBusMessage *get_properties(DBusConnection *conn,
192 DBusMessage *msg, void *data)
194 struct connman_element *element = data;
197 DBusMessageIter array, dict;
200 DBG("conn %p", conn);
202 reply = dbus_message_new_method_return(msg);
206 dbus_message_iter_init_append(reply, &array);
208 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
209 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
210 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
211 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
213 if (element->parent != NULL &&
214 element->parent->type != CONNMAN_ELEMENT_TYPE_ROOT) {
215 append_entry(&dict, "Parent",
216 DBUS_TYPE_OBJECT_PATH, &element->parent->path);
219 str = type2string(element->type);
221 append_entry(&dict, "Type", DBUS_TYPE_STRING, &str);
222 str = subtype2string(element->subtype);
224 append_entry(&dict, "Subtype", DBUS_TYPE_STRING, &str);
226 append_entry(&dict, "Enabled", DBUS_TYPE_BOOLEAN, &element->enabled);
228 if (element->priority > 0)
229 append_entry(&dict, "Priority",
230 DBUS_TYPE_UINT16, &element->priority);
232 if (element->network.identifier != NULL)
233 append_entry(&dict, "Identifier",
234 DBUS_TYPE_STRING, &element->network.identifier);
236 if (element->ipv4.address != NULL)
237 append_entry(&dict, "IPv4.Address",
238 DBUS_TYPE_STRING, &element->ipv4.address);
239 if (element->ipv4.netmask != NULL)
240 append_entry(&dict, "IPv4.Netmask",
241 DBUS_TYPE_STRING, &element->ipv4.netmask);
242 if (element->ipv4.gateway != NULL)
243 append_entry(&dict, "IPv4.Gateway",
244 DBUS_TYPE_STRING, &element->ipv4.gateway);
246 connman_element_lock(element);
248 for (list = element->properties; list; list = list->next) {
249 struct connman_property *property = list->data;
251 append_property(&dict, property);
254 connman_element_unlock(element);
256 dbus_message_iter_close_container(&array, &dict);
261 static DBusMessage *set_property(DBusConnection *conn,
262 DBusMessage *msg, void *data)
264 struct connman_element *element = data;
265 DBusMessageIter iter;
266 DBusMessageIter value;
270 DBG("conn %p", conn);
272 if (dbus_message_iter_init(msg, &iter) == FALSE)
273 return __connman_error_invalid_arguments(msg);
275 dbus_message_iter_get_basic(&iter, &name);
276 dbus_message_iter_next(&iter);
277 dbus_message_iter_recurse(&iter, &value);
279 if (__connman_security_check_privileges(msg) < 0)
280 return __connman_error_permission_denied(msg);
282 connman_element_lock(element);
284 for (list = element->properties; list; list = list->next) {
285 struct connman_property *property = list->data;
288 if (g_str_equal(property->name, name) == FALSE)
291 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
294 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
296 if (property->type == DBUS_TYPE_STRING) {
297 dbus_message_iter_get_basic(&value, &str);
298 g_free(property->value);
299 property->value = g_strdup(str);
301 property->value = NULL;
304 connman_element_unlock(element);
306 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
309 static DBusMessage *clear_property(DBusConnection *conn,
310 DBusMessage *msg, void *data)
312 struct connman_element *element = data;
316 DBG("conn %p", conn);
318 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
319 DBUS_TYPE_INVALID) == FALSE)
320 return __connman_error_invalid_arguments(msg);
322 if (__connman_security_check_privileges(msg) < 0)
323 return __connman_error_permission_denied(msg);
325 connman_element_lock(element);
327 for (list = element->properties; list; list = list->next) {
328 struct connman_property *property = list->data;
330 if (g_str_equal(property->name, name) == FALSE)
333 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
336 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
339 property->flags |= CONNMAN_PROPERTY_FLAG_REFERENCE;
341 if (property->type == DBUS_TYPE_STRING)
342 g_free(property->value);
344 property->value = NULL;
347 connman_element_unlock(element);
349 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
352 static DBusMessage *do_update(DBusConnection *conn,
353 DBusMessage *msg, void *data)
355 struct connman_element *element = data;
357 DBG("conn %p", conn);
359 if (element->driver == NULL)
360 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
362 if (element->enabled == FALSE)
363 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
365 if (element->driver->update) {
366 DBG("Calling update callback");
367 element->driver->update(element);
370 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
373 static DBusMessage *do_enable(DBusConnection *conn,
374 DBusMessage *msg, void *data)
376 struct connman_element *element = data;
378 DBG("conn %p", conn);
380 if (element->driver == NULL)
381 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
383 if (element->enabled == TRUE)
384 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
386 if (element->driver->enable) {
387 DBG("Calling enable callback");
388 if (element->driver->enable(element) == 0) {
389 element->enabled = TRUE;
391 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
392 CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
393 DBUS_TYPE_OBJECT_PATH, &element->path,
398 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
401 static DBusMessage *do_disable(DBusConnection *conn,
402 DBusMessage *msg, void *data)
404 struct connman_element *element = data;
406 DBG("conn %p", conn);
408 if (element->driver == NULL)
409 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
411 if (element->enabled == FALSE)
412 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
414 if (element->driver->disable) {
415 DBG("Calling disable callback");
416 if (element->driver->disable(element) == 0) {
417 element->enabled = FALSE;
419 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
420 CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
421 DBUS_TYPE_OBJECT_PATH, &element->path,
426 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
429 static GDBusMethodTable element_methods[] = {
430 { "GetProperties", "", "a{sv}", get_properties },
431 { "SetProperty", "sv", "", set_property },
432 { "ClearProperty", "s", "", clear_property },
433 { "Update", "", "", do_update },
434 { "Enable", "", "", do_enable },
435 { "Disable", "", "", do_disable },
439 static GDBusSignalTable element_signals[] = {
440 { "PropertyChanged", "sv" },
444 struct append_filter {
445 enum connman_element_type type;
446 DBusMessageIter *iter;
449 static gboolean append_path(GNode *node, gpointer data)
451 struct connman_element *element = node->data;
452 struct append_filter *filter = data;
454 DBG("element %p name %s", element, element->name);
456 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
459 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
460 filter->type != element->type)
463 dbus_message_iter_append_basic(filter->iter,
464 DBUS_TYPE_OBJECT_PATH, &element->path);
469 void __connman_element_list(enum connman_element_type type,
470 DBusMessageIter *iter)
472 struct append_filter filter = { type, iter };
476 g_static_rw_lock_reader_lock(&element_lock);
477 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
478 append_path, &filter);
479 g_static_rw_lock_reader_unlock(&element_lock);
482 static gint compare_priority(gconstpointer a, gconstpointer b)
484 const struct connman_driver *driver1 = a;
485 const struct connman_driver *driver2 = b;
487 return driver2->priority - driver1->priority;
490 static gboolean match_driver(struct connman_element *element,
491 struct connman_driver *driver)
493 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
496 if (element->type != driver->type &&
497 driver->type != CONNMAN_ELEMENT_TYPE_UNKNOWN)
500 if (element->subtype == driver->subtype ||
501 driver->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
507 static gboolean probe_driver(GNode *node, gpointer data)
509 struct connman_element *element = node->data;
510 struct connman_driver *driver = data;
512 DBG("element %p name %s", element, element->name);
514 if (!element->driver && match_driver(element, driver) == TRUE) {
515 if (driver->probe(element) < 0)
518 connman_element_lock(element);
519 element->driver = driver;
520 connman_element_unlock(element);
527 * connman_driver_register:
528 * @driver: driver definition
530 * Register a new driver
532 * Returns: %0 on success
534 int connman_driver_register(struct connman_driver *driver)
536 DBG("driver %p name %s", driver, driver->name);
538 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
544 g_static_rw_lock_writer_lock(&element_lock);
546 driver_list = g_slist_insert_sorted(driver_list, driver,
549 if (element_root != NULL)
550 g_node_traverse(element_root, G_PRE_ORDER,
551 G_TRAVERSE_ALL, -1, probe_driver, driver);
553 g_static_rw_lock_writer_unlock(&element_lock);
558 static gboolean remove_driver(GNode *node, gpointer data)
560 struct connman_element *element = node->data;
561 struct connman_driver *driver = data;
563 DBG("element %p name %s", element, element->name);
565 if (element->driver == driver) {
567 driver->remove(element);
569 connman_element_lock(element);
570 element->driver = NULL;
571 connman_element_unlock(element);
578 * connman_driver_unregister:
579 * @driver: driver definition
581 * Remove a previously registered driver
583 void connman_driver_unregister(struct connman_driver *driver)
585 DBG("driver %p name %s", driver, driver->name);
587 g_static_rw_lock_writer_lock(&element_lock);
589 driver_list = g_slist_remove(driver_list, driver);
591 if (element_root != NULL)
592 g_node_traverse(element_root, G_POST_ORDER,
593 G_TRAVERSE_ALL, -1, remove_driver, driver);
595 g_static_rw_lock_writer_unlock(&element_lock);
599 * connman_element_create:
600 * @name: element name
602 * Allocate a new element and assign the given #name to it. If the name
603 * is #NULL, it will be later on created based on the element type.
605 * Returns: a newly-allocated #connman_element structure
607 struct connman_element *connman_element_create(const char *name)
609 struct connman_element *element;
611 element = g_try_new0(struct connman_element, 1);
615 DBG("element %p", element);
617 element->refcount = 1;
619 g_static_mutex_init(&element->mutex);
621 element->name = g_strdup(name);
622 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
623 element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
624 element->state = CONNMAN_ELEMENT_STATE_CLOSED;
626 element->enabled = FALSE;
631 struct connman_element *connman_element_ref(struct connman_element *element)
633 DBG("element %p name %s refcount %d", element, element->name,
634 g_atomic_int_get(&element->refcount) + 1);
636 g_atomic_int_inc(&element->refcount);
641 static void free_properties(struct connman_element *element)
645 DBG("element %p name %s", element, element->name);
647 connman_element_lock(element);
649 for (list = element->properties; list; list = list->next) {
650 struct connman_property *property = list->data;
652 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)) {
653 if (property->type == DBUS_TYPE_STRING)
654 g_free(property->value);
660 g_slist_free(element->properties);
662 element->properties = NULL;
664 connman_element_unlock(element);
667 void connman_element_unref(struct connman_element *element)
669 DBG("element %p name %s refcount %d", element, element->name,
670 g_atomic_int_get(&element->refcount) - 1);
672 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
673 free_properties(element);
674 g_free(element->ipv4.address);
675 g_free(element->ipv4.netmask);
676 g_free(element->ipv4.gateway);
677 g_free(element->ipv4.network);
678 g_free(element->ipv4.broadcast);
679 g_free(element->ipv4.nameserver);
680 g_free(element->network.identifier);
681 g_free(element->path);
682 g_free(element->name);
687 int connman_element_add_static_property(struct connman_element *element,
688 const char *name, int type, const void *value)
690 struct connman_property *property;
692 DBG("element %p name %s", element, element->name);
694 if (type != DBUS_TYPE_STRING)
697 property = g_try_new0(struct connman_property, 1);
698 if (property == NULL)
701 property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
702 property->id = CONNMAN_PROPERTY_ID_INVALID;
703 property->name = g_strdup(name);
704 property->type = type;
706 DBG("name %s type %d value %p", name, type, value);
709 case DBUS_TYPE_STRING:
710 property->value = g_strdup(*((const char **) value));
714 connman_element_lock(element);
715 element->properties = g_slist_append(element->properties, property);
716 connman_element_unlock(element);
721 static void *get_reference_value(struct connman_element *element,
722 enum connman_property_id id)
726 DBG("element %p name %s", element, element->name);
728 for (list = element->properties; list; list = list->next) {
729 struct connman_property *property = list->data;
731 if (property->id != id)
734 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
735 return property->value;
738 if (element->parent == NULL)
741 return get_reference_value(element->parent, id);
744 static void set_reference_properties(struct connman_element *element)
748 DBG("element %p name %s", element, element->name);
750 for (list = element->properties; list; list = list->next) {
751 struct connman_property *property = list->data;
753 if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
756 property->value = get_reference_value(element->parent,
761 static struct connman_property *create_property(struct connman_element *element,
762 enum connman_property_id id)
764 struct connman_property *property;
767 DBG("element %p name %s", element, element->name);
769 connman_element_lock(element);
771 for (list = element->properties; list; list = list->next) {
772 property = list->data;
774 if (property->id == id)
778 property = g_try_new0(struct connman_property, 1);
779 if (property == NULL)
782 property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
784 property->name = g_strdup(propid2name(id));
785 property->type = propid2type(id);
787 if (property->name == NULL) {
793 element->properties = g_slist_append(element->properties, property);
796 connman_element_unlock(element);
801 static void create_default_properties(struct connman_element *element)
803 struct connman_property *property;
806 DBG("element %p name %s", element, element->name);
808 for (i = 0; propid_table[i].name; i++) {
809 DBG("property %s", propid_table[i].name);
811 property = create_property(element, propid_table[i].id);
813 property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
815 if (propid_table[i].type != DBUS_TYPE_STRING)
818 if (propid_table[i].value)
819 property->value = g_strdup(propid_table[i].value);
821 property->value = g_strdup("");
825 static int define_properties_valist(struct connman_element *element,
828 enum connman_property_id id;
830 DBG("element %p name %s", element, element->name);
832 id = va_arg(args, enum connman_property_id);
834 while (id != CONNMAN_PROPERTY_ID_INVALID) {
836 DBG("property %d", id);
838 create_property(element, id);
840 id = va_arg(args, enum connman_property_id);
847 * connman_element_define_properties:
848 * @element: an element
849 * @varargs: list of property identifiers
851 * Define the valid properties for an element.
853 * Returns: %0 on success
855 int connman_element_define_properties(struct connman_element *element, ...)
860 DBG("element %p name %s", element, element->name);
862 va_start(args, element);
864 err = define_properties_valist(element, args);
871 int connman_element_create_property(struct connman_element *element,
872 const char *name, int type)
877 int connman_element_set_property(struct connman_element *element,
878 enum connman_property_id id, const void *value)
881 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
882 connman_element_lock(element);
883 g_free(element->ipv4.address);
884 element->ipv4.address = g_strdup(*((const char **) value));
885 connman_element_unlock(element);
887 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
888 connman_element_lock(element);
889 g_free(element->ipv4.netmask);
890 element->ipv4.netmask = g_strdup(*((const char **) value));
891 connman_element_unlock(element);
893 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
894 connman_element_lock(element);
895 g_free(element->ipv4.gateway);
896 element->ipv4.gateway = g_strdup(*((const char **) value));
897 connman_element_unlock(element);
899 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
900 connman_element_lock(element);
901 g_free(element->ipv4.nameserver);
902 element->ipv4.nameserver = g_strdup(*((const char **) value));
903 connman_element_unlock(element);
909 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
910 CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
911 DBUS_TYPE_OBJECT_PATH, &element->path,
917 int connman_element_get_value(struct connman_element *element,
918 enum connman_property_id id, void *value)
920 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
924 case CONNMAN_PROPERTY_ID_IPV4_ADDRESS:
925 if (element->ipv4.address == NULL)
926 return connman_element_get_value(element->parent,
928 connman_element_lock(element);
929 *((char **) value) = element->ipv4.address;
930 connman_element_unlock(element);
932 case CONNMAN_PROPERTY_ID_IPV4_NETMASK:
933 if (element->ipv4.netmask == NULL)
934 return connman_element_get_value(element->parent,
936 connman_element_lock(element);
937 *((char **) value) = element->ipv4.netmask;
938 connman_element_unlock(element);
940 case CONNMAN_PROPERTY_ID_IPV4_GATEWAY:
941 if (element->ipv4.gateway == NULL)
942 return connman_element_get_value(element->parent,
944 connman_element_lock(element);
945 *((char **) value) = element->ipv4.gateway;
946 connman_element_unlock(element);
948 case CONNMAN_PROPERTY_ID_IPV4_NAMESERVER:
949 if (element->ipv4.nameserver == NULL)
950 return connman_element_get_value(element->parent,
952 connman_element_lock(element);
953 *((char **) value) = element->ipv4.nameserver;
954 connman_element_unlock(element);
964 * connman_element_register:
965 * @element: the element to register
966 * @parent: the parent to register the element with
968 * Register an element with the core. It will be register under the given
969 * parent of if %NULL is provided under the root element.
971 * Returns: %0 on success
973 int connman_element_register(struct connman_element *element,
974 struct connman_element *parent)
976 DBG("element %p name %s parent %p", element, element->name, parent);
978 if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE) {
979 if (g_pattern_match_simple(device_filter,
980 element->name) == FALSE) {
981 DBG("ignoring %s device", element->name);
986 if (connman_element_ref(element) == NULL)
989 connman_element_lock(element);
991 __connman_element_load(element);
993 if (element->name == NULL) {
994 element->name = g_strdup(type2string(element->type));
995 if (element->name == NULL) {
996 connman_element_unlock(element);
1001 element->parent = parent;
1003 connman_element_unlock(element);
1005 if (thread_register != NULL)
1006 g_thread_pool_push(thread_register, element, NULL);
1011 void connman_element_unregister(struct connman_element *element)
1013 DBG("element %p name %s", element, element->name);
1015 if (thread_unregister != NULL)
1016 g_thread_pool_push(thread_unregister, element, NULL);
1019 void connman_element_unregister_children(struct connman_element *element)
1021 DBG("element %p name %s", element, element->name);
1023 if (thread_unregister_children != NULL)
1024 g_thread_pool_push(thread_unregister_children, element, NULL);
1027 static gboolean update_element(GNode *node, gpointer user_data)
1029 struct connman_element *element = node->data;
1031 DBG("element %p name %s", element, element->name);
1033 if (element->driver && element->driver->update)
1034 element->driver->update(element);
1036 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
1037 CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
1038 DBUS_TYPE_OBJECT_PATH, &element->path,
1044 void connman_element_update(struct connman_element *element)
1048 DBG("element %p name %s", element, element->name);
1050 g_static_rw_lock_reader_lock(&element_lock);
1052 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1055 g_node_traverse(node, G_PRE_ORDER,
1056 G_TRAVERSE_ALL, -1, update_element, NULL);
1058 g_static_rw_lock_reader_unlock(&element_lock);
1061 static void register_element(gpointer data, gpointer user_data)
1063 struct connman_element *element = data;
1064 const gchar *basepath;
1068 g_static_rw_lock_writer_lock(&element_lock);
1070 connman_element_lock(element);
1072 if (element->parent) {
1073 node = g_node_find(element_root, G_PRE_ORDER,
1074 G_TRAVERSE_ALL, element->parent);
1075 basepath = element->parent->path;
1077 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
1078 element->subtype = element->parent->subtype;
1080 element->parent = element_root->data;
1082 node = element_root;
1086 element->path = g_strdup_printf("%s/%s", basepath, element->name);
1088 set_reference_properties(element);
1090 connman_element_unlock(element);
1092 DBG("element %p path %s", element, element->path);
1094 g_node_append_data(node, element);
1096 if (g_dbus_register_interface(connection, element->path,
1097 CONNMAN_ELEMENT_INTERFACE,
1098 element_methods, element_signals,
1099 NULL, element, NULL) == FALSE)
1100 connman_error("Failed to register %s", element->path);
1102 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
1103 CONNMAN_MANAGER_INTERFACE, "ElementAdded",
1104 DBUS_TYPE_OBJECT_PATH, &element->path,
1107 g_static_rw_lock_writer_unlock(&element_lock);
1109 __connman_element_store(element);
1111 g_static_rw_lock_writer_lock(&element_lock);
1113 for (list = driver_list; list; list = list->next) {
1114 struct connman_driver *driver = list->data;
1116 if (match_driver(element, driver) == FALSE)
1119 DBG("driver %p name %s", driver, driver->name);
1121 if (driver->probe(element) == 0) {
1122 connman_element_lock(element);
1123 element->driver = driver;
1124 connman_element_unlock(element);
1129 g_static_rw_lock_writer_unlock(&element_lock);
1132 static gboolean remove_element(GNode *node, gpointer user_data)
1134 struct connman_element *element = node->data;
1135 struct connman_element *root = user_data;
1137 DBG("element %p name %s", element, element->name);
1139 if (element == root)
1142 if (element->driver) {
1143 if (element->driver->remove)
1144 element->driver->remove(element);
1146 connman_element_lock(element);
1147 element->driver = NULL;
1148 connman_element_unlock(element);
1152 g_node_unlink(node);
1153 g_node_destroy(node);
1156 g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
1157 CONNMAN_MANAGER_INTERFACE, "ElementRemoved",
1158 DBUS_TYPE_OBJECT_PATH, &element->path,
1161 g_dbus_unregister_interface(connection, element->path,
1162 CONNMAN_ELEMENT_INTERFACE);
1164 connman_element_unref(element);
1169 static void unregister_element(gpointer data, gpointer user_data)
1171 struct connman_element *element = data;
1174 DBG("element %p name %s", element, element->name);
1176 g_static_rw_lock_writer_lock(&element_lock);
1178 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1181 g_node_traverse(node, G_POST_ORDER,
1182 G_TRAVERSE_ALL, -1, remove_element, NULL);
1184 g_static_rw_lock_writer_unlock(&element_lock);
1187 static void unregister_children(gpointer data, gpointer user_data)
1189 struct connman_element *element = data;
1192 DBG("element %p name %s", element, element->name);
1194 g_static_rw_lock_writer_lock(&element_lock);
1196 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
1199 g_node_traverse(node, G_POST_ORDER,
1200 G_TRAVERSE_ALL, -1, remove_element, element);
1202 g_static_rw_lock_writer_unlock(&element_lock);
1205 int __connman_element_init(DBusConnection *conn, const char *device)
1207 struct connman_element *element;
1209 DBG("conn %p", conn);
1211 connection = dbus_connection_ref(conn);
1212 if (connection == NULL)
1215 device_filter = g_strdup(device);
1217 g_static_rw_lock_writer_lock(&element_lock);
1219 element = connman_element_create("root");
1221 element->path = g_strdup("/");
1222 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
1224 create_default_properties(element);
1226 element_root = g_node_new(element);
1228 g_static_rw_lock_writer_unlock(&element_lock);
1230 thread_register = g_thread_pool_new(register_element,
1231 NULL, 1, FALSE, NULL);
1232 thread_unregister = g_thread_pool_new(unregister_element,
1233 NULL, 1, FALSE, NULL);
1234 thread_unregister_children = g_thread_pool_new(unregister_children,
1235 NULL, 1, FALSE, NULL);
1240 static gboolean free_driver(GNode *node, gpointer data)
1242 struct connman_element *element = node->data;
1244 DBG("element %p name %s", element, element->name);
1246 if (element->driver) {
1247 if (element->driver->remove)
1248 element->driver->remove(element);
1250 connman_element_lock(element);
1251 element->driver = NULL;
1252 connman_element_unlock(element);
1258 static gboolean free_node(GNode *node, gpointer data)
1260 struct connman_element *element = node->data;
1262 DBG("element %p name %s", element, element->name);
1264 if (g_node_depth(node) > 1)
1265 g_thread_pool_push(thread_unregister, element, NULL);
1270 void __connman_element_cleanup(void)
1274 g_thread_pool_free(thread_register, TRUE, TRUE);
1275 thread_register = NULL;
1277 g_static_rw_lock_writer_lock(&element_lock);
1278 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1280 g_static_rw_lock_writer_unlock(&element_lock);
1282 g_static_rw_lock_writer_lock(&element_lock);
1283 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
1285 g_static_rw_lock_writer_unlock(&element_lock);
1287 g_thread_pool_free(thread_unregister, FALSE, TRUE);
1288 thread_unregister = NULL;
1290 g_thread_pool_free(thread_unregister_children, FALSE, TRUE);
1291 thread_unregister_children = NULL;
1293 g_static_rw_lock_writer_lock(&element_lock);
1294 g_node_destroy(element_root);
1295 element_root = NULL;
1296 g_static_rw_lock_writer_unlock(&element_lock);
1298 g_free(device_filter);
1300 dbus_connection_unref(connection);