From c1d22452f496d9b888e7a7ce3e705c4837d75ab0 Mon Sep 17 00:00:00 2001 From: Albert Esteve Date: Tue, 23 Jan 2024 17:02:19 +0100 Subject: [PATCH] Allow a single retry with wrong msg_type 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 --- src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d5f160e..c118c19 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -329,8 +329,7 @@ impl Vhal { pub fn get_gear_selection(&self) -> Result { 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)?) @@ -343,8 +342,8 @@ impl Vhal { pub fn get_vehicle_speed(&self) -> Result { 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() } @@ -355,8 +354,8 @@ impl Vhal { pub fn get_vehicle_display_speed(&self) -> Result { 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() } @@ -427,6 +426,17 @@ impl Vhal { Ok(msg) } + + fn recv_retry(&self) -> Result { + 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)]