2010-10-13 21:17:51 +02:00
|
|
|
#include <stdio.h>
|
2020-01-12 15:31:49 +01:00
|
|
|
#include <stdint.h>
|
2014-11-12 14:59:16 +01:00
|
|
|
#include <inttypes.h>
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
#include "blobmsg.h"
|
2012-10-21 03:40:53 +02:00
|
|
|
#include "blobmsg_json.h"
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
|
|
|
|
|
|
|
|
#define indent_printf(indent, ...) do { \
|
|
|
|
if (indent > 0) \
|
|
|
|
fwrite(indent_str, indent, 1, stderr); \
|
|
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
|
|
} while(0)
|
|
|
|
|
2014-07-03 12:56:40 +02:00
|
|
|
static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
static void
|
2019-06-10 08:47:38 +02:00
|
|
|
dump_table(struct blob_attr *head, size_t len, int indent, bool array)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
2014-07-03 12:28:21 +02:00
|
|
|
struct blob_attr *attr;
|
2010-10-13 21:17:51 +02:00
|
|
|
struct blobmsg_hdr *hdr;
|
|
|
|
|
|
|
|
indent_printf(indent, "{\n");
|
|
|
|
__blob_for_each_attr(attr, head, len) {
|
|
|
|
hdr = blob_data(attr);
|
2011-01-29 18:00:40 +01:00
|
|
|
if (!array)
|
|
|
|
indent_printf(indent + 1, "%s : ", hdr->name);
|
2014-07-03 12:56:40 +02:00
|
|
|
dump_attr_data(attr, 0, indent + 1);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
indent_printf(indent, "}\n");
|
|
|
|
}
|
|
|
|
|
2014-07-03 12:56:40 +02:00
|
|
|
static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
2014-07-03 12:56:40 +02:00
|
|
|
int type = blobmsg_type(data);
|
2010-10-13 21:17:51 +02:00
|
|
|
switch(type) {
|
|
|
|
case BLOBMSG_TYPE_STRING:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
case BLOBMSG_TYPE_INT8:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data));
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
case BLOBMSG_TYPE_INT16:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data));
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
case BLOBMSG_TYPE_INT32:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data));
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
case BLOBMSG_TYPE_INT64:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data));
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
2016-11-19 18:55:49 +01:00
|
|
|
case BLOBMSG_TYPE_DOUBLE:
|
2020-01-12 15:31:49 +01:00
|
|
|
indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
|
2016-11-19 18:55:49 +01:00
|
|
|
break;
|
2010-10-13 21:17:51 +02:00
|
|
|
case BLOBMSG_TYPE_TABLE:
|
|
|
|
case BLOBMSG_TYPE_ARRAY:
|
|
|
|
if (!indent)
|
|
|
|
indent_printf(indent, "\n");
|
2014-07-03 12:56:40 +02:00
|
|
|
dump_table(blobmsg_data(data), blobmsg_data_len(data),
|
|
|
|
next_indent, type == BLOBMSG_TYPE_ARRAY);
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FOO_MESSAGE,
|
|
|
|
FOO_LIST,
|
|
|
|
FOO_TESTDATA
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct blobmsg_policy pol[] = {
|
|
|
|
[FOO_MESSAGE] = {
|
|
|
|
.name = "message",
|
|
|
|
.type = BLOBMSG_TYPE_STRING,
|
|
|
|
},
|
|
|
|
[FOO_LIST] = {
|
|
|
|
.name = "list",
|
|
|
|
.type = BLOBMSG_TYPE_ARRAY,
|
|
|
|
},
|
|
|
|
[FOO_TESTDATA] = {
|
|
|
|
.name = "testdata",
|
|
|
|
.type = BLOBMSG_TYPE_TABLE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void dump_message(struct blob_buf *buf)
|
|
|
|
{
|
|
|
|
struct blob_attr *tb[ARRAY_SIZE(pol)];
|
|
|
|
|
|
|
|
if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
|
|
|
|
fprintf(stderr, "Parse failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tb[FOO_MESSAGE])
|
|
|
|
fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
|
|
|
|
|
|
|
|
if (tb[FOO_LIST]) {
|
|
|
|
fprintf(stderr, "List: ");
|
2013-09-11 17:59:33 +02:00
|
|
|
dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
if (tb[FOO_TESTDATA]) {
|
|
|
|
fprintf(stderr, "Testdata: ");
|
2013-09-11 17:59:33 +02:00
|
|
|
dump_table(blobmsg_data(tb[FOO_TESTDATA]), blobmsg_data_len(tb[FOO_TESTDATA]), 0, false);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_message(struct blob_buf *buf)
|
|
|
|
{
|
|
|
|
void *tbl;
|
|
|
|
|
|
|
|
blobmsg_add_string(buf, "message", "Hello, world!");
|
|
|
|
|
2011-01-29 18:00:40 +01:00
|
|
|
tbl = blobmsg_open_table(buf, "testdata");
|
2016-11-19 18:55:49 +01:00
|
|
|
blobmsg_add_double(buf, "double", 1.337e2);
|
2020-01-12 15:31:49 +01:00
|
|
|
blobmsg_add_u8(buf, "foo", 0);
|
|
|
|
blobmsg_add_u8(buf, "poo", 100);
|
|
|
|
blobmsg_add_u8(buf, "moo-min", INT8_MIN);
|
|
|
|
blobmsg_add_u8(buf, "moo-max", INT8_MAX);
|
|
|
|
blobmsg_add_u16(buf, "bar-min", INT16_MIN);
|
|
|
|
blobmsg_add_u16(buf, "bar-max", INT16_MAX);
|
|
|
|
blobmsg_add_u32(buf, "baz-min", INT32_MIN);
|
|
|
|
blobmsg_add_u32(buf, "baz-max", INT32_MAX);
|
|
|
|
blobmsg_add_u64(buf, "taz-min", INT64_MIN);
|
|
|
|
blobmsg_add_u64(buf, "taz-max", INT64_MAX);
|
2011-01-29 18:00:40 +01:00
|
|
|
blobmsg_add_string(buf, "world", "2");
|
|
|
|
blobmsg_close_table(buf, tbl);
|
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
tbl = blobmsg_open_array(buf, "list");
|
2020-01-12 15:31:49 +01:00
|
|
|
blobmsg_add_u8(buf, NULL, 0);
|
|
|
|
blobmsg_add_u8(buf, NULL, 100);
|
|
|
|
blobmsg_add_u8(buf, NULL, INT8_MIN);
|
|
|
|
blobmsg_add_u8(buf, NULL, INT8_MAX);
|
|
|
|
blobmsg_add_u16(buf, NULL, INT16_MIN);
|
|
|
|
blobmsg_add_u16(buf, NULL, INT16_MAX);
|
|
|
|
blobmsg_add_u32(buf, NULL, INT32_MIN);
|
|
|
|
blobmsg_add_u32(buf, NULL, INT32_MAX);
|
|
|
|
blobmsg_add_u64(buf, NULL, INT64_MIN);
|
|
|
|
blobmsg_add_u64(buf, NULL, INT64_MAX);
|
2016-11-19 18:55:49 +01:00
|
|
|
blobmsg_add_double(buf, "double", 1.337e2);
|
2010-10-13 21:17:51 +02:00
|
|
|
blobmsg_close_table(buf, tbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-11-19 14:31:44 +01:00
|
|
|
char *json = NULL;
|
2010-10-13 21:17:51 +02:00
|
|
|
static struct blob_buf buf;
|
|
|
|
|
|
|
|
blobmsg_buf_init(&buf);
|
|
|
|
fill_message(&buf);
|
2020-01-12 15:31:49 +01:00
|
|
|
fprintf(stderr, "[*] blobmsg dump:\n");
|
2010-10-13 21:17:51 +02:00
|
|
|
dump_message(&buf);
|
2019-11-19 14:31:44 +01:00
|
|
|
|
|
|
|
json = blobmsg_format_json(buf.head, true);
|
|
|
|
if (!json)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2020-01-12 15:31:49 +01:00
|
|
|
fprintf(stderr, "\n[*] blobmsg to json: %s\n", json);
|
|
|
|
|
|
|
|
blobmsg_buf_init(&buf);
|
|
|
|
if (!blobmsg_add_json_from_string(&buf, json))
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
fprintf(stderr, "\n[*] blobmsg from json:\n");
|
|
|
|
dump_message(&buf);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
if (buf.buf)
|
|
|
|
free(buf.buf);
|
2019-11-19 14:31:44 +01:00
|
|
|
free(json);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|