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;
40 static gboolean started = FALSE;
42 static const char *type2string(enum connman_element_type type)
45 case CONNMAN_ELEMENT_TYPE_UNKNOWN:
47 case CONNMAN_ELEMENT_TYPE_ROOT:
49 case CONNMAN_ELEMENT_TYPE_NETWORK:
57 enum connman_element_type type;
58 element_cb_t callback;
62 static gboolean foreach_callback(GNode *node, gpointer user_data)
64 struct connman_element *element = node->data;
65 struct foreach_data *data = user_data;
67 DBG("element %p name %s", element, element->name);
69 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
72 if (data->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
73 data->type != element->type)
77 data->callback(element, data->user_data);
82 void __connman_element_foreach(struct connman_element *element,
83 enum connman_element_type type,
84 element_cb_t callback, gpointer user_data)
86 struct foreach_data data = { type, callback, user_data };
91 if (element != NULL) {
92 node = g_node_find(element_root, G_PRE_ORDER,
93 G_TRAVERSE_ALL, element);
99 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
100 foreach_callback, &data);
103 struct append_filter {
104 enum connman_element_type type;
105 DBusMessageIter *iter;
108 static gboolean append_path(GNode *node, gpointer user_data)
110 struct connman_element *element = node->data;
111 struct append_filter *filter = user_data;
113 DBG("element %p name %s", element, element->name);
115 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
118 if (filter->type != CONNMAN_ELEMENT_TYPE_UNKNOWN &&
119 filter->type != element->type)
122 if (filter->type == CONNMAN_ELEMENT_TYPE_NETWORK &&
123 __connman_network_has_driver(element->network) == FALSE)
126 dbus_message_iter_append_basic(filter->iter,
127 DBUS_TYPE_OBJECT_PATH, &element->path);
132 void __connman_element_list(struct connman_element *element,
133 enum connman_element_type type,
134 DBusMessageIter *iter)
136 struct append_filter filter = { type, iter };
141 if (element != NULL) {
142 node = g_node_find(element_root, G_PRE_ORDER,
143 G_TRAVERSE_ALL, element);
149 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
150 append_path, &filter);
153 static gint compare_priority(gconstpointer a, gconstpointer b)
155 const struct connman_driver *driver1 = a;
156 const struct connman_driver *driver2 = b;
158 return driver2->priority - driver1->priority;
161 static gboolean match_driver(struct connman_element *element,
162 struct connman_driver *driver)
164 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
167 if (element->type == driver->type ||
168 driver->type == CONNMAN_ELEMENT_TYPE_UNKNOWN)
174 static gboolean probe_driver(GNode *node, gpointer data)
176 struct connman_element *element = node->data;
177 struct connman_driver *driver = data;
179 DBG("element %p name %s", element, element->name);
181 if (!element->driver && match_driver(element, driver) == TRUE) {
182 if (driver->probe(element) < 0)
185 element->driver = driver;
191 void __connman_driver_rescan(struct connman_driver *driver)
193 DBG("driver %p name %s", driver, driver->name);
198 if (element_root != NULL)
199 g_node_traverse(element_root, G_PRE_ORDER,
200 G_TRAVERSE_ALL, -1, probe_driver, driver);
204 * connman_driver_register:
205 * @driver: driver definition
207 * Register a new driver
209 * Returns: %0 on success
211 int connman_driver_register(struct connman_driver *driver)
213 DBG("driver %p name %s", driver, driver->name);
215 if (driver->type == CONNMAN_ELEMENT_TYPE_ROOT)
221 driver_list = g_slist_insert_sorted(driver_list, driver,
224 if (started == FALSE)
227 if (element_root != NULL)
228 g_node_traverse(element_root, G_PRE_ORDER,
229 G_TRAVERSE_ALL, -1, probe_driver, driver);
234 static gboolean remove_driver(GNode *node, gpointer data)
236 struct connman_element *element = node->data;
237 struct connman_driver *driver = data;
239 DBG("element %p name %s", element, element->name);
241 if (element->driver == driver) {
243 driver->remove(element);
245 element->driver = NULL;
252 * connman_driver_unregister:
253 * @driver: driver definition
255 * Remove a previously registered driver
257 void connman_driver_unregister(struct connman_driver *driver)
259 DBG("driver %p name %s", driver, driver->name);
261 driver_list = g_slist_remove(driver_list, driver);
263 if (element_root != NULL)
264 g_node_traverse(element_root, G_POST_ORDER,
265 G_TRAVERSE_ALL, -1, remove_driver, driver);
268 static void unregister_property(gpointer data)
270 struct connman_property *property = data;
272 DBG("property %p", property);
274 g_free(property->value);
278 static void unregister_child(gpointer data)
280 struct connman_element *element = data;
282 DBG("element %p", element);
284 connman_element_unref(element);
287 void __connman_element_initialize(struct connman_element *element)
289 DBG("element %p", element);
291 element->refcount = 1;
293 element->name = NULL;
294 element->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
295 element->state = CONNMAN_ELEMENT_STATE_UNKNOWN;
296 element->error = CONNMAN_ELEMENT_ERROR_UNKNOWN;
298 element->enabled = FALSE;
300 element->children = g_hash_table_new_full(g_str_hash, g_str_equal,
301 NULL, unregister_child);
303 element->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
304 g_free, unregister_property);
308 * connman_element_create:
309 * @name: element name
311 * Allocate a new element and assign the given #name to it. If the name
312 * is #NULL, it will be later on created based on the element type.
314 * Returns: a newly-allocated #connman_element structure
316 struct connman_element *connman_element_create(const char *name)
318 struct connman_element *element;
320 element = g_try_new0(struct connman_element, 1);
324 DBG("element %p", element);
326 __connman_element_initialize(element);
328 element->name = g_strdup(name);
333 struct connman_element *connman_element_ref(struct connman_element *element)
335 DBG("element %p name %s refcount %d", element, element->name,
336 g_atomic_int_get(&element->refcount) + 1);
338 g_atomic_int_inc(&element->refcount);
343 static void free_properties(struct connman_element *element)
345 DBG("element %p name %s", element, element->name);
347 g_hash_table_destroy(element->properties);
348 element->properties = NULL;
351 static void free_children(struct connman_element *element)
353 DBG("element %p name %s", element, element->name);
355 g_hash_table_destroy(element->children);
356 element->children = NULL;
359 void connman_element_unref(struct connman_element *element)
361 DBG("element %p name %s refcount %d", element, element->name,
362 g_atomic_int_get(&element->refcount) - 1);
364 if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
365 if (element->destruct)
366 element->destruct(element);
367 free_children(element);
368 free_properties(element);
369 g_free(element->devname);
370 g_free(element->path);
371 g_free(element->name);
376 static int set_static_property(struct connman_element *element,
377 const char *name, int type, const void *value)
379 struct connman_property *property;
381 DBG("element %p name %s", element, element->name);
383 if (type != DBUS_TYPE_STRING && type != DBUS_TYPE_BYTE)
386 property = g_try_new0(struct connman_property, 1);
387 if (property == NULL)
390 property->id = CONNMAN_PROPERTY_ID_INVALID;
391 property->type = type;
393 DBG("name %s type %d value %p", name, type, value);
396 case DBUS_TYPE_STRING:
397 property->value = g_strdup(*((const char **) value));
399 case DBUS_TYPE_BOOLEAN:
401 property->value = g_try_malloc(1);
402 if (property->value != NULL)
403 memcpy(property->value, value, 1);
407 g_hash_table_replace(element->properties, g_strdup(name), property);
412 static int set_static_array_property(struct connman_element *element,
413 const char *name, int type, const void *value, int len)
415 struct connman_property *property;
417 DBG("element %p name %s", element, element->name);
419 if (type != DBUS_TYPE_BYTE)
422 property = g_try_new0(struct connman_property, 1);
423 if (property == NULL)
426 property->id = CONNMAN_PROPERTY_ID_INVALID;
427 property->type = DBUS_TYPE_ARRAY;
428 property->subtype = type;
430 DBG("name %s type %d value %p", name, type, value);
434 property->value = g_try_malloc(len);
435 if (property->value != NULL) {
436 memcpy(property->value,
437 *((const unsigned char **) value), len);
438 property->size = len;
443 g_hash_table_replace(element->properties, g_strdup(name), property);
448 int connman_element_get_value(struct connman_element *element,
449 enum connman_property_id id, void *value)
454 static gboolean get_static_property(struct connman_element *element,
455 const char *name, void *value)
457 struct connman_property *property;
458 gboolean found = FALSE;
460 DBG("element %p name %s", element, element->name);
462 property = g_hash_table_lookup(element->properties, name);
463 if (property != NULL) {
464 switch (property->type) {
465 case DBUS_TYPE_STRING:
466 *((char **) value) = property->value;
469 case DBUS_TYPE_BOOLEAN:
471 memcpy(value, property->value, 1);
477 if (found == FALSE && element->parent != NULL)
478 return get_static_property(element->parent, name, value);
483 static gboolean get_static_array_property(struct connman_element *element,
484 const char *name, void *value, unsigned int *len)
486 struct connman_property *property;
487 gboolean found = FALSE;
489 DBG("element %p name %s", element, element->name);
491 property = g_hash_table_lookup(element->properties, name);
492 if (property != NULL) {
493 *((void **) value) = property->value;
494 *len = property->size;
502 * connman_element_set_string:
503 * @element: element structure
504 * @key: unique identifier
505 * @value: string value
507 * Set string value for specific key
509 int connman_element_set_string(struct connman_element *element,
510 const char *key, const char *value)
512 return set_static_property(element, key, DBUS_TYPE_STRING, &value);
516 * connman_element_get_string:
517 * @element: element structure
518 * @key: unique identifier
520 * Get string value for specific key
522 const char *connman_element_get_string(struct connman_element *element,
527 if (get_static_property(element, key, &value) == FALSE)
534 * connman_element_set_bool:
535 * @element: element structure
536 * @key: unique identifier
537 * @value: boolean value
539 * Set boolean value for specific key
541 int connman_element_set_bool(struct connman_element *element,
542 const char *key, connman_bool_t value)
544 return set_static_property(element, key, DBUS_TYPE_BOOLEAN, &value);
548 * connman_element_get_bool:
549 * @element: element structure
550 * @key: unique identifier
552 * Get boolean value for specific key
554 connman_bool_t connman_element_get_bool(struct connman_element *element,
557 connman_bool_t value;
559 if (get_static_property(element, key, &value) == FALSE)
566 * connman_element_set_uint8:
567 * @element: element structure
568 * @key: unique identifier
569 * @value: integer value
571 * Set integer value for specific key
573 int connman_element_set_uint8(struct connman_element *element,
574 const char *key, connman_uint8_t value)
576 return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
580 * connman_element_get_uint8:
581 * @element: element structure
582 * @key: unique identifier
584 * Get integer value for specific key
586 connman_uint8_t connman_element_get_uint8(struct connman_element *element,
589 connman_uint8_t value;
591 if (get_static_property(element, key, &value) == FALSE)
598 * connman_element_set_blob:
599 * @element: element structure
600 * @key: unique identifier
604 * Set binary blob value for specific key
606 int connman_element_set_blob(struct connman_element *element,
607 const char *key, const void *data, unsigned int size)
609 return set_static_array_property(element, key,
610 DBUS_TYPE_BYTE, &data, size);
614 * connman_element_get_blob:
615 * @element: element structure
616 * @key: unique identifier
617 * @size: pointer to blob size
619 * Get binary blob value for specific key
621 const void *connman_element_get_blob(struct connman_element *element,
622 const char *key, unsigned int *size)
626 if (get_static_array_property(element, key, &value, size) == FALSE)
632 static void probe_element(struct connman_element *element)
636 DBG("element %p name %s", element, element->name);
638 for (list = driver_list; list; list = list->next) {
639 struct connman_driver *driver = list->data;
641 if (match_driver(element, driver) == FALSE)
644 DBG("driver %p name %s", driver, driver->name);
646 if (driver->probe(element) == 0) {
647 element->driver = driver;
653 static void register_element(gpointer data, gpointer user_data)
655 struct connman_element *element = data;
656 const gchar *basepath;
659 if (element->parent) {
660 node = g_node_find(element_root, G_PRE_ORDER,
661 G_TRAVERSE_ALL, element->parent);
662 basepath = element->parent->path;
664 element->parent = element_root->data;
667 basepath = CONNMAN_PATH "/device";
670 element->path = g_strdup_printf("%s/%s", basepath, element->name);
673 connman_error("Element registration for %s failed",
678 DBG("element %p path %s", element, element->path);
680 g_node_append_data(node, element);
682 if (started == FALSE)
685 probe_element(element);
689 * connman_element_register:
690 * @element: the element to register
691 * @parent: the parent to register the element with
693 * Register an element with the core. It will be register under the given
694 * parent of if %NULL is provided under the root element.
696 * Returns: %0 on success
698 int connman_element_register(struct connman_element *element,
699 struct connman_element *parent)
701 DBG("element %p name %s parent %p", element, element->name, parent);
703 if (element->devname == NULL)
704 element->devname = g_strdup(element->name);
706 if (connman_element_ref(element) == NULL)
709 if (element->name == NULL) {
710 element->name = g_strdup(type2string(element->type));
711 if (element->name == NULL) {
716 element->parent = parent;
718 register_element(element, NULL);
723 static gboolean remove_element(GNode *node, gpointer user_data)
725 struct connman_element *element = node->data;
726 struct connman_element *root = user_data;
728 DBG("element %p name %s", element, element->name);
735 if (element->driver) {
736 if (element->driver->remove)
737 element->driver->remove(element);
739 element->driver = NULL;
742 g_node_destroy(node);
744 connman_element_unref(element);
749 void connman_element_unregister(struct connman_element *element)
753 DBG("element %p name %s", element, element->name);
755 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
758 g_node_traverse(node, G_POST_ORDER,
759 G_TRAVERSE_ALL, -1, remove_element, NULL);
762 void connman_element_unregister_children(struct connman_element *element)
766 DBG("element %p name %s", element, element->name);
768 node = g_node_find(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, element);
771 g_node_traverse(node, G_POST_ORDER,
772 G_TRAVERSE_ALL, -1, remove_element, element);
775 int __connman_element_init()
777 struct connman_element *element;
781 connection = connman_dbus_get_connection();
782 if (connection == NULL)
784 element = connman_element_create("root");
786 element->path = g_strdup("/");
787 element->type = CONNMAN_ELEMENT_TYPE_ROOT;
789 element_root = g_node_new(element);
791 __connman_technology_init();
792 __connman_notifier_init();
793 __connman_location_init();
794 __connman_service_init();
795 __connman_provider_init();
796 __connman_network_init();
801 static gboolean probe_node(GNode *node, gpointer data)
803 struct connman_element *element = node->data;
805 DBG("element %p name %s", element, element->name);
807 if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
813 probe_element(element);
818 void __connman_element_start(void)
822 __connman_storage_init_profile();
824 g_node_traverse(element_root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
829 __connman_rtnl_start();
831 __connman_dhcp_init();
832 __connman_wpad_init();
833 __connman_wispr_init();
835 __connman_rfkill_init();
838 void __connman_element_stop(void)
842 __connman_rfkill_cleanup();
844 __connman_wispr_cleanup();
845 __connman_wpad_cleanup();
846 __connman_dhcp_cleanup();
847 __connman_provider_cleanup();
850 static gboolean free_driver(GNode *node, gpointer data)
852 struct connman_element *element = node->data;
854 DBG("element %p name %s", element, element->name);
856 if (element->driver) {
857 if (element->driver->remove)
858 element->driver->remove(element);
860 element->driver = NULL;
866 static gboolean free_node(GNode *node, gpointer data)
868 struct connman_element *element = node->data;
870 DBG("element %p name %s", element, element->name);
872 if (g_node_depth(node) > 1)
873 connman_element_unregister(element);
878 void __connman_element_cleanup(void)
882 __connman_network_cleanup();
883 __connman_service_cleanup();
884 __connman_location_cleanup();
885 __connman_notifier_cleanup();
886 __connman_technology_cleanup();
888 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
891 g_node_traverse(element_root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
894 connman_element_unref(element_root->data);
896 g_node_destroy(element_root);
899 if (connection == NULL)
902 dbus_connection_unref(connection);