Skip to content

Commit

Permalink
Use RTC driver in HTTP server demo
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <nick@nickspinale.com>
  • Loading branch information
nspin committed Jan 7, 2024
1 parent 2290023 commit 7404d29
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub enum Request {

#[derive(Debug, Serialize, Deserialize)]
pub struct NowResponse {
pub micros: Seconds,
pub unix_time: Seconds,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// SPDX-License-Identifier: BSD-2-Clause
//

#![allow(dead_code)]

pub mod channels {
use sel4_microkit::Channel;

Expand Down
75 changes: 24 additions & 51 deletions crates/examples/microkit/http-server/pds/pl031-driver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#![no_main]
#![feature(never_type)]

use core::time::Duration;

use sel4_microkit::{memory_region_symbol, protection_domain, Channel, Handler, MessageInfo};
use sel4_microkit_message::MessageInfoExt as _;

Expand All @@ -22,9 +20,7 @@ use config::channels;

#[protection_domain]
fn init() -> HandlerImpl {
let mut driver =
unsafe { Driver::new(memory_region_symbol!(pl031_mmio_vaddr: *mut ()).as_ptr()) };
sel4_microkit::debug_println!("XXX {:?}", driver.now());
let driver = unsafe { Driver::new(memory_region_symbol!(pl031_mmio_vaddr: *mut ()).as_ptr()) };
HandlerImpl { driver }
}

Expand All @@ -35,50 +31,27 @@ struct HandlerImpl {
impl Handler for HandlerImpl {
type Error = !;

// fn notified(&mut self, channel: Channel) -> Result<(), Self::Error> {
// match channel {
// channels::DEVICE => {
// self.driver.handle_interrupt();
// channels::DEVICE.irq_ack().unwrap();
// channels::CLIENT.notify();
// }
// _ => {
// unreachable!()
// }
// }
// Ok(())
// }

// fn protected(
// &mut self,
// channel: Channel,
// msg_info: MessageInfo,
// ) -> Result<MessageInfo, Self::Error> {
// Ok(match channel {
// channels::CLIENT => match msg_info.recv_using_postcard::<Request>() {
// Ok(req) => match req {
// Request::Now => {
// let now = self.driver.now();
// MessageInfo::send_using_postcard(NowResponse {
// micros: now.as_micros().try_into().unwrap(),
// })
// .unwrap()
// }
// Request::SetTimeout { relative_micros } => {
// self.driver
// .set_timeout(Duration::from_micros(relative_micros));
// MessageInfo::send_empty()
// }
// Request::ClearTimeout => {
// self.driver.clear_timeout();
// MessageInfo::send_empty()
// }
// },
// Err(_) => MessageInfo::send_unspecified_error(),
// },
// _ => {
// unreachable!()
// }
// })
// }
fn protected(
&mut self,
channel: Channel,
msg_info: MessageInfo,
) -> Result<MessageInfo, Self::Error> {
Ok(match channel {
channels::CLIENT => match msg_info.recv_using_postcard::<Request>() {
Ok(req) => match req {
Request::Now => {
let now = self.driver.now();
MessageInfo::send_using_postcard(NowResponse {
unix_time: now.as_secs().try_into().unwrap(),
})
.unwrap()
}
},
Err(_) => MessageInfo::send_unspecified_error(),
},
_ => {
unreachable!()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ use server::Server;
const HTTP_PORT: u16 = 80;
const HTTPS_PORT: u16 = 443;

// TODO
const NOW: u64 = 1704284617;

pub async fn run_server<
T: BlockIO<ReadOnly, BlockSize = constant_block_sizes::BlockSize512> + Clone + 'static,
>(
now_unix_time: Duration,
now_fn: impl 'static + Send + Sync + Fn() -> Instant,
_timers_ctx: TimerManager,
network_ctx: ManagedInterface,
Expand All @@ -66,7 +64,7 @@ pub async fn run_server<
}
});

let tls_config = Arc::new(mk_tls_config(cert_pem, priv_pem, now_fn));
let tls_config = Arc::new(mk_tls_config(cert_pem, priv_pem, now_unix_time, now_fn));

let use_socket_for_https_closure: SocketUser<T> = Box::new({
move |server, socket| {
Expand Down Expand Up @@ -153,6 +151,7 @@ async fn use_socket_for_https<D: fat::BlockDevice + 'static, T: fat::TimeSource
fn mk_tls_config(
cert_pem: &str,
priv_pem: &str,
now_unix_time: Duration,
now_fn: impl 'static + Send + Sync + Fn() -> Instant,
) -> ServerConfig {
let cert_der = match rustls_pemfile::read_one_from_slice(cert_pem.as_bytes())
Expand Down Expand Up @@ -180,7 +179,7 @@ fn mk_tls_config(
.with_single_cert(vec![cert_der], key_der)
.unwrap();
config.time_provider = TimeProvider::new(GetCurrentTimeImpl::new(
UnixTime::since_unix_epoch(Duration::from_secs(NOW)),
UnixTime::since_unix_epoch(now_unix_time),
now_fn,
));

Expand Down
18 changes: 11 additions & 7 deletions crates/examples/microkit/http-server/pds/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ mod block_client;
mod config;
mod handler;
mod net_client;
mod rtc_client;
mod timer_client;

use block_client::BlockClient;
use config::channels;
use handler::HandlerImpl;
use net_client::NetClient;
use rtc_client::RTCClient;
use timer_client::TimerClient;

const BLOCK_CACHE_SIZE_IN_BLOCKS: usize = 128;
Expand Down Expand Up @@ -77,11 +79,17 @@ static LOGGER: Logger = LoggerBuilder::const_default()
fn init() -> impl Handler {
LOGGER.set().unwrap();

let timer_client = TimerClient::new(channels::TIMER_DRIVER);
let rtc_client = RTCClient::new(channels::RTC_DRIVER);
let timer_client = Arc::new(TimerClient::new(channels::TIMER_DRIVER));
let net_client = NetClient::new(channels::NET_DRIVER);
let block_client = BlockClient::new(channels::BLOCK_DRIVER);

let timer_client = Arc::new(timer_client);
let now_unix_time = Duration::from_secs(rtc_client.now().into());

let now_fn = {
let timer_client = timer_client.clone();
move || Instant::ZERO + Duration::from_micros(timer_client.now())
};

let notify_net: fn() = || channels::NET_DRIVER.notify();
let notify_block: fn() = || channels::BLOCK_DRIVER.notify();
Expand Down Expand Up @@ -158,11 +166,6 @@ fn init() -> impl Handler {
)
};

let now_fn = {
let timer_client = timer_client.clone();
move || Instant::ZERO + Duration::from_micros(timer_client.now())
};

HandlerImpl::new(
channels::TIMER_DRIVER,
channels::NET_DRIVER,
Expand All @@ -179,6 +182,7 @@ fn init() -> impl Handler {
let fs_block_io = disk.partition_using_mbr(&entry);
let fs_block_io = Rc::new(fs_block_io);
run_server(
now_unix_time,
now_fn,
timers_ctx,
network_ctx,
Expand Down
30 changes: 30 additions & 0 deletions crates/examples/microkit/http-server/pds/server/src/rtc_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use sel4_microkit::MessageInfo;
use sel4_microkit_message::MessageInfoExt as _;

use microkit_http_server_example_pl031_driver_interface_types::*;

pub struct RTCClient {
channel: sel4_microkit::Channel,
}

impl RTCClient {
pub fn new(channel: sel4_microkit::Channel) -> Self {
Self { channel }
}

pub fn now(&self) -> Seconds {
let req = Request::Now;
let resp: NowResponse = self
.channel
.pp_call(MessageInfo::send_using_postcard(req).unwrap())
.recv_using_postcard()
.unwrap();
resp.unix_time
}
}

0 comments on commit 7404d29

Please sign in to comment.