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