lager-net 0.2.0

First-class Rust client for Lager box nets: drive power supplies, batteries, e-loads, GPIO, ADC, DAC, SPI, I2C, UART and more from cargo tests.
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Debug-probe nets (J-Link / OpenOCD): flash, erase, reset, memory reads,
//! and RTT log streaming.
//!
//! Unlike the instrument nets, debug talks to the box's dedicated **debug
//! service on port 8765** (published on the box host), not the port-9000
//! server. A [`crate::LagerBox`] transparently reaches both: the debug
//! service lives on the same host, and the crate resolves the debug net's
//! full saved record from `:9000/nets/list` to hand to the service.
//!
//! ```no_run
//! # #[cfg(feature = "blocking")]
//! # fn demo() -> lager::Result<()> {
//! use lager::LagerBox;
//!
//! let lager = LagerBox::from_env()?;
//! let debug = lager.debug("debug1");
//!
//! debug.connect()?;
//! debug.erase()?;
//! debug.flash("firmware.hex")?;
//! debug.reset(false)?;
//! let vector_table = debug.read_memory(0x0800_0000, 16)?;
//! println!("{vector_table:02x?}");
//! # Ok(())
//! # }
//! # fn main() {}
//! ```

use std::path::Path;
use std::time::Duration;

use serde_json::{json, Value};

use crate::error::{Error, Result};
use crate::wire::{self, DebugConnection, DebugInfo, DebugStatus, Timeout};

/// Base64-encode bytes with the standard alphabet (with padding). Kept
/// in-crate so the debug feature adds no extra dependency.
fn base64_encode(input: &[u8]) -> String {
    const ALPHABET: &[u8; 64] =
        b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
    for chunk in input.chunks(3) {
        let b0 = chunk[0] as u32;
        let b1 = *chunk.get(1).unwrap_or(&0) as u32;
        let b2 = *chunk.get(2).unwrap_or(&0) as u32;
        let n = (b0 << 16) | (b1 << 8) | b2;
        out.push(ALPHABET[(n >> 18 & 0x3F) as usize] as char);
        out.push(ALPHABET[(n >> 12 & 0x3F) as usize] as char);
        out.push(if chunk.len() > 1 {
            ALPHABET[(n >> 6 & 0x3F) as usize] as char
        } else {
            '='
        });
        out.push(if chunk.len() > 2 {
            ALPHABET[(n & 0x3F) as usize] as char
        } else {
            '='
        });
    }
    out
}

/// Firmware image kind, inferred from the file extension by
/// [`DebugNet::flash`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FirmwareKind {
    /// Intel HEX (`.hex`).
    Hex,
    /// ELF (`.elf`).
    Elf,
    /// Raw binary (`.bin`), flashed at a base address.
    Bin,
}

impl FirmwareKind {
    fn from_path(path: &Path) -> Result<Self> {
        match path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase) {
            Some(ext) if ext == "hex" => Ok(FirmwareKind::Hex),
            Some(ext) if ext == "elf" => Ok(FirmwareKind::Elf),
            Some(ext) if ext == "bin" => Ok(FirmwareKind::Bin),
            _ => Err(Error::Config(format!(
                "cannot infer firmware type from '{}'; use flash_with to set it explicitly",
                path.display()
            ))),
        }
    }
}

/// Options for [`DebugNet::connect_with`].
#[derive(Debug, Clone)]
pub struct ConnectOptions {
    /// SWD/JTAG speed (e.g. `"4000"` kHz or `"adaptive"`). `None` uses the
    /// service default (`adaptive`).
    pub speed: Option<String>,
    /// Force a fresh backend start even if one is already running.
    pub force: bool,
    /// Halt the target immediately after connecting.
    pub halt: bool,
    /// Start a GDB server (needed for reset/read_memory on some backends).
    pub gdb: bool,
}

impl Default for ConnectOptions {
    fn default() -> Self {
        ConnectOptions {
            speed: None,
            force: false,
            halt: false,
            gdb: true,
        }
    }
}

