Skip to content

Commit

Permalink
Allow a single retry with wrong msg_type
Browse files Browse the repository at this point in the history
Getters may fail as the VehicleService sends
messages periodically, and the expected message
with the value may not be the first one to be received.

To fix it, allow one retry. One should be enough
in most cases. The downside of this is that the
unexpected message is discarded. Therefore,
getters shall be used with care.

Alternatively, we could have getters that
discharge the responsibility of processing
the response to users.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
  • Loading branch information
aesteve-rh committed Jan 25, 2024
1 parent 80d3f65 commit 72ff5ab
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ impl Vhal {

pub fn get_gear_selection(&self) -> Result<c::VehicleGear> {
self.get_property(VehicleProperty::GEAR_SELECTION, 0)?;
let resp = self.recv_cmd()?;
resp.is_valid(VehicleHalProto::MsgType::GET_PROPERTY_RESP)?;
let resp = self.recv_retry()?;

Ok(c::VehicleGear::try_from(resp.expect_i32()?)
.map_err(|_| VhalError::ReceiveMessageValueError)?)
Expand All @@ -343,8 +342,8 @@ impl Vhal {

pub fn get_vehicle_speed(&self) -> Result<f32> {
self.get_property(VehicleProperty::PERF_VEHICLE_SPEED, 0)?;
let resp = self.recv_cmd()?;
resp.is_valid(VehicleHalProto::MsgType::GET_PROPERTY_RESP)?;
let resp = self.recv_retry()?;

resp.expect_f32()
}

Expand All @@ -355,8 +354,8 @@ impl Vhal {

pub fn get_vehicle_display_speed(&self) -> Result<f32> {
self.get_property(VehicleProperty::PERF_VEHICLE_SPEED_DISPLAY, 0)?;
let resp = self.recv_cmd()?;
resp.is_valid(VehicleHalProto::MsgType::GET_PROPERTY_RESP)?;
let resp = self.recv_retry()?;

resp.expect_f32()
}

Expand Down Expand Up @@ -427,6 +426,17 @@ impl Vhal {

Ok(msg)
}

fn recv_retry(&self) -> Result<EmulatorMessage> {
let mut resp = self.recv_cmd()?;
if resp.is_valid(VehicleHalProto::MsgType::GET_PROPERTY_RESP).is_err() {
// Allow a single retry.
resp = self.recv_cmd()?;
resp.is_valid(VehicleHalProto::MsgType::GET_PROPERTY_RESP)?;
}

Ok(resp)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 72ff5ab

Please sign in to comment.