make ubus_start_request static
[project/ubus.git] / cli.c
1 #include "libubus.h"
2
3 static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
4 {
5         struct blob_attr *cur;
6         char *s;
7         int rem;
8
9         fprintf(stderr, "'%s' @%08x\n", obj->path, obj->id);
10
11         if (!obj->signature)
12                 return;
13
14         blob_for_each_attr(cur, obj->signature, rem) {
15                 s = blobmsg_format_json(cur, false);
16                 fprintf(stderr, "\t%s\n", s);
17                 free(s);
18         }
19 }
20
21 static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg)
22 {
23         if (!msg)
24                 return;
25
26         fprintf(stderr, "%s\n", blobmsg_format_json(msg, true));
27 }
28
29
30 static int usage(char *prog)
31 {
32         fprintf(stderr,
33                 "Usage: %s <command> [arguments...]\n"
34                 "Commands:\n"
35                 " - list [<path>]                       List objects\n"
36                 " - call <path> <method> [<message>]    Call an object method\n"
37                 "\n", prog);
38         return 1;
39 }
40
41 int main(int argc, char **argv)
42 {
43         static struct ubus_context *ctx;
44         char *cmd;
45         int ret;
46
47         ctx = ubus_connect(NULL);
48         if (!ctx) {
49                 fprintf(stderr, "Failed to connect to ubus\n");
50                 return -1;
51         }
52
53         cmd = argv[1];
54         if (argc < 2)
55                 return usage(argv[0]);
56
57         if (!strcmp(cmd, "list")) {
58                 const char *path = NULL;
59
60                 if (argc == 3)
61                         path = argv[2];
62
63                 ret = ubus_lookup(ctx, path, receive_lookup, NULL);
64         } else if (!strcmp(cmd, "call")) {
65                 uint32_t id;
66
67                 if (argc < 4 || argc > 5)
68                         return usage(argv[0]);
69
70                 ret = ubus_lookup_id(ctx, argv[2], &id);
71                 if (!ret)
72                         ret = ubus_invoke(ctx, id, argv[3], NULL, receive_data, NULL);
73         } else {
74                 return usage(argv[0]);
75         }
76
77         if (ret)
78                 fprintf(stderr, "Failed: %s\n", ubus_strerror(ret));
79
80         ubus_free(ctx);
81         return 0;
82 }