1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Test that an Alarm implementation is working by trying a few edge
//! cases on the delay, including delays of 1 and 0 delays. Depends
//! on a working UART and debug! macro.
//!
//! Author: Philip Levis <plevis@google.com>
//! Last Modified: 6/17/2020
use core::cell::Cell;
use kernel::debug;
use kernel::hil::time::{Alarm, AlarmClient, ConvertTicks, Ticks};

pub struct TestAlarmEdgeCases<'a, A: 'a> {
    alarm: &'a A,
    counter: Cell<usize>,
    alarms: [u32; 20],
}

impl<'a, A: Alarm<'a>> TestAlarmEdgeCases<'a, A> {
    pub fn new(alarm: &'a A) -> TestAlarmEdgeCases<'a, A> {
        TestAlarmEdgeCases {
            alarm,
            counter: Cell::new(0),
            alarms: [
                100, 200, 25, 25, 25, 25, 500, 0, 448, 15, 19, 1, 0, 33, 5, 1000, 27, 1, 0, 1,
            ],
        }
    }

    pub fn run(&self) {
        debug!("Starting alarm edge case tests.");
        self.set_next_alarm();
    }

    fn set_next_alarm(&self) {
        let counter = self.counter.get();
        let delay = self.alarm.ticks_from_ms(self.alarms[counter % 20]);
        let now = self.alarm.now();
        let start = now.wrapping_sub(A::Ticks::from(10));

        debug!(
            "{}: Setting alarm to {} + {} = {}",
            now.into_u32(),
            start.into_u32(),
            delay.into_u32(),
            start.wrapping_add(delay).into_u32()
        );
        self.alarm.set_alarm(start, delay);
        self.counter.set(counter + 1);
    }
}

impl<'a, A: Alarm<'a>> AlarmClient for TestAlarmEdgeCases<'a, A> {
    fn alarm(&self) {
        let now = self.alarm.now();
        debug!("Alarm fired at {}.", now.into_u32());
        self.set_next_alarm();
    }
}