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
1 change: 1 addition & 0 deletions blobmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
9 changes: 8 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
2 changes: 1 addition & 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
1 change: 1 addition & 0 deletions tests/fuzz/test-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static void fuzz_blobmsg_parse(const uint8_t *data, size_t size)
BLOBMSG_TYPE_INT32,
BLOBMSG_TYPE_INT16,
BLOBMSG_TYPE_INT8,
BLOBMSG_TYPE_BOOL,
BLOBMSG_TYPE_DOUBLE,
BLOBMSG_TYPE_TROUBLE,
};
Expand Down
44 changes: 44 additions & 0 deletions tests/test-blobmsg-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum {
FOO_INT8_MIN,
FOO_DOUBLE_MAX,
FOO_DOUBLE_MIN,
FOO_BOOL_TRUE,
FOO_BOOL_FALSE,
__FOO_MAX
};

Expand Down Expand Up @@ -67,6 +69,14 @@ static const struct blobmsg_policy pol[] = {
.name = "double_min",
.type = BLOBMSG_TYPE_DOUBLE,
},
[FOO_BOOL_TRUE] = {
.name = "bool_true",
.type = BLOBMSG_TYPE_BOOL,
},
[FOO_BOOL_FALSE] = {
.name = "bool_false",
.type = BLOBMSG_TYPE_BOOL,
},
};

static const struct blobmsg_policy pol_json[] = {
Expand Down Expand Up @@ -114,6 +124,14 @@ static const struct blobmsg_policy pol_json[] = {
.name = "double_min",
.type = BLOBMSG_TYPE_DOUBLE,
},
[FOO_BOOL_TRUE] = {
.name = "bool_true",
.type = BLOBMSG_TYPE_BOOL,
},
[FOO_BOOL_FALSE] = {
.name = "bool_false",
.type = BLOBMSG_TYPE_BOOL,
},
};

#ifndef ARRAY_SIZE
Expand Down Expand Up @@ -150,6 +168,10 @@ static void dump_message(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %s\n", blobmsg_get_bool(tb[FOO_BOOL_TRUE]) ? "true" : "false");
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %s\n", blobmsg_get_bool(tb[FOO_BOOL_FALSE]) ? "true" : "false");
}

static void dump_message_cast_u64(struct blob_buf *buf)
Expand Down Expand Up @@ -182,6 +204,10 @@ static void dump_message_cast_u64(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_TRUE]));
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_FALSE]));
}

static void dump_message_cast_s64(struct blob_buf *buf)
Expand Down Expand Up @@ -214,6 +240,10 @@ static void dump_message_cast_s64(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_TRUE]));
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_FALSE]));
}

static void dump_message_json(struct blob_buf *buf)
Expand Down Expand Up @@ -250,6 +280,10 @@ static void dump_message_json(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %s\n", blobmsg_get_bool(tb[FOO_BOOL_TRUE]) ? "true" : "false");
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %s\n", blobmsg_get_bool(tb[FOO_BOOL_FALSE]) ? "true" : "false");
}

static void dump_message_cast_u64_json(struct blob_buf *buf)
Expand Down Expand Up @@ -282,6 +316,10 @@ static void dump_message_cast_u64_json(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_TRUE]));
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_FALSE]));
}

static void dump_message_cast_s64_json(struct blob_buf *buf)
Expand Down Expand Up @@ -314,6 +352,10 @@ static void dump_message_cast_s64_json(struct blob_buf *buf)
fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX]));
if (tb[FOO_DOUBLE_MIN])
fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN]));
if (tb[FOO_BOOL_TRUE])
fprintf(stderr, "bool_true: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_TRUE]));
if (tb[FOO_BOOL_FALSE])
fprintf(stderr, "bool_false: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_FALSE]));
}

static void
Expand All @@ -330,6 +372,8 @@ fill_message(struct blob_buf *buf)
blobmsg_add_u8(buf, "int8_min", INT8_MIN);
blobmsg_add_double(buf, "double_max", DBL_MAX);
blobmsg_add_double(buf, "double_min", DBL_MIN);
blobmsg_add_bool(buf, "bool_true", true);
blobmsg_add_bool(buf, "bool_false", false);
}

int main(int argc, char **argv)
Expand Down