3 #include <libubox/blobmsg_json.h>
6 static struct blob_buf b
;
7 static int timeout
= 30;
8 static bool simple_output
= false;
10 static const char *format_type(void *priv
, struct blob_attr
*attr
)
12 static const char * const attr_types
[] = {
13 [BLOBMSG_TYPE_INT8
] = "\"Boolean\"",
14 [BLOBMSG_TYPE_INT32
] = "\"Integer\"",
15 [BLOBMSG_TYPE_STRING
] = "\"String\"",
17 const char *type
= NULL
;
20 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
23 typeid = blobmsg_get_u32(attr
);
24 if (typeid < ARRAY_SIZE(attr_types
))
25 type
= attr_types
[typeid];
27 type
= "\"(unknown)\"";
32 static void receive_list_result(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
34 struct blob_attr
*cur
;
39 printf("%s\n", obj
->path
);
43 printf("'%s' @%08x\n", obj
->path
, obj
->id
);
48 blob_for_each_attr(cur
, obj
->signature
, rem
) {
49 s
= blobmsg_format_json_with_cb(cur
, false, format_type
, NULL
);
55 static void receive_call_result_data(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
61 str
= blobmsg_format_json(msg
, true);
66 static void receive_event(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
67 const char *type
, struct blob_attr
*msg
)
71 str
= blobmsg_format_json(msg
, true);
72 printf("{ \"%s\": %s }\n", type
, str
);
76 static int ubus_cli_list(struct ubus_context
*ctx
, int argc
, char **argv
)
78 const char *path
= NULL
;
86 return ubus_lookup(ctx
, path
, receive_list_result
, NULL
);
89 static int ubus_cli_call(struct ubus_context
*ctx
, int argc
, char **argv
)
94 if (argc
< 2 || argc
> 3)
98 if (argc
== 3 && !blobmsg_add_json_from_string(&b
, argv
[2])) {
99 fprintf(stderr
, "Failed to parse message data\n");
103 ret
= ubus_lookup_id(ctx
, argv
[0], &id
);
107 return ubus_invoke(ctx
, id
, argv
[1], b
.head
, receive_call_result_data
, NULL
, timeout
* 1000);
110 static int ubus_cli_listen(struct ubus_context
*ctx
, int argc
, char **argv
)
112 static struct ubus_event_handler listener
;
116 memset(&listener
, 0, sizeof(listener
));
117 listener
.cb
= receive_event
;
127 ret
= ubus_register_event_handler(ctx
, &listener
, event
);
140 fprintf(stderr
, "Error while registering for event '%s': %s\n",
141 event
, ubus_strerror(ret
));
153 static int ubus_cli_send(struct ubus_context
*ctx
, int argc
, char **argv
)
155 if (argc
< 1 || argc
> 2)
158 blob_buf_init(&b
, 0);
160 if (argc
== 2 && !blobmsg_add_json_from_string(&b
, argv
[1])) {
161 fprintf(stderr
, "Failed to parse message data\n");
165 return ubus_send_event(ctx
, argv
[0], b
.head
);
168 static int usage(const char *prog
)
171 "Usage: %s [<options>] <command> [arguments...]\n"
173 " -s <socket>: Set the unix domain socket to connect to\n"
174 " -t <timeout>: Set the timeout (in seconds) for a command to complete\n"
175 " -S: Use simplified output (for scripts)\n"
178 " - list [<path>] List objects\n"
179 " - call <path> <method> [<message>] Call an object method\n"
180 " - listen [<path>...] Listen for events\n"
181 " - send <type> [<message>] Send an event\n"
189 int (*cb
)(struct ubus_context
*ctx
, int argc
, char **argv
);
191 { "list", ubus_cli_list
},
192 { "call", ubus_cli_call
},
193 { "listen", ubus_cli_listen
},
194 { "send", ubus_cli_send
},
197 int main(int argc
, char **argv
)
199 const char *progname
, *ubus_socket
= NULL
;
200 static struct ubus_context
*ctx
;
207 while ((ch
= getopt(argc
, argv
, "s:t:S")) != -1) {
210 ubus_socket
= optarg
;
213 timeout
= atoi(optarg
);
216 simple_output
= true;
219 return usage(progname
);
228 return usage(progname
);
230 ctx
= ubus_connect(ubus_socket
);
232 fprintf(stderr
, "Failed to connect to ubus\n");
240 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
241 if (strcmp(commands
[i
].name
, cmd
) != 0)
244 ret
= commands
[i
].cb(ctx
, argc
, argv
);
249 fprintf(stderr
, "Command failed: %s\n", ubus_strerror(ret
));