add functionality for registering anonymous objects as event listeners
[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                 " - listen [<path>...]                  Listen for events\n"
38                 "\n", prog);
39         return 1;
40 }
41
42 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
43 {
44         static struct ubus_object listener;
45         const char *event;
46         int ret = 0;
47
48         if (!argc) {
49                 event = "*";
50                 ret = ubus_register_event_handler(ctx, &listener, NULL);
51         }
52
53         for (;argc;argv++, argc--) {
54                 event = argv[0];
55                 ret = ubus_register_event_handler(ctx, &listener, argv[0]);
56                 if (ret)
57                         break;
58         }
59
60         if (ret) {
61                 fprintf(stderr, "Error while registering for event '%s': %s\n",
62                         event, ubus_strerror(ret));
63         }
64
65         return 0;
66 }
67
68 int main(int argc, char **argv)
69 {
70         static struct ubus_context *ctx;
71         char *cmd;
72         int ret;
73
74         ctx = ubus_connect(NULL);
75         if (!ctx) {
76                 fprintf(stderr, "Failed to connect to ubus\n");
77                 return -1;
78         }
79
80         cmd = argv[1];
81         if (argc < 2)
82                 return usage(argv[0]);
83
84         if (!strcmp(cmd, "list")) {
85                 const char *path = NULL;
86
87                 if (argc == 3)
88                         path = argv[2];
89
90                 ret = ubus_lookup(ctx, path, receive_lookup, NULL);
91         } else if (!strcmp(cmd, "call")) {
92                 uint32_t id;
93
94                 if (argc < 4 || argc > 5)
95                         return usage(argv[0]);
96
97                 ret = ubus_lookup_id(ctx, argv[2], &id);
98                 if (!ret)
99                         ret = ubus_invoke(ctx, id, argv[3], NULL, receive_data, NULL);
100         } else if (!strcmp(cmd, "listen")) {
101                 ret = ubus_cli_listen(ctx, argc - 2, argv + 2);
102         } else {
103                 return usage(argv[0]);
104         }
105
106         if (ret)
107                 fprintf(stderr, "Failed: %s\n", ubus_strerror(ret));
108
109         ubus_free(ctx);
110         return ret;
111 }