laser-dac 0.14.1

Unified laser DAC abstraction supporting multiple protocols
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Ether Dream DAC streaming interface.

use super::{Addressed, ProtocolError};
use crate::protocols::ether_dream::protocol::{
    self, Command, ReadBytes, SizeBytes, WriteBytes, WriteToBytes,
};
use std::borrow::Cow;
use std::error::Error;
use std::io::{self, BufReader, Read, Write};
use std::{fmt, mem, net, ops, time};

/// A bi-directional communication stream between the user and a `Dac`.
pub struct Stream {
    dac: Addressed,
    tcp_reader: BufReader<net::TcpStream>,
    tcp_writer: net::TcpStream,
    command_buffer: Vec<QueuedCommand>,
    point_buffer: Vec<protocol::DacPoint>,
    bytes: Vec<u8>,
    /// When the command whose reply carried the current status was sent.
    status_sent_at: time::Instant,
    /// When the reply that carried the current status was read.
    status_received_at: time::Instant,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum QueuedCommand {
    PrepareStream,
    Begin(protocol::command::Begin),
    Update(protocol::command::Update),
    PointRate(protocol::command::PointRate),
    Data(ops::Range<usize>),
    Stop,
    EmergencyStop,
    ClearEmergencyStop,
    Ping,
}

impl QueuedCommand {
    /// The point rate this command sends, if it carries one.
    fn point_rate(&self) -> Option<u32> {
        match self {
            QueuedCommand::Begin(b) => Some(b.point_rate),
            QueuedCommand::Update(u) => Some(u.point_rate),
            QueuedCommand::PointRate(r) => Some(r.0),
            _ => None,
        }
    }
}

pub struct CommandQueue<'a> {
    stream: &'a mut Stream,
}

#[derive(Debug)]
pub enum CommunicationError {
    Io(io::Error),
    Protocol(ProtocolError),
    Response(ResponseError),
}

#[derive(Debug)]
pub struct ResponseError {
    pub response: protocol::DacResponse,
    pub kind: ResponseErrorKind,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ResponseErrorKind {
    UnexpectedCommand(u8),
    Nak(Nak),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Nak {
    Full,
    Invalid,
    StopCondition,
}

impl Stream {
    fn send_command<C>(&mut self, command: C) -> io::Result<()>
    where
        C: Command + WriteToBytes,
    {
        send_command(&mut self.bytes, &mut self.tcp_writer, command)
    }

    fn recv_response(&mut self, expected_command: u8) -> Result<(), CommunicationError> {
        recv_response_buffered(
            &mut self.bytes,
            &mut self.tcp_reader,
            &mut self.dac,
            expected_command,
        )
    }

    pub fn dac(&self) -> &Addressed {
        &self.dac
    }

    /// When the command that produced the current [`dac`](Self::dac) status
    /// was sent; for the connection hello, when the connection was opened.
    ///
    /// The firmware samples its status while it processes a command, so this
    /// is a better anchor for decaying the reported fullness than the time
    /// the reply arrived. A delayed reply otherwise makes the ring look
    /// fuller than it is. Erring early is the conservative direction.
    pub fn status_sent_at(&self) -> time::Instant {
        self.status_sent_at
    }

    /// When the reply that produced the current [`dac`](Self::dac) status was
    /// read from the socket; for the connection hello, when it was read.
    ///
    /// The firmware sampled the status no later than this, so decaying the
    /// reported fullness from here never under-reads it. Use it where
    /// over-reading is the safe direction, such as deciding whether a chunk
    /// fits. [`status_sent_at`](Self::status_sent_at) is the matching lower
    /// bound.
    pub fn status_received_at(&self) -> time::Instant {
        self.status_received_at
    }

    /// Ask the firmware for its build string with the `'v'` command.
    ///
    /// Send this only when no other command is in flight. Firmware that does
    /// not implement `'v'` may reset the connection, which surfaces as an I/O
    /// error; the stream is unusable afterwards and must be reconnected.
    ///
    /// Returns `Ok(None)` if the firmware answered with a normal NAK frame
    /// instead of a build string.
    pub fn query_version(&mut self) -> Result<Option<String>, CommunicationError> {
        const V: u8 = protocol::command::Version::START_BYTE;
        let sent_at = time::Instant::now();
        self.send_command(protocol::command::Version)?;
        let mut raw = [0u8; protocol::command::Version::RESPONSE_SIZE_BYTES];
        let head = protocol::DacResponse::SIZE_BYTES;
        read_exact_with_budget(&mut self.tcp_reader, &mut raw[..head], VERSION_BUDGET)?;
        // A NAK frame for 'v' starts with a response code then 'v'. No real
        // build string starts that way.
        if Nak::from_protocol(raw[0]).is_some() && raw[1] == V {
            let response = (&raw[..head]).read_bytes::<protocol::DacResponse>()?;
            self.dac.update_status(&response.dac_status)?;
            self.status_sent_at = sent_at;
            self.status_received_at = time::Instant::now();
            return Ok(None);
        }
        read_exact_with_budget(&mut self.tcp_reader, &mut raw[head..], VERSION_BUDGET)?;
        Ok(Some(protocol::command::Version::decode_response(&raw)))
    }

    /// Address of the connected DAC.
    pub fn peer_addr(&self) -> io::Result<net::SocketAddr> {
        self.tcp_writer.peer_addr()
    }

    pub fn queue_commands(&mut self) -> CommandQueue<'_> {
        self.command_buffer.clear();
        self.point_buffer.clear();
        CommandQueue { stream: self }
    }

    pub fn set_nodelay(&self, b: bool) -> io::Result<()> {
        self.tcp_writer.set_nodelay(b)
    }

    pub fn nodelay(&self) -> io::Result<bool> {
        self.tcp_writer.nodelay()
    }

    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
        self.tcp_writer.set_ttl(ttl)
    }

