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