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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Provide multiple Timers on top of a single underlying Alarm.

use core::cell::Cell;
use core::cmp;

use kernel::collections::list::{List, ListLink, ListNode};
use kernel::hil::time::{self, Alarm, Ticks, Time, Timer};
use kernel::utilities::cells::{NumericCellExt, OptionalCell};
use kernel::ErrorCode;

use crate::virtualizers::virtual_alarm::VirtualMuxAlarm;

#[derive(Copy, Clone, Debug, PartialEq)]
enum Mode {
    Disabled,
    OneShot,
    Repeating,
}

/// An object to multiplex multiple "virtual" timers over a single underlying alarm. A
/// `VirtualTimer` is a node in a linked list of timers that share the same underlying alarm.
pub struct VirtualTimer<'a, A: Alarm<'a>> {
    /// Underlying alarm which multiplexes all these virtual timers.
    mux: &'a MuxTimer<'a, A>,
    /// Reference time point when this timer was setup.
    when: Cell<A::Ticks>,
    /// Interval between events reported by this timer.
    interval: Cell<A::Ticks>,
    /// Current mode of this timer.
    mode: Cell<Mode>,
    /// Next timer in the list.
    next: ListLink<'a, VirtualTimer<'a, A>>,
    /// Timer client for this node in the list.
    client: OptionalCell<&'a dyn time::TimerClient>,
}

impl<'a, A: Alarm<'a>> ListNode<'a, VirtualTimer<'a, A>> for VirtualTimer<'a, A> {
    fn next(&self) -> &'a ListLink<VirtualTimer<'a, A>> {
        &self.next
    }
}

impl<'a, A: Alarm<'a>> VirtualTimer<'a, A> {
    /// After calling new, always call setup()
    pub fn new(mux_timer: &'a MuxTimer<'a, A>) -> VirtualTimer<'a, A> {
        let zero = A::Ticks::from(0);
        let v = VirtualTimer {
            mux: mux_timer,
            when: Cell::new(zero),
            interval: Cell::new(zero),
            mode: Cell::new(Mode::Disabled),
            next: ListLink::empty(),
            client: OptionalCell::empty(),
        };
        v
    }

    /// Call this method immediately after new() to link this to the mux, otherwise timers won't
    /// fire
    pub fn setup(&'a self) {
        self.mux.timers.push_head(self);
    }

    // Start a new timer, configuring its mode and adjusting the underlying alarm if needed.
    fn start_timer(&self, interval: A::Ticks, mode: Mode) -> A::Ticks {
        if self.mode.get() == Mode::Disabled {
            self.mux.enabled.increment();
        }
        self.mode.set(mode);

        // We can't fire faster than the minimum dt of the alarm.
        let real_interval: A::Ticks = A::Ticks::from(cmp::max(
            interval.into_u32(),
            self.mux.alarm.minimum_dt().into_u32(),
        ));

        let now = self.mux.alarm.now();
        self.interval.set(real_interval);
        self.when.set(now.wrapping_add(real_interval));
        self.mux.calculate_alarm(now, real_interval);

        real_interval
    }
}

impl<'a, A: Alarm<'a>> Time for VirtualTimer<'a, A> {
    type Frequency = A::Frequency;
    type Ticks = A::Ticks;

    fn now(&self) -> A::Ticks {
        self.mux.alarm.now()
    }
}

impl<'a, A: Alarm<'a>> Timer<'a> for VirtualTimer<'a, A> {
    fn set_timer_client(&self, client: &'a dyn time::TimerClient) {
        self.client.set(client);
    }

    fn cancel(&self) -> Result<(), ErrorCode> {
        match self.mode.get() {
            Mode::Disabled => Ok(()),
            Mode::OneShot | Mode::Repeating => {
                self.mode.set(Mode::Disabled);
                self.mux.enabled.decrement();

                // If there are not more enabled timers, disable the
                // underlying alarm.
                if self.mux.enabled.get() == 0 {
                    let _ = self.mux.alarm.disarm();
                }
                Ok(())
            }
        }
    }

    fn interval(&self) -> Option<Self::Ticks> {
        match self.mode.get() {
            Mode::Disabled => None,
            Mode::OneShot | Mode::Repeating => Some(self.interval.get()),
        }
    }

    fn is_oneshot(&self) -> bool {
        self.mode.get() == Mode::OneShot
    }

    fn is_repeating(&self) -> bool {
        self.mode.get() == Mode::Repeating
    }

