Skip to content

Added own timer.#468

Closed
shilingwangggg wants to merge 6 commits intogoogle:developfrom
shilingwangggg:feature/own-timer
Closed

Added own timer.#468
shilingwangggg wants to merge 6 commits intogoogle:developfrom
shilingwangggg:feature/own-timer

Conversation

@shilingwangggg
Copy link
Copy Markdown
Contributor

Create Timer trait for future use. Implement Timer using libtock core.

@shilingwangggg shilingwangggg requested a review from ia0 April 26, 2022 19:26
@@ -0,0 +1,70 @@
trait Timer: Sized {
fn start(milliseconds: u32) -> Self;
fn has_elapsed(self) -> Option<Self>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some documentation for those 2 functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation in Rust use three slashes:

/// Creates and starts a timer.
fn start(milliseconds: u32) -> Self;

/// Returns the remaining timer of a timer.
///
/// In particular, if the timer has elapsed, `None` is returned.
fn has_elapsed(self) -> Option<Self>;

And actually I would rename has_elapsed to is_running, because Some usually means true.

Copy link
Copy Markdown
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I guess we should try using it in this PR directly. Otherwise we don't know if it works.

@@ -0,0 +1,91 @@
// Copyright 2019 Google LLC
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change the year to 2022. This is all new code.

@@ -0,0 +1,70 @@
trait Timer: Sized {
fn start(milliseconds: u32) -> Self;
fn has_elapsed(self) -> Option<Self>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation in Rust use three slashes:

/// Creates and starts a timer.
fn start(milliseconds: u32) -> Self;

/// Returns the remaining timer of a timer.
///
/// In particular, if the timer has elapsed, `None` is returned.
fn has_elapsed(self) -> Option<Self>;

And actually I would rename has_elapsed to is_running, because Some usually means true.

pub fn should_wink(&self, now: CtapInstant) -> bool {
self.wink_permission.is_granted(now)
pub fn should_wink(&self) -> bool {
self.wink_permission.is_some() && self.wink_permission.unwrap().has_elapsed().is_some()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. First I'm surprised this compiles. Second, the signature of should_wink might be problematic with the has_elapsed API. I see at least 2 options:

  1. should_wink only returns the status since last update, i.e. self.wink_permission.is_some() (my preference)
  2. should_wink is &mut self and also updates the timer.

pub fn update_wink_timeout(&mut self, now: CtapInstant) {
self.wink_permission = self.wink_permission.check_expiration(now);
pub fn update_wink_timeout(&mut self) {
self.wink_permission = Some(LibtockAlarmTimer::start(Self::WINK_TIMEOUT_DURATION));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me. I would expect this to update the timer, not reset it.

self.wink_permission = self.wink_permission.and_then(LibtockAlarmTimer::has_expired);

src/ctap/mod.rs Outdated
pub fn new_reset() -> StatefulPermission {
StatefulPermission {
permission: TimedPermission::granted(now, RESET_TIMEOUT_DURATION),
timer: Timer::start(30000),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the constant?

pub fn clear_timer(&mut self, now: CtapInstant) {
if !self.permission.is_granted(now) {
pub fn clear_timer(&mut self) {
if self.timer.is_some() && self.timer.unwrap().has_elapsed() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should first update the timer, then check if elapsed.

self.timer = self.timer.and_then(has_expired);
if self.timer.is_none() { self.clear(); }

/// NFC only allows 19.8 seconds.
/// TODO(#15) multiplex over transports, add NFC
const INITIAL_USAGE_TIME_LIMIT: Milliseconds<ClockInt> = Milliseconds(30000 as ClockInt);
const INITIAL_USAGE_TIME_LIMIT:u32 = 30000_u32;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the unit?

}
self.usage_timer = self.usage_timer.check_expiration(now);
if !self.usage_timer.is_granted(now) {
if self.usage_timer.is_none() || self.usage_timer.unwrap().has_elapsed().is_none() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also update the timer. We could actually provide a function for convenience:

/// Updates the expiration of a timer.
pub fn update_timer<T: Timer>(timer: &mut Option<T>) {
    *timer = core::mem::replace(timer, None).and_then(T::has_expired);
}

@kaczmarczyck kaczmarczyck mentioned this pull request Feb 24, 2023
@kaczmarczyck
Copy link
Copy Markdown
Collaborator

Picked up by #596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants