Skip to content

Commit

Permalink
crates/sel4cp: Rework message API
Browse files Browse the repository at this point in the history
  • Loading branch information
nspin committed Aug 30, 2023
1 parent a3240eb commit b5b5432
Show file tree
Hide file tree
Showing 35 changed files with 572 additions and 398 deletions.
24 changes: 10 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ members = [
"crates/sel4cp",
"crates/sel4cp/macros",
"crates/sel4cp/message",
"crates/sel4cp/message/postcard",
"crates/sel4cp/message/types",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
license = "BSD-2-Clause"

[dependencies]
num_enum = { version = "0.5.9", default-features = false }
zerocopy = "0.6.1"
serde = { version = "1.0.147", default-features = false }
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#![no_std]

use zerocopy::{AsBytes, FromBytes};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)]
#[repr(C)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
pub height: usize,
pub width: usize,
pub draft_start: usize,
pub draft_size: usize,
}

#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)]
#[repr(C)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Response {
pub height: usize,
pub width: usize,
Expand Down
34 changes: 16 additions & 18 deletions crates/examples/sel4cp/banscii/pds/artist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sel4_externally_shared::{
ExternallySharedRef,
};
use sel4cp::{memory_region_symbol, protection_domain, Channel, Handler, MessageInfo};
use sel4cp_message::{MessageInfoExt as _, NoMessageValue, StatusMessageLabel};
use sel4cp_message::MessageInfoExt as _;

use banscii_artist_interface_types::*;

