mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
rkyv-test-server: add DoubleString api call
This demonstrates how we might use strings in syscalls. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
461f5c5f8d
commit
ff180ca737
3 changed files with 52 additions and 3 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -63,7 +63,14 @@ fn do_op(op: api::MathOperation) -> Result<i32, api::Error> {
|
|||
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::<api::MathResult, _>().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<i32, api::Error> {
|
||||
|
|
@ -156,3 +163,23 @@ pub fn hook_log_messages(cb: fn(&str, &str)) {
|
|||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn double_string<const N: usize>(value: &xous::String<N>) -> xous::String<N> {
|
||||
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::<api::StringDoubler, _>().unwrap().value.as_str())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ fn value_or(val: Option<i32>, 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::<api::MathOperation, _>().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::<api::StringDoubler, _>().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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue