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 a1062e0a5a8b247cb849b729867dd585c3d22761..47bdb0a4e758683a4c044b4e56dfa1968b8d4acc 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 60d9bb6cac67849b8a03698dacf53e66afc49122..ad2db0c5b55e9df6821cb3fc5df75de26578e24e 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;
 };