From 41cf03de4c8c0cc1bf0297796893f925e8ee1cd8 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Tue, 15 Nov 2022 13:36:33 -0700 Subject: fuel_gauge: Simplify edge cases Update the local static function to take the type in and use an assert since the bound checking is done at the call site. Also, improve the bounds checking to >= BATTERY_TYPE_COUNT. BRANCH=none BUG=none TEST=twister Signed-off-by: Yuval Peress Change-Id: Ia79bdaafdcd6c48d29d8cef5f981f746066d35b5 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4030056 Code-Coverage: Zoss Reviewed-by: Aaron Massey --- common/battery_fuel_gauge.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/common/battery_fuel_gauge.c b/common/battery_fuel_gauge.c index 88f941f194..e6aca68472 100644 --- a/common/battery_fuel_gauge.c +++ b/common/battery_fuel_gauge.c @@ -237,17 +237,12 @@ enum ec_error_list battery_sleep_fuel_gauge(void) return sb_write(sleep_command->reg_addr, sleep_command->reg_data); } -static enum ec_error_list battery_get_fet_status_regval(int *regval) +static enum ec_error_list battery_get_fet_status_regval(int type, int *regval) { int rv; uint8_t data[6]; - int type = get_battery_type(); - /* If battery type is not known, can't check CHG/DCHG FETs */ - if (type == BATTERY_TYPE_COUNT) { - /* Still don't know, so return here */ - return EC_ERROR_BUSY; - } + ASSERT(type < BATTERY_TYPE_COUNT); /* Read the status of charge/discharge FETs */ if (board_battery_info[type].fuel_gauge.fet.mfgacc_support == 1) { @@ -270,7 +265,7 @@ int battery_is_charge_fet_disabled(void) int type = get_battery_type(); /* If battery type is not known, can't check CHG/DCHG FETs */ - if (type == BATTERY_TYPE_COUNT) { + if (type >= BATTERY_TYPE_COUNT) { /* Still don't know, so return here */ return -1; } @@ -281,7 +276,7 @@ int battery_is_charge_fet_disabled(void) if (!board_battery_info[type].fuel_gauge.fet.cfet_mask) return 0; - rv = battery_get_fet_status_regval(®); + rv = battery_get_fet_status_regval(type, ®); if (rv) return -1; @@ -307,12 +302,12 @@ enum battery_disconnect_state battery_get_disconnect_state(void) int type = get_battery_type(); /* If battery type is not known, can't check CHG/DCHG FETs */ - if (type == BATTERY_TYPE_COUNT) { + if (type >= BATTERY_TYPE_COUNT) { /* Still don't know, so return here */ return BATTERY_DISCONNECT_ERROR; } - if (battery_get_fet_status_regval(®)) + if (battery_get_fet_status_regval(type, ®)) return BATTERY_DISCONNECT_ERROR; if ((reg & board_battery_info[type].fuel_gauge.fet.reg_mask) == -- cgit v1.2.1