Add more advanced properties implementation
authorMarcel Holtmann <marcel@holtmann.org>
Mon, 25 Aug 2008 09:19:06 +0000 (11:19 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Mon, 25 Aug 2008 09:19:06 +0000 (11:19 +0200)
include/element.h
include/property.h
src/element.c

index e2d7ea8..57103d3 100644 (file)
@@ -73,6 +73,7 @@ struct connman_driver;
 struct connman_element {
        gint refcount;
        GStaticMutex mutex;
+       gint index;
        gchar *name;
        gchar *path;
        enum connman_element_type type;
@@ -89,12 +90,6 @@ struct connman_element {
        GSList *properties;
 
        struct {
-               int index;
-               short flags;
-               gchar *name;
-       } netdev;
-
-       struct {
                gchar *identifier;
        } network;
 
@@ -111,16 +106,19 @@ struct connman_element {
 #define connman_element_lock(element)    g_static_mutex_lock(&(element)->mutex)
 #define connman_element_unlock(element)  g_static_mutex_unlock(&(element)->mutex)
 
-extern struct connman_element *connman_element_create(void);
+extern struct connman_element *connman_element_create(const char *name);
 extern struct connman_element *connman_element_ref(struct connman_element *element);
 extern void connman_element_unref(struct connman_element *element);
 
 extern int connman_element_add_static_property(struct connman_element *element,
                                const char *name, int type, const void *value);
+extern int connman_element_define_properties(struct connman_element *element, ...);
+extern int connman_element_create_property(struct connman_element *element,
+                                               const char *name, int type);
 extern int connman_element_set_property(struct connman_element *element,
-                       enum connman_property_type type, const void *value);
+                               enum connman_property_id id, const void *value);
 extern int connman_element_get_value(struct connman_element *element,
-                               enum connman_property_type type, void *value);
+                               enum connman_property_id id, void *value);
 
 extern int connman_element_register(struct connman_element *element,
                                        struct connman_element *parent);
index d343948..14198be 100644 (file)
@@ -32,21 +32,29 @@ extern "C" {
  * @short_description: Functions for handling properties
  */
 
-enum connman_property_type {
-       CONNMAN_PROPERTY_TYPE_INVALID = 0,
+enum connman_property_id {
+       CONNMAN_PROPERTY_ID_INVALID = 0,
 
-       CONNMAN_PROPERTY_TYPE_IPV4_ADDRESS,
-       CONNMAN_PROPERTY_TYPE_IPV4_NETMASK,
-       CONNMAN_PROPERTY_TYPE_IPV4_GATEWAY,
-       CONNMAN_PROPERTY_TYPE_IPV4_NAMESERVER,
+       CONNMAN_PROPERTY_ID_IPV4_METHOD,
+       CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
+       CONNMAN_PROPERTY_ID_IPV4_NETMASK,
+       CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
+       CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
 };
 
+/**
+ * connman_property_flags:
+ * @CONNMAN_PROPERTY_FLAG_STATIC: read-only property
+ * @CONNMAN_PROPERTY_FLAG_REFERENCE: inheritated value (reference only)
+ */
 enum connman_property_flags {
-       CONNMAN_PROPERTY_FLAG_STATIC = (1 << 0),
+       CONNMAN_PROPERTY_FLAG_STATIC     = (1 << 0),
+       CONNMAN_PROPERTY_FLAG_REFERENCE  = (1 << 1),
 };
 
 struct connman_property {
        enum connman_property_flags flags;
+       enum connman_property_id id;
        char *name;
        int type;
        void *value;
index 66c91d0..a9012b2 100644 (file)
@@ -24,6 +24,7 @@
 #endif
 
 #include <errno.h>
+#include <stdarg.h>
 
 #include <glib.h>
 #include <gdbus.h>
@@ -43,6 +44,49 @@ static GThreadPool *thread_unregister_children = NULL;
 
 static gchar *device_filter = NULL;
 
+static struct {
+       enum connman_property_id id;
+       int type;
+       const char *name;
+       const void *value;
+} propid_table[] = {
+       { CONNMAN_PROPERTY_ID_IPV4_METHOD,
+               DBUS_TYPE_STRING, "IPv4.Method", "dhcp" },
+       { CONNMAN_PROPERTY_ID_IPV4_ADDRESS,
+               DBUS_TYPE_STRING, "IPv4.Address" },
+       { CONNMAN_PROPERTY_ID_IPV4_NETMASK,
+               DBUS_TYPE_STRING, "IPv4.Netmask" },
+       { CONNMAN_PROPERTY_ID_IPV4_GATEWAY,
+               DBUS_TYPE_STRING, "IPv4.Gateway" },
+       { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
+               DBUS_TYPE_STRING, "IPv4.Nameserver" },
+       { }
+};
+
+static int propid2type(enum connman_property_id id)
+{
+       int i;
+
+       for (i = 0; propid_table[i].name; i++) {
+               if (propid_table[i].id == id)
+                       return propid_table[i].type;
+       }
+
+       return DBUS_TYPE_INVALID;
+}
+
+static const char *propid2name(enum connman_property_id id)
+{
+       int i;
+
+       for (i = 0; propid_table[i].name; i++) {
+               if (propid_table[i].id == id)
+                       return propid_table[i].name;
+       }
+
+       return NULL;
+}
+
 static const char *type2string(enum connman_element_type type)
 {
        switch (type) {
@@ -136,11 +180,10 @@ static void append_entry(DBusMessageIter *dict,
 static void append_property(DBusMessageIter *dict,
                                struct connman_property *property)
 {
-       if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC) {
-               append_entry(dict, property->name, property->type,
-                                                       &property->value);
+       if (property->value == NULL)
                return;
-       }
+
+       append_entry(dict, property->name, property->type, &property->value);
 }
 
 static DBusMessage *get_properties(DBusConnection *conn,
@@ -165,9 +208,11 @@ static DBusMessage *get_properties(DBusConnection *conn,
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
-       if (element->parent != NULL)
+       if (element->parent != NULL &&
+                       element->parent->type != CONNMAN_ELEMENT_TYPE_ROOT) {
                append_entry(&dict, "Parent",
                                DBUS_TYPE_OBJECT_PATH, &element->parent->path);
+       }
 
        str = type2string(element->type);
        if (str != NULL)
@@ -196,12 +241,16 @@ static DBusMessage *get_properties(DBusConnection *conn,
                append_entry(&dict, "IPv4.Gateway",
                                DBUS_TYPE_STRING, &element->ipv4.gateway);
 
+       connman_element_lock(element);
+
        for (list = element->properties; list; list = list->next) {
                struct connman_property *property = list->data;
 
                append_property(&dict, property);
        }
 
+       connman_element_unlock(element);
+
        dbus_message_iter_close_container(&array, &dict);
 
        return reply;
@@ -210,6 +259,91 @@ static DBusMessage *get_properties(DBusConnection *conn,
 static DBusMessage *set_property(DBusConnection *conn,
                                        DBusMessage *msg, void *data)
 {
+       struct connman_element *element = data;
+       DBusMessageIter iter;
+       DBusMessageIter value;
+       const char *name;
+       GSList *list;
+
+       DBG("conn %p", conn);
+
+       if (dbus_message_iter_init(msg, &iter) == FALSE)
+               return __connman_error_invalid_arguments(msg);
+
+       dbus_message_iter_get_basic(&iter, &name);
+       dbus_message_iter_next(&iter);
+       dbus_message_iter_recurse(&iter, &value);
+
+       if (__connman_security_check_privileges(msg) < 0)
+               return __connman_error_permission_denied(msg);
+
+       connman_element_lock(element);
+
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+               const char *str;
+
+               if (g_str_equal(property->name, name) == FALSE)
+                       continue;
+
+               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
+                       continue;
+
+               property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+               if (property->type == DBUS_TYPE_STRING) {
+                       dbus_message_iter_get_basic(&value, &str);
+                       g_free(property->value);
+                       property->value = g_strdup(str);
+               } else
+                       property->value = NULL;
+       }
+
+       connman_element_unlock(element);
+
+       return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+}
+
+static DBusMessage *clear_property(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       struct connman_element *element = data;
+       const char *name;
+       GSList *list;
+
+       DBG("conn %p", conn);
+
+       if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
+                                               DBUS_TYPE_INVALID) == FALSE)
+               return __connman_error_invalid_arguments(msg);
+
+       if (__connman_security_check_privileges(msg) < 0)
+               return __connman_error_permission_denied(msg);
+
+       connman_element_lock(element);
+
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+
+               if (g_str_equal(property->name, name) == FALSE)
+                       continue;
+
+               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
+                       continue;
+
+               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
+                       continue;
+
+               property->flags |= CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+               if (property->type == DBUS_TYPE_STRING)
+                       g_free(property->value);
+
+               property->value = NULL;
+       }
+
+       connman_element_unlock(element);
+
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
@@ -270,6 +404,7 @@ static DBusMessage *do_disable(DBusConnection *conn,
 static GDBusMethodTable element_methods[] = {
        { "GetProperties", "",   "a{sv}", get_properties },
        { "SetProperty",   "sv", "",      set_property   },
+       { "ClearProperty", "s",  "",      clear_property },
        { "Update",        "",   "",      do_update      },
        { "Enable",        "",   "",      do_enable      },
        { "Disable",       "",   "",      do_disable     },
@@ -435,11 +570,22 @@ void connman_driver_unregister(struct connman_driver *driver)
        g_static_rw_lock_writer_unlock(&element_lock);
 }
 
-struct connman_element *connman_element_create(void)
+/**
+ * connman_element_create:
+ * @name: element name
+ *
+ * Allocate a new element and assign the given #name to it. If the name
+ * is #NULL, it will be later on created based on the element type.
+ *
+ * Returns: a newly-allocated #connman_element structure
+ */
+struct connman_element *connman_element_create(const char *name)
 {
        struct connman_element *element;
 
-       element = g_new0(struct connman_element, 1);
+       element = g_try_new0(struct connman_element, 1);
+       if (element == NULL)
+               return NULL;
 
        DBG("element %p", element);
 
@@ -447,14 +593,13 @@ struct connman_element *connman_element_create(void)
 
        g_static_mutex_init(&element->mutex);
 
+       element->name    = g_strdup(name);
        element->type    = CONNMAN_ELEMENT_TYPE_UNKNOWN;
        element->subtype = CONNMAN_ELEMENT_SUBTYPE_UNKNOWN;
        element->state   = CONNMAN_ELEMENT_STATE_CLOSED;
-
+       element->index   = -1;
        element->enabled = FALSE;
 
-       element->netdev.index = -1;
-
        return element;
 }
 
@@ -468,24 +613,39 @@ struct connman_element *connman_element_ref(struct connman_element *element)
        return element;
 }
 
-void connman_element_unref(struct connman_element *element)
+static void free_properties(struct connman_element *element)
 {
-       DBG("element %p name %s refcount %d", element, element->name,
-                               g_atomic_int_get(&element->refcount) - 1);
+       GSList *list;
 
-       if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
-               GSList *list;
+       DBG("element %p name %s", element, element->name);
+
+       connman_element_lock(element);
 
-               for (list = element->properties; list; list = list->next) {
-                       struct connman_property *property = list->data;
-                       if ((property->flags & CONNMAN_PROPERTY_FLAG_STATIC) &&
-                                       property->type == DBUS_TYPE_STRING)
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+
+               if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)) {
+                       if (property->type == DBUS_TYPE_STRING)
                                g_free(property->value);
-                       g_free(property);
-                       list->data = NULL;
                }
-               g_slist_free(element->properties);
 
+               g_free(property);
+       }
+
+       g_slist_free(element->properties);
+
+       element->properties = NULL;
+
+       connman_element_unlock(element);
+}
+
+void connman_element_unref(struct connman_element *element)
+{
+       DBG("element %p name %s refcount %d", element, element->name,
+                               g_atomic_int_get(&element->refcount) - 1);
+
+       if (g_atomic_int_dec_and_test(&element->refcount) == TRUE) {
+               free_properties(element);
                g_free(element->ipv4.address);
                g_free(element->ipv4.netmask);
                g_free(element->ipv4.gateway);
@@ -493,7 +653,6 @@ void connman_element_unref(struct connman_element *element)
                g_free(element->ipv4.broadcast);
                g_free(element->ipv4.nameserver);
                g_free(element->network.identifier);
-               g_free(element->netdev.name);
                g_free(element->path);
                g_free(element->name);
                g_free(element);
@@ -515,9 +674,9 @@ int connman_element_add_static_property(struct connman_element *element,
                return -ENOMEM;
 
        property->flags = CONNMAN_PROPERTY_FLAG_STATIC;
-
-       property->name = g_strdup(name);
-       property->type = type;
+       property->id    = CONNMAN_PROPERTY_ID_INVALID;
+       property->name  = g_strdup(name);
+       property->type  = type;
 
        DBG("name %s type %d value %p", name, type, value);
 
@@ -534,9 +693,166 @@ int connman_element_add_static_property(struct connman_element *element,
        return 0;
 }
 
+static void *get_reference_value(struct connman_element *element,
+                                               enum connman_property_id id)
+{
+       GSList *list;
+
+       DBG("element %p name %s", element, element->name);
+
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+
+               if (property->id != id)
+                       continue;
+
+               if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
+                       return property->value;
+       }
+
+       if (element->parent == NULL)
+               return NULL;
+
+       return get_reference_value(element->parent, id);
+}
+
+static void set_reference_properties(struct connman_element *element)
+{
+       GSList *list;
+
+       DBG("element %p name %s", element, element->name);
+
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+
+               if (!(property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE))
+                       continue;
+
+               property->value = get_reference_value(element->parent,
+                                                               property->id);
+       }
+}
+
+static struct connman_property *create_property(struct connman_element *element,
+                                               enum connman_property_id id)
+{
+       struct connman_property *property;
+       GSList *list;
+
+       DBG("element %p name %s", element, element->name);
+
+       connman_element_lock(element);
+
+       for (list = element->properties; list; list = list->next) {
+               property = list->data;
+
+               if (property->id == id)
+                       goto unlock;
+       }
+
+       property = g_try_new0(struct connman_property, 1);
+       if (property == NULL)
+               goto unlock;
+
+       property->flags = CONNMAN_PROPERTY_FLAG_REFERENCE;
+       property->id    = id;
+       property->name  = g_strdup(propid2name(id));
+       property->type  = propid2type(id);
+
+       if (property->name == NULL) {
+               g_free(property);
+               property = NULL;
+               goto unlock;
+       }
+
+       element->properties = g_slist_append(element->properties, property);
+
+unlock:
+       connman_element_unlock(element);
+
+       return property;
+}
+
+static void create_default_properties(struct connman_element *element)
+{
+       struct connman_property *property;
+       int i;
+
+       DBG("element %p name %s", element, element->name);
+
+       for (i = 0; propid_table[i].name; i++) {
+               DBG("property %s", propid_table[i].name);
+
+               property = create_property(element, propid_table[i].id);
+
+               property->flags &= ~CONNMAN_PROPERTY_FLAG_REFERENCE;
+
+               if (propid_table[i].type != DBUS_TYPE_STRING)
+                       continue;
+
+               if (propid_table[i].value)
+                       property->value = g_strdup(propid_table[i].value);
+               else
+                       property->value = g_strdup("");
+       }
+}
+
+static int define_properties_valist(struct connman_element *element,
+                                                               va_list args)
+{
+       enum connman_property_id id;
+
+       DBG("element %p name %s", element, element->name);
+
+       id = va_arg(args, enum connman_property_id);
+
+       while (id != CONNMAN_PROPERTY_ID_INVALID) {
+
+               DBG("property %d", id);
+
+               create_property(element, id);
+
+               id = va_arg(args, enum connman_property_id);
+       }
+
+       return 0;
+}
+
+/**
+ * connman_element_define_properties:
+ * @element: an element
+ * @varargs: list of property identifiers
+ *
+ * Define the valid properties for an element.
+ *
+ * Returns: %0 on success
+ */
+int connman_element_define_properties(struct connman_element *element, ...)
+{
+       va_list args;
+       int err;
+
+       DBG("element %p name %s", element, element->name);
+
+       va_start(args, element);
+
+       err = define_properties_valist(element, args);
+
+       va_end(args);
+
+       return err;
+}
+
+int connman_element_create_property(struct connman_element *element,
+                                               const char *name, int type)
+{
+       return -EIO;
+}
+
 int connman_element_set_property(struct connman_element *element,
-                       enum connman_property_type type, const void *value)
+                               enum connman_property_id id, const void *value)
 {
+#if 0
        switch (type) {
        case CONNMAN_PROPERTY_TYPE_INVALID:
                return -EINVAL;
@@ -565,6 +881,7 @@ int connman_element_set_property(struct connman_element *element,
                connman_element_unlock(element);
                break;
        }
+#endif
 
        g_dbus_emit_signal(connection, CONNMAN_MANAGER_PATH,
                                CONNMAN_MANAGER_INTERFACE, "ElementUpdated",
@@ -575,11 +892,12 @@ int connman_element_set_property(struct connman_element *element,
 }
 
 int connman_element_get_value(struct connman_element *element,
-                               enum connman_property_type type, void *value)
+                               enum connman_property_id id, void *value)
 {
        if (element->type == CONNMAN_ELEMENT_TYPE_ROOT)
                return -EINVAL;
 
+#if 0
        switch (type) {
        case CONNMAN_PROPERTY_TYPE_INVALID:
                return -EINVAL;
@@ -616,18 +934,29 @@ int connman_element_get_value(struct connman_element *element,
                connman_element_unlock(element);
                break;
        }
+#endif
 
        return 0;
 }
 
+/**
+ * connman_element_register:
+ * @element: the element to register
+ * @parent: the parent to register the element with
+ *
+ * Register an element with the core. It will be register under the given
+ * parent of if %NULL is provided under the root element.
+ *
+ * Returns: %0 on success
+ */
 int connman_element_register(struct connman_element *element,
                                        struct connman_element *parent)
 {
        DBG("element %p name %s parent %p", element, element->name, parent);
 
        if (device_filter && element->type == CONNMAN_ELEMENT_TYPE_DEVICE) {
-               if (g_str_equal(device_filter, element->netdev.name) == FALSE)
-                       return -EINVAL;
+               if (g_str_equal(device_filter, element->name) == FALSE)
+                       return -EPERM;
        }
 
        if (connman_element_ref(element) == NULL)
@@ -639,8 +968,10 @@ int connman_element_register(struct connman_element *element,
 
        if (element->name == NULL) {
                element->name = g_strdup(type2string(element->type));
-               if (element->name == NULL)
+               if (element->name == NULL) {
+                       connman_element_unlock(element);
                        return -EINVAL;
+               }
        }
 
        element->parent = parent;
@@ -722,12 +1053,16 @@ static void register_element(gpointer data, gpointer user_data)
                if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_UNKNOWN)
                        element->subtype = element->parent->subtype;
        } else {
+               element->parent = element_root->data;
+
                node = element_root;
                basepath = "";
        }
 
        element->path = g_strdup_printf("%s/%s", basepath, element->name);
 
+       set_reference_properties(element);
+
        connman_element_unlock(element);
 
        DBG("element %p path %s", element, element->path);
@@ -857,12 +1192,13 @@ int __connman_element_init(DBusConnection *conn, const char *device)
 
        g_static_rw_lock_writer_lock(&element_lock);
 
-       element = connman_element_create();
+       element = connman_element_create("root");
 
-       element->name = g_strdup("root");
        element->path = g_strdup("/");
        element->type = CONNMAN_ELEMENT_TYPE_ROOT;
 
+       create_default_properties(element);
+
        element_root = g_node_new(element);
 
        g_static_rw_lock_writer_unlock(&element_lock);