Skip to content

Commit

Permalink
Never trap for LED operations (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeyeh authored Apr 20, 2024
1 parent 33fe483 commit f75edc9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/prelude/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Minor

- Change `led::{get,set}()` to never trap and return an error instead
- Document all public API
- Remove deprecated `println!` macro
- Add `Sha384` and `HmacSha384` to the `rust-crypto` feature
Expand Down
2 changes: 2 additions & 0 deletions crates/prelude/src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn count() -> usize {
/// Returns the status of a LED.
///
/// The `led` argument is the index of the LED. It must be less than [count()].
#[track_caller]
pub fn get(led: usize) -> api::Status {
let status = convert_bool(unsafe { api::get(api::get::Params { led }) }).unwrap();
match status {
Expand All @@ -44,6 +45,7 @@ pub fn get(led: usize) -> api::Status {
///
/// The `led` argument is the index of the LED. It must be less than [count()]. The `status`
/// argument is the new status.
#[track_caller]
pub fn set(led: usize, status: api::Status) {
let params = api::set::Params { led, status: status as usize };
convert_unit(unsafe { api::set(params) }).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions crates/scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.3.1-git

### Minor

- Change `led::{get,set}()` to never trap and return an error instead

### Patch

- Simplify `#[cfg(all)]` attributes between board and applet features
Expand Down
20 changes: 13 additions & 7 deletions crates/scheduler/src/call/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use wasefire_board_api::led::Api as _;
use wasefire_board_api::Api as Board;
#[cfg(feature = "board-api-led")]
use wasefire_board_api::{self as board, Id, Support};

#[cfg(feature = "board-api-led")]
use crate::Trap;
use wasefire_error::{Code, Error};

use crate::{DispatchSchedulerCall, SchedulerCall};

pub fn process<B: Board>(call: Api<DispatchSchedulerCall<B>>) {
Expand All @@ -44,8 +44,10 @@ fn count<B: Board>(call: SchedulerCall<B, api::count::Sig>) {
fn get<B: Board>(call: SchedulerCall<B, api::get::Sig>) {
let api::get::Params { led } = call.read();
let result = try {
let id = Id::new(*led as usize).ok_or(Trap)?;
board::Led::<B>::get(id)
match Id::new(*led as usize) {
Some(id) => board::Led::<B>::get(id),
None => Err(Error::user(Code::InvalidArgument)),
}
};
call.reply(result);
}
Expand All @@ -54,9 +56,13 @@ fn get<B: Board>(call: SchedulerCall<B, api::get::Sig>) {
fn set<B: Board>(call: SchedulerCall<B, api::set::Sig>) {
let api::set::Params { led, status } = call.read();
let result = try {
let id = Id::new(*led as usize).ok_or(Trap)?;
let on = matches!(api::Status::try_from(*status)?, api::Status::On);
board::Led::<B>::set(id, on)
match Id::new(*led as usize) {
Some(id) => {
let on = matches!(api::Status::try_from(*status)?, api::Status::On);
board::Led::<B>::set(id, on)
}
None => Err(Error::user(Code::InvalidArgument)),
}
};
call.reply(result);
}

0 comments on commit f75edc9

Please sign in to comment.