ethercrab 0.7.1

A pure Rust EtherCAT MainDevice supporting std and no_std environments
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
use crate::{
    Command, PduLoop,
    error::PduError,
    fmt,
    generate::write_packed,
    pdu_loop::{
        frame_element::{FrameBox, FrameElement, FrameState, receiving_frame::ReceiveFrameFut},
        frame_header::EthercatFrameHeader,
        pdu_flags::PduFlags,
        pdu_header::PduHeader,
    },
};
use core::{ptr::NonNull, sync::atomic::AtomicU8};
use ethercrab_wire::{
    EtherCrabWireRead, EtherCrabWireSized, EtherCrabWireWrite, EtherCrabWireWriteSized,
};

/// A frame in a freshly allocated state.
///
/// This typestate may only be created by
/// [`alloc_frame`](crate::pdu_loop::storage::PduStorageRef::alloc_frame).
#[derive(Debug)]
pub struct CreatedFrame<'sto> {
    inner: FrameBox<'sto>,
    pdu_count: u8,
    /// Position of the last frame's header in the payload.
    ///
    /// Used for updating the `more_follows` flag when pushing a new PDU.
    last_header_location: Option<usize>,
}

impl<'sto> CreatedFrame<'sto> {
    /// The size of a completely empty PDU.
    ///
    /// Includes header and 2 bytes for working counter.
    pub const PDU_OVERHEAD_BYTES: usize = PduHeader::PACKED_LEN + 2;

    pub(in crate::pdu_loop) fn claim_created(
        frame: NonNull<FrameElement<0>>,
        frame_index: u8,
        pdu_idx: &'sto AtomicU8,
        frame_data_len: usize,
    ) -> Result<Self, PduError> {
        let frame = unsafe { FrameElement::claim_created(frame, frame_index)? };

        let mut inner = FrameBox::new(frame, pdu_idx, frame_data_len);

        inner.init();

        Ok(Self {
            inner,
            pdu_count: 0,
            last_header_location: None,
        })
    }

    pub fn storage_slot_index(&self) -> u8 {
        self.inner.storage_slot_index()
    }

    pub fn is_empty(&self) -> bool {
        self.pdu_count == 0
    }

    /// The frame has been initialised, filled with a data payload (if required), and is now ready
    /// to be sent.
    ///
    /// This method returns a future that should be fulfilled when a response to the sent frame is
    /// received.
    pub fn mark_sendable(
        mut self,
        pdu_loop: &'sto PduLoop<'sto>,
        timeout: crate::timer_factory::LabeledTimeout,
        retries: usize,
    ) -> ReceiveFrameFut<'sto> {
        EthercatFrameHeader::pdu(self.inner.pdu_payload_len() as u16)
            .pack_to_slice_unchecked(self.inner.ecat_frame_header_mut());

        self.inner.set_state(FrameState::Sendable);

