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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::{cmp, hash};
use serde::{Deserialize, Serialize};
use crate::reception::exchange::mobile::Mobile;
use crate::reception::exchange::{PathHistory, PositionConfidence, ReferencePosition};
use crate::reception::mortal::{etsi_now, timestamp, Mortal};
use crate::reception::typed::Typed;
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct DecentralizedEnvironmentalNotificationMessage {
pub protocol_version: u8,
pub station_id: u32,
pub management_container: ManagementContainer,
pub situation_container: Option<SituationContainer>,
pub location_container: Option<LocationContainer>,
pub alacarte_container: Option<AlacarteContainer>,
}
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagementContainer {
pub action_id: ActionId,
pub detection_time: u64,
pub reference_time: u64,
pub termination: Option<u8>,
pub event_position: ReferencePosition,
pub relevance_distance: Option<u8>,
pub relevance_traffic_direction: Option<u8>,
pub validity_duration: Option<u32>,
pub transmission_interval: Option<u16>,
pub station_type: Option<u8>,
pub confidence: Option<PositionConfidence>,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Hash)]
pub struct ActionId {
pub originating_station_id: u32,
pub sequence_number: u16,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct SituationContainer {
pub information_quality: Option<u8>,
pub event_type: EventType,
pub linked_cause: Option<EventType>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct LocationContainer {
pub event_speed: Option<u16>,
pub event_position_heading: Option<u16>,
pub traces: Vec<Trace>,
pub road_type: Option<u8>,
pub confidence: Option<LocationContainerConfidence>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct AlacarteContainer {
pub lane_position: Option<i8>,
pub positioning_solution: Option<u8>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct EventType {
pub cause: u8,
pub subcause: Option<u8>,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Trace {
#[serde(rename = "path_history")]
pub path_history: Vec<PathHistory>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct LocationContainerConfidence {
pub speed: Option<u8>,
pub heading: Option<u8>,
}
impl DecentralizedEnvironmentalNotificationMessage {
pub fn new_stationary_vehicle(
station_id: u32,
originating_station_id: u32,
event_position: ReferencePosition,
sequence_number: u16,
etsi_timestamp: u128,
event_position_heading: Option<u16>,
) -> Self {
Self::new(
station_id,
originating_station_id,
event_position,
sequence_number,
etsi_timestamp,
94,
Option::Some(0),
None,
None,
Some(0),
event_position_heading,
)
}
pub fn new_traffic_condition(
station_id: u32,
originating_station_id: u32,
event_position: ReferencePosition,
sequence_number: u16,
etsi_timestamp: u128,
subcause: Option<u8>,
relevance_distance: Option<u8>,
relevance_traffic_direction: Option<u8>,
event_speed: Option<u16>,
event_position_heading: Option<u16>,
) -> Self {
Self::new(
station_id,
originating_station_id,
event_position,
sequence_number,
etsi_timestamp,
1,
subcause,
relevance_distance,
relevance_traffic_direction,
event_speed,
event_position_heading,
)
}
fn new(
station_id: u32,
originating_station_id: u32,
event_position: ReferencePosition,
sequence_number: u16,
etsi_timestamp: u128,
cause: u8,
subcause: Option<u8>,
relevance_distance: Option<u8>,
relevance_traffic_direction: Option<u8>,
event_speed: Option<u16>,
event_position_heading: Option<u16>,
) -> Self {
Self {
protocol_version: 2,
station_id,
management_container: ManagementContainer {
action_id: ActionId {
originating_station_id,
sequence_number,
..Default::default()
},
detection_time: etsi_timestamp as u64,
reference_time: etsi_timestamp as u64,
event_position,
validity_duration: Option::Some(10),
station_type: Option::Some(5),
relevance_distance,
relevance_traffic_direction,
..Default::default()
},
situation_container: Option::from(SituationContainer {
event_type: EventType { cause, subcause },
..Default::default()
}),
location_container: Option::from(LocationContainer {
event_speed,
event_position_heading,
..Default::default()
}),
..Default::default()
}
}
pub fn is_stationary_vehicle(&self) -> bool {
self.situation_container.is_some()
&& 94 == self.situation_container.as_ref().unwrap().event_type.cause
}
pub fn is_traffic_condition(&self) -> bool {
self.situation_container.is_some()
&& 1 == self.situation_container.as_ref().unwrap().event_type.cause
}
}
impl hash::Hash for DecentralizedEnvironmentalNotificationMessage {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.management_container.hash(state);
}
}
impl cmp::Eq for DecentralizedEnvironmentalNotificationMessage {}
impl cmp::PartialEq for DecentralizedEnvironmentalNotificationMessage {
fn eq(&self, other: &Self) -> bool {
self.management_container == other.management_container
}
}
impl Mobile for DecentralizedEnvironmentalNotificationMessage {
fn mobile_id(&self) -> u32 {
self.station_id
}
fn position(&self) -> &ReferencePosition {
&self.management_container.event_position
}
fn speed(&self) -> Option<u16> {
if let Some(location_container) = &self.location_container {
return location_container.event_speed;
}
None
}
fn heading(&self) -> Option<u16> {
if let Some(location_container) = &self.location_container {
return location_container.event_position_heading;
}
None
}
}
impl Mortal for DecentralizedEnvironmentalNotificationMessage {
fn timeout(&self) -> u128 {
timestamp(self.management_container.detection_time as u128)
+ (self
.management_container
.validity_duration
.unwrap_or_default()
* 1000) as u128
}
fn terminate(&mut self) {
self.management_container.termination = Some(0);
self.management_container.detection_time = etsi_now() as u64;
self.management_container.validity_duration = Some(10);
}
fn terminated(&self) -> bool {
self.management_container.termination.is_some()
}
}
impl Typed for DecentralizedEnvironmentalNotificationMessage {
fn get_type() -> String {
"denm".to_string()
}
}
impl Default for ManagementContainer {
fn default() -> Self {
Self {
action_id: Default::default(),
detection_time: Default::default(),
reference_time: Default::default(),
termination: Default::default(),
event_position: Default::default(),
relevance_distance: Default::default(),
relevance_traffic_direction: Default::default(),
validity_duration: Some(600),
transmission_interval: Default::default(),
station_type: Default::default(),
confidence: Default::default(),
}
}
}
impl cmp::PartialEq for ManagementContainer {
fn eq(&self, other: &Self) -> bool {
self.action_id == other.action_id
}
}
impl hash::Hash for ManagementContainer {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.action_id.hash(state);
}
}
#[cfg(test)]
mod tests {
use crate::reception::exchange::decentralized_environmental_notification_message::DecentralizedEnvironmentalNotificationMessage;
use crate::reception::exchange::reference_position::ReferencePosition;
use crate::reception::mortal::{etsi_timestamp, now};
#[test]
fn create_new_stationary_vehicle() {
let station_id = 4567;
let originating_station_id = 1230;
let event_position = ReferencePosition::default();
let sequence_number = 10;
let reference_timestamp = now();
let event_position_heading = Some(3000);
std::thread::sleep(std::time::Duration::from_secs(1));
let denm = DecentralizedEnvironmentalNotificationMessage::new_stationary_vehicle(
station_id,
originating_station_id,
event_position.clone(),
sequence_number,
reference_timestamp,
event_position_heading,
);
assert_eq!(denm.station_id, station_id);
assert_eq!(
denm.management_container.action_id.originating_station_id,
originating_station_id
);
assert_eq!(denm.management_container.event_position, event_position);
assert_eq!(
denm.management_container.action_id.sequence_number,
sequence_number
);
assert_eq!(
denm.management_container.reference_time,
reference_timestamp as u64
);
let etsi_ref_time = etsi_timestamp(reference_timestamp) as u64;
assert!(denm.management_container.detection_time >= etsi_ref_time + 1000);
}
}