add the status method to the signature
[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 UBUS_METHOD_START("status"),
106 UBUS_METHOD_END(),
107 };
108
109 static struct ubus_object_type iface_object_type =
110 UBUS_OBJECT_TYPE("netifd_iface", iface_object_sig);
111
112
113 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
116 {
117 struct interface *iface;
118
119 iface = container_of(obj, struct interface, ubus);
120 set_interface_up(iface);
121
122 return 0;
123 }
124
125 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method,
127 struct blob_attr *msg)
128 {
129 struct interface *iface;
130
131 iface = container_of(obj, struct interface, ubus);
132 set_interface_down(iface);
133
134 return 0;
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 struct interface *iface;
142
143 iface = container_of(obj, struct interface, ubus);
144
145 blob_buf_init(&b, 0);
146 blobmsg_add_u8(&b, "up", iface->up);
147 blobmsg_add_u8(&b, "active", iface->active);
148 blobmsg_add_u8(&b, "autostart", iface->autostart);
149 if (iface->main_dev.dev) {
150 struct device *dev = iface->main_dev.dev;
151 const char *field;
152 void *devinfo;
153
154 /* use a different field for virtual devices */
155 if (dev->avl.key)
156 field = "device";
157 else
158 field = "link";
159
160 devinfo = blobmsg_open_table(&b, field);
161 blobmsg_add_string(&b, "name", dev->ifname);
162
163 if (dev->type->dump_status)
164 dev->type->dump_status(dev, &b);
165
166 blobmsg_close_table(&b, devinfo);
167 }
168
169 ubus_send_reply(ctx, req, b.head);
170
171 return 0;
172 }
173
174 static struct ubus_method iface_object_methods[] = {
175 { .name = "up", .handler = netifd_handle_up },
176 { .name = "down", .handler = netifd_handle_down },
177 { .name = "status", .handler = netifd_handle_status },
178 };
179
180
181 void netifd_ubus_add_interface(struct interface *iface)
182 {
183 struct ubus_object *obj = &iface->ubus;
184 char *name;
185
186 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
187 if (!name)
188 return;
189
190 sprintf(name, "%s.%s", main_object.name, iface->name);
191 obj->name = name;
192 obj->type = &iface_object_type;
193 obj->methods = iface_object_methods;
194 obj->n_methods = ARRAY_SIZE(iface_object_methods);
195 if (ubus_add_object(ctx, &iface->ubus)) {
196 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
197 free(name);
198 obj->name = NULL;
199 }
200 }
201
202 void netifd_ubus_remove_interface(struct interface *iface)
203 {
204 if (!iface->ubus.name)
205 return;
206
207 ubus_remove_object(ctx, &iface->ubus);
208 free((void *) iface->ubus.name);
209 }