    pub fn ttl(&self) -> io::Result<u32> {
        self.tcp_writer.ttl()
    }

    pub fn set_read_timeout(&self, duration: Option<time::Duration>) -> io::Result<()> {
        self.tcp_reader.get_ref().set_read_timeout(duration)
    }

    pub fn set_write_timeout(&self, duration: Option<time::Duration>) -> io::Result<()> {
        self.tcp_writer.set_write_timeout(duration)
    }

    pub fn set_timeout(&self, duration: Option<time::Duration>) -> io::Result<()> {
        self.set_read_timeout(duration)?;
        self.set_write_timeout(duration)
    }
}

impl<'a> CommandQueue<'a> {
    pub fn prepare_stream(self) -> Self {
        self.stream
            .command_buffer
            .push(QueuedCommand::PrepareStream);
        self
    }

    pub fn begin(self, low_water_mark: u16, point_rate: u32) -> Self {
        let begin = protocol::command::Begin {
            low_water_mark,
            point_rate,
        };
        self.stream.command_buffer.push(QueuedCommand::Begin(begin));
        self
    }

    pub fn update(self, low_water_mark: u16, point_rate: u32) -> Self {
        let update = protocol::command::Update {
            low_water_mark,
            point_rate,
        };
        self.stream
            .command_buffer
            .push(QueuedCommand::Update(update));
        self
    }

    pub fn point_rate(self, point_rate: u32) -> Self {
        let point_rate = protocol::command::PointRate(point_rate);
        self.stream
            .command_buffer
            .push(QueuedCommand::PointRate(point_rate));
        self
    }

    pub fn data<I>(self, points: I) -> Self
    where
        I: IntoIterator<Item = protocol::DacPoint>,
    {
        let start = self.stream.point_buffer.len();
        self.stream.point_buffer.extend(points);
        let mut end = self.stream.point_buffer.len();
        // A single Data command addresses at most u16::MAX points. Drop any
        // excess rather than panicking on the output thread; the write path
        // enforces the same bound. In practice chunks are capped far below this.
        if end - start > u16::MAX as usize {
            log::warn!(
                "Ether Dream: data chunk of {} points exceeds {}, truncating",
                end - start,
                u16::MAX
            );
            end = start + u16::MAX as usize;
            self.stream.point_buffer.truncate(end);
        }
        self.stream
            .command_buffer
            .push(QueuedCommand::Data(start..end));
        self
    }

