Add function to find first device of a given type
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 16 Jul 2009 01:48:43 +0000 (03:48 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 16 Jul 2009 01:48:43 +0000 (03:48 +0200)
src/connman.h
src/element.c

index 84ebf60..9b68083 100644 (file)
@@ -163,6 +163,8 @@ struct connman_device *__connman_element_get_device(struct connman_element *elem
 const char *__connman_element_get_device_path(struct connman_element *element);
 const char *__connman_element_get_network_path(struct connman_element *element);
 
+struct connman_device *__connman_element_find_device(enum connman_device_type type);
+
 const char *__connman_element_type2string(enum connman_element_type type);
 
 static inline void __connman_element_lock(struct connman_element *element)
index bdf222c..0175019 100644 (file)
@@ -330,6 +330,40 @@ const char *__connman_element_get_network_path(struct connman_element *element)
        return __connman_element_get_network_path(element->parent);
 }
 
+struct find_data {
+       enum connman_device_type type;
+       struct connman_device *device;
+};
+
+static gboolean find_device(GNode *node, gpointer user_data)
+{
+       struct connman_element *element = node->data;
+       struct find_data *data = user_data;
+
+       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE)
+               return FALSE;
+
+       if (element->device == NULL)
+               return FALSE;
+
+       if (data->type != connman_device_get_type(element->device))
+               return FALSE;
+
+       data->device = element->device;
+
+       return TRUE;
+}
+
+struct connman_device *__connman_element_find_device(enum connman_device_type type)
+{
+       struct find_data data = { .type = type, .device = NULL };
+
+       g_node_traverse(element_root, G_PRE_ORDER,
+                               G_TRAVERSE_ALL, -1, find_device, &data);
+
+       return data.device;
+}
+
 static gint compare_priority(gconstpointer a, gconstpointer b)
 {
        const struct connman_driver *driver1 = a;