22fa7683bc401e28694777c532cbc8e188fe5ca8
3 static void receive_lookup(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
9 fprintf(stderr
, "'%s' @%08x\n", obj
->path
, obj
->id
);
14 blob_for_each_attr(cur
, obj
->signature
, rem
) {
15 s
= blobmsg_format_json(cur
, false);
16 fprintf(stderr
, "\t%s\n", s
);
21 static void receive_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
26 fprintf(stderr
, "%s\n", blobmsg_format_json(msg
, true));
30 static int usage(char *prog
)
33 "Usage: %s <command> [arguments...]\n"
35 " - list [<path>] List objects\n"
36 " - call <path> <method> [<message>] Call an object method\n"
37 " - listen [<path>...] Listen for events\n"
42 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
44 static struct ubus_object listener
;
50 ret
= ubus_register_event_handler(ctx
, &listener
, NULL
);
53 for (;argc
;argv
++, argc
--) {
55 ret
= ubus_register_event_handler(ctx
, &listener
, argv
[0]);
61 fprintf(stderr
, "Error while registering for event '%s': %s\n",
62 event
, ubus_strerror(ret
));
73 int main(int argc
, char **argv
)
75 static struct ubus_context
*ctx
;
79 ctx
= ubus_connect(NULL
);
81 fprintf(stderr
, "Failed to connect to ubus\n");
87 return usage(argv
[0]);
89 if (!strcmp(cmd
, "list")) {
90 const char *path
= NULL
;
95 ret
= ubus_lookup(ctx
, path
, receive_lookup
, NULL
);
96 } else if (!strcmp(cmd
, "call")) {
99 if (argc
< 4 || argc
> 5)
100 return usage(argv
[0]);
102 ret
= ubus_lookup_id(ctx
, argv
[2], &id
);
104 ret
= ubus_invoke(ctx
, id
, argv
[3], NULL
, receive_data
, NULL
);
105 } else if (!strcmp(cmd
, "listen")) {
106 ret
= ubus_cli_listen(ctx
, argc
- 2, argv
+ 2);
108 return usage(argv
[0]);
112 fprintf(stderr
, "Failed: %s\n", ubus_strerror(ret
));