1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! UART nets: interactive serial streaming over the box's Socket.IO
//! `/uart` namespace (feature `uart`).
//!
//! The box owns the serial port (exclusive open) and streams data as hex
//! chunks over `uart_data` events; writes go up as `uart_write` events.
//! Only one session per net or device is allowed — a second opener gets a
//! clear "already in use" error.
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, Instant};
use rust_socketio::ClientBuilder;
use serde_json::{json, Value};
use crate::error::{Error, Result};
use crate::nets::sio::{hex_decode, hex_encode, payload_json};
/// How long to wait for the box to confirm the serial port opened.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
#[derive(Debug)]
enum UartEvent {
Connected { device_path: String, baudrate: u64 },
Data(Vec<u8>),
Status(String),
Error(String),
Stopped,
}
/// A live UART streaming session.
///
/// Created via [`crate::LagerBox::uart`]. Data received from the device is
/// buffered internally; pull it with [`Uart::read`] or scan for expected
/// output with [`Uart::wait_for`]. The session is stopped cleanly on
/// [`Uart::stop`] or drop.
pub struct Uart {
socket: Option<rust_socketio::client::Client>,
rx: Receiver<UartEvent>,
netname: String,
device_path: String,
baudrate: u64,
buf: Vec<u8>,
last_status: Option<String>,
}
impl Uart {
pub(crate) fn open(
base_url: &str,
netname: String,
bearer_token: Option<String>,
) -> Result<Self> {
let (tx, rx) = std::sync::mpsc::channel::<UartEvent>();
let socket = {
let tx_connected: Sender<UartEvent> = tx.clone();
let tx_data = tx.clone();
let tx_status = tx.clone();
let tx_error = tx.clone();
let tx_stopped = tx;
let mut builder = ClientBuilder::new(base_url).namespace("/uart");
// Boxes behind an authenticating gateway need the bearer token
// on the Socket.IO handshake too (same reverse proxy).
if let Some(token) = &bearer_token {
builder = builder.opening_header("Authorization", format!("Bearer {token}"));
}
builder
.on("uart_connected", move |payload, _| {
let info = payload_json(payload).unwrap_or(Value::Null);
let device_path = info
.get("device_path")
.and_then(Value::as_str)
.unwrap_or_default()
.to_string();
let baudrate = info.get("baudrate").and_then(Value::as_u64).unwrap_or(0);
let _ = tx_connected.send(UartEvent::Connected {
device_path,
baudrate,
});
})
.on("uart_data", move |payload, _| {
if let Some(bytes) = payload_json(payload)
.as_ref()
.and_then(|v| v.get("data"))
.and_then(Value::as_str)
.and_then(hex_decode)
{
let _ = tx_data.send(UartEvent::Data(bytes));
}
})
.on("uart_status", move |payload, _| {
// reconnecting/reconnected notifications during device
// re-enumeration; informational only.
if let Some(status) = payload_json(payload)
.as_ref()
.and_then(|v| v.get("status"))
.and_then(Value::as_str)
{
let _ = tx_status.send(UartEvent::Status(status.to_string()));
}
})
.on("error", move |payload, _| {
let message = payload_json(payload)
.as_ref()
.and_then(|v| v.get("message"))
.and_then(Value::as_str)
.unwrap_or("unknown UART error")
.to_string();
let _ = tx_error.send(UartEvent::Error(message));
})
.on("uart_stopped", move |_, _| {
let _ = tx_stopped.send(UartEvent::Stopped);
})
.connect()
.map_err(|e| Error::Connection(format!("Socket.IO connect failed: {e}")))?
};
socket
.emit("start_uart", json!({ "netname": netname, "overrides": {} }))
.map_err(|e| Error::Stream(format!("could not start UART session: {e}")))?;
// Wait for the box to open the port (or refuse).
let deadline = Instant::now() + CONNECT_TIMEOUT;
let mut uart = Uart {
socket: Some(socket),
rx,
netname,
device_path: String::new(),
baudrate: 0,
buf: Vec::new(),
last_status: None,
};
loop {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(Error::Timeout(format!(
"box did not confirm UART session for '{}' within {CONNECT_TIMEOUT:?}",
uart.netname
)));
}
match uart.rx.recv_timeout(remaining) {
Ok(UartEvent::Connected {
device_path,
baudrate,
}) => {
uart.device_path = device_path;
uart.baudrate = baudrate;
return Ok(uart);
}
// Data can beat the connected event; keep it.
Ok(UartEvent::Data(bytes)) => uart.buf.extend_from_slice(&bytes),
Ok(UartEvent::Error(msg)) => return Err(Error::Stream(msg)),
Ok(_) => {}
Err(RecvTimeoutError::Timeout) => {}
Err(RecvTimeoutError::Disconnected) => {
return Err(Error::Stream("UART session closed unexpectedly".to_string()))
}
}
}
}
/// Name of the net this session streams.
pub fn netname(&self) -> &str {
&self.netname
}
/// Device path on the box (e.g. `/dev/ttyUSB0`).
pub fn device_path(&self) -> &str {
&self.device_path
}
/// Baud rate the port was opened at.
pub fn baudrate(&self) -> u64 {
self.baudrate
}
/// The most recent session status notification from the box, e.g.
/// `"reconnecting"` / `"reconnected"` while the adapter re-enumerates
/// (hub power-cycle, DUT reflash). Updated during reads.
pub fn last_status(&self) -> Option<&str> {
self.last_status.as_deref()
}
/// Write raw bytes to the device.
pub fn write(&self, data: &[u8]) -> Result<()> {
let socket = self
.socket
.as_ref()
.ok_or_else(|| Error::Stream("UART session already stopped".to_string()))?;
socket
.emit("uart_write", json!({ "data": hex_encode(data) }))
.map_err(|e| Error::Stream(format!("UART write failed: {e}")))
}
/// Write a string to the device.
pub fn write_str(&self, s: &str) -> Result<()> {
self.write(s.as_bytes())
}
/// Drain incoming events into the internal buffer. Returns an error if
/// the box reported a session error.
fn pump(&mut self, wait: Duration) -> Result<()> {
let mut wait = wait;
loop {
match self.rx.recv_timeout(wait) {
Ok(UartEvent::Data(bytes)) => {
self.buf.extend_from_slice(&bytes);
// Something arrived: keep draining whatever is already
// queued without waiting again.
wait = Duration::ZERO;
}
Ok(UartEvent::Error(msg)) => return Err(Error::Stream(msg)),
Ok(UartEvent::Status(status)) => self.last_status = Some(status),
Ok(_) => {}
Err(RecvTimeoutError::Timeout) => return Ok(()),
Err(RecvTimeoutError::Disconnected) => {
return Err(Error::Stream("UART session closed unexpectedly".to_string()))
}
}
}
}
/// Return whatever bytes arrive within `timeout`. May return an empty
/// vector if the device was quiet.
///
/// Note this waits out the **full** `timeout` when the device stays
/// idle (data that does arrive is still drained without further
/// waiting). Poll loops that only want what has already arrived should
/// use [`Uart::try_read`] instead, which never blocks.
pub fn read(&mut self, timeout: Duration) -> Result<Vec<u8>> {
self.pump(timeout)?;
Ok(std::mem::take(&mut self.buf))
}
/// Return the bytes already received, without waiting.
///
/// Drains everything the session has queued (and anything buffered by
/// a previous [`Uart::wait_for`]) and returns immediately — an empty
/// vector when the device has been quiet. This is the non-blocking
/// complement to [`Uart::read`] for poll loops.
pub fn try_read(&mut self) -> Result<Vec<u8>> {
self.pump(Duration::ZERO)?;
Ok(std::mem::take(&mut self.buf))
}
/// Accumulate output until `needle` appears or `timeout` elapses.
///
/// On success returns everything received up to and including `needle`;
/// bytes after the needle stay buffered for the next read.
pub fn wait_for(&mut self, needle: &[u8], timeout: Duration) -> Result<Vec<u8>> {
if needle.is_empty() {
return Ok(Vec::new());
}
let deadline = Instant::now() + timeout;
loop {
if let Some(pos) = self
.buf
.windows(needle.len())
.position(|w| w == needle)
{
let mut rest = self.buf.split_off(pos + needle.len());
std::mem::swap(&mut self.buf, &mut rest);
return Ok(rest);
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(Error::Timeout(format!(
"'{}' did not appear on UART '{}' within {timeout:?}",
String::from_utf8_lossy(needle),
self.netname
)));
}
self.pump(remaining)?;
}
}
/// Stop the session cleanly (tells the box to close the port).
pub fn stop(mut self) -> Result<()> {
self.shutdown();
Ok(())
}
fn shutdown(&mut self) {
if let Some(socket) = self.socket.take() {
let _ = socket.emit("stop_uart", json!({}));
let _ = socket.disconnect();
}
}
}
impl Drop for Uart {
fn drop(&mut self) {
self.shutdown();
}
}