re-org api so each protocol has its own file

This commit is contained in:
bunnie 2021-10-18 17:33:20 +08:00
parent 98bbe848fc
commit 84a8bf05f8
3 changed files with 72 additions and 58 deletions

View file

@ -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<IpAddress> 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<u16>, // 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 {
}
}
}
}
}

View file

@ -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<u16>, // 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,
}

View file

@ -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<u8>,
}
///////// UdpSocket implementation
pub struct UdpSocket{
net: NetConn,
cb_sid: SID,
@ -33,6 +38,7 @@ pub struct UdpSocket{
read_timeout: Option<Duration>,
write_timeout: Option<Duration>,
ticktimer: ticktimer_server::Ticktimer,
dest_addr: Option<SocketAddr>,
}
// 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<usize> {
unimplemented!("work in progress")
}
pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
unimplemented!("work in progress")
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
unimplemented!("work in progress")
}
@ -297,10 +311,6 @@ impl UdpSocket {
unimplemented!("work in progress")
}
pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
unimplemented!("work in progress")
}
pub fn duplicate(&self) -> io::Result<UdpSocket> {
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")
}