2 * Copyright (C) 2011 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include <libubox/blobmsg_json.h>
19 static struct blob_buf b
;
20 static int timeout
= 30;
21 static bool simple_output
= false;
22 static int verbose
= 0;
24 static const char *format_type(void *priv
, struct blob_attr
*attr
)
26 static const char * const attr_types
[] = {
27 [BLOBMSG_TYPE_INT8
] = "\"Boolean\"",
28 [BLOBMSG_TYPE_INT32
] = "\"Integer\"",
29 [BLOBMSG_TYPE_STRING
] = "\"String\"",
30 [BLOBMSG_TYPE_ARRAY
] = "\"Array\"",
31 [BLOBMSG_TYPE_TABLE
] = "\"Table\"",
33 const char *type
= NULL
;
36 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
39 typeid = blobmsg_get_u32(attr
);
40 if (typeid < ARRAY_SIZE(attr_types
))
41 type
= attr_types
[typeid];
43 type
= "\"(unknown)\"";
48 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
50 struct blob_attr
*cur
;
54 if (simple_output
|| !verbose
) {
55 printf("%s\n", obj
->path
);
59 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
64 blob_for_each_attr(cur
, obj
->signature
, rem
) {
65 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
, -1);
71 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
77 str
= blobmsg_format_json_indent(msg
, true, simple_output
? -1 : 0);
82 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
83 const char *type
, struct blob_attr
*msg
)
87 str
= blobmsg_format_json(msg
, true);
88 printf("{ \"%s\": %s }\n", type
, str
);
92 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
94 const char *path
= NULL
;
102 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
105 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
110 if (argc
< 2 || argc
> 3)
113 blob_buf_init(&b
, 0);
114 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
116 fprintf(stderr
, "Failed to parse message data\n");
120 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
124 return ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
127 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
129 static struct ubus_event_handler listener
;
133 memset(&listener
, 0, sizeof(listener
));
134 listener
.cb
= receive_event
;
144 ret
= ubus_register_event_handler(ctx
, &listener
, event
);
158 fprintf(stderr
, "Error while registering for event '%s': %s\n",
159 event
, ubus_strerror(ret
));
171 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
173 if (argc
< 1 || argc
> 2)
176 blob_buf_init(&b
, 0);
178 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
180 fprintf(stderr
, "Failed to parse message data\n");
184 return ubus_send_event(ctx
, argv
[0], b
.head
);
187 struct cli_wait_data
{
188 struct uloop_timeout timeout
;
189 struct ubus_event_handler ev
;
194 static void wait_check_object(struct cli_wait_data
*data
, const char *path
)
198 for (i
= 0; i
< data
->n_pending
; i
++) {
199 if (strcmp(path
, data
->pending
[i
]) != 0)
203 if (i
== data
->n_pending
)
206 memmove(&data
->pending
[i
], &data
->pending
[i
+ 1],
207 (data
->n_pending
- i
) * sizeof(*data
->pending
));
211 if (!data
->n_pending
)
215 static void wait_event_cb(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
216 const char *type
, struct blob_attr
*msg
)
218 static const struct blobmsg_policy policy
= {
219 "path", BLOBMSG_TYPE_STRING
221 struct cli_wait_data
*data
= container_of(ev
, struct cli_wait_data
, ev
);
222 struct blob_attr
*attr
;
225 if (strcmp(type
, "ubus.object.add") != 0)
228 blobmsg_parse(&policy
, 1, &attr
, blob_data(msg
), blob_len(msg
));
232 path
= blobmsg_data(attr
);
233 wait_check_object(data
, path
);
236 static void wait_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
238 struct cli_wait_data
*data
= priv
;
240 wait_check_object(data
, obj
->path
);
244 static void wait_timeout(struct uloop_timeout
*timeout
)
249 static int ubus_cli_wait_for(struct ubus_context
*ctx
, int argc
, char **argv
)
251 struct cli_wait_data data
= {
252 .timeout
.cb
= wait_timeout
,
253 .ev
.cb
= wait_event_cb
,
265 ret
= ubus_lookup(ctx
, NULL
, wait_list_cb
, &data
);
272 ret
= ubus_register_event_handler(ctx
, &data
.ev
, "ubus.object.add");
276 uloop_timeout_set(&data
.timeout
, timeout
* 1000);
281 return UBUS_STATUS_TIMEOUT
;
287 static int usage(const char *prog
)
290 "Usage: %s [<options>] <command> [arguments...]\n"
292 " -s <socket>: Set the unix domain socket to connect to\n"
293 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
294 " -S: Use simplified output (for scripts)\n"
295 " -v: More verbose output\n"
298 " - list [<path>] List objects\n"
299 " - call <path> <method> [<message>] Call an object method\n"
300 " - listen [<path>...] Listen for events\n"
301 " - send <type> [<message>] Send an event\n"
302 " - wait_for <object> [<object>...] Wait for multiple objects to appear on ubus\n"
310 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
312 { "list", ubus_cli_list
},
313 { "call", ubus_cli_call
},
314 { "listen", ubus_cli_listen
},
315 { "send", ubus_cli_send
},
316 { "wait_for", ubus_cli_wait_for
},
319 int main(int argc
, char **argv
)
321 const char *progname
, *ubus_socket
= NULL
;
322 static struct ubus_context
*ctx
;
329 while ((ch
= getopt(argc
, argv
, "vs:t:S")) != -1) {
332 ubus_socket
= optarg
;
335 timeout
= atoi(optarg
);
338 simple_output
= true;
344 return usage(progname
);
353 return usage(progname
);
355 ctx
= ubus_connect(ubus_socket
);
358 fprintf(stderr
, "Failed to connect to ubus\n");
366 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
367 if (strcmp(commands
[i
].name
, cmd
) != 0)
370 ret
= commands
[i
].cb(ctx
, argc
, argv
);
374 if (ret
> 0 && !simple_output
)
375 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));