Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion blobmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

static const int blob_type[__BLOBMSG_TYPE_LAST] = {
[BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
[BLOBMSG_TYPE_BOOL] = BLOB_ATTR_INT8,
[BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
[BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
[BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
Expand Down Expand Up @@ -210,7 +211,10 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,

if (policy[i].type != BLOBMSG_TYPE_UNSPEC &&
policy[i].type != BLOBMSG_CAST_INT64 &&
blob_id(attr) != policy[i].type)
blob_id(attr) != policy[i].type &&
// For backward compatibility, accept BLOBMSG_TYPE_INT8 when the policy expects the new BLOBMSG_TYPE_BOOL.
!(policy[i].type == BLOBMSG_TYPE_BOOL &&
blob_id(attr) == BLOBMSG_TYPE_INT8))
continue;

if (policy[i].type == BLOBMSG_CAST_INT64 &&
Expand Down
11 changes: 10 additions & 1 deletion blobmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum blobmsg_type {
BLOBMSG_TYPE_INT32,
BLOBMSG_TYPE_INT16,
BLOBMSG_TYPE_INT8,
BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
BLOBMSG_TYPE_BOOL,
BLOBMSG_TYPE_DOUBLE,
__BLOBMSG_TYPE_LAST,
BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
Expand Down Expand Up @@ -231,6 +231,13 @@ blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8);
}

static inline int
blobmsg_add_bool(struct blob_buf *buf, const char *name, bool val)
{
uint8_t v = val ? 1 : 0;
return blobmsg_add_field(buf, BLOBMSG_TYPE_BOOL, name, &v, 1);
}

static inline int
blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
{
Expand Down Expand Up @@ -319,6 +326,7 @@ static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr)
tmp = blobmsg_get_u16(attr);
break;
case BLOBMSG_TYPE_INT8:
case BLOBMSG_TYPE_BOOL:
tmp = blobmsg_get_u8(attr);
break;
default:
Expand All @@ -345,6 +353,7 @@ static inline int64_t blobmsg_cast_s64(struct blob_attr *attr)
tmp = (int16_t) blobmsg_get_u16(attr);
break;
case BLOBMSG_TYPE_INT8:
case BLOBMSG_TYPE_BOOL:
tmp = (int8_t) blobmsg_get_u8(attr);
break;
default:
Expand Down
5 changes: 4 additions & 1 deletion blobmsg_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
blobmsg_add_string(b, name, json_object_get_string(obj));
break;
case json_type_boolean:
blobmsg_add_u8(b, name, json_object_get_boolean(obj));
blobmsg_add_bool(b, name, json_object_get_boolean(obj));
break;
case json_type_int: {
int64_t i64 = json_object_get_int64(obj);
Expand Down Expand Up @@ -262,6 +262,9 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo
case BLOBMSG_TYPE_BOOL:
snprintf(buf, sizeof(buf), "%s", *(uint8_t *)data ? "true" : "false");
break;
case BLOBMSG_TYPE_INT8:
snprintf(buf, sizeof(buf), "%" PRId8, *(int8_t *)data);
break;
case BLOBMSG_TYPE_INT16:
snprintf(buf, sizeof(buf), "%" PRId16, (int16_t) be16_to_cpu(*(uint16_t *)data));
break;
Expand Down
104 changes: 68 additions & 36 deletions tests/cram/test_blobmsg.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
Expand All @@ -33,17 +35,19 @@ check that blobmsg is producing expected results:
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308]}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":0,"poo":100,"moo-min":-128,"moo-max":127,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2","flag-true":true,"flag-false":false},"list":[0,100,-128,127,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308,true,false]}

[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
0 (i32)
100 (i32)
-128 (i32)
127 (i32)
-32768 (i32)
32767 (i32)
-2147483648 (i32)
Expand All @@ -52,21 +56,25 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
\tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tfoo : 0 (i32) (esc)
\tpoo : 100 (i32) (esc)
\tmoo-min : -128 (i32) (esc)
\tmoo-max : 127 (i32) (esc)
\tbar-min : -32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

$ test-blobmsg-san
Expand All @@ -85,6 +93,8 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
Expand All @@ -100,17 +110,19 @@ check that blobmsg is producing expected results:
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308]}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":0,"poo":100,"moo-min":-128,"moo-max":127,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2","flag-true":true,"flag-false":false},"list":[0,100,-128,127,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308,true,false]}

[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
0 (i32)
100 (i32)
-128 (i32)
127 (i32)
-32768 (i32)
32767 (i32)
-2147483648 (i32)
Expand All @@ -119,21 +131,25 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
\tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tfoo : 0 (i32) (esc)
\tpoo : 100 (i32) (esc)
\tmoo-min : -128 (i32) (esc)
\tmoo-max : 127 (i32) (esc)
\tbar-min : -32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

$ test-blobmsg-san
Expand All @@ -152,6 +168,8 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
Expand All @@ -167,17 +185,19 @@ check that blobmsg is producing expected results:
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308]}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":0,"poo":100,"moo-min":-128,"moo-max":127,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2","flag-true":true,"flag-false":false},"list":[0,100,-128,127,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308,true,false]}

[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
0 (i32)
100 (i32)
-128 (i32)
127 (i32)
-32768 (i32)
32767 (i32)
-2147483648 (i32)
Expand All @@ -186,21 +206,25 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
\tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tfoo : 0 (i32) (esc)
\tpoo : 100 (i32) (esc)
\tmoo-min : -128 (i32) (esc)
\tmoo-max : 127 (i32) (esc)
\tbar-min : -32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

$ test-blobmsg-san
Expand All @@ -219,6 +243,8 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
Expand All @@ -234,17 +260,19 @@ check that blobmsg is producing expected results:
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}

[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308]}
[*] blobmsg to json: {"message":"Hello, world!","testdata":{"dbl-min":2.2250738585072014e-308,"dbl-max":1.7976931348623157e+308,"foo":0,"poo":100,"moo-min":-128,"moo-max":127,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2","flag-true":true,"flag-false":false},"list":[0,100,-128,127,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,2.2250738585072014e-308,1.7976931348623157e+308,true,false]}

[*] blobmsg from json:
Message: Hello, world!
List: {
0 (i8)
1 (i8)
1 (i8)
1 (i8)
0 (i32)
100 (i32)
-128 (i32)
127 (i32)
-32768 (i32)
32767 (i32)
-2147483648 (i32)
Expand All @@ -253,19 +281,23 @@ check that blobmsg is producing expected results:
9223372036854775807 (i64)
0.000000 (dbl)
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl)
true (bool)
false (bool)
}
Testdata: {
\tdbl-min : 0.000000 (dbl) (esc)
\tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc)
\tfoo : 0 (i8) (esc)
\tpoo : 1 (i8) (esc)
\tmoo-min : 1 (i8) (esc)
\tmoo-max : 1 (i8) (esc)
\tfoo : 0 (i32) (esc)
\tpoo : 100 (i32) (esc)
\tmoo-min : -128 (i32) (esc)
\tmoo-max : 127 (i32) (esc)
\tbar-min : -32768 (i32) (esc)
\tbar-max : 32767 (i32) (esc)
\tbaz-min : -2147483648 (i32) (esc)
\tbaz-max : 2147483647 (i32) (esc)
\ttaz-min : -9223372036854775808 (i64) (esc)
\ttaz-max : 9223372036854775807 (i64) (esc)
\tworld : 2 (str) (esc)
\tflag-true : true (bool) (esc)
\tflag-false : false (bool) (esc)
}
Loading
Loading