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_INT8
] = "\"Boolean\"",
13 [BLOBMSG_TYPE_INT32
] = "\"Integer\"",
14 [BLOBMSG_TYPE_STRING
] = "\"String\"",
16 const char *type
= NULL
;
19 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
22 typeid = blobmsg_get_u32(attr
);
23 if (typeid < ARRAY_SIZE(attr_types
))
24 type
= attr_types
[typeid];
26 type
= "\"(unknown)\"";
31 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
33 struct blob_attr
*cur
;
37 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
42 blob_for_each_attr(cur
, obj
->signature
, rem
) {
43 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
);
49 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
55 str
= blobmsg_format_json(msg
, true);
60 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
61 const char *type
, struct blob_attr
*msg
)
65 str
= blobmsg_format_json(msg
, true);
66 printf("\"%s\": %s\n", type
, str
);
70 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
72 const char *path
= NULL
;
80 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
83 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
88 if (argc
< 2 || argc
> 3)
92 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
93 fprintf(stderr
, "Failed to parse message data\n");
97 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
101 return ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
104 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
106 static struct ubus_event_handler listener
;
110 memset(&listener
, 0, sizeof(listener
));
111 listener
.cb
= receive_event
;
121 ret
= ubus_register_event_handler(ctx
, &listener
, event
);
134 fprintf(stderr
, "Error while registering for event '%s': %s\n",
135 event
, ubus_strerror(ret
));
147 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
149 if (argc
< 1 || argc
> 2)
152 blob_buf_init(&b
, 0);
154 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
155 fprintf(stderr
, "Failed to parse message data\n");
159 return ubus_send_event(ctx
, argv
[0], b
.head
);
162 static int usage(const char *prog
)
165 "Usage: %s [<options>] <command> [arguments...]\n"
167 " -s <socket>: Set the unix domain socket to connect to\n"
170 " - list [<path>] List objects\n"
171 " - call <path> <method> [<message>] Call an object method\n"
172 " - listen [<path>...] Listen for events\n"
173 " - send <type> [<message>] Send an event\n"
181 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
183 { "list", ubus_cli_list
},
184 { "call", ubus_cli_call
},
185 { "listen", ubus_cli_listen
},
186 { "send", ubus_cli_send
},
189 int main(int argc
, char **argv
)
191 const char *progname
, *ubus_socket
= NULL
;
192 static struct ubus_context
*ctx
;
199 while ((ch
= getopt(argc
, argv
, "s:t:")) != -1) {
202 ubus_socket
= optarg
;
205 timeout
= atoi(optarg
);
208 return usage(progname
);
217 return usage(progname
);
219 ctx
= ubus_connect(ubus_socket
);
221 fprintf(stderr
, "Failed to connect to ubus\n");
229 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
230 if (strcmp(commands
[i
].name
, cmd
) != 0)
233 ret
= commands
[i
].cb(ctx
, argc
, argv
);
238 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));