Skip to content

Commit 1cff609

Browse files
aaronemasseycarlescufi
authored andcommitted
emul: emul_sbs_gauge read/write returns ret code
To facilitate testing improper reads and writes on various registers, enable the emul_sbs_gauge reg_read/write handlers to return custom exit codes. Signed-off-by: Aaron Massey <aaronmassey@google.com>
1 parent df42cf3 commit 1cff609

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

subsys/emul/i2c/emul_sbs_gauge.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ struct sbs_gauge_emul_cfg {
3131
uint16_t addr;
3232
};
3333

34-
static void reg_write(const struct emul *target, int reg, int val)
34+
static int reg_write(const struct emul *target, int reg, int val)
3535
{
3636
ARG_UNUSED(target);
3737

3838
LOG_INF("write %x = %x", reg, val);
3939
switch (reg) {
4040
default:
4141
LOG_INF("Unknown write %x", reg);
42+
return -EIO;
4243
}
44+
45+
return 0;
4346
}
4447

45-
static int reg_read(const struct emul *target, int reg)
48+
static int reg_read(const struct emul *target, int reg, int *val)
4649
{
47-
int val;
48-
4950
ARG_UNUSED(target);
5051

5152
switch (reg) {
@@ -61,15 +62,15 @@ static int reg_read(const struct emul *target, int reg)
6162
case SBS_GAUGE_CMD_CYCLE_COUNT:
6263
case SBS_GAUGE_CMD_DESIGN_VOLTAGE:
6364
/* Arbitrary stub value. */
64-
val = 1;
65+
*val = 1;
6566
break;
6667
default:
6768
LOG_ERR("Unknown register 0x%x read", reg);
6869
return -EIO;
6970
}
70-
LOG_INF("read 0x%x = 0x%x", reg, val);
71+
LOG_INF("read 0x%x = 0x%x", reg, *val);
7172

72-
return val;
73+
return 0;
7374
}
7475

7576
static int sbs_gauge_emul_transfer_i2c(const struct emul *target, struct i2c_msg *msgs,
@@ -79,6 +80,7 @@ static int sbs_gauge_emul_transfer_i2c(const struct emul *target, struct i2c_msg
7980
struct sbs_gauge_emul_data *data;
8081
unsigned int val;
8182
int reg;
83+
int rc;
8284

8385
data = target->data;
8486

@@ -102,7 +104,7 @@ static int sbs_gauge_emul_transfer_i2c(const struct emul *target, struct i2c_msg
102104
if (msgs->flags & I2C_MSG_READ) {
103105
switch (msgs->len - 1) {
104106
case 1:
105-
val = reg_read(target, reg);
107+
rc = reg_read(target, reg, &val);
106108
msgs->buf[0] = val;
107109
break;
108110
default:
@@ -113,15 +115,15 @@ static int sbs_gauge_emul_transfer_i2c(const struct emul *target, struct i2c_msg
113115
if (msgs->len != 1) {
114116
LOG_ERR("Unexpected msg1 length %d", msgs->len);
115117
}
116-
reg_write(target, reg, msgs->buf[0]);
118+
rc = reg_write(target, reg, msgs->buf[0]);
117119
}
118120
break;
119121
default:
120122
LOG_ERR("Invalid number of messages: %d", num_msgs);
121123
return -EIO;
122124
}
123125

124-
return 0;
126+
return rc;
125127
}
126128

127129
static const struct i2c_emul_api sbs_gauge_emul_api_i2c = {

0 commit comments

Comments
 (0)