4 static struct blob_buf b;
5 static struct ubus_msg_buf *retmsg;
6 static int *retmsg_data;
8 static struct blob_attr *attrbuf[UBUS_ATTR_MAX];
10 typedef int (*ubus_cmd_cb)(struct ubus_client *cl, struct ubus_msg_buf *ub);
12 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
13 [UBUS_ATTR_SIGNATURE] = { .type = BLOB_ATTR_NESTED },
14 [UBUS_ATTR_OBJTYPE] = { .type = BLOB_ATTR_INT32 },
15 [UBUS_ATTR_OBJPATH] = { .type = BLOB_ATTR_STRING },
16 [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
17 [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
20 struct blob_attr **ubus_parse_msg(struct blob_attr *msg)
22 blob_parse(msg, attrbuf, ubus_policy, UBUS_ATTR_MAX);
26 static void ubus_msg_init(struct ubus_msg_buf *ub, uint8_t type, uint16_t seq, uint32_t peer)
34 static struct ubus_msg_buf *ubus_msg_from_blob(bool shared)
36 return ubus_msg_new(b.head, blob_raw_len(b.head), shared);
39 static struct ubus_msg_buf *ubus_reply_from_blob(struct ubus_msg_buf *ub, bool shared)
41 struct ubus_msg_buf *new;
43 new = ubus_msg_new(b.head, blob_raw_len(b.head), shared);
47 ubus_msg_init(new, UBUS_MSG_DATA, ub->hdr.seq, ub->hdr.peer);
51 bool ubusd_send_hello(struct ubus_client *cl)
53 struct ubus_msg_buf *ub;
56 ub = ubus_msg_from_blob(true);
60 ubus_msg_init(ub, UBUS_MSG_HELLO, 0, cl->id.id);
61 ubus_msg_send(cl, ub);
65 static int ubusd_send_pong(struct ubus_client *cl, struct ubus_msg_buf *ub)
67 ub->hdr.type = UBUS_MSG_DATA;
68 ubus_msg_send(cl, ubus_msg_ref(ub));
72 static int ubusd_handle_publish(struct ubus_client *cl, struct ubus_msg_buf *ub)
74 struct ubus_object *obj;
75 struct blob_attr **attr;
77 attr = ubus_parse_msg(ub->data);
78 obj = ubusd_create_object(cl, attr);
80 return UBUS_STATUS_INVALID_ARGUMENT;
83 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
84 if (attr[UBUS_ATTR_SIGNATURE])
85 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id.id);
87 ub = ubus_reply_from_blob(ub, true);
89 return UBUS_STATUS_NO_DATA;
91 ubus_msg_send(cl, ub);
95 static void ubusd_send_obj(struct ubus_client *cl, struct ubus_msg_buf *ub, struct ubus_object *obj)
97 struct ubus_method *m;
100 blob_buf_init(&b, 0);
103 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->path.key);
104 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
106 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
107 list_for_each_entry(m, &obj->type->methods, list)
108 blob_put(&b, blob_id(m->data), blob_data(m->data), blob_len(m->data));
109 blob_nest_end(&b, s);
111 ub = ubus_reply_from_blob(ub, true);
115 ubus_msg_send(cl, ub);
118 static int ubusd_handle_lookup(struct ubus_client *cl, struct ubus_msg_buf *ub)
120 struct ubus_object *obj;
121 struct blob_attr **attr;
123 bool wildcard = false;
127 attr = ubus_parse_msg(ub->data);
128 if (!attr[UBUS_ATTR_OBJPATH]) {
129 avl_for_each_element(&path, obj, path)
130 ubusd_send_obj(cl, ub, obj);
134 objpath = blob_data(attr[UBUS_ATTR_OBJPATH]);
135 len = strlen(objpath);
136 if (objpath[len - 1] != '*') {
137 obj = avl_find_element(&path, objpath, obj, path);
139 return UBUS_STATUS_NOT_FOUND;
141 ubusd_send_obj(cl, ub, obj);
148 obj = avl_find_ge_element(&path, objpath, obj, path);
150 return UBUS_STATUS_NOT_FOUND;
152 while (!strncmp(objpath, obj->path.key, len)) {
154 ubusd_send_obj(cl, ub, obj);
155 if (obj == avl_last_element(&path, obj, path))
157 obj = avl_next_element(obj, path);
161 return UBUS_STATUS_NOT_FOUND;
166 static int ubusd_handle_invoke(struct ubus_client *cl, struct ubus_msg_buf *ub)
168 struct ubus_object *obj = NULL;
169 struct blob_attr **attr;
173 attr = ubus_parse_msg(ub->data);
174 if (!attr[UBUS_ATTR_METHOD] || !attr[UBUS_ATTR_OBJID])
175 return UBUS_STATUS_INVALID_ARGUMENT;
177 id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
179 return UBUS_STATUS_NOT_FOUND;
181 obj = container_of(id, struct ubus_object, id);
183 method = blob_data(attr[UBUS_ATTR_METHOD]);
184 blob_buf_init(&b, 0);
185 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id.id);
186 blob_put_string(&b, UBUS_ATTR_METHOD, method);
187 if (attr[UBUS_ATTR_DATA])
188 blob_put(&b, UBUS_ATTR_DATA, blob_data(attr[UBUS_ATTR_DATA]),
189 blob_len(attr[UBUS_ATTR_DATA]));
193 ub = ubus_reply_from_blob(ub, true);
195 return UBUS_STATUS_NO_DATA;
197 ub->hdr.type = UBUS_MSG_INVOKE;
198 ub->hdr.peer = cl->id.id;
199 ubus_msg_send(obj->client, ub);
204 static int ubusd_handle_status(struct ubus_client *cl, struct ubus_msg_buf *ub)
206 struct blob_attr **attr;
207 struct ubus_object *obj;
210 attr = ubus_parse_msg(ub->data);
211 if (!attr[UBUS_ATTR_OBJID] || !attr[UBUS_ATTR_STATUS])
214 id = ubus_find_id(&objects, blob_get_int32(attr[UBUS_ATTR_OBJID]));
218 obj = container_of(id, struct ubus_object, id);
219 if (cl != obj->client)
222 cl = ubusd_get_client_by_id(ub->hdr.peer);
226 ub->hdr.peer = blob_get_int32(attr[UBUS_ATTR_OBJID]);
227 ubus_msg_send(cl, ub);
235 static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {
236 [UBUS_MSG_PING] = ubusd_send_pong,
237 [UBUS_MSG_PUBLISH] = ubusd_handle_publish,
238 [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,
239 [UBUS_MSG_INVOKE] = ubusd_handle_invoke,
240 [UBUS_MSG_STATUS] = ubusd_handle_status,
243 void ubusd_receive_message(struct ubus_client *cl, struct ubus_msg_buf *ub)
245 ubus_cmd_cb cb = NULL;
248 retmsg->hdr.seq = ub->hdr.seq;
249 retmsg->hdr.peer = ub->hdr.peer;
251 if (ub->hdr.type < __UBUS_MSG_LAST)
252 cb = handlers[ub->hdr.type];
257 ret = UBUS_STATUS_INVALID_COMMAND;
264 *retmsg_data = htonl(ret);
265 ubus_msg_send(cl, ubus_msg_ref(retmsg));
268 static void __init ubusd_proto_init(void)
270 blob_buf_init(&b, 0);
271 blob_put_int32(&b, UBUS_ATTR_STATUS, 0);
273 retmsg = ubus_msg_from_blob(false);
277 retmsg->hdr.type = UBUS_MSG_STATUS;
278 retmsg_data = blob_data(blob_data(retmsg->data));