s/set_device_present/device_set_present/
[project/netifd.git] / ubus.c
1 #include <string.h>
2
3 #include "netifd.h"
4 #include "interface.h"
5 #include "ubus.h"
6
7 static struct ubus_context *ctx = NULL;
8 static struct blob_buf b;
9
10 /* global object */
11
12 enum {
13         DEV_NAME,
14         DEV_FORCE,
15         DEV_LAST,
16 };
17
18 static const struct blobmsg_policy dev_policy[] = {
19         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
20         [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
21 };
22
23 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
24                                 struct ubus_request_data *req, const char *method,
25                                 struct blob_attr *msg)
26 {
27         struct device *dev;
28         struct blob_attr *tb[DEV_LAST];
29         bool add = !strncmp(method, "add", 3);
30
31         blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
32
33         if (!tb[DEV_NAME])
34                 return UBUS_STATUS_INVALID_ARGUMENT;
35
36         dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
37         if (!dev)
38                 return UBUS_STATUS_NOT_FOUND;
39
40         if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
41                 device_set_present(dev, add);
42         else
43                 check_device_state(dev);
44
45         return 0;
46 }
47
48 static struct ubus_method main_object_methods[] = {
49         UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
50         UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
51 };
52
53 static struct ubus_object_type main_object_type =
54         UBUS_OBJECT_TYPE("netifd", main_object_methods);
55
56 static struct ubus_object main_object = {
57         .name = "network.interface",
58         .type = &main_object_type,
59         .methods = main_object_methods,
60         .n_methods = ARRAY_SIZE(main_object_methods),
61 };
62
63 int netifd_ubus_init(const char *path)
64 {
65         int ret;
66
67         ctx = ubus_connect(path);
68         if (!ctx)
69                 return -EIO;
70
71         DPRINTF("connected as %08x\n", ctx->local_id);
72         uloop_init();
73         ubus_add_uloop(ctx);
74
75         ret = ubus_add_object(ctx, &main_object);
76         if (ret != 0)
77                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
78
79         return 0;
80 }
81
82 void netifd_ubus_done(void)
83 {
84         ubus_free(ctx);
85 }
86
87
88 /* per-interface object */
89
90 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
91                             struct ubus_request_data *req, const char *method,
92                             struct blob_attr *msg)
93 {
94         struct interface *iface;
95
96         iface = container_of(obj, struct interface, ubus);
97         set_interface_up(iface);
98
99         return 0;
100 }
101
102 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
103                               struct ubus_request_data *req, const char *method,
104                               struct blob_attr *msg)
105 {
106         struct interface *iface;
107
108         iface = container_of(obj, struct interface, ubus);
109         set_interface_down(iface);
110
111         return 0;
112 }
113
114 static void netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
115 {
116         struct interface_error *error;
117         void *e, *e2, *e3;
118         int i;
119
120         e = blobmsg_open_array(b, "errors");
121         list_for_each_entry(error, &iface->errors, list) {
122                 e2 = blobmsg_open_table(b, NULL);
123
124                 blobmsg_add_string(b, "subsystem", error->subsystem);
125                 blobmsg_add_string(b, "code", error->code);
126                 if (error->data[0]) {
127                         e3 = blobmsg_open_array(b, "data");
128                         for (i = 0; error->data[i]; i++)
129                                 blobmsg_add_string(b, NULL, error->data[i]);
130                         blobmsg_close_array(b, e3);
131                 }
132
133                 blobmsg_close_table(b, e2);
134         }
135         blobmsg_close_array(b, e);
136 }
137
138 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
139                                 struct ubus_request_data *req, const char *method,
140                                 struct blob_attr *msg)
141 {
142         static const char *iface_state[] = {
143                 [IFS_SETUP] = "setup",
144                 [IFS_UP] = "up",
145                 [IFS_TEARDOWN] = "teardown",
146                 [IFS_DOWN] = "down",
147         };
148         struct interface *iface;
149
150         iface = container_of(obj, struct interface, ubus);
151
152         blob_buf_init(&b, 0);
153         blobmsg_add_string(&b, "state", iface_state[iface->state]);
154         blobmsg_add_u8(&b, "active", iface->active);
155         blobmsg_add_u8(&b, "autostart", iface->autostart);
156         if (iface->main_dev.dev) {
157                 struct device *dev = iface->main_dev.dev;
158                 const char *field;
159                 void *devinfo;
160
161                 /* use a different field for virtual devices */
162                 if (dev->avl.key)
163                         field = "device";
164                 else
165                         field = "link";
166
167                 devinfo = blobmsg_open_table(&b, field);
168                 blobmsg_add_string(&b, "name", dev->ifname);
169
170                 if (dev->type->dump_status)
171                         dev->type->dump_status(dev, &b);
172
173                 blobmsg_close_table(&b, devinfo);
174         }
175
176         if (!list_is_empty(&iface->errors))
177                 netifd_add_interface_errors(&b, iface);
178
179         ubus_send_reply(ctx, req, b.head);
180
181         return 0;
182 }
183
184
185 static struct ubus_method iface_object_methods[] = {
186         { .name = "up", .handler = netifd_handle_up },
187         { .name = "down", .handler = netifd_handle_down },
188         { .name = "status", .handler = netifd_handle_status },
189 };
190
191 static struct ubus_object_type iface_object_type =
192         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
193
194
195 void netifd_ubus_add_interface(struct interface *iface)
196 {
197         struct ubus_object *obj = &iface->ubus;
198         char *name;
199
200         name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
201         if (!name)
202                 return;
203
204         sprintf(name, "%s.%s", main_object.name, iface->name);
205         obj->name = name;
206         obj->type = &iface_object_type;
207         obj->methods = iface_object_methods;
208         obj->n_methods = ARRAY_SIZE(iface_object_methods);
209         if (ubus_add_object(ctx, &iface->ubus)) {
210                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
211                 free(name);
212                 obj->name = NULL;
213         }
214 }
215
216 void netifd_ubus_remove_interface(struct interface *iface)
217 {
218         if (!iface->ubus.name)
219                 return;
220
221         ubus_remove_object(ctx, &iface->ubus);
222         free((void *) iface->ubus.name);
223 }