2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
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
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.
16 #include <libubox/blobmsg_json.h>
19 static struct blob_buf b;
20 static int timeout = 30;
21 static bool simple_output = false;
22 static int verbose = 0;
24 static const char *format_type(void *priv, struct blob_attr *attr)
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\"",
33 const char *type = NULL;
36 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
39 typeid = blobmsg_get_u32(attr);
40 if (typeid < ARRAY_SIZE(attr_types))
41 type = attr_types[typeid];
43 type = "\"(unknown)\"";
48 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
50 struct blob_attr *cur;
54 if (simple_output || !verbose) {
55 printf("%s\n", obj->path);
59 printf("'%s' @%08x\n", obj->path, obj->id);
64 blob_for_each_attr(cur, obj->signature, rem) {
65 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
71 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
77 str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
82 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
83 const char *type, struct blob_attr *msg)
87 str = blobmsg_format_json(msg, true);
88 printf("{ \"%s\": %s }\n", type, str);
93 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
95 const char *path = NULL;
103 return ubus_lookup(ctx, path, receive_list_result, NULL);
106 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
111 if (argc < 2 || argc > 3)
114 blob_buf_init(&b, 0);
115 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
117 fprintf(stderr, "Failed to parse message data\n");
121 ret = ubus_lookup_id(ctx, argv[0], &id);
125 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
128 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
130 static struct ubus_event_handler listener;
134 memset(&listener, 0, sizeof(listener));
135 listener.cb = receive_event;
145 ret = ubus_register_event_handler(ctx, &listener, event);
159 fprintf(stderr, "Error while registering for event '%s': %s\n",
160 event, ubus_strerror(ret));
172 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
174 if (argc < 1 || argc > 2)
177 blob_buf_init(&b, 0);
179 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
181 fprintf(stderr, "Failed to parse message data\n");
185 return ubus_send_event(ctx, argv[0], b.head);
188 struct cli_wait_data {
189 struct uloop_timeout timeout;
190 struct ubus_event_handler ev;
195 static void wait_check_object(struct cli_wait_data *data, const char *path)
199 for (i = 0; i < data->n_pending; i++) {
200 if (strcmp(path, data->pending[i]) != 0)
204 if (i == data->n_pending)
207 memmove(&data->pending[i], &data->pending[i + 1],
208 (data->n_pending - i) * sizeof(*data->pending));
212 if (!data->n_pending)
216 static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
217 const char *type, struct blob_attr *msg)
219 static const struct blobmsg_policy policy = {
220 "path", BLOBMSG_TYPE_STRING
222 struct cli_wait_data *data = container_of(ev, struct cli_wait_data, ev);
223 struct blob_attr *attr;
226 if (strcmp(type, "ubus.object.add") != 0)
229 blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
233 path = blobmsg_data(attr);
234 wait_check_object(data, path);
237 static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
239 struct cli_wait_data *data = priv;
241 wait_check_object(data, obj->path);
245 static void wait_timeout(struct uloop_timeout *timeout)
250 static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
252 struct cli_wait_data data = {
253 .timeout.cb = wait_timeout,
254 .ev.cb = wait_event_cb,
266 ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
273 ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
277 uloop_timeout_set(&data.timeout, timeout * 1000);
282 return UBUS_STATUS_TIMEOUT;
288 static int usage(const char *prog)
291 "Usage: %s [<options>] <command> [arguments...]\n"
293 " -s <socket>: Set the unix domain socket to connect to\n"
294 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
295 " -S: Use simplified output (for scripts)\n"
296 " -v: More verbose output\n"
299 " - list [<path>] List objects\n"
300 " - call <path> <method> [<message>] Call an object method\n"
301 " - listen [<path>...] Listen for events\n"
302 " - send <type> [<message>] Send an event\n"
303 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
311 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
313 { "list", ubus_cli_list },
314 { "call", ubus_cli_call },
315 { "listen", ubus_cli_listen },
316 { "send", ubus_cli_send },
317 { "wait_for", ubus_cli_wait_for },
320 int main(int argc, char **argv)
322 const char *progname, *ubus_socket = NULL;
323 static struct ubus_context *ctx;
330 while ((ch = getopt(argc, argv, "vs:t:S")) != -1) {
333 ubus_socket = optarg;
336 timeout = atoi(optarg);
339 simple_output = true;
345 return usage(progname);
354 return usage(progname);
356 ctx = ubus_connect(ubus_socket);
359 fprintf(stderr, "Failed to connect to ubus\n");
367 for (i = 0; i < ARRAY_SIZE(commands); i++) {
368 if (strcmp(commands[i].name, cmd) != 0)
371 ret = commands[i].cb(ctx, argc, argv);
375 if (ret > 0 && !simple_output)
376 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));