    pub fn stop(self) -> Self {
        self.stream.command_buffer.push(QueuedCommand::Stop);
        self
    }

    pub fn emergency_stop(self) -> Self {
        self.stream
            .command_buffer
            .push(QueuedCommand::EmergencyStop);
        self
    }

    pub fn clear_emergency_stop(self) -> Self {
        self.stream
            .command_buffer
            .push(QueuedCommand::ClearEmergencyStop);
        self
    }

    pub fn ping(self) -> Self {
        self.stream.command_buffer.push(QueuedCommand::Ping);
        self
    }

    /// Send every queued command, then read one response per command.
    ///
    /// Every response is read even after a NAK, and the first NAK is
    /// returned, so the stream stays in sync for the next submit.
    ///
    /// A `'b'`, `'u'` or `'q'` whose rate is 0, or above the DAC's advertised
    /// `max_point_rate` (when it advertises one), is rejected with an
    /// [`io::ErrorKind::InvalidInput`] error before anything is sent. Rate 0
    /// hangs Ether Dream firmware until a power cycle. An over-max rate is
    /// NAKed after only the opcode byte is consumed, and the firmware then
    /// parses the argument bytes as further commands (a low-water mark of 0
    /// becomes two emergency stops).
    pub fn submit(self) -> Result<(), CommunicationError> {
        let CommandQueue { stream } = self;

        let max_rate = stream.dac().max_point_rate;
        let unsafe_rate = |rate: u32| rate == 0 || (max_rate > 0 && rate > max_rate);
        if let Some(rate) = stream
            .command_buffer
            .iter()
            .filter_map(QueuedCommand::point_rate)
            .find(|&rate| unsafe_rate(rate))
        {
            stream.command_buffer.clear();
            stream.point_buffer.clear();
            return Err(CommunicationError::Io(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!("point rate {rate} is outside 1..={max_rate} pps"),
            )));
        }

        let mut command_bytes = vec![];
        let mut command_buffer = mem::take(&mut stream.command_buffer);

        let mut sent_at = Vec::with_capacity(command_buffer.len());
        for command in command_buffer.drain(..) {
            sent_at.push(time::Instant::now());
            let start_byte = match command {
                QueuedCommand::PrepareStream => {
                    stream.send_command(protocol::command::PrepareStream)?;
                    protocol::command::PrepareStream::START_BYTE
                }
                QueuedCommand::Begin(begin) => {
                    stream.send_command(begin)?;
                    protocol::command::Begin::START_BYTE
                }
                QueuedCommand::Update(update) => {
                    stream.send_command(update)?;
                    protocol::command::Update::START_BYTE
                }
                QueuedCommand::PointRate(point_rate) => {
                    stream.send_command(point_rate)?;
                    protocol::command::PointRate::START_BYTE
                }
                QueuedCommand::Data(range) => {
                    let points = Cow::Borrowed(&stream.point_buffer[range]);
                    let data = protocol::command::Data { points };
                    send_command(&mut stream.bytes, &mut stream.tcp_writer, data)?;
                    protocol::command::Data::START_BYTE
                }
                QueuedCommand::Stop => {
                    stream.send_command(protocol::command::Stop)?;
                    protocol::command::Stop::START_BYTE
                }
                QueuedCommand::EmergencyStop => {
                    stream.send_command(protocol::command::EmergencyStop)?;
                    protocol::command::EmergencyStop::START_BYTE
                }
                QueuedCommand::ClearEmergencyStop => {
                    stream.send_command(protocol::command::ClearEmergencyStop)?;
                    protocol::command::ClearEmergencyStop::START_BYTE
                }
                QueuedCommand::Ping => {
                    stream.send_command(protocol::command::Ping)?;
                    protocol::command::Ping::START_BYTE
                }
            };
            command_bytes.push(start_byte);
        }

        mem::swap(&mut stream.command_buffer, &mut command_buffer);

