simplify object signatures by reusing the parser policy to define them
This commit is contained in:
parent
7cd569acc8
commit
e49b34f912
3 changed files with 36 additions and 82 deletions
58
libubus.c
58
libubus.c
|
@ -615,59 +615,29 @@ static void ubus_add_object_cb(struct ubus_request *req, int type, struct blob_a
|
|||
avl_insert(&req->ctx->objects, &obj->avl);
|
||||
}
|
||||
|
||||
static bool ubus_push_table_data(const struct ubus_signature **sig, int *rem, bool array)
|
||||
static void ubus_push_method_data(const struct ubus_method *m)
|
||||
{
|
||||
const struct ubus_signature *cur;
|
||||
bool nest_type;
|
||||
void *nest;
|
||||
void *mtbl;
|
||||
int i;
|
||||
|
||||
while (rem) {
|
||||
cur = (*sig)++;
|
||||
(*rem)--;
|
||||
switch(cur->type) {
|
||||
case UBUS_SIGNATURE_END:
|
||||
return !array;
|
||||
case BLOBMSG_TYPE_INT32:
|
||||
case BLOBMSG_TYPE_STRING:
|
||||
blobmsg_add_u32(&b, cur->name, cur->type);
|
||||
break;
|
||||
case BLOBMSG_TYPE_TABLE:
|
||||
case BLOBMSG_TYPE_ARRAY:
|
||||
nest_type = cur->type == BLOBMSG_TYPE_ARRAY;
|
||||
nest = blobmsg_open_nested(&b, cur->name, nest_type);
|
||||
if (!ubus_push_table_data(sig, rem, nest_type))
|
||||
return false;
|
||||
blobmsg_close_table(&b, nest);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
if (array)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
mtbl = blobmsg_open_table(&b, m->name);
|
||||
|
||||
for (i = 0; i < m->n_policy; i++)
|
||||
blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
|
||||
|
||||
blobmsg_close_table(&b, mtbl);
|
||||
}
|
||||
|
||||
static bool ubus_push_object_type(struct ubus_object_type *type)
|
||||
static bool ubus_push_object_type(const struct ubus_object_type *type)
|
||||
{
|
||||
void *s, *m;
|
||||
int rem = type->n_signature;
|
||||
const struct ubus_signature *sig = type->signature;
|
||||
void *s;
|
||||
int i;
|
||||
|
||||
s = blob_nest_start(&b, UBUS_ATTR_SIGNATURE);
|
||||
while (rem) {
|
||||
if (sig->type != UBUS_SIGNATURE_METHOD)
|
||||
return false;
|
||||
|
||||
m = blobmsg_open_table(&b, sig->name);
|
||||
for (i = 0; i < type->n_methods; i++)
|
||||
ubus_push_method_data(&type->methods[i]);
|
||||
|
||||
sig++;
|
||||
rem--;
|
||||
if (!ubus_push_table_data(&sig, &rem, false))
|
||||
return false;
|
||||
|
||||
blobmsg_close_table(&b, m);
|
||||
}
|
||||
blob_nest_end(&b, s);
|
||||
|
||||
return true;
|
||||
|
|
43
libubus.h
43
libubus.h
|
@ -26,43 +26,36 @@ typedef void (*ubus_data_handler_t)(struct ubus_request *req,
|
|||
int type, struct blob_attr *msg);
|
||||
typedef void (*ubus_complete_handler_t)(struct ubus_request *req, int ret);
|
||||
|
||||
|
||||
#define UBUS_SIGNATURE(_type, _name) { .type = _type, .name = _name }
|
||||
|
||||
#define UBUS_METHOD_START(_name) UBUS_SIGNATURE(UBUS_SIGNATURE_METHOD, _name)
|
||||
#define UBUS_METHOD_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
|
||||
|
||||
#define UBUS_FIELD(_type, _name) UBUS_SIGNATURE(BLOBMSG_TYPE_ ## _type, _name)
|
||||
|
||||
#define UBUS_ARRAY(_name) UBUS_FIELD(ARRAY, _name)
|
||||
#define UBUS_ARRAY_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
|
||||
|
||||
#define UBUS_TABLE_START(_name) UBUS_FIELD(TABLE, _name)
|
||||
#define UBUS_TABLE_END() UBUS_SIGNATURE(UBUS_SIGNATURE_END, NULL)
|
||||
|
||||
#define UBUS_OBJECT_TYPE(_name, _signature) \
|
||||
#define UBUS_OBJECT_TYPE(_name, _methods) \
|
||||
{ \
|
||||
.name = _name, \
|
||||
.id = 0, \
|
||||
.n_signature = ARRAY_SIZE(_signature), \
|
||||
.signature = _signature \
|
||||
.n_methods = ARRAY_SIZE(_methods), \
|
||||
.methods = _methods \
|
||||
}
|
||||
|
||||
struct ubus_signature {
|
||||
int type;
|
||||
#define UBUS_METHOD(_name, _handler, _policy) \
|
||||
{ \
|
||||
.name = _name, \
|
||||
.handler = _handler, \
|
||||
.policy = _policy, \
|
||||
.n_policy = ARRAY_SIZE(_policy) \
|
||||
}
|
||||
|
||||
struct ubus_method {
|
||||
const char *name;
|
||||
ubus_handler_t handler;
|
||||
|
||||
const struct blobmsg_policy *policy;
|
||||
int n_policy;
|
||||
};
|
||||
|
||||
struct ubus_object_type {
|
||||
const char *name;
|
||||
uint32_t id;
|
||||
int n_signature;
|
||||
const struct ubus_signature *signature;
|
||||
};
|
||||
|
||||
struct ubus_method {
|
||||
const char *name;
|
||||
ubus_handler_t handler;
|
||||
const struct ubus_method *methods;
|
||||
int n_methods;
|
||||
};
|
||||
|
||||
struct ubus_object {
|
||||
|
|
|
@ -5,18 +5,6 @@
|
|||
static struct ubus_context *ctx;
|
||||
struct blob_buf b;
|
||||
|
||||
static const struct ubus_signature test_object_sig[] = {
|
||||
UBUS_METHOD_START("hello"),
|
||||
UBUS_TABLE_START(NULL),
|
||||
UBUS_FIELD(INT32, "id"),
|
||||
UBUS_FIELD(STRING, "msg"),
|
||||
UBUS_TABLE_END(),
|
||||
UBUS_METHOD_END(),
|
||||
};
|
||||
|
||||
static struct ubus_object_type test_object_type =
|
||||
UBUS_OBJECT_TYPE("test", test_object_sig);
|
||||
|
||||
enum {
|
||||
HELLO_ID,
|
||||
HELLO_MSG,
|
||||
|
@ -50,9 +38,12 @@ static int test_hello(struct ubus_context *ctx, struct ubus_object *obj,
|
|||
}
|
||||
|
||||
static const struct ubus_method test_methods[] = {
|
||||
{ .name = "hello", .handler = test_hello },
|
||||
UBUS_METHOD("hello", test_hello, hello_policy),
|
||||
};
|
||||
|
||||
static struct ubus_object_type test_object_type =
|
||||
UBUS_OBJECT_TYPE("test", test_methods);
|
||||
|
||||
static struct ubus_object test_object = {
|
||||
.name = "test",
|
||||
.type = &test_object_type,
|
||||
|
|
Loading…
Reference in a new issue