1 #include <libubox/blobmsg_json.h>
4 static struct blob_buf b
;
6 static void receive_lookup(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
12 fprintf(stderr
, "'%s' @%08x\n", obj
->path
, obj
->id
);
17 blob_for_each_attr(cur
, obj
->signature
, rem
) {
18 s
= blobmsg_format_json(cur
, false);
19 fprintf(stderr
, "\t%s\n", s
);
24 static void receive_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
30 str
= blobmsg_format_json(msg
, true);
31 fprintf(stderr
, "%s\n", str
);
36 static int usage(char *prog
)
39 "Usage: %s <command> [arguments...]\n"
41 " - list [<path>] List objects\n"
42 " - call <path> <method> [<message>] Call an object method\n"
43 " - listen [<path>...] Listen for events\n"
48 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
49 const char *type
, struct blob_attr
*msg
)
54 str
= blobmsg_format_json(msg
, true);
58 fprintf(stderr
, "\"%s\":{ %s }\n", type
, str
);
62 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
64 static struct ubus_event_handler listener
;
68 memset(&listener
, 0, sizeof(listener
));
69 listener
.cb
= receive_event
;
73 ret
= ubus_register_event_handler(ctx
, &listener
, NULL
);
76 for (;argc
;argv
++, argc
--) {
78 ret
= ubus_register_event_handler(ctx
, &listener
, argv
[0]);
84 fprintf(stderr
, "Error while registering for event '%s': %s\n",
85 event
, ubus_strerror(ret
));
96 int main(int argc
, char **argv
)
98 static struct ubus_context
*ctx
;
102 ctx
= ubus_connect(NULL
);
104 fprintf(stderr
, "Failed to connect to ubus\n");
110 return usage(argv
[0]);
112 if (!strcmp(cmd
, "list")) {
113 const char *path
= NULL
;
118 ret
= ubus_lookup(ctx
, path
, receive_lookup
, NULL
);
119 } else if (!strcmp(cmd
, "call")) {
122 if (argc
< 4 || argc
> 5)
123 return usage(argv
[0]);
125 blob_buf_init(&b
, 0);
126 if (argc
== 5 && !blobmsg_add_json_from_string(&b
, argv
[4])) {
127 fprintf(stderr
, "Failed to parse message data\n");
131 ret
= ubus_lookup_id(ctx
, argv
[2], &id
);
133 ret
= ubus_invoke(ctx
, id
, argv
[3], b
.head
, receive_data
, NULL
);
134 } else if (!strcmp(cmd
, "listen")) {
135 ret
= ubus_cli_listen(ctx
, argc
- 2, argv
+ 2);
137 return usage(argv
[0]);
141 fprintf(stderr
, "Failed: %s\n", ubus_strerror(ret
));