simplify object signatures by reusing the parser policy to define them
authorFelix Fietkau <nbd@openwrt.org>
Sun, 27 Mar 2011 18:03:18 +0000 (20:03 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 27 Mar 2011 18:03:18 +0000 (20:03 +0200)
libubus.c
libubus.h
ubus-example.c

index d84e970c39022658ec6082658b517697088ac1b7..049aac94f6d88abcfab4641dd830e17dec72aaf4 100644 (file)
--- 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;
index b58a4dc448008993f58e410757e0a0106c1ab384..cead47648cebabf6804a22e28877267257eec98a 100644 (file)
--- 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 {
index 5daa46fa0b95414d83c764384d366fe9cecb09c8..517ad0104d0217c034ad983fa48f70a648e8903a 100644 (file)
@@ -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,