2 * Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
16 #include "libubus-internal.h"
19 ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
20 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
22 struct ubus_subscriber *s;
24 if (!obj || !attrbuf[UBUS_ATTR_TARGET])
27 if (obj->methods != &watch_method)
30 s = container_of(obj, struct ubus_subscriber, obj);
32 s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
38 ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
39 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
41 if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
44 obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
45 if (obj->subscribe_cb)
46 obj->subscribe_cb(ctx, obj);
51 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
52 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
54 struct ubus_request_data req = {
61 bool no_reply = false;
64 ret = UBUS_STATUS_NOT_FOUND;
68 if (!attrbuf[UBUS_ATTR_METHOD]) {
69 ret = UBUS_STATUS_INVALID_ARGUMENT;
73 if (attrbuf[UBUS_ATTR_NO_REPLY])
74 no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
79 if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) {
80 req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]);
81 req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]);
82 req.acl.object = obj->name;
84 for (method = 0; method < obj->n_methods; method++)
85 if (!obj->methods[method].name ||
86 !strcmp(obj->methods[method].name,
87 blob_data(attrbuf[UBUS_ATTR_METHOD])))
91 ret = UBUS_STATUS_METHOD_NOT_FOUND;
95 ret = obj->methods[method].handler(ctx, obj, &req,
96 blob_data(attrbuf[UBUS_ATTR_METHOD]),
97 attrbuf[UBUS_ATTR_DATA]);
100 if (req.deferred || no_reply)
104 ubus_complete_deferred_request(ctx, &req, ret);
108 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
110 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
111 struct ubus_object *, struct blob_attr **, int fd);
112 struct ubus_msghdr *hdr = &buf->hdr;
113 struct blob_attr **attrbuf;
114 struct ubus_object *obj;
116 void *prev_data = NULL;
117 attrbuf = ubus_parse_msg(buf->data);
118 if (!attrbuf[UBUS_ATTR_OBJID])
121 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
122 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
125 case UBUS_MSG_INVOKE:
126 cb = ubus_process_invoke;
128 case UBUS_MSG_UNSUBSCRIBE:
129 cb = ubus_process_unsubscribe;
131 case UBUS_MSG_NOTIFY:
132 cb = ubus_process_notify;
138 if (buf == &ctx->msgbuf) {
139 prev_data = buf->data;
143 cb(ctx, hdr, obj, attrbuf, fd);
149 buf->data = prev_data;
153 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
155 struct ubus_object *obj = req->priv;
156 struct blob_attr **attrbuf = ubus_parse_msg(msg);
158 if (!attrbuf[UBUS_ATTR_OBJID])
161 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
163 if (attrbuf[UBUS_ATTR_OBJTYPE])
164 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
166 obj->avl.key = &obj->id;
167 avl_insert(&req->ctx->objects, &obj->avl);
170 static void ubus_push_method_data(const struct ubus_method *m)
175 mtbl = blobmsg_open_table(&b, m->name);
177 for (i = 0; i < m->n_policy; i++) {
178 if (m->mask && !(m->mask & (1 << i)))
181 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
184 blobmsg_close_table(&b, mtbl);
187 static bool ubus_push_object_type(const struct ubus_object_type *type)
192 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
194 for (i = 0; i < type->n_methods; i++)
195 ubus_push_method_data(&type->methods[i]);
197 blob_nest_end(&b, s);
202 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
204 struct ubus_request req;
207 blob_buf_init(&b, 0);
209 if (obj->name && obj->type) {
210 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
213 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
214 else if (!ubus_push_object_type(obj->type))
215 return UBUS_STATUS_INVALID_ARGUMENT;
218 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
219 return UBUS_STATUS_INVALID_ARGUMENT;
221 req.raw_data_cb = ubus_add_object_cb;
223 ret = ubus_complete_request(ctx, &req, 0);
228 return UBUS_STATUS_NO_DATA;
233 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
235 struct ubus_object *obj = req->priv;
236 struct blob_attr **attrbuf = ubus_parse_msg(msg);
238 if (!attrbuf[UBUS_ATTR_OBJID])
241 avl_delete(&req->ctx->objects, &obj->avl);
245 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
249 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
251 struct ubus_request req;
254 blob_buf_init(&b, 0);
255 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
257 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
258 return UBUS_STATUS_INVALID_ARGUMENT;
260 req.raw_data_cb = ubus_remove_object_cb;
262 ret = ubus_complete_request(ctx, &req, 0);
267 return UBUS_STATUS_NO_DATA;