/// Default per-operation timeouts, matching the Python `DebugServiceClient`.
const CONNECT_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(30));
const FLASH_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(180));
const ERASE_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(120));
const RESET_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(10));
const MEMRD_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(30));
const QUICK_TIMEOUT: Timeout = Timeout::After(Duration::from_secs(10));

/// Options for [`DebugNet::rtt`] / RTT streaming.
#[derive(Debug, Clone, Copy, Default)]
pub struct RttOptions {
    /// RTT channel (0 or 1).
    pub channel: u32,
    /// RAM start address for the RTT control-block search (advanced).
    pub search_addr: Option<u64>,
    /// Size of the RAM region to search, in bytes (advanced).
    pub search_size: Option<u64>,
}

/// Build the JSON body for a debug op: the full net record plus extra params.
pub(crate) fn debug_body(net_record: &Value, extra: Value) -> Value {
    let mut obj = serde_json::Map::new();
    obj.insert("net".to_string(), net_record.clone());
    if let Value::Object(map) = extra {
        obj.extend(map);
    }
    Value::Object(obj)
}

/// Shared request-body builders, used by both the blocking and async handles
/// so the two cannot diverge.
pub(crate) mod ops {
    use super::*;

    pub(crate) fn connect(net: &Value, opts: &ConnectOptions) -> (String, Value, Timeout) {
        let mut extra = json!({
            "force": opts.force,
            "halt": opts.halt,
            "gdb": opts.gdb,
        });
        if let Some(speed) = &opts.speed {
            extra["speed"] = json!(speed);
        }
        ("/debug/connect".into(), debug_body(net, extra), CONNECT_TIMEOUT)
    }

    pub(crate) fn disconnect(net: &Value, keep_running: bool) -> (String, Value, Timeout) {
        (
            "/debug/disconnect".into(),
            debug_body(net, json!({ "keep_jlink_running": keep_running })),
            QUICK_TIMEOUT,
        )
    }

    pub(crate) fn reset(net: &Value, halt: bool) -> (String, Value, Timeout) {
        (
            "/debug/reset".into(),
            debug_body(net, json!({ "halt": halt })),
            RESET_TIMEOUT,
        )
    }

    pub(crate) fn erase(net: &Value) -> (String, Value, Timeout) {
        ("/debug/erase".into(), debug_body(net, json!({})), ERASE_TIMEOUT)
    }

    pub(crate) fn read_memory(net: &Value, address: u64, length: usize) -> (String, Value, Timeout) {
        (
            "/debug/memrd".into(),
            debug_body(net, json!({ "start_addr": address, "length": length })),
            MEMRD_TIMEOUT,
        )
    }

    pub(crate) fn info(net: &Value) -> (String, Value, Timeout) {
        ("/debug/info".into(), debug_body(net, json!({})), QUICK_TIMEOUT)
    }

    pub(crate) fn status(net: &Value) -> (String, Value, Timeout) {
        ("/debug/status".into(), debug_body(net, json!({})), QUICK_TIMEOUT)
    }

    /// Build the flash body. `kind` picks the payload field; `address` is
    /// only used for [`FirmwareKind::Bin`].
    pub(crate) fn flash(
        net: &Value,
        contents: &[u8],
        kind: FirmwareKind,
        address: Option<u32>,
    ) -> (String, Value, Timeout) {
        let b64 = base64_encode(contents);
        let payload = match kind {
            FirmwareKind::Hex => json!({ "hexfile": { "content": b64 } }),
            FirmwareKind::Elf => json!({ "elffile": { "content": b64 } }),
            FirmwareKind::Bin => json!({
                "binfile": { "content": b64, "address": address.unwrap_or(0x0800_0000) }
            }),
        };
        ("/debug/flash".into(), debug_body(net, payload), FLASH_TIMEOUT)
    }

