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
//!
//! # Cyphal/UDP transport
//!
//! The current version of the transport is documented in [a forum post from 2022-12-02](https://forum.opencyphal.org/t/cyphal-udp-architectural-issues-caused-by-the-dependency-between-the-nodes-ip-address-and-its-identity/1765/60).
//!
//! If the `std` feature is enabled, this implementation requires the `std` library for sockets.
//!
//! ## How sockets work
//!
//! ### Sending
//!
//! A transport can use one socket to send all outgoing message and service transfers.
//! This socket binds to and an ephemeral UDP port on one network interface.
//!
//! Outgoing transfers get sent to a multicast address based on the port ID and a fixed
//! UDP port.
//!
//! ### Receiving transfers
//!
//! All transfers are received through one socket, which joins any multicast groups required to
//! receive the correct frames.
//!

#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate canadensis_core;
extern crate canadensis_header;
extern crate crc_any;
pub extern crate embedded_nal;
extern crate fallible_collections;
extern crate hash32;
extern crate hash32_derive;
extern crate heapless;
extern crate log;
extern crate nb;
extern crate zerocopy;

use canadensis_core::transport::Transport;
use canadensis_core::{OutOfMemoryError, Priority};
use canadensis_header::{NodeId16, TransferId64};
use core::fmt::Debug;
use crc_any::CRCu32;

pub use crate::rx::{UdpReceiver, UdpSessionData};
pub use crate::tx::UdpTransmitter;

mod address;
pub mod driver;
mod rx;
mod tx;

/// Size of the transfer CRC in bytes
///
/// This is added to the end of every frame
const TRANSFER_CRC_SIZE: usize = 4;

/// The minimum size of a Cyphal/UDP packet
///
/// This is also the minimum MTU. It includes the Cyphal/UDP header, 1 byte of data, and the
/// transfer CRC.
pub const MIN_PACKET_SIZE: usize = canadensis_header::SIZE + 1 + TRANSFER_CRC_SIZE;

/// The default UDP port used for communication
pub const DEFAULT_PORT: u16 = 9382;

/// The Cyphal/UDP transport
///
/// This matches [the standard described on the forum on 2022-12-02](https://forum.opencyphal.org/t/cyphal-udp-architectural-issues-caused-by-the-dependency-between-the-nodes-ip-address-and-its-identity/1765/60).
pub struct UdpTransport(());

impl Transport for UdpTransport {
    type NodeId = UdpNodeId;
    type TransferId = UdpTransferId;
    type Priority = Priority;
}

/// A UDP node ID
pub type UdpNodeId = NodeId16;

pub struct UdpTransferIds([UdpTransferId; u16::MAX as usize + 1]);

impl Default for UdpTransferIds {
    fn default() -> Self {
        UdpTransferIds([UdpTransferId::default(); u16::MAX as usize + 1])
    }
}

impl AsMut<[UdpTransferId]> for UdpTransferIds {
    fn as_mut(&mut self) -> &mut [UdpTransferId] {
        &mut self.0
    }
}

/// A UDP transfer identifier
///
/// This is just a `u64`.
pub type UdpTransferId = TransferId64;

#[derive(Debug)]
pub enum Error<S> {
    Memory(OutOfMemoryError),
    Socket(S),
}

impl<S> From<OutOfMemoryError> for Error<S> {
    fn from(oom: OutOfMemoryError) -> Self {
        Error::Memory(oom)
    }
}

/// Returns a CRC calculator used for data
fn data_crc() -> CRCu32 {
    CRCu32::crc32c()
}