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