api_impl/
time.rs

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
// Copyright 2024 The Fuchsia Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implementation of TEE Internal Core API Specification 7 Time API
// Functions return specific error codes where applicable and panic on all other errors.

use tee_internal::binding::{TEE_Time, TEE_TIMEOUT_INFINITE};
use tee_internal::Result;

fn nanos_to_time(nanos: i64) -> TEE_Time {
    const NANOSECONDS_PER_MILLISECOND: i64 = 1000 * 1000;
    const MILLISECONDS_PER_SECOND: i64 = 1000;
    const NANOSECONDS_PER_SECOND: i64 = NANOSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND;

    let seconds = nanos / NANOSECONDS_PER_SECOND;
    let nanos_remainder = nanos - seconds * NANOSECONDS_PER_SECOND;
    let millis = nanos_remainder / NANOSECONDS_PER_MILLISECOND;
    TEE_Time { seconds: seconds as u32, millis: millis as u32 }
}

pub fn get_system_time() -> TEE_Time {
    inspect_stubs::track_stub!(TODO("https://fxbug.dev/384101082"), "get_system_time");
    let now_utc = fuchsia_runtime::utc_time();
    nanos_to_time(now_utc.into_nanos())
}

pub fn wait(timeout: u32) -> Result {
    inspect_stubs::track_stub!(TODO("https://fxbug.dev/370103570"), "wait should be cancelable");
    if timeout == TEE_TIMEOUT_INFINITE {
        std::thread::sleep(std::time::Duration::MAX);
    } else {
        std::thread::sleep(std::time::Duration::from_millis(timeout as u64));
    }
    Ok(())
}

pub fn get_ta_persistent_time() -> Result<TEE_Time> {
    inspect_stubs::track_stub!(TODO("https://fxbug.dev/384101082"), "get_ta_persistent_time");
    // For now, just return our system time.
    Ok(get_system_time())
}

pub fn set_ta_persistent_time(_time: &TEE_Time) -> Result {
    inspect_stubs::track_stub!(TODO("https://fxbug.dev/384101082"), "set_ta_persistent_time");
    // For now, ignore.
    Ok(())
}

pub fn get_ree_time() -> TEE_Time {
    inspect_stubs::track_stub!(TODO("https://fxbug.dev/384101082"), "get_ree_time");
    // For now, just return our system time.
    get_system_time()
}