Skip to content

Commit

Permalink
Adapt examples to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Fabritius committed Sep 13, 2023
1 parent 3562504 commit 77fbbc5
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 76 deletions.
93 changes: 68 additions & 25 deletions examples/linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

use core::iter::Enumerate;
use embassy_executor::Spawner;
use embassy_time::{Duration, queue};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_time::Duration;
use embassy_time::Timer;
use heapless::spsc::{Consumer, Producer, Queue};
use heimlig::client::api::Api;
use heimlig::common::jobs::{Request, Response};
use heimlig::common::jobs::{Request, RequestType, Response};
use heimlig::common::queues;
use heimlig::common::queues::{RequestSink, ResponseSink};
use heimlig::crypto::rng;
use heimlig::crypto::rng::Rng;
use heimlig::hsm::core::Core;
use heimlig::hsm::keystore::NoKeyStore;
use embassy_sync::mutex::Mutex;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use heimlig::common::queues;
use heimlig::common::queues::{RequestSink, ResponseSink};
use heimlig::hsm::workers::rng_worker::RngWorker;
use log::{error, info};
use rand::RngCore;

// Request and response queues between tasks
const QUEUE_SIZE: usize = 8;
static mut CLIENT_TO_HSM: Queue<Request, QUEUE_SIZE> = Queue::new();
static mut HSM_TO_CLIENT: Queue<Response, QUEUE_SIZE> = Queue::new();
static mut CLIENT_TO_CORE: Queue<Request, QUEUE_SIZE> = Queue::new();
static mut CORE_TO_CLIENT: Queue<Response, QUEUE_SIZE> = Queue::new();
static mut CORE_TO_RNG_WORKER: Queue<Response, QUEUE_SIZE> = Queue::new();
static mut RNG_WORKER_TO_CORE: Queue<Request, QUEUE_SIZE> = Queue::new();

struct EntropySource {}

Expand Down Expand Up @@ -89,29 +92,60 @@ impl<'a> Iterator for ResponseQueueSource<'_, 'a> {

#[embassy_executor::task]
async fn hsm_task(
req_rx: Consumer<'static, Request<'_>, QUEUE_SIZE>,
resp_tx: Producer<'static, Response<'_>, QUEUE_SIZE>,
client_req_rx: Consumer<'static, Request<'_>, QUEUE_SIZE>,
client_resp_tx: Producer<'static, Response<'_>, QUEUE_SIZE>,
rng_req_tx: Producer<'static, Request<'_>, QUEUE_SIZE>,
rng_req_rx: Consumer<'static, Request<'_>, QUEUE_SIZE>,
rng_resp_tx: Producer<'static, Response<'_>, QUEUE_SIZE>,
rng_resp_rx: Consumer<'static, Response<'_>, QUEUE_SIZE>,
) {
// Channel
let requests_source = RequestQueueSource { consumer: req_rx };
let responses_sink = ResponseQueueSink { producer: resp_tx };
// Channels
let client_requests = RequestQueueSource {
consumer: client_req_rx,
};
let client_responses = ResponseQueueSink {
producer: client_resp_tx,
};
let rng_requests_rx = RequestQueueSource {
consumer: rng_req_rx,
};
let rng_requests_tx = RequestQueueSink {
producer: rng_req_tx,
};
let rng_responses_rx = ResponseQueueSource {
consumer: rng_resp_rx,
};
let rng_responses_tx = ResponseQueueSink {
producer: rng_resp_tx,
};

let rng = Rng::new(EntropySource {}, None);
let key_store = NoKeyStore{};
let mut rng_worker = RngWorker {
rng,
requests: rng_requests_rx.enumerate(),
responses: rng_responses_tx,
};
let key_store = NoKeyStore {};
let key_store = Mutex::<NoopRawMutex, NoKeyStore>::new(key_store);
let mut core: Core<
NoopRawMutex,
NoKeyStore,
Enumerate<RequestQueueSource<'_, '_>>,
ResponseQueueSink<'_,'_>,
ResponseQueueSink<'_, '_>,
RequestQueueSink<'_, '_>,
Enumerate<ResponseQueueSource<'_, '_>>,
> = Core::new(&key_store, requests_source.enumerate(), responses_sink);
> = Core::new(&key_store, client_requests.enumerate(), client_responses);
core.add_worker_channel(
&[RequestType::GetRandom],
rng_requests_tx,
rng_responses_rx.enumerate(),
);

/*loop {
core.process_next().expect("failed to process next request");
loop {
core.execute().expect("failed to forward request");
rng_worker.execute().expect("failed to process request");
Timer::after(Duration::from_millis(100)).await;
}*/
}
}

#[embassy_executor::task]
Expand Down Expand Up @@ -166,14 +200,23 @@ async fn main(spawner: Spawner) {

// Queues
// Unsafe: Access to mutable static only happens here. Static lifetime is required by embassy tasks.
let (req_tx, req_rx) = unsafe { CLIENT_TO_HSM.split() };
let (resp_tx, resp_rx) = unsafe { HSM_TO_CLIENT.split() };
let (client_req_tx, client_req_rx) = unsafe { CLIENT_TO_CORE.split() };
let (client_resp_tx, client_resp_rx) = unsafe { CORE_TO_CLIENT.split() };
let (rng_resp_tx, rng_resp_rx) = unsafe { CORE_TO_RNG_WORKER.split() };
let (rng_req_tx, rng_req_rx) = unsafe { RNG_WORKER_TO_CORE.split() };

// Start tasks
spawner
.spawn(hsm_task(req_rx, resp_tx))
.expect("failed to spawn HSM task");
.spawn(hsm_task(
client_req_rx,
client_resp_tx,
rng_req_tx,
rng_req_rx,
rng_resp_tx,
rng_resp_rx,
))
.expect("Failed to spawn HSM task");
spawner
.spawn(client_task(resp_rx, req_tx))
.expect("failed to spawn client task");
.spawn(client_task(client_resp_rx, client_req_tx))
.expect("Failed to spawn client task");
}
28 changes: 24 additions & 4 deletions examples/stm32h745i/cm4/Cargo.lock

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

29 changes: 25 additions & 4 deletions examples/stm32h745i/cm7/Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/stm32h745i/cm7/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ cortex-m-rt = { version = "0.7", default-features = false }
defmt = { version = "0.3", default-features = false }
defmt-rtt = { version = "0.4", default-features = false }
embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", version = "0.2", features = ["arch-cortex-m", "defmt", "executor-thread", "integrated-timers", "nightly"] }
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", version = "0.1", features = ["defmt", "defmt-timestamp-uptime", "nightly", "tick-hz-32_768", "unstable-traits"] }
embassy-stm32 = { git = "https://github.com/embassy-rs/embassy.git", version = "0.1", features = ["nightly", "defmt", "stm32h745xi-cm7", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] }
embassy-sync = { version = "0.2", default-features = false }
embassy-time = { git = "https://github.com/embassy-rs/embassy.git", version = "0.1", features = ["defmt", "defmt-timestamp-uptime", "nightly", "tick-hz-32_768", "unstable-traits"] }
heapless = { version = "0.7", default-features = false }
panic-probe = { version = "0.3", features = ["print-defmt"] }
rand = { version = "0.8", default-features = false }
Expand Down
Loading

0 comments on commit 77fbbc5

Please sign in to comment.