|  | 
|  | 1 | +use crate::os::xous::ffi::Connection; | 
|  | 2 | +use crate::os::xous::services::connect; | 
|  | 3 | +use core::sync::atomic::{AtomicU32, Ordering}; | 
|  | 4 | + | 
|  | 5 | +pub(crate) enum NetBlockingScalar { | 
|  | 6 | +    StdGetTtlUdp(u16 /* fd */),                /* 36 */ | 
|  | 7 | +    StdSetTtlUdp(u16 /* fd */, u32 /* ttl */), /* 37 */ | 
|  | 8 | +    StdGetTtlTcp(u16 /* fd */),                /* 36 */ | 
|  | 9 | +    StdSetTtlTcp(u16 /* fd */, u32 /* ttl */), /* 37 */ | 
|  | 10 | +    StdGetNodelay(u16 /* fd */),               /* 38 */ | 
|  | 11 | +    StdSetNodelay(u16 /* fd */, bool),         /* 39 */ | 
|  | 12 | +    StdTcpClose(u16 /* fd */),                 /* 34 */ | 
|  | 13 | +    StdUdpClose(u16 /* fd */),                 /* 41 */ | 
|  | 14 | +    StdTcpStreamShutdown(u16 /* fd */, crate::net::Shutdown /* how */), /* 46 */ | 
|  | 15 | +} | 
|  | 16 | + | 
|  | 17 | +pub(crate) enum NetLendMut { | 
|  | 18 | +    StdTcpConnect,                                    /* 30 */ | 
|  | 19 | +    StdTcpTx(u16 /* fd */),                           /* 31 */ | 
|  | 20 | +    StdTcpPeek(u16 /* fd */, bool /* nonblocking */), /* 32 */ | 
|  | 21 | +    StdTcpRx(u16 /* fd */, bool /* nonblocking */),   /* 33 */ | 
|  | 22 | +    StdGetAddress(u16 /* fd */),                      /* 35 */ | 
|  | 23 | +    StdUdpBind,                                       /* 40 */ | 
|  | 24 | +    StdUdpRx(u16 /* fd */),                           /* 42 */ | 
|  | 25 | +    StdUdpTx(u16 /* fd */),                           /* 43 */ | 
|  | 26 | +    StdTcpListen,                                     /* 44 */ | 
|  | 27 | +    StdTcpAccept(u16 /* fd */),                       /* 45 */ | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +impl Into<usize> for NetLendMut { | 
|  | 31 | +    fn into(self) -> usize { | 
|  | 32 | +        match self { | 
|  | 33 | +            NetLendMut::StdTcpConnect => 30, | 
|  | 34 | +            NetLendMut::StdTcpTx(fd) => 31 | ((fd as usize) << 16), | 
|  | 35 | +            NetLendMut::StdTcpPeek(fd, blocking) => { | 
|  | 36 | +                32 | ((fd as usize) << 16) | if blocking { 0x8000 } else { 0 } | 
|  | 37 | +            } | 
|  | 38 | +            NetLendMut::StdTcpRx(fd, blocking) => { | 
|  | 39 | +                33 | ((fd as usize) << 16) | if blocking { 0x8000 } else { 0 } | 
|  | 40 | +            } | 
|  | 41 | +            NetLendMut::StdGetAddress(fd) => 35 | ((fd as usize) << 16), | 
|  | 42 | +            NetLendMut::StdUdpBind => 40, | 
|  | 43 | +            NetLendMut::StdUdpRx(fd) => 42 | ((fd as usize) << 16), | 
|  | 44 | +            NetLendMut::StdUdpTx(fd) => 43 | ((fd as usize) << 16), | 
|  | 45 | +            NetLendMut::StdTcpListen => 44, | 
|  | 46 | +            NetLendMut::StdTcpAccept(fd) => 45 | ((fd as usize) << 16), | 
|  | 47 | +        } | 
|  | 48 | +    } | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +impl<'a> Into<[usize; 5]> for NetBlockingScalar { | 
|  | 52 | +    fn into(self) -> [usize; 5] { | 
|  | 53 | +        match self { | 
|  | 54 | +            NetBlockingScalar::StdGetTtlTcp(fd) => [36 | ((fd as usize) << 16), 0, 0, 0, 0], | 
|  | 55 | +            NetBlockingScalar::StdGetTtlUdp(fd) => [36 | ((fd as usize) << 16), 0, 0, 0, 1], | 
|  | 56 | +            NetBlockingScalar::StdSetTtlTcp(fd, ttl) => { | 
|  | 57 | +                [37 | ((fd as usize) << 16), ttl as _, 0, 0, 0] | 
|  | 58 | +            } | 
|  | 59 | +            NetBlockingScalar::StdSetTtlUdp(fd, ttl) => { | 
|  | 60 | +                [37 | ((fd as usize) << 16), ttl as _, 0, 0, 1] | 
|  | 61 | +            } | 
|  | 62 | +            NetBlockingScalar::StdGetNodelay(fd) => [38 | ((fd as usize) << 16), 0, 0, 0, 0], | 
|  | 63 | +            NetBlockingScalar::StdSetNodelay(fd, enabled) => { | 
|  | 64 | +                [39 | ((fd as usize) << 16), if enabled { 1 } else { 0 }, 0, 0, 1] | 
|  | 65 | +            } | 
|  | 66 | +            NetBlockingScalar::StdTcpClose(fd) => [34 | ((fd as usize) << 16), 0, 0, 0, 0], | 
|  | 67 | +            NetBlockingScalar::StdUdpClose(fd) => [41 | ((fd as usize) << 16), 0, 0, 0, 0], | 
|  | 68 | +            NetBlockingScalar::StdTcpStreamShutdown(fd, how) => [ | 
|  | 69 | +                46 | ((fd as usize) << 16), | 
|  | 70 | +                match how { | 
|  | 71 | +                    crate::net::Shutdown::Read => 1, | 
|  | 72 | +                    crate::net::Shutdown::Write => 2, | 
|  | 73 | +                    crate::net::Shutdown::Both => 3, | 
|  | 74 | +                }, | 
|  | 75 | +                0, | 
|  | 76 | +                0, | 
|  | 77 | +                0, | 
|  | 78 | +            ], | 
|  | 79 | +        } | 
|  | 80 | +    } | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +/// Return a `Connection` to the Network server. This server provides all | 
|  | 84 | +/// OS-level networking functions. | 
|  | 85 | +pub(crate) fn net_server() -> Connection { | 
|  | 86 | +    static NET_CONNECTION: AtomicU32 = AtomicU32::new(0); | 
|  | 87 | +    let cid = NET_CONNECTION.load(Ordering::Relaxed); | 
|  | 88 | +    if cid != 0 { | 
|  | 89 | +        return cid.into(); | 
|  | 90 | +    } | 
|  | 91 | + | 
|  | 92 | +    let cid = connect("_Middleware Network Server_").unwrap(); | 
|  | 93 | +    NET_CONNECTION.store(cid.into(), Ordering::Relaxed); | 
|  | 94 | +    cid | 
|  | 95 | +} | 
0 commit comments