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