2c2e703f09c0b564d5ddecdeea53cbc44464aec2
[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(void *data, int len, int type, 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, *last_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(blobmsg_data(attr), blobmsg_data_len(attr), blob_id(attr), 0, indent + 1);
28                 last_attr = attr;
29         }
30         indent_printf(indent, "}\n");
31 }
32
33 static void dump_attr_data(void *data, int len, int type, int indent, int next_indent)
34 {
35         switch(type) {
36         case BLOBMSG_TYPE_STRING:
37                 indent_printf(indent, "%s\n", (char *) data);
38                 break;
39         case BLOBMSG_TYPE_INT8:
40                 indent_printf(indent, "%d\n", *(uint8_t *)data);
41                 break;
42         case BLOBMSG_TYPE_INT16:
43                 indent_printf(indent, "%d\n", *(uint16_t *)data);
44                 break;
45         case BLOBMSG_TYPE_INT32:
46                 indent_printf(indent, "%d\n", *(uint32_t *)data);
47                 break;
48         case BLOBMSG_TYPE_INT64:
49                 indent_printf(indent, "%lld\n", *(uint64_t *)data);
50                 break;
51         case BLOBMSG_TYPE_TABLE:
52         case BLOBMSG_TYPE_ARRAY:
53                 if (!indent)
54                         indent_printf(indent, "\n");
55                 dump_table(data, len, next_indent, type == BLOBMSG_TYPE_ARRAY);
56                 break;
57         }
58 }
59
60 enum {
61         FOO_MESSAGE,
62         FOO_LIST,
63         FOO_TESTDATA
64 };
65
66 static const struct blobmsg_policy pol[] = {
67         [FOO_MESSAGE] = {
68                 .name = "message",
69                 .type = BLOBMSG_TYPE_STRING,
70         },
71         [FOO_LIST] = {
72                 .name = "list",
73                 .type = BLOBMSG_TYPE_ARRAY,
74         },
75         [FOO_TESTDATA] = {
76                 .name = "testdata",
77                 .type = BLOBMSG_TYPE_TABLE,
78         },
79 };
80
81 #ifndef ARRAY_SIZE
82 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
83 #endif
84
85 static void dump_message(struct blob_buf *buf)
86 {
87         struct blob_attr *tb[ARRAY_SIZE(pol)];
88
89         if (blobmsg_parse(pol, ARRAY_SIZE(pol), tb, blob_data(buf->head), blob_len(buf->head)) != 0) {
90                 fprintf(stderr, "Parse failed\n");
91                 return;
92         }
93         if (tb[FOO_MESSAGE])
94                 fprintf(stderr, "Message: %s\n", (char *) blobmsg_data(tb[FOO_MESSAGE]));
95
96         if (tb[FOO_LIST]) {
97                 fprintf(stderr, "List: ");
98                 dump_table(blobmsg_data(tb[FOO_LIST]), blob_len(tb[FOO_LIST]), 0, true);
99         }
100         if (tb[FOO_TESTDATA]) {
101                 fprintf(stderr, "Testdata: ");
102                 dump_table(blobmsg_data(tb[FOO_TESTDATA]), blob_len(tb[FOO_TESTDATA]), 0, false);
103         }
104 }
105
106 static void
107 fill_message(struct blob_buf *buf)
108 {
109         void *tbl;
110
111         blobmsg_add_string(buf, "message", "Hello, world!");
112
113         tbl = blobmsg_open_table(buf, "testdata");
114         blobmsg_add_u32(buf, "hello", 1);
115         blobmsg_add_string(buf, "world", "2");
116         blobmsg_close_table(buf, tbl);
117
118         tbl = blobmsg_open_array(buf, "list");
119         blobmsg_add_u32(buf, NULL, 0);
120         blobmsg_add_u32(buf, NULL, 1);
121         blobmsg_add_u32(buf, NULL, 2);
122         blobmsg_close_table(buf, tbl);
123 }
124
125 int main(int argc, char **argv)
126 {
127         static struct blob_buf buf;
128
129         blobmsg_buf_init(&buf);
130         fill_message(&buf);
131         dump_message(&buf);
132         fprintf(stderr, "json: %s\n", blobmsg_format_json(buf.head, false));
133
134         if (buf.buf)
135                 free(buf.buf);
136
137         return 0;
138 }