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