Skip to main content

canopen_rs/sdo/
mod.rs

1//! Service Data Object (SDO) protocol (CiA 301 §7.2.4).
2//!
3//! SDOs provide confirmed, addressed read/write access to any object
4//! dictionary entry. Each transfer step is a client request frame answered by
5//! a server response frame, exchanged on a pair of CAN ids (by default
6//! `0x600 + node` for requests and `0x580 + node` for responses).
7//!
8//! This module implements **expedited** and **segmented** transfer: a value of
9//! one to four bytes carried inline in a single exchange, or a larger value
10//! split across a run of segment frames. Block transfer follows.
11//!
12//! The free functions here encode and decode the raw 8-byte CAN *data field*.
13//! On top of them, [`SdoServer`] services requests against an
14//! [`ObjectDictionary`](crate::object_dictionary::ObjectDictionary) and
15//! [`SdoClient`] drives read/write transactions — both as sans-I/O state
16//! machines that consume and produce frames without touching a bus, so the
17//! transport (see [`request_cob_id`]/[`response_cob_id`]) stays a separate
18//! concern.
19//!
20//! # Example: encode an expedited download
21//!
22//! Writing `UNSIGNED32 0x1234_5678` to object `0x2000` is a single 8-byte
23//! request frame (see [`SdoClient`] / [`SdoServer`] to drive a whole exchange):
24//!
25//! ```
26//! use canopen_rs::sdo::encode_download_expedited;
27//! use canopen_rs::{Address, Value};
28//!
29//! let frame = encode_download_expedited(
30//!     Address::new(0x2000, 0),
31//!     &Value::Unsigned32(0x1234_5678),
32//! )
33//! .unwrap();
34//! // cmd | index (LE) | subindex | value (LE)
35//! assert_eq!(frame, [0x23, 0x00, 0x20, 0x00, 0x78, 0x56, 0x34, 0x12]);
36//! ```
37
38use heapless::Vec;
39
40use crate::datatypes::{DataType, Value};
41use crate::object_dictionary::Address;
42use crate::types::NodeId;
43use crate::{Error, Result};
44
45pub mod client;
46pub mod server;
47
48pub use client::{SdoClient, SdoEvent};
49pub use server::SdoServer;
50
51/// COB-ID base for SDO client→server (request) frames: `0x600 + node id`.
52pub const SDO_REQUEST_COB_BASE: u16 = 0x600;
53/// COB-ID base for SDO server→client (response) frames: `0x580 + node id`.
54pub const SDO_RESPONSE_COB_BASE: u16 = 0x580;
55
56/// The 8-byte SDO payload carried in a CAN frame's data field.
57pub type SdoPayload = [u8; 8];
58
59/// The COB-ID of the SDO request channel (client → server) for `node`.
60pub const fn request_cob_id(node: NodeId) -> u16 {
61    SDO_REQUEST_COB_BASE + node.raw() as u16
62}
63
64/// The COB-ID of the SDO response channel (server → client) for `node`.
65pub const fn response_cob_id(node: NodeId) -> u16 {
66    SDO_RESPONSE_COB_BASE + node.raw() as u16
67}
68
69// --- Command specifiers (top three bits of byte 0) -------------------------
70const CCS_DOWNLOAD_SEGMENT: u8 = 0x00; // client: 000xxxxx
71const CCS_DOWNLOAD_INITIATE: u8 = 0x20; // client: 001xxxxx
72const CCS_UPLOAD_INITIATE: u8 = 0x40; // client: 010xxxxx
73const CCS_UPLOAD_SEGMENT: u8 = 0x60; // client: 011xxxxx
74                                     // The server's upload-segment specifier (scs 000) equals CCS_DOWNLOAD_SEGMENT,
75                                     // which is why one data-segment codec serves both directions.
76const SCS_DOWNLOAD_SEGMENT: u8 = 0x20; // server: 001xxxxx
77const SCS_UPLOAD_INITIATE: u8 = 0x40; // server: 010xxxxx
78const SCS_DOWNLOAD_INITIATE: u8 = 0x60; // server: 011xxxxx
79const CS_ABORT: u8 = 0x80; // either:  100xxxxx
80
81// Top-three-bit command-specifier mask.
82const CS_MASK: u8 = 0xE0;
83
84// Low-byte flag bits.
85const EXPEDITED: u8 = 0x02; // 'e' in an initiate frame
86const SIZE_INDICATED: u8 = 0x01; // 's' in an initiate frame
87const EXPEDITED_SIZED: u8 = EXPEDITED | SIZE_INDICATED; // expedited + size (0x03)
88const TOGGLE: u8 = 0x10; // 't' in a segment frame
89const NO_MORE_SEGMENTS: u8 = 0x01; // 'c' in a data-segment frame (last segment)
90
91/// Maximum data bytes carried by a single SDO segment frame.
92pub const SEGMENT_DATA_MAX: usize = 7;
93
94/// SDO abort codes (CiA 301 §7.2.4.3.17). The value is the 32-bit code sent
95/// little-endian in bytes 4..8 of an abort frame.
96#[derive(Debug, Clone, Copy, PartialEq, Eq)]
97#[non_exhaustive]
98pub enum SdoAbortCode {
99    /// Toggle bit not alternated.
100    ToggleBitNotAlternated = 0x0503_0000,
101    /// SDO protocol timed out.
102    ProtocolTimedOut = 0x0504_0000,
103    /// Client/server command specifier not valid or unknown.
104    CommandInvalid = 0x0504_0001,
105    /// Unsupported access to an object.
106    UnsupportedAccess = 0x0601_0000,
107    /// Attempt to read a write-only object.
108    ReadOfWriteOnly = 0x0601_0001,
109    /// Attempt to write a read-only object.
110    WriteOfReadOnly = 0x0601_0002,
111    /// Object does not exist in the object dictionary.
112    ObjectDoesNotExist = 0x0602_0000,
113    /// Data type does not match; length of service parameter too high.
114    DataTypeMismatchLengthHigh = 0x0607_0012,
115    /// Data type does not match; length of service parameter too low.
116    DataTypeMismatchLengthLow = 0x0607_0013,
117    /// Sub-index does not exist.
118    SubIndexDoesNotExist = 0x0609_0011,
119    /// General error.
120    General = 0x0800_0000,
121}
122
123/// Encode an expedited SDO **download** (write) request writing `value` to
124/// `addr`.
125///
126/// Returns [`Error::UnsupportedTransfer`] for values larger than four bytes,
127/// which require segmented transfer.
128pub fn encode_download_expedited(addr: Address, value: &Value) -> Result<SdoPayload> {
129    let len = value.size();
130    if len == 0 || len > 4 {
131        return Err(Error::UnsupportedTransfer);
132    }
133    let mut p = [0u8; 8];
134    // n = number of *unused* data bytes = 4 - len.
135    p[0] = CCS_DOWNLOAD_INITIATE | (((4 - len) as u8) << 2) | EXPEDITED_SIZED;
136    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
137    p[3] = addr.subindex;
138    value.encode_le(&mut p[4..4 + len])?;
139    Ok(p)
140}
141
142/// Encode the server's **download response** (write confirmation) for `addr`.
143pub fn encode_download_response(addr: Address) -> SdoPayload {
144    let mut p = [0u8; 8];
145    p[0] = SCS_DOWNLOAD_INITIATE;
146    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
147    p[3] = addr.subindex;
148    p
149}
150
151/// Decode a server download response, returning the confirmed address.
152pub fn decode_download_response(p: &SdoPayload) -> Result<Address> {
153    if p[0] != SCS_DOWNLOAD_INITIATE {
154        return Err(Error::UnexpectedCommand);
155    }
156    Ok(Address::new(u16::from_le_bytes([p[1], p[2]]), p[3]))
157}
158
159/// Encode an SDO **upload** (read) request for `addr`.
160pub fn encode_upload_request(addr: Address) -> SdoPayload {
161    let mut p = [0u8; 8];
162    p[0] = CCS_UPLOAD_INITIATE;
163    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
164    p[3] = addr.subindex;
165    p
166}
167
168/// Encode the server's expedited **upload response** carrying `value` for
169/// `addr`.
170///
171/// Returns [`Error::UnsupportedTransfer`] for values larger than four bytes.
172pub fn encode_upload_expedited_response(addr: Address, value: &Value) -> Result<SdoPayload> {
173    let len = value.size();
174    if len == 0 || len > 4 {
175        return Err(Error::UnsupportedTransfer);
176    }
177    let mut p = [0u8; 8];
178    p[0] = SCS_UPLOAD_INITIATE | (((4 - len) as u8) << 2) | EXPEDITED_SIZED;
179    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
180    p[3] = addr.subindex;
181    value.encode_le(&mut p[4..4 + len])?;
182    Ok(p)
183}
184
185/// Decode an expedited upload response into `(address, value)`, interpreting
186/// the inline data as `data_type` (the client knows the expected type from
187/// its OD/EDS).
188///
189/// Returns [`Error::UnexpectedCommand`] if the frame is not an expedited
190/// upload response, or [`Error::TypeMismatch`] if the server's data length
191/// disagrees with `data_type`.
192pub fn decode_upload_expedited_response(
193    p: &SdoPayload,
194    data_type: DataType,
195) -> Result<(Address, Value)> {
196    let cmd = p[0];
197    // scs must be "upload initiate" and the frame must be expedited + sized.
198    if cmd & 0xE0 != SCS_UPLOAD_INITIATE || cmd & EXPEDITED_SIZED != EXPEDITED_SIZED {
199        return Err(Error::UnexpectedCommand);
200    }
201    let n = (cmd >> 2) & 0x03;
202    let len = 4 - n as usize;
203    if len != data_type.size() {
204        return Err(Error::TypeMismatch);
205    }
206    let addr = Address::new(u16::from_le_bytes([p[1], p[2]]), p[3]);
207    let value = Value::decode_le(data_type, &p[4..4 + len])?;
208    Ok((addr, value))
209}
210
211/// Encode an SDO **abort** for `addr` with `code`.
212pub fn encode_abort(addr: Address, code: SdoAbortCode) -> SdoPayload {
213    let mut p = [0u8; 8];
214    p[0] = CS_ABORT;
215    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
216    p[3] = addr.subindex;
217    p[4..8].copy_from_slice(&(code as u32).to_le_bytes());
218    p
219}
220
221/// Decode an SDO abort frame into `(address, raw_abort_code)`.
222pub fn decode_abort(p: &SdoPayload) -> Result<(Address, u32)> {
223    if p[0] != CS_ABORT {
224        return Err(Error::UnexpectedCommand);
225    }
226    let addr = Address::new(u16::from_le_bytes([p[1], p[2]]), p[3]);
227    let code = u32::from_le_bytes([p[4], p[5], p[6], p[7]]);
228    Ok((addr, code))
229}
230
231// === Segmented transfer ====================================================
232//
233// For values larger than four bytes, transfer proceeds in two phases: an
234// *initiate* exchange declaring the total byte count, then a run of *segment*
235// exchanges each carrying up to seven data bytes. A per-transfer *toggle* bit
236// alternates on every segment (starting at 0) to detect lost or duplicated
237// frames, and the final data segment sets the "no more segments" bit.
238//
239// The initiate *download response* (server) and initiate *upload request*
240// (client) are byte-identical to the expedited case, so reuse
241// [`encode_download_response`] / [`decode_download_response`] and
242// [`encode_upload_request`] for them.
243
244/// A decoded SDO data segment: its toggle bit, whether it is the last segment,
245/// and the (borrowed) data bytes it carries.
246///
247/// The download-segment request (client → server) and the upload-segment
248/// response (server → client) share this exact frame layout, so one type and
249/// one codec serve both directions.
250#[derive(Debug, Clone, Copy, PartialEq, Eq)]
251pub struct Segment<'a> {
252    /// The toggle bit for this segment (alternates each segment from `false`).
253    pub toggle: bool,
254    /// Whether this is the final segment of the transfer.
255    pub last: bool,
256    /// The segment's payload (1..=7 bytes).
257    pub data: &'a [u8],
258}
259
260/// Encode a client **segmented download initiate** request declaring a
261/// `size`-byte transfer to `addr` (command `0x21`).
262pub fn encode_download_initiate_segmented(addr: Address, size: u32) -> SdoPayload {
263    let mut p = [0u8; 8];
264    p[0] = CCS_DOWNLOAD_INITIATE | SIZE_INDICATED;
265    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
266    p[3] = addr.subindex;
267    p[4..8].copy_from_slice(&size.to_le_bytes());
268    p
269}
270
271/// Decode a download initiate request into `(address, size)`, requiring a
272/// segmented (non-expedited), size-indicated request.
273pub fn decode_download_initiate_segmented(p: &SdoPayload) -> Result<(Address, u32)> {
274    if p[0] & CS_MASK != CCS_DOWNLOAD_INITIATE
275        || p[0] & EXPEDITED != 0
276        || p[0] & SIZE_INDICATED == 0
277    {
278        return Err(Error::UnexpectedCommand);
279    }
280    let addr = Address::new(u16::from_le_bytes([p[1], p[2]]), p[3]);
281    Ok((addr, u32::from_le_bytes([p[4], p[5], p[6], p[7]])))
282}
283
284/// Encode the server's **segmented upload initiate response** declaring a
285/// `size`-byte transfer for `addr` (command `0x41`).
286pub fn encode_upload_initiate_segmented_response(addr: Address, size: u32) -> SdoPayload {
287    let mut p = [0u8; 8];
288    p[0] = SCS_UPLOAD_INITIATE | SIZE_INDICATED;
289    p[1..3].copy_from_slice(&addr.index.to_le_bytes());
290    p[3] = addr.subindex;
291    p[4..8].copy_from_slice(&size.to_le_bytes());
292    p
293}
294
295/// Decode a segmented upload initiate response into `(address, size)`.
296///
297/// Returns [`Error::UnexpectedCommand`] if the frame is not an upload initiate
298/// response, or if it is expedited (use [`decode_upload_expedited_response`]
299/// for that case).
300pub fn decode_upload_initiate_segmented_response(p: &SdoPayload) -> Result<(Address, u32)> {
301    if p[0] & CS_MASK != SCS_UPLOAD_INITIATE || p[0] & EXPEDITED != 0 || p[0] & SIZE_INDICATED == 0
302    {
303        return Err(Error::UnexpectedCommand);
304    }
305    let addr = Address::new(u16::from_le_bytes([p[1], p[2]]), p[3]);
306    Ok((addr, u32::from_le_bytes([p[4], p[5], p[6], p[7]])))
307}
308
309/// Encode a **data segment** carrying 1..=7 bytes of `data`.
310///
311/// Used for both the download-segment request and the upload-segment response
312/// (identical layout). `toggle` alternates each segment (the first is
313/// `false`); `last` marks the final segment. Returns [`Error::BadLength`]
314/// unless `data` is 1..=7 bytes.
315pub fn encode_data_segment(data: &[u8], toggle: bool, last: bool) -> Result<SdoPayload> {
316    if data.is_empty() || data.len() > SEGMENT_DATA_MAX {
317        return Err(Error::BadLength);
318    }
319    let mut p = [0u8; 8];
320    // ccs/scs for a data segment are both 000, so byte 0's top bits stay 0.
321    let n = (SEGMENT_DATA_MAX - data.len()) as u8;
322    p[0] = (n << 1) & 0x0E;
323    if toggle {
324        p[0] |= TOGGLE;
325    }
326    if last {
327        p[0] |= NO_MORE_SEGMENTS;
328    }
329    p[1..1 + data.len()].copy_from_slice(data);
330    Ok(p)
331}
332
333/// Decode a **data segment** frame (download request or upload response).
334pub fn decode_data_segment(p: &SdoPayload) -> Result<Segment<'_>> {
335    if p[0] & CS_MASK != CCS_DOWNLOAD_SEGMENT {
336        return Err(Error::UnexpectedCommand);
337    }
338    let n = ((p[0] >> 1) & 0x07) as usize;
339    if n > SEGMENT_DATA_MAX {
340        return Err(Error::BadLength);
341    }
342    Ok(Segment {
343        toggle: p[0] & TOGGLE != 0,
344        last: p[0] & NO_MORE_SEGMENTS != 0,
345        data: &p[1..1 + (SEGMENT_DATA_MAX - n)],
346    })
347}
348
349/// Encode the server's **download segment response** (acknowledgement) with the
350/// segment's `toggle` bit (command `0x20 | toggle`).
351pub fn encode_download_segment_response(toggle: bool) -> SdoPayload {
352    let mut p = [0u8; 8];
353    p[0] = SCS_DOWNLOAD_SEGMENT | if toggle { TOGGLE } else { 0 };
354    p
355}
356
357/// Decode a download segment response, returning its toggle bit.
358pub fn decode_download_segment_response(p: &SdoPayload) -> Result<bool> {
359    if p[0] & CS_MASK != SCS_DOWNLOAD_SEGMENT {
360        return Err(Error::UnexpectedCommand);
361    }
362    Ok(p[0] & TOGGLE != 0)
363}
364
365/// Encode the client's **upload segment request** (poll for the next segment)
366/// with the expected `toggle` bit (command `0x60 | toggle`).
367pub fn encode_upload_segment_request(toggle: bool) -> SdoPayload {
368    let mut p = [0u8; 8];
369    p[0] = CCS_UPLOAD_SEGMENT | if toggle { TOGGLE } else { 0 };
370    p
371}
372
373/// Decode an upload segment request, returning its toggle bit.
374pub fn decode_upload_segment_request(p: &SdoPayload) -> Result<bool> {
375    if p[0] & CS_MASK != CCS_UPLOAD_SEGMENT {
376        return Err(Error::UnexpectedCommand);
377    }
378    Ok(p[0] & TOGGLE != 0)
379}
380
381/// Splits a byte buffer into SDO download data segments, tracking the toggle
382/// bit. Drive it after a successful download-initiate handshake: emit each
383/// frame, await its acknowledgement, then take the next.
384#[derive(Debug)]
385pub struct SegmentWriter<'a> {
386    data: &'a [u8],
387    pos: usize,
388    toggle: bool,
389}
390
391impl<'a> SegmentWriter<'a> {
392    /// Start splitting `data` (which should be the >4-byte value already
393    /// declared in the initiate request).
394    pub const fn new(data: &'a [u8]) -> Self {
395        Self {
396            data,
397            pos: 0,
398            toggle: false,
399        }
400    }
401
402    /// Whether every byte has been emitted.
403    pub const fn is_done(&self) -> bool {
404        self.pos >= self.data.len()
405    }
406
407    /// Produce the next download data-segment frame, or `None` when finished.
408    pub fn next_segment(&mut self) -> Option<SdoPayload> {
409        if self.is_done() {
410            return None;
411        }
412        let remaining = self.data.len() - self.pos;
413        let len = remaining.min(SEGMENT_DATA_MAX);
414        let last = remaining <= SEGMENT_DATA_MAX;
415        let frame = encode_data_segment(&self.data[self.pos..self.pos + len], self.toggle, last)
416            .expect("len is 1..=7");
417        self.pos += len;
418        self.toggle = !self.toggle;
419        Some(frame)
420    }
421}
422
423/// Reassembles SDO upload data segments into a bounded buffer of capacity `N`,
424/// tracking and validating the toggle bit. Push each decoded [`Segment`] until
425/// [`SegmentReader::is_done`], then read [`SegmentReader::data`].
426#[derive(Debug)]
427pub struct SegmentReader<const N: usize> {
428    buf: Vec<u8, N>,
429    toggle: bool,
430    done: bool,
431}
432
433impl<const N: usize> Default for SegmentReader<N> {
434    fn default() -> Self {
435        Self::new()
436    }
437}
438
439impl<const N: usize> SegmentReader<N> {
440    /// A new, empty reassembler.
441    pub const fn new() -> Self {
442        Self {
443            buf: Vec::new(),
444            toggle: false,
445            done: false,
446        }
447    }
448
449    /// Whether the final segment has been received.
450    pub const fn is_done(&self) -> bool {
451        self.done
452    }
453
454    /// The reassembled bytes so far.
455    pub fn data(&self) -> &[u8] {
456        &self.buf
457    }
458
459    /// Append a decoded segment.
460    ///
461    /// Returns [`Error::ToggleMismatch`] if the segment's toggle bit is out of
462    /// sequence, [`Error::UnexpectedCommand`] if the transfer is already
463    /// complete, or [`Error::Overflow`] if the data exceeds capacity `N`.
464    pub fn push(&mut self, segment: &Segment) -> Result<()> {
465        if self.done {
466            return Err(Error::UnexpectedCommand);
467        }
468        if segment.toggle != self.toggle {
469            return Err(Error::ToggleMismatch);
470        }
471        self.buf
472            .extend_from_slice(segment.data)
473            .map_err(|_| Error::Overflow)?;
474        self.toggle = !self.toggle;
475        if segment.last {
476            self.done = true;
477        }
478        Ok(())
479    }
480}
481
482#[cfg(test)]
483mod tests {
484    use super::*;
485
486    // --- COB-IDs -----------------------------------------------------------
487    #[test]
488    fn cob_ids_follow_convention() {
489        let node = NodeId::new(0x05).unwrap();
490        assert_eq!(request_cob_id(node), 0x605);
491        assert_eq!(response_cob_id(node), 0x585);
492    }
493
494    // --- Download (write) --------------------------------------------------
495    // Known-good frame: expedited download of UNSIGNED32 0x12345678 to
496    // object 0x2000 sub 0. Command 0x23 = download initiate, expedited,
497    // size indicated, 4 data bytes. Index and value are little-endian.
498    #[test]
499    fn download_u32_matches_known_frame() {
500        let f = encode_download_expedited(Address::new(0x2000, 0), &Value::Unsigned32(0x1234_5678))
501            .unwrap();
502        assert_eq!(f, [0x23, 0x00, 0x20, 0x00, 0x78, 0x56, 0x34, 0x12]);
503    }
504
505    // Known-good frame: expedited download of UNSIGNED8 0x7F to 0x2001 sub 0.
506    // Command 0x2F = download initiate, expedited, size indicated, 1 data byte.
507    #[test]
508    fn download_u8_matches_known_frame() {
509        let f =
510            encode_download_expedited(Address::new(0x2001, 0), &Value::Unsigned8(0x7F)).unwrap();
511        assert_eq!(f, [0x2F, 0x01, 0x20, 0x00, 0x7F, 0x00, 0x00, 0x00]);
512    }
513
514    #[test]
515    fn download_i16_matches_known_frame() {
516        // -2 as INTEGER16 = 0xFFFE little-endian; command 0x2B = 2 data bytes.
517        let f = encode_download_expedited(Address::new(0x6000, 1), &Value::Integer16(-2)).unwrap();
518        assert_eq!(f, [0x2B, 0x00, 0x60, 0x01, 0xFE, 0xFF, 0x00, 0x00]);
519    }
520
521    #[test]
522    fn download_response_roundtrips() {
523        let f = encode_download_response(Address::new(0x2000, 0));
524        assert_eq!(f, [0x60, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00]);
525        assert_eq!(
526            decode_download_response(&f).unwrap(),
527            Address::new(0x2000, 0)
528        );
529    }
530
531    #[test]
532    fn value_too_large_for_expedited_rejected() {
533        assert_eq!(
534            encode_download_expedited(Address::new(0x2000, 0), &Value::Unsigned64(1)),
535            Err(Error::UnsupportedTransfer)
536        );
537    }
538
539    // --- Upload (read) -----------------------------------------------------
540    // Known-good frame: upload (read) request for object 0x1000 sub 0.
541    #[test]
542    fn upload_request_matches_known_frame() {
543        let f = encode_upload_request(Address::new(0x1000, 0));
544        assert_eq!(f, [0x40, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00]);
545    }
546
547    // Known-good frame: expedited upload response for device type object
548    // 0x1000 = UNSIGNED32 0x00000192. Command 0x43 = upload initiate,
549    // expedited, size indicated, 4 data bytes.
550    #[test]
551    fn upload_response_device_type_decodes() {
552        let f = [0x43, 0x00, 0x10, 0x00, 0x92, 0x01, 0x00, 0x00];
553        let (addr, value) = decode_upload_expedited_response(&f, DataType::Unsigned32).unwrap();
554        assert_eq!(addr, Address::new(0x1000, 0));
555        assert_eq!(value, Value::Unsigned32(0x0000_0192));
556    }
557
558    #[test]
559    fn upload_response_encode_matches_known_frame() {
560        let f = encode_upload_expedited_response(
561            Address::new(0x1000, 0),
562            &Value::Unsigned32(0x0000_0192),
563        )
564        .unwrap();
565        assert_eq!(f, [0x43, 0x00, 0x10, 0x00, 0x92, 0x01, 0x00, 0x00]);
566    }
567
568    #[test]
569    fn upload_response_wrong_type_size_errors() {
570        // Frame declares 4 data bytes; decoding as U16 (2 bytes) must fail.
571        let f = [0x43, 0x00, 0x10, 0x00, 0x92, 0x01, 0x00, 0x00];
572        assert_eq!(
573            decode_upload_expedited_response(&f, DataType::Unsigned16),
574            Err(Error::TypeMismatch)
575        );
576    }
577
578    #[test]
579    fn decode_upload_rejects_non_upload_frame() {
580        let f = encode_download_response(Address::new(0x1000, 0));
581        assert_eq!(
582            decode_upload_expedited_response(&f, DataType::Unsigned32),
583            Err(Error::UnexpectedCommand)
584        );
585    }
586
587    // --- Abort -------------------------------------------------------------
588    // Known-good frame: abort of 0x1000 sub 0 with code 0x06020000
589    // (object does not exist), sent little-endian in bytes 4..8.
590    #[test]
591    fn abort_object_missing_matches_known_frame() {
592        let f = encode_abort(Address::new(0x1000, 0), SdoAbortCode::ObjectDoesNotExist);
593        assert_eq!(f, [0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x02, 0x06]);
594        let (addr, code) = decode_abort(&f).unwrap();
595        assert_eq!(addr, Address::new(0x1000, 0));
596        assert_eq!(code, 0x0602_0000);
597    }
598
599    // --- Segmented transfer ------------------------------------------------
600    // Known-good frame: segmented download initiate of a 20-byte value to
601    // 0x2000 sub 0. Command 0x21 = download initiate, size indicated, not
602    // expedited; size 20 little-endian in bytes 4..8.
603    #[test]
604    fn download_initiate_segmented_matches_known_frame() {
605        let f = encode_download_initiate_segmented(Address::new(0x2000, 0), 20);
606        assert_eq!(f, [0x21, 0x00, 0x20, 0x00, 20, 0x00, 0x00, 0x00]);
607        assert_eq!(
608            decode_download_initiate_segmented(&f).unwrap(),
609            (Address::new(0x2000, 0), 20)
610        );
611    }
612
613    // Known-good frame: segmented upload initiate response, 20-byte value from
614    // 0x2000 sub 0. Command 0x41 = upload initiate, size indicated, segmented.
615    #[test]
616    fn upload_initiate_segmented_response_matches_known_frame() {
617        let f = encode_upload_initiate_segmented_response(Address::new(0x2000, 0), 20);
618        assert_eq!(f, [0x41, 0x00, 0x20, 0x00, 20, 0x00, 0x00, 0x00]);
619        assert_eq!(
620            decode_upload_initiate_segmented_response(&f).unwrap(),
621            (Address::new(0x2000, 0), 20)
622        );
623    }
624
625    // A segmented initiate must not decode an expedited response and vice versa.
626    #[test]
627    fn segmented_initiate_rejects_expedited_frame() {
628        let expedited = [0x43, 0x00, 0x10, 0x00, 0x92, 0x01, 0x00, 0x00];
629        assert_eq!(
630            decode_upload_initiate_segmented_response(&expedited),
631            Err(Error::UnexpectedCommand)
632        );
633    }
634
635    // Known-good frame: a full 7-byte first data segment, toggle 0, not last.
636    // Command 0x00: n = 0 unused bytes, toggle clear, continue.
637    #[test]
638    fn data_segment_full_matches_known_frame() {
639        let f = encode_data_segment(&[1, 2, 3, 4, 5, 6, 7], false, false).unwrap();
640        assert_eq!(f, [0x00, 1, 2, 3, 4, 5, 6, 7]);
641    }
642
643    // Known-good frame: a final 3-byte segment, toggle 1, last. Command 0x19 =
644    // toggle (0x10) | n=4 unused bytes (4 << 1 = 0x08) | last (0x01).
645    #[test]
646    fn data_segment_last_matches_known_frame() {
647        let f = encode_data_segment(&[0xAA, 0xBB, 0xCC], true, true).unwrap();
648        assert_eq!(f, [0x19, 0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x00, 0x00]);
649        let seg = decode_data_segment(&f).unwrap();
650        assert!(seg.toggle);
651        assert!(seg.last);
652        assert_eq!(seg.data, &[0xAA, 0xBB, 0xCC]);
653    }
654
655    #[test]
656    fn segment_ack_and_poll_toggle_roundtrip() {
657        assert_eq!(
658            encode_download_segment_response(false),
659            [0x20, 0, 0, 0, 0, 0, 0, 0]
660        );
661        assert_eq!(
662            encode_download_segment_response(true),
663            [0x30, 0, 0, 0, 0, 0, 0, 0]
664        );
665        assert!(decode_download_segment_response(&encode_download_segment_response(true)).unwrap());
666
667        assert_eq!(
668            encode_upload_segment_request(false),
669            [0x60, 0, 0, 0, 0, 0, 0, 0]
670        );
671        assert_eq!(
672            encode_upload_segment_request(true),
673            [0x70, 0, 0, 0, 0, 0, 0, 0]
674        );
675        assert!(decode_upload_segment_request(&encode_upload_segment_request(true)).unwrap());
676    }
677
678    // End-to-end: split a 12-byte value into segments and reassemble it. The
679    // writer emits two frames (7 + 5), toggles alternating false/true, the
680    // second marked last; the reader validates the toggle and rebuilds it.
681    #[test]
682    fn segment_writer_reader_roundtrip() {
683        let payload: [u8; 12] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
684        let mut writer = SegmentWriter::new(&payload);
685        let mut reader = SegmentReader::<64>::new();
686
687        let f1 = writer.next_segment().unwrap();
688        assert_eq!(f1[0] & TOGGLE, 0); // first toggle is 0
689        reader.push(&decode_data_segment(&f1).unwrap()).unwrap();
690        assert!(!reader.is_done());
691
692        let f2 = writer.next_segment().unwrap();
693        assert_ne!(f2[0] & TOGGLE, 0); // second toggle is 1
694        reader.push(&decode_data_segment(&f2).unwrap()).unwrap();
695
696        assert!(reader.is_done());
697        assert!(writer.next_segment().is_none());
698        assert_eq!(reader.data(), &payload);
699    }
700
701    #[test]
702    fn reader_rejects_toggle_out_of_sequence() {
703        let mut reader = SegmentReader::<16>::new();
704        // Second push should expect toggle=true; supplying false must fail.
705        reader
706            .push(&Segment {
707                toggle: false,
708                last: false,
709                data: &[1, 2, 3],
710            })
711            .unwrap();
712        assert_eq!(
713            reader.push(&Segment {
714                toggle: false,
715                last: true,
716                data: &[4, 5]
717            }),
718            Err(Error::ToggleMismatch)
719        );
720    }
721
722    #[test]
723    fn reader_overflow_is_reported() {
724        let mut reader = SegmentReader::<4>::new();
725        assert_eq!(
726            reader.push(&Segment {
727                toggle: false,
728                last: false,
729                data: &[1, 2, 3, 4, 5]
730            }),
731            Err(Error::Overflow)
732        );
733    }
734}