use int8 as boolean
[project/ubus.git] / ubus-example.c
1 #include <unistd.h>
2
3 #include "libubus.h"
4
5 static struct ubus_context *ctx;
6 struct blob_buf b;
7
8 static const struct ubus_signature test_object_sig[] = {
9         UBUS_METHOD_START("hello"),
10           UBUS_TABLE_START(NULL),
11             UBUS_FIELD(INT32, "id"),
12             UBUS_FIELD(STRING, "msg"),
13           UBUS_TABLE_END(),
14         UBUS_METHOD_END(),
15 };
16
17 static struct ubus_object_type test_object_type =
18         UBUS_OBJECT_TYPE("test", test_object_sig);
19
20 enum {
21         HELLO_ID,
22         HELLO_MSG,
23         HELLO_LAST
24 };
25
26 static const struct blobmsg_policy hello_policy[] = {
27         [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
28         [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
29 };
30
31 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
32                       struct ubus_request_data *req, const char *method,
33                       struct blob_attr *msg)
34 {
35         struct blob_attr *tb[HELLO_LAST];
36         char *msgstr = "(unknown)";
37         char *strbuf;
38
39         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
40
41         if (tb[HELLO_MSG])
42                 msgstr = blobmsg_data(tb[HELLO_MSG]);
43
44         blob_buf_init(&b, 0);
45         strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
46         sprintf(strbuf, "%s: Hello, world: %s", obj->name, msgstr);
47         blobmsg_add_string_buffer(&b);
48         ubus_send_reply(ctx, req, b.head);
49         return 0;
50 }
51
52 static const struct ubus_method test_methods[] = {
53         { .name = "hello", .handler = test_hello },
54 };
55
56 static struct ubus_object test_object = {
57         .name = "test",
58         .type = &test_object_type,
59         .methods = test_methods,
60         .n_methods = ARRAY_SIZE(test_methods),
61 };
62
63 static struct ubus_object test_object2 = {
64         .name = "test2",
65         .type = &test_object_type,
66         .methods = test_methods,
67         .n_methods = ARRAY_SIZE(test_methods),
68 };
69
70 int main(int argc, char **argv)
71 {
72         const char *progname, *ubus_socket = NULL;
73         int ret = 0;
74         int ch;
75
76         progname = argv[0];
77
78         while ((ch = getopt(argc, argv, "s:")) != -1) {
79                 switch (ch) {
80                 case 's':
81                         ubus_socket = optarg;
82                         break;
83                 default:
84                         break;
85                 }
86         }
87
88         argc -= optind;
89         argv += optind;
90
91         ctx = ubus_connect(ubus_socket);
92         if (!ctx) {
93                 fprintf(stderr, "Failed to connect to ubus\n");
94                 return -1;
95         }
96
97         ret = ubus_add_object(ctx, &test_object);
98         if (ret)
99                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
100
101         ret = ubus_add_object(ctx, &test_object2);
102         if (ret)
103                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
104
105         uloop_init();
106         ubus_add_uloop(ctx);
107         uloop_run();
108         uloop_done();
109
110         ubus_free(ctx);
111         return 0;
112 }