From 9ecc7e079194a7a3cd8645bfd8e490a4185c9be6 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Mon, 29 Jan 2024 10:59:04 +0100 Subject: [PATCH] Refactor enum2int API --- apps/snmp/snmp_handler.c | 19 +------------ apps/snmp/snmp_lib.c | 4 +-- lib/clixon/clixon_xml_map.h | 1 + lib/src/clixon_xml_map.c | 54 +++++++++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 34 deletions(-) diff --git a/apps/snmp/snmp_handler.c b/apps/snmp/snmp_handler.c index d0e9b932..c4a58029 100644 --- a/apps/snmp/snmp_handler.c +++ b/apps/snmp/snmp_handler.c @@ -658,10 +658,7 @@ snmp_table_rowstatus_get(clixon_handle h, cxobj *xt = NULL; char *xpath = NULL; cxobj *xr; - int ret; char *body; - char *intstr; - char *reason = NULL; clixon_debug(CLIXON_DBG_CLIENT, ""); /* Prepare backend call by constructing namespace context */ @@ -673,20 +670,8 @@ snmp_table_rowstatus_get(clixon_handle h, clicon_ptr_get(h, "snmp-rowstatus-tree", (void**)&xt); if (xt && (xr = xpath_first(xt, nsc, "%s", xpath)) != NULL && (body = xml_body(xr)) != NULL) { - if ((ret = yang_enum2valstr(yrestype, body, &intstr)) < 0) + if (yang_enum2int(yrestype, body, rowstatus) < 0) goto done; - if (ret == 0){ - clixon_debug(CLIXON_DBG_CLIENT, "%s invalid or not found", body); - *rowstatus = 0; - } - else { - if ((ret = parse_int32(intstr, rowstatus, &reason)) < 0) - goto done; - if (ret == 0){ - clixon_debug(CLIXON_DBG_CLIENT, "parse_int32: %s", reason); - *rowstatus = 0; - } - } } else *rowstatus = 0; @@ -696,8 +681,6 @@ snmp_table_rowstatus_get(clixon_handle h, free(xpath); if (nsc) xml_nsctx_free(nsc); - if (reason) - free(reason); return retval; } diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index 34f27a33..10ce69b3 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -778,7 +778,7 @@ type_xml2snmp_pre(char *xmlstr0, { int retval = -1; yang_stmt *yrestype; /* resolved type */ - char *restype = NULL; /* resolved type */ + char *restype = NULL; /* resolved type */ char *str = NULL; int ret; cbuf *cb = NULL; @@ -809,7 +809,7 @@ type_xml2snmp_pre(char *xmlstr0, else str = "1"; } - else if( strcmp(restype, "decimal64") == 0 ) { + else if (strcmp(restype, "decimal64") == 0 ) { cg_var* cv = yang_cv_get(ys); int64_t num; diff --git a/lib/clixon/clixon_xml_map.h b/lib/clixon/clixon_xml_map.h index 7d8d2c13..53562765 100644 --- a/lib/clixon/clixon_xml_map.h +++ b/lib/clixon/clixon_xml_map.h @@ -69,6 +69,7 @@ int assign_namespace_body(cxobj *x0, cxobj *x1); int xml_merge(cxobj *x0, cxobj *x1, yang_stmt *yspec, char **reason); int yang_valstr2enum(yang_stmt *ytype, char *valstr, char **enumstr); int yang_enum2valstr(yang_stmt *ytype, char *enumstr, char **valstr); +int yang_enum2int(yang_stmt *ytype, char *enumstr, int32_t *val); int yang_enum_int_value(cxobj *node, int32_t *val); int xml_copy_marked(cxobj *x0, cxobj *x1); int yang_check_when_xpath(cxobj *xn, cxobj *xp, yang_stmt *yn, int *hit, int *nrp, char **xpathp); diff --git a/lib/src/clixon_xml_map.c b/lib/src/clixon_xml_map.c index 4920fa1b..77e261b4 100644 --- a/lib/src/clixon_xml_map.c +++ b/lib/src/clixon_xml_map.c @@ -1511,17 +1511,16 @@ yang_valstr2enum(yang_stmt *ytype, return retval; } -/*! Given a YANG (enum) type node and a value, return the string containing corresponding int str +/*! Given a YANG (enum) type node and an enum name, return the string containing corresponding int str * + * Only handles explicit values * @param[in] ytype YANG type noden * @param[in] enumstr Value of enum * @param[out] valstr Corresponding string containing an int (direct pointer, dont free) * @retval 1 OK, result in valstr * @retval 0 Invalid, not found * @retval -1 Error - * @note does not handle implicit values * @see yang_enum2int handles implicit values - * @note does not handle implicit values, see yang_enum_int_value which does */ int yang_enum2valstr(yang_stmt *ytype, @@ -1550,6 +1549,42 @@ yang_enum2valstr(yang_stmt *ytype, goto done; } +/*! Given a YANG (enum) type node and an enum name, return the corresponding int + * + * Handles implicit values + * @param[in] ytype YANG type noden + * @param[in] enumstr Value of enum + * @param[out] int32 Int value + * @retval 0 OK, result in val + * @retval -1 Error + */ +int +yang_enum2int(yang_stmt *ytype, + char *enumstr, + int32_t *val) +{ + int retval = -1; + yang_stmt *yenum; + cg_var *cv; + + if (val == NULL){ + clixon_err(OE_UNIX, EINVAL, "val is NULL"); + goto done; + } + if ((yenum = yang_find(ytype, Y_ENUM, enumstr)) == NULL){ + clixon_err(OE_YANG, 0, "No such enum %s", enumstr); + goto done; + } + if ((cv = yang_cv_get(yenum)) == NULL){ + clixon_err(OE_YANG, 0, "Enum lacks cv"); + goto done; + } + *val = cv_int32_get(cv); + retval = 0; + done: + return retval; +} + /*! Get integer value from xml node from yang enumeration * * @param[in] node XML node in a tree @@ -1568,13 +1603,11 @@ int yang_enum_int_value(cxobj *node, int32_t *val) { - int retval = -1; + int retval = -1; yang_stmt *yspec; yang_stmt *ys; yang_stmt *ytype; yang_stmt *yrestype; /* resolved type */ - yang_stmt *yenum; - cg_var *cv; if (node == NULL) goto done; @@ -1593,15 +1626,8 @@ yang_enum_int_value(cxobj *node, } if (strcmp(yang_argument_get(yrestype), "enumeration")) goto done; - if ((yenum = yang_find(yrestype, Y_ENUM, xml_body(node))) == NULL){ - clixon_err(OE_YANG, 0, "No such enum: %s", xml_body(node)); + if (yang_enum2int(yrestype, xml_body(node), val) < 0) goto done; - } - if ((cv = yang_cv_get(yenum)) == NULL){ - clixon_err(OE_YANG, 0, "Enum lacks cv"); - goto done; - } - *val = cv_int32_get(cv); retval = 0; done: return retval;