blobmsg: add support for double
authorAndré Gaul <andre@gaul.io>
Sat, 19 Nov 2016 17:55:49 +0000 (18:55 +0100)
committerFelix Fietkau <nbd@nbd.name>
Wed, 4 Jan 2017 20:36:31 +0000 (21:36 +0100)
This adds support for double floating point type to make it more JSON
compatible. For type checking it also adds a stub BLOB_ATTR_DOUBLE type.
If necessary, the accessor functions for blob can be added later

Signed-off-by: André Gaul <andre@gaul.io>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
blob.c
blob.h
blobmsg.c
blobmsg.h
blobmsg_json.c
examples/blobmsg-example.c

diff --git a/blob.c b/blob.c
index ec8617bef8c725c5efae727753d5f4e817ee2bb0..03d5e9c27ee73ec52bd30514a0cd6044cb7c6cf1 100644 (file)
--- a/blob.c
+++ b/blob.c
@@ -192,6 +192,7 @@ static const int blob_type_minlen[BLOB_ATTR_LAST] = {
        [BLOB_ATTR_INT16] = sizeof(uint16_t),
        [BLOB_ATTR_INT32] = sizeof(uint32_t),
        [BLOB_ATTR_INT64] = sizeof(uint64_t),
+       [BLOB_ATTR_DOUBLE] = sizeof(double),
 };
 
 bool
diff --git a/blob.h b/blob.h
index 307523d0cad535d79a398cc1c15ac0b37e8ddabd..a092f5dabca6ac3bc3c9a6bc579ae58240dcaa6a 100644 (file)
--- a/blob.h
+++ b/blob.h
@@ -39,6 +39,7 @@ enum {
        BLOB_ATTR_INT16,
        BLOB_ATTR_INT32,
        BLOB_ATTR_INT64,
+       BLOB_ATTR_DOUBLE,
        BLOB_ATTR_LAST
 };
 
index 1e93376a27b65cf816f94f87738b98e8bb497ef6..c2bb7179fb66c15afd468e36e23426c927ea0f46 100644 (file)
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -20,6 +20,7 @@ static const int blob_type[__BLOBMSG_TYPE_LAST] = {
        [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
        [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
        [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
+       [BLOBMSG_TYPE_DOUBLE] = BLOB_ATTR_DOUBLE,
        [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
        [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
 };
index 861a4e8db9336f81056b18e01809c3a0c08dbfec..797729870fbd449b057315dedf54b07032fe4fca 100644 (file)
--- a/blobmsg.h
+++ b/blobmsg.h
@@ -31,6 +31,7 @@ enum blobmsg_type {
        BLOBMSG_TYPE_INT32,
        BLOBMSG_TYPE_INT16,
        BLOBMSG_TYPE_INT8,
+       BLOBMSG_TYPE_DOUBLE,
        __BLOBMSG_TYPE_LAST,
        BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
        BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
@@ -113,6 +114,18 @@ int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
 int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
                       const void *data, unsigned int len);
 
+static inline int
+blobmsg_add_double(struct blob_buf *buf, const char *name, double val)
+{
+       union {
+               double d;
+               uint64_t u64;
+       } v;
+       v.d = val;
+       v.u64 = cpu_to_be64(v.u64);
+       return blobmsg_add_field(buf, BLOBMSG_TYPE_DOUBLE, name, &v.u64, 8);
+}
+
 static inline int
 blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
 {
@@ -212,6 +225,16 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
        return tmp;
 }
 
+static inline double blobmsg_get_double(struct blob_attr *attr)
+{
+       union {
+               double d;
+               uint64_t u64;
+       } v;
+       v.u64 = blobmsg_get_u64(attr);
+       return v.d;
+}
+
 static inline char *blobmsg_get_string(struct blob_attr *attr)
 {
        if (!attr)
index 8a6ed8f8e44342495bb1038e26ed508ea54ea5b3..ca9dd1a4a8eff1f959e0ae193e45d4e528e7e04e 100644 (file)
@@ -69,6 +69,9 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
        case json_type_int:
                blobmsg_add_u32(b, name, json_object_get_int(obj));
                break;
+       case json_type_double:
+               blobmsg_add_double(b, name, json_object_get_double(obj));
+               break;
        case json_type_null:
                blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
                break;
@@ -255,6 +258,9 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo
        case BLOBMSG_TYPE_INT64:
                sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data));
                break;
+       case BLOBMSG_TYPE_DOUBLE:
+               sprintf(buf, "%lf", blobmsg_get_double(attr));
+               break;
        case BLOBMSG_TYPE_STRING:
                blobmsg_format_string(s, data);
                return;
index 01b05181ebc8704491c7cd70617f94e76c66df80..1c8601735fd0be06663b638aa7a19ef663e82380 100644 (file)
@@ -49,6 +49,9 @@ static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
        case BLOBMSG_TYPE_INT64:
                indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data));
                break;
+       case BLOBMSG_TYPE_DOUBLE:
+               indent_printf(indent, "%lf\n", blobmsg_get_double(data));
+               break;
        case BLOBMSG_TYPE_TABLE:
        case BLOBMSG_TYPE_ARRAY:
                if (!indent)
@@ -113,6 +116,7 @@ fill_message(struct blob_buf *buf)
        blobmsg_add_string(buf, "message", "Hello, world!");
 
        tbl = blobmsg_open_table(buf, "testdata");
+       blobmsg_add_double(buf, "double", 1.337e2);
        blobmsg_add_u32(buf, "hello", 1);
        blobmsg_add_string(buf, "world", "2");
        blobmsg_close_table(buf, tbl);
@@ -121,6 +125,7 @@ fill_message(struct blob_buf *buf)
        blobmsg_add_u32(buf, NULL, 0);
        blobmsg_add_u32(buf, NULL, 1);
        blobmsg_add_u32(buf, NULL, 2);
+       blobmsg_add_double(buf, "double", 1.337e2);
        blobmsg_close_table(buf, tbl);
 }