Skip to content

Commit

Permalink
mc: Code refactor to reduce copy-paste ratio
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
AlexanderAmelkin committed Jun 17, 2018
1 parent e8e94d8 commit 0310208
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
33 changes: 29 additions & 4 deletions include/ipmitool/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* 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;

h = data[1] << 8; /* MSB */
h |= data[0]; /* LSB */
h = ipmi[1] << 8; /* MSB */
h |= ipmi[0]; /* LSB */

return h;
}
Expand All @@ -130,6 +130,31 @@ static inline void htoipmi16(uint16_t h, uint8_t *ipmi)
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_write(file) ipmi_open_file(file, 1)

Expand Down
11 changes: 9 additions & 2 deletions include/ipmitool/ipmi_mc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

#include <ipmitool/ipmi.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_COLD_RESET 0x02
Expand Down Expand Up @@ -85,8 +92,8 @@ struct ipm_devid_rsp {
#define IPM_DEV_IPMI_VERSION_MINOR(x) \
((x & IPM_DEV_IPMI_VER_MINOR_MASK) >> IPM_DEV_IPMI_VER_MINOR_SHIFT)

#define IPM_DEV_MANUFACTURER_ID(x) \
((uint32_t) ((x[2] & 0x0F) << 16 | x[1] << 8 | x[0]))
#define IPM_DEV_MANUFACTURER_ID_MASK 0x0FFFFF
#define IPM_DEV_MANUFACTURER_ID(x) (ipmi24toh(x) & IPM_DEV_MANUFACTURER_ID_MASK)

#define IPM_DEV_ADTL_SUPPORT_BITS (8)

Expand Down
2 changes: 2 additions & 0 deletions include/ipmitool/ipmi_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#include <ipmitool/helper.h>

#define CC_STRING(cc) val2str(cc, completion_code_vals)

extern const struct valstr completion_code_vals[];
extern const struct valstr entity_id_vals[];
extern const struct valstr entity_device_type_vals[];
Expand Down
3 changes: 1 addition & 2 deletions lib/ipmi_ime.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ static int ImeGetInfo(struct ipmi_intf *intf)
{
rc = IME_SUCCESS;
printf("Manufacturer Name : %s\n",
val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
ipmi_oem_info) );
OEM_MFG_STRING(devid->manufacturer_id));

printf("Product ID : %u (0x%02x%02x)\n",
buf2short((uint8_t *)(devid->product_id)),
Expand Down
37 changes: 17 additions & 20 deletions lib/ipmi_mc.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ ipmi_mc_reset(struct ipmi_intf * intf, int cmd)
return (-1);
} else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "MC reset command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return (-1);
}

Expand Down Expand Up @@ -287,7 +287,7 @@ ipmi_mc_get_enables(struct ipmi_intf * intf)
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -337,7 +337,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv)
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -379,7 +379,7 @@ ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv)
}
else if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Set Global Enables command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -429,7 +429,7 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf)
}
if (rsp->ccode > 0) {
lprintf(LOG_ERR, "Get Device ID command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand All @@ -447,16 +447,13 @@ ipmi_mc_get_deviceid(struct ipmi_intf * intf)
printf("Manufacturer ID : %lu\n",
(long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id));
printf("Manufacturer Name : %s\n",
val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
ipmi_oem_info) );
OEM_MFG_STRING(devid->manufacturer_id));

printf("Product ID : %u (0x%02x%02x)\n",
buf2short((uint8_t *)(devid->product_id)),
devid->product_id[1], devid->product_id[0]);

product=oemval2str(IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
(devid->product_id[1]<<8)+devid->product_id[0],
ipmi_oem_product_info);
product = OEM_PROD_STRING(devid->manufacturer_id, devid->product_id);

if (product!=NULL) {
printf("Product Name : %s\n", product);
Expand Down Expand Up @@ -585,7 +582,7 @@ static int ipmi_mc_get_selftest(struct ipmi_intf * intf)

if (rsp->ccode) {
lprintf(LOG_ERR, "Bad response: (%s)",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -738,15 +735,15 @@ ipmi_mc_get_watchdog(struct ipmi_intf * intf)

if (rsp->ccode) {
lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data;

/* Convert 100ms intervals to seconds */
init_cnt = (double)ipmi16toh(wdt_res->init_cnt_le) / 10.0;
pres_cnt = (double)ipmi16toh(wdt_res->pres_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;

printf("Watchdog Timer Use: %s (0x%02x)\n",
wdt_use[IPMI_WDT_GET(wdt_res->use, USE)]->get, wdt_res->use);
Expand Down Expand Up @@ -941,7 +938,7 @@ ipmi_mc_set_watchdog(struct ipmi_intf * intf, int argc, char *argv[])
rc = rsp->ccode;
if (rc) {
lprintf(LOG_ERR, "Set Watchdog Timer command failed: %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
goto out;
}

Expand Down Expand Up @@ -1000,7 +997,7 @@ ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)

if (rsp->ccode) {
lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s",
val2str(rsp->ccode, completion_code_vals));
CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -1035,9 +1032,9 @@ ipmi_mc_rst_watchdog(struct ipmi_intf * intf)

if (rsp->ccode) {
lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s",
(rsp->ccode == IPM_WATCHDOG_RESET_ERROR) ?
"Attempt to reset uninitialized watchdog" :
val2str(rsp->ccode, completion_code_vals));
(rsp->ccode == IPM_WATCHDOG_RESET_ERROR)
? "Attempt to reset uninitialized watchdog"
: CC_STRING(rsp->ccode));
return -1;
}

Expand Down Expand Up @@ -1375,7 +1372,7 @@ ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv, int is_set)
}
else if (rc > 0) {
lprintf(LOG_ERR, "%s command failed: %s", argv[0],
val2str(rc, completion_code_vals));
CC_STRING(rc));
}
return rc;
}

0 comments on commit 0310208

Please sign in to comment.