        // Read every response even after a NAK. Returning at the first NAK
        // would leave the remaining responses queued in the socket, and the
        // next submit would read them as its own.
        let mut first_nak = None;
        for (command_byte, sent_at) in command_bytes.into_iter().zip(sent_at) {
            let result = stream.recv_response(command_byte);
            let received_at = time::Instant::now();
            match result {
                Ok(()) => {
                    stream.status_sent_at = sent_at;
                    stream.status_received_at = received_at;
                }
                Err(CommunicationError::Response(e))
                    if matches!(e.kind, ResponseErrorKind::Nak(_)) =>
                {
                    stream.status_sent_at = sent_at;
                    stream.status_received_at = received_at;
                    first_nak.get_or_insert(CommunicationError::Response(e));
                }
                Err(e) => return Err(e),
            }
        }

        match first_nak {
            Some(e) => Err(e),
            None => Ok(()),
        }
    }
}

impl protocol::DacResponse {
    fn check_errors(&self, expected_command: u8) -> Result<(), ResponseError> {
        if self.command != expected_command {
            return Err(ResponseError {
                response: *self,
                kind: ResponseErrorKind::UnexpectedCommand(self.command),
            });
        }

        if let Some(nak) = Nak::from_protocol(self.response) {
            return Err(ResponseError {
                response: *self,
                kind: ResponseErrorKind::Nak(nak),
            });
        }

        Ok(())
    }
}

impl Nak {
    pub fn from_protocol(nak: u8) -> Option<Self> {
        Some(match nak {
            protocol::DacResponse::NAK_FULL => Nak::Full,
            protocol::DacResponse::NAK_INVALID => Nak::Invalid,
            protocol::DacResponse::NAK_STOP_CONDITION => Nak::StopCondition,
            _ => return None,
        })
    }

    pub fn to_protocol(&self) -> u8 {
        match *self {
            Nak::Full => protocol::DacResponse::NAK_FULL,
            Nak::Invalid => protocol::DacResponse::NAK_INVALID,
            Nak::StopCondition => protocol::DacResponse::NAK_STOP_CONDITION,
        }
    }
}

impl Error for CommunicationError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            CommunicationError::Io(err) => Some(err),
            CommunicationError::Protocol(err) => Some(err),
            CommunicationError::Response(err) => Some(err),
        }
    }
}

impl Error for ResponseError {}

impl fmt::Display for CommunicationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CommunicationError::Io(err) => err.fmt(f),
            CommunicationError::Protocol(err) => err.fmt(f),
            CommunicationError::Response(err) => err.fmt(f),
        }
    }
}

impl fmt::Display for ResponseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.kind {
            ResponseErrorKind::UnexpectedCommand(_) => {
                write!(f, "received response to unexpected command")
            }
            ResponseErrorKind::Nak(nak) => match nak {
                Nak::Full => write!(f, "DAC responded with \"NAK - Full\""),
                Nak::Invalid => write!(f, "DAC responded with \"NAK - Invalid\""),
                Nak::StopCondition => write!(f, "DAC responded with \"NAK - Stop Condition\""),
            },
        }
    }
}

impl From<io::Error> for CommunicationError {
    fn from(err: io::Error) -> Self {
        CommunicationError::Io(err)
    }
}

impl From<ProtocolError> for CommunicationError {
    fn from(err: ProtocolError) -> Self {
        CommunicationError::Protocol(err)
    }
}

impl From<ResponseError> for CommunicationError {
    fn from(err: ResponseError) -> Self {
        CommunicationError::Response(err)
    }
}

/// Per-`read` timeout. A single response may span several of these if the DAC
/// is briefly slow; see [`READ_BUDGET`].
const READ_TIMEOUT: time::Duration = time::Duration::from_millis(500);

/// Total time budget for reading one complete response before giving up. A
/// Wi-Fi latency spike should be ridden out (retrying the in-progress read)
/// rather than restarting the whole stream.
const READ_BUDGET: time::Duration = time::Duration::from_secs(2);

/// Budget for the `'v'` reply. Kept short: firmware that ignores `'v'`
/// should not stall a connect for the full [`READ_BUDGET`].
const VERSION_BUDGET: time::Duration = time::Duration::from_millis(500);

