lua: propagate incoming message to method callback inside the lua context
[project/ubus.git] / cli.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 <libubox/blobmsg_json.h>
17 #include "libubus.h"
18
19 static struct blob_buf b;
20 static int timeout = 30;
21 static bool simple_output = false;
22 static int verbose = 0;
23
24 static const char *format_type(void *priv, struct blob_attr *attr)
25 {
26         static const char * const attr_types[] = {
27                 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
28                 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
29                 [BLOBMSG_TYPE_STRING] = "\"String\"",
30                 [BLOBMSG_TYPE_ARRAY] = "\"Array\"",
31                 [BLOBMSG_TYPE_TABLE] = "\"Table\"",
32         };
33         const char *type = NULL;
34         int typeid;
35
36         if (blob_id(attr) != BLOBMSG_TYPE_INT32)
37                 return NULL;
38
39         typeid = blobmsg_get_u32(attr);
40         if (typeid < ARRAY_SIZE(attr_types))
41                 type = attr_types[typeid];
42         if (!type)
43                 type = "\"(unknown)\"";
44
45         return type;
46 }
47
48 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
49 {
50         struct blob_attr *cur;
51         char *s;
52         int rem;
53
54         if (simple_output || !verbose) {
55                 printf("%s\n", obj->path);
56                 return;
57         }
58
59         printf("'%s' @%08x\n", obj->path, obj->id);
60
61         if (!obj->signature)
62                 return;
63
64         blob_for_each_attr(cur, obj->signature, rem) {
65                 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
66                 printf("\t%s\n", s);
67                 free(s);
68         }
69 }
70
71 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
72 {
73         char *str;
74         if (!msg)
75                 return;
76
77         str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
78         printf("%s\n", str);
79         free(str);
80 }
81
82 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
83                           const char *type, struct blob_attr *msg)
84 {
85         char *str;
86
87         str = blobmsg_format_json(msg, true);
88         printf("{ \"%s\": %s }\n", type, str);
89         free(str);
90 }
91
92 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
93 {
94         const char *path = NULL;
95
96         if (argc > 1)
97                 return -2;
98
99         if (argc == 1)
100                 path = argv[0];
101
102         return ubus_lookup(ctx, path, receive_list_result, NULL);
103 }
104
105 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
106 {
107         uint32_t id;
108         int ret;
109
110         if (argc < 2 || argc > 3)
111                 return -2;
112
113         blob_buf_init(&b, 0);
114         if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
115                 if (!simple_output)
116                         fprintf(stderr, "Failed to parse message data\n");
117                 return -1;
118         }
119
120         ret = ubus_lookup_id(ctx, argv[0], &id);
121         if (ret)
122                 return ret;
123
124         return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
125 }
126
127 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
128 {
129         static struct ubus_event_handler listener;
130         const char *event;
131         int ret = 0;
132
133         memset(&listener, 0, sizeof(listener));
134         listener.cb = receive_event;
135
136         if (argc > 0) {
137                 event = argv[0];
138         } else {
139                 event = "*";
140                 argc = 1;
141         }
142
143         do {
144                 ret = ubus_register_event_handler(ctx, &listener, event);
145                 if (ret)
146                         break;
147
148                 argv++;
149                 argc--;
150                 if (argc <= 0)
151                         break;
152
153                 event = argv[0];
154         } while (1);
155
156         if (ret) {
157                 if (!simple_output)
158                         fprintf(stderr, "Error while registering for event '%s': %s\n",
159                                 event, ubus_strerror(ret));
160                 return -1;
161         }
162
163         uloop_init();
164         ubus_add_uloop(ctx);
165         uloop_run();
166         uloop_done();
167
168         return 0;
169 }
170
171 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
172 {
173         if (argc < 1 || argc > 2)
174                 return -2;
175
176         blob_buf_init(&b, 0);
177
178         if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
179                 if (!simple_output)
180                         fprintf(stderr, "Failed to parse message data\n");
181                 return -1;
182         }
183
184         return ubus_send_event(ctx, argv[0], b.head);
185 }
186
187 static int usage(const char *prog)
188 {
189         fprintf(stderr,
190                 "Usage: %s [<options>] <command> [arguments...]\n"
191                 "Options:\n"
192                 " -s <socket>:          Set the unix domain socket to connect to\n"
193                 " -t <timeout>:         Set the timeout (in seconds) for a command to complete\n"
194                 " -S:                   Use simplified output (for scripts)\n"
195                 " -v:                   More verbose output\n"
196                 "\n"
197                 "Commands:\n"
198                 " - list [<path>]                       List objects\n"
199                 " - call <path> <method> [<message>]    Call an object method\n"
200                 " - listen [<path>...]                  Listen for events\n"
201                 " - send <type> [<message>]             Send an event\n"
202                 "\n", prog);
203         return 1;
204 }
205
206
207 struct {
208         const char *name;
209         int (*cb)(struct ubus_context *ctx, int argc, char **argv);
210 } commands[] = {
211         { "list", ubus_cli_list },
212         { "call", ubus_cli_call },
213         { "listen", ubus_cli_listen },
214         { "send", ubus_cli_send },
215 };
216
217 int main(int argc, char **argv)
218 {
219         const char *progname, *ubus_socket = NULL;
220         static struct ubus_context *ctx;
221         char *cmd;
222         int ret = 0;
223         int i, ch;
224
225         progname = argv[0];
226
227         while ((ch = getopt(argc, argv, "vs:t:S")) != -1) {
228                 switch (ch) {
229                 case 's':
230                         ubus_socket = optarg;
231                         break;
232                 case 't':
233                         timeout = atoi(optarg);
234                         break;
235                 case 'S':
236                         simple_output = true;
237                         break;
238                 case 'v':
239                         verbose++;
240                         break;
241                 default:
242                         return usage(progname);
243                 }
244         }
245
246         argc -= optind;
247         argv += optind;
248
249         cmd = argv[0];
250         if (argc < 1)
251                 return usage(progname);
252
253         ctx = ubus_connect(ubus_socket);
254         if (!ctx) {
255                 if (!simple_output)
256                         fprintf(stderr, "Failed to connect to ubus\n");
257                 return -1;
258         }
259
260         argv++;
261         argc--;
262
263         ret = -2;
264         for (i = 0; i < ARRAY_SIZE(commands); i++) {
265                 if (strcmp(commands[i].name, cmd) != 0)
266                         continue;
267
268                 ret = commands[i].cb(ctx, argc, argv);
269                 break;
270         }
271
272         if (ret > 0 && !simple_output)
273                 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));
274         else if (ret == -2)
275                 usage(progname);
276
277         ubus_free(ctx);
278         return ret;
279 }