libits_client/reception/exchange/
message.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 serde::{Deserialize, Serialize};
10
11use crate::analyse::configuration::Configuration;
12use crate::reception::exchange::collective_perception_message::CollectivePerceptionMessage;
13use crate::reception::exchange::cooperative_awareness_message::CooperativeAwarenessMessage;
14use crate::reception::exchange::decentralized_environmental_notification_message::DecentralizedEnvironmentalNotificationMessage;
15use crate::reception::exchange::mobile::Mobile;
16use crate::reception::exchange::ReferencePosition;
17use crate::reception::mortal::{etsi_timestamp, Mortal};
18use crate::reception::typed::Typed;
19
20#[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)]
21#[serde(untagged)]
22pub enum Message {
23    CAM(CooperativeAwarenessMessage),
24    DENM(DecentralizedEnvironmentalNotificationMessage),
25    CPM(CollectivePerceptionMessage),
26}
27
28impl Message {
29    pub fn get_type(&self) -> String {
30        match self {
31            // FIXME find how to call get_type() on any Message
32            Message::CAM(_) => CooperativeAwarenessMessage::get_type(),
33            Message::DENM(_) => DecentralizedEnvironmentalNotificationMessage::get_type(),
34            Message::CPM(_) => CollectivePerceptionMessage::get_type(),
35        }
36    }
37
38    pub fn appropriate(&mut self, configuration: &Configuration, timestamp: u128) -> u32 {
39        match self {
40            // FIXME find how to change the fileds on any Message
41            Message::CAM(ref mut message) => {
42                let station_id = configuration.station_id(Some(message.station_id));
43                message.station_id = station_id;
44                // TODO update the generation delta time
45                station_id
46            }
47            Message::DENM(ref mut message) => {
48                let station_id = configuration.station_id(Some(message.station_id));
49                message.station_id = station_id;
50                // FIXME find why the serde Serializer can't match the u128
51                message.management_container.reference_time = etsi_timestamp(timestamp) as u64;
52                station_id
53            }
54            Message::CPM(ref mut message) => {
55                let station_id = configuration.station_id(Some(message.station_id));
56                message.station_id = station_id;
57                // TODO update the generation delta time
58                station_id
59            }
60        }
61    }
62}
63
64impl Mortal for Message {
65    fn timeout(&self) -> u128 {
66        if let Message::DENM(message) = self {
67            return message.timeout();
68        }
69        // TODO implement a timeout on the cam and cpm
70        0
71    }
72
73    fn terminate(&mut self) {
74        if let Message::DENM(message) = self {
75            message.terminate();
76        }
77        // TODO implement a terminate on the cam and cpm
78    }
79
80    fn terminated(&self) -> bool {
81        if let Message::DENM(message) = self {
82            return message.terminated();
83        }
84        // TODO implement a timeout on the cam and cpm
85        false
86    }
87}
88
89impl Mobile for Message {
90    fn mobile_id(&self) -> u32 {
91        match self {
92            // FIXME find how to call mobile_id() on any Message implementing Mobile
93            Message::CAM(message) => message.mobile_id(),
94            Message::DENM(message) => message.mobile_id(),
95            Message::CPM(message) => message.mobile_id(),
96        }
97    }
98
99    fn position(&self) -> &ReferencePosition {
100        match self {
101            // FIXME find how to call position() on any Message implementing Mobile
102            Message::CAM(message) => message.position(),
103            Message::DENM(message) => message.position(),
104            Message::CPM(message) => message.position(),
105        }
106    }
107
108    fn speed(&self) -> Option<u16> {
109        match self {
110            // FIXME find how to call speed() on any Message implementing Mobile
111            Message::CAM(message) => message.speed(),
112            Message::DENM(message) => message.speed(),
113            Message::CPM(message) => message.speed(),
114        }
115    }
116
117    fn heading(&self) -> Option<u16> {
118        match self {
119            // FIXME find how to call speed() on any Message implementing Mobile
120            Message::CAM(message) => message.heading(),
121            Message::DENM(message) => message.heading(),
122            Message::CPM(message) => message.heading(),
123        }
124    }
125
126    fn stopped(&self) -> bool {
127        match self {
128            // FIXME find how to call stopped() on any Message implementing Mobile
129            Message::CAM(message) => message.stopped(),
130            Message::DENM(message) => message.stopped(),
131            Message::CPM(message) => message.stopped(),
132        }
133    }
134}