Skip to content

Commit

Permalink
orb-mcu-util: don't fail on build (#118)
Browse files Browse the repository at this point in the history
* orb-mcu-util: don't fail on build

when fetching data, don't fail
but prefer unfilled struct

* orb-mcu-util: board info to return Result<Self,Self>

on error, it means board info is incomplete

---------

Co-authored-by: Ryan Butler <[email protected]>
  • Loading branch information
fouge and TheButlah authored Jun 11, 2024
1 parent defc11d commit 852886e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 30 deletions.
57 changes: 40 additions & 17 deletions mcu-util/src/orb/main_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Sub;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time;
use tracing::{debug, info, warn};
use tracing::{debug, error, info};

use orb_mcu_interface::can::canfd::CanRawMessaging;
use orb_mcu_interface::can::isotp::{CanIsoTpMessaging, IsoTpNodeIdentifier};
Expand Down Expand Up @@ -106,11 +106,14 @@ impl Board for MainBoard {
}

async fn fetch_info(&mut self, info: &mut OrbInfo) -> Result<()> {
let main = MainBoardInfo::new().build(self).await?;
let board_info = MainBoardInfo::new()
.build(self)
.await
.unwrap_or_else(|board_info| board_info);

info.hw_rev = main.hw_version;
info.main_fw_versions = main.fw_versions;
info.main_battery_status = main.battery_status;
info.hw_rev = board_info.hw_version;
info.main_fw_versions = board_info.fw_versions;
info.main_battery_status = board_info.battery_status;

Ok(())
}
Expand Down Expand Up @@ -206,8 +209,11 @@ impl Board for MainBoard {
}

