#![cfg_attr(target_os = "none", no_std)] pub mod api; use core::fmt::Write; use xous_ipc::{String, Buffer}; use num_traits::ToPrimitive; pub struct XousNames { conn: xous::CID, } impl XousNames { pub fn new() -> Result { let conn = xous::connect(xous::SID::from_bytes(b"xous-name-server").unwrap()).expect("Couldn't connect to XousNames"); Ok(XousNames { conn, }) } pub fn register_name(&self, name: &str) -> Result { let mut registration_name = String::<64>::new(); // could also do String::from_str() but in this case we want things to fail if the string is too long. write!(registration_name, "{}", name).expect("NS: name probably too long"); let mut buf = Buffer::as_buf(registration_name).or(Err(xous::Error::InternalError))?; buf.lend_mut( self.conn, api::Opcode::Register.to_u32().unwrap() ) .or(Err(xous::Error::InternalError))?; match buf.as_copy_obj().unwrap() { api::Return::SID(sid_raw) => { let sid = sid_raw.into(); xous::create_server_with_sid(sid).expect("NS: can't auto-register server"); Ok(sid) } api::Return::Failure => { Err(xous::Error::InternalError) } _ => unimplemented!("NS: unimplemented return codes") } } /// note: if this throws an AccessDenied error, you can retry with a request_authenticate_connection() call (to be written) pub fn request_connection(&self, name: &str) -> Result { let mut lookup_name = xous_ipc::String::<64>::new(); write!(lookup_name, "{}", name).expect("NS: name problably too long"); let mut buf = Buffer::as_buf(lookup_name).or(Err(xous::Error::InternalError))?; buf.lend_mut( self.conn, api::Opcode::Lookup.to_u32().unwrap() ) .or(Err(xous::Error::InternalError))?; match buf.as_copy_obj().unwrap() { api::Return::CID(cid) => Ok(cid), api::Return::AuthenticateRequest(_) => Err(xous::Error::AccessDenied), _ => Err(xous::Error::ServerNotFound), } } /// note: you probably want to use this one, to avoid synchronization issues on startup as servers register asynhcronously pub fn request_connection_blocking(&self, name: &str) -> Result { loop { match self.request_connection(name) { Ok(val) => return Ok(val), Err(xous::Error::AccessDenied) => return Err(xous::Error::AccessDenied), _ => (), } xous::yield_slice(); } } }