enable -Wmissing-declarations
[project/ubus.git] / ubus-example.c
1 /*
2  * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <unistd.h>
15
16 #include "libubus.h"
17
18 static struct ubus_context *ctx;
19 static struct ubus_watch_object test_event;
20 struct blob_buf b;
21
22 enum {
23         HELLO_ID,
24         HELLO_MSG,
25         __HELLO_MAX
26 };
27
28 static const struct blobmsg_policy hello_policy[] = {
29         [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
30         [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
31 };
32
33 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
34                       struct ubus_request_data *req, const char *method,
35                       struct blob_attr *msg)
36 {
37         struct blob_attr *tb[__HELLO_MAX];
38         char *msgstr = "(unknown)";
39         char *strbuf;
40
41         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
42
43         if (tb[HELLO_MSG])
44                 msgstr = blobmsg_data(tb[HELLO_MSG]);
45
46         blob_buf_init(&b, 0);
47         strbuf = blobmsg_alloc_string_buffer(&b, "message", 64 + strlen(obj->name) + strlen(msgstr));
48         sprintf(strbuf, "%s received a message: %s", obj->name, msgstr);
49         blobmsg_add_string_buffer(&b);
50         ubus_send_reply(ctx, req, b.head);
51         return 0;
52 }
53
54 enum {
55         WATCH_ID,
56         __WATCH_MAX
57 };
58
59 static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
60         [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
61 };
62
63 static void test_handle_event(struct ubus_context *ctx, struct ubus_watch_object *w,
64                        uint32_t id)
65 {
66         fprintf(stderr, "Object %08x went away\n", id);
67 }
68
69 static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
70                       struct ubus_request_data *req, const char *method,
71                       struct blob_attr *msg)
72 {
73         struct blob_attr *tb[__WATCH_MAX];
74         int ret;
75
76         blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));
77         if (!tb[WATCH_ID])
78                 return UBUS_STATUS_INVALID_ARGUMENT;
79
80         test_event.cb = test_handle_event;
81         ret = ubus_watch_object_add(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
82         fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
83         return ret;
84 }
85
86 static const struct ubus_method test_methods[] = {
87         UBUS_METHOD("hello", test_hello, hello_policy),
88         UBUS_METHOD("watch", test_watch, watch_policy),
89 };
90
91 static struct ubus_object_type test_object_type =
92         UBUS_OBJECT_TYPE("test", test_methods);
93
94 static struct ubus_object test_object = {
95         .name = "test",
96         .type = &test_object_type,
97         .methods = test_methods,
98         .n_methods = ARRAY_SIZE(test_methods),
99 };
100
101 static struct ubus_object test_client_object = {
102         .type = &test_object_type,
103         .methods = test_methods,
104         .n_methods = ARRAY_SIZE(test_methods),
105 };
106
107 static void server_main(void)
108 {
109         int ret;
110
111         ret = ubus_add_object(ctx, &test_object);
112         if (ret)
113                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
114
115         ret = ubus_register_watch_object(ctx, &test_event);
116         if (ret)
117                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
118
119         uloop_run();
120 }
121
122 static void client_main(void)
123 {
124         uint32_t id;
125         int ret;
126
127         ret = ubus_add_object(ctx, &test_client_object);
128         if (ret) {
129                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
130                 return;
131         }
132
133         if (ubus_lookup_id(ctx, test_object.name, &id)) {
134                 fprintf(stderr, "Failed to look up test object\n");
135                 return;
136         }
137
138         blob_buf_init(&b, 0);
139         blobmsg_add_u32(&b, "id", test_client_object.id);
140         ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
141         uloop_run();
142 }
143
144 int main(int argc, char **argv)
145 {
146         const char *ubus_socket = NULL;
147         bool client = false;
148         int ch;
149
150         while ((ch = getopt(argc, argv, "cs:")) != -1) {
151                 switch (ch) {
152                 case 's':
153                         ubus_socket = optarg;
154                         break;
155                 case 'c':
156                         client = true;
157                         break;
158                 default:
159                         break;
160                 }
161         }
162
163         argc -= optind;
164         argv += optind;
165
166         uloop_init();
167
168         ctx = ubus_connect(ubus_socket);
169         if (!ctx) {
170                 fprintf(stderr, "Failed to connect to ubus\n");
171                 return -1;
172         }
173
174         ubus_add_uloop(ctx);
175
176         if (client)
177                 client_main();
178         else
179                 server_main();
180
181         ubus_free(ctx);
182         uloop_done();
183
184         return 0;
185 }