/// Write timeout — bounds how long a hung DAC can wedge `write_all`.
const WRITE_TIMEOUT: time::Duration = time::Duration::from_secs(2);

/// Establishes a TCP stream connection with the DAC at the given address.
pub fn connect(
    broadcast: &protocol::DacBroadcast,
    dac_ip: net::IpAddr,
) -> Result<Stream, CommunicationError> {
    let addr = net::SocketAddr::new(dac_ip, protocol::COMMUNICATION_PORT);
    connect_inner(broadcast, addr, &net::TcpStream::connect)
}

/// Connect to a DAC by socket address, without needing its UDP broadcast.
///
/// Use this when broadcasts cannot reach the host (different subnet, a
/// firewall, or link-local setups where they are simply not seen) or to reach
/// a DAC on a non-standard port, such as a simulator.
///
/// When `broadcast` is `None` the DAC's identity is synthesised: the MAC
/// address and revisions are reported as zero, and the buffer capacity and
/// maximum point rate fall back to 1799 points, the smallest known ring
/// (ED1), and 100 000 pps, which every known Ether Dream advertises. The
/// live status comes from the hello
/// frame the DAC sends on connect either way.
pub fn connect_to(
    addr: net::SocketAddr,
    broadcast: Option<&protocol::DacBroadcast>,
    timeout: time::Duration,
) -> Result<Stream, CommunicationError> {
    let fallback;
    let broadcast = match broadcast {
        Some(b) => b,
        None => {
            fallback = synthetic_broadcast();
            &fallback
        }
    };
    let connect = |addr| net::TcpStream::connect_timeout(&addr, timeout);
    connect_inner(broadcast, addr, &connect)
}

/// Broadcast used by [`connect_to`] when none was received.
pub(crate) fn synthetic_broadcast() -> protocol::DacBroadcast {
    protocol::DacBroadcast {
        mac_address: [0; 6],
        hw_revision: 0,
        sw_revision: 0,
        buffer_capacity: SYNTHETIC_BUFFER_CAPACITY,
        max_point_rate: SYNTHETIC_MAX_POINT_RATE,
        dac_status: protocol::DacStatus {
            protocol: 0,
            light_engine_state: protocol::DacStatus::LIGHT_ENGINE_READY,
            playback_state: protocol::DacStatus::PLAYBACK_IDLE,
            source: protocol::DacStatus::SOURCE_NETWORK_STREAMING,
            light_engine_flags: 0,
            playback_flags: 0,
            source_flags: 0,
            buffer_fullness: 0,
            point_rate: 0,
            point_count: 0,
        },
    }
}

/// Capacity assumed when connecting without a broadcast.
pub(crate) const SYNTHETIC_BUFFER_CAPACITY: u16 = 1799;
/// Max point rate assumed when connecting without a broadcast.
pub(crate) const SYNTHETIC_MAX_POINT_RATE: u32 = 100_000;

/// Establishes a TCP stream connection with a timeout.
pub fn connect_timeout(
    broadcast: &protocol::DacBroadcast,
    dac_ip: net::IpAddr,
    timeout: time::Duration,
) -> Result<Stream, CommunicationError> {
    let connect = |addr| net::TcpStream::connect_timeout(&addr, timeout);
    let addr = net::SocketAddr::new(dac_ip, protocol::COMMUNICATION_PORT);
    connect_inner(broadcast, addr, &connect)
}

