mc: Code refactor to reduce copy-paste ratio

* Replace with a macro all calls to val2str() for completion codes
* Replace with a macro all calls to val2str() for manufacturer name
* Replace with a macro all calls to val2str() for product name
* Add ipmi24toh() and ipmi32toh() helpers, unify ipmi*toh() interface

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin 2018-06-17 13:34:56 +03:00
parent e8e94d8976
commit 0310208383
No known key found for this signature in database
GPG Key ID: E893587B5B74178D
5 changed files with 58 additions and 28 deletions

View File

@ -113,13 +113,13 @@ uint16_t ipmi_get_oem_id(struct ipmi_intf *intf);
/* le16toh(), hto16le(), et. al. don't exist for Windows or Apple */ /* le16toh(), hto16le(), et. al. don't exist for Windows or Apple */
/* For portability, let's simply define our own versions here */ /* For portability, let's simply define our own versions here */
static inline uint16_t ipmi16toh(uint16_t le) static inline uint16_t ipmi16toh(void *ipmi16)
{ {
uint8_t *data = (uint8_t *)&le; uint8_t *ipmi = (uint8_t *)ipmi16;
uint16_t h; uint16_t h;
h = data[1] << 8; /* MSB */ h = ipmi[1] << 8; /* MSB */
h |= data[0]; /* LSB */ h |= ipmi[0]; /* LSB */
return h; return h;
} }
@ -130,6 +130,31 @@ static inline void htoipmi16(uint16_t h, uint8_t *ipmi)
ipmi[1] = h >> 8; /* MSB */ ipmi[1] = h >> 8; /* MSB */
} }
static inline uint32_t ipmi24toh(void *ipmi24)
{
uint8_t *ipmi = (uint8_t *)ipmi24;
uint32_t h = 0;
h = ipmi[2] << 16;
h |= ipmi[1] << 8;
h |= ipmi[0];
return h;
}
static inline uint32_t ipmi32toh(void *ipmi32)
{
uint8_t *ipmi = (uint8_t *)ipmi32;
uint32_t h;
h = ipmi[3] << 24;
h |= ipmi[2] << 16;
h |= ipmi[1] << 8;
h |= ipmi[0];
return h;
}
#define ipmi_open_file_read(file) ipmi_open_file(file, 0) #define ipmi_open_file_read(file) ipmi_open_file(file, 0)
#define ipmi_open_file_write(file) ipmi_open_file(file, 1) #define ipmi_open_file_write(file) ipmi_open_file(file, 1)

View File

