libits_client/reception/
mortal.rs

1// Software Name: its-client
2// SPDX-FileCopyrightText: Copyright (c) 2016-2022 Orange
3// SPDX-License-Identifier: MIT License
4//
5// This software is distributed under the MIT license, see LICENSE.txt file for more details.
6//
7// Author: Frédéric GARDES <frederic.gardes@orange.com> et al.
8// Software description: This Intelligent Transportation Systems (ITS) [MQTT](https://mqtt.org/) client based on the [JSon](https://www.json.org) [ETSI](https://www.etsi.org/committee/its) specification transcription provides a ready to connect project for the mobility (connected and autonomous vehicles, road side units, vulnerable road users,...).
9use std::time::{SystemTime, UNIX_EPOCH};
10
11pub trait Mortal {
12    fn timeout(&self) -> u128;
13
14    fn expired(&self) -> bool {
15        // debug!("we check if {} greater than {}", now() , self.timeout());
16        now() > self.timeout()
17    }
18
19    fn terminate(&mut self);
20
21    fn terminated(&self) -> bool;
22
23    fn remaining_time(&self) -> u128 {
24        return (self.timeout() - now()) / 1000;
25    }
26}
27
28pub fn now() -> u128 {
29    SystemTime::now()
30        .duration_since(UNIX_EPOCH)
31        .unwrap()
32        .as_millis()
33}
34
35/// Unit: millisecond since ETSI epoch (2004/01/01, so 1072915195000).
36/// Time at which a new DENM, an update DENM or a cancellation DENM is generated.
37/// utcStartOf2004(0), oneMillisecAfterUTCStartOf2004(1)
38pub(crate) fn etsi_now() -> u128 {
39    etsi_timestamp(now())
40}
41
42pub(crate) fn timestamp(etsi_timestamp: u128) -> u128 {
43    etsi_timestamp + 1072915195000
44}
45
46/// Unit: millisecond since ETSI epoch (2004/01/01, so 1072915195000).
47/// Time at which a new DENM, an update DENM or a cancellation DENM is generated.
48/// utcStartOf2004(0), oneMillisecAfterUTCStartOf2004(1)
49pub fn etsi_timestamp(timestamp: u128) -> u128 {
50    timestamp - 1072915195000
51}