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