mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
services: add rkyv-test-client and rkyv-test-server
These servers demonstrate how we might use rkyv in the future. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
364cd94326
commit
fd255d8f53
13 changed files with 693 additions and 15 deletions
66
Cargo.lock
generated
66
Cargo.lock
generated
|
|
@ -83,7 +83,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
"xous-names",
|
"xous-names",
|
||||||
|
|
@ -124,7 +124,7 @@ version = "0.5.0"
|
||||||
source = "git+https://github.com/betrusted-io/blitstr.git?rev=f1f1ca2352a90b08f97fcb48ab790053dda25f42#f1f1ca2352a90b08f97fcb48ab790053dda25f42"
|
source = "git+https://github.com/betrusted-io/blitstr.git?rev=f1f1ca2352a90b08f97fcb48ab790053dda25f42#f1f1ca2352a90b08f97fcb48ab790053dda25f42"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -241,7 +241,7 @@ dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"typenum",
|
"typenum",
|
||||||
"utralib",
|
"utralib",
|
||||||
|
|
@ -415,7 +415,7 @@ dependencies = [
|
||||||
"keyboard",
|
"keyboard",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"trng",
|
"trng",
|
||||||
"utralib",
|
"utralib",
|
||||||
|
|
@ -479,7 +479,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"minifb",
|
"minifb",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
"xous-names",
|
"xous-names",
|
||||||
|
|
@ -559,7 +559,7 @@ dependencies = [
|
||||||
"keyboard",
|
"keyboard",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
@ -572,7 +572,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"graphics-server",
|
"graphics-server",
|
||||||
"log",
|
"log",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"xous",
|
"xous",
|
||||||
"xous-names",
|
"xous-names",
|
||||||
]
|
]
|
||||||
|
|
@ -585,7 +585,7 @@ dependencies = [
|
||||||
"ime-plugin-api",
|
"ime-plugin-api",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
@ -616,7 +616,7 @@ dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
@ -658,7 +658,7 @@ dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
@ -765,6 +765,17 @@ dependencies = [
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-derive"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 1.0.24",
|
||||||
|
"quote 1.0.8",
|
||||||
|
"syn 1.0.60",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.44"
|
version = "0.1.44"
|
||||||
|
|
@ -1044,15 +1055,40 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rkyv"
|
name = "rkyv"
|
||||||
version = "0.4.1"
|
version = "0.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "45a6fdbe501948402e00ede914b4dc21d340105b2eff740ed47e3d45ac846f5c"
|
checksum = "70de01b38fe7baba4ecdd33b777096d2b326993d8ea99bc5b6ede691883d3010"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"memoffset",
|
"memoffset",
|
||||||
"ptr_meta",
|
"ptr_meta",
|
||||||
"rkyv_derive 0.4.0",
|
"rkyv_derive 0.4.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rkyv-test-client"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"log",
|
||||||
|
"log-server",
|
||||||
|
"rkyv-test-server",
|
||||||
|
"ticktimer-server",
|
||||||
|
"xous",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rkyv-test-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"log",
|
||||||
|
"log-server",
|
||||||
|
"num-derive",
|
||||||
|
"num-traits",
|
||||||
|
"rkyv 0.4.3",
|
||||||
|
"ticktimer-server",
|
||||||
|
"xous",
|
||||||
|
"xous-names",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rkyv_derive"
|
name = "rkyv_derive"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|
@ -1173,7 +1209,7 @@ dependencies = [
|
||||||
"llio",
|
"llio",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
@ -1610,7 +1646,7 @@ dependencies = [
|
||||||
"hex",
|
"hex",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"rustc-std-workspace-core",
|
"rustc-std-workspace-core",
|
||||||
"xous-macros",
|
"xous-macros",
|
||||||
]
|
]
|
||||||
|
|
@ -1634,7 +1670,7 @@ dependencies = [
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"log-server",
|
"log-server",
|
||||||
"rkyv 0.4.1",
|
"rkyv 0.4.3",
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
"utralib",
|
"utralib",
|
||||||
"xous",
|
"xous",
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ members = [
|
||||||
"services/benchmark-target",
|
"services/benchmark-target",
|
||||||
"services/ime-frontend",
|
"services/ime-frontend",
|
||||||
"services/ime-plugin-shell",
|
"services/ime-plugin-shell",
|
||||||
|
"services/rkyv-test-server",
|
||||||
|
"services/rkyv-test-client",
|
||||||
"services/shellchat",
|
"services/shellchat",
|
||||||
"svd2utra",
|
"svd2utra",
|
||||||
"xtask",
|
"xtask",
|
||||||
|
|
|
||||||
16
services/rkyv-test-client/Cargo.toml
Normal file
16
services/rkyv-test-client/Cargo.toml
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
authors = ["Sean Cross <sean@xobs.io>"]
|
||||||
|
description = "Program to test various aspects of the kernel"
|
||||||
|
edition = "2018"
|
||||||
|
name = "rkyv-test-client"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
log = "0.4"
|
||||||
|
log-server = {path = "../log-server"}
|
||||||
|
rkyv-test-server = {path = "../rkyv-test-server"}
|
||||||
|
ticktimer-server = {path = "../ticktimer-server"}
|
||||||
|
xous = {path = "../../xous-rs"}
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
5
services/rkyv-test-client/README.md
Normal file
5
services/rkyv-test-client/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Kernel Test
|
||||||
|
|
||||||
|
Run various tests inside the kernel.
|
||||||
|
|
||||||
|
This is supposed to be the only program running.
|
||||||
17
services/rkyv-test-client/build.rs
Normal file
17
services/rkyv-test-client/build.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
// NOTE: Adapted from cortex-m/build.rs
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let target = env::var("TARGET").unwrap();
|
||||||
|
|
||||||
|
let target_os = target.split('-').nth(2).unwrap_or("none");
|
||||||
|
|
||||||
|
// If we're not running on a desktop-class operating system, emit the "baremetal"
|
||||||
|
// config setting. This will enable software to do tasks such as
|
||||||
|
// managing memory.
|
||||||
|
if target_os == "none" {
|
||||||
|
println!("cargo:rustc-cfg=baremetal");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
}
|
||||||
27
services/rkyv-test-client/src/main.rs
Normal file
27
services/rkyv-test-client/src/main.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
|
fn print_log_messages(log_message: &str, prefix: &str) {
|
||||||
|
log::info!("Got a message hook! Message: [{}], prefix: [{}]", log_message, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[xous::xous_main]
|
||||||
|
fn rkyv_test_client() -> ! {
|
||||||
|
log_server::init_wait().unwrap();
|
||||||
|
log::info!(
|
||||||
|
"Hello, world! This is the client, PID {}",
|
||||||
|
xous::current_pid().unwrap().get()
|
||||||
|
);
|
||||||
|
let ticktimer_server_id = xous::SID::from_bytes(b"ticktimer-server").unwrap();
|
||||||
|
let ticktimer_conn = xous::connect(ticktimer_server_id).unwrap();
|
||||||
|
|
||||||
|
rkyv_test_server::hook_log_messages(print_log_messages);
|
||||||
|
|
||||||
|
let mut idx = 0;
|
||||||
|
loop {
|
||||||
|
log::info!("2 + {} = {}", idx, rkyv_test_server::add(2, idx).unwrap());
|
||||||
|
ticktimer_server::sleep_ms(ticktimer_conn, 500).ok();
|
||||||
|
rkyv_test_server::log_message("prefix", "Hi there");
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
services/rkyv-test-server/Cargo.toml
Normal file
19
services/rkyv-test-server/Cargo.toml
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
[package]
|
||||||
|
authors = ["Sean Cross <sean@xobs.io>"]
|
||||||
|
description = "Program to test various aspects of the kernel"
|
||||||
|
edition = "2018"
|
||||||
|
name = "rkyv-test-server"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
log = "0.4"
|
||||||
|
log-server = {path = "../log-server"}
|
||||||
|
num-derive = {version = "0.3", default-features = false}
|
||||||
|
num-traits = {version = "0.2", default-features = false}
|
||||||
|
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
|
||||||
|
ticktimer-server = {path = "../ticktimer-server"}
|
||||||
|
xous = {path = "../../xous-rs"}
|
||||||
|
xous-names = {path = "../xous-names"}
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
5
services/rkyv-test-server/README.md
Normal file
5
services/rkyv-test-server/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Kernel Test
|
||||||
|
|
||||||
|
Run various tests inside the kernel.
|
||||||
|
|
||||||
|
This is supposed to be the only program running.
|
||||||
17
services/rkyv-test-server/build.rs
Normal file
17
services/rkyv-test-server/build.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
// NOTE: Adapted from cortex-m/build.rs
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let target = env::var("TARGET").unwrap();
|
||||||
|
|
||||||
|
let target_os = target.split('-').nth(2).unwrap_or("none");
|
||||||
|
|
||||||
|
// If we're not running on a desktop-class operating system, emit the "baremetal"
|
||||||
|
// config setting. This will enable software to do tasks such as
|
||||||
|
// managing memory.
|
||||||
|
if target_os == "none" {
|
||||||
|
println!("cargo:rustc-cfg=baremetal");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
}
|
||||||
62
services/rkyv-test-server/src/api.rs
Normal file
62
services/rkyv-test-server/src/api.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
pub(crate) const SERVER_NAME: &str = "Rkyv Test Server 1";
|
||||||
|
|
||||||
|
/// A `usize` value that gets set as the `id` for every message handled
|
||||||
|
/// by our server.
|
||||||
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||||
|
pub(crate) enum Opcode {
|
||||||
|
/// Perform a mathematical function
|
||||||
|
Mathematics = 0,
|
||||||
|
|
||||||
|
/// Log the string to the log server with the given prefix
|
||||||
|
LogString = 1,
|
||||||
|
|
||||||
|
/// Callback with log string
|
||||||
|
AddLogStringCallback = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// These enums indicate what kind of callback type we're sending.
|
||||||
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||||
|
pub(crate) enum CallbackType {
|
||||||
|
/// A log message was sent
|
||||||
|
LogString,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A rich structure that contains multiple values.
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub(crate) enum MathOperation {
|
||||||
|
/// Add two numbers together and return the result.
|
||||||
|
Add(i32, i32),
|
||||||
|
|
||||||
|
/// Subtract two numbers and return the result.
|
||||||
|
Subtract(i32, i32),
|
||||||
|
|
||||||
|
/// Multiply the two numbers
|
||||||
|
Multiply(i32, i32),
|
||||||
|
|
||||||
|
/// Divide the two numbers
|
||||||
|
Divide(i32, i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub(crate) struct LogString {
|
||||||
|
pub(crate) prefix: xous::String<32>,
|
||||||
|
pub(crate) message: xous::String<512>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
InternalError,
|
||||||
|
Overflow,
|
||||||
|
Underflow,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub(crate) enum MathResult {
|
||||||
|
Value(i32),
|
||||||
|
Error(Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
|
||||||
|
pub(crate) struct RegisterCallback {
|
||||||
|
server: (u32, u32, u32, u32)
|
||||||
|
}
|
||||||
195
services/rkyv-test-server/src/buffer.rs
Normal file
195
services/rkyv-test-server/src/buffer.rs
Normal file
|
|
@ -0,0 +1,195 @@
|
||||||
|
use rkyv::{ser::Serializer, Fallible};
|
||||||
|
use xous::{
|
||||||
|
map_memory, send_message, unmap_memory, Error, MemoryAddress, MemoryFlags, MemoryMessage,
|
||||||
|
MemoryRange, MemorySize, Message, Result, CID,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Buffer<'a> {
|
||||||
|
range: MemoryRange,
|
||||||
|
valid: MemoryRange,
|
||||||
|
offset: Option<MemoryAddress>,
|
||||||
|
slice: &'a mut [u8],
|
||||||
|
should_drop: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct XousDeserializer;
|
||||||
|
|
||||||
|
// Unreachable enum pattern, swap out for the never type (!) whenever that gets stabilized
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum XousUnreachable {}
|
||||||
|
|
||||||
|
impl rkyv::Fallible for XousDeserializer {
|
||||||
|
type Error = XousUnreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Buffer<'a> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn new(len: usize) -> Self {
|
||||||
|
let remainder = if ((len & 0xFFF) == 0) && (len > 0) {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
0x1000 - (len & 0xFFF)
|
||||||
|
};
|
||||||
|
|
||||||
|
let flags = MemoryFlags::R | MemoryFlags::W;
|
||||||
|
|
||||||
|
// Allocate enough memory to hold the requested data
|
||||||
|
let new_mem = map_memory(
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
// Ensure our byte size is a multiple of 4096
|
||||||
|
len + remainder,
|
||||||
|
flags,
|
||||||
|
)
|
||||||
|
.expect("Buffer: error in new()/map_memory");
|
||||||
|
|
||||||
|
let mut valid = new_mem;
|
||||||
|
valid.size = MemorySize::new(len + remainder).unwrap();
|
||||||
|
Buffer {
|
||||||
|
range: new_mem,
|
||||||
|
slice: unsafe {
|
||||||
|
core::slice::from_raw_parts_mut(new_mem.as_mut_ptr(), len + remainder)
|
||||||
|
},
|
||||||
|
valid,
|
||||||
|
offset: None,
|
||||||
|
should_drop: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub unsafe fn from_memory_message(mem: &'a MemoryMessage) -> Self {
|
||||||
|
Buffer {
|
||||||
|
range: mem.buf,
|
||||||
|
slice: core::slice::from_raw_parts_mut(mem.buf.as_mut_ptr(), mem.buf.len()),
|
||||||
|
valid: mem.buf,
|
||||||
|
offset: mem.offset,
|
||||||
|
should_drop: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Perform a mutable lend of this Buffer to the server.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn lend_mut(&mut self, connection: CID, id: u32) -> core::result::Result<Result, Error> {
|
||||||
|
let msg = MemoryMessage {
|
||||||
|
id: id as usize,
|
||||||
|
buf: self.valid,
|
||||||
|
offset: self.offset,
|
||||||
|
valid: MemorySize::new(self.slice.len()),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update the offset pointer if the server modified it.
|
||||||
|
let result = send_message(connection, Message::MutableBorrow(msg));
|
||||||
|
if let Ok(Result::MemoryReturned(offset, _valid)) = result {
|
||||||
|
self.offset = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn lend(&self, connection: CID, id: u32) -> core::result::Result<Result, Error> {
|
||||||
|
let msg = MemoryMessage {
|
||||||
|
id: id as usize,
|
||||||
|
buf: self.valid,
|
||||||
|
offset: self.offset,
|
||||||
|
valid: MemorySize::new(self.slice.len()),
|
||||||
|
};
|
||||||
|
send_message(connection, Message::Borrow(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn send(mut self, connection: CID, id: u32) -> core::result::Result<Result, Error> {
|
||||||
|
let msg = MemoryMessage {
|
||||||
|
id: id as usize,
|
||||||
|
buf: self.valid,
|
||||||
|
offset: self.offset,
|
||||||
|
valid: MemorySize::new(self.slice.len()),
|
||||||
|
};
|
||||||
|
let result = send_message(connection, Message::Move(msg))?;
|
||||||
|
|
||||||
|
// prevents it from being Dropped.
|
||||||
|
self.should_drop = false;
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn try_from<S>(src: S) -> core::result::Result<Self, ()>
|
||||||
|
where
|
||||||
|
S: rkyv::Serialize<rkyv::ser::serializers::BufferSerializer<Buffer<'a>>>,
|
||||||
|
{
|
||||||
|
let buf = Self::new(4096);
|
||||||
|
let mut ser = rkyv::ser::serializers::BufferSerializer::new(buf);
|
||||||
|
let pos = ser.serialize_value(&src).or(Err(()))?;
|
||||||
|
let mut buf = ser.into_inner();
|
||||||
|
buf.offset = MemoryAddress::new(pos);
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn serialize_from<S>(self, src: S) -> core::result::Result<Self, ()>
|
||||||
|
where
|
||||||
|
S: rkyv::Serialize<rkyv::ser::serializers::BufferSerializer<Buffer<'a>>>,
|
||||||
|
{
|
||||||
|
let mut ser = rkyv::ser::serializers::BufferSerializer::new(self);
|
||||||
|
let pos = ser.serialize_value(&src).or(Err(()))?;
|
||||||
|
let mut buf = ser.into_inner();
|
||||||
|
buf.offset = MemoryAddress::new(pos);
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn try_into<T, U>(&self) -> core::result::Result<&U, ()>
|
||||||
|
where
|
||||||
|
T: rkyv::Archive<Archived = U>,
|
||||||
|
{
|
||||||
|
let pos = self.offset.map(|o| o.get()).unwrap_or_default();
|
||||||
|
let r = unsafe { rkyv::archived_value::<T>(self.slice, pos) };
|
||||||
|
Ok(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn deserialize<T, U>(&self) -> core::result::Result<T, ()>
|
||||||
|
where
|
||||||
|
T: rkyv::Archive<Archived = U>,
|
||||||
|
U: rkyv::Deserialize<T, dyn Fallible<Error = XousUnreachable>>,
|
||||||
|
{
|
||||||
|
let pos = self.offset.map(|o| o.get()).unwrap_or_default();
|
||||||
|
let r = unsafe { rkyv::archived_value::<T>(self.slice, pos) };
|
||||||
|
Ok(r.deserialize(&mut XousDeserializer {}).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> core::convert::AsRef<[u8]> for Buffer<'a> {
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
self.slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> core::convert::AsMut<[u8]> for Buffer<'a> {
|
||||||
|
fn as_mut(&mut self) -> &mut [u8] {
|
||||||
|
self.slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> core::ops::Deref for Buffer<'a> {
|
||||||
|
type Target = [u8];
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&*self.slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> core::ops::DerefMut for Buffer<'a> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut *self.slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Drop for Buffer<'a> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if self.should_drop {
|
||||||
|
unmap_memory(self.range).expect("Buffer: failed to drop memory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
158
services/rkyv-test-server/src/lib.rs
Normal file
158
services/rkyv-test-server/src/lib.rs
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
mod buffer;
|
||||||
|
|
||||||
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
use num_traits::{FromPrimitive, ToPrimitive};
|
||||||
|
|
||||||
|
// Note that connection IDs are never 0, so this is invalid, and could map to `None`.
|
||||||
|
static SERVER_CID: AtomicU32 = AtomicU32::new(0);
|
||||||
|
|
||||||
|
/// Ensure we have a connection to our own server. If a connection does not exist,
|
||||||
|
/// have the nameserver create one for us.
|
||||||
|
fn ensure_connection() -> xous::CID {
|
||||||
|
let mut cid = SERVER_CID.load(Ordering::Relaxed);
|
||||||
|
if cid == 0 {
|
||||||
|
cid = xous_names::request_connection_blocking(api::SERVER_NAME).unwrap();
|
||||||
|
SERVER_CID.store(cid, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
cid
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add two numbers to gether, and return the result. Returns
|
||||||
|
/// an Error if one is detected.
|
||||||
|
pub fn add(arg1: i32, arg2: i32) -> Result<i32, api::Error> {
|
||||||
|
// Create an opcode variant. This can be a rich structure.
|
||||||
|
let op = api::MathOperation::Add(arg1, arg2);
|
||||||
|
|
||||||
|
// 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).or(Err(api::Error::InternalError))?;
|
||||||
|
|
||||||
|
// Mutably lend our op to the server, specifying the `api::Opcode::Mathematics` opcode.
|
||||||
|
// This will return a structure that we can deserialize back into something we
|
||||||
|
// can verify.
|
||||||
|
buf.lend_mut(
|
||||||
|
ensure_connection(),
|
||||||
|
api::Opcode::Mathematics.to_u32().unwrap(),
|
||||||
|
)
|
||||||
|
.or(Err(api::Error::InternalError))?;
|
||||||
|
|
||||||
|
// Turn the result of the response into a friendly value that can be understood
|
||||||
|
// by the caller.
|
||||||
|
match buf.deserialize().unwrap() {
|
||||||
|
api::MathResult::Value(v) => Ok(v),
|
||||||
|
api::MathResult::Error(val) => Err(val),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Condensed form of the operation, that allows for reuse among other operations.
|
||||||
|
fn do_op(op: api::MathOperation) -> Result<i32, api::Error> {
|
||||||
|
// 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).or(Err(api::Error::InternalError))?;
|
||||||
|
|
||||||
|
// Lend our op to the server, specifying the `api::Opcode::Mathematics` opcode.
|
||||||
|
// This will return a structure that we can deserialize back into something we
|
||||||
|
// can verify.
|
||||||
|
buf.lend_mut(
|
||||||
|
ensure_connection(),
|
||||||
|
api::Opcode::Mathematics.to_u32().unwrap(),
|
||||||
|
)
|
||||||
|
.or(Err(api::Error::InternalError))?;
|
||||||
|
Err(api::Error::Overflow)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subtract(arg1: i32, arg2: i32) -> Result<i32, api::Error> {
|
||||||
|
do_op(api::MathOperation::Subtract(arg1, arg2))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn multiply(arg1: i32, arg2: i32) -> Result<i32, api::Error> {
|
||||||
|
do_op(api::MathOperation::Multiply(arg1, arg2))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn divide(arg1: i32, arg2: i32) -> Result<i32, api::Error> {
|
||||||
|
do_op(api::MathOperation::Divide(arg1, arg2))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_message(prefix: &str, message: &str) {
|
||||||
|
let op = api::LogString {
|
||||||
|
prefix: xous::String::from_str(prefix),
|
||||||
|
message: xous::String::from_str(message),
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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 buf = buffer::Buffer::try_from(op).unwrap();
|
||||||
|
|
||||||
|
// Send the message to the server.
|
||||||
|
buf.lend(
|
||||||
|
ensure_connection(),
|
||||||
|
api::Opcode::LogString.to_u32().unwrap(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Callback messages will be sent to this private server.
|
||||||
|
// Ideally it would be held in a Mutex.
|
||||||
|
static mut CALLBACK_SID: Option<xous::SID> = None;
|
||||||
|
static mut CALLBACK_ARRAY: [Option<fn(&str, &str)>; 8] = [None; 8];
|
||||||
|
|
||||||
|
// This callback server runs in its own thread.
|
||||||
|
fn callback_server() {
|
||||||
|
let sid = unsafe { CALLBACK_SID }.unwrap();
|
||||||
|
loop {
|
||||||
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
|
match FromPrimitive::from_usize(msg.body.id()) {
|
||||||
|
Some(api::CallbackType::LogString) => {
|
||||||
|
let mem = msg.body.memory_message().unwrap();
|
||||||
|
let buffer = unsafe { buffer::Buffer::from_memory_message(mem) };
|
||||||
|
let log_string = buffer.try_into::<api::LogString, _>().unwrap();
|
||||||
|
unsafe {
|
||||||
|
for entry in CALLBACK_ARRAY.iter() {
|
||||||
|
if let Some(cb) = entry {
|
||||||
|
cb(log_string.message.as_str(), log_string.prefix.as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hook_log_messages(cb: fn(&str, &str)) {
|
||||||
|
// Add this entry to the callback array
|
||||||
|
unsafe {
|
||||||
|
for entry in CALLBACK_ARRAY.iter_mut() {
|
||||||
|
if entry.is_none() {
|
||||||
|
*entry = Some(cb);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no thread exists to handle callbacks, create one.
|
||||||
|
// Publish this server with the main thread so it can call us as necessary.
|
||||||
|
if unsafe { CALLBACK_SID }.is_none() {
|
||||||
|
let sid = xous::create_server().unwrap();
|
||||||
|
unsafe { CALLBACK_SID = Some(sid) };
|
||||||
|
xous::create_thread_0(callback_server).unwrap();
|
||||||
|
let sid = sid.to_u32();
|
||||||
|
xous::send_message(
|
||||||
|
ensure_connection(),
|
||||||
|
xous::Message::Scalar(xous::ScalarMessage {
|
||||||
|
id: api::Opcode::AddLogStringCallback.to_usize().unwrap(),
|
||||||
|
arg1: sid.0 as _,
|
||||||
|
arg2: sid.1 as _,
|
||||||
|
arg3: sid.2 as _,
|
||||||
|
arg4: sid.3 as _,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
119
services/rkyv-test-server/src/main.rs
Normal file
119
services/rkyv-test-server/src/main.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
|
mod api;
|
||||||
|
mod buffer;
|
||||||
|
use num_traits::{FromPrimitive, ToPrimitive};
|
||||||
|
|
||||||
|
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) {
|
||||||
|
let buffer = unsafe { buffer::Buffer::from_memory_message(mem) };
|
||||||
|
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) {
|
||||||
|
let buffer = unsafe { buffer::Buffer::from_memory_message(mem) };
|
||||||
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[xous::xous_main]
|
||||||
|
fn test_main() -> ! {
|
||||||
|
let mut callback_conn = None;
|
||||||
|
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();
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
Some(api::Opcode::LogString) => {
|
||||||
|
let memory = msg.body.memory_message().unwrap();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
handle_log_string(msg.body.memory_message().unwrap())
|
||||||
|
}
|
||||||
|
Some(api::Opcode::AddLogStringCallback) => {
|
||||||
|
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 _);
|
||||||
|
callback_conn = Some(xous::connect(sid).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue