watchdog_core/
platform.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4/// Platform hooks required by the watchdog device.
5#[async_trait::async_trait]
6pub trait WatchdogPlatform: Send {
7    /// Callback fired when the timer expires.
8    async fn on_timeout(&mut self);
9
10    // Check if the watchdog previously timed-out, clearing the bit in the
11    // process.
12    async fn read_and_clear_boot_status(&mut self) -> bool;
13}
14
15/// A simple implementation of [`WatchdogPlatform`], suitable for ephemeral VMs.
16pub struct SimpleWatchdogPlatform {
17    watchdog_status: bool,
18    cb: Box<dyn Fn() + Send + Sync>,
19}
20
21impl SimpleWatchdogPlatform {
22    pub fn new(on_timeout: Box<dyn Fn() + Send + Sync>) -> Self {
23        SimpleWatchdogPlatform {
24            watchdog_status: false,
25            cb: on_timeout,
26        }
27    }
28}
29
30#[async_trait::async_trait]
31impl WatchdogPlatform for SimpleWatchdogPlatform {
32    async fn on_timeout(&mut self) {
33        self.watchdog_status = true;
34        (self.cb)()
35    }
36
37    async fn read_and_clear_boot_status(&mut self) -> bool {
38        if self.watchdog_status {
39            self.watchdog_status = false;
40        }
41        self.watchdog_status
42    }
43}