1
0
Fork 0

answer ARTIQ SystemInfo

This commit is contained in:
Sebastien Bourdeauducq 2020-04-12 17:44:32 +08:00
parent d3d2d75489
commit a56221fcfb
3 changed files with 159 additions and 23 deletions

63
Cargo.lock generated
View File

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]] [[package]]
name = "bit_field" name = "bit_field"
version = "0.10.0" version = "0.10.0"
@ -90,12 +96,50 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fdcec5e97041c7f0f1c5b7d93f12e57293c831c646f4cc7a5db59460c7ea8de6" checksum = "fdcec5e97041c7f0f1c5b7d93f12e57293c831c646f4cc7a5db59460c7ea8de6"
[[package]]
name = "num-derive"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c8b15b261814f992e33760b1fca9fe8b693d8a65299f20c9901688636cfb746"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-traits"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "pin-utils" name = "pin-utils"
version = "0.1.0-alpha.4" version = "0.1.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587" checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
[[package]]
name = "proc-macro2"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "r0" name = "r0"
version = "1.0.0" version = "1.0.0"
@ -109,6 +153,8 @@ dependencies = [
"libasync", "libasync",
"libboard_zynq", "libboard_zynq",
"libsupport_zynq", "libsupport_zynq",
"num-derive",
"num-traits",
] ]
[[package]] [[package]]
@ -122,6 +168,23 @@ dependencies = [
"managed", "managed",
] ]
[[package]]
name = "syn"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
[[package]] [[package]]
name = "vcell" name = "vcell"
version = "0.1.2" version = "0.1.2"

View File

@ -13,3 +13,8 @@ default = ["target_zc706"]
libboard_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libboard_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" }
libsupport_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libsupport_zynq = { git = "https://git.m-labs.hk/M-Labs/zc706.git" }
libasync = { git = "https://git.m-labs.hk/M-Labs/zc706.git" } libasync = { git = "https://git.m-labs.hk/M-Labs/zc706.git" }
num-derive = "0.3"
[dependencies.num-traits]
version = "0.2"
default-features = false

View File

@ -1,6 +1,9 @@
use core::{mem::transmute, task::Poll}; use core::{mem::transmute, task::Poll};
use core::fmt;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use alloc::{borrow::ToOwned, format};
use libboard_zynq::{ use libboard_zynq::{
println, println,
self as zynq, self as zynq,
@ -16,36 +19,101 @@ use libsupport_zynq::alloc::{vec, vec::Vec};
use libasync::smoltcp::{Sockets, TcpStream}; use libasync::smoltcp::{Sockets, TcpStream};
async fn handle_connection(stream: TcpStream) -> smoltcp::Result<()> { #[derive(Debug, Clone, Copy, PartialEq, Eq)]
stream.send("Enter your name: ".bytes()).await?; pub enum Error {
let name = stream.recv(|buf| { NetworkError(smoltcp::Error),
UnexpectedPattern,
UnrecognizedPacket,
}
pub type Result<T> = core::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::NetworkError(error) => write!(f, "network error: {}", error),
&Error::UnexpectedPattern => write!(f, "unexpected pattern"),
&Error::UnrecognizedPacket => write!(f, "unrecognized packet"),
}
}
}
impl From<smoltcp::Error> for Error {
fn from(error: smoltcp::Error) -> Self {
Error::NetworkError(error)
}
}
async fn expect(stream: &TcpStream, pattern: &[u8]) -> Result<()> {
stream.recv(|buf| {
for (i, b) in buf.iter().enumerate() { for (i, b) in buf.iter().enumerate() {
if *b == '\n' as u8 { if *b == pattern[i] {
return match core::str::from_utf8(&buf[0..i]) { if i + 1 == pattern.len() {
Ok(name) => return Poll::Ready((i + 1, Ok(())));
Poll::Ready((i + 1, Some(name.to_owned()))), }
Err(_) => } else {
Poll::Ready((i + 1, None)) return Poll::Ready((i + 1, Err(Error::UnexpectedPattern)));
};
} }
} }
if buf.len() > 100 { Poll::Pending
// Too much input, consume all }).await?
Poll::Ready((buf.len(), None)) }
async fn read_u8(stream: &TcpStream) -> Result<u8> {
Ok(stream.recv(|buf| {
if buf.len() >= 1 {
Poll::Ready((1, buf[0]))
} else { } else {
Poll::Pending Poll::Pending
} }
}).await?; }).await?)
match name { }
Some(name) =>
stream.send(format!("Hello {}!\n", name).bytes()).await?, #[derive(FromPrimitive, ToPrimitive)]
None => enum Request {
stream.send("I had trouble reading your name.\n".bytes()).await?, SystemInfo = 3,
} LoadKernel = 5,
stream.flush().await; RunKernel = 6,
RPCReply = 7,
RPCException = 8,
}
#[derive(FromPrimitive, ToPrimitive)]
enum Reply {
SystemInfo = 2,
LoadCompleted = 5,
LoadFailed = 6,
KernelFinished = 7,
KernelStartupFailed = 8,
KernelException = 9,
RPCRequest = 10,
WatchdogExpired = 14,
ClockFailure = 15,
}
async fn send_header(stream: &TcpStream, reply: Reply) -> Result<()> {
stream.send([0x5a, 0x5a, 0x5a, 0x5a, reply.to_u8().unwrap()].iter().copied()).await?;
Ok(()) Ok(())
} }
async fn handle_connection(stream: TcpStream) -> Result<()> {
expect(&stream, b"ARTIQ coredev\n").await?;
loop {
expect(&stream, &[0x5a, 0x5a, 0x5a, 0x5a]).await?;
let request: Request = FromPrimitive::from_u8(read_u8(&stream).await?)
.ok_or(Error::UnrecognizedPacket)?;
match request {
Request::SystemInfo => {
send_header(&stream, Reply::SystemInfo).await?;
stream.send("ARZQ".bytes()).await?;
},
_ => return Err(Error::UnrecognizedPacket)
}
}
}
const HWADDR: [u8; 6] = [0, 0x23, 0xab, 0xad, 0x1d, 0xea]; const HWADDR: [u8; 6] = [0, 0x23, 0xab, 0xad, 0x1d, 0xea];
const IPADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 52])); const IPADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 52]));
@ -89,7 +157,7 @@ pub fn network_main() {
TcpStream::listen(1381, 2048, 2048, 8, |stream| async { TcpStream::listen(1381, 2048, 2048, 8, |stream| async {
let _ = handle_connection(stream) let _ = handle_connection(stream)
.await .await
.map_err(|e| println!("Connection: {:?}", e)); .map_err(|e| println!("Connection: {}", e));
}); });
let mut time = 0u32; let mut time = 0u32;