3 #include <libubox/blobmsg_json.h>
6 static struct blob_buf b;
7 static int timeout = 30;
8 static bool simple_output = false;
10 static const char *format_type(void *priv, struct blob_attr *attr)
12 static const char * const attr_types[] = {
13 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
14 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
15 [BLOBMSG_TYPE_STRING] = "\"String\"",
17 const char *type = NULL;
20 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
23 typeid = blobmsg_get_u32(attr);
24 if (typeid < ARRAY_SIZE(attr_types))
25 type = attr_types[typeid];
27 type = "\"(unknown)\"";
32 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
34 struct blob_attr *cur;
39 printf("%s\n", obj->path);
43 printf("'%s' @%08x\n", obj->path, obj->id);
48 blob_for_each_attr(cur, obj->signature, rem) {
49 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL);
55 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
61 str = blobmsg_format_json(msg, true);
66 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
67 const char *type, struct blob_attr *msg)
71 str = blobmsg_format_json(msg, true);
72 printf("{ \"%s\": %s }\n", type, str);
76 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
78 const char *path = NULL;
86 return ubus_lookup(ctx, path, receive_list_result, NULL);
89 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
94 if (argc < 2 || argc > 3)
98 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
100 fprintf(stderr, "Failed to parse message data\n");
104 ret = ubus_lookup_id(ctx, argv[0], &id);
108 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
111 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
113 static struct ubus_event_handler listener;
117 memset(&listener, 0, sizeof(listener));
118 listener.cb = receive_event;
128 ret = ubus_register_event_handler(ctx, &listener, event);
142 fprintf(stderr, "Error while registering for event '%s': %s\n",
143 event, ubus_strerror(ret));
155 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
157 if (argc < 1 || argc > 2)
160 blob_buf_init(&b, 0);
162 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
164 fprintf(stderr, "Failed to parse message data\n");
168 return ubus_send_event(ctx, argv[0], b.head);
171 static int usage(const char *prog)
174 "Usage: %s [<options>] <command> [arguments...]\n"
176 " -s <socket>: Set the unix domain socket to connect to\n"
177 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
178 " -S: Use simplified output (for scripts)\n"
181 " - list [<path>] List objects\n"
182 " - call <path> <method> [<message>] Call an object method\n"
183 " - listen [<path>...] Listen for events\n"
184 " - send <type> [<message>] Send an event\n"
192 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
194 { "list", ubus_cli_list },
195 { "call", ubus_cli_call },
196 { "listen", ubus_cli_listen },
197 { "send", ubus_cli_send },
200 int main(int argc, char **argv)
202 const char *progname, *ubus_socket = NULL;
203 static struct ubus_context *ctx;
210 while ((ch = getopt(argc, argv, "s:t:S")) != -1) {
213 ubus_socket = optarg;
216 timeout = atoi(optarg);
219 simple_output = true;
222 return usage(progname);
231 return usage(progname);
233 ctx = ubus_connect(ubus_socket);
236 fprintf(stderr, "Failed to connect to ubus\n");
244 for (i = 0; i < ARRAY_SIZE(commands); i++) {
245 if (strcmp(commands[i].name, cmd) != 0)
248 ret = commands[i].cb(ctx, argc, argv);
252 if (ret > 0 && !simple_output)
253 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));