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