        ReceiveFrameFut {
            frame: Some(self.inner),
            pdu_loop,
            timeout_timer: crate::timer_factory::timer(timeout),
            timeout,
            retries_left: retries,
        }
    }

    /// Push a PDU into this frame, consuming as much space as possible.
    ///
    /// Returns the number of bytes from the given `data` that were written into the frame, or
    /// `None` if the input slice is empty or the frame is full.
    pub(crate) fn push_pdu_slice_rest(
        &mut self,
        command: Command,
        bytes: &[u8],
    ) -> Result<Option<(usize, PduResponseHandle)>, PduError> {
        let consumed = self.inner.pdu_payload_len();

        if bytes.is_empty() {
            return Ok(None);
        }

        // The maximum number of bytes we can insert into this frame
        let max_bytes = self
            .inner
            .pdu_buf()
            .len()
            .saturating_sub(consumed)
            .saturating_sub(Self::PDU_OVERHEAD_BYTES);

        if max_bytes == 0 {
            fmt::trace!("Pushed 0 bytes of {} into PDU", bytes.len());

            return Ok(None);
        }

        let sub_slice_len = max_bytes.min(bytes.packed_len());

        let bytes = &bytes[0..sub_slice_len];

        let flags = PduFlags::new(sub_slice_len as u16, false);

        let alloc_size = sub_slice_len + Self::PDU_OVERHEAD_BYTES;

        let buf_range = consumed..(consumed + alloc_size);

        // Establish mapping between this PDU index and the Ethernet frame it's being put in
        let pdu_idx = self.inner.next_pdu_idx();

        fmt::trace!(
            "Write PDU {:#04x} into rest of frame index {} ({}, {} frame bytes + {} payload bytes at {:?})",
            pdu_idx,
            self.inner.storage_slot_index(),
            command,
            sub_slice_len,
            Self::PDU_OVERHEAD_BYTES,
            buf_range
        );

        let l = self.inner.pdu_buf_mut().len();

        let pdu_buf = self
            .inner
            .pdu_buf_mut()
            .get_mut(buf_range.clone())
            .ok_or_else(|| {
                fmt::error!(
                    "Fill rest of PDU buf range too long: wanted {:?} from {:?}",
                    buf_range,
                    0..l
                );

                PduError::TooLong
            })?;

        let header = PduHeader {
            command_code: command.code(),
            index: pdu_idx,
            command_raw: command.pack(),
            flags,
            irq: 0,
        };

        let pdu_buf = write_packed(header, pdu_buf);

        // Payload
        let _pdu_buf = write_packed(bytes, pdu_buf);

        // Next two bytes are working counter, but they are always zero on send (and the buffer is
        // zero-initialised) so there's nothing to do.

        // Don't need to check length here as we do that with `pdu_buf_mut().get_mut()` above.
        self.inner.add_pdu(alloc_size, pdu_idx);

        let index_in_frame = self.pdu_count;

        self.pdu_count += 1;

        // Frame was added successfully, so now we can update the previous PDU `more_follows` flag to true.
        if let Some(last_header_location) = self.last_header_location.as_mut() {
            // Flags start at 6th bit of header
            let flags_offset = 6usize;

            let last_flags_buf = fmt::unwrap_opt!(
                self.inner
                    .pdu_buf_mut()
                    .get_mut((*last_header_location + flags_offset)..)
            );

            let mut last_flags = fmt::unwrap!(PduFlags::unpack_from_slice(last_flags_buf));

            last_flags.more_follows = true;

            last_flags.pack_to_slice_unchecked(last_flags_buf);

            // Previous header is now the one we just inserted
            *last_header_location = buf_range.start;
        } else {
            self.last_header_location = Some(0);
        }

        Ok(Some((
            sub_slice_len,
            PduResponseHandle {
                index_in_frame,
                pdu_idx,
                command_code: command.code(),
                alloc_size,
            },
        )))
    }

    pub(crate) fn can_push_pdu_payload(&self, packed_len: usize) -> bool {
        let alloc_size = packed_len + Self::PDU_OVERHEAD_BYTES;

        let start_byte = self.inner.pdu_payload_len();

        start_byte + alloc_size <= self.inner.pdu_buf().len()
    }

    /// Push a PDU into this frame.
    ///
    /// # Errors
    ///
    /// Returns [`PduError::TooLong`] if the remaining space in the frame is not enough to hold the
    /// new PDU.
    pub fn push_pdu(
        &mut self,
        command: Command,
        data: impl EtherCrabWireWrite,
        len_override: Option<u16>,
    ) -> Result<PduResponseHandle, PduError> {
        let data_length_usize =
            len_override.map_or(data.packed_len(), |l| usize::from(l).max(data.packed_len()));

        let flags = PduFlags::new(data_length_usize as u16, false);

        // PDU header + data + working counter (space is required for the response value - we never
        // actually write it)
        let alloc_size = data_length_usize + Self::PDU_OVERHEAD_BYTES;

        // The number of payload bytes already consumed in this frame (e.g. from prior PDU
        // insertions). This is the start byte of the current PDU we want to push.
        let start_byte = self.inner.pdu_payload_len();

        // Comprises PDU header, body, working counter
        let buf_range = start_byte..(start_byte + alloc_size);

        // Establish mapping between this PDU index and the Ethernet frame it's being put in
        let pdu_idx = self.inner.next_pdu_idx();

        fmt::trace!(
            "Write PDU {:#04x} into frame index {} ({}, {} bytes at {:?})",
            pdu_idx,
            self.inner.storage_slot_index(),
            command,
            data_length_usize,
            buf_range
        );

        let l = self.inner.pdu_buf_mut().len();

        let pdu_buf = self
            .inner
            .pdu_buf_mut()
            .get_mut(buf_range.clone())
            .ok_or_else(|| {
                fmt::trace!(
                    "Push PDU buf range too long: wanted {:?} from {:?}",
                    buf_range,
                    0..l
                );

                PduError::TooLong
            })?;

        let header = PduHeader {
            command_code: command.code(),
            index: pdu_idx,
            command_raw: command.pack(),
            flags,
            irq: 0,
        };

        let pdu_buf = write_packed(header, pdu_buf);

        // Payload
        let _pdu_buf = write_packed(data, pdu_buf);

        // Next two bytes are working counter, but they are always zero on send (and the buffer is
        // zero-initialised) so there's nothing to do.

        // Don't need to check length here as we do that with `pdu_buf_mut().get_mut()` above.
        self.inner.add_pdu(alloc_size, pdu_idx);

        let index_in_frame = self.pdu_count;

        self.pdu_count += 1;

        // Frame was added successfully, so now we can update the previous PDU `more_follows` flag to true.
        if let Some(last_header_location) = self.last_header_location.as_mut() {
            // Flags start at 6th bit of header
            let flags_offset = 6usize;

            let last_flags_buf = fmt::unwrap_opt!(
                self.inner
                    .pdu_buf_mut()
                    .get_mut((*last_header_location + flags_offset)..)
            );

            let mut last_flags = fmt::unwrap!(PduFlags::unpack_from_slice(last_flags_buf));

            last_flags.more_follows = true;

            last_flags.pack_to_slice_unchecked(last_flags_buf);

            // Previous header is now the one we just inserted
            *last_header_location = buf_range.start;
        } else {
            self.last_header_location = Some(0);
        }

        Ok(PduResponseHandle {
            index_in_frame,
            pdu_idx,
            command_code: command.code(),
            alloc_size,
        })
    }
}