    /// Build the RTT body. RTT streaming is blocking-only (the async client
    /// exposes no `rtt()`), so this is unused in async-only builds.
    #[cfg(feature = "blocking")]
    pub(crate) fn rtt_body(net: &Value, opts: &RttOptions) -> Value {
        let mut extra = json!({ "channel": opts.channel, "timeout": Value::Null });
        if let Some(a) = opts.search_addr {
            extra["search_addr"] = json!(a);
        }
        if let Some(s) = opts.search_size {
            extra["search_size"] = json!(s);
        }
        debug_body(net, extra)
    }
}

/// Find the saved record for a debug net by name in a raw nets list.
pub(crate) fn find_debug_record(records: Vec<Value>, name: &str) -> Result<Value> {
    let mut wrong_role = false;
    for rec in records {
        if rec.get("name").and_then(Value::as_str) == Some(name) {
            if rec.get("role").and_then(Value::as_str) == Some("debug") {
                return Ok(rec);
            }
            wrong_role = true;
        }
    }
    Err(Error::Box {
        status: 404,
        message: if wrong_role {
            format!("net '{name}' exists but is not a debug net")
        } else {
            format!("debug net '{name}' not found on this box")
        },
    })
}

/// Read a firmware file and infer its kind from the extension.
pub(crate) fn read_firmware(path: &Path) -> Result<(Vec<u8>, FirmwareKind)> {
    let kind = FirmwareKind::from_path(path)?;
    let bytes = std::fs::read(path)
        .map_err(|e| Error::Config(format!("cannot read firmware '{}': {e}", path.display())))?;
    Ok((bytes, kind))
}

// ---------------------------------------------------------------------------
// Blocking handle
// ---------------------------------------------------------------------------

/// Handle for a debug-probe net (blocking).
///
/// Created via [`crate::LagerBox::debug`]. Cheap to construct; the net's
/// saved record is fetched from the box on first use.
#[cfg(feature = "blocking")]
#[derive(Clone)]
pub struct DebugNet<'a> {
    pub(crate) client: &'a crate::client::LagerBox,
    pub(crate) name: String,
}

#[cfg(feature = "blocking")]
impl DebugNet<'_> {
    /// Name of the net this handle drives.
    pub fn name(&self) -> &str {
        &self.name
    }

    fn net_record(&self) -> Result<Value> {
        self.client.debug_net_record(&self.name)
    }

    fn call(&self, path: &str, body: Value, timeout: Timeout) -> Result<Value> {
        let req = wire::debug_request(path, body, timeout);
        let (status, resp) = self.client.execute_debug(&req)?;
        wire::parse_debug(status, resp)
    }

    /// Connect to the probe with default options (starts a GDB server).
    pub fn connect(&self) -> Result<DebugConnection> {
        self.connect_with(&ConnectOptions::default())
    }

    /// Connect to the probe with explicit options.
    pub fn connect_with(&self, opts: &ConnectOptions) -> Result<DebugConnection> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::connect(&net, opts);
        let resp = self.call(&path, body, timeout)?;
        serde_json::from_value(resp).map_err(Into::into)
    }

    /// Disconnect. If `keep_running` is true the gdbserver is left running so
    /// an external GDB client can stay attached.
    pub fn disconnect(&self, keep_running: bool) -> Result<()> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::disconnect(&net, keep_running);
        self.call(&path, body, timeout).map(|_| ())
    }

    /// Reset the target, optionally halting at the reset vector.
    pub fn reset(&self, halt: bool) -> Result<()> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::reset(&net, halt);
        self.call(&path, body, timeout).map(|_| ())
    }

    /// Mass-erase the target flash.
    pub fn erase(&self) -> Result<()> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::erase(&net);
        self.call(&path, body, timeout).map(|_| ())
    }

    /// Flash a firmware file, inferring the type from its extension
    /// (`.hex`, `.elf`, `.bin`). `.bin` is flashed at `0x08000000`; use
    /// [`DebugNet::flash_bin`] to choose the address.
    pub fn flash(&self, firmware_path: impl AsRef<Path>) -> Result<()> {
        let path = firmware_path.as_ref();
        let (contents, kind) = read_firmware(path)?;
        self.flash_bytes(&contents, kind, None)
    }

    /// Flash a raw binary at an explicit base address.
    pub fn flash_bin(&self, firmware_path: impl AsRef<Path>, address: u32) -> Result<()> {
        let contents = std::fs::read(firmware_path.as_ref())
            .map_err(|e| Error::Config(format!("cannot read firmware: {e}")))?;
        self.flash_bytes(&contents, FirmwareKind::Bin, Some(address))
    }

    /// Flash raw firmware bytes of a known kind.
    pub fn flash_bytes(
        &self,
        contents: &[u8],
        kind: FirmwareKind,
        address: Option<u32>,
    ) -> Result<()> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::flash(&net, contents, kind, address);
        self.call(&path, body, timeout).map(|_| ())
    }

    /// Read `length` bytes of target memory starting at `address`.
    pub fn read_memory(&self, address: u64, length: usize) -> Result<Vec<u8>> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::read_memory(&net, address, length);
        let resp = self.call(&path, body, timeout)?;
        wire::debug_memory_bytes(&resp)
    }

    /// Probe/target information (device, arch, backend, connected state).
    pub fn info(&self) -> Result<DebugInfo> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::info(&net);
        let resp = self.call(&path, body, timeout)?;
        serde_json::from_value(resp).map_err(Into::into)
    }

    /// Whether a gdbserver/daemon is currently running for this probe.
    pub fn status(&self) -> Result<DebugStatus> {
        let net = self.net_record()?;
        let (path, body, timeout) = ops::status(&net);
        let resp = self.call(&path, body, timeout)?;
        serde_json::from_value(resp).map_err(Into::into)
    }

    /// Open an RTT log stream on channel 0.
    ///
    /// Returns a reader that yields the target's RTT output as raw bytes
    /// until the connection closes or the reader is dropped. Wrap it in a
    /// [`std::io::BufReader`] to read lines.
    pub fn rtt(&self) -> Result<RttStream> {
        self.rtt_with(&RttOptions::default())
    }

    /// Open an RTT log stream with explicit options.
    pub fn rtt_with(&self, opts: &RttOptions) -> Result<RttStream> {
        let net = self.net_record()?;
        let body = ops::rtt_body(&net, opts);
        let req = wire::debug_request("/debug/rtt", body, Timeout::Unbounded);
        let reader = self.client.stream_debug(&req)?;
        Ok(RttStream { reader })
    }
}

/// A live RTT byte stream (blocking). Implements [`std::io::Read`], so it can
/// be wrapped in a [`std::io::BufReader`] and read line by line.
#[cfg(feature = "blocking")]
pub struct RttStream {
    reader: Box<dyn std::io::Read + Send + Sync>,
}

#[cfg(feature = "blocking")]
impl std::io::Read for RttStream {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.reader.read(buf)
    }
}

// ---------------------------------------------------------------------------
// Async handle
// ---------------------------------------------------------------------------

/// Handle for a debug-probe net (async).
///
/// RTT streaming is not provided on the async client yet; use the blocking
/// [`DebugNet::rtt`] for log streaming.
#[cfg(feature = "async")]
#[derive(Clone)]
pub struct AsyncDebugNet<'a> {
    pub(crate) client: &'a crate::async_client::AsyncLagerBox,
    pub(crate) name: String,
}

