pass ubus_msg_buf to callback of internal object
[project/ubus.git] / examples / server.c
1 /*
2  * Copyright (C) 2011-2014 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 #include <signal.h>
16
17 #include <libubox/blobmsg_json.h>
18 #include "libubus.h"
19 #include "count.h"
20
21 static struct ubus_context *ctx;
22 static struct ubus_subscriber test_event;
23 static struct blob_buf b;
24
25 enum {
26         HELLO_ID,
27         HELLO_MSG,
28         __HELLO_MAX
29 };
30
31 static const struct blobmsg_policy hello_policy[] = {
32         [HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
33         [HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
34 };
35
36 struct hello_request {
37         struct ubus_request_data req;
38         struct uloop_timeout timeout;
39         int fd;
40         int idx;
41         char data[];
42 };
43
44 static void test_hello_fd_reply(struct uloop_timeout *t)
45 {
46         struct hello_request *req = container_of(t, struct hello_request, timeout);
47         char *data;
48
49         data = alloca(strlen(req->data) + 32);
50         sprintf(data, "msg%d: %s\n", ++req->idx, req->data);
51         if (write(req->fd, data, strlen(data)) < 0) {
52                 close(req->fd);
53                 free(req);
54                 return;
55         }
56
57         uloop_timeout_set(&req->timeout, 1000);
58 }
59
60 static void test_hello_reply(struct uloop_timeout *t)
61 {
62         struct hello_request *req = container_of(t, struct hello_request, timeout);
63         int fds[2];
64
65         blob_buf_init(&b, 0);
66         blobmsg_add_string(&b, "message", req->data);
67         ubus_send_reply(ctx, &req->req, b.head);
68
69         if (pipe(fds) == -1) {
70                 fprintf(stderr, "Failed to create pipe\n");
71                 return;
72         }
73         ubus_request_set_fd(ctx, &req->req, fds[0]);
74         ubus_complete_deferred_request(ctx, &req->req, 0);
75         req->fd = fds[1];
76
77         req->timeout.cb = test_hello_fd_reply;
78         test_hello_fd_reply(t);
79 }
80
81 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
82                       struct ubus_request_data *req, const char *method,
83                       struct blob_attr *msg)
84 {
85         struct hello_request *hreq;
86         struct blob_attr *tb[__HELLO_MAX];
87         const char *format = "%s received a message: %s";
88         const char *msgstr = "(unknown)";
89
90         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
91
92         if (tb[HELLO_MSG])
93                 msgstr = blobmsg_data(tb[HELLO_MSG]);
94
95         hreq = calloc(1, sizeof(*hreq) + strlen(format) + strlen(obj->name) + strlen(msgstr) + 1);
96         if (!hreq)
97                 return UBUS_STATUS_UNKNOWN_ERROR;
98
99         sprintf(hreq->data, format, obj->name, msgstr);
100         ubus_defer_request(ctx, req, &hreq->req);
101         hreq->timeout.cb = test_hello_reply;
102         uloop_timeout_set(&hreq->timeout, 1000);
103
104         return 0;
105 }
106
107 enum {
108         WATCH_ID,
109         WATCH_COUNTER,
110         __WATCH_MAX
111 };
112
113 static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
114         [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
115         [WATCH_COUNTER] = { .name = "counter", .type = BLOBMSG_TYPE_INT32 },
116 };
117
118 static void
119 test_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s,
120                    uint32_t id)
121 {
122         fprintf(stderr, "Object %08x went away\n", id);
123 }
124
125 static int
126 test_notify(struct ubus_context *ctx, struct ubus_object *obj,
127             struct ubus_request_data *req, const char *method,
128             struct blob_attr *msg)
129 {
130 #if 0
131         char *str;
132
133         str = blobmsg_format_json(msg, true);
134         fprintf(stderr, "Received notification '%s': %s\n", method, str);
135         free(str);
136 #endif
137
138         return 0;
139 }
140
141 static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
142                       struct ubus_request_data *req, const char *method,
143                       struct blob_attr *msg)
144 {
145         struct blob_attr *tb[__WATCH_MAX];
146         int ret;
147
148         blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));
149         if (!tb[WATCH_ID])
150                 return UBUS_STATUS_INVALID_ARGUMENT;
151
152         test_event.remove_cb = test_handle_remove;
153         test_event.cb = test_notify;
154         ret = ubus_subscribe(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
155         fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
156         return ret;
157 }
158
159 enum {
160     COUNT_TO,
161     COUNT_STRING,
162     __COUNT_MAX
163 };
164
165 static const struct blobmsg_policy count_policy[__COUNT_MAX] = {
166     [COUNT_TO] = { .name = "to", .type = BLOBMSG_TYPE_INT32 },
167     [COUNT_STRING] = { .name = "string", .type = BLOBMSG_TYPE_STRING },
168 };
169
170 static int test_count(struct ubus_context *ctx, struct ubus_object *obj,
171                       struct ubus_request_data *req, const char *method,
172                       struct blob_attr *msg)
173 {
174         struct blob_attr *tb[__COUNT_MAX];
175         char *s1, *s2;
176         uint32_t num;
177
178         blobmsg_parse(count_policy, __COUNT_MAX, tb, blob_data(msg), blob_len(msg));
179         if (!tb[COUNT_TO] || !tb[COUNT_STRING])
180                 return UBUS_STATUS_INVALID_ARGUMENT;
181
182         num = blobmsg_get_u32(tb[COUNT_TO]);
183         s1 = blobmsg_get_string(tb[COUNT_STRING]);
184         s2 = count_to_number(num);
185         if (!s1 || !s2) {
186                 free(s2);
187                 return UBUS_STATUS_UNKNOWN_ERROR;
188         }
189         blob_buf_init(&b, 0);
190         blobmsg_add_u32(&b, "rc", strcmp(s1, s2));
191         ubus_send_reply(ctx, req, b.head);
192         free(s2);
193
194         return 0;
195 }
196
197 static const struct ubus_method test_methods[] = {
198         UBUS_METHOD("hello", test_hello, hello_policy),
199         UBUS_METHOD("watch", test_watch, watch_policy),
200         UBUS_METHOD("count", test_count, count_policy),
201 };
202
203 static struct ubus_object_type test_object_type =
204         UBUS_OBJECT_TYPE("test", test_methods);
205
206 static struct ubus_object test_object = {
207         .name = "test",
208         .type = &test_object_type,
209         .methods = test_methods,
210         .n_methods = ARRAY_SIZE(test_methods),
211 };
212
213 static void server_main(void)
214 {
215         int ret;
216
217         ret = ubus_add_object(ctx, &test_object);
218         if (ret)
219                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
220
221         ret = ubus_register_subscriber(ctx, &test_event);
222         if (ret)
223                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
224
225         uloop_run();
226 }
227
228 int main(int argc, char **argv)
229 {
230         const char *ubus_socket = NULL;
231         int ch;
232
233         while ((ch = getopt(argc, argv, "cs:")) != -1) {
234                 switch (ch) {
235                 case 's':
236                         ubus_socket = optarg;
237                         break;
238                 default:
239                         break;
240                 }
241         }
242
243         argc -= optind;
244         argv += optind;
245
246         uloop_init();
247         signal(SIGPIPE, SIG_IGN);
248
249         ctx = ubus_connect(ubus_socket);
250         if (!ctx) {
251                 fprintf(stderr, "Failed to connect to ubus\n");
252                 return -1;
253         }
254
255         ubus_add_uloop(ctx);
256
257         server_main();
258
259         ubus_free(ctx);
260         uloop_done();
261
262         return 0;
263 }