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 listen_timeout
;
21 static int timeout
= 30;
22 static bool simple_output
= false;
23 static int verbose
= 0;
24 static int monitor_dir
= -1;
25 static uint32_t monitor_mask
;
26 static const char * const monitor_types
[] = {
27 [UBUS_MSG_HELLO
] = "hello",
28 [UBUS_MSG_STATUS
] = "status",
29 [UBUS_MSG_DATA
] = "data",
30 [UBUS_MSG_PING
] = "ping",
31 [UBUS_MSG_LOOKUP
] = "lookup",
32 [UBUS_MSG_INVOKE
] = "invoke",
33 [UBUS_MSG_ADD_OBJECT
] = "add_object",
34 [UBUS_MSG_REMOVE_OBJECT
] = "remove_object",
35 [UBUS_MSG_SUBSCRIBE
] = "subscribe",
36 [UBUS_MSG_UNSUBSCRIBE
] = "unsubscribe",
37 [UBUS_MSG_NOTIFY
] = "notify",
40 static const char *format_type(void *priv
, struct blob_attr
*attr
)
42 static const char * const attr_types
[] = {
43 [BLOBMSG_TYPE_INT8
] = "\"Boolean\"",
44 [BLOBMSG_TYPE_INT32
] = "\"Integer\"",
45 [BLOBMSG_TYPE_STRING
] = "\"String\"",
46 [BLOBMSG_TYPE_ARRAY
] = "\"Array\"",
47 [BLOBMSG_TYPE_TABLE
] = "\"Table\"",
49 const char *type
= NULL
;
52 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
55 typeid = blobmsg_get_u32(attr
);
56 if (typeid < ARRAY_SIZE(attr_types
))
57 type
= attr_types
[typeid];
59 type
= "\"(unknown)\"";
64 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
66 struct blob_attr
*cur
;
70 if (simple_output
|| !verbose
) {
71 printf("%s\n", obj
->path
);
75 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
80 blob_for_each_attr(cur
, obj
->signature
, rem
) {
81 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
, -1);
87 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
93 str
= blobmsg_format_json_indent(msg
, true, simple_output
? -1 : 0);
98 static void print_event(const char *type
, struct blob_attr
*msg
)
102 str
= blobmsg_format_json(msg
, true);
103 printf("{ \"%s\": %s }\n", type
, str
);
108 static int receive_request(struct ubus_context
*ctx
, struct ubus_object
*obj
,
109 struct ubus_request_data
*req
,
110 const char *method
, struct blob_attr
*msg
)
112 print_event(method
, msg
);
116 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
117 const char *type
, struct blob_attr
*msg
)
119 print_event(type
, msg
);
122 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
124 const char *path
= NULL
;
132 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
135 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
140 if (argc
< 2 || argc
> 3)
143 blob_buf_init(&b
, 0);
144 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
145 return UBUS_STATUS_PARSE_ERROR
;
148 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
152 return ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
155 struct cli_listen_data
{
156 struct uloop_timeout timeout
;
160 static void ubus_cli_listen_timeout(struct uloop_timeout
*timeout
)
162 struct cli_listen_data
*data
= container_of(timeout
, struct cli_listen_data
, timeout
);
163 data
->timed_out
= true;
167 static void do_listen(struct ubus_context
*ctx
, struct cli_listen_data
*data
)
169 memset(data
, 0, sizeof(*data
));
170 data
->timeout
.cb
= ubus_cli_listen_timeout
;
174 uloop_timeout_set(&data
->timeout
, listen_timeout
* 1000);
179 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
181 struct ubus_event_handler ev
= {
184 struct cli_listen_data data
;
196 ret
= ubus_register_event_handler(ctx
, &ev
, event
);
210 fprintf(stderr
, "Error while registering for event '%s': %s\n",
211 event
, ubus_strerror(ret
));
215 do_listen(ctx
, &data
);
220 static int ubus_cli_subscribe(struct ubus_context
*ctx
, int argc
, char **argv
)
222 struct ubus_subscriber sub
= {
223 .cb
= receive_request
,
225 struct cli_listen_data data
;
233 fprintf(stderr
, "You need to specify an object to subscribe to\n");
237 ret
= ubus_register_subscriber(ctx
, &sub
);
238 for (; !ret
&& argc
> 0; argc
--, argv
++) {
241 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
245 ret
= ubus_subscribe(ctx
, &sub
, id
);
250 fprintf(stderr
, "Error while registering for event '%s': %s\n",
251 event
, ubus_strerror(ret
));
255 do_listen(ctx
, &data
);
261 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
263 if (argc
< 1 || argc
> 2)
266 blob_buf_init(&b
, 0);
268 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
269 return UBUS_STATUS_PARSE_ERROR
;
272 return ubus_send_event(ctx
, argv
[0], b
.head
);
275 struct cli_wait_data
{
276 struct uloop_timeout timeout
;
277 struct ubus_event_handler ev
;
282 static void wait_check_object(struct cli_wait_data
*data
, const char *path
)
286 for (i
= 0; i
< data
->n_pending
; i
++) {
287 if (strcmp(path
, data
->pending
[i
]) != 0)
291 if (i
== data
->n_pending
)
294 memmove(&data
->pending
[i
], &data
->pending
[i
+ 1],
295 (data
->n_pending
- i
) * sizeof(*data
->pending
));
299 if (!data
->n_pending
)
303 static void wait_event_cb(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
304 const char *type
, struct blob_attr
*msg
)
306 static const struct blobmsg_policy policy
= {
307 "path", BLOBMSG_TYPE_STRING
309 struct cli_wait_data
*data
= container_of(ev
, struct cli_wait_data
, ev
);
310 struct blob_attr
*attr
;
313 if (strcmp(type
, "ubus.object.add") != 0)
316 blobmsg_parse(&policy
, 1, &attr
, blob_data(msg
), blob_len(msg
));
320 path
= blobmsg_data(attr
);
321 wait_check_object(data
, path
);
324 static void wait_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
326 struct cli_wait_data
*data
= priv
;
328 wait_check_object(data
, obj
->path
);
332 static void wait_timeout(struct uloop_timeout
*timeout
)
337 static int ubus_cli_wait_for(struct ubus_context
*ctx
, int argc
, char **argv
)
339 struct cli_wait_data data
= {
340 .timeout
.cb
= wait_timeout
,
341 .ev
.cb
= wait_event_cb
,
353 ret
= ubus_register_event_handler(ctx
, &data
.ev
, "ubus.object.add");
360 ret
= ubus_lookup(ctx
, NULL
, wait_list_cb
, &data
);
367 uloop_timeout_set(&data
.timeout
, timeout
* 1000);
372 return UBUS_STATUS_TIMEOUT
;
378 ubus_cli_msg_type(uint32_t type
)
380 const char *ret
= NULL
;
381 static char unk_type
[16];
384 if (type
< ARRAY_SIZE(monitor_types
))
385 ret
= monitor_types
[type
];
388 snprintf(unk_type
, sizeof(unk_type
), "%d", type
);
396 ubus_cli_get_monitor_data(struct blob_attr
*data
)
398 static const struct blob_attr_info policy
[UBUS_ATTR_MAX
] = {
399 [UBUS_ATTR_STATUS
] = { .type
= BLOB_ATTR_INT32
},
400 [UBUS_ATTR_OBJPATH
] = { .type
= BLOB_ATTR_STRING
},
401 [UBUS_ATTR_OBJID
] = { .type
= BLOB_ATTR_INT32
},
402 [UBUS_ATTR_METHOD
] = { .type
= BLOB_ATTR_STRING
},
403 [UBUS_ATTR_OBJTYPE
] = { .type
= BLOB_ATTR_INT32
},
404 [UBUS_ATTR_SIGNATURE
] = { .type
= BLOB_ATTR_NESTED
},
405 [UBUS_ATTR_DATA
] = { .type
= BLOB_ATTR_NESTED
},
406 [UBUS_ATTR_ACTIVE
] = { .type
= BLOB_ATTR_INT8
},
407 [UBUS_ATTR_NO_REPLY
] = { .type
= BLOB_ATTR_INT8
},
408 [UBUS_ATTR_USER
] = { .type
= BLOB_ATTR_STRING
},
409 [UBUS_ATTR_GROUP
] = { .type
= BLOB_ATTR_STRING
},
411 static const char * const names
[UBUS_ATTR_MAX
] = {
412 [UBUS_ATTR_STATUS
] = "status",
413 [UBUS_ATTR_OBJPATH
] = "objpath",
414 [UBUS_ATTR_OBJID
] = "objid",
415 [UBUS_ATTR_METHOD
] = "method",
416 [UBUS_ATTR_OBJTYPE
] = "objtype",
417 [UBUS_ATTR_SIGNATURE
] = "signature",
418 [UBUS_ATTR_DATA
] = "data",
419 [UBUS_ATTR_ACTIVE
] = "active",
420 [UBUS_ATTR_NO_REPLY
] = "no_reply",
421 [UBUS_ATTR_USER
] = "user",
422 [UBUS_ATTR_GROUP
] = "group",
424 struct blob_attr
*tb
[UBUS_ATTR_MAX
];
427 blob_buf_init(&b
, 0);
428 blob_parse(data
, tb
, policy
, UBUS_ATTR_MAX
);
430 for (i
= 0; i
< UBUS_ATTR_MAX
; i
++) {
431 const char *n
= names
[i
];
432 struct blob_attr
*v
= tb
[i
];
437 switch(policy
[i
].type
) {
438 case BLOB_ATTR_INT32
:
439 blobmsg_add_u32(&b
, n
, blob_get_int32(v
));
441 case BLOB_ATTR_STRING
:
442 blobmsg_add_string(&b
, n
, blob_data(v
));
445 blobmsg_add_u8(&b
, n
, !!blob_get_int8(v
));
447 case BLOB_ATTR_NESTED
:
448 blobmsg_add_field(&b
, BLOBMSG_TYPE_TABLE
, n
, blobmsg_data(v
), blobmsg_data_len(v
));
453 return blobmsg_format_json(b
.head
, true);
457 ubus_cli_monitor_cb(struct ubus_context
*ctx
, uint32_t seq
, struct blob_attr
*msg
)
459 static const struct blob_attr_info policy
[UBUS_MONITOR_MAX
] = {
460 [UBUS_MONITOR_CLIENT
] = { .type
= BLOB_ATTR_INT32
},
461 [UBUS_MONITOR_PEER
] = { .type
= BLOB_ATTR_INT32
},
462 [UBUS_MONITOR_SEND
] = { .type
= BLOB_ATTR_INT8
},
463 [UBUS_MONITOR_TYPE
] = { .type
= BLOB_ATTR_INT32
},
464 [UBUS_MONITOR_DATA
] = { .type
= BLOB_ATTR_NESTED
},
466 struct blob_attr
*tb
[UBUS_MONITOR_MAX
];
467 uint32_t client
, peer
, type
;
471 blob_parse_untrusted(msg
, blob_raw_len(msg
), tb
, policy
, UBUS_MONITOR_MAX
);
473 if (!tb
[UBUS_MONITOR_CLIENT
] ||
474 !tb
[UBUS_MONITOR_PEER
] ||
475 !tb
[UBUS_MONITOR_SEND
] ||
476 !tb
[UBUS_MONITOR_TYPE
] ||
477 !tb
[UBUS_MONITOR_DATA
]) {
478 printf("Invalid monitor msg\n");
482 send
= blob_get_int32(tb
[UBUS_MONITOR_SEND
]);
483 client
= blob_get_int32(tb
[UBUS_MONITOR_CLIENT
]);
484 peer
= blob_get_int32(tb
[UBUS_MONITOR_PEER
]);
485 type
= blob_get_int32(tb
[UBUS_MONITOR_TYPE
]);
487 if (monitor_mask
&& type
< 32 && !(monitor_mask
& (1 << type
)))
490 if (monitor_dir
>= 0 && send
!= monitor_dir
)
493 data
= ubus_cli_get_monitor_data(tb
[UBUS_MONITOR_DATA
]);
494 printf("%s %08x #%08x %14s: %s\n", send
? "->" : "<-", client
, peer
, ubus_cli_msg_type(type
), data
);
499 static int ubus_cli_monitor(struct ubus_context
*ctx
, int argc
, char **argv
)
505 ctx
->monitor_cb
= ubus_cli_monitor_cb
;
506 ret
= ubus_monitor_start(ctx
);
513 ubus_monitor_stop(ctx
);
517 static int add_monitor_type(const char *type
)
521 for (i
= 0; i
< ARRAY_SIZE(monitor_types
); i
++) {
522 if (!monitor_types
[i
] || strcmp(monitor_types
[i
], type
) != 0)
525 monitor_mask
|= 1 << i
;
532 static int usage(const char *prog
)
535 "Usage: %s [<options>] <command> [arguments...]\n"
537 " -s <socket>: Set the unix domain socket to connect to\n"
538 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
539 " -S: Use simplified output (for scripts)\n"
540 " -v: More verbose output\n"
541 " -m <type>: (for monitor): include a specific message type\n"
542 " (can be used more than once)\n"
543 " -M <r|t> (for monitor): only capture received or transmitted traffic\n"
546 " - list [<path>] List objects\n"
547 " - call <path> <method> [<message>] Call an object method\n"
548 " - subscribe <path> [<path>...] Subscribe to object(s) notifications\n"
549 " - listen [<path>...] Listen for events\n"
550 " - send <type> [<message>] Send an event\n"
551 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
552 " - monitor Monitor ubus traffic\n"
560 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
562 { "list", ubus_cli_list
},
563 { "call", ubus_cli_call
},
564 { "listen", ubus_cli_listen
},
565 { "subscribe", ubus_cli_subscribe
},
566 { "send", ubus_cli_send
},
567 { "wait_for", ubus_cli_wait_for
},
568 { "monitor", ubus_cli_monitor
},
571 int main(int argc
, char **argv
)
573 const char *progname
, *ubus_socket
= NULL
;
574 struct ubus_context
*ctx
;
582 while ((ch
= getopt(argc
, argv
, "m:M:vs:t:S")) != -1) {
585 ubus_socket
= optarg
;
588 listen_timeout
= atoi(optarg
);
589 timeout
= atoi(optarg
);
592 simple_output
= true;
598 if (add_monitor_type(optarg
))
599 return usage(progname
);
610 return usage(progname
);
614 return usage(progname
);
623 return usage(progname
);
625 ctx
= ubus_connect(ubus_socket
);
628 fprintf(stderr
, "Failed to connect to ubus\n");
636 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
637 if (strcmp(commands
[i
].name
, cmd
) != 0)
640 ret
= commands
[i
].cb(ctx
, argc
, argv
);
644 if (ret
> 0 && !simple_output
)
645 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));