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