From 6f336d04f14b420822902dca7ef595cd49847d21 Mon Sep 17 00:00:00 2001 From: "eyjhbb@gmail.com" Date: Mon, 24 Sep 2018 15:41:00 +0200 Subject: [PATCH] sensor: Refactor ipmi_sensor_print_fc_threshold() Break the function into smaller pieces to reduce complexity. --- lib/ipmi_sensor.c | 333 +++++++++++++++++++++++++--------------------- 1 file changed, 182 insertions(+), 151 deletions(-) diff --git a/lib/ipmi_sensor.c b/lib/ipmi_sensor.c index 762bf39..81f0c7b 100644 --- a/lib/ipmi_sensor.c +++ b/lib/ipmi_sensor.c @@ -260,6 +260,184 @@ print_thresh_setting(struct sdr_record_full_sensor *full, } } +static void +dump_sensor_fc_thredshold_csv( + int thresh_available, + const char *thresh_status, + struct ipmi_rs *rsp, + struct sensor_reading *sr) +{ + printf("%s", sr->s_id); + if (sr->s_reading_valid) { + if (sr->s_has_analog_value) + printf(",%.3f,%s,%s", + sr->s_a_val, sr->s_a_units, thresh_status); + else + printf(",0x%x,%s,%s", + sr->s_reading, sr->s_a_units, thresh_status); + } else { + printf(",%s,%s,%s", + "na", sr->s_a_units, "na"); + } + if (thresh_available && sr->full) { +#define PTS(bit, dataidx) { \ +print_thresh_setting(sr->full, rsp->data[0] & (bit), \ +rsp->data[(dataidx)], ",", "%.3f", "0x%x", "%s"); \ +} + PTS(LOWER_NON_RECOV_SPECIFIED, 3); + PTS(LOWER_CRIT_SPECIFIED, 2); + PTS(LOWER_NON_CRIT_SPECIFIED, 1); + PTS(UPPER_NON_CRIT_SPECIFIED, 4); + PTS(UPPER_CRIT_SPECIFIED, 5); + PTS(UPPER_NON_RECOV_SPECIFIED, 6); +#undef PTS + } else { + printf(",%s,%s,%s,%s,%s,%s", + "na", "na", "na", "na", "na", "na"); + } + printf("\n"); +} + +/* output format + * id value units status thresholds.... + */ +static void +dump_sensor_fc_thredshold( + int thresh_available, + const char *thresh_status, + struct ipmi_rs *rsp, + struct sensor_reading *sr) +{ + printf("%-16s ", sr->s_id); + if (sr->s_reading_valid) { + if (sr->s_has_analog_value) + printf("| %-10.3f | %-10s | %-6s", + sr->s_a_val, sr->s_a_units, thresh_status); + else + printf("| 0x%-8x | %-10s | %-6s", + sr->s_reading, sr->s_a_units, thresh_status); + } else { + printf("| %-10s | %-10s | %-6s", + "na", sr->s_a_units, "na"); + } + if (thresh_available && sr->full) { +#define PTS(bit, dataidx) { \ +print_thresh_setting(sr->full, rsp->data[0] & (bit), \ +rsp->data[(dataidx)], "| ", "%-10.3f", "0x%-8x", "%-10s"); \ +} + PTS(LOWER_NON_RECOV_SPECIFIED, 3); + PTS(LOWER_CRIT_SPECIFIED, 2); + PTS(LOWER_NON_CRIT_SPECIFIED, 1); + PTS(UPPER_NON_CRIT_SPECIFIED, 4); + PTS(UPPER_CRIT_SPECIFIED, 5); + PTS(UPPER_NON_RECOV_SPECIFIED, 6); +#undef PTS + } else { + printf("| %-10s| %-10s| %-10s| %-10s| %-10s| %-10s", + "na", "na", "na", "na", "na", "na"); + } + + printf("\n"); +} + +static void +dump_sensor_fc_thredshold_verbose( + int thresh_available, + const char *thresh_status, + struct ipmi_intf *intf, + struct sdr_record_common_sensor *sensor, + struct ipmi_rs *rsp, + struct sensor_reading *sr) +{ + printf("Sensor ID : %s (0x%x)\n", + sr->s_id, sensor->keys.sensor_num); + + printf(" Entity ID : %d.%d\n", + sensor->entity.id, sensor->entity.instance); + + printf(" Sensor Type (Threshold) : %s\n", + ipmi_get_sensor_type(intf, sensor->sensor.type)); + + printf(" Sensor Reading : "); + if (sr->s_reading_valid) { + if (sr->full) { + uint16_t raw_tol = __TO_TOL(sr->full->mtol); + if (sr->s_has_analog_value) { + double tol = + sdr_convert_sensor_tolerance(sr->full, + raw_tol); + printf("%.*f (+/- %.*f) %s\n", + (sr->s_a_val == (int) + sr->s_a_val) ? 0 : 3, + sr->s_a_val, + (tol == (int) tol) ? 0 : 3, tol, + sr->s_a_units); + } else { + printf("0x%x (+/- 0x%x) %s\n", + sr->s_reading, + raw_tol, + sr->s_a_units); + } + } else { + printf("0x%x %s\n", + sr->s_reading, sr->s_a_units); + } + printf(" Status : %s\n", thresh_status); + + if (thresh_available) { + if (sr->full) { +#define PTS(bit, dataidx, str) { \ +print_thresh_setting(sr->full, rsp->data[0] & (bit), \ + rsp->data[(dataidx)], \ + (str), "%.3f\n", "0x%x\n", "%s\n"); \ +} + PTS(LOWER_NON_RECOV_SPECIFIED, 3, " Lower Non-Recoverable : "); + PTS(LOWER_CRIT_SPECIFIED, 2, " Lower Critical : "); + PTS(LOWER_NON_CRIT_SPECIFIED, 1, " Lower Non-Critical : "); + PTS(UPPER_NON_CRIT_SPECIFIED, 4, " Upper Non-Critical : "); + PTS(UPPER_CRIT_SPECIFIED, 5, " Upper Critical : "); + PTS(UPPER_NON_RECOV_SPECIFIED, 6, " Upper Non-Recoverable : "); +#undef PTS + + } + ipmi_sdr_print_sensor_hysteresis(sensor, sr->full, + sr->full ? sr->full->threshold.hysteresis.positive : + sr->compact->threshold.hysteresis.positive, + "Positive Hysteresis"); + + ipmi_sdr_print_sensor_hysteresis(sensor, sr->full, + sr->full ? sr->full->threshold.hysteresis.negative : + sr->compact->threshold.hysteresis.negative, + "Negative Hysteresis"); + } else { + printf(" Sensor Threshold Settings not available\n"); + } + } else { + printf(" Unable to read sensor: Device Not Present\n\n"); + } + + ipmi_sdr_print_sensor_event_status(intf, + sensor->keys. + sensor_num, + sensor->sensor.type, + sensor->event_type, + ANALOG_SENSOR, + sensor->keys.owner_id, + sensor->keys.lun, + sensor->keys.channel); + ipmi_sdr_print_sensor_event_enable(intf, + sensor->keys. + sensor_num, + sensor->sensor.type, + sensor->event_type, + ANALOG_SENSOR, + sensor->keys.owner_id, + sensor->keys.lun, + sensor->keys.channel); + + printf("\n"); +} + static int ipmi_sensor_print_fc_threshold(struct ipmi_intf *intf, struct sdr_record_common_sensor *sensor, @@ -288,160 +466,13 @@ ipmi_sensor_print_fc_threshold(struct ipmi_intf *intf, thresh_available = 0; if (csv_output) { - printf("%s", sr->s_id); - if (sr->s_reading_valid) { - if (sr->s_has_analog_value) - printf(",%.3f,%s,%s", - sr->s_a_val, sr->s_a_units, thresh_status); - else - printf(",0x%x,%s,%s", - sr->s_reading, sr->s_a_units, thresh_status); - } else { - printf(",%s,%s,%s", - "na", sr->s_a_units, "na"); - } - if (thresh_available && sr->full) { -#define PTS(bit, dataidx) { \ -print_thresh_setting(sr->full, rsp->data[0] & (bit), \ - rsp->data[(dataidx)], ",", "%.3f", "0x%x", "%s"); \ -} - PTS(LOWER_NON_RECOV_SPECIFIED, 3); - PTS(LOWER_CRIT_SPECIFIED, 2); - PTS(LOWER_NON_CRIT_SPECIFIED, 1); - PTS(UPPER_NON_CRIT_SPECIFIED, 4); - PTS(UPPER_CRIT_SPECIFIED, 5); - PTS(UPPER_NON_RECOV_SPECIFIED, 6); -#undef PTS - } else { - printf(",%s,%s,%s,%s,%s,%s", - "na", "na", "na", "na", "na", "na"); - } - printf("\n"); + dump_sensor_fc_thredshold_csv(thresh_available, thresh_status, rsp, sr); } else { if (verbose == 0) { - /* output format - * id value units status thresholds.... - */ - printf("%-16s ", sr->s_id); - if (sr->s_reading_valid) { - if (sr->s_has_analog_value) - printf("| %-10.3f | %-10s | %-6s", - sr->s_a_val, sr->s_a_units, thresh_status); - else - printf("| 0x%-8x | %-10s | %-6s", - sr->s_reading, sr->s_a_units, thresh_status); - } else { - printf("| %-10s | %-10s | %-6s", - "na", sr->s_a_units, "na"); - } - if (thresh_available && sr->full) { -#define PTS(bit, dataidx) { \ - print_thresh_setting(sr->full, rsp->data[0] & (bit), \ - rsp->data[(dataidx)], "| ", "%-10.3f", "0x%-8x", "%-10s"); \ -} - PTS(LOWER_NON_RECOV_SPECIFIED, 3); - PTS(LOWER_CRIT_SPECIFIED, 2); - PTS(LOWER_NON_CRIT_SPECIFIED, 1); - PTS(UPPER_NON_CRIT_SPECIFIED, 4); - PTS(UPPER_CRIT_SPECIFIED, 5); - PTS(UPPER_NON_RECOV_SPECIFIED, 6); -#undef PTS - } else { - printf("| %-10s| %-10s| %-10s| %-10s| %-10s| %-10s", - "na", "na", "na", "na", "na", "na"); - } - - printf("\n"); + dump_sensor_fc_thredshold(thresh_available, thresh_status, rsp, sr); } else { - printf("Sensor ID : %s (0x%x)\n", - sr->s_id, sensor->keys.sensor_num); - - printf(" Entity ID : %d.%d\n", - sensor->entity.id, sensor->entity.instance); - - printf(" Sensor Type (Threshold) : %s\n", - ipmi_get_sensor_type(intf, sensor->sensor. - type)); - - printf(" Sensor Reading : "); - if (sr->s_reading_valid) { - if (sr->full) { - uint16_t raw_tol = __TO_TOL(sr->full->mtol); - if (sr->s_has_analog_value) { - double tol = - sdr_convert_sensor_tolerance(sr->full, - raw_tol); - printf("%.*f (+/- %.*f) %s\n", - (sr->s_a_val == (int) - sr->s_a_val) ? 0 : 3, - sr->s_a_val, - (tol == (int) tol) ? 0 : 3, tol, - sr->s_a_units); - } else { - printf("0x%x (+/- 0x%x) %s\n", - sr->s_reading, - raw_tol, - sr->s_a_units); - } - } else { - printf("0x%x %s\n", sr->s_reading, - sr->s_a_units); - } - printf(" Status : %s\n", thresh_status); - - if (thresh_available) { - if (sr->full) { -#define PTS(bit, dataidx, str) { \ -print_thresh_setting(sr->full, rsp->data[0] & (bit), \ - rsp->data[(dataidx)], \ - (str), "%.3f\n", "0x%x\n", "%s\n"); \ -} - - PTS(LOWER_NON_RECOV_SPECIFIED, 3, " Lower Non-Recoverable : "); - PTS(LOWER_CRIT_SPECIFIED, 2, " Lower Critical : "); - PTS(LOWER_NON_CRIT_SPECIFIED, 1, " Lower Non-Critical : "); - PTS(UPPER_NON_CRIT_SPECIFIED, 4, " Upper Non-Critical : "); - PTS(UPPER_CRIT_SPECIFIED, 5, " Upper Critical : "); - PTS(UPPER_NON_RECOV_SPECIFIED, 6, " Upper Non-Recoverable : "); -#undef PTS - - } - ipmi_sdr_print_sensor_hysteresis(sensor, sr->full, - sr->full ? sr->full->threshold.hysteresis.positive : - sr->compact->threshold.hysteresis.positive, - "Positive Hysteresis"); - - ipmi_sdr_print_sensor_hysteresis(sensor, sr->full, - sr->full ? sr->full->threshold.hysteresis.negative : - sr->compact->threshold.hysteresis.negative, - "Negative Hysteresis"); - } else { - printf(" Sensor Threshold Settings not available\n"); - } - } else { - printf(" Unable to read sensor: Device Not Present\n\n"); - } - - ipmi_sdr_print_sensor_event_status(intf, - sensor->keys. - sensor_num, - sensor->sensor.type, - sensor->event_type, - ANALOG_SENSOR, - sensor->keys.owner_id, - sensor->keys.lun, - sensor->keys.channel); - ipmi_sdr_print_sensor_event_enable(intf, - sensor->keys. - sensor_num, - sensor->sensor.type, - sensor->event_type, - ANALOG_SENSOR, - sensor->keys.owner_id, - sensor->keys.lun, - sensor->keys.channel); - - printf("\n"); + dump_sensor_fc_thredshold_verbose(thresh_available, thresh_status, + intf, sensor, rsp, sr); } }