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