diff options
author | Max <msuraev@sysmocom.de> | 2017-02-24 13:59:14 +0100 |
---|---|---|
committer | Max <msuraev@sysmocom.de> | 2017-03-01 16:37:59 +0000 |
commit | 70c7d4160dfb9b710e4bf93d3d6265dce8b0392f (patch) | |
tree | c2791ce6eeb6dafa55f79d851e2f21cb0bd4358e /src/ctrl | |
parent | 9756c4691d576c5bfe175245a82d00b2d03ed70e (diff) |
Use value_string for ctrl_type
Use value_string for enum ctrl_type instead of custom code. Add
corresponding unit tests.
Related: OS#1615
Change-Id: Icd4e96dd9f00876cb70b43cfcf42ab4f10311b28
Diffstat (limited to 'src/ctrl')
-rw-r--r-- | src/ctrl/control_cmd.c | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/src/ctrl/control_cmd.c b/src/ctrl/control_cmd.c index 2cf66cf3..1cc8247b 100644 --- a/src/ctrl/control_cmd.c +++ b/src/ctrl/control_cmd.c @@ -33,42 +33,22 @@ #include <osmocom/core/msgb.h> #include <osmocom/core/talloc.h> - +#include <osmocom/core/utils.h> #include <osmocom/vty/command.h> #include <osmocom/vty/vector.h> extern vector ctrl_node_vec; -static struct ctrl_cmd_map ccm[] = { - {"GET", CTRL_TYPE_GET}, - {"SET", CTRL_TYPE_SET}, - {"GET_REPLY", CTRL_TYPE_GET_REPLY}, - {"SET_REPLY", CTRL_TYPE_SET_REPLY}, - {"TRAP", CTRL_TYPE_TRAP}, - {"ERROR", CTRL_TYPE_ERROR}, - {NULL} +const struct value_string ctrl_type_vals[] = { + { CTRL_TYPE_GET, "GET" }, + { CTRL_TYPE_SET, "SET" }, + { CTRL_TYPE_GET_REPLY, "GET_REPLY" }, + { CTRL_TYPE_SET_REPLY, "SET_REPLY" }, + { CTRL_TYPE_TRAP, "TRAP" }, + { CTRL_TYPE_ERROR, "ERROR" }, + { CTRL_TYPE_UNKNOWN, NULL } }; -static int ctrl_cmd_str2type(char *s) -{ - int i; - for (i=0; ccm[i].cmd != NULL; i++) { - if (strcasecmp(s, ccm[i].cmd) == 0) - return ccm[i].type; - } - return CTRL_TYPE_UNKNOWN; -} - -static char *ctrl_cmd_type2str(int type) -{ - int i; - for (i=0; ccm[i].cmd != NULL; i++) { - if (ccm[i].type == type) - return ccm[i].cmd; - } - return NULL; -} - /* Functions from libosmocom */ extern vector cmd_make_descvec(const char *string, const char *descstr); @@ -308,7 +288,7 @@ struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg) goto err; } - cmd->type = ctrl_cmd_str2type(tmp); + cmd->type = get_string_value(ctrl_type_vals, tmp); if (cmd->type == CTRL_TYPE_UNKNOWN) { cmd->type = CTRL_TYPE_ERROR; cmd->id = "err"; @@ -403,7 +383,8 @@ err: struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) { struct msgb *msg; - char *type, *tmp; + const char *type; + char *tmp; if (!cmd->id) return NULL; @@ -412,7 +393,7 @@ struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd) if (!msg) return NULL; - type = ctrl_cmd_type2str(cmd->type); + type = get_value_string(ctrl_type_vals, cmd->type); switch (cmd->type) { case CTRL_TYPE_GET: |