nal: use minimq example

This commit is contained in:
occheung 2020-09-01 17:41:40 +08:00
parent 843556d5e9
commit 9a42674bf9
1 changed files with 170 additions and 89 deletions

View File

@ -1,109 +1,190 @@
use embedded_nal::{TcpStack, Mode, SocketAddr}; use core::cell::RefCell;
use embedded_nal::SocketAddr::{V4, V6}; use nb;
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use heapless::{consts, Vec};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
use smoltcp::socket::SocketSet;
use smoltcp::socket::{SocketHandle, TcpSocket, TcpSocketBuffer};
use smoltcp::time::{Duration, Instant};
use smoltcp::Error;
use nb::Error as nbError; use stm32h7_ethernet as ethernet;
use core::cell; use smoltcp as net;
const BUFFER_SIZE: usize = 2048; use cortex_m_semihosting::hprintln;
pub static mut TX_STORAGE: &'static mut [u8] = &mut [0; BUFFER_SIZE];
pub static mut RX_STORAGE: &'static mut [u8] = &mut [0; BUFFER_SIZE];
/* use minimq::embedded_nal;
* Struct for a TCP socket
* TODO: Consider handling all sockets in this struct
*/
pub struct NalTcpClient {}
// impl NalTcpClient { #[derive(Debug)]
// pub fn new(socket: &'a mut TcpSocket<'a>) -> Self { pub enum NetworkError {
NoSocket,
ConnectionFailure,
ReadFailure,
WriteFailure,
}
// NalTcpClient { pub struct NetStorage {
// socket, pub ip_addrs: [net::wire::IpCidr; 1],
// } pub neighbor_cache: [Option<(net::wire::IpAddress, net::iface::Neighbor)>; 8],
// } }
// }
impl<'a> TcpStack for &'a NalTcpClient{ pub type NetworkInterface =
// The type returned when we create a new TCP socket net::iface::EthernetInterface<'static, 'static, 'static, ethernet::EthernetDMA<'static>>;
type TcpSocket = smoltcp::socket::TcpSocket<'a>;
// The type returned when we have an error
type Error = Error;
// Open a new TCP socket. The socket starts in the unconnected state. // TODO: The network stack likely needs a time-tracking mechanic here to support
fn open(&self, mode: Mode) -> Result<Self::TcpSocket, Self::Error> { // blocking/nonblocking/timeout based operations.
let tx_buffer = unsafe { TcpSocketBuffer::new(&mut TX_STORAGE[..]) }; pub struct NetworkStack<'a, 'b, 'c, 'n> {
let rx_buffer = unsafe { TcpSocketBuffer::new(&mut RX_STORAGE[..]) }; network_interface: &'n mut NetworkInterface,
let mut socket = TcpSocket::new(rx_buffer, tx_buffer); sockets: RefCell<net::socket::SocketSet<'a, 'b, 'c>>,
if let Mode::Timeout(dur) = mode { next_port: RefCell<u16>,
socket.set_timeout(Some(Duration::from_millis(dur.into()))); unused_handles: RefCell<Vec<net::socket::SocketHandle, consts::U16>>,
}
impl<'a, 'b, 'c, 'n> NetworkStack<'a, 'b, 'c, 'n> {
pub fn new(
interface: &'n mut NetworkInterface,
sockets: net::socket::SocketSet<'a, 'b, 'c>,
) -> Self {
let mut unused_handles: Vec<net::socket::SocketHandle, consts::U16> = Vec::new();
for socket in sockets.iter() {
unused_handles.push(socket.handle()).unwrap();
} }
NetworkStack {
network_interface: interface,
sockets: RefCell::new(sockets),
next_port: RefCell::new(49152),
unused_handles: RefCell::new(unused_handles),
}
}
pub fn update(&mut self, time: u32) -> bool {
match self.network_interface.poll(
&mut self.sockets.borrow_mut(),
net::time::Instant::from_millis(time as i64),
) {
Ok(changed) => changed == false,
Err(e) => {
hprintln!("{:?}", e);
true
}
}
}
fn get_ephemeral_port(&self) -> u16 {
// Get the next ephemeral port
let current_port = self.next_port.borrow().clone();
let (next, wrap) = self.next_port.borrow().overflowing_add(1);
*self.next_port.borrow_mut() = if wrap { 49152 } else { next };
return current_port;
}
}
impl<'a, 'b, 'c, 'n> embedded_nal::TcpStack for NetworkStack<'a, 'b, 'c, 'n> {
type TcpSocket = net::socket::SocketHandle;
type Error = NetworkError;
fn open(&self, _mode: embedded_nal::Mode) -> Result<Self::TcpSocket, Self::Error> {
// TODO: Handle mode?
match self.unused_handles.borrow_mut().pop() {
Some(handle) => {
// Abort any active connections on the handle.
let mut sockets = self.sockets.borrow_mut();
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(handle);
internal_socket.abort();
Ok(handle)
}
None => Err(NetworkError::NoSocket),
}
}
fn connect(
&self,
socket: Self::TcpSocket,
remote: embedded_nal::SocketAddr,
) -> Result<Self::TcpSocket, Self::Error> {
// TODO: Handle socket mode?
let mut sockets = self.sockets.borrow_mut();
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
// If we're already in the process of connecting, ignore the request silently.
if internal_socket.is_open() {
return Ok(socket);
}
match remote.ip() {
embedded_nal::IpAddr::V4(addr) => {
let address = {
let octets = addr.octets();
net::wire::Ipv4Address::new(octets[0], octets[1], octets[2], octets[3])
};
internal_socket
.connect((address, remote.port()), self.get_ephemeral_port())
.map_err(|_| NetworkError::ConnectionFailure)?;
}
embedded_nal::IpAddr::V6(addr) => {
let address = {
let octets = addr.segments();
net::wire::Ipv6Address::new(
octets[0], octets[1], octets[2], octets[3], octets[4], octets[5],
octets[6], octets[7],
)
};
internal_socket
.connect((address, remote.port()), self.get_ephemeral_port())
.map_err(|_| NetworkError::ConnectionFailure)?;
}
};
Ok(socket) Ok(socket)
} }
// Connect to the given remote host and port.
fn connect(
&self,
mut socket: Self::TcpSocket,
remote: SocketAddr,
) -> Result<Self::TcpSocket, Self::Error> {
let result = match remote {
V4(v4_addr) => {
let ip_addr = v4_addr.ip().octets();
socket.connect((IpAddress::v4(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]), v4_addr.port()), 49500)
},
V6(v6_addr) => {
let ip_addr = v6_addr.ip().segments();
socket.connect((IpAddress::v6(ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3],
ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]), v6_addr.port()), 49500)
}
};
match result {
Ok(_) => Ok(socket),
Err(_e) => Err(_e),
}
}
/// Check if this socket is connected
fn is_connected(&self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> { fn is_connected(&self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
Ok(socket.is_active()) let mut sockets = self.sockets.borrow_mut();
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
Ok(socket.may_send() && socket.may_recv())
} }
/// Write to the stream. Returns the number of bytes written is returned
/// (which may be less than `buffer.len()`), or an error.
fn write(&self, socket: &mut Self::TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> { fn write(&self, socket: &mut Self::TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> {
if socket.can_send() { // TODO: Handle the socket mode.
socket.send_slice(buffer).map_err(nbError::Other)
} else { let mut sockets = self.sockets.borrow_mut();
Err(nbError::Other(Error::Illegal)) let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
let result = socket.send_slice(buffer);
match result {
Ok(num_bytes) => Ok(num_bytes),
Err(_) => Err(nb::Error::Other(NetworkError::WriteFailure)),
} }
} }
/// Read from the stream. Returns `Ok(n)`, which means `n` bytes of
/// data have been received and they have been placed in
/// `&buffer[0..n]`, or an error.
fn read( fn read(
&self, &self,
socket: &mut Self::TcpSocket, socket: &mut Self::TcpSocket,
buffer: &mut [u8], buffer: &mut [u8],
) -> nb::Result<usize, Self::Error> { ) -> nb::Result<usize, Self::Error> {
if socket.can_recv() { // TODO: Handle the socket mode.
socket.recv_slice(buffer).map_err(nbError::Other)
} else { let mut sockets = self.sockets.borrow_mut();
Err(nbError::Other(Error::Illegal)) let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
let result = socket.recv_slice(buffer);
match result {
Ok(num_bytes) => Ok(num_bytes),
Err(_) => Err(nb::Error::Other(NetworkError::ReadFailure)),
} }
} }
/// Close an existing TCP socket. fn close(&self, socket: Self::TcpSocket) -> Result<(), Self::Error> {
fn close(&self, mut socket: Self::TcpSocket) -> Result<(), Self::Error> { // TODO: Free the ephemeral port in use by the socket.
socket.close();
let mut sockets = self.sockets.borrow_mut();
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
internal_socket.close();
self.unused_handles.borrow_mut().push(socket).unwrap();
Ok(()) Ok(())
} }
} }