From e49b34f91267449e89d3f482b7c16bbf84bf90fd Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 27 Mar 2011 20:03:18 +0200 Subject: [PATCH] simplify object signatures by reusing the parser policy to define them --- libubus.c | 62 +++++++++++++------------------------------------- libubus.h | 43 +++++++++++++++------------------- ubus-example.c | 17 ++++---------- 3 files changed, 38 insertions(+), 84 deletions(-) diff --git a/libubus.c b/libubus.c index d84e970..049aac9 100644 --- a/libubus.c +++ b/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) -{ - const struct ubus_signature *cur; - bool nest_type; - void *nest; - - 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; +static void ubus_push_method_data(const struct ubus_method *m) +{ + void *mtbl; + int i; + + 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; diff --git a/libubus.h b/libubus.h index b58a4dc..cead476 100644 --- a/libubus.h +++ b/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 \ + } + +#define UBUS_METHOD(_name, _handler, _policy) \ + { \ + .name = _name, \ + .handler = _handler, \ + .policy = _policy, \ + .n_policy = ARRAY_SIZE(_policy) \ } -struct ubus_signature { - int type; +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 { diff --git a/ubus-example.c b/ubus-example.c index 5daa46f..517ad01 100644 --- a/ubus-example.c +++ b/ubus-example.c @@ -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, -- 2.25.1