async fn switch_images(&mut self) -> Result<()> {
let main = MainBoardInfo::new().build(self).await?;
if let Some(fw_versions) = main.fw_versions {
let board_info = MainBoardInfo::new()
.build(self)
.await
.unwrap_or_else(|board_info| board_info);
if let Some(fw_versions) = board_info.fw_versions {
if let Some(secondary_app) = fw_versions.secondary_app {
if let Some(primary_app) = fw_versions.primary_app {
return if (primary_app.commit_hash == 0
Expand Down Expand Up @@ -329,9 +335,11 @@ impl MainBoardInfo {
}

/// Fetches `MainBoardInfo` from the main board
/// doesn't fail, but lazily fetches as much info as it could
/// on timeout, returns the info that was fetched so far
async fn build(mut self, main_board: &mut MainBoard) -> Result<Self> {
main_board
async fn build(mut self, main_board: &mut MainBoard) -> Result<Self, Self> {
let mut is_err = false;
if let Err(e) = main_board
.isotp_iface
.send(McuPayload::ToMain(
main_messaging::jetson_to_mcu::Payload::ValueGet(
Expand All @@ -341,8 +349,13 @@ impl MainBoardInfo {
},
),
))
.await?;
main_board
.await
{
is_err = true;
error!("error asking for firmware version: {e}");
}

if let Err(e) = main_board
.isotp_iface
.send(McuPayload::ToMain(
main_messaging::jetson_to_mcu::Payload::ValueGet(
Expand All @@ -352,8 +365,13 @@ impl MainBoardInfo {
},
),
))
.await?;
main_board
.await
{
is_err = true;
error!("error asking for hardware version: {e}");
}

if let Err(e) = main_board
.isotp_iface
.send(McuPayload::ToMain(
main_messaging::jetson_to_mcu::Payload::ValueGet(
Expand All @@ -362,7 +380,12 @@ impl MainBoardInfo {
},
),
))
.await?;
.await
{
is_err = true;
error!("error asking for battery status: {e}");
}

let mut now = std::time::Instant::now();
let mut timeout = std::time::Duration::from_secs(2);
let mut battery_status = BatteryStatus {
Expand Down Expand Up @@ -401,8 +424,8 @@ impl MainBoardInfo {
timeout = timeout.sub(now.elapsed());
now = std::time::Instant::now();
} else {
warn!("Timeout waiting on main board info");
return Ok(self);
error!("Timeout waiting on main board info");
return Err(self);
}

if self.battery_status.is_none()
Expand All @@ -418,7 +441,7 @@ impl MainBoardInfo {
&& self.fw_versions.is_some()
&& self.battery_status.is_some()
{
return Ok(self);
return if is_err { Err(self) } else { Ok(self) };
}
}
}
Expand Down
48 changes: 35 additions & 13 deletions mcu-util/src/orb/security_board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Sub;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time;
use tracing::{debug, info, warn};
use tracing::{debug, error, info};

use orb_mcu_interface::can::canfd::CanRawMessaging;
use orb_mcu_interface::can::isotp::{CanIsoTpMessaging, IsoTpNodeIdentifier};
Expand Down Expand Up @@ -118,7 +118,10 @@ impl Board for SecurityBoard {
}

async fn fetch_info(&mut self, info: &mut OrbInfo) -> Result<()> {
let board_info = SecurityBoardInfo::new().build(self).await?;
let board_info = SecurityBoardInfo::new()
.build(self)
.await
.unwrap_or_else(|board_info| board_info);

info.sec_fw_versions = board_info.fw_versions;
info.sec_battery_status = board_info.battery_status;
Expand Down Expand Up @@ -218,7 +221,10 @@ impl Board for SecurityBoard {
}

async fn switch_images(&mut self) -> Result<()> {
let board_info = SecurityBoardInfo::new().build(self).await?;
let board_info = SecurityBoardInfo::new()
.build(self)
.await
.unwrap_or_else(|board_info| board_info);
if let Some(fw_versions) = board_info.fw_versions {
if let Some(secondary_app) = fw_versions.secondary_app {
if let Some(primary_app) = fw_versions.primary_app {
Expand Down Expand Up @@ -341,8 +347,9 @@ impl SecurityBoardInfo {

/// Fetches `SecurityBoardInfo` from the security board
/// on timeout, returns the info that was fetched so far
async fn build(mut self, sec_board: &mut SecurityBoard) -> Result<Self> {
sec_board
async fn build(mut self, sec_board: &mut SecurityBoard) -> Result<Self, Self> {
let mut is_err = false;
if let Err(e) = sec_board
.isotp_iface
.send(McuPayload::ToSec(
security_messaging::jetson_to_sec::Payload::ValueGet(
Expand All @@ -352,8 +359,13 @@ impl SecurityBoardInfo {
},
),
))
.await?;
sec_board
.await
{
is_err = true;
error!("Failed to fetch firmware versions: {:?}", e);
}

if let Err(e) = sec_board
.isotp_iface
.send(McuPayload::ToSec(
security_messaging::jetson_to_sec::Payload::ValueGet(
Expand All @@ -363,8 +375,13 @@ impl SecurityBoardInfo {
},
),
))
.await?;
sec_board
.await
{
is_err = true;
error!("Failed to fetch hardware versions: {:?}", e);
}

if let Err(e) = sec_board
.isotp_iface
.send(McuPayload::ToSec(
security_messaging::jetson_to_sec::Payload::ValueGet(
Expand All @@ -374,7 +391,12 @@ impl SecurityBoardInfo {
},
),
))
.await?;
.await
{
is_err = true;
error!("Failed to fetch battery status: {:?}", e);
}

let mut now = std::time::Instant::now();
let mut timeout = std::time::Duration::from_secs(2);
let mut battery_status = BatteryStatus {
Expand All @@ -401,8 +423,8 @@ impl SecurityBoardInfo {
timeout = timeout.sub(now.elapsed());
now = std::time::Instant::now();
} else {
warn!("Timeout waiting on security board info");
return Ok(self);
error!("Timeout waiting on security board info");
return Err(self);
}

if self.battery_status.is_none()
Expand All @@ -415,7 +437,7 @@ impl SecurityBoardInfo {

// check that all fields are set in BoardInfo
if self.fw_versions.is_some() && self.battery_status.is_some() {
return Ok(self);
return if is_err { Err(self) } else { Ok(self) };
}
}
}
Expand Down

0 comments on commit 852886e

Please sign in to comment.