diff --git a/services/net/src/api.rs b/services/net/src/api.rs index cf8542a2d..d5e4cdbbd 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -1,3 +1,6 @@ +pub(crate) mod udp; +pub(crate) use udp::*; + use rkyv::{Archive, Deserialize, Serialize}; use std::net::SocketAddr; use smoltcp::wire::IpAddress; @@ -8,6 +11,7 @@ pub(crate) const SERVER_NAME_NET: &str = "_Middleware Network Server_"; pub(crate) enum Opcode { UdpBind, UdpClose, + UdpTx, /// initiates a ping packet //PingSend, @@ -31,12 +35,6 @@ pub(crate) enum NetCallback { Drop, } -#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] -pub(crate) enum NetUdpCallback { - RxData, - Drop, -} - #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] pub(crate) enum NetIpAddr { Ipv4([u8; 4]), @@ -70,49 +68,6 @@ impl From for NetIpAddr { } } -pub(crate) const UDP_RESPONSE_MAX_LEN: usize = 1800; -/// The data field for a UDP response is limited to less than the theoretical -/// size of 64k. While UDP allows for a 64k packet, it seems no protoctols -/// in practice utilize such a length (about 512 bytes is the biggest), due -/// to MTU limitations downstream. Within Xous, memory is shared on a page-basis, -/// which is 4096 bytes, so the cost to share a page is almost the same regardless -/// of its size, as long as it is smaller than 4096 bytes. Hence, the number -/// 1800 bytes is picked to be a bit larger than our wifi MTU, but small -/// enough to fit in a page of RAM. Why not make it even bigger? Mainly to save -/// on the cost to repeatedly zeroize parts of RAM that are never used. -#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) struct NetUdpResponse { - pub endpoint_ip_addr: NetIpAddr, - pub len: u16, - pub endpoint_port: u16, - pub data: [u8; UDP_RESPONSE_MAX_LEN], -} - -#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) struct ScalarHook { - pub sid: (u32, u32, u32, u32), - pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API) - pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking. - pub token: Option<[u32; 4]>, // 128-bit random token used to identify a connection to the net crate -} - -#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) struct NetUdpBind { - pub(crate) cb_sid: [u32; 4], - pub(crate) ip_addr: NetIpAddr, - pub(crate) port: u16, - pub(crate) max_payload: Option, // defaults to MTU if not specified -} - -#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] -pub(crate) enum NetMemResponse { - Ok, - OutOfMemory, - SocketInUse, - AccessDenied, - Invalid, -} - /// This defines a Xous Scalar message endpoint. Used for defining /// notification messages for incoming packets, for main loops that want to be /// able to process both events from Xous and from the network. @@ -165,4 +120,4 @@ impl XousScalarEndpoint { } } } -} \ No newline at end of file +} diff --git a/services/net/src/api/udp.rs b/services/net/src/api/udp.rs new file mode 100644 index 000000000..d7036aacb --- /dev/null +++ b/services/net/src/api/udp.rs @@ -0,0 +1,53 @@ +use rkyv::{Archive, Deserialize, Serialize}; +use crate::api::*; + +//////// Intra-crate UDP structures +pub(crate) const UDP_RESPONSE_MAX_LEN: usize = 1800; +/// The data field for a UDP response is limited to less than the theoretical +/// size of 64k. While UDP allows for a 64k packet, it seems no protoctols +/// in practice utilize such a length (about 512 bytes is the biggest), due +/// to MTU limitations downstream. Within Xous, memory is shared on a page-basis, +/// which is 4096 bytes, so the cost to share a page is almost the same regardless +/// of its size, as long as it is smaller than 4096 bytes. Hence, the number +/// 1800 bytes is picked to be a bit larger than our wifi MTU, but small +/// enough to fit in a page of RAM. Why not make it even bigger? Mainly to save +/// on the cost to repeatedly zeroize parts of RAM that are never used. +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct NetUdpResponse { + pub endpoint_ip_addr: NetIpAddr, + pub len: u16, + pub endpoint_port: u16, + pub data: [u8; UDP_RESPONSE_MAX_LEN], +} + +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct ScalarHook { + pub sid: (u32, u32, u32, u32), + pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API) + pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking. + pub token: Option<[u32; 4]>, // 128-bit random token used to identify a connection to the net crate +} + +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) struct NetUdpBind { + pub(crate) cb_sid: [u32; 4], + pub(crate) ip_addr: NetIpAddr, + pub(crate) port: u16, + pub(crate) max_payload: Option, // defaults to MTU if not specified +} + +#[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone)] +pub(crate) enum NetMemResponse { + Ok, + OutOfMemory, + SocketInUse, + AccessDenied, + Invalid, +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum NetUdpCallback { + RxData, + Drop, +} + diff --git a/services/net/src/backend/udp.rs b/services/net/src/backend/udp.rs index 2260e7df1..2bdb7545a 100644 --- a/services/net/src/backend/udp.rs +++ b/services/net/src/backend/udp.rs @@ -17,12 +17,17 @@ use xous::{CID, SID, Message, msg_blocking_scalar_unpack}; use xous_ipc::Buffer; use crate::NetConn; use crate::api::*; +//use crate::api::udp::*; use num_traits::*; +//////// Public structures pub struct UdpRx { pub endpoint: IpEndpoint, pub data: Vec, } + + +///////// UdpSocket implementation pub struct UdpSocket{ net: NetConn, cb_sid: SID, @@ -33,6 +38,7 @@ pub struct UdpSocket{ read_timeout: Option, write_timeout: Option, ticktimer: ticktimer_server::Ticktimer, + dest_addr: Option, } // next steps: build this stub, and figure out how to clean up the error handling code. @@ -139,6 +145,7 @@ impl UdpSocket { read_timeout: None, write_timeout: None, ticktimer: ticktimer_server::Ticktimer::new().unwrap(), + dest_addr: None, }) }, _ => { @@ -284,11 +291,18 @@ impl UdpSocket { self.recv_from_inner(pkt, true) } + pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> { + unimplemented!("work in progress") + } pub fn send(&self, _: &[u8]) -> io::Result { unimplemented!("work in progress") } + pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result { + unimplemented!("work in progress") + } + pub fn peer_addr(&self) -> io::Result { unimplemented!("work in progress") } @@ -297,10 +311,6 @@ impl UdpSocket { unimplemented!("work in progress") } - pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result { - unimplemented!("work in progress") - } - pub fn duplicate(&self) -> io::Result { unimplemented!("work in progress") } @@ -353,10 +363,6 @@ impl UdpSocket { unimplemented!("work in progress") } - pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> { - unimplemented!("work in progress") - } - pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> { unimplemented!("ipv6 not implemented") }