Unwrapping enqueue

This commit is contained in:
Ryan Summers 2021-05-27 17:10:03 +02:00
parent 3ce93b8fcd
commit 97911c55f9
3 changed files with 56 additions and 45 deletions

5
Cargo.lock generated
View File

@ -38,7 +38,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9a69a963b70ddacfcd382524f72a4576f359af9334b3bf48a79566590bb8bfa"
dependencies = [
"bitrate",
"cortex-m 0.7.2",
"cortex-m 0.6.7",
"embedded-hal",
]
@ -778,7 +778,6 @@ dependencies = [
[[package]]
name = "smoltcp-nal"
version = "0.1.0"
source = "git+https://github.com/quartiq/smoltcp-nal.git?branch=feature/nal-update#65d94c4ab9e06d2e5c48547a0c9cd6836591e355"
dependencies = [
"embedded-nal",
"heapless 0.7.1",
@ -828,7 +827,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b672c837e0ee8158ecc7fce0f9a948dd0693a9c588338e728d14b73307a0b7d"
dependencies = [
"bare-metal 0.2.5",
"cortex-m 0.7.2",
"cortex-m 0.6.7",
"cortex-m-rt",
"vcell",
]

View File

@ -7,8 +7,6 @@ use stm32h7xx_hal::{
prelude::*,
};
const NUM_SOCKETS: usize = 5;
use smoltcp_nal::smoltcp;
use embedded_hal::digital::v2::{InputPin, OutputPin};
@ -19,13 +17,18 @@ use super::{
DigitalInput0, DigitalInput1, EthernetPhy, NetworkStack, AFE0, AFE1,
};
const NUM_TCP_SOCKETS: usize = 5;
const NUM_UDP_SOCKETS: usize = 1;
const NUM_SOCKETS: usize = NUM_UDP_SOCKETS + NUM_TCP_SOCKETS;
pub struct NetStorage {
pub ip_addrs: [smoltcp::wire::IpCidr; 1],
// Note: There is an additional socket set item required for the DHCP socket.
pub sockets:
[Option<smoltcp::socket::SocketSetItem<'static>>; NUM_SOCKETS + 1],
pub socket_storage: [SocketStorage; NUM_SOCKETS],
pub tcp_socket_storage: [TcpSocketStorage; NUM_TCP_SOCKETS],
pub udp_socket_storage: [UdpSocketStorage; NUM_UDP_SOCKETS],
pub neighbor_cache:
[Option<(smoltcp::wire::IpAddress, smoltcp::iface::Neighbor)>; 8],
pub routes_cache:
@ -37,17 +40,35 @@ pub struct NetStorage {
pub dhcp_rx_storage: [u8; 600],
}
#[derive(Copy, Clone)]
pub struct SocketStorage {
pub struct UdpSocketStorage {
rx_storage: [u8; 1024],
tx_storage: [u8; 1024 * 3],
tx_storage: [u8; 1024],
tx_metadata: [smoltcp::storage::PacketMetadata<smoltcp::wire::IpEndpoint>; 10],
rx_metadata: [smoltcp::storage::PacketMetadata<smoltcp::wire::IpEndpoint>; 10],
}
impl SocketStorage {
impl UdpSocketStorage {
const fn new() -> Self {
Self {
rx_storage: [0; 1024],
tx_storage: [0; 1024 * 3],
tx_storage: [0; 1024],
tx_metadata: [smoltcp::storage::PacketMetadata::<smoltcp::wire::IpEndpoint>::EMPTY; 10],
rx_metadata: [smoltcp::storage::PacketMetadata::<smoltcp::wire::IpEndpoint>::EMPTY; 10],
}
}
}
#[derive(Copy, Clone)]
pub struct TcpSocketStorage {
rx_storage: [u8; 1024],
tx_storage: [u8; 1024],
}
impl TcpSocketStorage {
const fn new() -> Self {
Self {
rx_storage: [0; 1024],
tx_storage: [0; 1024],
}
}
}
@ -61,8 +82,9 @@ impl NetStorage {
)],
neighbor_cache: [None; 8],
routes_cache: [None; 8],
sockets: [None, None, None, None, None, None],
socket_storage: [SocketStorage::new(); NUM_SOCKETS],
sockets: [None, None, None, None, None, None, None],
tcp_socket_storage: [TcpSocketStorage::new(); NUM_TCP_SOCKETS],
udp_socket_storage: [UdpSocketStorage::new(); NUM_UDP_SOCKETS],
dhcp_tx_storage: [0; 600],
dhcp_rx_storage: [0; 600],
dhcp_rx_metadata: [smoltcp::socket::RawPacketMetadata::EMPTY; 1],
@ -594,7 +616,7 @@ pub fn setup(
let mut sockets =
smoltcp::socket::SocketSet::new(&mut store.sockets[..]);
for storage in store.socket_storage.iter_mut() {
for storage in store.tcp_socket_storage[..].iter_mut() {
let tcp_socket = {
let rx_buffer = smoltcp::socket::TcpSocketBuffer::new(
&mut storage.rx_storage[..],
@ -608,6 +630,22 @@ pub fn setup(
sockets.add(tcp_socket);
}
for storage in store.udp_socket_storage[..].iter_mut() {
let udp_socket = {
let rx_buffer = smoltcp::socket::UdpSocketBuffer::new(
&mut storage.rx_metadata[..],
&mut storage.rx_storage[..],
);
let tx_buffer = smoltcp::socket::UdpSocketBuffer::new(
&mut storage.tx_metadata[..],
&mut storage.tx_storage[..],
);
smoltcp::socket::UdpSocket::new(rx_buffer, tx_buffer)
};
sockets.add(udp_socket);
}
sockets
};

View File

@ -3,7 +3,7 @@ use heapless::{
spsc::{Consumer, Producer, Queue},
Vec,
};
use smoltcp_nal::embedded_nal::{SocketAddr, TcpClientStack};
use smoltcp_nal::embedded_nal::{SocketAddr, UdpClientStack};
use super::NetworkReference;
use crate::hardware::design_parameters::SAMPLE_BUFFER_SIZE;
@ -66,7 +66,7 @@ impl BlockGenerator {
pub struct DataStream {
stack: NetworkReference,
socket: Option<<NetworkReference as TcpClientStack>::TcpSocket>,
socket: Option<<NetworkReference as UdpClientStack>::UdpSocket>,
queue: Consumer<'static, AdcDacData, BlockBufferSize>,
remote: Option<SocketAddr>,
}
@ -126,7 +126,7 @@ impl DataStream {
self.stack
.socket()
.map_err(|err| match err {
<NetworkReference as TcpClientStack>::Error::NoIpAddress => (),
<NetworkReference as UdpClientStack>::Error::NoIpAddress => (),
_ => ()
})?;
@ -154,22 +154,6 @@ impl DataStream {
self.remote = Some(remote);
}
fn manage_reconnection(&mut self) {
if self.socket.is_none() || self.remote.is_none() {
return
}
let mut socket = self.socket.borrow_mut().unwrap();
let connected = match self.stack.is_connected(&mut socket) {
Ok(connected) => connected,
_ => return,
};
if !connected {
self.stack.connect(&mut socket, self.remote.unwrap()).unwrap();
}
}
pub fn process(&mut self) -> bool {
// If there's no socket available, try to connect to our remote.
if self.socket.is_none() && self.remote.is_some() {
@ -185,7 +169,7 @@ impl DataStream {
}
let mut handle = self.socket.borrow_mut().unwrap();
let capacity = self.stack.lock(|stack| stack.get_remaining_send_buffer(handle)).unwrap();
let capacity = self.stack.lock(|stack| stack.get_remaining_send_buffer(handle.handle)).unwrap();
// TODO: Clean up magic numbers.
if capacity < 72 {
@ -198,9 +182,6 @@ impl DataStream {
if let Some(data) = self.queue.dequeue() {
// Reconnect the socket if we're no longer connected.
self.manage_reconnection();
let block = DataBlock {
adcs: data.adcs,
dacs: data.dacs,
@ -213,14 +194,7 @@ impl DataStream {
// Transmit the data block.
// TODO: Should we measure how many packets get dropped as telemetry?
match self.stack.send(&mut handle, &data) {
Ok(len) => {
if len != data.len() {
log::error!("Short message: {} {}", len, data.len());
}
},
_ => {},
}
self.stack.send(&mut handle, &data).ok();
}
self.queue.ready()