Expand Down Expand Up @@ -58,14 +58,14 @@ impl Handler for HandlerImpl {
msg_info: MessageInfo,
) -> Result<MessageInfo, Self::Error> {
Ok(match channel {
ASSISTANT => match msg_info.recv::<Request>() {
Ok(msg) => {
let draft_height = msg.height;
let draft_width = msg.width;
ASSISTANT => match msg_info.recv_using_postcard::<Request>() {
Ok(req) => {
let draft_height = req.height;
let draft_width = req.width;
let draft = self
.region_in
.as_ptr()
.index(msg.draft_start..msg.draft_start + msg.draft_size)
.index(req.draft_start..req.draft_start + req.draft_size)
.copy_to_vec();

let masterpiece = Masterpiece::complete(draft_height, draft_width, &draft);
Expand All @@ -91,19 +91,17 @@ impl Handler for HandlerImpl {
.index(signature_start..signature_end)
.copy_from_slice(signature);

MessageInfo::send(
StatusMessageLabel::Ok,
Response {
height: masterpiece.height,
width: masterpiece.width,
masterpiece_start,
masterpiece_size,
signature_start,
signature_size,
},
)
MessageInfo::send_using_postcard(Response {
height: masterpiece.height,
width: masterpiece.width,
masterpiece_start,
masterpiece_size,
signature_start,
signature_size,
})
.unwrap()
}
Err(_) => MessageInfo::send(StatusMessageLabel::Error, NoMessageValue),
Err(_) => MessageInfo::send_unspecified_error(),
},
_ => {
unreachable!()
Expand Down
68 changes: 28 additions & 40 deletions crates/examples/sel4cp/banscii/pds/assistant/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use sel4_externally_shared::{
ExternallySharedRef,
};
use sel4cp::{memory_region_symbol, protection_domain, Channel, Handler, MessageInfo};
use sel4cp_message::{MessageInfoExt as _, NoMessageLabel, NoMessageValue, StatusMessageLabel};
use sel4cp_message::MessageInfoExt as _;

use banscii_artist_interface_types as artist;
use banscii_assistant_core::Draft;
use banscii_pl011_driver_interface_types as driver;
use banscii_pl011_driver_interface_types as pl011_driver;

const PL011_DRIVER: Channel = Channel::new(0);
const TALENT: Channel = Channel::new(1);
const ARTIST: Channel = Channel::new(1);

const REGION_SIZE: usize = 0x4_000;

Expand Down Expand Up @@ -120,33 +120,31 @@ impl HandlerImpl {
.index(draft_start..draft_end)
.copy_from_slice(&draft.pixel_data);

let msg_info = TALENT.pp_call(MessageInfo::send(
NoMessageLabel,
artist::Request {
height: draft.height,
width: draft.width,
draft_start,
draft_size,
},
));

assert_eq!(msg_info.label().try_into(), Ok(StatusMessageLabel::Ok));
let req = artist::Request {
height: draft.height,
width: draft.width,
draft_start,
draft_size,
};

let msg = msg_info.recv::<artist::Response>().unwrap();
let resp: artist::Response = ARTIST
.pp_call(MessageInfo::send_using_postcard(req).unwrap())
.recv_using_postcard()
.unwrap();

let height = msg.height;
let width = msg.width;
let height = resp.height;
let width = resp.width;

let pixel_data = self
.region_in
.as_ptr()
.index(msg.masterpiece_start..msg.masterpiece_start + msg.masterpiece_size)
.index(resp.masterpiece_start..resp.masterpiece_start + resp.masterpiece_size)
.copy_to_vec();

let signature = self
.region_in
.as_ptr()
.index(msg.signature_start..msg.signature_start + msg.signature_size)
.index(resp.signature_start..resp.signature_start + resp.signature_size)
.copy_to_vec();

newline();
Expand Down Expand Up @@ -180,30 +178,20 @@ fn newline() {
}

fn get_char() -> Option<u8> {
let msg_info = PL011_DRIVER.pp_call(MessageInfo::send(
driver::RequestTag::GetChar,
NoMessageValue,
));
match msg_info.label().try_into().ok() {
Some(driver::GetCharResponseTag::Some) => match msg_info.recv() {
Ok(driver::GetCharSomeResponse { val }) => Some(val),
Err(_) => {
panic!()
}
},
Some(driver::GetCharResponseTag::None) => None,
_ => {
panic!()
}
}
let req = pl011_driver::Request::GetChar;
let resp: pl011_driver::GetCharSomeResponse = PL011_DRIVER
.pp_call(MessageInfo::send_using_postcard(req).unwrap())
.recv_using_postcard()
.unwrap();
resp.val
}

fn put_char(val: u8) {
let msg_info = PL011_DRIVER.pp_call(MessageInfo::send(
driver::RequestTag::PutChar,
driver::PutCharRequest { val },
));
assert_eq!(msg_info.label().try_into(), Ok(StatusMessageLabel::Ok));
let req = pl011_driver::Request::PutChar { val };
PL011_DRIVER
.pp_call(MessageInfo::send_using_postcard(req).unwrap())
.recv_empty()
.unwrap();
}

fn put_str(s: &str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ edition = "2021"
license = "BSD-2-Clause"

[dependencies]
num_enum = { version = "0.5.9", default-features = false }
zerocopy = "0.6.1"
serde = { version = "1.0.147", default-features = false }
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
#![no_std]

use num_enum::{IntoPrimitive, TryFromPrimitive};
use zerocopy::{AsBytes, FromBytes};
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
#[cfg_attr(target_pointer_width = "32", repr(u32))]
#[cfg_attr(target_pointer_width = "64", repr(u64))]
pub enum RequestTag {
PutChar,
#[derive(Debug, Serialize, Deserialize)]
pub enum Request {
PutChar { val: u8 },
GetChar,
}

#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)]
#[repr(C)]
pub struct PutCharRequest {
pub val: u8,
}

#[derive(Clone, Copy, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
#[cfg_attr(target_pointer_width = "32", repr(u32))]
#[cfg_attr(target_pointer_width = "64", repr(u64))]
pub enum GetCharResponseTag {
None,
Some,
}

#[derive(Clone, Copy, PartialEq, Eq, AsBytes, FromBytes)]
#[repr(C)]
#[derive(Debug, Serialize, Deserialize)]
pub struct GetCharSomeResponse {
pub val: u8,
pub val: Option<u8>,
}
27 changes: 12 additions & 15 deletions crates/examples/sel4cp/banscii/pds/pl011-driver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use heapless::Deque;

use sel4cp::{memory_region_symbol, protection_domain, Channel, Handler, MessageInfo};
use sel4cp_message::{MessageInfoExt as _, NoMessageValue, StatusMessageLabel};
use sel4cp_message::MessageInfoExt as _;

use banscii_pl011_driver_core::Driver;
use banscii_pl011_driver_interface_types::*;
Expand Down Expand Up @@ -61,24 +61,21 @@ impl Handler for HandlerImpl {
msg_info: MessageInfo,
) -> Result<MessageInfo, Self::Error> {
Ok(match channel {
ASSISTANT => match msg_info.label().try_into().ok() {
Some(RequestTag::PutChar) => match msg_info.recv() {
Ok(PutCharRequest { val }) => {
ASSISTANT => match msg_info.recv_using_postcard::<Request>() {
Ok(req) => match req {
Request::PutChar { val } => {
self.driver.put_char(val);
MessageInfo::send(StatusMessageLabel::Ok, NoMessageValue)
MessageInfo::send_empty()
}
Err(_) => MessageInfo::send(StatusMessageLabel::Error, NoMessageValue),
},
Some(RequestTag::GetChar) => match self.buffer.pop_front() {
Some(val) => {
MessageInfo::send(GetCharResponseTag::Some, GetCharSomeResponse { val })
}
None => {
self.notify = true;
MessageInfo::send(GetCharResponseTag::None, NoMessageValue)
Request::GetChar => {
let val = self.buffer.pop_front();
if val.is_some() {
self.notify = true;
}
MessageInfo::send_using_postcard(GetCharSomeResponse { val }).unwrap()
}
},
None => MessageInfo::send(StatusMessageLabel::Error, NoMessageValue),
Err(_) => MessageInfo::send_unspecified_error(),
},
_ => {
unreachable!()
Expand Down
13 changes: 7 additions & 6 deletions crates/examples/sel4cp/http-server/pds/server/src/net_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sel4cp::MessageInfo;
use sel4cp_message::{MessageInfoExt as _, NoMessageValue, StatusMessageLabel};
use sel4cp_message::MessageInfoExt as _;

use sel4cp_http_server_example_virtio_net_driver_interface_types::*;

Expand All @@ -13,11 +13,12 @@ impl NetClient {
}

pub fn get_mac_address(&self) -> MacAddress {
let msg_info = self
let req = Request::GetMacAddress;
let resp: GetMacAddressResponse = self
.channel
.pp_call(MessageInfo::send(RequestTag::GetMacAddress, NoMessageValue));
assert_eq!(msg_info.label().try_into(), Ok(StatusMessageLabel::Ok));
let GetMacAddressResponse { mac_address } = msg_info.recv().unwrap();
mac_address
.pp_call(MessageInfo::send_using_postcard(req).unwrap())
.recv_using_postcard()
.unwrap();
resp.mac_address
}
}
Loading

0 comments on commit b5b5432

Please sign in to comment.