Compare commits
No commits in common. "9a42674bf927cbbf60a6b423108b753dd4dc6eb7" and "13bf8b6080722bd5a55f07f8a5a2217b0b913944" have entirely different histories.
9a42674bf9
...
13bf8b6080
|
@ -44,3 +44,8 @@ Select a different gdb config file from ```gdb_config``` directory.
|
||||||
set-gdb-config-file <filename>
|
set-gdb-config-file <filename>
|
||||||
```
|
```
|
||||||
Leave <filename> as blank for default openocd.gdb configuration.
|
Leave <filename> as blank for default openocd.gdb configuration.
|
||||||
|
|
||||||
|
# Known problems
|
||||||
|
|
||||||
|
This version does not compile on its own.
|
||||||
|
The SCPI crate referenced in ```cargo.toml``` refers a modified SCPI pasrsng crate stored locally.
|
||||||
|
|
|
@ -1,190 +1,109 @@
|
||||||
use core::cell::RefCell;
|
use embedded_nal::{TcpStack, Mode, SocketAddr};
|
||||||
use nb;
|
use embedded_nal::SocketAddr::{V4, V6};
|
||||||
|
|
||||||
use heapless::{consts, Vec};
|
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr};
|
||||||
|
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
|
||||||
|
use smoltcp::socket::SocketSet;
|
||||||
|
use smoltcp::socket::{SocketHandle, TcpSocket, TcpSocketBuffer};
|
||||||
|
use smoltcp::time::{Duration, Instant};
|
||||||
|
use smoltcp::Error;
|
||||||
|
|
||||||
use stm32h7_ethernet as ethernet;
|
use nb::Error as nbError;
|
||||||
use smoltcp as net;
|
use core::cell;
|
||||||
|
|
||||||
use cortex_m_semihosting::hprintln;
|
const BUFFER_SIZE: usize = 2048;
|
||||||
|
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 {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
// impl NalTcpClient {
|
||||||
pub enum NetworkError {
|
// pub fn new(socket: &'a mut TcpSocket<'a>) -> Self {
|
||||||
NoSocket,
|
|
||||||
ConnectionFailure,
|
|
||||||
ReadFailure,
|
|
||||||
WriteFailure,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct NetStorage {
|
// NalTcpClient {
|
||||||
pub ip_addrs: [net::wire::IpCidr; 1],
|
// socket,
|
||||||
pub neighbor_cache: [Option<(net::wire::IpAddress, net::iface::Neighbor)>; 8],
|
// }
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
pub type NetworkInterface =
|
impl<'a> TcpStack for &'a NalTcpClient{
|
||||||
net::iface::EthernetInterface<'static, 'static, 'static, ethernet::EthernetDMA<'static>>;
|
// The type returned when we create a new TCP socket
|
||||||
|
type TcpSocket = smoltcp::socket::TcpSocket<'a>;
|
||||||
|
// The type returned when we have an error
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
// TODO: The network stack likely needs a time-tracking mechanic here to support
|
// Open a new TCP socket. The socket starts in the unconnected state.
|
||||||
// blocking/nonblocking/timeout based operations.
|
fn open(&self, mode: Mode) -> Result<Self::TcpSocket, Self::Error> {
|
||||||
pub struct NetworkStack<'a, 'b, 'c, 'n> {
|
let tx_buffer = unsafe { TcpSocketBuffer::new(&mut TX_STORAGE[..]) };
|
||||||
network_interface: &'n mut NetworkInterface,
|
let rx_buffer = unsafe { TcpSocketBuffer::new(&mut RX_STORAGE[..]) };
|
||||||
sockets: RefCell<net::socket::SocketSet<'a, 'b, 'c>>,
|
let mut socket = TcpSocket::new(rx_buffer, tx_buffer);
|
||||||
next_port: RefCell<u16>,
|
if let Mode::Timeout(dur) = mode {
|
||||||
unused_handles: RefCell<Vec<net::socket::SocketHandle, consts::U16>>,
|
socket.set_timeout(Some(Duration::from_millis(dur.into())));
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_connected(&self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
|
// Connect to the given remote host and port.
|
||||||
let mut sockets = self.sockets.borrow_mut();
|
fn connect(
|
||||||
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
&self,
|
||||||
|
mut socket: Self::TcpSocket,
|
||||||
Ok(socket.may_send() && socket.may_recv())
|
remote: SocketAddr,
|
||||||
}
|
) -> Result<Self::TcpSocket, Self::Error> {
|
||||||
|
let result = match remote {
|
||||||
fn write(&self, socket: &mut Self::TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> {
|
V4(v4_addr) => {
|
||||||
// TODO: Handle the socket mode.
|
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)
|
||||||
let mut sockets = self.sockets.borrow_mut();
|
},
|
||||||
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
V6(v6_addr) => {
|
||||||
|
let ip_addr = v6_addr.ip().segments();
|
||||||
let result = socket.send_slice(buffer);
|
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 {
|
match result {
|
||||||
Ok(num_bytes) => Ok(num_bytes),
|
Ok(_) => Ok(socket),
|
||||||
Err(_) => Err(nb::Error::Other(NetworkError::WriteFailure)),
|
Err(_e) => Err(_e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(
|
/// Check if this socket is connected
|
||||||
&self,
|
fn is_connected(&self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
|
||||||
socket: &mut Self::TcpSocket,
|
Ok(socket.is_active())
|
||||||
buffer: &mut [u8],
|
}
|
||||||
) -> nb::Result<usize, Self::Error> {
|
|
||||||
// TODO: Handle the socket mode.
|
|
||||||
|
|
||||||
let mut sockets = self.sockets.borrow_mut();
|
/// Write to the stream. Returns the number of bytes written is returned
|
||||||
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
/// (which may be less than `buffer.len()`), or an error.
|
||||||
|
fn write(&self, socket: &mut Self::TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> {
|
||||||
let result = socket.recv_slice(buffer);
|
if socket.can_send() {
|
||||||
|
socket.send_slice(buffer).map_err(nbError::Other)
|
||||||
match result {
|
} else {
|
||||||
Ok(num_bytes) => Ok(num_bytes),
|
Err(nbError::Other(Error::Illegal))
|
||||||
Err(_) => Err(nb::Error::Other(NetworkError::ReadFailure)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&self, socket: Self::TcpSocket) -> Result<(), Self::Error> {
|
/// Read from the stream. Returns `Ok(n)`, which means `n` bytes of
|
||||||
// TODO: Free the ephemeral port in use by the socket.
|
/// data have been received and they have been placed in
|
||||||
|
/// `&buffer[0..n]`, or an error.
|
||||||
|
fn read(
|
||||||
|
&self,
|
||||||
|
socket: &mut Self::TcpSocket,
|
||||||
|
buffer: &mut [u8],
|
||||||
|
) -> nb::Result<usize, Self::Error> {
|
||||||
|
if socket.can_recv() {
|
||||||
|
socket.recv_slice(buffer).map_err(nbError::Other)
|
||||||
|
} else {
|
||||||
|
Err(nbError::Other(Error::Illegal))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut sockets = self.sockets.borrow_mut();
|
/// Close an existing TCP socket.
|
||||||
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
|
fn close(&self, mut socket: Self::TcpSocket) -> Result<(), Self::Error> {
|
||||||
internal_socket.close();
|
socket.close();
|
||||||
|
|
||||||
self.unused_handles.borrow_mut().push(socket).unwrap();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue