From bb35d370bab07bdd0662853498fd647fe934382d Mon Sep 17 00:00:00 2001 From: Zdenek Styblik Date: Thu, 8 Jan 2015 19:43:27 +0100 Subject: [PATCH] Add eval_ccode() into helper Commit adds eval_ccode() into helper in order to save some code repetition on evaluation of retvals from _ipmi_* functions. --- include/ipmitool/helper.h | 2 ++ lib/helper.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/ipmitool/helper.h b/include/ipmitool/helper.h index 4d0c785..b7ad628 100644 --- a/include/ipmitool/helper.h +++ b/include/ipmitool/helper.h @@ -83,6 +83,8 @@ int str2ushort(const char * str, uint16_t * ushrt_ptr); int str2char(const char * str, int8_t * chr_ptr); int str2uchar(const char * str, uint8_t * uchr_ptr); +int eval_ccode(const int ccode); + int is_fru_id(const char *argv_ptr, uint8_t *fru_id_ptr); int is_ipmi_channel_num(const char *argv_ptr, uint8_t *channel_ptr); int is_ipmi_user_id(const char *argv_ptr, uint8_t *ipmi_uid_ptr); diff --git a/lib/helper.c b/lib/helper.c index 600bfa0..30546f7 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -672,6 +672,37 @@ ipmi_start_daemon(struct ipmi_intf *intf) dup(fd); } +/* eval_ccode - evaluate return value of _ipmi_* functions and print error error + * message, if conditions are met. + * + * @ccode - return value of _ipmi_* function. + * + * returns - 0 if ccode is 0, otherwise (-1) and error might get printed-out. + */ +int +eval_ccode(const int ccode) +{ + if (ccode == 0) { + return 0; + } else if (ccode < 0) { + switch (ccode) { + case (-1): + lprintf(LOG_ERR, "IPMI response is NULL."); + break; + case (-2): + lprintf(LOG_ERR, "Unexpected data length received."); + break; + default: + break; + } + return (-1); + } else { + lprintf(LOG_ERR, "IPMI command failed: %s", + val2str(ccode, completion_code_vals)); + return (-1); + } +} + /* is_fru_id - wrapper for str-2-int FRU ID conversion. Message is printed * on error. * FRU ID range: <0..255>