set the default socket name to /var/run/ubus.sock
[project/ubus.git] / cli.c
1 #include <unistd.h>
2
3 #include <libubox/blobmsg_json.h>
4 #include "libubus.h"
5
6 static struct blob_buf b;
7
8 static const char *attr_types[] = {
9         [BLOBMSG_TYPE_INT32] = "\"Integer\"",
10         [BLOBMSG_TYPE_STRING] = "\"String\"",
11 };
12
13 static const char *format_type(void *priv, struct blob_attr *attr)
14 {
15         const char *type = NULL;
16         int typeid;
17
18         if (blob_id(attr) != BLOBMSG_TYPE_INT32)
19                 return NULL;
20
21         typeid = blobmsg_get_u32(attr);
22         if (typeid < ARRAY_SIZE(attr_types))
23                 type = attr_types[typeid];
24         if (!type)
25                 type = "\"(unknown)\"";
26
27         return type;
28 }
29
30 static void receive_lookup(struct ubus_context *ctx, struct ubus_object_data *obj, void *priv)
31 {
32         struct blob_attr *cur;
33         char *s;
34         int rem;
35
36         fprintf(stderr, "'%s' @%08x\n", obj->path, obj->id);
37
38         if (!obj->signature)
39                 return;
40
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);
44                 free(s);
45         }
46 }
47
48 static void receive_data(struct ubus_request *req, int type, struct blob_attr *msg)
49 {
50         char *str;
51         if (!msg)
52                 return;
53
54         str = blobmsg_format_json(msg, true);
55         fprintf(stderr, "%s\n", str);
56         free(str);
57 }
58
59
60 static void receive_event(struct ubus_context *ctx, struct ubus_event_handler *ev,
61                           const char *type, struct blob_attr *msg)
62 {
63         char *str;
64
65         if (msg)
66                 str = blobmsg_format_json(msg, true);
67         else
68                 str = "";
69
70         fprintf(stderr, "\"%s\":{ %s }\n", type, str);
71         free(str);
72 }
73
74 static int ubus_cli_listen(struct ubus_context *ctx, int argc, char **argv)
75 {
76         static struct ubus_event_handler listener;
77         const char *event;
78         int ret = 0;
79
80         memset(&listener, 0, sizeof(listener));
81         listener.cb = receive_event;
82
83         if (!argc) {
84                 event = "*";
85                 ret = ubus_register_event_handler(ctx, &listener, NULL);
86         }
87
88         for (;argc;argv++, argc--) {
89                 event = argv[0];
90                 ret = ubus_register_event_handler(ctx, &listener, argv[0]);
91                 if (ret)
92                         break;
93         }
94
95         if (ret) {
96                 fprintf(stderr, "Error while registering for event '%s': %s\n",
97                         event, ubus_strerror(ret));
98         }
99
100         uloop_init();
101         ubus_add_uloop(ctx);
102         uloop_run();
103         uloop_done();
104
105         return 0;
106 }
107
108 static int usage(const char *prog)
109 {
110         fprintf(stderr,
111                 "Usage: %s [<options>] <command> [arguments...]\n"
112                 "Options:\n"
113                 " -s <socket>:          Set the unix domain socket to connect to\n"
114                 "\n"
115                 "Commands:\n"
116                 " - list [<path>]                       List objects\n"
117                 " - call <path> <method> [<message>]    Call an object method\n"
118                 " - listen [<path>...]                  Listen for events\n"
119                 "\n", prog);
120         return 1;
121 }
122
123 int main(int argc, char **argv)
124 {
125         const char *progname, *ubus_socket = NULL;
126         static struct ubus_context *ctx;
127         char *cmd;
128         int ret = 0;
129         int ch;
130
131         progname = argv[0];
132
133         while ((ch = getopt(argc, argv, "s:")) != -1) {
134                 switch (ch) {
135                 case 's':
136                         ubus_socket = optarg;
137                         break;
138                 default:
139                         return usage(progname);
140                 }
141         }
142
143         argc -= optind;
144         argv += optind;
145
146         ctx = ubus_connect(ubus_socket);
147         if (!ctx) {
148                 fprintf(stderr, "Failed to connect to ubus\n");
149                 return -1;
150         }
151
152         cmd = argv[0];
153         if (argc < 1)
154                 return usage(progname);
155
156         argv++;
157         argc--;
158
159         if (!strcmp(cmd, "list")) {
160                 const char *path = NULL;
161
162                 if (argc == 1)
163                         path = argv[0];
164
165                 ret = ubus_lookup(ctx, path, receive_lookup, NULL);
166         } else if (!strcmp(cmd, "call")) {
167                 uint32_t id;
168
169                 if (argc < 2 || argc > 3)
170                         return usage(progname);
171
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");
175                         goto out;
176                 }
177
178                 ret = ubus_lookup_id(ctx, argv[0], &id);
179                 if (!ret)
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);
183         } else {
184                 return usage(progname);
185         }
186
187         if (ret)
188                 fprintf(stderr, "Failed: %s\n", ubus_strerror(ret));
189
190 out:
191         ubus_free(ctx);
192         return ret;
193 }