From ff180ca737df74ec0438013adfee63b4d8653739 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 26 Mar 2021 12:15:57 +0800 Subject: [PATCH] rkyv-test-server: add DoubleString api call This demonstrates how we might use strings in syscalls. Signed-off-by: Sean Cross --- services/rkyv-test-server/src/api.rs | 8 ++++++++ services/rkyv-test-server/src/lib.rs | 29 ++++++++++++++++++++++++++- services/rkyv-test-server/src/main.rs | 18 +++++++++++++++-- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/services/rkyv-test-server/src/api.rs b/services/rkyv-test-server/src/api.rs index 92c6ee12e..970fe0df1 100644 --- a/services/rkyv-test-server/src/api.rs +++ b/services/rkyv-test-server/src/api.rs @@ -12,6 +12,9 @@ pub(crate) enum Opcode { /// Callback with log string AddLogStringCallback = 2, + + /// Double the string + DoubleString = 3, } /// These enums indicate what kind of callback type we're sending. @@ -43,6 +46,11 @@ pub(crate) struct LogString { pub(crate) message: xous::String<512>, } +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub(crate) struct StringDoubler { + pub(crate) value: xous::String<512>, +} + #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug)] pub enum Error { InternalError, diff --git a/services/rkyv-test-server/src/lib.rs b/services/rkyv-test-server/src/lib.rs index 1ac3d572d..e9874a559 100644 --- a/services/rkyv-test-server/src/lib.rs +++ b/services/rkyv-test-server/src/lib.rs @@ -63,7 +63,14 @@ fn do_op(op: api::MathOperation) -> Result { api::Opcode::Mathematics.to_u32().unwrap(), ) .or(Err(api::Error::InternalError))?; - Err(api::Error::Overflow) + + // Don't deserialize it -- use the archived version + match *buf.try_into::().unwrap() { + api::ArchivedMathResult::Value(v) => Ok(v), + api::ArchivedMathResult::Error(api::ArchivedError::InternalError) => Err(api::Error::InternalError), + api::ArchivedMathResult::Error(api::ArchivedError::Overflow) => Err(api::Error::Overflow), + api::ArchivedMathResult::Error(api::ArchivedError::Underflow) => Err(api::Error::Underflow), + } } pub fn subtract(arg1: i32, arg2: i32) -> Result { @@ -156,3 +163,23 @@ pub fn hook_log_messages(cb: fn(&str, &str)) { .unwrap(); } } + +pub fn double_string(value: &xous::String) -> xous::String { + let op = api::StringDoubler { + value: xous::String::from_str(value.as_str().unwrap()), + }; + + // Convert the opcode into a serialized buffer. This consumes the opcode, which will + // exist on the heap in its own page. Furthermore, this will be in a flattened format + // suitable for passing around as a message. + let mut buf = buffer::Buffer::try_from(op).unwrap(); + + // Send the message to the server. + buf.lend_mut( + ensure_connection(), + api::Opcode::DoubleString.to_u32().unwrap(), + ) + .unwrap(); + + xous::String::from_str(buf.try_into::().unwrap().value.as_str()) +} diff --git a/services/rkyv-test-server/src/main.rs b/services/rkyv-test-server/src/main.rs index 95513eb3f..c9801cd4c 100644 --- a/services/rkyv-test-server/src/main.rs +++ b/services/rkyv-test-server/src/main.rs @@ -10,7 +10,7 @@ fn value_or(val: Option, default: api::MathResult) -> api::MathResult { } fn handle_math_withcopy(mem: &mut xous::MemoryMessage) { - let buffer = unsafe { buffer::Buffer::from_memory_message(mem) }; + let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) }; let response = { use api::MathOperation::*; match buffer.deserialize().unwrap() { @@ -38,7 +38,7 @@ fn handle_math_withcopy(mem: &mut xous::MemoryMessage) { // 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) { - let buffer = unsafe { buffer::Buffer::from_memory_message(mem) }; + let mut buffer = unsafe { buffer::Buffer::from_memory_message_mut(mem) }; let response = { use api::ArchivedMathOperation::*; match *buffer.try_into::().unwrap() { @@ -73,6 +73,17 @@ fn handle_log_string(mem: &xous::MemoryMessage) { ); } +/// 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) }; + let mut response = api::StringDoubler { value: xous::String::new() }; + for ch in buffer.try_into::().unwrap().value.as_str().chars() { + write!(response.value, "{}{}", ch, ch).ok(); + } + buffer.serialize_from(response).unwrap(); +} + #[xous::xous_main] fn test_main() -> ! { let mut callback_conn = None; @@ -90,6 +101,9 @@ fn test_main() -> ! { Some(api::Opcode::Mathematics) => { handle_math_withcopy(msg.body.memory_message_mut().unwrap()) } + Some(api::Opcode::DoubleString) => { + double_string(msg.body.memory_message_mut().unwrap()) + } Some(api::Opcode::LogString) => { let memory = msg.body.memory_message().unwrap(); if let Some(callback_sid) = callback_conn {