diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-15 00:52:13 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-15 00:56:59 +0200 |
| commit | 288309b9d51830aada3d09311ff6da95faab78e1 (patch) | |
| tree | 2256480f69f8e2aa36a568b1aa85a68bbe0da42d /embassy-net-driver/src/lib.rs | |
| parent | d07821d8516f39568066c6a0ccf0953f3fad5318 (diff) | |
net-driver: document crate.
Diffstat (limited to 'embassy-net-driver/src/lib.rs')
| -rw-r--r-- | embassy-net-driver/src/lib.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/embassy-net-driver/src/lib.rs b/embassy-net-driver/src/lib.rs index a39cfecc1..4149bf4a4 100644 --- a/embassy-net-driver/src/lib.rs +++ b/embassy-net-driver/src/lib.rs | |||
| @@ -1,20 +1,57 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | #![warn(missing_docs)] | ||
| 3 | #![doc = include_str!("../README.md")] | ||
| 2 | 4 | ||
| 3 | use core::task::Context; | 5 | use core::task::Context; |
| 4 | 6 | ||
| 7 | /// Main `embassy-net` driver API. | ||
| 8 | /// | ||
| 9 | /// This is essentially an interface for sending and receiving raw network frames. | ||
| 10 | /// | ||
| 11 | /// The interface is based on _tokens_, which are types that allow to receive/transmit a | ||
| 12 | /// single packet. The `receive` and `transmit` functions only construct such tokens, the | ||
| 13 | /// real sending/receiving operation are performed when the tokens are consumed. | ||
| 5 | pub trait Driver { | 14 | pub trait Driver { |
| 15 | /// A token to receive a single network packet. | ||
| 6 | type RxToken<'a>: RxToken | 16 | type RxToken<'a>: RxToken |
| 7 | where | 17 | where |
| 8 | Self: 'a; | 18 | Self: 'a; |
| 19 | |||
| 20 | /// A token to transmit a single network packet. | ||
| 9 | type TxToken<'a>: TxToken | 21 | type TxToken<'a>: TxToken |
| 10 | where | 22 | where |
| 11 | Self: 'a; | 23 | Self: 'a; |
| 12 | 24 | ||
| 25 | /// Construct a token pair consisting of one receive token and one transmit token. | ||
| 26 | /// | ||
| 27 | /// If there is a packet ready to be received, this function must return `Some`. | ||
| 28 | /// If there isn't, it must return `None`, and wake `cx.waker()` when a packet is ready. | ||
| 29 | /// | ||
| 30 | /// The additional transmit token makes it possible to generate a reply packet based | ||
| 31 | /// on the contents of the received packet. For example, this makes it possible to | ||
| 32 | /// handle arbitrarily large ICMP echo ("ping") requests, where the all received bytes | ||
| 33 | /// need to be sent back, without heap allocation. | ||
| 13 | fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)>; | 34 | fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)>; |
| 35 | |||
| 36 | /// Construct a transmit token. | ||
| 37 | /// | ||
| 38 | /// If there is free space in the transmit buffer to transmit a packet, this function must return `Some`. | ||
| 39 | /// If there isn't, it must return `None`, and wake `cx.waker()` when space becomes available. | ||
| 40 | /// | ||
| 41 | /// Note that [`TxToken::consume`] is infallible, so it is not allowed to return a token | ||
| 42 | /// if there is no free space and fail later. | ||
| 14 | fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>>; | 43 | fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>>; |
| 44 | |||
| 45 | /// Get the link state. | ||
| 46 | /// | ||
| 47 | /// This function must return the current link state of the device, and wake `cx.waker()` when | ||
| 48 | /// the link state changes. | ||
| 15 | fn link_state(&mut self, cx: &mut Context) -> LinkState; | 49 | fn link_state(&mut self, cx: &mut Context) -> LinkState; |
| 16 | 50 | ||
| 51 | /// Get a description of device capabilities. | ||
| 17 | fn capabilities(&self) -> Capabilities; | 52 | fn capabilities(&self) -> Capabilities; |
| 53 | |||
| 54 | /// Get the device's Ethernet address. | ||
| 18 | fn ethernet_address(&self) -> [u8; 6]; | 55 | fn ethernet_address(&self) -> [u8; 6]; |
| 19 | } | 56 | } |
| 20 | 57 | ||
| @@ -140,10 +177,15 @@ impl Default for Medium { | |||
| 140 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 177 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 141 | #[non_exhaustive] | 178 | #[non_exhaustive] |
| 142 | pub struct ChecksumCapabilities { | 179 | pub struct ChecksumCapabilities { |
| 180 | /// Checksum behavior for IPv4. | ||
| 143 | pub ipv4: Checksum, | 181 | pub ipv4: Checksum, |
| 182 | /// Checksum behavior for UDP. | ||
| 144 | pub udp: Checksum, | 183 | pub udp: Checksum, |
| 184 | /// Checksum behavior for TCP. | ||
| 145 | pub tcp: Checksum, | 185 | pub tcp: Checksum, |
| 186 | /// Checksum behavior for ICMPv4. | ||
| 146 | pub icmpv4: Checksum, | 187 | pub icmpv4: Checksum, |
| 188 | /// Checksum behavior for ICMPv6. | ||
| 147 | pub icmpv6: Checksum, | 189 | pub icmpv6: Checksum, |
| 148 | } | 190 | } |
| 149 | 191 | ||
| @@ -167,9 +209,12 @@ impl Default for Checksum { | |||
| 167 | } | 209 | } |
| 168 | } | 210 | } |
| 169 | 211 | ||
| 212 | /// The link state of a network device. | ||
| 170 | #[derive(PartialEq, Eq, Clone, Copy)] | 213 | #[derive(PartialEq, Eq, Clone, Copy)] |
| 171 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 214 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 172 | pub enum LinkState { | 215 | pub enum LinkState { |
| 216 | /// The link is down. | ||
| 173 | Down, | 217 | Down, |
| 218 | /// The link is up. | ||
| 174 | Up, | 219 | Up, |
| 175 | } | 220 | } |
