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