2021-03-25 15:39:35 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
|
mod buffer;
|
|
|
|
|
use num_traits::{FromPrimitive, ToPrimitive};
|
2021-03-26 12:22:33 +00:00
|
|
|
use xous_ipc::String;
|
2021-03-25 15:39:35 +00:00
|
|
|
|
|
|
|
|
fn value_or(val: Option<i32>, default: api::MathResult) -> api::MathResult {
|
|
|
|
|
val.map(|v| api::MathResult::Value(v)).unwrap_or(default)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_math_withcopy(mem: &mut xous::MemoryMessage) {
|
2021-03-26 04:15:57 +00:00
|
|
|
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
2021-03-25 15:39:35 +00:00
|
|
|
let response = {
|
|
|
|
|
use api::MathOperation::*;
|
|
|
|
|
match buffer.deserialize().unwrap() {
|
|
|
|
|
Add(a, b) => value_or(
|
|
|
|
|
a.checked_add(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Overflow),
|
|
|
|
|
),
|
|
|
|
|
Subtract(a, b) => value_or(
|
|
|
|
|
a.checked_sub(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Underflow),
|
|
|
|
|
),
|
|
|
|
|
Multiply(a, b) => value_or(
|
|
|
|
|
a.checked_mul(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Overflow),
|
|
|
|
|
),
|
|
|
|
|
Divide(a, b) => value_or(
|
|
|
|
|
a.checked_div(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Underflow),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
buffer.serialize_from(response).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This doesn't deserialize the struct, and therefore operates entirely
|
|
|
|
|
// on the archived data. This saves a copy step.
|
|
|
|
|
fn handle_math_zerocopy(mem: &mut xous::MemoryMessage) {
|
2021-03-26 04:15:57 +00:00
|
|
|
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
2021-03-25 15:39:35 +00:00
|
|
|
let response = {
|
|
|
|
|
use api::ArchivedMathOperation::*;
|
|
|
|
|
match *buffer.try_into::<api::MathOperation, _>().unwrap() {
|
|
|
|
|
Add(a, b) => value_or(
|
|
|
|
|
a.checked_add(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Overflow),
|
|
|
|
|
),
|
|
|
|
|
Subtract(a, b) => value_or(
|
|
|
|
|
a.checked_sub(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Underflow),
|
|
|
|
|
),
|
|
|
|
|
Multiply(a, b) => value_or(
|
|
|
|
|
a.checked_mul(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Overflow),
|
|
|
|
|
),
|
|
|
|
|
Divide(a, b) => value_or(
|
|
|
|
|
a.checked_div(b),
|
|
|
|
|
api::MathResult::Error(api::Error::Underflow),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
buffer.serialize_from(response).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_log_string(mem: &xous::MemoryMessage) {
|
|
|
|
|
let buffer = unsafe { buffer::Buffer::from_memory_message(mem) };
|
|
|
|
|
let log_string = buffer.try_into::<api::LogString, _>().unwrap();
|
|
|
|
|
log::info!(
|
|
|
|
|
"Prefix: {} Message: {}",
|
|
|
|
|
log_string.prefix.as_str(),
|
|
|
|
|
log_string.message.as_str()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 04:15:57 +00:00
|
|
|
/// Take the given string and double each character in an output string.
|
|
|
|
|
fn double_string(mem: &mut xous::MemoryMessage) {
|
|
|
|
|
use core::fmt::Write;
|
|
|
|
|
let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) };
|
2021-03-26 04:41:15 +00:00
|
|
|
let mut response = api::StringDoubler {
|
2021-03-26 12:22:33 +00:00
|
|
|
value: String::new(),
|
2021-03-26 04:41:15 +00:00
|
|
|
};
|
|
|
|
|
for ch in buffer
|
|
|
|
|
.try_into::<api::StringDoubler, _>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.value
|
|
|
|
|
.as_str()
|
|
|
|
|
.chars()
|
|
|
|
|
{
|
2021-03-26 04:15:57 +00:00
|
|
|
write!(response.value, "{}{}", ch, ch).ok();
|
|
|
|
|
}
|
|
|
|
|
buffer.serialize_from(response).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 15:39:35 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn test_main() -> ! {
|
|
|
|
|
log_server::init_wait().unwrap();
|
|
|
|
|
|
|
|
|
|
log::info!(
|
|
|
|
|
"Hello, world! This is the server, PID {}",
|
|
|
|
|
xous::current_pid().unwrap()
|
|
|
|
|
);
|
|
|
|
|
let sid = xous_names::register_name(api::SERVER_NAME).unwrap();
|
|
|
|
|
|
2021-03-26 04:41:15 +00:00
|
|
|
let mut logstring_callback_connections = [None; 32];
|
|
|
|
|
|
2021-03-25 15:39:35 +00:00
|
|
|
loop {
|
|
|
|
|
let mut msg = xous::receive_message(sid).unwrap();
|
|
|
|
|
match FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(api::Opcode::Mathematics) => {
|
|
|
|
|
handle_math_withcopy(msg.body.memory_message_mut().unwrap())
|
|
|
|
|
}
|
2021-03-26 04:15:57 +00:00
|
|
|
Some(api::Opcode::DoubleString) => {
|
|
|
|
|
double_string(msg.body.memory_message_mut().unwrap())
|
|
|
|
|
}
|
2021-03-25 15:39:35 +00:00
|
|
|
Some(api::Opcode::LogString) => {
|
|
|
|
|
let memory = msg.body.memory_message().unwrap();
|
2021-03-26 04:41:15 +00:00
|
|
|
// If a callback exists, first pass this message to the callback server.
|
|
|
|
|
for callback_conn in logstring_callback_connections.iter() {
|
|
|
|
|
if let Some(callback_sid) = callback_conn {
|
|
|
|
|
let buffer = unsafe { buffer::Buffer::from_memory_message(memory) };
|
|
|
|
|
buffer
|
|
|
|
|
.lend(
|
|
|
|
|
*callback_sid,
|
|
|
|
|
api::CallbackType::LogString.to_u32().unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
}
|
2021-03-25 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
handle_log_string(msg.body.memory_message().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Some(api::Opcode::AddLogStringCallback) => {
|
2021-03-26 04:41:15 +00:00
|
|
|
// The Log String Callback provides us a SID. Connect to that SID
|
|
|
|
|
// and add it to the list of connections available.
|
2021-03-25 15:39:35 +00:00
|
|
|
if let xous::Message::Scalar(xous::ScalarMessage {
|
|
|
|
|
id: _id,
|
|
|
|
|
arg1,
|
|
|
|
|
arg2,
|
|
|
|
|
arg3,
|
|
|
|
|
arg4,
|
|
|
|
|
}) = msg.body
|
|
|
|
|
{
|
|
|
|
|
let sid = xous::SID::from_u32(arg1 as _, arg2 as _, arg3 as _, arg4 as _);
|
2021-03-26 04:41:15 +00:00
|
|
|
let cb_conn = Some(xous::connect(sid).unwrap());
|
2021-03-26 04:42:35 +00:00
|
|
|
// Add this callback connection to the list of callbacks. If `AddLogStringCallback`
|
|
|
|
|
// is called multiple times, then it will receive multiple callbacks.
|
|
|
|
|
for entry in logstring_callback_connections.iter_mut() {
|
|
|
|
|
if *entry == None {
|
|
|
|
|
*entry = cb_conn;
|
|
|
|
|
break;
|
2021-03-26 04:41:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-25 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|