6 #include "blobmsg_json.h"
8 static const char *indent_str
= "\t\t\t\t\t\t\t\t\t\t\t\t\t";
10 #define indent_printf(indent, ...) do { \
12 fwrite(indent_str, indent, 1, stderr); \
13 fprintf(stderr, __VA_ARGS__); \
16 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
);
19 dump_table(struct blob_attr
*head
, size_t len
, int indent
, bool array
)
21 struct blob_attr
*attr
;
22 struct blobmsg_hdr
*hdr
;
24 indent_printf(indent
, "{\n");
25 __blob_for_each_attr(attr
, head
, len
) {
26 hdr
= blob_data(attr
);
28 indent_printf(indent
+ 1, "%s : ", hdr
->name
);
29 dump_attr_data(attr
, 0, indent
+ 1);
31 indent_printf(indent
, "}\n");
34 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
)
36 int type
= blobmsg_type(data
);
38 case BLOBMSG_TYPE_STRING
:
39 indent_printf(indent
, "%s (str)\n", blobmsg_get_string(data
));
41 case BLOBMSG_TYPE_INT8
:
42 indent_printf(indent
, "%d (i8)\n", (int8_t) blobmsg_get_u8(data
));
44 case BLOBMSG_TYPE_INT16
:
45 indent_printf(indent
, "%d (i16)\n", (int16_t) blobmsg_get_u16(data
));
47 case BLOBMSG_TYPE_INT32
:
48 indent_printf(indent
, "%d (i32)\n", (int32_t) blobmsg_get_u32(data
));
50 case BLOBMSG_TYPE_INT64
:
51 indent_printf(indent
, "%"PRId64
" (i64)\n", (int64_t) blobmsg_get_u64(data
));
53 case BLOBMSG_TYPE_DOUBLE
:
54 indent_printf(indent
, "%lf (dbl)\n", blobmsg_get_double(data
));
56 case BLOBMSG_TYPE_TABLE
:
57 case BLOBMSG_TYPE_ARRAY
:
59 indent_printf(indent
, "\n");
60 dump_table(blobmsg_data(data
), blobmsg_data_len(data
),
61 next_indent
, type
== BLOBMSG_TYPE_ARRAY
);
72 static const struct blobmsg_policy pol
[] = {
75 .type
= BLOBMSG_TYPE_STRING
,
79 .type
= BLOBMSG_TYPE_ARRAY
,
83 .type
= BLOBMSG_TYPE_TABLE
,
88 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
91 static void dump_message(struct blob_buf
*buf
)
93 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
95 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
96 fprintf(stderr
, "Parse failed\n");
100 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
103 fprintf(stderr
, "List: ");
104 dump_table(blobmsg_data(tb
[FOO_LIST
]), blobmsg_data_len(tb
[FOO_LIST
]), 0, true);
106 if (tb
[FOO_TESTDATA
]) {
107 fprintf(stderr
, "Testdata: ");
108 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blobmsg_data_len(tb
[FOO_TESTDATA
]), 0, false);
113 fill_message(struct blob_buf
*buf
)
117 blobmsg_add_string(buf
, "message", "Hello, world!");
119 tbl
= blobmsg_open_table(buf
, "testdata");
120 blobmsg_add_double(buf
, "double", 1.337e2
);
121 blobmsg_add_u8(buf
, "foo", 0);
122 blobmsg_add_u8(buf
, "poo", 100);
123 blobmsg_add_u8(buf
, "moo-min", INT8_MIN
);
124 blobmsg_add_u8(buf
, "moo-max", INT8_MAX
);
125 blobmsg_add_u16(buf
, "bar-min", INT16_MIN
);
126 blobmsg_add_u16(buf
, "bar-max", INT16_MAX
);
127 blobmsg_add_u32(buf
, "baz-min", INT32_MIN
);
128 blobmsg_add_u32(buf
, "baz-max", INT32_MAX
);
129 blobmsg_add_u64(buf
, "taz-min", INT64_MIN
);
130 blobmsg_add_u64(buf
, "taz-max", INT64_MAX
);
131 blobmsg_add_string(buf
, "world", "2");
132 blobmsg_close_table(buf
, tbl
);
134 tbl
= blobmsg_open_array(buf
, "list");
135 blobmsg_add_u8(buf
, NULL
, 0);
136 blobmsg_add_u8(buf
, NULL
, 100);
137 blobmsg_add_u8(buf
, NULL
, INT8_MIN
);
138 blobmsg_add_u8(buf
, NULL
, INT8_MAX
);
139 blobmsg_add_u16(buf
, NULL
, INT16_MIN
);
140 blobmsg_add_u16(buf
, NULL
, INT16_MAX
);
141 blobmsg_add_u32(buf
, NULL
, INT32_MIN
);
142 blobmsg_add_u32(buf
, NULL
, INT32_MAX
);
143 blobmsg_add_u64(buf
, NULL
, INT64_MIN
);
144 blobmsg_add_u64(buf
, NULL
, INT64_MAX
);
145 blobmsg_add_double(buf
, "double", 1.337e2
);
146 blobmsg_close_table(buf
, tbl
);
149 int main(int argc
, char **argv
)
152 static struct blob_buf buf
;
154 blobmsg_buf_init(&buf
);
156 fprintf(stderr
, "[*] blobmsg dump:\n");
159 json
= blobmsg_format_json(buf
.head
, true);
163 fprintf(stderr
, "\n[*] blobmsg to json: %s\n", json
);
165 blobmsg_buf_init(&buf
);
166 if (!blobmsg_add_json_from_string(&buf
, json
))
169 fprintf(stderr
, "\n[*] blobmsg from json:\n");