libubus: fix recursive synchrnonous invoke commands
[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 struct blob_buf b;
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         UBUS_METHOD("hello", test_hello, hello_policy),
55 };
56
57 static struct ubus_object_type test_object_type =
58         UBUS_OBJECT_TYPE("test", test_methods);
59
60 static struct ubus_object test_object = {
61         .name = "test",
62         .type = &test_object_type,
63         .methods = test_methods,
64         .n_methods = ARRAY_SIZE(test_methods),
65 };
66
67 static struct ubus_object test_object2 = {
68         .name = "test2",
69         .type = &test_object_type,
70         .methods = test_methods,
71         .n_methods = ARRAY_SIZE(test_methods),
72 };
73
74 int main(int argc, char **argv)
75 {
76         const char *ubus_socket = NULL;
77         int ret = 0;
78         int ch;
79
80         while ((ch = getopt(argc, argv, "s:")) != -1) {
81                 switch (ch) {
82                 case 's':
83                         ubus_socket = optarg;
84                         break;
85                 default:
86                         break;
87                 }
88         }
89
90         argc -= optind;
91         argv += optind;
92
93         ctx = ubus_connect(ubus_socket);
94         if (!ctx) {
95                 fprintf(stderr, "Failed to connect to ubus\n");
96                 return -1;
97         }
98
99         ret = ubus_add_object(ctx, &test_object);
100         if (ret)
101                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
102
103         ret = ubus_add_object(ctx, &test_object2);
104         if (ret)
105                 fprintf(stderr, "Failed to add_object object: %s\n", ubus_strerror(ret));
106
107         uloop_init();
108         ubus_add_uloop(ctx);
109         uloop_run();
110         uloop_done();
111
112         ubus_free(ctx);
113         return 0;
114 }