58b0bc4eb574ff3b715c007a2aa991b5d95809fe
[project/libubox.git] / tests / test-blobmsg.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <inttypes.h>
4
5 #include "blobmsg.h"
6 #include "blobmsg_json.h"
7
8 static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
9
10 #define indent_printf(indent, ...) do { \
11 if (indent > 0) \
12 fwrite(indent_str, indent, 1, stderr); \
13 fprintf(stderr, __VA_ARGS__); \
14 } while(0)
15
16 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
17
18 static void
19 dump_table(struct blob_attr *head, size_t len, int indent, bool array)
20 {
21 struct blob_attr *attr;
22 struct blobmsg_hdr *hdr;
23
24 indent_printf(indent, "{\n");
25 __blob_for_each_attr(attr, head, len) {
26 hdr = blob_data(attr);
27 if (!array)
28 indent_printf(indent + 1, "%s : ", hdr->name);
29 dump_attr_data(attr, 0, indent + 1);
30 }
31 indent_printf(indent, "}\n");
32 }
33
34 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
35 {
36 int type = blobmsg_type(data);
37 switch(type) {
38 case BLOBMSG_TYPE_STRING:
39 indent_printf(indent, "%s (str)\n", blobmsg_get_string(data));
40 break;
41 case BLOBMSG_TYPE_INT8:
42 indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data));
43 break;
44 case BLOBMSG_TYPE_INT16:
45 indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data));
46 break;
47 case BLOBMSG_TYPE_INT32:
48 indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data));
49 break;
50 case BLOBMSG_TYPE_INT64:
51 indent_printf(indent, "%"PRId64" (i64)\n", (int64_t) blobmsg_get_u64(data));
52 break;
53 case BLOBMSG_TYPE_DOUBLE:
54 indent_printf(indent, "%lf (dbl)\n", blobmsg_get_double(data));
55 break;
56 case BLOBMSG_TYPE_TABLE:
57 case BLOBMSG_TYPE_ARRAY:
58 if (!indent)
59 indent_printf(indent, "\n");
60 dump_table(blobmsg_data(data), blobmsg_data_len(data),
61 next_indent, type == BLOBMSG_TYPE_ARRAY);
62 break;
63 }
64 }
65
66 enum {
67 FOO_MESSAGE,
68 FOO_LIST,
69 FOO_TESTDATA
70 };
71
72 static const struct blobmsg_policy pol[] = {
73 [FOO_MESSAGE] = {
74 .name = "message",
75 .type = BLOBMSG_TYPE_STRING,
76 },
77 [FOO_LIST] = {
78 .name = "list",
79 .type = BLOBMSG_TYPE_ARRAY,
80 },
81 [FOO_TESTDATA] = {
82 .name = "testdata",
83 .type = BLOBMSG_TYPE_TABLE,
84 },
85 };
86
87 #ifndef ARRAY_SIZE
88 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
89 #endif
90
91 static void dump_message(struct blob_buf *buf)
92 {
93 struct blob_attr *tb[ARRAY_SIZE(pol)];
94
95 if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
96 fprintf(stderr, "Parse failed\n");
97 return;
98 }
99 if (tb[FOO_MESSAGE])
100 fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
101
102 if (tb[FOO_LIST]) {
103 fprintf(stderr, "List: ");
104 dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true);
105 }
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);
109 }
110 }
111
112 static void
113 fill_message(struct blob_buf *buf)
114 {
115 void *tbl;
116
117 blobmsg_add_string(buf, "message", "Hello, world!");
118
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);
133
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);
147 }
148
149 int main(int argc, char **argv)
150 {
151 char *json = NULL;
152 static struct blob_buf buf;
153
154 blobmsg_buf_init(&buf);
155 fill_message(&buf);
156 fprintf(stderr, "[*] blobmsg dump:\n");
157 dump_message(&buf);
158
159 json = blobmsg_format_json(buf.head, true);
160 if (!json)
161 exit(EXIT_FAILURE);
162
163 fprintf(stderr, "\n[*] blobmsg to json: %s\n", json);
164
165 blobmsg_buf_init(&buf);
166 if (!blobmsg_add_json_from_string(&buf, json))
167 exit(EXIT_FAILURE);
168
169 fprintf(stderr, "\n[*] blobmsg from json:\n");
170 dump_message(&buf);
171
172 if (buf.buf)
173 free(buf.buf);
174 free(json);
175
176 return 0;
177 }