kernel/hil/
date_time.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! HIL for Date Time interface
6
7use crate::ErrorCode;
8
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub enum DayOfWeek {
11    Sunday,
12    Monday,
13    Tuesday,
14    Wednesday,
15    Thursday,
16    Friday,
17    Saturday,
18}
19
20#[derive(Copy, Clone, Debug, PartialEq)]
21pub enum Month {
22    January,
23    February,
24    March,
25    April,
26    May,
27    June,
28    July,
29    August,
30    September,
31    October,
32    November,
33    December,
34}
35
36#[derive(Copy, Clone, Debug, PartialEq)]
37pub struct DateTimeValues {
38    pub year: u16,
39    pub month: Month,
40    pub day: u8,
41    pub day_of_week: DayOfWeek,
42    pub hour: u8,
43    pub minute: u8,
44    pub seconds: u8,
45}
46
47/// Interface for reading and setting the current time
48pub trait DateTime<'a> {
49    /// Request driver to return date and time
50    ///
51    /// When successful, this function will be followed by the callback
52    /// `callback_get_date` which provides the actual date and time
53    /// or an error.
54    fn get_date_time(&self) -> Result<(), ErrorCode>;
55
56    /// Sets the current date and time
57    ///
58    /// When successful this function call must be followed by a call
59    /// to `callback_set_date`.
60    fn set_date_time(&self, date_time: DateTimeValues) -> Result<(), ErrorCode>;
61
62    /// Sets a client that calls the callback function when date and time is requested
63    fn set_client(&self, client: &'a dyn DateTimeClient);
64}
65
66/// Callback handler for when current date is read or set.
67pub trait DateTimeClient {
68    /// Called when a date time reading has completed.
69    /// Takes `Ok(DateTime)` of current date and passes it when scheduling an upcall.
70    /// If an error is encountered it takes an `Err(ErrorCode)`
71    fn get_date_time_done(&self, datetime: Result<DateTimeValues, ErrorCode>);
72
73    /// Called when a date is set
74    /// Takes `Ok(())` if time is set correctly.
75    /// Takes  `Err(ErrorCode)` in case of an error
76    fn set_date_time_done(&self, result: Result<(), ErrorCode>);
77}