5 static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
7 #define indent_printf(indent, ...) do { \
9 fwrite(indent_str, indent, 1, stderr); \
10 fprintf(stderr, __VA_ARGS__); \
13 static void dump_attr_data(void *data, int len, int type, int indent, int next_indent);
16 dump_table(struct blob_attr *head, int len, int indent, bool array)
18 struct blob_attr *attr, *last_attr;
19 struct blobmsg_hdr *hdr;
21 indent_printf(indent, "{\n");
22 __blob_for_each_attr(attr, head, len) {
23 hdr = blob_data(attr);
25 indent_printf(indent + 1, "%s : ", hdr->name);
26 dump_attr_data(blobmsg_data(attr), blobmsg_data_len(attr), blob_id(attr), 0, indent + 1);
29 indent_printf(indent, "}\n");
32 static void dump_attr_data(void *data, int len, int type, int indent, int next_indent)
35 case BLOBMSG_TYPE_STRING:
36 indent_printf(indent, "%s\n", (char *) data);
38 case BLOBMSG_TYPE_INT8:
39 indent_printf(indent, "%d\n", *(uint8_t *)data);
41 case BLOBMSG_TYPE_INT16:
42 indent_printf(indent, "%d\n", *(uint16_t *)data);
44 case BLOBMSG_TYPE_INT32:
45 indent_printf(indent, "%d\n", *(uint32_t *)data);
47 case BLOBMSG_TYPE_INT64:
48 indent_printf(indent, "%lld\n", *(uint64_t *)data);
50 case BLOBMSG_TYPE_TABLE:
51 case BLOBMSG_TYPE_ARRAY:
53 indent_printf(indent, "\n");
54 dump_table(data, len, next_indent, type == BLOBMSG_TYPE_ARRAY);
65 static const struct blobmsg_policy pol[] = {
68 .type = BLOBMSG_TYPE_STRING,
72 .type = BLOBMSG_TYPE_ARRAY,
76 .type = BLOBMSG_TYPE_TABLE,
81 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
84 static void dump_message(struct blob_buf *buf)
86 struct blob_attr *tb[ARRAY_SIZE(pol)];
88 if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
89 fprintf(stderr, "Parse failed\n");
93 fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
96 fprintf(stderr, "List: ");
97 dump_table(blobmsg_data(tb[FOO_LIST]), blob_len(tb[FOO_LIST]), 0, true);
99 if (tb[FOO_TESTDATA]) {
100 fprintf(stderr, "Testdata: ");
101 dump_table(blobmsg_data(tb[FOO_TESTDATA]), blob_len(tb[FOO_TESTDATA]), 0, false);
106 fill_message(struct blob_buf *buf)
110 blobmsg_add_string(buf, "message", "Hello, world!");
112 tbl = blobmsg_open_table(buf, "testdata");
113 blobmsg_add_u32(buf, "hello", 1);
114 blobmsg_add_string(buf, "world", "2");
115 blobmsg_close_table(buf, tbl);
117 tbl = blobmsg_open_array(buf, "list");
118 blobmsg_add_u32(buf, NULL, 0);
119 blobmsg_add_u32(buf, NULL, 1);
120 blobmsg_add_u32(buf, NULL, 2);
121 blobmsg_close_table(buf, tbl);
124 int main(int argc, char **argv)
126 static struct blob_buf buf;
128 blobmsg_buf_init(&buf);
131 fprintf(stderr, "json: %s\n", blobmsg_format_json(buf.head));