refactor TRNG server to new API

This commit is contained in:
bunnie 2021-04-01 17:20:36 +08:00
parent 2407f17267
commit a3adf797b0
6 changed files with 58 additions and 57 deletions

2
Cargo.lock generated
View file

@ -1378,6 +1378,8 @@ dependencies = [
"heapless",
"log",
"log-server",
"num-derive",
"num-traits",
"ticktimer-server",
"utralib",
"xous",

View file

@ -12,6 +12,8 @@ log-server = { path = "../log-server" }
ticktimer-server = { path = "../ticktimer-server" }
xous-names = { path = "../xous-names" }
log = "0.4"
num-derive = {version = "0.3", default-features = false}
num-traits = {version = "0.2", default-features = false}
[target.'cfg(not(any(windows,unix)))'.dependencies]

View file

@ -1,36 +1,10 @@
use xous::{Message, ScalarMessage};
pub(crate) const SERVER_NAME_TRNG: &str = "_TRNG manager_";
#[allow(dead_code)]
#[derive(Debug)]
pub enum Opcode {
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Opcode {
/// Get one or two 32-bit words of TRNG data
GetTrng(usize),
}
impl core::convert::TryFrom<& Message> for Opcode {
type Error = &'static str;
fn try_from(message: & Message) -> Result<Self, Self::Error> {
match message {
Message::BlockingScalar(m) => match m.id {
0 => Ok(Opcode::GetTrng(m.arg1)),
_ => Err("TRNG api: unknown BlockingScalar ID"),
},
_ => Err("TRNG api: unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::GetTrng(count) => Message::BlockingScalar(ScalarMessage {
id: 0,
arg1: count.into(),
arg2: 0,
arg3: 0,
arg4: 0,
}),
// _ => panic!("TRNG api: Opcode type not handled by Into(), refer to helper method"),
}
}
GetTrng,
}

View file

@ -2,21 +2,42 @@
pub mod api;
use xous::{CID, send_message};
use num_traits::ToPrimitive;
pub fn get_u32(cid: CID) -> Result<u32, xous::Error> {
let response = send_message(cid, api::Opcode::GetTrng(1).into())?;
if let xous::Result::Scalar2(trng, _) = response {
Ok(trng as u32)
} else {
panic!("unexpected return value: {:#?}", response);
}
pub struct Trng {
conn: CID,
}
pub fn get_u64(cid: CID) -> Result<u64, xous::Error> {
let response = send_message(cid, api::Opcode::GetTrng(2).into())?;
impl Trng {
pub fn new(xns: xous_names::XousNames) -> Result<Self, xous::Error> {
let conn = xns.request_connection_blocking(api::SERVER_NAME_TRNG).expect("Can't connect to TRNG server");
Ok(Trng {
conn
})
}
pub fn get_u32(&self) -> Result<u32, xous::Error> {
let response = send_message(self.conn,
xous::Message::BlockingScalar(xous::ScalarMessage {
id: api::Opcode::GetTrng.to_usize().unwrap(),
arg1: 1 /* count */, arg2: 0, arg3: 0, arg4: 0
})
).expect("TRNG|LIB: can't get_u32");
if let xous::Result::Scalar2(trng, _) = response {
Ok(trng as u32)
} else {
panic!("unexpected return value: {:#?}", response);
}
}
pub fn get_u64(&self) -> Result<u64, xous::Error> {
let response = send_message(self.conn,
xous::Message::BlockingScalar(xous::ScalarMessage {
id: api::Opcode::GetTrng.to_usize().unwrap(),
arg1: 2 /* count */, arg2: 0, arg3: 0, arg4: 0
})
).expect("TRNG|LIB: can't get_u32");
if let xous::Result::Scalar2(lo, hi) = response {
Ok( lo as u64 | ((hi as u64) << 32) )
} else {
panic!("unexpected return value: {:#?}", response);
Ok( lo as u64 | ((hi as u64) << 32) )
} else {
panic!("unexpected return value: {:#?}", response);
}
}
}

View file

@ -4,7 +4,7 @@
mod api;
use api::*;
use core::convert::TryFrom;
use num_traits::FromPrimitive;
use log::{error, info};
@ -154,7 +154,7 @@ fn xmain() -> ! {
info!("TRNG: my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();
let trng_sid = xns.register_name(xous::names::SERVER_NAME_TRNG).expect("TRNG: can't register server");
let trng_sid = xns.register_name(api::SERVER_NAME_TRNG).expect("TRNG: can't register server");
info!("TRNG: registered with NS -- {:?}", trng_sid);
#[cfg(target_os = "none")]
@ -166,17 +166,20 @@ fn xmain() -> ! {
info!("TRNG: ready to accept requests");
loop {
let envelope = xous::receive_message(trng_sid).unwrap();
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
match opcode {
Opcode::GetTrng(count) => {
let val: [u32; 2] = trng.get_trng(count);
xous::return_scalar2(envelope.sender, val[0] as _, val[1] as _)
.expect("TRNG: couldn't return GetTrng request");
},
let msg = xous::receive_message(trng_sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(api::Opcode::GetTrng) => {
if let xous::Message::BlockingScalar(xous::ScalarMessage {
id: _, arg1: count, arg2: _, arg3: _, arg4: _
}) = msg.body {
let val: [u32; 2] = trng.get_trng(count);
xous::return_scalar2(msg.sender, val[0] as _, val[1] as _)
.expect("TRNG: couldn't return GetTrng request");
} else { log::error!("TRNG: GetTrng malformed message"); }
}
None => {
log::error!("TRNG: couldn't convert opcode")
}
} else {
error!("KBD: couldn't convert opcode");
}
}
}

View file

@ -10,7 +10,6 @@ pub const SERVER_NAME_LLIO: &str = "_Low Level I/O manager_";
pub const SERVER_NAME_SHELL: &str = "_Shell_";
pub const SERVER_NAME_GFX: &str = "_Graphics_";
pub const SERVER_NAME_KBD: &str = "_Matrix keyboard driver_";
pub const SERVER_NAME_TRNG: &str = "_TRNG manager_";
pub const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_";
pub const SERVER_NAME_STATUS: &str = "_Status bar manager_";
pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_";