move example to examples/
[project/ubus.git] / examples / 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 static 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 struct hello_request {
34         struct ubus_request_data req;
35         struct uloop_timeout timeout;
36         char data[];
37 };
38
39 static void test_hello_reply(struct uloop_timeout *t)
40 {
41         struct hello_request *req = container_of(t, struct hello_request, timeout);
42
43         blob_buf_init(&b, 0);
44         blobmsg_add_string(&b, "message", req->data);
45         ubus_send_reply(ctx, &req->req, b.head);
46         ubus_complete_deferred_request(ctx, &req->req, 0);
47         free(req);
48 }
49
50 static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
51                       struct ubus_request_data *req, const char *method,
52                       struct blob_attr *msg)
53 {
54         struct hello_request *hreq;
55         struct blob_attr *tb[__HELLO_MAX];
56         const char *format = "%s received a message: %s";
57         const char *msgstr = "(unknown)";
58
59         blobmsg_parse(hello_policy, ARRAY_SIZE(hello_policy), tb, blob_data(msg), blob_len(msg));
60
61         if (tb[HELLO_MSG])
62                 msgstr = blobmsg_data(tb[HELLO_MSG]);
63
64         hreq = calloc(1, sizeof(*hreq) + strlen(format) + strlen(obj->name) + strlen(msgstr) + 1);
65         sprintf(hreq->data, format, obj->name, msgstr);
66         ubus_defer_request(ctx, req, &hreq->req);
67         hreq->timeout.cb = test_hello_reply;
68         uloop_timeout_set(&hreq->timeout, 1000);
69
70         return 0;
71 }
72
73 enum {
74         WATCH_ID,
75         __WATCH_MAX
76 };
77
78 static const struct blobmsg_policy watch_policy[__WATCH_MAX] = {
79         [WATCH_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
80 };
81
82 static void test_handle_event(struct ubus_context *ctx, struct ubus_watch_object *w,
83                        uint32_t id)
84 {
85         fprintf(stderr, "Object %08x went away\n", id);
86 }
87
88 static int test_watch(struct ubus_context *ctx, struct ubus_object *obj,
89                       struct ubus_request_data *req, const char *method,
90                       struct blob_attr *msg)
91 {
92         struct blob_attr *tb[__WATCH_MAX];
93         int ret;
94
95         blobmsg_parse(watch_policy, __WATCH_MAX, tb, blob_data(msg), blob_len(msg));
96         if (!tb[WATCH_ID])
97                 return UBUS_STATUS_INVALID_ARGUMENT;
98
99         test_event.cb = test_handle_event;
100         ret = ubus_watch_object_add(ctx, &test_event, blobmsg_get_u32(tb[WATCH_ID]));
101         fprintf(stderr, "Watching object %08x: %s\n", blobmsg_get_u32(tb[WATCH_ID]), ubus_strerror(ret));
102         return ret;
103 }
104
105 static const struct ubus_method test_methods[] = {
106         UBUS_METHOD("hello", test_hello, hello_policy),
107         UBUS_METHOD("watch", test_watch, watch_policy),
108 };
109
110 static struct ubus_object_type test_object_type =
111         UBUS_OBJECT_TYPE("test", test_methods);
112
113 static struct ubus_object test_object = {
114         .name = "test",
115         .type = &test_object_type,
116         .methods = test_methods,
117         .n_methods = ARRAY_SIZE(test_methods),
118 };
119
120 static struct ubus_object test_client_object = {
121         .type = &test_object_type,
122         .methods = test_methods,
123         .n_methods = ARRAY_SIZE(test_methods),
124 };
125
126 static void server_main(void)
127 {
128         int ret;
129
130         ret = ubus_add_object(ctx, &test_object);
131         if (ret)
132                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
133
134         ret = ubus_register_watch_object(ctx, &test_event);
135         if (ret)
136                 fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
137
138         uloop_run();
139 }
140
141 static void client_main(void)
142 {
143         uint32_t id;
144         int ret;
145
146         ret = ubus_add_object(ctx, &test_client_object);
147         if (ret) {
148                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
149                 return;
150         }
151
152         if (ubus_lookup_id(ctx, test_object.name, &id)) {
153                 fprintf(stderr, "Failed to look up test object\n");
154                 return;
155         }
156
157         blob_buf_init(&b, 0);
158         blobmsg_add_u32(&b, "id", test_client_object.id);
159         ubus_invoke(ctx, id, "watch", b.head, NULL, 0, 3000);
160         uloop_run();
161 }
162
163 int main(int argc, char **argv)
164 {
165         const char *ubus_socket = NULL;
166         bool client = false;
167         int ch;
168
169         while ((ch = getopt(argc, argv, "cs:")) != -1) {
170                 switch (ch) {
171                 case 's':
172                         ubus_socket = optarg;
173                         break;
174                 case 'c':
175                         client = true;
176                         break;
177                 default:
178                         break;
179                 }
180         }
181
182         argc -= optind;
183         argv += optind;
184
185         uloop_init();
186
187         ctx = ubus_connect(ubus_socket);
188         if (!ctx) {
189                 fprintf(stderr, "Failed to connect to ubus\n");
190                 return -1;
191         }
192
193         ubus_add_uloop(ctx);
194
195         if (client)
196                 client_main();
197         else
198                 server_main();
199
200         ubus_free(ctx);
201         uloop_done();
202
203         return 0;
204 }