8 #include "blobmsg_json.h"
10 static const char *indent_str
= "\t\t\t\t\t\t\t\t\t\t\t\t\t";
12 #define indent_printf(indent, ...) do { \
14 fwrite(indent_str, indent, 1, stderr); \
15 fprintf(stderr, __VA_ARGS__); \
18 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
);
21 dump_table(struct blob_attr
*head
, size_t len
, int indent
, bool array
)
23 struct blob_attr
*attr
;
24 struct blobmsg_hdr
*hdr
;
26 indent_printf(indent
, "{\n");
27 __blob_for_each_attr(attr
, head
, len
) {
28 hdr
= blob_data(attr
);
30 indent_printf(indent
+ 1, "%s : ", hdr
->name
);
31 dump_attr_data(attr
, 0, indent
+ 1);
33 indent_printf(indent
, "}\n");
36 static void dump_attr_data(struct blob_attr
*data
, int indent
, int next_indent
)
38 int type
= blobmsg_type(data
);
40 case BLOBMSG_TYPE_STRING
:
41 indent_printf(indent
, "%s (str)\n", blobmsg_get_string(data
));
43 case BLOBMSG_TYPE_INT8
:
44 indent_printf(indent
, "%d (i8)\n", (int8_t) blobmsg_get_u8(data
));
46 case BLOBMSG_TYPE_INT16
:
47 indent_printf(indent
, "%d (i16)\n", (int16_t) blobmsg_get_u16(data
));
49 case BLOBMSG_TYPE_INT32
:
50 indent_printf(indent
, "%d (i32)\n", (int32_t) blobmsg_get_u32(data
));
52 case BLOBMSG_TYPE_INT64
:
53 indent_printf(indent
, "%"PRId64
" (i64)\n", (int64_t) blobmsg_get_u64(data
));
55 case BLOBMSG_TYPE_DOUBLE
:
56 indent_printf(indent
, "%lf (dbl)\n", blobmsg_get_double(data
));
58 case BLOBMSG_TYPE_TABLE
:
59 case BLOBMSG_TYPE_ARRAY
:
61 indent_printf(indent
, "\n");
62 dump_table(blobmsg_data(data
), blobmsg_data_len(data
),
63 next_indent
, type
== BLOBMSG_TYPE_ARRAY
);
74 static const struct blobmsg_policy pol
[] = {
77 .type
= BLOBMSG_TYPE_STRING
,
81 .type
= BLOBMSG_TYPE_ARRAY
,
85 .type
= BLOBMSG_TYPE_TABLE
,
90 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
93 static void dump_message(struct blob_buf
*buf
)
95 struct blob_attr
*tb
[ARRAY_SIZE(pol
)];
97 if (blobmsg_parse(pol
, ARRAY_SIZE(pol
), tb
, blob_data(buf
->head
), blob_len(buf
->head
)) != 0) {
98 fprintf(stderr
, "Parse failed\n");
102 fprintf(stderr
, "Message: %s\n", (char *) blobmsg_data(tb
[FOO_MESSAGE
]));
105 fprintf(stderr
, "List: ");
106 dump_table(blobmsg_data(tb
[FOO_LIST
]), blobmsg_data_len(tb
[FOO_LIST
]), 0, true);
108 if (tb
[FOO_TESTDATA
]) {
109 fprintf(stderr
, "Testdata: ");
110 dump_table(blobmsg_data(tb
[FOO_TESTDATA
]), blobmsg_data_len(tb
[FOO_TESTDATA
]), 0, false);
115 fill_message(struct blob_buf
*buf
)
119 blobmsg_add_string(buf
, "message", "Hello, world!");
121 tbl
= blobmsg_open_table(buf
, "testdata");
122 blobmsg_add_double(buf
, "dbl-min", DBL_MIN
);
123 blobmsg_add_double(buf
, "dbl-max", DBL_MAX
);
124 blobmsg_add_u8(buf
, "foo", 0);
125 blobmsg_add_u8(buf
, "poo", 100);
126 blobmsg_add_u8(buf
, "moo-min", INT8_MIN
);
127 blobmsg_add_u8(buf
, "moo-max", INT8_MAX
);
128 blobmsg_add_u16(buf
, "bar-min", INT16_MIN
);
129 blobmsg_add_u16(buf
, "bar-max", INT16_MAX
);
130 blobmsg_add_u32(buf
, "baz-min", INT32_MIN
);
131 blobmsg_add_u32(buf
, "baz-max", INT32_MAX
);
132 blobmsg_add_u64(buf
, "taz-min", INT64_MIN
);
133 blobmsg_add_u64(buf
, "taz-max", INT64_MAX
);
134 blobmsg_add_string(buf
, "world", "2");
135 blobmsg_close_table(buf
, tbl
);
137 tbl
= blobmsg_open_array(buf
, "list");
138 blobmsg_add_u8(buf
, NULL
, 0);
139 blobmsg_add_u8(buf
, NULL
, 100);
140 blobmsg_add_u8(buf
, NULL
, INT8_MIN
);
141 blobmsg_add_u8(buf
, NULL
, INT8_MAX
);
142 blobmsg_add_u16(buf
, NULL
, INT16_MIN
);
143 blobmsg_add_u16(buf
, NULL
, INT16_MAX
);
144 blobmsg_add_u32(buf
, NULL
, INT32_MIN
);
145 blobmsg_add_u32(buf
, NULL
, INT32_MAX
);
146 blobmsg_add_u64(buf
, NULL
, INT64_MIN
);
147 blobmsg_add_u64(buf
, NULL
, INT64_MAX
);
148 blobmsg_add_double(buf
, NULL
, DBL_MIN
);
149 blobmsg_add_double(buf
, NULL
, DBL_MAX
);
150 blobmsg_close_table(buf
, tbl
);
153 int main(int argc
, char **argv
)
156 static struct blob_buf buf
;
158 blobmsg_buf_init(&buf
);
160 fprintf(stderr
, "[*] blobmsg dump:\n");
163 json
= blobmsg_format_json(buf
.head
, true);
167 fprintf(stderr
, "\n[*] blobmsg to json: %s\n", json
);
169 blobmsg_buf_init(&buf
);
170 if (!blobmsg_add_json_from_string(&buf
, json
))
173 fprintf(stderr
, "\n[*] blobmsg from json:\n");