fn connect_inner(
    broadcast: &protocol::DacBroadcast,
    dac_addr: net::SocketAddr,
    connect: &dyn Fn(net::SocketAddr) -> io::Result<net::TcpStream>,
) -> Result<Stream, CommunicationError> {
    let mut dac = Addressed::from_broadcast(broadcast)?;

    let opened_at = time::Instant::now();
    let tcp_stream = connect(dac_addr)?;

    tcp_stream.set_nodelay(true)?;

    // Read timeout prevents blocking forever on a dead DAC. On a timeout the
    // in-progress read is retried (preserving bytes already consumed) up to
    // `READ_BUDGET` before failing — see `read_exact_with_budget`.
    tcp_stream.set_read_timeout(Some(READ_TIMEOUT))?;
    // Write timeout prevents a hung DAC from wedging the session thread forever
    // inside `write_all`.
    tcp_stream.set_write_timeout(Some(WRITE_TIMEOUT))?;

    let tcp_writer = tcp_stream.try_clone()?;
    let mut tcp_reader = BufReader::new(tcp_stream);

    let mut bytes = vec![];

    recv_response_buffered(
        &mut bytes,
        &mut tcp_reader,
        &mut dac,
        protocol::command::Ping::START_BYTE,
    )?;
    let hello_received_at = time::Instant::now();

    Ok(Stream {
        dac,
        tcp_reader,
        tcp_writer,
        command_buffer: vec![],
        point_buffer: vec![],
        bytes,
        status_sent_at: opened_at,
        status_received_at: hello_received_at,
    })
}

fn send_command<C>(
    bytes: &mut Vec<u8>,
    tcp_stream: &mut net::TcpStream,
    command: C,
) -> io::Result<()>
where
    C: Command + WriteToBytes,
{
    bytes.clear();
    bytes.write_bytes(command)?;
    tcp_stream.write_all(bytes)?;
    Ok(())
}

fn recv_response_buffered(
    bytes: &mut Vec<u8>,
    tcp_reader: &mut BufReader<net::TcpStream>,
    dac: &mut Addressed,
    expected_command: u8,
) -> Result<(), CommunicationError> {
    const MAX_RETRIES: usize = 5;

    for _ in 0..=MAX_RETRIES {
        bytes.resize(protocol::DacResponse::SIZE_BYTES, 0);
        read_exact_with_budget(tcp_reader, bytes, READ_BUDGET)?;
        let response = (&bytes[..]).read_bytes::<protocol::DacResponse>()?;

        // Always update status from every response, even mismatched ones.
        dac.update_status(&response.dac_status)?;

        if response.command == expected_command {
            response.check_errors(expected_command)?;
            return Ok(());
        }

        // Command mismatch — check if it's a stale/unsolicited frame we can skip.
        // ACK mismatch: stale pipeline response from a previous command batch.
        // NAK_INVALID ('I') mismatch: unsolicited status frame from the DAC.
        if response.response == protocol::DacResponse::ACK
            || response.response == protocol::DacResponse::NAK_INVALID
        {
            log::debug!(
                "ignoring unsolicited response (got command 0x{:02X}, expected 0x{:02X}, response 0x{:02X})",
                response.command,
                expected_command,
                response.response,
            );
            continue;
        }

        // Non-ACK, non-status mismatch — real error.
        return Err(CommunicationError::Response(ResponseError {
            response,
            kind: ResponseErrorKind::UnexpectedCommand(response.command),
        }));
    }

    Err(CommunicationError::Io(io::Error::other(
        "too many unsolicited responses",
    )))
}

/// Fill `buf` completely, retrying reads that time out (or are interrupted)
/// while tracking bytes already consumed, so a per-read timeout never desyncs
/// the stream by dropping a partial response. Fails once `budget` elapses.
fn read_exact_with_budget<R: Read>(
    reader: &mut R,
    buf: &mut [u8],
    budget: time::Duration,
) -> io::Result<()> {
    let start = time::Instant::now();
    let mut filled = 0;
    while filled < buf.len() {
        match reader.read(&mut buf[filled..]) {
            Ok(0) => {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "connection closed mid-response",
                ));
            }
            Ok(n) => filled += n,
            Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(ref e)
                if e.kind() == io::ErrorKind::WouldBlock || e.kind() == io::ErrorKind::TimedOut =>
            {
                // Partial data (if any) is retained in `buf[..filled]`; keep
                // waiting for the rest until the overall budget is exhausted.
                if start.elapsed() >= budget {
                    return Err(io::Error::new(
                        io::ErrorKind::TimedOut,
                        "timed out reading response",
                    ));
                }
            }
            Err(e) => return Err(e),
        }
    }
    Ok(())
}