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;
23 static const char *format_type(void *priv, struct blob_attr *attr)
25 static const char * const attr_types[] = {
26 [BLOBMSG_TYPE_INT8] = "\"Boolean\"",
27 [BLOBMSG_TYPE_INT32] = "\"Integer\"",
28 [BLOBMSG_TYPE_STRING] = "\"String\"",
30 const char *type = NULL;
33 if (blob_id(attr) != BLOBMSG_TYPE_INT32)
36 typeid = blobmsg_get_u32(attr);
37 if (typeid < ARRAY_SIZE(attr_types))
38 type = attr_types[typeid];
40 type = "\"(unknown)\"";
45 static void receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
47 struct blob_attr *cur;
52 printf("%s\n", obj->path);
56 printf("'%s' @%08x\n", obj->path, obj->id);
61 blob_for_each_attr(cur, obj->signature, rem) {
62 s = blobmsg_format_json_with_cb(cur, false, format_type, NULL);
68 static void receive_call_result_data(struct ubus_request *req, int type, struct blob_attr *msg)
74 str = blobmsg_format_json(msg, true);
79 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
80 const char *type, struct blob_attr *msg)
84 str = blobmsg_format_json(msg, true);
85 printf("{ \"%s\": %s }\n", type, str);
89 static int ubus_cli_list(struct ubus_context *ctx, int argc, char **argv)
91 const char *path = NULL;
99 return ubus_lookup(ctx, path, receive_list_result, NULL);
102 static int ubus_cli_call(struct ubus_context *ctx, int argc, char **argv)
107 if (argc < 2 || argc > 3)
110 blob_buf_init(&b, 0);
111 if (argc == 3 && !blobmsg_add_json_from_string(&b, argv[2])) {
113 fprintf(stderr, "Failed to parse message data\n");
117 ret = ubus_lookup_id(ctx, argv[0], &id);
121 return ubus_invoke(ctx, id, argv[1], b.head, receive_call_result_data, NULL, timeout * 1000);
124 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
126 static struct ubus_event_handler listener;
130 memset(&listener, 0, sizeof(listener));
131 listener.cb = receive_event;
141 ret = ubus_register_event_handler(ctx, &listener, event);
155 fprintf(stderr, "Error while registering for event '%s': %s\n",
156 event, ubus_strerror(ret));
168 static int ubus_cli_send(struct ubus_context *ctx, int argc, char **argv)
170 if (argc < 1 || argc > 2)
173 blob_buf_init(&b, 0);
175 if (argc == 2 && !blobmsg_add_json_from_string(&b, argv[1])) {
177 fprintf(stderr, "Failed to parse message data\n");
181 return ubus_send_event(ctx, argv[0], b.head);
184 static int usage(const char *prog)
187 "Usage: %s [<options>] <command> [arguments...]\n"
189 " -s <socket>: Set the unix domain socket to connect to\n"
190 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
191 " -S: Use simplified output (for scripts)\n"
194 " - list [<path>] List objects\n"
195 " - call <path> <method> [<message>] Call an object method\n"
196 " - listen [<path>...] Listen for events\n"
197 " - send <type> [<message>] Send an event\n"
205 int (*cb)(struct ubus_context *ctx, int argc, char **argv);
207 { "list", ubus_cli_list },
208 { "call", ubus_cli_call },
209 { "listen", ubus_cli_listen },
210 { "send", ubus_cli_send },
213 int main(int argc, char **argv)
215 const char *progname, *ubus_socket = NULL;
216 static struct ubus_context *ctx;
223 while ((ch = getopt(argc, argv, "s:t:S")) != -1) {
226 ubus_socket = optarg;
229 timeout = atoi(optarg);
232 simple_output = true;
235 return usage(progname);
244 return usage(progname);
246 ctx = ubus_connect(ubus_socket);
249 fprintf(stderr, "Failed to connect to ubus\n");
257 for (i = 0; i < ARRAY_SIZE(commands); i++) {
258 if (strcmp(commands[i].name, cmd) != 0)
261 ret = commands[i].cb(ctx, argc, argv);
265 if (ret > 0 && !simple_output)
266 fprintf(stderr, "Command failed: %s\n", ubus_strerror(ret));