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\"",
31 const char *type
= NULL
;
34 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
37 typeid = blobmsg_get_u32(attr
);
38 if (typeid < ARRAY_SIZE(attr_types
))
39 type
= attr_types
[typeid];
41 type
= "\"(unknown)\"";
46 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
48 struct blob_attr
*cur
;
52 if (simple_output
|| !verbose
) {
53 printf("%s\n", obj
->path
);
57 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
62 blob_for_each_attr(cur
, obj
->signature
, rem
) {
63 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
, -1);
69 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
75 str
= blobmsg_format_json_indent(msg
, true, simple_output
? -1 : 0);
80 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
81 const char *type
, struct blob_attr
*msg
)
85 str
= blobmsg_format_json(msg
, true);
86 printf("{ \"%s\": %s }\n", type
, str
);
90 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
92 const char *path
= NULL
;
100 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
103 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
108 if (argc
< 2 || argc
> 3)
111 blob_buf_init(&b
, 0);
112 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
114 fprintf(stderr
, "Failed to parse message data\n");
118 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
122 return ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
125 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
127 static struct ubus_event_handler listener
;
131 memset(&listener
, 0, sizeof(listener
));
132 listener
.cb
= receive_event
;
142 ret
= ubus_register_event_handler(ctx
, &listener
, event
);
156 fprintf(stderr
, "Error while registering for event '%s': %s\n",
157 event
, ubus_strerror(ret
));
169 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
171 if (argc
< 1 || argc
> 2)
174 blob_buf_init(&b
, 0);
176 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
178 fprintf(stderr
, "Failed to parse message data\n");
182 return ubus_send_event(ctx
, argv
[0], b
.head
);
185 static int usage(const char *prog
)
188 "Usage: %s [<options>] <command> [arguments...]\n"
190 " -s <socket>: Set the unix domain socket to connect to\n"
191 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
192 " -S: Use simplified output (for scripts)\n"
193 " -v: More verbose output\n"
196 " - list [<path>] List objects\n"
197 " - call <path> <method> [<message>] Call an object method\n"
198 " - listen [<path>...] Listen for events\n"
199 " - send <type> [<message>] Send an event\n"
207 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
209 { "list", ubus_cli_list
},
210 { "call", ubus_cli_call
},
211 { "listen", ubus_cli_listen
},
212 { "send", ubus_cli_send
},
215 int main(int argc
, char **argv
)
217 const char *progname
, *ubus_socket
= NULL
;
218 static struct ubus_context
*ctx
;
225 while ((ch
= getopt(argc
, argv
, "vs:t:S")) != -1) {
228 ubus_socket
= optarg
;
231 timeout
= atoi(optarg
);
234 simple_output
= true;
240 return usage(progname
);
249 return usage(progname
);
251 ctx
= ubus_connect(ubus_socket
);
254 fprintf(stderr
, "Failed to connect to ubus\n");
262 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
263 if (strcmp(commands
[i
].name
, cmd
) != 0)
266 ret
= commands
[i
].cb(ctx
, argc
, argv
);
270 if (ret
> 0 && !simple_output
)
271 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));