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;
23 static int monitor_dir = -1;
24 static uint32_t monitor_mask;
25 static const char * const monitor_types[] = {
26 [UBUS_MSG_HELLO] = "hello",
27 [UBUS_MSG_STATUS] = "status",
28 [UBUS_MSG_DATA] = "data",
29 [UBUS_MSG_PING] = "ping",
30 [UBUS_MSG_LOOKUP] = "lookup",
31 [UBUS_MSG_INVOKE] = "invoke",
32 [UBUS_MSG_ADD_OBJECT] = "add_object",
33 [UBUS_MSG_REMOVE_OBJECT] = "remove_object",
34 [UBUS_MSG_SUBSCRIBE] = "subscribe",
35 [UBUS_MSG_UNSUBSCRIBE] = "unsubscribe",
36 [UBUS_MSG_NOTIFY] = "notify",
39 static const char *format_type(void *priv, struct blob_attr *attr)
41 static const char * const attr_types[] = {
42 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
43 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
44 [BLOBMSG_TYPE_STRING] = "\"String\"",
45 [BLOBMSG_TYPE_ARRAY] = "\"Array\"",
46 [BLOBMSG_TYPE_TABLE] = "\"Table\"",
48 const char *type = NULL;
51 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
54 typeid = blobmsg_get_u32(attr);
55 if (typeid < ARRAY_SIZE(attr_types))
56 type = attr_types[typeid];
58 type = "\"(unknown)\"";
63 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
65 struct blob_attr *cur;
69 if (simple_output || !verbose) {
70 printf("%s\n", obj->path);
74 printf("'%s' @%08x\n", obj->path, obj->id);
79 blob_for_each_attr(cur, obj->signature, rem) {
80 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL, -1);
86 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
92 str = blobmsg_format_json_indent(msg, true, simple_output ? -1 : 0);
97 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
98 const char *type, struct blob_attr *msg)
102 str = blobmsg_format_json(msg, true);
103 printf("{ \"%s\": %s }\n", type, str);
108 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
110 const char *path = NULL;
118 return ubus_lookup(ctx, path, receive_list_result, NULL);
121 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
126 if (argc < 2 || argc > 3)
129 blob_buf_init(&b, 0);
130 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
132 fprintf(stderr, "Failed to parse message data\n");
136 ret = ubus_lookup_id(ctx, argv[0], &id);
140 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
143 struct cli_listen_data {
144 struct uloop_timeout timeout;
145 struct ubus_event_handler ev;
149 static void listen_timeout(struct uloop_timeout *timeout)
151 struct cli_listen_data *data = container_of(timeout, struct cli_listen_data, timeout);
152 data->timed_out = true;
156 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
158 struct cli_listen_data data = {
159 .timeout.cb = listen_timeout,
160 .ev.cb = receive_event,
174 ret = ubus_register_event_handler(ctx, &data.ev, event);
188 fprintf(stderr, "Error while registering for event '%s': %s\n",
189 event, ubus_strerror(ret));
195 uloop_timeout_set(&data.timeout, timeout * 1000);
202 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
204 if (argc < 1 || argc > 2)
207 blob_buf_init(&b, 0);
209 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
211 fprintf(stderr, "Failed to parse message data\n");
215 return ubus_send_event(ctx, argv[0], b.head);
218 struct cli_wait_data {
219 struct uloop_timeout timeout;
220 struct ubus_event_handler ev;
225 static void wait_check_object(struct cli_wait_data *data, const char *path)
229 for (i = 0; i < data->n_pending; i++) {
230 if (strcmp(path, data->pending[i]) != 0)
234 if (i == data->n_pending)
237 memmove(&data->pending[i], &data->pending[i + 1],
238 (data->n_pending - i) * sizeof(*data->pending));
242 if (!data->n_pending)
246 static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev,
247 const char *type, struct blob_attr *msg)
249 static const struct blobmsg_policy policy = {
250 "path", BLOBMSG_TYPE_STRING
252 struct cli_wait_data *data = container_of(ev, struct cli_wait_data, ev);
253 struct blob_attr *attr;
256 if (strcmp(type, "ubus.object.add") != 0)
259 blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
263 path = blobmsg_data(attr);
264 wait_check_object(data, path);
267 static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
269 struct cli_wait_data *data = priv;
271 wait_check_object(data, obj->path);
275 static void wait_timeout(struct uloop_timeout *timeout)
280 static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
282 struct cli_wait_data data = {
283 .timeout.cb = wait_timeout,
284 .ev.cb = wait_event_cb,
296 ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
303 ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
307 uloop_timeout_set(&data.timeout, timeout * 1000);
312 return UBUS_STATUS_TIMEOUT;
318 ubus_cli_msg_type(uint32_t type)
320 const char *ret = NULL;
321 static char unk_type[16];
324 if (type < ARRAY_SIZE(monitor_types))
325 ret = monitor_types[type];
328 snprintf(unk_type, sizeof(unk_type), "%d", type);
336 ubus_cli_get_monitor_data(struct blob_attr *data)
338 static const struct blob_attr_info policy[UBUS_ATTR_MAX] = {
339 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
340 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
341 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
342 [UBUS_ATTR_METHOD] = { .type = BLOB_ATTR_STRING },
343 [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
344 [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
345 [UBUS_ATTR_DATA] = { .type = BLOB_ATTR_NESTED },
346 [UBUS_ATTR_ACTIVE] = { .type = BLOB_ATTR_INT8 },
347 [UBUS_ATTR_NO_REPLY] = { .type = BLOB_ATTR_INT8 },
348 [UBUS_ATTR_USER] = { .type = BLOB_ATTR_STRING },
349 [UBUS_ATTR_GROUP] = { .type = BLOB_ATTR_STRING },
351 static const char * const names[UBUS_ATTR_MAX] = {
352 [UBUS_ATTR_STATUS] = "status",
353 [UBUS_ATTR_OBJPATH] = "objpath",
354 [UBUS_ATTR_OBJID] = "objid",
355 [UBUS_ATTR_METHOD] = "method",
356 [UBUS_ATTR_OBJTYPE] = "objtype",
357 [UBUS_ATTR_SIGNATURE] = "signature",
358 [UBUS_ATTR_DATA] = "data",
359 [UBUS_ATTR_ACTIVE] = "active",
360 [UBUS_ATTR_NO_REPLY] = "no_reply",
361 [UBUS_ATTR_USER] = "user",
362 [UBUS_ATTR_GROUP] = "group",
364 struct blob_attr *tb[UBUS_ATTR_MAX];
367 blob_buf_init(&b, 0);
368 blob_parse(data, tb, policy, UBUS_ATTR_MAX);
370 for (i = 0; i < UBUS_ATTR_MAX; i++) {
371 const char *n = names[i];
372 struct blob_attr *v = tb[i];
377 switch(policy[i].type) {
378 case BLOB_ATTR_INT32:
379 blobmsg_add_u32(&b, n, blob_get_int32(v));
381 case BLOB_ATTR_STRING:
382 blobmsg_add_string(&b, n, blob_data(v));
385 blobmsg_add_u8(&b, n, !!blob_get_int8(v));
387 case BLOB_ATTR_NESTED:
388 blobmsg_add_field(&b, BLOBMSG_TYPE_TABLE, n, blobmsg_data(v), blobmsg_data_len(v));
393 return blobmsg_format_json(b.head, true);
397 ubus_cli_monitor_cb(struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg)
399 static const struct blob_attr_info policy[UBUS_MONITOR_MAX] = {
400 [UBUS_MONITOR_CLIENT] = { .type = BLOB_ATTR_INT32 },
401 [UBUS_MONITOR_PEER] = { .type = BLOB_ATTR_INT32 },
402 [UBUS_MONITOR_SEND] = { .type = BLOB_ATTR_INT8 },
403 [UBUS_MONITOR_TYPE] = { .type = BLOB_ATTR_INT32 },
404 [UBUS_MONITOR_DATA] = { .type = BLOB_ATTR_NESTED },
406 struct blob_attr *tb[UBUS_MONITOR_MAX];
407 uint32_t client, peer, type;
411 blob_parse(msg, tb, policy, UBUS_MONITOR_MAX);
413 if (!tb[UBUS_MONITOR_CLIENT] ||
414 !tb[UBUS_MONITOR_PEER] ||
415 !tb[UBUS_MONITOR_SEND] ||
416 !tb[UBUS_MONITOR_TYPE] ||
417 !tb[UBUS_MONITOR_DATA]) {
418 printf("Invalid monitor msg\n");
422 send = blob_get_int32(tb[UBUS_MONITOR_SEND]);
423 client = blob_get_int32(tb[UBUS_MONITOR_CLIENT]);
424 peer = blob_get_int32(tb[UBUS_MONITOR_PEER]);
425 type = blob_get_int32(tb[UBUS_MONITOR_TYPE]);
427 if (monitor_mask && type < 32 && !(monitor_mask & (1 << type)))
430 if (monitor_dir >= 0 && send != monitor_dir)
433 data = ubus_cli_get_monitor_data(tb[UBUS_MONITOR_DATA]);
434 printf("%s %08x #%08x %14s: %s\n", send ? "->" : "<-", client, peer, ubus_cli_msg_type(type), data);
439 static int ubus_cli_monitor(struct ubus_context *ctx, int argc, char **argv)
445 ctx->monitor_cb = ubus_cli_monitor_cb;
446 ret = ubus_monitor_start(ctx);
453 ubus_monitor_stop(ctx);
457 static int add_monitor_type(const char *type)
461 for (i = 0; i < ARRAY_SIZE(monitor_types); i++) {
462 if (!monitor_types[i] || strcmp(monitor_types[i], type) != 0)
465 monitor_mask |= 1 << i;
472 static int usage(const char *prog)
475 "Usage: %s [<options>] <command> [arguments...]\n"
477 " -s <socket>: Set the unix domain socket to connect to\n"
478 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
479 " -S: Use simplified output (for scripts)\n"
480 " -v: More verbose output\n"
481 " -m <type>: (for monitor): include a specific message type\n"
482 " (can be used more than once)\n"
483 " -M <r|t> (for monitor): only capture received or transmitted traffic\n"
486 " - list [<path>] List objects\n"
487 " - call <path> <method> [<message>] Call an object method\n"
488 " - listen [<path>...] Listen for events\n"
489 " - send <type> [<message>] Send an event\n"
490 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
491 " - monitor Monitor ubus traffic\n"
499 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
501 { "list", ubus_cli_list },
502 { "call", ubus_cli_call },
503 { "listen", ubus_cli_listen },
504 { "send", ubus_cli_send },
505 { "wait_for", ubus_cli_wait_for },
506 { "monitor", ubus_cli_monitor },
509 int main(int argc, char **argv)
511 const char *progname, *ubus_socket = NULL;
512 struct ubus_context *ctx;
519 while ((ch = getopt(argc, argv, "m:M:vs:t:S")) != -1) {
522 ubus_socket = optarg;
525 timeout = atoi(optarg);
528 simple_output = true;
534 if (add_monitor_type(optarg))
535 return usage(progname);
546 return usage(progname);
550 return usage(progname);
559 return usage(progname);
561 ctx = ubus_connect(ubus_socket);
564 fprintf(stderr, "Failed to connect to ubus\n");
572 for (i = 0; i < ARRAY_SIZE(commands); i++) {
573 if (strcmp(commands[i].name, cmd) != 0)
576 ret = commands[i].cb(ctx, argc, argv);
580 if (ret > 0 && !simple_output)
581 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));