Skip to content

Commit e4495ac

Browse files
committed
tacd: error propagation instead of unwraping part 1
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.
1 parent 3cb20ac commit e4495ac

File tree

12 files changed

+131
-122
lines changed

12 files changed

+131
-122
lines changed

src/adc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Adc {
8383

8484
let adc = Self {
8585
usb_host_curr: AdcChannel {
86-
fast: stm32_thread.clone().get_channel("usb-host-curr").unwrap(),
86+
fast: stm32_thread.clone().get_channel("usb-host-curr")?,
8787
topic: bb.topic(
8888
"/v1/usb/host/total/feedback/current",
8989
true,
@@ -94,7 +94,7 @@ impl Adc {
9494
),
9595
},
9696
usb_host1_curr: AdcChannel {
97-
fast: stm32_thread.clone().get_channel("usb-host1-curr").unwrap(),
97+
fast: stm32_thread.clone().get_channel("usb-host1-curr")?,
9898
topic: bb.topic(
9999
"/v1/usb/host/port1/feedback/current",
100100
true,
@@ -105,7 +105,7 @@ impl Adc {
105105
),
106106
},
107107
usb_host2_curr: AdcChannel {
108-
fast: stm32_thread.clone().get_channel("usb-host2-curr").unwrap(),
108+
fast: stm32_thread.clone().get_channel("usb-host2-curr")?,
109109
topic: bb.topic(
110110
"/v1/usb/host/port2/feedback/current",
111111
true,
@@ -116,7 +116,7 @@ impl Adc {
116116
),
117117
},
118118
usb_host3_curr: AdcChannel {
119-
fast: stm32_thread.clone().get_channel("usb-host3-curr").unwrap(),
119+
fast: stm32_thread.clone().get_channel("usb-host3-curr")?,
120120
topic: bb.topic(
121121
"/v1/usb/host/port3/feedback/current",
122122
true,
@@ -127,7 +127,7 @@ impl Adc {
127127
),
128128
},
129129
out0_volt: AdcChannel {
130-
fast: stm32_thread.clone().get_channel("out0-volt").unwrap(),
130+
fast: stm32_thread.clone().get_channel("out0-volt")?,
131131
topic: bb.topic(
132132
"/v1/output/out_0/feedback/voltage",
133133
true,
@@ -138,7 +138,7 @@ impl Adc {
138138
),
139139
},
140140
out1_volt: AdcChannel {
141-
fast: stm32_thread.clone().get_channel("out1-volt").unwrap(),
141+
fast: stm32_thread.clone().get_channel("out1-volt")?,
142142
topic: bb.topic(
143143
"/v1/output/out_1/feedback/voltage",
144144
true,
@@ -149,7 +149,7 @@ impl Adc {
149149
),
150150
},
151151
iobus_curr: AdcChannel {
152-
fast: stm32_thread.clone().get_channel("iobus-curr").unwrap(),
152+
fast: stm32_thread.clone().get_channel("iobus-curr")?,
153153
topic: bb.topic(
154154
"/v1/iobus/feedback/current",
155155
true,
@@ -160,7 +160,7 @@ impl Adc {
160160
),
161161
},
162162
iobus_volt: AdcChannel {
163-
fast: stm32_thread.clone().get_channel("iobus-volt").unwrap(),
163+
fast: stm32_thread.clone().get_channel("iobus-volt")?,
164164
topic: bb.topic(
165165
"/v1/iobus/feedback/voltage",
166166
true,
@@ -171,7 +171,7 @@ impl Adc {
171171
),
172172
},
173173
pwr_volt: AdcChannel {
174-
fast: powerboard_thread.clone().get_channel("pwr-volt").unwrap(),
174+
fast: powerboard_thread.clone().get_channel("pwr-volt")?,
175175
topic: bb.topic(
176176
"/v1/dut/feedback/voltage",
177177
true,
@@ -182,7 +182,7 @@ impl Adc {
182182
),
183183
},
184184
pwr_curr: AdcChannel {
185-
fast: powerboard_thread.get_channel("pwr-curr").unwrap(),
185+
fast: powerboard_thread.get_channel("pwr-curr")?,
186186
topic: bb.topic(
187187
"/v1/dut/feedback/current",
188188
true,

src/dbus.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
1819
use async_std::sync::Arc;
1920

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

2324
#[cfg(feature = "demo_mode")]
2425
mod zb {
25-
pub type Result<T> = std::result::Result<T, ()>;
26+
use anyhow::Result;
2627

2728
pub struct Connection;
2829
pub struct ConnectionBuilder;
@@ -51,7 +52,7 @@ mod zb {
5152
pub use zbus::*;
5253
}
5354

54-
use zb::{Connection, ConnectionBuilder, Result};
55+
use zb::{Connection, ConnectionBuilder};
5556

5657
pub mod networkmanager;
5758
pub mod rauc;
@@ -76,20 +77,17 @@ impl DbusSession {
7677
bb: &mut BrokerBuilder,
7778
led_dut: Arc<Topic<BlinkPattern>>,
7879
led_uplink: Arc<Topic<BlinkPattern>>,
79-
) -> Self {
80+
) -> Result<Self> {
8081
let tacd = Tacd::new();
8182

82-
let conn_builder = ConnectionBuilder::system()
83-
.unwrap()
84-
.name("de.pengutronix.tacd")
85-
.unwrap();
83+
let conn_builder = ConnectionBuilder::system()?.name("de.pengutronix.tacd")?;
8684

87-
let conn = Arc::new(tacd.serve(conn_builder).build().await.unwrap());
85+
let conn = Arc::new(tacd.serve(conn_builder).build().await?);
8886

89-
Self {
87+
Ok(Self {
9088
network: Network::new(bb, &conn, led_dut, led_uplink),
9189
rauc: Rauc::new(bb, &conn),
9290
systemd: Systemd::new(bb, &conn).await,
93-
}
91+
})
9492
}
9593
}

src/digital_io.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::{Context, Result};
1819
use async_std::prelude::*;
1920
use async_std::sync::Arc;
2021
use async_std::task::spawn;
@@ -58,43 +59,42 @@ fn handle_line_wo(
5859
initial: bool,
5960
inverted: bool,
6061
led_topic: Option<Arc<Topic<BlinkPattern>>>,
61-
) -> Arc<Topic<bool>> {
62+
) -> Result<Arc<Topic<bool>>> {
6263
let topic = bb.topic_rw(path, Some(initial));
63-
let line = find_line(line_name).unwrap();
64-
let dst = line
65-
.request(LineRequestFlags::OUTPUT, (initial ^ inverted) as _, "tacd")
66-
.unwrap();
64+
let line = find_line(line_name).with_context(|| format!("couldn't find line {line_name}"))?;
65+
let dst = line.request(LineRequestFlags::OUTPUT, (initial ^ inverted) as _, "tacd")?;
6766

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

7069
spawn(async move {
7170
while let Some(ev) = src.next().await {
72-
dst.set_value((ev ^ inverted) as _).unwrap();
71+
dst.set_value((ev ^ inverted) as _)?;
7372

7473
if let Some(led) = &led_topic {
7574
let pattern = BlinkPattern::solid(if ev { 1.0 } else { 0.0 });
7675
led.set(pattern);
7776
}
7877
}
78+
anyhow::Ok(())
7979
});
8080

81-
topic
81+
Ok(topic)
8282
}
8383

8484
impl DigitalIo {
8585
pub fn new(
8686
bb: &mut BrokerBuilder,
8787
led_0: Arc<Topic<BlinkPattern>>,
8888
led_1: Arc<Topic<BlinkPattern>>,
89-
) -> Self {
89+
) -> Result<Self> {
9090
let out_0 = handle_line_wo(
9191
bb,
9292
"/v1/output/out_0/asserted",
9393
"OUT_0",
9494
false,
9595
false,
9696
Some(led_0),
97-
);
97+
)?;
9898

9999
let out_1 = handle_line_wo(
100100
bb,
@@ -103,16 +103,16 @@ impl DigitalIo {
103103
false,
104104
false,
105105
Some(led_1),
106-
);
106+
)?;
107107

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

111-
Self {
111+
Ok(Self {
112112
out_0,
113113
out_1,
114114
uart_rx_en,
115115
uart_tx_en,
116-
}
116+
})
117117
}
118118
}

src/digital_io/gpio/demo_mode.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,28 @@ pub struct LineHandle {
2727
}
2828

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

3838
match self.name.as_str() {
3939
"OUT_0" => iio_thread_stm32
40-
.get_channel("out0-volt")
41-
.unwrap()
40+
.get_channel("out0-volt")?
4241
.set(val != 0),
4342
"OUT_1" => iio_thread_stm32
44-
.get_channel("out1-volt")
45-
.unwrap()
43+
.get_channel("out1-volt")?
4644
.set(val != 0),
4745
"DUT_PWR_EN" => {
4846
iio_thread_pwr
4947
.clone()
50-
.get_channel("pwr-curr")
51-
.unwrap()
48+
.get_channel("pwr-curr")?
5249
.set(val == 0);
5350
iio_thread_pwr
54-
.get_channel("pwr-volt")
55-
.unwrap()
51+
.get_channel("pwr-volt")?
5652
.set(val == 0);
5753
}
5854
_ => {}

src/digital_io/gpio/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct LineHandle {
3333
}
3434

3535
impl LineHandle {
36-
pub fn set_value(&self, val: u8) -> Result<(), ()> {
36+
pub fn set_value(&self, val: u8) -> Result<()> {
3737
println!("GPIO simulation set {} to {}", self.name, val);
3838
self.val.store(val, Ordering::Relaxed);
3939
Ok(())

0 commit comments

Comments
 (0)