Skip to content

Commit

Permalink
add dev_random example
Browse files Browse the repository at this point in the history
This example illustrates how to use the mount option on partitions.
  • Loading branch information
wucke13 committed Jul 14, 2023
1 parent 8ce18f9 commit 50a123d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- hello_part
- fuel_tank
- ping
- dev_random
env:
DURATION: 10s
RUST_LOG: trace
Expand Down
9 changes: 9 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ members = [

"examples/ping_client",
"examples/ping_server",

"examples/dev_random",
]

[workspace.dependencies]
Expand Down
11 changes: 11 additions & 0 deletions examples/dev_random/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dev_random"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
a653rs = { workspace = true, features = ["macros"] }
a653rs-linux = { path = "../../partition" }
log.workspace = true
55 changes: 55 additions & 0 deletions examples/dev_random/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use a653rs::partition;
use a653rs::prelude::PartitionExt;
use a653rs_linux::partition::ApexLogger;
use log::LevelFilter;

fn main() {
ApexLogger::install_panic_hook();
ApexLogger::install_logger(LevelFilter::Trace).unwrap();

dev_random::Partition.run()
}

#[partition(a653rs_linux::partition::ApexLinuxPartition)]
mod dev_random {
use log::info;
use std::{fs::*, io::Read};

#[start(cold)]
fn cold_start(mut ctx: start::Context) {
// create and start an aperiodic process
ctx.create_process_0().unwrap().start().unwrap();
}

// do the same as a cold_start
#[start(warm)]
fn warm_start(ctx: start::Context) {
cold_start(ctx);
}

// this aperiodic process opens /dev/random and reads some random bytes from it
#[aperiodic(
time_capacity = "Infinite",
stack_size = "8KB",
base_priority = 1,
deadline = "Soft"
)]
fn process_0(_: process_0::Context) {
info!("started process_0");

// open the device file and read its metadata
let filename = "/dev/random";
let mut f = File::open(&filename).expect("no file found");
let metadata = metadata(&filename).expect("unable to read metadata");
info!("metadata: {metadata:#?}");

// read 16 bytes from the device
let mut buffer = [0u8; 16];
f.read(&mut buffer).expect("buffer overflow");
info!("got some randomness: {buffer:?}");

info!("terminating this partitiong by setting the operating mode to idle");
// TODO wait for https://github.com/DLR-FT/a653rs/issues/22 to be fixed
// Hypervisor::set_partition_mode(OperatingMode::Idle);
}
}
4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
name = "ping";
partitions = [ "ping_server" "ping_client" ];
}
{
name = "dev_random";
partitions = [ "dev_random" ];
}
];

cargoPackageList = ps: builtins.map (p: "--package=${p}") ps;
Expand Down

0 comments on commit 50a123d

Please sign in to comment.