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
)
27 str
= blobmsg_format_json(msg
, true);
28 fprintf(stderr
, "%s\n", str
);
33 static int usage(char *prog
)
36 "Usage: %s <command> [arguments...]\n"
38 " - list [<path>] List objects\n"
39 " - call <path> <method> [<message>] Call an object method\n"
40 " - listen [<path>...] Listen for events\n"
45 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
46 const char *type
, struct blob_attr
*msg
)
51 str
= blobmsg_format_json(msg
, true);
55 fprintf(stderr
, "\"%s\":{ %s }\n", type
, str
);
59 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
61 static struct ubus_event_handler listener
;
65 memset(&listener
, 0, sizeof(listener
));
66 listener
.cb
= receive_event
;
70 ret
= ubus_register_event_handler(ctx
, &listener
, NULL
);
73 for (;argc
;argv
++, argc
--) {
75 ret
= ubus_register_event_handler(ctx
, &listener
, argv
[0]);
81 fprintf(stderr
, "Error while registering for event '%s': %s\n",
82 event
, ubus_strerror(ret
));
93 int main(int argc
, char **argv
)
95 static struct ubus_context
*ctx
;
99 ctx
= ubus_connect(NULL
);
101 fprintf(stderr
, "Failed to connect to ubus\n");
107 return usage(argv
[0]);
109 if (!strcmp(cmd
, "list")) {
110 const char *path
= NULL
;
115 ret
= ubus_lookup(ctx
, path
, receive_lookup
, NULL
);
116 } else if (!strcmp(cmd
, "call")) {
119 if (argc
< 4 || argc
> 5)
120 return usage(argv
[0]);
122 ret
= ubus_lookup_id(ctx
, argv
[2], &id
);
124 ret
= ubus_invoke(ctx
, id
, argv
[3], NULL
, receive_data
, NULL
);
125 } else if (!strcmp(cmd
, "listen")) {
126 ret
= ubus_cli_listen(ctx
, argc
- 2, argv
+ 2);
128 return usage(argv
[0]);
132 fprintf(stderr
, "Failed: %s\n", ubus_strerror(ret
));