emul: SBS Gauge emulator reset rule

Since we have state that gets mutated between tests in the SBS fuel gauge
emulator we ought to reset its state before each test.

Add a reset rule to the SBS fuel gauge emulator that resets the state
before each test. Note: This includes allowing the emulator state to be
modified by user-threads.

Signed-off-by: Aaron Massey <aaronmassey@google.com>
This commit is contained in:
Aaron Massey 2023-05-26 15:24:45 -06:00 committed by Carles Cufí
commit 3e33d6af89

View file

@ -253,6 +253,33 @@ static const struct i2c_emul_api sbs_gauge_emul_api_i2c = {
.transfer = sbs_gauge_emul_transfer_i2c, .transfer = sbs_gauge_emul_transfer_i2c,
}; };
static void sbs_gauge_emul_reset(const struct emul *target)
{
struct sbs_gauge_emul_data *data = target->data;
memset(data, 0, sizeof(*data));
}
#ifdef CONFIG_ZTEST
#include <zephyr/ztest.h>
/* Add test reset handlers in when using emulators with tests */
#define SBS_GAUGE_EMUL_RESET_RULE_BEFORE(inst) \
sbs_gauge_emul_reset(EMUL_DT_GET(DT_DRV_INST(inst)));
static void emul_sbs_gauge_reset_rule_after(const struct ztest_unit_test *test, void *data)
{
ARG_UNUSED(test);
ARG_UNUSED(data);
DT_INST_FOREACH_STATUS_OKAY(SBS_GAUGE_EMUL_RESET_RULE_BEFORE)
}
ZTEST_RULE(emul_sbs_gauge_reset, NULL, emul_sbs_gauge_reset_rule_after);
#else /* !CONFIG_ZTEST */
/* Stub ZTEST_DMEM in case emulator is not used in a testing environment. */
#define ZTEST_DMEM
#endif /* CONFIG_ZTEST */
/** /**
* Set up a new SBS_GAUGE emulator (I2C) * Set up a new SBS_GAUGE emulator (I2C)
* *
@ -262,9 +289,10 @@ static const struct i2c_emul_api sbs_gauge_emul_api_i2c = {
*/ */
static int emul_sbs_sbs_gauge_init(const struct emul *target, const struct device *parent) static int emul_sbs_sbs_gauge_init(const struct emul *target, const struct device *parent)
{ {
ARG_UNUSED(target);
ARG_UNUSED(parent); ARG_UNUSED(parent);
sbs_gauge_emul_reset(target);
return 0; return 0;
} }