3 #include <libubox/blobmsg_json.h>
6 static struct blob_buf b;
7 static int timeout = 30;
9 static const char *format_type(void *priv, struct blob_attr *attr)
11 static const char * const attr_types[] = {
12 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
13 [BLOBMSG_TYPE_STRING] = "\"String\"",
15 const char *type = NULL;
18 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
21 typeid = blobmsg_get_u32(attr);
22 if (typeid < ARRAY_SIZE(attr_types))
23 type = attr_types[typeid];
25 type = "\"(unknown)\"";
30 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
32 struct blob_attr *cur;
36 printf("'%s' @%08x\n", obj->path, obj->id);
41 blob_for_each_attr(cur, obj->signature, rem) {
42 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL);
48 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
54 str = blobmsg_format_json(msg, true);
59 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
60 const char *type, struct blob_attr *msg)
64 str = blobmsg_format_json(msg, true);
65 printf("\"%s\": %s\n", type, str);
69 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
71 const char *path = NULL;
79 return ubus_lookup(ctx, path, receive_list_result, NULL);
82 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
87 if (argc < 2 || argc > 3)
91 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
92 fprintf(stderr, "Failed to parse message data\n");
96 ret = ubus_lookup_id(ctx, argv[0], &id);
100 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
103 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
105 static struct ubus_event_handler listener;
109 memset(&listener, 0, sizeof(listener));
110 listener.cb = receive_event;
120 ret = ubus_register_event_handler(ctx, &listener, event);
133 fprintf(stderr, "Error while registering for event '%s': %s\n",
134 event, ubus_strerror(ret));
146 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
148 if (argc < 1 || argc > 2)
151 blob_buf_init(&b, 0);
153 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
154 fprintf(stderr, "Failed to parse message data\n");
158 return ubus_send_event(ctx, argv[0], b.head);
161 static int usage(const char *prog)
164 "Usage: %s [<options>] <command> [arguments...]\n"
166 " -s <socket>: Set the unix domain socket to connect to\n"
169 " - list [<path>] List objects\n"
170 " - call <path> <method> [<message>] Call an object method\n"
171 " - listen [<path>...] Listen for events\n"
172 " - send <type> [<message>] Send an event\n"
180 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
182 { "list", ubus_cli_list },
183 { "call", ubus_cli_call },
184 { "listen", ubus_cli_listen },
185 { "send", ubus_cli_send },
188 int main(int argc, char **argv)
190 const char *progname, *ubus_socket = NULL;
191 static struct ubus_context *ctx;
198 while ((ch = getopt(argc, argv, "s:t:")) != -1) {
201 ubus_socket = optarg;
204 timeout = atoi(optarg);
207 return usage(progname);
216 return usage(progname);
218 ctx = ubus_connect(ubus_socket);
220 fprintf(stderr, "Failed to connect to ubus\n");
228 for (i = 0; i < ARRAY_SIZE(commands); i++) {
229 if (strcmp(commands[i].name, cmd) != 0)
232 ret = commands[i].cb(ctx, argc, argv);
237 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));