xous-core/services/xous-names/src/main.rs

119 lines
5.4 KiB
Rust
Raw Normal View History

2021-01-04 15:34:36 +00:00
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
2021-01-04 20:40:34 +00:00
extern crate hash32_derive;
2021-01-04 15:34:36 +00:00
mod api;
2021-02-12 17:42:31 +00:00
use xous::buffer;
2021-01-04 15:34:36 +00:00
2021-01-05 08:53:19 +00:00
// use heapless::String;
2021-01-04 15:34:36 +00:00
use heapless::consts::*;
use heapless::FnvIndexMap;
2021-01-04 15:34:36 +00:00
use core::convert::TryInto;
2021-01-04 15:34:36 +00:00
use log::{error, info};
const FAIL_TIMEOUT_MS: u64 = 100;
2021-01-04 15:34:36 +00:00
#[xous::xous_main]
fn xmain() -> ! {
2021-03-10 20:12:14 +00:00
let debug1 = false;
2021-01-04 15:34:36 +00:00
log_server::init_wait().unwrap();
info!("NS: my PID is {}", xous::process::id());
2021-01-04 15:34:36 +00:00
let name_server = xous::create_server_with_address(b"xous-name-server")
.expect("Couldn't create xousnames-server");
2021-01-04 20:40:34 +00:00
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap();
// this limits the number of available servers to be requested to 128...!
let mut name_table = FnvIndexMap::<_, _, U128>::new();
2021-01-04 15:34:36 +00:00
info!("NS: started");
use core::pin::Pin;
use rkyv::{archived_value_mut, Unarchive};
2021-01-04 15:34:36 +00:00
loop {
let envelope = xous::receive_message(name_server).unwrap();
if debug1{info!("NS: received message");}
2021-01-04 15:34:36 +00:00
if let xous::Message::MutableBorrow(m) = &envelope.body {
let mut buf = unsafe { buffer::XousBuffer::from_memory_message(m) };
let value = unsafe {
archived_value_mut::<api::Request>(Pin::new(buf.as_mut()), m.id.try_into().unwrap())
};
let new_value = match &*value {
rkyv::Archived::<api::Request>::Register(registration_name) => {
use rkyv::Unarchive;
let name = registration_name.unarchive();
if debug1{info!("NS: registration request for '{}'", name);}
if !name_table.contains_key(&name) {
let new_sid =
xous::create_server_id().expect("NS: create server failed, maybe OOM?");
name_table
.insert(name, new_sid)
.expect("NS: register name failure, maybe out of HashMap capacity?");
if debug1{info!("NS: request successful, SID is {:?}", new_sid);}
rkyv::Archived::<api::Request>::SID(new_sid.into())
} else {
// compute the next interval, rounded to a multiple of FAIL_TIMEOUT_MS to reduce timing side channels
let target_time: u64 = ((ticktimer_server::elapsed_ms(ticktimer_conn)
.unwrap()
/ FAIL_TIMEOUT_MS)
+ 1)
* FAIL_TIMEOUT_MS;
info!("NS: request failed, waiting for deterministic timeout");
while ticktimer_server::elapsed_ms(ticktimer_conn).unwrap() < target_time {
xous::yield_slice();
}
info!("NS: deterministic timeout done");
rkyv::Archived::<api::Request>::Failure
2021-01-04 20:40:34 +00:00
}
}
rkyv::Archived::<api::Request>::Lookup(lookup_name) => {
if debug1{info!("NS: Lookup request for '{}'", lookup_name);}
let name = lookup_name.unarchive();
if let Some(server_sid) = name_table.get(&name) {
let sender_pid = envelope
.sender
.pid()
.expect("NS: can't extract sender PID on Lookup");
match xous::connect_for_process(sender_pid, *server_sid)
.expect("NS: can't broker connection")
{
xous::Result::ConnectionID(connection_id) => {
if debug1{info!("NS: lookup success, returning connection {}", connection_id);}
rkyv::Archived::<api::Request>::CID(connection_id)
}
_ => {
info!(
"NS: Can't find request '{}' in table, dumping table:",
lookup_name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
rkyv::Archived::<api::Request>::Failure
}
}
} else {
info!(
"NS: Can't find request '{}' in table, dumping table:",
lookup_name
);
for (key, val) in name_table.iter() {
info!("NS: name: '{}', sid: '{:?}'", key, val);
}
// no authenticate remedy currently supported, but we'd put that code somewhere around here eventually.
rkyv::Archived::<api::Request>::Failure
}
}
_ => panic!("Invalid response from the server -- corruption occurred"),
};
unsafe { *value.get_unchecked_mut() = new_value };
2021-01-04 15:34:36 +00:00
} else {
error!("NS: couldn't convert opcode");
}
}
}