From a3adf797b01515b120c1ea33bef8c2648f602739 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 1 Apr 2021 17:20:36 +0800 Subject: [PATCH] refactor TRNG server to new API --- Cargo.lock | 2 ++ services/trng/Cargo.toml | 2 ++ services/trng/src/api.rs | 36 +++++------------------------- services/trng/src/lib.rs | 47 ++++++++++++++++++++++++++++----------- services/trng/src/main.rs | 27 ++++++++++++---------- xous-rs/src/names.rs | 1 - 6 files changed, 58 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 34e9ef1a5..f35d9617b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1378,6 +1378,8 @@ dependencies = [ "heapless", "log", "log-server", + "num-derive", + "num-traits", "ticktimer-server", "utralib", "xous", diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index f82ad83b5..a16aa8f69 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -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] diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index 48a83387d..96ecb5b5b 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -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 { - 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 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, } diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 45f98edcb..5baddfd55 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -2,21 +2,42 @@ pub mod api; use xous::{CID, send_message}; +use num_traits::ToPrimitive; -pub fn get_u32(cid: CID) -> Result { - 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 { - let response = send_message(cid, api::Opcode::GetTrng(2).into())?; +impl Trng { + pub fn new(xns: xous_names::XousNames) -> Result { + 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 { + 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 { + 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); + } } } diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index 8a309ef11..48f82b828 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -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"); } } } diff --git a/xous-rs/src/names.rs b/xous-rs/src/names.rs index 071ea843b..590ce58b1 100644 --- a/xous-rs/src/names.rs +++ b/xous-rs/src/names.rs @@ -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_";