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]));
39 ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
40 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
42 if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
45 obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
46 if (obj->subscribe_cb)
47 obj->subscribe_cb(ctx, obj);
53 ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
54 struct ubus_object *obj, struct blob_attr **attrbuf, int fd)
56 struct ubus_request_data req = {
63 bool no_reply = false;
66 ret = UBUS_STATUS_NOT_FOUND;
70 if (!attrbuf[UBUS_ATTR_METHOD]) {
71 ret = UBUS_STATUS_INVALID_ARGUMENT;
75 if (attrbuf[UBUS_ATTR_NO_REPLY])
76 no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
81 if (attrbuf[UBUS_ATTR_USER] && attrbuf[UBUS_ATTR_GROUP]) {
82 req.acl.user = blobmsg_get_string(attrbuf[UBUS_ATTR_USER]);
83 req.acl.group = blobmsg_get_string(attrbuf[UBUS_ATTR_GROUP]);
84 req.acl.object = obj->name;
86 for (method = 0; method < obj->n_methods; method++)
87 if (!obj->methods[method].name ||
88 !strcmp(obj->methods[method].name,
89 blob_data(attrbuf[UBUS_ATTR_METHOD])))
93 ret = UBUS_STATUS_METHOD_NOT_FOUND;
97 ret = obj->methods[method].handler(ctx, obj, &req,
98 blob_data(attrbuf[UBUS_ATTR_METHOD]),
99 attrbuf[UBUS_ATTR_DATA]);
102 if (req.deferred || no_reply)
106 ubus_complete_deferred_request(ctx, &req, ret);
110 void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf, int fd)
112 void (*cb)(struct ubus_context *, struct ubus_msghdr *,
113 struct ubus_object *, struct blob_attr **, int fd);
114 struct ubus_msghdr *hdr = &buf->hdr;
115 struct blob_attr **attrbuf;
116 struct ubus_object *obj;
118 void *prev_data = NULL;
119 attrbuf = ubus_parse_msg(buf->data);
120 if (!attrbuf[UBUS_ATTR_OBJID])
123 objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
124 obj = avl_find_element(&ctx->objects, &objid, obj, avl);
127 case UBUS_MSG_INVOKE:
128 cb = ubus_process_invoke;
130 case UBUS_MSG_UNSUBSCRIBE:
131 cb = ubus_process_unsubscribe;
133 case UBUS_MSG_NOTIFY:
134 cb = ubus_process_notify;
140 if (buf == &ctx->msgbuf) {
141 prev_data = buf->data;
145 cb(ctx, hdr, obj, attrbuf, fd);
151 buf->data = prev_data;
155 static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
157 struct ubus_object *obj = req->priv;
158 struct blob_attr **attrbuf = ubus_parse_msg(msg);
160 if (!attrbuf[UBUS_ATTR_OBJID])
163 obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
165 if (attrbuf[UBUS_ATTR_OBJTYPE])
166 obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
168 obj->avl.key = &obj->id;
169 avl_insert(&req->ctx->objects, &obj->avl);
172 static void ubus_push_method_data(const struct ubus_method *m)
177 mtbl = blobmsg_open_table(&b, m->name);
179 for (i = 0; i < m->n_policy; i++) {
180 if (m->mask && !(m->mask & (1 << i)))
183 blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
186 blobmsg_close_table(&b, mtbl);
189 static bool ubus_push_object_type(const struct ubus_object_type *type)
194 s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
196 for (i = 0; i < type->n_methods; i++)
197 ubus_push_method_data(&type->methods[i]);
199 blob_nest_end(&b, s);
204 int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
206 struct ubus_request req;
209 blob_buf_init(&b, 0);
211 if (obj->name && obj->type) {
212 blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
215 blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
216 else if (!ubus_push_object_type(obj->type))
217 return UBUS_STATUS_INVALID_ARGUMENT;
220 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
221 return UBUS_STATUS_INVALID_ARGUMENT;
223 req.raw_data_cb = ubus_add_object_cb;
225 ret = ubus_complete_request(ctx, &req, 0);
230 return UBUS_STATUS_NO_DATA;
235 static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
237 struct ubus_object *obj = req->priv;
238 struct blob_attr **attrbuf = ubus_parse_msg(msg);
240 if (!attrbuf[UBUS_ATTR_OBJID])
243 avl_delete(&req->ctx->objects, &obj->avl);
247 if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
251 int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
253 struct ubus_request req;
256 blob_buf_init(&b, 0);
257 blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
259 if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
260 return UBUS_STATUS_INVALID_ARGUMENT;
262 req.raw_data_cb = ubus_remove_object_cb;
264 ret = ubus_complete_request(ctx, &req, 0);
269 return UBUS_STATUS_NO_DATA;