6eec16db81ee9e87827f9ba63525b0d8fa7b983a
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_array(struct blob_attr
*head
, int len
, int indent
)
18 struct blob_attr
*attr
;
20 indent_printf(indent
, "{\n");
21 __blob_for_each_attr(attr
, head
, len
) {
22 dump_attr_data(blob_data(attr
), blob_len(attr
), blob_id(attr
), indent
+ 1, indent
+ 1);
24 indent_printf(indent
, "}\n");
28 dump_table(struct blob_attr
*head
, int len
, int indent
)
30 struct blob_attr
*attr
, *last_attr
;
31 struct blobmsg_hdr
*hdr
;
33 indent_printf(indent
, "{\n");
34 __blob_for_each_attr(attr
, head
, len
) {
35 hdr
= blob_data(attr
);
36 indent_printf(indent
+ 1, "%s : ", hdr
->name
);
37 dump_attr_data(blobmsg_data(attr
), blobmsg_data_len(attr
), blob_id(attr
), 0, indent
+ 1);
40 indent_printf(indent
, "}\n");
43 static void dump_attr_data(void *data
, int len
, int type
, int indent
, int next_indent
)
46 case BLOBMSG_TYPE_STRING
:
47 indent_printf(indent
, "%s\n", (char *) data
);
49 case BLOBMSG_TYPE_INT8
:
50 indent_printf(indent
, "%d\n", *(uint8_t *)data
);
52 case BLOBMSG_TYPE_INT16
:
53 indent_printf(indent
, "%d\n", *(uint16_t *)data
);
55 case BLOBMSG_TYPE_INT32
:
56 indent_printf(indent
, "%d\n", *(uint32_t *)data
);
58 case BLOBMSG_TYPE_INT64
:
59 indent_printf(indent
, "%lld\n", *(uint64_t *)data
);
61 case BLOBMSG_TYPE_TABLE
:
63 indent_printf(indent
, "\n");
64 dump_table(data
, len
, next_indent
);
66 case BLOBMSG_TYPE_ARRAY
:
68 indent_printf(indent
, "\n");
69 dump_array(data
, len
, next_indent
);
80 static const struct blobmsg_policy pol
[] = {
83 .type
= BLOBMSG_TYPE_STRING
,
87 .type
= BLOBMSG_TYPE_ARRAY
,
91 .type
= BLOBMSG_TYPE_TABLE
,
96 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
99 static void dump_message(struct blob_buf
*buf
)
101 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
103 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
104 fprintf(stderr
, "Parse failed\n");
108 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
111 fprintf(stderr
, "List: ");
112 dump_array(blobmsg_data(tb
[FOO_LIST
]), blob_len(tb
[FOO_LIST
]), 0);
114 if (tb
[FOO_TESTDATA
]) {
115 fprintf(stderr
, "Testdata: ");
116 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blob_len(tb
[FOO_TESTDATA
]), 0);
121 fill_message(struct blob_buf
*buf
)
125 blobmsg_add_string(buf
, "message", "Hello, world!");
127 tbl
= blobmsg_open_array(buf
, "list");
128 blobmsg_add_u32(buf
, NULL
, 0);
129 blobmsg_add_u32(buf
, NULL
, 1);
130 blobmsg_add_u32(buf
, NULL
, 2);
131 blobmsg_close_table(buf
, tbl
);
133 tbl
= blobmsg_open_table(buf
, "testdata");
134 blobmsg_add_u32(buf
, "hello", 1);
135 blobmsg_add_string(buf
, "world", "2");
136 blobmsg_close_table(buf
, tbl
);
139 int main(int argc
, char **argv
)
141 static struct blob_buf buf
;
143 blobmsg_buf_init(&buf
);