#include "libubus.h"
static struct blob_buf b;
+static int listen_timeout;
static int timeout = 30;
static bool simple_output = false;
static int verbose = 0;
[BLOBMSG_TYPE_TABLE] = "\"Table\"",
};
const char *type = NULL;
- int typeid;
+ size_t typeid;
if (blob_id(attr) != BLOBMSG_TYPE_INT32)
return NULL;
{
struct blob_attr *cur;
char *s;
- int rem;
+ size_t rem;
if (simple_output || !verbose) {
printf("%s\n", obj->path);
free(str);
}
-static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
- const char *type, struct blob_attr *msg)
+static void print_event(const char *type, struct blob_attr *msg)
{
char *str;
free(str);
}
+static int receive_request(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req,
+ const char *method, struct blob_attr *msg)
+{
+ print_event(method, msg);
+ return 0;
+}
+
+static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
+ const char *type, struct blob_attr *msg)
+{
+ print_event(type, msg);
+}
+
static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
{
const char *path = NULL;
struct cli_listen_data {
struct uloop_timeout timeout;
- struct ubus_event_handler ev;
bool timed_out;
};
-static void listen_timeout(struct uloop_timeout *timeout)
+static void ubus_cli_listen_timeout(struct uloop_timeout *timeout)
{
struct cli_listen_data *data = container_of(timeout, struct cli_listen_data, timeout);
data->timed_out = true;
uloop_end();
}
+static void do_listen(struct ubus_context *ctx, struct cli_listen_data *data)
+{
+ memset(data, 0, sizeof(*data));
+ data->timeout.cb = ubus_cli_listen_timeout;
+ uloop_init();
+ ubus_add_uloop(ctx);
+ if (listen_timeout)
+ uloop_timeout_set(&data->timeout, listen_timeout * 1000);
+ uloop_run();
+ uloop_done();
+}
+
static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
{
- struct cli_listen_data data = {
- .timeout.cb = listen_timeout,
- .ev.cb = receive_event,
- .timed_out = false,
+ struct ubus_event_handler ev = {
+ .cb = receive_event,
};
+ struct cli_listen_data data;
const char *event;
int ret = 0;
}
do {
- ret = ubus_register_event_handler(ctx, &data.ev, event);
+ ret = ubus_register_event_handler(ctx, &ev, event);
if (ret)
break;
return -1;
}
- uloop_init();
- ubus_add_uloop(ctx);
- uloop_timeout_set(&data.timeout, timeout * 1000);
- uloop_run();
- uloop_done();
+ do_listen(ctx, &data);
+
+ return 0;
+}
+
+static int ubus_cli_subscribe(struct ubus_context *ctx, int argc, char **argv)
+{
+ struct ubus_subscriber sub = {
+ .cb = receive_request,
+ };
+ struct cli_listen_data data;
+ const char *event;
+ int ret = 0;
+
+ if (argc > 0) {
+ event = argv[0];
+ } else {
+ if (!simple_output)
+ fprintf(stderr, "You need to specify an object to subscribe to\n");
+ return -1;
+ }
+
+ ret = ubus_register_subscriber(ctx, &sub);
+ for (; !ret && argc > 0; argc--, argv++) {
+ uint32_t id;
+
+ ret = ubus_lookup_id(ctx, argv[0], &id);
+ if (ret)
+ break;
+
+ ret = ubus_subscribe(ctx, &sub, id);
+ }
+
+ if (ret) {
+ if (!simple_output)
+ fprintf(stderr, "Error while registering for event '%s': %s\n",
+ event, ubus_strerror(ret));
+ return -1;
+ }
+
+ do_listen(ctx, &data);
return 0;
}
+
static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
{
if (argc < 1 || argc > 2)
uloop_init();
ubus_add_uloop(ctx);
- ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
+ ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
if (ret)
return ret;
if (!data.n_pending)
return ret;
- ret = ubus_register_event_handler(ctx, &data.ev, "ubus.object.add");
+ ret = ubus_lookup(ctx, NULL, wait_list_cb, &data);
if (ret)
return ret;
+ if (!data.n_pending)
+ return ret;
+
uloop_timeout_set(&data.timeout, timeout * 1000);
uloop_run();
uloop_done();
bool send;
char *data;
- blob_parse(msg, tb, policy, UBUS_MONITOR_MAX);
+ blob_parse_untrusted(msg, blob_raw_len(msg), tb, policy, UBUS_MONITOR_MAX);
if (!tb[UBUS_MONITOR_CLIENT] ||
!tb[UBUS_MONITOR_PEER] ||
static int add_monitor_type(const char *type)
{
- int i;
+ size_t i;
for (i = 0; i < ARRAY_SIZE(monitor_types); i++) {
if (!monitor_types[i] || strcmp(monitor_types[i], type) != 0)
"Commands:\n"
" - list [<path>] List objects\n"
" - call <path> <method> [<message>] Call an object method\n"
+ " - subscribe <path> [<path>...] Subscribe to object(s) notifications\n"
" - listen [<path>...] Listen for events\n"
" - send <type> [<message>] Send an event\n"
" - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
{ "list", ubus_cli_list },
{ "call", ubus_cli_call },
{ "listen", ubus_cli_listen },
+ { "subscribe", ubus_cli_subscribe },
{ "send", ubus_cli_send },
{ "wait_for", ubus_cli_wait_for },
{ "monitor", ubus_cli_monitor },
{
const char *progname, *ubus_socket = NULL;
struct ubus_context *ctx;
- char *cmd;
int ret = 0;
- int i, ch;
+ char *cmd;
+ size_t i;
+ int ch;
progname = argv[0];
ubus_socket = optarg;
break;
case 't':
+ listen_timeout = atoi(optarg);
timeout = atoi(optarg);
break;
case 'S':