Skip to content

Commit

Permalink
tacd: error propagation instead of unwraping part 1
Browse files Browse the repository at this point in the history
Instead of unwrapping Results and potentially panicking we replace it
with proper error propagation that can be acted upon in the future e.g.
return different exit codes for systemd or display error messages.
  • Loading branch information
KarlK90 committed Sep 28, 2023
1 parent 3cb20ac commit c0391af
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 128 deletions.
20 changes: 10 additions & 10 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Adc {

let adc = Self {
usb_host_curr: AdcChannel {
fast: stm32_thread.clone().get_channel("usb-host-curr").unwrap(),
fast: stm32_thread.clone().get_channel("usb-host-curr")?,
topic: bb.topic(
"/v1/usb/host/total/feedback/current",
true,
Expand All @@ -94,7 +94,7 @@ impl Adc {
),
},
usb_host1_curr: AdcChannel {
fast: stm32_thread.clone().get_channel("usb-host1-curr").unwrap(),
fast: stm32_thread.clone().get_channel("usb-host1-curr")?,
topic: bb.topic(
"/v1/usb/host/port1/feedback/current",
true,
Expand All @@ -105,7 +105,7 @@ impl Adc {
),
},
usb_host2_curr: AdcChannel {
fast: stm32_thread.clone().get_channel("usb-host2-curr").unwrap(),
fast: stm32_thread.clone().get_channel("usb-host2-curr")?,
topic: bb.topic(
"/v1/usb/host/port2/feedback/current",
true,
Expand All @@ -116,7 +116,7 @@ impl Adc {
),
},
usb_host3_curr: AdcChannel {
fast: stm32_thread.clone().get_channel("usb-host3-curr").unwrap(),
fast: stm32_thread.clone().get_channel("usb-host3-curr")?,
topic: bb.topic(
"/v1/usb/host/port3/feedback/current",
true,
Expand All @@ -127,7 +127,7 @@ impl Adc {
),
},
out0_volt: AdcChannel {
fast: stm32_thread.clone().get_channel("out0-volt").unwrap(),
fast: stm32_thread.clone().get_channel("out0-volt")?,
topic: bb.topic(
"/v1/output/out_0/feedback/voltage",
true,
Expand All @@ -138,7 +138,7 @@ impl Adc {
),
},
out1_volt: AdcChannel {
fast: stm32_thread.clone().get_channel("out1-volt").unwrap(),
fast: stm32_thread.clone().get_channel("out1-volt")?,
topic: bb.topic(
"/v1/output/out_1/feedback/voltage",
true,
Expand All @@ -149,7 +149,7 @@ impl Adc {
),
},
iobus_curr: AdcChannel {
fast: stm32_thread.clone().get_channel("iobus-curr").unwrap(),
fast: stm32_thread.clone().get_channel("iobus-curr")?,
topic: bb.topic(
"/v1/iobus/feedback/current",
true,
Expand All @@ -160,7 +160,7 @@ impl Adc {
),
},
iobus_volt: AdcChannel {
fast: stm32_thread.clone().get_channel("iobus-volt").unwrap(),
fast: stm32_thread.clone().get_channel("iobus-volt")?,
topic: bb.topic(
"/v1/iobus/feedback/voltage",
true,
Expand All @@ -171,7 +171,7 @@ impl Adc {
),
},
pwr_volt: AdcChannel {
fast: powerboard_thread.clone().get_channel("pwr-volt").unwrap(),
fast: powerboard_thread.clone().get_channel("pwr-volt")?,
topic: bb.topic(
"/v1/dut/feedback/voltage",
true,
Expand All @@ -182,7 +182,7 @@ impl Adc {
),
},
pwr_curr: AdcChannel {
fast: powerboard_thread.get_channel("pwr-curr").unwrap(),
fast: powerboard_thread.get_channel("pwr-curr")?,
topic: bb.topic(
"/v1/dut/feedback/current",
true,
Expand Down
18 changes: 8 additions & 10 deletions src/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use anyhow::Result;
use async_std::sync::Arc;

use crate::broker::{BrokerBuilder, Topic};
use crate::led::BlinkPattern;

#[cfg(feature = "demo_mode")]
mod zb {
pub type Result<T> = std::result::Result<T, ()>;
use anyhow::Result;

pub struct Connection;
pub struct ConnectionBuilder;
Expand Down Expand Up @@ -51,7 +52,7 @@ mod zb {
pub use zbus::*;
}

use zb::{Connection, ConnectionBuilder, Result};
use zb::{Connection, ConnectionBuilder};

pub mod networkmanager;
pub mod rauc;
Expand All @@ -76,20 +77,17 @@ impl DbusSession {
bb: &mut BrokerBuilder,
led_dut: Arc<Topic<BlinkPattern>>,
led_uplink: Arc<Topic<BlinkPattern>>,
) -> Self {
) -> Result<Self> {
let tacd = Tacd::new();

let conn_builder = ConnectionBuilder::system()
.unwrap()
.name("de.pengutronix.tacd")
.unwrap();
let conn_builder = ConnectionBuilder::system()?.name("de.pengutronix.tacd")?;

let conn = Arc::new(tacd.serve(conn_builder).build().await.unwrap());
let conn = Arc::new(tacd.serve(conn_builder).build().await?);

Self {
Ok(Self {
network: Network::new(bb, &conn, led_dut, led_uplink),
rauc: Rauc::new(bb, &conn),
systemd: Systemd::new(bb, &conn).await,
}
})
}
}
28 changes: 14 additions & 14 deletions src/digital_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use anyhow::{Context, Result};
use async_std::prelude::*;
use async_std::sync::Arc;
use async_std::task::spawn;
Expand Down Expand Up @@ -58,43 +59,42 @@ fn handle_line_wo(
initial: bool,
inverted: bool,
led_topic: Option<Arc<Topic<BlinkPattern>>>,
) -> Arc<Topic<bool>> {
) -> Result<Arc<Topic<bool>>> {
let topic = bb.topic_rw(path, Some(initial));
let line = find_line(line_name).unwrap();
let dst = line
.request(LineRequestFlags::OUTPUT, (initial ^ inverted) as _, "tacd")
.unwrap();
let line = find_line(line_name).with_context(|| format!("couldn't find line {line_name}"))?;
let dst = line.request(LineRequestFlags::OUTPUT, (initial ^ inverted) as _, "tacd")?;

let (mut src, _) = topic.clone().subscribe_unbounded();

spawn(async move {
while let Some(ev) = src.next().await {
dst.set_value((ev ^ inverted) as _).unwrap();
dst.set_value((ev ^ inverted) as _)?;

if let Some(led) = &led_topic {
let pattern = BlinkPattern::solid(if ev { 1.0 } else { 0.0 });
led.set(pattern);
}
}
anyhow::Ok(())
});

topic
Ok(topic)
}

impl DigitalIo {
pub fn new(
bb: &mut BrokerBuilder,
led_0: Arc<Topic<BlinkPattern>>,
led_1: Arc<Topic<BlinkPattern>>,
) -> Self {
) -> Result<Self> {
let out_0 = handle_line_wo(
bb,
"/v1/output/out_0/asserted",
"OUT_0",
false,
false,
Some(led_0),
);
)?;

let out_1 = handle_line_wo(
bb,
Expand All @@ -103,16 +103,16 @@ impl DigitalIo {
false,
false,
Some(led_1),
);
)?;

let uart_rx_en = handle_line_wo(bb, "/v1/uart/rx/enabled", "UART_RX_EN", true, true, None);
let uart_tx_en = handle_line_wo(bb, "/v1/uart/tx/enabled", "UART_TX_EN", true, true, None);
let uart_rx_en = handle_line_wo(bb, "/v1/uart/rx/enabled", "UART_RX_EN", true, true, None)?;
let uart_tx_en = handle_line_wo(bb, "/v1/uart/tx/enabled", "UART_TX_EN", true, true, None)?;

Self {
Ok(Self {
out_0,
out_1,
uart_rx_en,
uart_tx_en,
}
})
}
}
24 changes: 7 additions & 17 deletions src/digital_io/gpio/demo_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,23 @@ pub struct LineHandle {
}

impl LineHandle {
pub fn set_value(&self, val: u8) -> Result<(), ()> {
pub fn set_value(&self, val: u8) -> Result<()> {
// This does not actually set up any IIO things.
// It is just a hack to let adc/iio/demo_mode.rs
// communicate with this function so that toggling an output
// has an effect on the measured values.
let iio_thread_stm32 = block_on(IioThread::new_stm32()).unwrap();
let iio_thread_pwr = block_on(IioThread::new_powerboard()).unwrap();
let iio_thread_stm32 = block_on(IioThread::new_stm32())?;
let iio_thread_pwr = block_on(IioThread::new_powerboard())?;

match self.name.as_str() {
"OUT_0" => iio_thread_stm32
.get_channel("out0-volt")
.unwrap()
.set(val != 0),
"OUT_1" => iio_thread_stm32
.get_channel("out1-volt")
.unwrap()
.set(val != 0),
"OUT_0" => iio_thread_stm32.get_channel("out0-volt")?.set(val != 0),
"OUT_1" => iio_thread_stm32.get_channel("out1-volt")?.set(val != 0),
"DUT_PWR_EN" => {
iio_thread_pwr
.clone()
.get_channel("pwr-curr")
.unwrap()
.set(val == 0);
iio_thread_pwr
.get_channel("pwr-volt")
.unwrap()
.get_channel("pwr-curr")?
.set(val == 0);
iio_thread_pwr.get_channel("pwr-volt")?.set(val == 0);
}
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/digital_io/gpio/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct LineHandle {
}

impl LineHandle {
pub fn set_value(&self, val: u8) -> Result<(), ()> {
pub fn set_value(&self, val: u8) -> Result<()> {
println!("GPIO simulation set {} to {}", self.name, val);
self.val.store(val, Ordering::Relaxed);
Ok(())
Expand Down
Loading

0 comments on commit c0391af

Please sign in to comment.