    fn is_enabled(&self) -> bool {
        match self.mode.get() {
            Mode::Disabled => false,
            Mode::OneShot | Mode::Repeating => true,
        }
    }

    fn oneshot(&self, interval: Self::Ticks) -> Self::Ticks {
        self.start_timer(interval, Mode::OneShot)
    }

    fn repeating(&self, interval: Self::Ticks) -> Self::Ticks {
        self.start_timer(interval, Mode::Repeating)
    }

    fn time_remaining(&self) -> Option<Self::Ticks> {
        match self.mode.get() {
            Mode::Disabled => None,
            Mode::OneShot | Mode::Repeating => {
                let when = self.when.get();
                let now = self.mux.alarm.now();
                Some(when.wrapping_sub(now))
            }
        }
    }
}

impl<'a, A: Alarm<'a>> time::AlarmClient for VirtualTimer<'a, A> {
    fn alarm(&self) {
        match self.mode.get() {
            Mode::Disabled => {} // Do nothing
            Mode::OneShot => {
                self.mode.set(Mode::Disabled);
                self.client.map(|client| client.timer());
            }
            Mode::Repeating => {
                // By setting the 'now' to be 'when', this ensures
                // the the repeating timer fires at a fixed interval:
                // it'll fire at when + (k * interval), for k=0...n.
                let when = self.when.get();
                let interval = self.interval.get();
                self.when.set(when.wrapping_add(interval));
                self.mux.calculate_alarm(when, interval);
                self.client.map(|client| client.timer());
            }
        }
    }
}

/// Structure to control a set of virtual timers multiplexed together on top of a single alarm.
pub struct MuxTimer<'a, A: Alarm<'a>> {
    /// Head of the linked list of virtual timers multiplexed together.
    timers: List<'a, VirtualTimer<'a, A>>,
    /// Number of virtual timers that are currently enabled.
    enabled: Cell<usize>,
    /// Underlying alarm, over which the virtual timers are multiplexed.
    alarm: &'a VirtualMuxAlarm<'a, A>,
}

impl<'a, A: Alarm<'a>> MuxTimer<'a, A> {
    pub const fn new(alarm: &'a VirtualMuxAlarm<'a, A>) -> MuxTimer<'a, A> {
        MuxTimer {
            timers: List::new(),
            enabled: Cell::new(0),
            alarm,
        }
    }

    fn calculate_alarm(&self, now: A::Ticks, interval: A::Ticks) {
        if self.enabled.get() == 1 {
            // First alarm, to just set it
            self.alarm.set_alarm(now, interval);
        } else {
            // If the current alarm doesn't fall within the range of
            // [now, now + interval), this means this new alarm
            // will fire sooner. This covers the case when the current
            // alarm is in the past, because it must have already fired
            // and the bottom half is pending. -pal
            let cur_alarm = self.alarm.get_alarm();
            let when = now.wrapping_add(interval);
            if !cur_alarm.within_range(now, when) {
                // This alarm is earlier: reset alarm to it
                self.alarm.set_alarm(now, interval);
            } else {
                // current alarm will fire earlier, keep it
            }
        }
    }
}

impl<'a, A: Alarm<'a>> time::AlarmClient for MuxTimer<'a, A> {
    fn alarm(&self) {
        // The "now" is when the alarm fired, not the current
        // time; this is case there was some delay. This also
        // ensures that all other timers are >= now.
        let now = self.alarm.get_alarm();
        // Check whether to fire each timer. At this level, alarms are one-shot,
        // so a repeating timer will reset its `when` in the alarm() callback.
        self.timers
            .iter()
            .filter(|cur| {
                cur.is_enabled()
                    && !now.within_range(
                        cur.when.get().wrapping_sub(cur.interval.get()),
                        cur.when.get(),
                    )
            })
            .for_each(|cur| {
                cur.alarm();
            });

        // Find the soonest alarm client (if any) and set the "next" underlying
        // alarm based on it.  This needs to happen after firing all expired
        // alarms since those may have reset new alarms.
        let next = self
            .timers
            .iter()
            .filter(|cur| cur.is_enabled())
            .min_by_key(|cur| cur.when.get().wrapping_sub(now).into_u32());

        // Set the alarm.
        if let Some(valrm) = next {
            self.alarm
                .set_alarm(now, valrm.when.get().wrapping_sub(now));
        } else {
            let _ = self.alarm.disarm();
        }
    }
}