impl Drop for CreatedFrame<'_> {
    fn drop(&mut self) {
        // ONLY free the frame if it's still in created state. If it's been moved into
        // sending/sent/receiving/etc, we must leave it alone.
        let _ = self.inner.swap_state(FrameState::Created, FrameState::None);
    }
}

// SAFETY: This unsafe impl is required due to `FrameBox` containing a `NonNull`, however this impl
// is ok because FrameBox also holds the lifetime `'sto` of the backing store, which is where the
// `NonNull<FrameElement>` comes from.
//
// For example, if the backing storage is is `'static`, we can send things between threads. If it's
// not, the associated lifetime will prevent the framebox from being used in anything that requires
// a 'static bound.
unsafe impl Send for CreatedFrame<'_> {}

#[derive(Debug)]
#[cfg_attr(test, derive(Eq, PartialEq))]
pub struct PduResponseHandle {
    // Might want this in the future
    #[allow(unused)]
    pub index_in_frame: u8,

    /// PDU wire index and command used to validate response match.
    pub pdu_idx: u8,
    pub command_code: u8,

    /// The number of bytes allocated for the PDU header, payload and WKC in the frame.
    pub alloc_size: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        PduStorage, RegisterAddress,
        ethernet::EthernetFrame,
        pdu_loop::frame_element::{AtomicFrameState, FIRST_PDU_EMPTY, FrameElement},
    };
    use atomic_waker::AtomicWaker;
    use core::{
        cell::UnsafeCell,
        ptr::NonNull,
        sync::atomic::{AtomicU8, AtomicU16},
    };

    #[test]
    fn chunked_send() {
        crate::test_logger();

        const MAX_PAYLOAD: usize = 32;

        const BUF_LEN: usize = PduStorage::element_size(MAX_PAYLOAD);

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        let whatever_handle = created.push_pdu(Command::frmw(0x1000, 0x0918).into(), 0u64, None);

        assert!(whatever_handle.is_ok());

        let big_frame = [0xaau8; MAX_PAYLOAD * 2];

        let (rest, _handle) = created
            .push_pdu_slice_rest(Command::fpwr(0x1000, 0x0918).into(), &big_frame)
            .expect("Should not fail")
            .unwrap();

        assert_eq!(rest, 12);
    }

    #[test]
    fn too_long() {
        crate::test_logger();

        const BUF_LEN: usize = 16;

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        let handle = created.push_pdu(Command::fpwr(0x1000, 0x0918).into(), [0xffu8; 9], None);

        assert_eq!(handle.unwrap_err(), PduError::TooLong);
    }

    #[test]
    fn auto_more_follows() {
        crate::test_logger();

        const BUF_LEN: usize = 64;

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        let handle = created.push_pdu(Command::fpwr(0x1000, 0x0918).into(), (), None);
        assert!(handle.is_ok());

        let handle = created.push_pdu(Command::fpwr(0x1001, 0x0918).into(), (), None);
        assert!(handle.is_ok());

        let handle = created.push_pdu(Command::fpwr(0x1002, 0x0918).into(), (), None);
        assert!(handle.is_ok());

        const FLAGS_OFFSET: usize = 6;

        assert_eq!(
            created.inner.pdu_buf()[FLAGS_OFFSET..][..2],
            PduFlags::new(0, true).pack()
        );

        assert_eq!(
            created.inner.pdu_buf()[PduHeader::PACKED_LEN + 2 + FLAGS_OFFSET..][..2],
            PduFlags::new(0, true).pack()
        );

        assert_eq!(
            created.inner.pdu_buf()[(PduHeader::PACKED_LEN + 2 + FLAGS_OFFSET) * 2..][..2],
            PduFlags::new(0, false).pack()
        );
    }

    #[test]
    fn push_rest_too_long() {
        crate::test_logger();

        const BUF_LEN: usize = 32;

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        let data = [0xaau8; 128];

        let res = created.push_pdu_slice_rest(Command::Nop, &data);

        // 32 byte frame contains all the headers with a little bit left over for writing some data
        // into.
        let expected_written = BUF_LEN
            - CreatedFrame::PDU_OVERHEAD_BYTES
            - EthercatFrameHeader::header_len()
            - EthernetFrame::<&[u8]>::header_len();

        // Just double checking
        assert_eq!(expected_written, 4);

        assert_eq!(
            res,
            Ok(Some((
                expected_written,
                PduResponseHandle {
                    index_in_frame: 0,
                    pdu_idx: 0,
                    command_code: 0,
                    // The size of this PDU with a 4 byte payload (plus 12 byte header)
                    alloc_size: 4 + CreatedFrame::PDU_OVERHEAD_BYTES
                }
            )))
        );

        // Can't push anything else
        let res = created.push_pdu_slice_rest(Command::Nop, &data);

        assert_eq!(res, Ok(None));
    }

    #[test]
    fn push_rest_after_dc_sync() {
        crate::test_logger();

        const BUF_LEN: usize = 64;

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        let dc_handle = created
            .push_pdu(
                Command::frmw(0x1000, RegisterAddress::DcSystemTime.into()).into(),
                0u64,
                None,
            )
            .expect("DC handle");

        // 12 byte PDU header plus 8 byte payload
        assert_eq!(dc_handle.alloc_size, 20);

        let data = [0xaau8; 128];

        let remaining = BUF_LEN
            - EthernetFrame::<&[u8]>::header_len()
            - EthercatFrameHeader::header_len()
            - dc_handle.alloc_size;

        // Just double checking
        assert_eq!(remaining, 28);

        let res = created.push_pdu_slice_rest(Command::Nop, &data);

        assert_eq!(
            res,
            Ok(Some((
                remaining - CreatedFrame::PDU_OVERHEAD_BYTES,
                PduResponseHandle {
                    index_in_frame: 1,
                    pdu_idx: 1,
                    command_code: 0,
                    alloc_size: remaining
                }
            )))
        );

        // Can't push anything else
        let res = created.push_pdu_slice_rest(Command::Nop, &data);

        assert_eq!(res, Ok(None));
    }

    #[test]
    fn push_rest_empty() {
        crate::test_logger();

        const BUF_LEN: usize = 64;

        let pdu_idx = AtomicU8::new(0);

        let frames = UnsafeCell::new([FrameElement {
            storage_slot_index: 0xab,
            status: AtomicFrameState::new(FrameState::None),
            waker: AtomicWaker::default(),
            ethernet_frame: [0u8; BUF_LEN],
            pdu_payload_len: 0,
            first_pdu: AtomicU16::new(FIRST_PDU_EMPTY),
        }]);

        let mut created = CreatedFrame::claim_created(
            unsafe { NonNull::new_unchecked(frames.get().cast()) },
            0xab,
            &pdu_idx,
            BUF_LEN,
        )
        .expect("Claim created");

        assert_eq!(
            created.push_pdu_slice_rest(
                Command::frmw(0x1000, RegisterAddress::DcSystemTime.into()).into(),
                &[]
            ),
            Ok(None)
        );
    }
}