about to add argument support

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
2024-01-06 00:13:59 +08:00
parent 36d4c143f1
commit 30c66a1cdd
8 changed files with 519 additions and 88 deletions

View File

@ -1,6 +1,9 @@
use std::sync::mpsc::Receiver;
pub mod log;
pub mod name;
pub mod panic_to_screen;
pub mod ticktimer;
use super::Memory;
pub type ResponseData = ([i64; 8], Option<(Vec<u8>, u64)>);
@ -19,48 +22,97 @@ pub enum LendResult {
}
pub trait Service {
fn scalar(&mut self, sender: u32, opcode: u32, _args: [u32; 4]) {
panic!("Unknown scalar to service {}: {}", sender, opcode);
}
fn blocking_scalar(&mut self, sender: u32, opcode: u32, _args: [u32; 4]) -> ScalarResult {
panic!("Unknown scalar to service {}: {}", sender, opcode);
}
fn lend(&mut self, sender: u32, opcode: u32, _buf: &[u8], extra: [u32; 2]) -> LendResult {
fn scalar(&mut self, _memory: &mut Memory, sender: u32, opcode: u32, args: [u32; 4]) {
panic!(
"Unknown lend to service {}: {} ({:?})",
sender, opcode, extra
"Unknown scalar to service {}: {} ({:?})",
sender, opcode, args
);
}
/// Mutable lend messages may block
fn lend_mut(
fn blocking_scalar(
&mut self,
_memory: &mut Memory,
sender: u32,
opcode: u32,
_buf: &mut [u8],
args: [u32; 4],
) -> ScalarResult {
panic!(
"Unknown scalar to service {}: {} ({:?})",
sender, opcode, args
);
}
fn lend(
&mut self,
_memory: &mut Memory,
sender: u32,
opcode: u32,
buf: &[u8],
extra: [u32; 2],
) -> LendResult {
panic!(
"Unknown lend_mut to service {}: {} ({:?})",
sender, opcode, extra
"Unknown lend {} bytes to service {}: {} ({:?})",
buf.len(),
sender,
opcode,
extra
);
}
/// Send-type messages return immediately, and memory is detached from the host process.
fn send(&mut self, sender: u32, opcode: u32, _buf: &[u8], extra: [u32; 2]) {
fn lend_mut(
&mut self,
_memory: &mut Memory,
sender: u32,
opcode: u32,
buf: &mut [u8],
extra: [u32; 2],
) -> LendResult {
panic!(
"Unknown send to service {}: {} ({:?})",
sender, opcode, extra
"Unknown lend_mut {} bytes to service {}: {} ({:?})",
buf.len(),
sender,
opcode,
extra
);
}
fn send(
&mut self,
_memory: &mut Memory,
sender: u32,
opcode: u32,
buf: &[u8],
extra: [u32; 2],
) {
panic!(
"Unknown send {} bytes to service {}: {} ({:?})",
buf.len(),
sender,
opcode,
extra
);
}
}
pub fn get_service(name: &[u32; 4]) -> Option<Box<dyn Service + Sync + Send>> {
let mut output_bfr = [0u8; core::mem::size_of::<u32>() * 4 /*args.len()*/];
// Combine the four arguments to form a single
// contiguous buffer. Note: The buffer size will change
// depending on the platfor's `usize` length.
for (src, dest) in name.iter().zip(output_bfr.chunks_mut(4)) {
dest.copy_from_slice(src.to_le_bytes().as_ref());
}
println!(
"Connecting to service: {}",
std::str::from_utf8(&output_bfr).unwrap_or("<invalid name>")
);
match name {
[0x6b636974, 0x656d6974, 0x65732d72, 0x72657672] => {
Some(Box::new(ticktimer::Ticktimer::new()))
}
[0x73756f78, 0x676f6c2d, 0x7265732d, 0x20726576] => Some(Box::new(log::Log::new())),
_ => None,
[0x73756f78, 0x6d616e2d, 0x65732d65, 0x72657672] => Some(Box::new(name::Name::new())),
_ => panic!("Unhandled service request: {:x?}", name),
}
}