examples: fix build error
[project/libubox.git] / examples / blobmsg-example.c
1 #include <stdio.h>
2
3 #include "blobmsg.h"
4 #include "blobmsg_json.h"
5
6 static const char *indent_str = "\t\t\t\t\t\t\t\t\t\t\t\t\t";
7
8 #define indent_printf(indent, ...) do { \
9 if (indent > 0) \
10 fwrite(indent_str, indent, 1, stderr); \
11 fprintf(stderr, __VA_ARGS__); \
12 } while(0)
13
14 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent);
15
16 static void
17 dump_table(struct blob_attr *head, int len, int indent, bool array)
18 {
19 struct blob_attr *attr;
20 struct blobmsg_hdr *hdr;
21
22 indent_printf(indent, "{\n");
23 __blob_for_each_attr(attr, head, len) {
24 hdr = blob_data(attr);
25 if (!array)
26 indent_printf(indent + 1, "%s : ", hdr->name);
27 dump_attr_data(attr, 0, indent + 1);
28 }
29 indent_printf(indent, "}\n");
30 }
31
32 static void dump_attr_data(struct blob_attr *data, int indent, int next_indent)
33 {
34 int type = blobmsg_type(data);
35 switch(type) {
36 case BLOBMSG_TYPE_STRING:
37 indent_printf(indent, "%s\n", (char *) blobmsg_data(data));
38 break;
39 case BLOBMSG_TYPE_INT8:
40 indent_printf(indent, "%d\n", blobmsg_get_u8(data));
41 break;
42 case BLOBMSG_TYPE_INT16:
43 indent_printf(indent, "%d\n", blobmsg_get_u16(data));
44 break;
45 case BLOBMSG_TYPE_INT32:
46 indent_printf(indent, "%d\n", blobmsg_get_u32(data));
47 break;
48 case BLOBMSG_TYPE_INT64:
49 indent_printf(indent, "%"PRIu64"\n", blobmsg_get_u64(data));
50 break;
51 case BLOBMSG_TYPE_TABLE:
52 case BLOBMSG_TYPE_ARRAY:
53 if (!indent)
54 indent_printf(indent, "\n");
55 dump_table(blobmsg_data(data), blobmsg_data_len(data),
56 next_indent, type == BLOBMSG_TYPE_ARRAY);
57 break;
58 }
59 }
60
61 enum {
62 FOO_MESSAGE,
63 FOO_LIST,
64 FOO_TESTDATA
65 };
66
67 static const struct blobmsg_policy pol[] = {
68 [FOO_MESSAGE] = {
69 .name = "message",
70 .type = BLOBMSG_TYPE_STRING,
71 },
72 [FOO_LIST] = {
73 .name = "list",
74 .type = BLOBMSG_TYPE_ARRAY,
75 },
76 [FOO_TESTDATA] = {
77 .name = "testdata",
78 .type = BLOBMSG_TYPE_TABLE,
79 },
80 };
81
82 #ifndef ARRAY_SIZE
83 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
84 #endif
85
86 static void dump_message(struct blob_buf *buf)
87 {
88 struct blob_attr *tb[ARRAY_SIZE(pol)];
89
90 if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
91 fprintf(stderr, "Parse failed\n");
92 return;
93 }
94 if (tb[FOO_MESSAGE])
95 fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
96
97 if (tb[FOO_LIST]) {
98 fprintf(stderr, "List: ");
99 dump_table(blobmsg_data(tb[FOO_LIST]), blobmsg_data_len(tb[FOO_LIST]), 0, true);
100 }
101 if (tb[FOO_TESTDATA]) {
102 fprintf(stderr, "Testdata: ");
103 dump_table(blobmsg_data(tb[FOO_TESTDATA]), blobmsg_data_len(tb[FOO_TESTDATA]), 0, false);
104 }
105 }
106
107 static void
108 fill_message(struct blob_buf *buf)
109 {
110 void *tbl;
111
112 blobmsg_add_string(buf, "message", "Hello, world!");
113
114 tbl = blobmsg_open_table(buf, "testdata");
115 blobmsg_add_u32(buf, "hello", 1);
116 blobmsg_add_string(buf, "world", "2");
117 blobmsg_close_table(buf, tbl);
118
119 tbl = blobmsg_open_array(buf, "list");
120 blobmsg_add_u32(buf, NULL, 0);
121 blobmsg_add_u32(buf, NULL, 1);
122 blobmsg_add_u32(buf, NULL, 2);
123 blobmsg_close_table(buf, tbl);
124 }
125
126 int main(int argc, char **argv)
127 {
128 static struct blob_buf buf;
129
130 blobmsg_buf_init(&buf);
131 fill_message(&buf);
132 dump_message(&buf);
133 fprintf(stderr, "json: %s\n", blobmsg_format_json(buf.head, true));
134
135 if (buf.buf)
136 free(buf.buf);
137
138 return 0;
139 }