From 1075dff5592139e9745d2dcc17fef488f68f91da Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 4 Jan 2021 23:34:36 +0800 Subject: [PATCH] the name server itself --- services/xous-names/Cargo.toml | 21 ++++++++ services/xous-names/src/api.rs | 92 +++++++++++++++++++++++++++++++++ services/xous-names/src/lib.rs | 5 ++ services/xous-names/src/main.rs | 41 +++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 services/xous-names/Cargo.toml create mode 100644 services/xous-names/src/api.rs create mode 100644 services/xous-names/src/lib.rs create mode 100644 services/xous-names/src/main.rs diff --git a/services/xous-names/Cargo.toml b/services/xous-names/Cargo.toml new file mode 100644 index 000000000..850fbf1c7 --- /dev/null +++ b/services/xous-names/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "xous-names" +version = "0.1.0" +authors = ["bunnie "] +edition = "2018" +description = "" + +[dependencies] +xous = { path = "../../xous-rs" } +heapless = "0.5" +log-server = { path = "../log-server" } +log = "0.4" + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} + +[target.'cfg(any(windows,unix))'.dependencies] + +[features] +debugprint = [] +default = [] # "debugprint" diff --git a/services/xous-names/src/api.rs b/services/xous-names/src/api.rs new file mode 100644 index 000000000..23b68017c --- /dev/null +++ b/services/xous-names/src/api.rs @@ -0,0 +1,92 @@ +use xous::{Message, ScalarMessage, SID}; + +pub struct Registration { + pub sid: SID, + pub name: [u8; 64], + pub name_len: usize, + pub success: bool, + // _padding: [u8; 4096-(core::mem::size_of::() + 64 + core::mem::size_of::() + core::mem::size_of::], +} + +pub struct Lookup { + sid: SID, + name: [u8; 64], + name_len: usize, + success: bool, + autheticate: bool, + challenge: [u32; 8], + response: [u32; 8], + // _padding: [u8; 4096-(core::mem::size_of::() + 64 + core::mem::size_of::() + core::mem::size_of::], +} + +pub const ID_REGISTER_NAME: usize = 0; +pub const ID_LOOKUP_NAME: usize = 1; +pub const ID_AUTHENTICATE: usize = 2; + +/* +#[derive(Debug)] +pub enum Opcode { + /// Register a 128-bit SID with a preferred, unique lookup name + RegisterName(Registration), + + /// Retrieve the 128-bit SID for a server, given its unique lookup name + LookupName(Lookup), + + /// Authenticate to the name server, based on nonce embedded in LookupName response + /// this is always as a response to a lookupname that reqeusts the authentication challenge-response + Authenticate(Lookup), +} + +impl<'a> core::convert::TryFrom<&'a Message> for Opcode { + type Error = &'static str; + fn try_from(message: &'a Message) -> Result { + match message { + Message::MutableBorrow(m) => { + if m.id as u16 == ID_REGISTER_NAME { + let registration: Registration = unsafe { + &mut *(m.buf.as_mut_ptr() as *mut Registration) + }; + Ok(Opcode::RegisterName(registration)) + } + } + _ => Err("unhandled message type"), + } + } +}*/ + +/* No "Into<>" methods -- we use the ipc::lend() to create messages +impl Into for Opcode { + fn into(self) -> Message { + match self { + Opcode::Reset => Message::Scalar(ScalarMessage { + id: 1, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), + Opcode::RecalculateSleep => Message::Scalar(ScalarMessage { + id: 131072, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), + Opcode::ElapsedMs => Message::BlockingScalar(ScalarMessage { + id: 4919, + arg1: 0, + arg2: 0, + arg3: 0, + arg4: 0, + }), + Opcode::SleepMs(ms) => Message::BlockingScalar(ScalarMessage { + id: 3, + arg1: ms, + arg2: 0, + arg3: 0, + arg4: 0, + }), + } + } +} +*/ \ No newline at end of file diff --git a/services/xous-names/src/lib.rs b/services/xous-names/src/lib.rs new file mode 100644 index 000000000..315ae600b --- /dev/null +++ b/services/xous-names/src/lib.rs @@ -0,0 +1,5 @@ +#![cfg_attr(target_os = "none", no_std)] + +pub mod api; + +use xous::{send_message, Error, CID}; diff --git a/services/xous-names/src/main.rs b/services/xous-names/src/main.rs new file mode 100644 index 000000000..611a9971e --- /dev/null +++ b/services/xous-names/src/main.rs @@ -0,0 +1,41 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +use api::*; + +use heapless::String; +use heapless::Vec; +use heapless::consts::*; + +use log::{error, info}; + +#[xous::xous_main] +fn xmain() -> ! { + log_server::init_wait().unwrap(); + + let name_server = + xous::create_server_with_address(b"xousnames-server").expect("Couldn't create xousnames-server"); + + info!("NS: started"); + loop { + let envelope = xous::receive_message(name_server).unwrap(); + info!("NS: received message"); + if let xous::Message::MutableBorrow(m) = &envelope.body { + if m.id == ID_REGISTER_NAME { + let registration: &mut Registration = unsafe { + &mut *(m.buf.as_mut_ptr() as *mut Registration) + }; + let namevec: Vec = Vec::from_slice(®istration.name).unwrap(); + let name: String = String::from_utf8(namevec).unwrap(); + info!("NS: registration request for {}", name); + registration.success = true; + xous::return_memory(envelope.sender, m.buf).unwrap(); + } else { + error!("NS: unknown message ID received"); + } + } else { + error!("NS: couldn't convert opcode"); + } + } +}