leodos_libcfs/cfe/
time.rs1use crate::cfe::duration::Duration;
8use crate::error::Result;
9use crate::ffi;
10use crate::status::check;
11use core::fmt;
12use core::ops::{Add, Sub};
13use core::str;
14
15#[derive(Debug, Clone, Copy)]
17#[repr(transparent)]
18pub struct SysTime(pub(crate) ffi::CFE_TIME_SysTime_t);
19
20impl PartialEq for SysTime {
22 fn eq(&self, other: &Self) -> bool {
23 self.0.Seconds == other.0.Seconds && self.0.Subseconds == other.0.Subseconds
24 }
25}
26
27impl Eq for SysTime {}
28
29impl PartialOrd for SysTime {
30 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
31 Some(self.cmp(other))
32 }
33}
34
35impl Ord for SysTime {
36 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
37 use core::cmp::Ordering;
38 match unsafe { ffi::CFE_TIME_Compare(self.0, other.0) } {
39 ffi::CFE_TIME_Compare_CFE_TIME_A_LT_B => Ordering::Less,
40 ffi::CFE_TIME_Compare_CFE_TIME_A_GT_B => Ordering::Greater,
41 _ => Ordering::Equal,
42 }
43 }
44}
45
46impl fmt::Display for SysTime {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 let mut buffer = [0u8; ffi::CFE_TIME_PRINTED_STRING_SIZE as usize];
50 unsafe {
51 ffi::CFE_TIME_Print(buffer.as_mut_ptr() as *mut libc::c_char, self.0);
52 }
53 let len = buffer.iter().position(|&b| b == 0).unwrap_or(0);
55
56 let s = str::from_utf8(&buffer[..len]).map_err(|_| fmt::Error)?;
58 f.write_str(s)
59 }
60}
61
62impl From<Duration> for SysTime {
63 fn from(duration: Duration) -> Self {
64 let subseconds = microseconds_to_subseconds(duration.nanos() / 1000);
65 SysTime(ffi::CFE_TIME_SysTime_t {
66 Seconds: duration.secs(),
67 Subseconds: subseconds,
68 })
69 }
70}
71
72impl SysTime {
73 pub fn seconds(&self) -> u32 {
75 self.0.Seconds
76 }
77
78 pub fn subseconds(&self) -> u32 {
81 self.0.Subseconds
82 }
83
84 pub fn now() -> Self {
87 Self(unsafe { ffi::CFE_TIME_GetTime() })
88 }
89
90 pub fn now_tai() -> Self {
95 Self(unsafe { ffi::CFE_TIME_GetTAI() })
96 }
97
98 pub fn now_utc() -> Self {
104 Self(unsafe { ffi::CFE_TIME_GetUTC() })
105 }
106
107 pub fn now_met() -> Self {
109 Self(unsafe { ffi::CFE_TIME_GetMET() })
110 }
111
112 pub fn now_stcf() -> SysTime {
114 SysTime(unsafe { ffi::CFE_TIME_GetSTCF() })
115 }
116
117 pub fn to_sc(&self) -> Self {
119 Self(unsafe { ffi::CFE_TIME_MET2SCTime(self.0) })
120 }
121
122 pub fn to_ccsds(self) -> [u8; 6] {
130 let mut ccsds_time = [0u8; 6];
131
132 let seconds = self.seconds();
133
134 ccsds_time[0..4].copy_from_slice(&seconds.to_be_bytes());
136
137 let subseconds = self.subseconds();
139
140 let subseconds_16bit = (subseconds >> 16) as u16;
144
145 ccsds_time[4..6].copy_from_slice(&subseconds_16bit.to_be_bytes());
147
148 ccsds_time
149 }
150}
151
152impl Add for SysTime {
153 type Output = Self;
154
155 fn add(self, other: Self) -> Self::Output {
157 Self(unsafe { ffi::CFE_TIME_Add(self.0, other.0) })
158 }
159}
160
161impl Sub for SysTime {
162 type Output = Self;
163
164 fn sub(self, other: Self) -> Self::Output {
166 Self(unsafe { ffi::CFE_TIME_Subtract(self.0, other.0) })
167 }
168}
169
170pub fn subseconds_to_microseconds(subseconds: u32) -> u32 {
172 unsafe { ffi::CFE_TIME_Sub2MicroSecs(subseconds) }
173}
174
175pub fn microseconds_to_subseconds(microseconds: u32) -> u32 {
180 unsafe { ffi::CFE_TIME_Micro2SubSecs(microseconds) }
181}
182
183pub type SynchCallback = unsafe extern "C" fn() -> i32;
185
186pub fn register_synch_callback(callback: SynchCallback) -> Result<()> {
193 check(unsafe { ffi::CFE_TIME_RegisterSynchCallback(Some(callback)) })?;
194 Ok(())
195}
196
197pub fn unregister_synch_callback(callback: SynchCallback) -> Result<()> {
199 check(unsafe { ffi::CFE_TIME_UnregisterSynchCallback(Some(callback)) })?;
200 Ok(())
201}
202
203pub fn get_met_seconds() -> u32 {
205 unsafe { ffi::CFE_TIME_GetMETseconds() }
206}
207
208pub fn get_met_subseconds() -> u32 {
210 unsafe { ffi::CFE_TIME_GetMETsubsecs() }
211}
212
213pub fn get_leap_seconds() -> i16 {
215 unsafe { ffi::CFE_TIME_GetLeapSeconds() }
216}
217
218pub fn get_clock_state() -> ffi::CFE_TIME_ClockState_Enum_t {
220 unsafe { ffi::CFE_TIME_GetClockState() }
221}
222
223pub fn get_clock_info() -> u16 {
225 unsafe { ffi::CFE_TIME_GetClockInfo() }
226}
227
228pub fn external_tone() {
232 unsafe { ffi::CFE_TIME_ExternalTone() };
233}
234
235pub fn external_met(new_met: SysTime) {
237 unsafe { ffi::CFE_TIME_ExternalMET(new_met.0) };
238}
239
240pub fn external_gps(new_time: SysTime, new_leaps: i16) {
242 unsafe { ffi::CFE_TIME_ExternalGPS(new_time.0, new_leaps) };
243}
244
245pub fn external_time(new_time: SysTime) {
247 unsafe { ffi::CFE_TIME_ExternalTime(new_time.0) };
248}