From 467121c9621ccb70c456c53d2c62a4e0cfeace32 Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Fri, 10 Jul 2020 10:11:07 +0200 Subject: [PATCH] Update third_party/libtock-drivers to support OpenSK. --- third_party/libtock-drivers/Cargo.toml | 71 +-- third_party/libtock-drivers/src/buttons.rs | 191 +++---- third_party/libtock-drivers/src/console.rs | 99 ++-- third_party/libtock-drivers/src/led.rs | 161 ++---- third_party/libtock-drivers/src/lib.rs | 23 +- third_party/libtock-drivers/src/result.rs | 68 ++- third_party/libtock-drivers/src/rng.rs | 51 +- third_party/libtock-drivers/src/timer.rs | 558 +++------------------ third_party/libtock-drivers/src/util.rs | 9 + 9 files changed, 394 insertions(+), 837 deletions(-) create mode 100644 third_party/libtock-drivers/src/util.rs diff --git a/third_party/libtock-drivers/Cargo.toml b/third_party/libtock-drivers/Cargo.toml index f5cb932f..9f67f8c4 100644 --- a/third_party/libtock-drivers/Cargo.toml +++ b/third_party/libtock-drivers/Cargo.toml @@ -1,67 +1,16 @@ [package] -name = "libtock" -version = "0.2.0" -authors = ["Tock Project Developers "] +name = "libtock_drivers" +version = "0.1.0" +authors = [ + "Tock Project Developers " + "Guillaume Endignoux ", +] license = "MIT/Apache-2.0" edition = "2018" -[features] -alloc = ["libtock_core/alloc"] -custom_panic_handler = ["libtock_core/custom_panic_handler"] -custom_alloc_error_handler = ["libtock_core/custom_alloc_error_handler"] -__internal_disable_gpio_in_integration_test = [] - [dependencies] -libtock_core = { path = "core" } -libtock_codegen = { path = "codegen" } -futures = { version = "0.3.1", default-features = false, features = ["unstable", "cfg-target-has-atomic"] } - -[dev-dependencies] -corepack = { version = "0.4.0", default-features = false, features = ["alloc"] } -# We pin the serde version because newer serde versions may not be compatible -# with the nightly toolchain used by libtock-rs. -serde = { version = "=1.0.84", default-features = false, features = ["derive"] } - -[[example]] -name = "alloc_error" -path = "examples-features/alloc_error.rs" -required-features = ["alloc", "custom_alloc_error_handler"] - -[[example]] -name = "ble_scanning" -path = "examples-features/ble_scanning.rs" -required-features = ["alloc"] - -[[example]] -name = "libtock_test" -path = "examples-features/libtock_test.rs" -required-features = ["alloc"] +libtock_core = { path = "../../third_party/libtock-rs/core" } -[[example]] -name = "panic" -path = "examples-features/panic.rs" -required-features = ["custom_panic_handler"] - -[[example]] -name = "simple_ble" -path = "examples-features/simple_ble.rs" -required-features = ["alloc"] - -[profile.dev] -panic = "abort" -lto = true -debug = true - -[profile.release] -panic = "abort" -lto = true -debug = true - -[workspace] -exclude = [ "tock" ] -members = [ - "codegen", - "core", - "test_runner", - "tools/print_sizes", -] +[features] +debug_ctap = [] +verbose_usb = ["debug_ctap"] diff --git a/third_party/libtock-drivers/src/buttons.rs b/third_party/libtock-drivers/src/buttons.rs index aac5cb22..5ab985fc 100644 --- a/third_party/libtock-drivers/src/buttons.rs +++ b/third_party/libtock-drivers/src/buttons.rs @@ -1,10 +1,7 @@ -use crate::callback::CallbackSubscription; -use crate::callback::Consumer; -use crate::result::OtherError; -use crate::result::OutOfRangeError; -use crate::result::TockResult; -use crate::syscalls; +use crate::result::{OtherError, TockResult}; use core::marker::PhantomData; +use libtock_core::callback::{CallbackSubscription, Consumer}; +use libtock_core::syscalls; const DRIVER_NUMBER: usize = 0x00003; @@ -19,87 +16,98 @@ mod subscribe_nr { pub const SUBSCRIBE_CALLBACK: usize = 0; } -#[non_exhaustive] -pub struct ButtonsDriverFactory; - -impl ButtonsDriverFactory { - pub fn init_driver(&mut self) -> TockResult { - let buttons_driver = ButtonsDriver { - num_buttons: syscalls::command(DRIVER_NUMBER, command_nr::COUNT, 0, 0)?, - lifetime: PhantomData, - }; - Ok(buttons_driver) - } +pub fn with_callback(callback: CB) -> WithCallback { + WithCallback { callback } } -pub struct ButtonsDriver<'a> { - num_buttons: usize, - lifetime: PhantomData<&'a ()>, +pub struct WithCallback { + callback: CB, } -impl<'a> ButtonsDriver<'a> { - pub fn num_buttons(&self) -> usize { - self.num_buttons +struct ButtonConsumer; + +impl Consumer> for ButtonConsumer { + fn consume(data: &mut WithCallback, button_num: usize, state: usize, _: usize) { + (data.callback)(button_num, state.into()); } +} - /// Returns the button at 0-based index `button_num` - pub fn get(&self, button_num: usize) -> Result { - if button_num < self.num_buttons { - Ok(Button { - button_num, - lifetime: PhantomData, - }) - } else { - Err(OutOfRangeError) - } +impl WithCallback { + pub fn init(&mut self) -> TockResult { + let count = syscalls::command(DRIVER_NUMBER, command_nr::COUNT, 0, 0)?; + + let subscription = syscalls::subscribe::( + DRIVER_NUMBER, + subscribe_nr::SUBSCRIBE_CALLBACK, + self, + )?; + + Ok(Buttons { + count: count as usize, + subscription, + }) } +} - pub fn buttons(&self) -> Buttons { - Buttons { - num_buttons: self.num_buttons, +pub struct Buttons<'a> { + count: usize, + #[allow(dead_code)] // Used in drop + subscription: CallbackSubscription<'a>, +} + +#[derive(Copy, Clone, Debug)] +pub enum ButtonsError { + NotSupported, + SubscriptionFailed, +} + +impl<'a> Buttons<'a> { + pub fn iter_mut(&mut self) -> ButtonIter { + ButtonIter { curr_button: 0, + button_count: self.count, lifetime: PhantomData, } } - - pub fn subscribe( - &self, - callback: &'a mut CB, - ) -> TockResult { - syscalls::subscribe::( - DRIVER_NUMBER, - subscribe_nr::SUBSCRIBE_CALLBACK, - callback, - ) - .map_err(Into::into) - } } -struct ButtonsEventConsumer; +#[derive(Copy, Clone, Debug)] +pub enum ButtonState { + Pressed, + Released, +} -impl Consumer for ButtonsEventConsumer { - fn consume(callback: &mut CB, button_num: usize, button_state: usize, _: usize) { - let button_state = match button_state { +impl From for ButtonState { + fn from(state: usize) -> ButtonState { + match state { 0 => ButtonState::Released, 1 => ButtonState::Pressed, - _ => return, - }; - callback(button_num, button_state); + _ => unreachable!(), + } } } -pub struct Buttons<'a> { - num_buttons: usize, +impl<'a, 'b> IntoIterator for &'b mut Buttons<'a> { + type Item = ButtonHandle<'b>; + type IntoIter = ButtonIter<'b>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + +pub struct ButtonIter<'a> { curr_button: usize, + button_count: usize, lifetime: PhantomData<&'a ()>, } -impl<'a> Iterator for Buttons<'a> { - type Item = Button<'a>; +impl<'a> Iterator for ButtonIter<'a> { + type Item = ButtonHandle<'a>; fn next(&mut self) -> Option { - if self.curr_button < self.num_buttons { - let item = Button { + if self.curr_button < self.button_count { + let item = ButtonHandle { button_num: self.curr_button, lifetime: PhantomData, }; @@ -111,57 +119,52 @@ impl<'a> Iterator for Buttons<'a> { } } -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum ButtonState { - Pressed, - Released, -} - -impl From for bool { - fn from(button_state: ButtonState) -> Self { - match button_state { - ButtonState::Released => false, - ButtonState::Pressed => true, - } - } -} - -pub struct Button<'a> { +pub struct ButtonHandle<'a> { button_num: usize, lifetime: PhantomData<&'a ()>, } -impl<'a> Button<'a> { - pub fn button_num(&self) -> usize { - self.button_num - } - - pub fn read(&self) -> TockResult { - let button_state = syscalls::command(DRIVER_NUMBER, command_nr::READ, self.button_num, 0)?; - match button_state { - 0 => Ok(ButtonState::Released), - 1 => Ok(ButtonState::Pressed), - _ => Err(OtherError::ButtonsDriverInvalidState.into()), - } - } - - pub fn enable_interrupt(&self) -> TockResult<()> { +impl<'a> ButtonHandle<'a> { + pub fn enable(&mut self) -> TockResult