aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Willmann <daniel@totalueberwachung.de>2011-05-05 17:01:09 +0200
committerHarald Welte <laforge@gnumonks.org>2014-08-21 15:34:12 +0200
commitfcf010f7215cbd33b68132023ab756a309265a55 (patch)
tree4d5d9516f7610419ce69aa3427da626d76f1dab5
parent1264cb404f460576690f52fdc8032b74de0bc04e (diff)
libctrl: Add macros to help define commands
-rw-r--r--openbsc/include/openbsc/control_cmd.h69
1 files changed, 69 insertions, 0 deletions
diff --git a/openbsc/include/openbsc/control_cmd.h b/openbsc/include/openbsc/control_cmd.h
index 8d543ee8..c78b3dc1 100644
--- a/openbsc/include/openbsc/control_cmd.h
+++ b/openbsc/include/openbsc/control_cmd.h
@@ -78,4 +78,73 @@ int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd);
struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg);
struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd);
+#define CTRL_CMD_DEFINE_RANGE(cmdname, cmdstr, dtype, element, min, max) \
+int get_##cmdname(struct ctrl_cmd *cmd, void *data) \
+{ \
+ dtype *node = data; \
+ cmd->reply = talloc_asprintf(cmd, "%i", node->element); \
+ if (!cmd->reply) { \
+ cmd->reply = "OOM"; \
+ return CTRL_CMD_ERROR; \
+ } \
+ return CTRL_CMD_REPLY; \
+} \
+int set_##cmdname(struct ctrl_cmd *cmd, void *data) \
+{ \
+ dtype *node = data; \
+ int tmp = atoi(cmd->value); \
+ node->element = tmp; \
+ return get_##cmdname(cmd, data); \
+} \
+int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data) \
+{ \
+ int tmp = atoi(value); \
+ if ((tmp >= min)&&(tmp <= max)) { \
+ return 0; \
+ } \
+ return -1; \
+} \
+struct ctrl_cmd_element cmd_##cmdname = { \
+ .name = cmdstr, \
+ .param = NULL, \
+ .get = &get_##cmdname, \
+ .set = &set_##cmdname, \
+ .verify = &verify_##cmdname, \
+}
+
+#define CTRL_CMD_DEFINE_STRING(cmdname, cmdstr, dtype, element) \
+int get_##cmdname(struct ctrl_cmd *cmd, dtype *data) \
+{ \
+ cmd->reply = talloc_asprintf(cmd, "%s", data->element); \
+ if (!cmd->reply) { \
+ cmd->reply = "OOM"; \
+ return CTRL_CMD_ERROR; \
+ } \
+ return CTRL_CMD_REPLY; \
+} \
+int set_##cmdname(struct ctrl_cmd *cmd, dtype *data) \
+{ \
+ bsc_replace_string(cmd->node, &data->element, cmd->value); \
+ return get_##cmdname(cmd, data); \
+} \
+struct ctrl_cmd_element cmd_##cmdname = { \
+ .name = cmdstr, \
+ .param = NULL, \
+ .get = &get_##cmdname, \
+ .set = &set_##cmdname, \
+ .verify = NULL, \
+}
+
+#define CTRL_CMD_DEFINE(cmdname, cmdstr) \
+int get_##cmdname(struct ctrl_cmd *cmd, void *data); \
+int set_##cmdname(struct ctrl_cmd *cmd, void *data); \
+int verify_##cmdname(struct ctrl_cmd *cmd, const char *value, void *data); \
+struct ctrl_cmd_element cmd_##cmdname = { \
+ .name = cmdstr, \
+ .param = NULL, \
+ .get = &get_##cmdname, \
+ .set = &set_##cmdname, \
+ .verify = &verify_##cmdname, \
+}
+
#endif /* _CONTROL_CMD_H */