2012-10-24 14:37:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011-2012 Felix Fietkau <nbd@openwrt.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License version 2.1
|
|
|
|
* as published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libubus.h"
|
|
|
|
#include "libubus-internal.h"
|
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
static void
|
|
|
|
ubus_process_unsubscribe(struct ubus_context *ctx, struct ubus_msghdr *hdr,
|
|
|
|
struct ubus_object *obj, struct blob_attr **attrbuf)
|
2012-10-24 14:37:44 +02:00
|
|
|
{
|
2012-12-14 13:42:10 +01:00
|
|
|
struct ubus_subscriber *s;
|
2012-10-24 14:37:44 +02:00
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
if (!obj || !attrbuf[UBUS_ATTR_TARGET])
|
|
|
|
return;
|
2012-10-24 14:37:44 +02:00
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
if (obj->methods != &watch_method)
|
2012-10-24 14:37:44 +02:00
|
|
|
return;
|
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
s = container_of(obj, struct ubus_subscriber, obj);
|
2013-08-29 11:05:51 +02:00
|
|
|
if (s->remove_cb)
|
|
|
|
s->remove_cb(ctx, s, blob_get_u32(attrbuf[UBUS_ATTR_TARGET]));
|
2012-12-14 13:42:10 +01:00
|
|
|
}
|
2012-10-24 14:37:44 +02:00
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
static void
|
|
|
|
ubus_process_notify(struct ubus_context *ctx, struct ubus_msghdr *hdr,
|
|
|
|
struct ubus_object *obj, struct blob_attr **attrbuf)
|
|
|
|
{
|
|
|
|
if (!obj || !attrbuf[UBUS_ATTR_ACTIVE])
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->has_subscribers = blob_get_u8(attrbuf[UBUS_ATTR_ACTIVE]);
|
|
|
|
if (obj->subscribe_cb)
|
|
|
|
obj->subscribe_cb(ctx, obj);
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
ubus_process_invoke(struct ubus_context *ctx, struct ubus_msghdr *hdr,
|
|
|
|
struct ubus_object *obj, struct blob_attr **attrbuf)
|
|
|
|
{
|
2014-02-18 15:02:48 +01:00
|
|
|
struct ubus_request_data req = {
|
|
|
|
.fd = -1,
|
|
|
|
};
|
2012-12-14 13:42:10 +01:00
|
|
|
int method;
|
|
|
|
int ret;
|
2012-12-14 20:05:34 +01:00
|
|
|
bool no_reply = false;
|
2012-10-24 14:37:44 +02:00
|
|
|
|
|
|
|
if (!obj) {
|
|
|
|
ret = UBUS_STATUS_NOT_FOUND;
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
if (!attrbuf[UBUS_ATTR_METHOD]) {
|
|
|
|
ret = UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
2012-12-14 20:05:34 +01:00
|
|
|
if (attrbuf[UBUS_ATTR_NO_REPLY])
|
|
|
|
no_reply = blob_get_int8(attrbuf[UBUS_ATTR_NO_REPLY]);
|
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
req.peer = hdr->peer;
|
|
|
|
req.seq = hdr->seq;
|
|
|
|
req.object = obj->id;
|
|
|
|
|
2012-10-24 14:37:44 +02:00
|
|
|
for (method = 0; method < obj->n_methods; method++)
|
|
|
|
if (!obj->methods[method].name ||
|
|
|
|
!strcmp(obj->methods[method].name,
|
|
|
|
blob_data(attrbuf[UBUS_ATTR_METHOD])))
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
ret = UBUS_STATUS_METHOD_NOT_FOUND;
|
|
|
|
goto send;
|
|
|
|
|
|
|
|
found:
|
|
|
|
ret = obj->methods[method].handler(ctx, obj, &req,
|
|
|
|
blob_data(attrbuf[UBUS_ATTR_METHOD]),
|
|
|
|
attrbuf[UBUS_ATTR_DATA]);
|
2012-12-14 20:05:34 +01:00
|
|
|
if (req.deferred || no_reply)
|
2012-10-24 14:37:44 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
send:
|
|
|
|
ubus_complete_deferred_request(ctx, &req, ret);
|
|
|
|
}
|
|
|
|
|
2014-09-15 15:29:29 +02:00
|
|
|
void __hidden ubus_process_obj_msg(struct ubus_context *ctx, struct ubus_msghdr_buf *buf)
|
2012-12-14 13:42:10 +01:00
|
|
|
{
|
|
|
|
void (*cb)(struct ubus_context *, struct ubus_msghdr *,
|
|
|
|
struct ubus_object *, struct blob_attr **);
|
2014-09-15 15:29:29 +02:00
|
|
|
struct ubus_msghdr *hdr = &buf->hdr;
|
2012-12-14 13:42:10 +01:00
|
|
|
struct blob_attr **attrbuf;
|
|
|
|
struct ubus_object *obj;
|
|
|
|
uint32_t objid;
|
2014-09-15 15:40:05 +02:00
|
|
|
void *prev_data = NULL;
|
2012-12-14 13:42:10 +01:00
|
|
|
|
2014-09-15 15:29:29 +02:00
|
|
|
attrbuf = ubus_parse_msg(buf->data);
|
2012-12-14 13:42:10 +01:00
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
|
|
|
objid = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
|
|
|
|
obj = avl_find_element(&ctx->objects, &objid, obj, avl);
|
|
|
|
|
|
|
|
switch (hdr->type) {
|
|
|
|
case UBUS_MSG_INVOKE:
|
|
|
|
cb = ubus_process_invoke;
|
|
|
|
break;
|
|
|
|
case UBUS_MSG_UNSUBSCRIBE:
|
|
|
|
cb = ubus_process_unsubscribe;
|
|
|
|
break;
|
|
|
|
case UBUS_MSG_NOTIFY:
|
|
|
|
cb = ubus_process_notify;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-09-15 15:40:05 +02:00
|
|
|
|
|
|
|
if (buf == &ctx->msgbuf) {
|
|
|
|
prev_data = buf->data;
|
|
|
|
buf->data = NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-14 13:42:10 +01:00
|
|
|
cb(ctx, hdr, obj, attrbuf);
|
2014-09-15 15:40:05 +02:00
|
|
|
|
|
|
|
if (prev_data) {
|
|
|
|
if (buf->data)
|
|
|
|
free(prev_data);
|
|
|
|
else
|
|
|
|
buf->data = prev_data;
|
|
|
|
}
|
2012-12-14 13:42:10 +01:00
|
|
|
}
|
|
|
|
|
2012-10-24 14:37:44 +02:00
|
|
|
static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_object *obj = req->priv;
|
|
|
|
struct blob_attr **attrbuf = ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJID]);
|
|
|
|
|
|
|
|
if (attrbuf[UBUS_ATTR_OBJTYPE])
|
|
|
|
obj->type->id = blob_get_u32(attrbuf[UBUS_ATTR_OBJTYPE]);
|
|
|
|
|
|
|
|
obj->avl.key = &obj->id;
|
|
|
|
avl_insert(&req->ctx->objects, &obj->avl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_push_method_data(const struct ubus_method *m)
|
|
|
|
{
|
|
|
|
void *mtbl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mtbl = blobmsg_open_table(&b, m->name);
|
|
|
|
|
2014-04-27 00:40:23 +02:00
|
|
|
for (i = 0; i < m->n_policy; i++) {
|
|
|
|
if (m->mask && !(m->mask & (1 << i)))
|
|
|
|
continue;
|
|
|
|
|
2012-10-24 14:37:44 +02:00
|
|
|
blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
|
2014-04-27 00:40:23 +02:00
|
|
|
}
|
2012-10-24 14:37:44 +02:00
|
|
|
|
|
|
|
blobmsg_close_table(&b, mtbl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ubus_push_object_type(const struct ubus_object_type *type)
|
|
|
|
{
|
|
|
|
void *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
|
|
|
|
|
|
|
|
for (i = 0; i < type->n_methods; i++)
|
|
|
|
ubus_push_method_data(&type->methods[i]);
|
|
|
|
|
|
|
|
blob_nest_end(&b, s);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_add_object(struct ubus_context *ctx, struct ubus_object *obj)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
|
|
|
|
if (obj->name && obj->type) {
|
|
|
|
blob_put_string(&b, UBUS_ATTR_OBJPATH, obj->name);
|
|
|
|
|
|
|
|
if (obj->type->id)
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJTYPE, obj->type->id);
|
|
|
|
else if (!ubus_push_object_type(obj->type))
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_ADD_OBJECT, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
req.raw_data_cb = ubus_add_object_cb;
|
|
|
|
req.priv = obj;
|
|
|
|
ret = ubus_complete_request(ctx, &req, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!obj->id)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ubus_remove_object_cb(struct ubus_request *req, int type, struct blob_attr *msg)
|
|
|
|
{
|
|
|
|
struct ubus_object *obj = req->priv;
|
|
|
|
struct blob_attr **attrbuf = ubus_parse_msg(msg);
|
|
|
|
|
|
|
|
if (!attrbuf[UBUS_ATTR_OBJID])
|
|
|
|
return;
|
|
|
|
|
|
|
|
obj->id = 0;
|
|
|
|
|
|
|
|
if (attrbuf[UBUS_ATTR_OBJTYPE] && obj->type)
|
|
|
|
obj->type->id = 0;
|
|
|
|
|
|
|
|
avl_delete(&req->ctx->objects, &obj->avl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ubus_remove_object(struct ubus_context *ctx, struct ubus_object *obj)
|
|
|
|
{
|
|
|
|
struct ubus_request req;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
blob_buf_init(&b, 0);
|
|
|
|
blob_put_int32(&b, UBUS_ATTR_OBJID, obj->id);
|
|
|
|
|
|
|
|
if (ubus_start_request(ctx, &req, b.head, UBUS_MSG_REMOVE_OBJECT, 0) < 0)
|
|
|
|
return UBUS_STATUS_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
req.raw_data_cb = ubus_remove_object_cb;
|
|
|
|
req.priv = obj;
|
|
|
|
ret = ubus_complete_request(ctx, &req, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (obj->id)
|
|
|
|
return UBUS_STATUS_NO_DATA;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|