@ -35,6 +35,13 @@
#include <ipmitool/ipmi.h> #include <ipmitool/ipmi.h>
#include <ipmitool/helper.h> #include <ipmitool/helper.h>
#include <ipmitool/ipmi_strings.h>
#define OEM_MFG_STRING(oem) val2str(IPM_DEV_MANUFACTURER_ID(oem),\
ipmi_oem_info)
#define OEM_PROD_STRING(oem, p) oemval2str(IPM_DEV_MANUFACTURER_ID(oem),\
ipmi16toh(p),\
ipmi_oem_product_info)
#define BMC_GET_DEVICE_ID 0x01 #define BMC_GET_DEVICE_ID 0x01
#define BMC_COLD_RESET 0x02 #define BMC_COLD_RESET 0x02
@ -85,8 +92,8 @@ struct ipm_devid_rsp {
#define IPM_DEV_IPMI_VERSION_MINOR(x) \ #define IPM_DEV_IPMI_VERSION_MINOR(x) \
((x & IPM_DEV_IPMI_VER_MINOR_MASK) >> IPM_DEV_IPMI_VER_MINOR_SHIFT) ((x & IPM_DEV_IPMI_VER_MINOR_MASK) >> IPM_DEV_IPMI_VER_MINOR_SHIFT)
#define IPM_DEV_MANUFACTURER_ID(x) \ #define IPM_DEV_MANUFACTURER_ID_MASK 0x0FFFFF
((uint32_t) ((x[2] & 0x0F) << 16 | x[1] << 8 | x[0])) #define IPM_DEV_MANUFACTURER_ID(x) (ipmi24toh(x) & IPM_DEV_MANUFACTURER_ID_MASK)
#define IPM_DEV_ADTL_SUPPORT_BITS (8) #define IPM_DEV_ADTL_SUPPORT_BITS (8)

View File

@ -35,6 +35,8 @@
#include <ipmitool/helper.h> #include <ipmitool/helper.h>
#define CC_STRING(cc) val2str(cc, completion_code_vals)
extern const struct valstr completion_code_vals[]; extern const struct valstr completion_code_vals[];
extern const struct valstr entity_id_vals[]; extern const struct valstr entity_id_vals[];
extern const struct valstr entity_device_type_vals[]; extern const struct valstr entity_device_type_vals[];

View File

@ -240,8 +240,7 @@ static int ImeGetInfo(struct ipmi_intf *intf)
{ {
rc = IME_SUCCESS; rc = IME_SUCCESS;
printf("Manufacturer Name : %s\n", printf("Manufacturer Name : %s\n",
val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), OEM_MFG_STRING(devid->manufacturer_id));
ipmi_oem_info) );
printf("Product ID : %u (0x%02x%02x)\n", printf("Product ID : %u (0x%02x%02x)\n",
buf2short((uint8_t *)(devid->product_id)), buf2short((uint8_t *)(devid->product_id)),

View File

@ -91,7 +91,7 @@ ipmi_mc_reset(struct ipmi_intf * intf, int cmd)
return (-1); return (-1);
} else if (rsp->ccode > 0) { } else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "MC reset command failed: %s", lprintf(LOG_ERR, "MC reset command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return (-1); return (-1);
} }
@ -287,7 +287,7 @@ ipmi_mc_get_enables(struct ipmi_intf * intf)
} }
if (rsp->ccode > 0) { if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Global Enables command failed: %s", lprintf(LOG_ERR, "Get Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -337,7 +337,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv)
} }
if (rsp->ccode > 0) { if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Global Enables command failed: %s", lprintf(LOG_ERR, "Get Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -379,7 +379,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv)
} }
else if (rsp->ccode > 0) { else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Set Global Enables command failed: %s", lprintf(LOG_ERR, "Set Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -429,7 +429,7 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf)
} }
if (rsp->ccode > 0) { if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Device ID command failed: %s", lprintf(LOG_ERR, "Get Device ID command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -447,16 +447,13 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf)
printf("Manufacturer ID : %lu\n", printf("Manufacturer ID : %lu\n",
(long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id)); (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id));
printf("Manufacturer Name : %s\n", printf("Manufacturer Name : %s\n",
val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), OEM_MFG_STRING(devid->manufacturer_id));
ipmi_oem_info) );
printf("Product ID : %u (0x%02x%02x)\n", printf("Product ID : %u (0x%02x%02x)\n",
buf2short((uint8_t *)(devid->product_id)), buf2short((uint8_t *)(devid->product_id)),
devid->product_id[1], devid->product_id[0]); devid->product_id[1], devid->product_id[0]);
product=oemval2str(IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id), product = OEM_PROD_STRING(devid->manufacturer_id, devid->product_id);
(devid->product_id[1]<<8)+devid->product_id[0],
ipmi_oem_product_info);
if (product!=NULL) { if (product!=NULL) {
printf("Product Name : %s\n", product); printf("Product Name : %s\n", product);
@ -585,7 +582,7 @@ static int ipmi_mc_get_selftest(struct ipmi_intf * intf)
if (rsp->ccode) { if (rsp->ccode) {
lprintf(LOG_ERR, "Bad response: (%s)", lprintf(LOG_ERR, "Bad response: (%s)",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -738,15 +735,15 @@ ipmi_mc_get_watchdog(struct ipmi_intf * intf)
if (rsp->ccode) { if (rsp->ccode) {
lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s", lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data; wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data;
/* Convert 100ms intervals to seconds */ /* Convert 100ms intervals to seconds */
init_cnt = (double)ipmi16toh(wdt_res->init_cnt_le) / 10.0; init_cnt = (double)ipmi16toh(&wdt_res->init_cnt_le) / 10.0;
pres_cnt = (double)ipmi16toh(wdt_res->pres_cnt_le) / 10.0; pres_cnt = (double)ipmi16toh(&wdt_res->pres_cnt_le) / 10.0;
printf("Watchdog Timer Use: %s (0x%02x)\n", printf("Watchdog Timer Use: %s (0x%02x)\n",
wdt_use[IPMI_WDT_GET(wdt_res->use, USE)]->get, wdt_res->use); wdt_use[IPMI_WDT_GET(wdt_res->use, USE)]->get, wdt_res->use);
@ -941,7 +938,7 @@ ipmi_mc_set_watchdog(struct ipmi_intf * intf, int argc, char *argv[])
rc = rsp->ccode; rc = rsp->ccode;
if (rc) { if (rc) {
lprintf(LOG_ERR, "Set Watchdog Timer command failed: %s", lprintf(LOG_ERR, "Set Watchdog Timer command failed: %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
goto out; goto out;
} }
@ -1000,7 +997,7 @@ ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)
if (rsp->ccode) { if (rsp->ccode) {
lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s", lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s",
val2str(rsp->ccode, completion_code_vals)); CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -1035,9 +1032,9 @@ ipmi_mc_rst_watchdog(struct ipmi_intf * intf)
if (rsp->ccode) { if (rsp->ccode) {
lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s", lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s",
(rsp->ccode == IPM_WATCHDOG_RESET_ERROR) ? (rsp->ccode == IPM_WATCHDOG_RESET_ERROR)
"Attempt to reset uninitialized watchdog" : ? "Attempt to reset uninitialized watchdog"
val2str(rsp->ccode, completion_code_vals)); : CC_STRING(rsp->ccode));
return -1; return -1;
} }
@ -1375,7 +1372,7 @@ ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv, int is_set)
} }
else if (rc > 0) { else if (rc > 0) {
lprintf(LOG_ERR, "%s command failed: %s", argv[0], lprintf(LOG_ERR, "%s command failed: %s", argv[0],
val2str(rc, completion_code_vals)); CC_STRING(rc));
} }
return rc; return rc;
} }