3 #include <libubox/blobmsg_json.h>
6 static struct blob_buf b
;
8 static const char *attr_types
[] = {
9 [BLOBMSG_TYPE_INT32
] = "\"Integer\"",
10 [BLOBMSG_TYPE_STRING
] = "\"String\"",
13 static const char *format_type(void *priv
, struct blob_attr
*attr
)
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_lookup(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
32 struct blob_attr
*cur
;
36 fprintf(stderr
, "'%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
);
43 fprintf(stderr
, "\t%s\n", s
);
48 static void receive_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
54 str
= blobmsg_format_json(msg
, true);
55 fprintf(stderr
, "%s\n", str
);
60 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
61 const char *type
, struct blob_attr
*msg
)
66 str
= blobmsg_format_json(msg
, true);
70 fprintf(stderr
, "\"%s\":{ %s }\n", type
, str
);
74 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
76 static struct ubus_event_handler listener
;
80 memset(&listener
, 0, sizeof(listener
));
81 listener
.cb
= receive_event
;
85 ret
= ubus_register_event_handler(ctx
, &listener
, NULL
);
88 for (;argc
;argv
++, argc
--) {
90 ret
= ubus_register_event_handler(ctx
, &listener
, argv
[0]);
96 fprintf(stderr
, "Error while registering for event '%s': %s\n",
97 event
, ubus_strerror(ret
));
108 static int usage(const char *prog
)
111 "Usage: %s [<options>] <command> [arguments...]\n"
113 " -s <socket>: Set the unix domain socket to connect to\n"
116 " - list [<path>] List objects\n"
117 " - call <path> <method> [<message>] Call an object method\n"
118 " - listen [<path>...] Listen for events\n"
123 int main(int argc
, char **argv
)
125 const char *progname
, *ubus_socket
= NULL
;
126 static struct ubus_context
*ctx
;
133 while ((ch
= getopt(argc
, argv
, "s:")) != -1) {
136 ubus_socket
= optarg
;
139 return usage(progname
);
146 ctx
= ubus_connect(ubus_socket
);
148 fprintf(stderr
, "Failed to connect to ubus\n");
154 return usage(progname
);
159 if (!strcmp(cmd
, "list")) {
160 const char *path
= NULL
;
165 ret
= ubus_lookup(ctx
, path
, receive_lookup
, NULL
);
166 } else if (!strcmp(cmd
, "call")) {
169 if (argc
< 2 || argc
> 3)
170 return usage(progname
);
172 blob_buf_init(&b
, 0);
173 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
174 fprintf(stderr
, "Failed to parse message data\n");
178 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
180 ret
= ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_data
, NULL
);
181 } else if (!strcmp(cmd
, "listen")) {
182 ret
= ubus_cli_listen(ctx
, argc
, argv
);
184 return usage(progname
);
188 fprintf(stderr
, "Failed: %s\n", ubus_strerror(ret
));