16 #include <libubox/blobmsg_json.h>
19 static struct blob_buf
b;
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)\"";
66 struct blob_attr *cur;
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);
93 str = blobmsg_format_json_indent(msg,
true,
simple_output ? -1 : 0);
102 str = blobmsg_format_json(msg,
true);
103 printf(
"{ \"%s\": %s }\n",
type, str);
110 const char *method,
struct blob_attr *msg)
117 const char *
type,
struct blob_attr *msg)
127 fprintf(stderr,
"Command failed: ubus %s ", cmd);
128 for (i = 0; i < argc; i++) {
129 fprintf(stderr,
"%s ", argv[i]);
141 const char *
path = NULL;
157 if (argc < 2 || argc > 3)
160 blob_buf_init(&
b, 0);
161 if (argc == 3 && !blobmsg_add_json_from_string(&
b, argv[2])) {
190 memset(data, 0,
sizeof(*data));
231 fprintf(stderr,
"Error while registering for event '%s': %s\n",
254 fprintf(stderr,
"You need to specify an object to subscribe to\n");
259 for (; !ret && argc > 0; argc--, argv++) {
271 fprintf(stderr,
"Error while registering for event '%s': %s\n",
284 if (argc < 1 || argc > 2)
287 blob_buf_init(&
b, 0);
289 if (argc == 2 && !blobmsg_add_json_from_string(&
b, argv[1])) {
325 const char *
type,
struct blob_attr *msg)
327 static const struct blobmsg_policy policy = {
328 "path", BLOBMSG_TYPE_STRING
331 struct blob_attr *attr;
334 if (strcmp(
type,
"ubus.object.add") != 0)
337 blobmsg_parse(&policy, 1, &attr, blob_data(msg), blob_len(msg));
341 path = blobmsg_data(attr);
401 const char *ret = NULL;
402 static char unk_type[16];
409 snprintf(unk_type,
sizeof(unk_type),
"%d",
type);
448 blob_buf_init(&
b, 0);
452 const char *n = names[i];
453 struct blob_attr *v = tb[i];
458 switch(policy[i].
type) {
459 case BLOB_ATTR_INT32:
460 blobmsg_add_u32(&
b, n, blob_get_int32(v));
462 case BLOB_ATTR_STRING:
463 blobmsg_add_string(&
b, n, blob_data(v));
466 blobmsg_add_u8(&
b, n, !!blob_get_int8(v));
468 case BLOB_ATTR_NESTED:
469 blobmsg_add_field(&
b, BLOBMSG_TYPE_TABLE, n, blobmsg_data(v), blobmsg_data_len(v));
474 return blobmsg_format_json(
b.head,
true);
499 printf(
"Invalid monitor msg\n");
556 "Usage: %s [<options>] <command> [arguments...]\n"
558 " -s <socket>: Set the unix domain socket to connect to\n"
559 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
560 " -S: Use simplified output (for scripts)\n"
561 " -v: More verbose output\n"
562 " -m <type>: (for monitor): include a specific message type\n"
563 " (can be used more than once)\n"
564 " -M <r|t> (for monitor): only capture received or transmitted traffic\n"
567 " - list [<path>] List objects\n"
568 " - call <path> <method> [<message>] Call an object method\n"
569 " - subscribe <path> [<path>...] Subscribe to object(s) notifications\n"
570 " - listen [<path>...] Listen for events\n"
571 " - send <type> [<message>] Send an event\n"
572 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
573 " - monitor Monitor ubus traffic\n"
592 int main(
int argc,
char **argv)
594 const char *progname, *ubus_socket = NULL;
603 while ((ch = getopt(argc, argv,
"m:M:vs:t:S")) != -1) {
606 ubus_socket = optarg;
620 return usage(progname);
631 return usage(progname);
635 return usage(progname);
644 return usage(progname);
649 fprintf(stderr,
"Failed to connect to ubus\n");
657 for (i = 0; i < ARRAY_SIZE(
commands); i++) {
static int ubus_cli_wait_for(struct ubus_context *ctx, int argc, char **argv)
static int ubus_cli_error(char *cmd, int argc, char **argv, int err)
static int receive_request(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
int main(int argc, char **argv)
static int ubus_cli_subscribe(struct ubus_context *ctx, int argc, char **argv)
static void wait_list_cb(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
static void wait_check_object(struct cli_wait_data *data, const char *path)
static bool simple_output
static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
static struct @0 commands[]
static int add_monitor_type(const char *type)
static const char *const monitor_types[]
static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
static void ubus_cli_monitor_cb(struct ubus_context *ctx, uint32_t seq, struct blob_attr *msg)
static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
static int ubus_cli_monitor(struct ubus_context *ctx, int argc, char **argv)
static const char * format_type(void *priv, struct blob_attr *attr)
static void ubus_cli_listen_timeout(struct uloop_timeout *timeout)
static const char * ubus_cli_msg_type(uint32_t type)
static uint32_t monitor_mask
static char * ubus_cli_get_monitor_data(struct blob_attr *data)
int(* cb)(struct ubus_context *ctx, int argc, char **argv)
static void print_event(const char *type, struct blob_attr *msg)
static int listen_timeout
static int usage(const char *prog)
static void wait_timeout(struct uloop_timeout *timeout)
static void do_listen(struct ubus_context *ctx, struct cli_listen_data *data)
static void wait_event_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg)
static struct ubus_context * ctx
int ubus_subscribe(struct ubus_context *ctx, struct ubus_subscriber *obj, uint32_t id)
int ubus_register_subscriber(struct ubus_context *ctx, struct ubus_subscriber *s)
struct ubus_context * ubus_connect(const char *path)
int ubus_lookup(struct ubus_context *ctx, const char *path, ubus_lookup_handler_t cb, void *priv)
int ubus_send_event(struct ubus_context *ctx, const char *id, struct blob_attr *data)
void ubus_free(struct ubus_context *ctx)
int ubus_lookup_id(struct ubus_context *ctx, const char *path, uint32_t *id)
const char * ubus_strerror(int error)
int ubus_register_event_handler(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *pattern)
static int ubus_monitor_start(struct ubus_context *ctx)
static void ubus_add_uloop(struct ubus_context *ctx)
static int ubus_monitor_stop(struct ubus_context *ctx)
static int ubus_invoke(struct ubus_context *ctx, uint32_t obj, const char *method, struct blob_attr *msg, ubus_data_handler_t cb, void *priv, int timeout)
struct uloop_timeout timeout
struct ubus_event_handler ev
struct uloop_timeout timeout
void(* monitor_cb)(struct ubus_context *ctx, uint32_t seq, struct blob_attr *data)
struct blob_attr * signature
@ UBUS_STATUS_PARSE_ERROR