libubus: add support for defining a policy mask
authorFelix Fietkau <nbd@openwrt.org>
Sat, 26 Apr 2014 22:40:23 +0000 (00:40 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sat, 26 Apr 2014 22:40:25 +0000 (00:40 +0200)
This allows sharing a policy array across methods, but masking out
unused entries for individual methods.

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
libubus-obj.c
libubus.h

index a1062e0..47bdb0a 100644 (file)
@@ -146,8 +146,12 @@ static void ubus_push_method_data(const struct ubus_method *m)
 
        mtbl = blobmsg_open_table(&b, m->name);
 
-       for (i = 0; i < m->n_policy; i++)
+       for (i = 0; i < m->n_policy; i++) {
+               if (m->mask && !(m->mask & (1 << i)))
+                       continue;
+
                blobmsg_add_u32(&b, m->policy[i].name, m->policy[i].type);
+       }
 
        blobmsg_close_table(&b, mtbl);
 }
index 60d9bb6..ad2db0c 100644 (file)
--- a/libubus.h
+++ b/libubus.h
@@ -67,24 +67,32 @@ typedef void (*ubus_connect_handler_t)(struct ubus_context *ctx);
                .methods = _methods                     \
        }
 
+#define __UBUS_METHOD_NOARG(_name, _handler)           \
+       .name = _name,                                  \
+       .handler = _handler
+
+#define __UBUS_METHOD(_name, _handler, _policy)                \
+       __UBUS_METHOD_NOARG(_name, _handler),           \
+       .policy = _policy,                              \
+       .n_policy = ARRAY_SIZE(_policy)
+
 #define UBUS_METHOD(_name, _handler, _policy)          \
+       { __UBUS_METHOD(_name, _handler, _policy) }
+
+#define UBUS_METHOD_MASK(_name, _handler, _policy, _mask) \
        {                                               \
-               .name = _name,                          \
-               .handler = _handler,                    \
-               .policy = _policy,                      \
-               .n_policy = ARRAY_SIZE(_policy)         \
+               __UBUS_METHOD(_name, _handler, _policy),\
+               .mask = _mask                           \
        }
 
 #define UBUS_METHOD_NOARG(_name, _handler)             \
-       {                                               \
-               .name = _name,                          \
-               .handler = _handler,                    \
-       }
+       { __UBUS_METHOD_NOARG(_name, _handler) }
 
 struct ubus_method {
        const char *name;
        ubus_handler_t handler;
 
+       unsigned long mask;
        const struct blobmsg_policy *policy;
        int n_policy;
 };