Add fully dynamic property storage capabilities
authorMarcel Holtmann <marcel@holtmann.org>
Wed, 11 Mar 2009 22:45:25 +0000 (23:45 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Wed, 11 Mar 2009 22:45:25 +0000 (23:45 +0100)
include/element.h
include/property.h
src/connection.c
src/device.c
src/element.c
src/network.c

index 8bdc42c..d8fa88c 100644 (file)
@@ -30,6 +30,7 @@ extern "C" {
 #include <glib.h>
 
 #include <connman/property.h>
+#include <connman/types.h>
 #include <connman/ipv4.h>
 
 /**
@@ -97,20 +98,21 @@ 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_set_static_property(struct connman_element *element,
-                               const char *name, int type, const void *value);
-extern int connman_element_set_static_array_property(struct connman_element *element,
-                       const char *name, int type, const void *value, int len);
-extern int connman_element_set_property(struct connman_element *element,
-                               enum connman_property_id id, const void *value);
 extern int connman_element_get_value(struct connman_element *element,
                                enum connman_property_id id, void *value);
-extern gboolean connman_element_get_static_property(struct connman_element *element,
-                                               const char *name, void *value);
-extern gboolean connman_element_get_static_array_property(struct connman_element *element,
-                                       const char *name, void *value, int *len);
-extern gboolean connman_element_match_static_property(struct connman_element *element,
-                                       const char *name, const void *value);
+
+extern int connman_element_set_string(struct connman_element *element,
+                                       const char *key, const char *value);
+extern const char *connman_element_get_string(struct connman_element *element,
+                                                       const char *key);
+extern int connman_element_set_uint8(struct connman_element *element,
+                               const char *key, connman_uint8_t value);
+extern connman_uint8_t connman_element_get_uint8(struct connman_element *element,
+                                                       const char *key);
+extern int connman_element_set_blob(struct connman_element *element,
+                       const char *key, const void *data, unsigned int size);
+extern const void *connman_element_get_blob(struct connman_element *element,
+                                       const char *key, unsigned int *size);
 
 extern int connman_element_register(struct connman_element *element,
                                        struct connman_element *parent);
index ec8254c..b4c7e18 100644 (file)
@@ -43,12 +43,19 @@ enum connman_property_id {
        CONNMAN_PROPERTY_ID_IPV4_NAMESERVER,
 };
 
+enum connman_property_type {
+       CONNMAN_PROPERTY_TYPE_INVALID = 0,
+       CONNMAN_PROPERTY_TYPE_STRING,
+       CONNMAN_PROPERTY_TYPE_UINT8,
+       CONNMAN_PROPERTY_TYPE_BLOB,
+};
+
 struct connman_property {
        enum connman_property_id id;
        int type;
        int subtype;
        void *value;
-       int size;
+       unsigned int size;
 };
 
 #ifdef __cplusplus
index d102380..79890dc 100644 (file)
@@ -300,9 +300,9 @@ static DBusMessage *get_properties(DBusConnection *conn,
        struct connman_element *element = data;
        DBusMessage *reply;
        DBusMessageIter array, dict;
-       connman_uint8_t strength = 0;
+       connman_uint8_t strength;
        const char *device, *network;
-       const char *type = NULL;
+       const char *type;
 
        DBG("conn %p", conn);
 
@@ -321,13 +321,12 @@ static DBusMessage *get_properties(DBusConnection *conn,
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
-       connman_element_get_static_property(element, "Type", &type);
-
+       type = connman_element_get_string(element, "Type");
        if (type != NULL)
                connman_dbus_dict_append_variant(&dict, "Type",
                                                DBUS_TYPE_STRING, &type);
 
-       connman_element_get_static_property(element, "Strength", &strength);
+       strength = connman_element_get_uint8(element, "Strength");
        if (strength > 0)
                connman_dbus_dict_append_variant(&dict, "Strength",
                                                DBUS_TYPE_BYTE, &strength);
index 3094038..010a115 100644 (file)
@@ -845,8 +845,7 @@ struct connman_device *connman_device_create(const char *node,
 
        str = type2string(type);
        if (str != NULL)
-               connman_element_set_static_property(&device->element,
-                                       "Type", DBUS_TYPE_STRING, &str);
+               connman_element_set_string(&device->element, "Type", str);
 
        device->element.ipv4.method = CONNMAN_IPV4_METHOD_DHCP;
 
@@ -1386,7 +1385,7 @@ int connman_device_set_string(struct connman_device *device,
                device->node = g_strdup(value);
        }
 
-       return 0;
+       return connman_element_set_string(&device->element, key, value);
 }
 
 /**
@@ -1406,7 +1405,7 @@ const char *connman_device_get_string(struct connman_device *device,
        else if (g_str_equal(key, "Node") == TRUE)
                return device->node;
 
-       return NULL;
+       return connman_element_get_string(&device->element, key);
 }
 
 static void set_offlinemode(struct connman_element *element, gpointer user_data)
index 9a2ec3b..614c875 100644 (file)
@@ -510,7 +510,7 @@ void connman_element_unref(struct connman_element *element)
        }
 }
 
-int connman_element_set_static_property(struct connman_element *element,
+static int set_static_property(struct connman_element *element,
                                const char *name, int type, const void *value)
 {
        struct connman_property *property;
@@ -549,7 +549,7 @@ int connman_element_set_static_property(struct connman_element *element,
        return 0;
 }
 
-int connman_element_set_static_array_property(struct connman_element *element,
+static int set_static_array_property(struct connman_element *element,
                        const char *name, int type, const void *value, int len)
 {
        struct connman_property *property;
@@ -589,7 +589,8 @@ int connman_element_set_static_array_property(struct connman_element *element,
        return 0;
 }
 
-int connman_element_set_property(struct connman_element *element,
+#if 0
+static int set_property(struct connman_element *element,
                                enum connman_property_id id, const void *value)
 {
        switch (id) {
@@ -629,6 +630,7 @@ int connman_element_set_property(struct connman_element *element,
 
        return 0;
 }
+#endif
 
 int connman_element_get_value(struct connman_element *element,
                                enum connman_property_id id, void *value)
@@ -692,7 +694,7 @@ int connman_element_get_value(struct connman_element *element,
        return 0;
 }
 
-gboolean connman_element_get_static_property(struct connman_element *element,
+static gboolean get_static_property(struct connman_element *element,
                                                const char *name, void *value)
 {
        struct connman_property *property;
@@ -719,14 +721,13 @@ gboolean connman_element_get_static_property(struct connman_element *element,
        __connman_element_unlock(element);
 
        if (found == FALSE && element->parent != NULL)
-               return connman_element_get_static_property(element->parent,
-                                                               name, value);
+               return get_static_property(element->parent, name, value);
 
        return found;
 }
 
-gboolean connman_element_get_static_array_property(struct connman_element *element,
-                                       const char *name, void *value, int *len)
+static gboolean get_static_array_property(struct connman_element *element,
+                       const char *name, void *value, unsigned int *len)
 {
        struct connman_property *property;
        gboolean found = FALSE;
@@ -747,7 +748,8 @@ gboolean connman_element_get_static_array_property(struct connman_element *eleme
        return found;
 }
 
-gboolean connman_element_match_static_property(struct connman_element *element,
+#if 0
+static gboolean match_static_property(struct connman_element *element,
                                        const char *name, const void *value)
 {
        struct connman_property *property;
@@ -768,6 +770,106 @@ gboolean connman_element_match_static_property(struct connman_element *element,
 
        return result;
 }
+#endif
+
+/**
+ * connman_element_set_string:
+ * @element: element structure
+ * @key: unique identifier
+ * @value: string value
+ *
+ * Set string value for specific key
+ */
+int connman_element_set_string(struct connman_element *element,
+                                       const char *key, const char *value)
+{
+       return set_static_property(element, key, DBUS_TYPE_STRING, &value);
+}
+
+/**
+ * connman_element_get_string:
+ * @element: element structure
+ * @key: unique identifier
+ *
+ * Get string value for specific key
+ */
+const char *connman_element_get_string(struct connman_element *element,
+                                                       const char *key)
+{
+       const char *value;
+
+       if (get_static_property(element, key, &value) == FALSE)
+               return NULL;
+
+       return value;
+}
+
+/**
+ * connman_element_set_uint8:
+ * @element: element structure
+ * @key: unique identifier
+ * @value: integer value
+ *
+ * Set integer value for specific key
+ */
+int connman_element_set_uint8(struct connman_element *element,
+                                       const char *key, connman_uint8_t value)
+{
+        return set_static_property(element, key, DBUS_TYPE_BYTE, &value);
+}
+
+/**
+ * connman_element_get_uint8:
+ * @element: element structure
+ * @key: unique identifier
+ *
+ * Get integer value for specific key
+ */
+connman_uint8_t connman_element_get_uint8(struct connman_element *element,
+                                                       const char *key)
+{
+       connman_uint8_t value;
+
+       if (get_static_property(element, key, &value) == FALSE)
+                return 0;
+
+       return value;
+}
+
+/**
+ * connman_element_set_blob:
+ * @element: element structure
+ * @key: unique identifier
+ * @data: blob data
+ * @size: blob size
+ *
+ * Set binary blob value for specific key
+ */
+int connman_element_set_blob(struct connman_element *element,
+                       const char *key, const void *data, unsigned int size)
+{
+       return set_static_array_property(element, key,
+                                               DBUS_TYPE_BYTE, data, size);
+}
+
+/**
+ * connman_element_get_blob:
+ * @element: element structure
+ * @key: unique identifier
+ * @size: pointer to blob size
+ *
+ * Get binary blob value for specific key
+ */
+const void *connman_element_get_blob(struct connman_element *element,
+                                       const char *key, unsigned int *size)
+{
+       void *value;
+
+       if (get_static_array_property(element, key, &value, size) == FALSE)
+               return NULL;
+
+       return value;
+}
 
 int __connman_element_append_ipv4(struct connman_element *element,
                                                DBusMessageIter *dict)
index 2618dad..1ef7486 100644 (file)
@@ -481,11 +481,9 @@ struct connman_network *connman_network_create(const char *identifier,
 
        str = type2string(type);
        if (str != NULL)
-               connman_element_set_static_property(&network->element,
-                                       "Type", DBUS_TYPE_STRING, &str);
+               connman_element_set_string(&network->element, "Type", str);
 
-       connman_element_set_static_property(&network->element,
-                                       "Strength", DBUS_TYPE_BYTE, &strength);
+       connman_element_set_uint8(&network->element, "Strength", strength);
 
        network->type = type;
        network->identifier = g_strdup(identifier);
@@ -855,7 +853,7 @@ int connman_network_set_string(struct connman_network *network,
                network->wifi.passphrase = g_strdup(value);
        }
 
-       return 0;
+       return connman_element_set_string(&network->element, key, value);
 }
 
 /**
@@ -881,7 +879,7 @@ const char *connman_network_get_string(struct connman_network *network,
        else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
                return network->wifi.passphrase;
 
-       return NULL;
+       return connman_element_get_string(&network->element, key);
 }
 
 /**
@@ -899,13 +897,10 @@ int connman_network_set_uint8(struct connman_network *network,
 
        if (g_str_equal(key, "Priority") == TRUE)
                network->priority = value;
-       else if (g_str_equal(key, "Strength") == TRUE) {
+       else if (g_str_equal(key, "Strength") == TRUE)
                network->strength = value;
-               connman_element_set_static_property(&network->element,
-                                       "Strength", DBUS_TYPE_BYTE, &value);
-       }
 
-       return 0;
+       return connman_element_set_uint8(&network->element, key, value);
 }
 
 /**
@@ -925,7 +920,7 @@ connman_uint8_t connman_network_get_uint8(struct connman_network *network,
        else if (g_str_equal(key, "Strength") == TRUE)
                return network->strength;
 
-       return 0;
+       return connman_element_get_uint8(&network->element, key);
 }
 
 /**
@@ -952,7 +947,7 @@ int connman_network_set_blob(struct connman_network *network,
                        network->wifi.ssid_len = 0;
        }
 
-       return 0;
+       return connman_element_set_blob(&network->element, key, data, size);
 }
 
 /**
@@ -974,7 +969,7 @@ const void *connman_network_get_blob(struct connman_network *network,
                return network->wifi.ssid;
        }
 
-       return NULL;
+       return connman_element_get_blob(&network->element, key, size);
 }
 
 void __connman_network_set_device(struct connman_network *network,