#[cfg(feature = "async")]
impl AsyncDebugNet<'_> {
    /// Name of the net this handle drives.
    pub fn name(&self) -> &str {
        &self.name
    }

    async fn net_record(&self) -> Result<Value> {
        self.client.debug_net_record(&self.name).await
    }

    async fn call(&self, path: &str, body: Value, timeout: Timeout) -> Result<Value> {
        let req = wire::debug_request(path, body, timeout);
        let (status, resp) = self.client.execute_debug(&req).await?;
        wire::parse_debug(status, resp)
    }

    /// Connect to the probe with default options (starts a GDB server).
    pub async fn connect(&self) -> Result<DebugConnection> {
        self.connect_with(&ConnectOptions::default()).await
    }

    /// Connect to the probe with explicit options.
    pub async fn connect_with(&self, opts: &ConnectOptions) -> Result<DebugConnection> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::connect(&net, opts);
        let resp = self.call(&path, body, timeout).await?;
        serde_json::from_value(resp).map_err(Into::into)
    }

    /// Disconnect. If `keep_running` is true the gdbserver is left running.
    pub async fn disconnect(&self, keep_running: bool) -> Result<()> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::disconnect(&net, keep_running);
        self.call(&path, body, timeout).await.map(|_| ())
    }

    /// Reset the target, optionally halting at the reset vector.
    pub async fn reset(&self, halt: bool) -> Result<()> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::reset(&net, halt);
        self.call(&path, body, timeout).await.map(|_| ())
    }

    /// Mass-erase the target flash.
    pub async fn erase(&self) -> Result<()> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::erase(&net);
        self.call(&path, body, timeout).await.map(|_| ())
    }

    /// Flash a firmware file, inferring type from extension.
    pub async fn flash(&self, firmware_path: impl AsRef<Path>) -> Result<()> {
        let (contents, kind) = read_firmware(firmware_path.as_ref())?;
        self.flash_bytes(&contents, kind, None).await
    }

    /// Flash a raw binary at an explicit base address.
    pub async fn flash_bin(&self, firmware_path: impl AsRef<Path>, address: u32) -> Result<()> {
        let contents = std::fs::read(firmware_path.as_ref())
            .map_err(|e| Error::Config(format!("cannot read firmware: {e}")))?;
        self.flash_bytes(&contents, FirmwareKind::Bin, Some(address)).await
    }

    /// Flash raw firmware bytes of a known kind.
    pub async fn flash_bytes(
        &self,
        contents: &[u8],
        kind: FirmwareKind,
        address: Option<u32>,
    ) -> Result<()> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::flash(&net, contents, kind, address);
        self.call(&path, body, timeout).await.map(|_| ())
    }

    /// Read `length` bytes of target memory starting at `address`.
    pub async fn read_memory(&self, address: u64, length: usize) -> Result<Vec<u8>> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::read_memory(&net, address, length);
        let resp = self.call(&path, body, timeout).await?;
        wire::debug_memory_bytes(&resp)
    }

    /// Probe/target information.
    pub async fn info(&self) -> Result<DebugInfo> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::info(&net);
        let resp = self.call(&path, body, timeout).await?;
        serde_json::from_value(resp).map_err(Into::into)
    }

    /// Whether a gdbserver/daemon is currently running for this probe.
    pub async fn status(&self) -> Result<DebugStatus> {
        let net = self.net_record().await?;
        let (path, body, timeout) = ops::status(&net);
        let resp = self.call(&path, body, timeout).await?;
        serde_json::from_value(resp).map_err(Into::into)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn base64_matches_reference() {
        assert_eq!(base64_encode(b""), "");
        assert_eq!(base64_encode(b"f"), "Zg==");
        assert_eq!(base64_encode(b"fo"), "Zm8=");
        assert_eq!(base64_encode(b"foo"), "Zm9v");
        assert_eq!(base64_encode(b"foob"), "Zm9vYg==");
        assert_eq!(base64_encode(b"fooba"), "Zm9vYmE=");
        assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
        assert_eq!(base64_encode(&[0x00, 0xff, 0x10]), "AP8Q");
    }

    #[test]
    fn firmware_kind_from_extension() {
        assert_eq!(
            FirmwareKind::from_path(Path::new("a/b/fw.hex")).unwrap(),
            FirmwareKind::Hex
        );
        assert_eq!(
            FirmwareKind::from_path(Path::new("FW.ELF")).unwrap(),
            FirmwareKind::Elf
        );
        assert!(FirmwareKind::from_path(Path::new("fw.txt")).is_err());
    }

    #[test]
    fn debug_body_wraps_net_and_params() {
        let net = json!({"name": "debug1", "role": "debug", "pin": "nrf52"});
        let body = debug_body(&net, json!({"halt": true}));
        assert_eq!(body["net"]["name"], "debug1");
        assert_eq!(body["halt"], true);
    }
}