libits-client 3.1.0

library to connect on an ITS MQTT server
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
/*
 * Software Name : libits-client
 * SPDX-FileCopyrightText: Copyright (c) Orange SA
 * SPDX-License-Identifier: MIT
 *
 * This software is distributed under the MIT license,
 * see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
 *
 * Authors: see CONTRIBUTORS.md
 */

use crate::exchange::etsi::reference_position::{PathPoint, ReferencePosition};
use crate::exchange::etsi::{
    acceleration_from_etsi, heading_from_etsi, heading_to_etsi, speed_from_etsi, speed_to_etsi,
    timestamp_to_generation_delta_time,
};
use crate::mobility::mobile::Mobile;
use std::any::type_name;

use crate::exchange::etsi::acceleration::Acceleration;
use crate::exchange::etsi::cause_code::CauseCode;
use crate::exchange::etsi::curvature::Curvature;
use crate::exchange::etsi::heading::Heading;
use crate::exchange::etsi::speed::Speed;
use crate::exchange::etsi::steering_wheel_angle::SteeringWheelAngle;
use crate::exchange::etsi::vehicle_length::VehicleLength;
use crate::exchange::etsi::yaw_rate::YawRate;
use crate::exchange::message::content::Content;
use crate::exchange::message::content_error::ContentError;
use crate::exchange::message::content_error::ContentError::NotAMortal;
use crate::exchange::mortal::Mortal;
use crate::mobility::position::Position;
use serde::{Deserialize, Serialize};

/// Represents a Cooperative Awareness Message (CAM) according to an ETSI standard.
///
/// This message is used to describe the information about itself.
/// It implements the schema defined in the [CAM version 2.4.0][1].
///
/// [1]: https://github.com/Orange-OpenSource/its-client/blob/master/schema/cam/cam_schema_2-4-0.json
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CooperativeAwarenessMessage {
    /// Protocol version.
    pub protocol_version: u8,
    /// Unique identifier for the station.
    pub station_id: u32,
    /// Time difference since the last generation of the message.
    pub generation_delta_time: u16,
    /// Basic container with basic information about the station.
    pub basic_container: BasicContainer,
    /// High-frequency container with detailed vehicle information.
    pub high_frequency_container: HighFrequencyContainer,

    // Optional fields
    /// Low-frequency container with less frequent data about the vehicle.
    pub low_frequency_container: Option<LowFrequencyContainer>,
    /// Special vehicle container for additional vehicle information.
    pub special_vehicle_container: Option<SpecialVehicleContainer>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct BasicContainer {
    /// Type of the station, represented as an unsigned 8-bit integer.
    // TODO: implement a StationType enum
    pub station_type: u8,
    /// Reference position of the station, including latitude, longitude, altitude, and confidence.
    pub reference_position: ReferencePosition,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct HighFrequencyContainer {
    // Optional fields
    /// Basic vehicle container with high-frequency data about the vehicle.
    pub basic_vehicle_container_high_frequency: Option<BasicVehicleContainerHighFrequency>,
    /// RSU container with high-frequency data about the Road Side Unit (RSU).
    pub rsu_container_high_frequency: Option<RSUContainerHighFrequency>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct BasicVehicleContainerHighFrequency {
    /// Heading of the vehicle, represented as an angle in degrees.
    pub heading: Heading,
    /// Speed of the vehicle, represented in centimeters per second.
    pub speed: Speed,
    /// Drive direction of the vehicle, represented as an unsigned 8-bit integer.
    //TODO: implement a DriveDirection enum
    pub drive_direction: u8,
    /// Length of the vehicle, represented in centimeters.
    pub vehicle_length: VehicleLength,
    /// Width of the vehicle, represented in centimeters.
    pub vehicle_width: u16,
    /// Longitudinal acceleration of the vehicle, represented in centimeters per second squared.
    pub longitudinal_acceleration: Acceleration,
    /// Curvature of the vehicle's path, represented in a specific unit.
    pub curvature: Curvature,
    /// Curvature calculation mode, represented as an unsigned 8-bit integer.
    //TODO: implement a CurvatureCalculationMode enum
    pub curvature_calculation_mode: u8,
    /// Yaw rate of the vehicle, represented in radians per second.
    pub yaw_rate: YawRate,

    // Optional fields
    /// Acceleration control information for the vehicle.
    pub acceleration_control: Option<AccelerationControl>,
    /// Lane position of the vehicle, represented as an optional signed 8-bit integer.
    pub lane_position: Option<i8>,
    /// Steering wheel angle of the vehicle, represented as an optional angle in degrees.
    pub steering_wheel_angle: Option<SteeringWheelAngle>,
    /// Lateral acceleration of the vehicle, represented in centimeters per second squared.
    pub lateral_acceleration: Option<Acceleration>,
    /// Vertical acceleration of the vehicle, represented in centimeters per second squared.
    pub vertical_acceleration: Option<Acceleration>,
    /// Performance class of the vehicle, represented as an optional enum.
    pub performance_class: Option<PerformanceClass>,
    /// CEN DSRC tolling zone information, if applicable.
    pub cen_dsrc_tolling_zone: Option<CenDSRCTollingZone>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RSUContainerHighFrequency {
    // TODO: implement the container
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct LowFrequencyContainer {
    // Optional fields
    /// Basic vehicle container with low-frequency data about the vehicle.
    pub basic_vehicle_container_low_frequency: Option<BasicVehicleContainerLowFrequency>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct BasicVehicleContainerLowFrequency {
    /// Role of the vehicle, represented as an unsigned 8-bit integer.
    // TODO: implement a VehicleRole enum
    pub vehicle_role: u8,
    /// Exterior lights status of the vehicle.
    pub exterior_lights: ExteriorLights,
    /// Path history of the vehicle, represented as a vector of path points.
    pub path_history: Vec<PathPoint>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct SpecialVehicleContainer {
    // Optional fields
    /// Public transport container with specific information about public transport vehicles.
    pub public_transport_container: Option<PublicTransportContainer>,
    /// Special transport container with specific information about special transport vehicles.
    pub special_transport_container: Option<SpecialTransportContainer>,
    /// Dangerous goods container with specific information about vehicles carrying dangerous goods.
    pub dangerous_goods_container: Option<DangerousGoodsContainer>,
    /// Road works container with specific information about road works.
    pub road_works_container_basic: Option<RoadWorksContainerBasic>,
    /// Rescue container with specific information about rescue vehicles.
    pub rescue_container: Option<RescueContainer>,
    /// Emergency container with specific information about emergency vehicles.
    pub emergency_container: Option<EmergencyContainer>,
    /// Safety car container with specific information about safety vehicles.
    pub safety_car_container: Option<SafetyCarContainer>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct SafetyCarContainer {
    /// Light bar and siren status for the safety car.
    pub light_bar_siren_in_use: LightBarSirenInUse,

    // Optional fields
    /// Incident indication for the safety car, if applicable.
    pub incident_indication: Option<IncidentIndication>,
    /// Traffic rule information for the safety car, if applicable.
    pub traffic_rule: Option<u8>,
    /// Speed limit information for the safety car, if applicable.
    pub speed_limit: Option<u8>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct EmergencyContainer {
    /// Light bar and siren status for the emergency vehicle.
    pub light_bar_siren_in_use: LightBarSirenInUse,

    // Optional fields
    /// Incident indication for the emergency vehicle, if applicable.
    pub incident_indication: Option<IncidentIndication>,
    /// Traffic rule information for the emergency vehicle, if applicable.
    pub emergency_priority: Option<EmergencyPriority>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct EmergencyPriority {
    /// Priority level of the emergency vehicle, represented as an unsigned 8-bit integer.
    pub request_for_right_of_way: bool,
    /// Request for free crossing at a traffic light.
    pub request_for_free_crossing_at_a_traffic_light: bool,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct IncidentIndication {
    /// Cause code for the incident, represented as an unsigned 8-bit integer.
    pub cc_and_scc: CauseCode,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RescueContainer {
    /// Light bar and siren status for the rescue vehicle.
    pub light_bar_siren_in_use: LightBarSirenInUse,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoadWorksContainerBasic {
    /// Cause code for the road works, represented as an unsigned 8-bit integer.
    pub light_bar_siren_in_use: LightBarSirenInUse,

    // Optional fields
    /// Cause code and subcause code for the road works, if applicable.
    pub road_works_sub_cause_code: Option<u8>,
    /// Traffic rule information for the road works, if applicable.
    pub closed_lanes: Option<ClosedLanes>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClosedLanes {
    // Optional fields
    /// Inner hard shoulder status, represented as an optional unsigned 8-bit integer.
    pub inner_hard_shoulder_status: Option<u8>,
    /// Outer hard shoulder status, represented as an optional unsigned 8-bit integer.
    pub outer_hard_shoulder_status: Option<u8>,
    /// Driving lane status, represented as an optional DrivingLaneStatus struct.
    pub driving_lane_status: Option<DrivingLaneStatus>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct DrivingLaneStatus {
    /// Status of each lane, represented as boolean values.
    pub lane_1_closed: bool,
    pub lane_2_closed: bool,
    pub lane_3_closed: bool,
    pub lane_4_closed: bool,
    pub lane_5_closed: bool,
    pub lane_6_closed: bool,
    pub lane_7_closed: bool,
    pub lane_8_closed: bool,
    pub lane_9_closed: bool,
    pub lane_10_closed: bool,
    pub lane_11_closed: bool,
    pub lane_12_closed: bool,
    pub lane_13_closed: bool,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct DangerousGoodsContainer {
    /// Light bar and siren status for the dangerous goods vehicle.
    pub dangerous_goods_basic: u8,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct SpecialTransportContainer {
    /// Light bar and siren status for the special transport vehicle.
    pub special_transport_type: SpecialTransportType,
    /// Light bar and siren status for the special transport vehicle.
    pub light_bar_siren_in_use: LightBarSirenInUse,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct SpecialTransportType {
    /// Type of the special transport, represented as boolean values.
    pub heavy_load: bool,
    pub excess_width: bool,
    pub excess_length: bool,
    pub excess_height: bool,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct LightBarSirenInUse {
    /// Light bar activation status, represented as a boolean value.
    pub light_bar_activated: bool,
    /// Siren activation status, represented as a boolean value.
    pub siren_activated: bool,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PublicTransportContainer {
    /// Light bar and siren status for the public transport vehicle.
    pub embarkation_status: bool,

    // Optional fields
    /// PT activation information for the public transport vehicle.
    pub pt_activation: Option<PTActivation>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PTActivation {
    pub pt_activation_type: u8,
    pub pt_activation_data: String,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct AccelerationControl {
    /// Indicates whether the control is engaged, represented as boolean values.
    pub brake_pedal_engaged: bool,
    pub gas_pedal_engaged: bool,
    pub emergency_brake_engaged: bool,
    pub collision_warning_engaged: bool,
    pub acc_engaged: bool,
    pub cruise_control_engaged: bool,
    pub speed_limiter_engaged: bool,
}

#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "u8", into = "u8")]
pub enum PerformanceClass {
    /// Represents an unavailable performance class.
    #[default]
    Unavailable = 0,
    /// Represents performance class A.
    A = 1,
    /// Represents performance class B.
    B = 2,
}

impl PerformanceClass {
    /// Attempts to convert a `u8` value into a `PerformanceClass` enum.
    /// Returns an error if the value is invalid.
    fn try_from(value: u8) -> Result<PerformanceClass, String> {
        match value {
            0 => Ok(PerformanceClass::Unavailable),
            1 => Ok(PerformanceClass::A),
            2 => Ok(PerformanceClass::B),
            _ => Err(format!("Invalid performance class value: {value}")),
        }
    }
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CenDSRCTollingZone {
    /// Latitude of the protected zone, represented as an integer.
    pub protected_zone_latitude: i32,
    /// Longitude of the protected zone, represented as an integer.
    pub protected_zone_longitude: i32,

    // Optional fields
    /// CEN DSRC tolling zone ID, represented as an optional unsigned 32-bit integer.
    pub cen_dsrc_tolling_zone_id: Option<u32>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExteriorLights {
    /// Indicates the status of various exterior lights, represented as boolean values.
    pub low_beam_headlights_on: bool,
    pub high_beam_headlights_on: bool,
    pub left_turn_signal_on: bool,
    pub right_turn_signal_on: bool,
    pub daytime_running_lights_on: bool,
    pub reverse_light_on: bool,
    pub fog_light_on: bool,
    pub parking_lights_on: bool,
}

impl CooperativeAwarenessMessage {
    /// Creates a message from minimal required information
    ///
    /// This function is a helper to quickly create messages, the remaining information is defaulted
    /// If you want to fill more information, you can still edit the returned struct, or you can
    /// initialize the struct directly with more information
    ///
    /// **Note: All mobility arguments have to be using SI units**
    pub fn new(
        station_id: u32,
        station_type: u8,
        position: Position,
        speed: f64,
        heading: f64,
    ) -> Self {
        CooperativeAwarenessMessage {
            station_id,
            basic_container: BasicContainer {
                station_type,
                reference_position: ReferencePosition::from(position),
            },
            high_frequency_container: HighFrequencyContainer {
                basic_vehicle_container_high_frequency: Some(BasicVehicleContainerHighFrequency {
                    heading: Heading {
                        value: heading_to_etsi(heading),
                        ..Default::default()
                    },
                    speed: Speed {
                        value: speed_to_etsi(speed),
                        ..Default::default()
                    },
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        }
    }
}

impl From<u8> for PerformanceClass {
    /// Converts a `u8` value into a `PerformanceClass` enum.
    /// Defaults to `PerformanceClass::Unavailable` if the value is invalid.
    fn from(value: u8) -> Self {
        Self::try_from(value).unwrap_or(PerformanceClass::Unavailable)
    }
}

impl From<PerformanceClass> for u8 {
    fn from(performance_class: PerformanceClass) -> Self {
        performance_class as u8
    }
}

impl Mobile for CooperativeAwarenessMessage {
    fn id(&self) -> u32 {
        self.station_id
    }

    fn position(&self) -> Position {
        self.basic_container.reference_position.as_position()
    }

    fn speed(&self) -> Option<f64> {
        self.high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .map(|vehicle| speed_from_etsi(vehicle.speed.value))
    }

    fn heading(&self) -> Option<f64> {
        self.high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .map(|vehicle| heading_from_etsi(vehicle.heading.value))
    }

    fn acceleration(&self) -> Option<f64> {
        self.high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .map(|vehicle| acceleration_from_etsi(vehicle.longitudinal_acceleration.value))
    }
}

impl Content for CooperativeAwarenessMessage {
    fn get_type(&self) -> &str {
        "cam"
    }

    fn appropriate(&mut self, timestamp: u64, new_station_id: u32) {
        self.station_id = new_station_id;
        self.generation_delta_time = timestamp_to_generation_delta_time(timestamp);
    }

    fn as_mobile(&self) -> Result<&dyn Mobile, ContentError> {
        Ok(self)
    }

    fn as_mortal(&self) -> Result<&dyn Mortal, ContentError> {
        Err(NotAMortal(type_name::<CooperativeAwarenessMessage>()))
    }
}

#[cfg(test)]
mod tests {
    use crate::exchange::etsi::acceleration::Acceleration;
    use crate::exchange::etsi::cooperative_awareness_message::{
        BasicContainer, BasicVehicleContainerHighFrequency, CooperativeAwarenessMessage,
        HighFrequencyContainer, PerformanceClass,
    };
    use crate::exchange::etsi::curvature::{Curvature, CurvatureConfidence};
    use crate::exchange::etsi::heading::Heading;
    use crate::exchange::etsi::reference_position::{Altitude, ReferencePosition};
    use crate::exchange::etsi::speed::Speed;
    use crate::exchange::etsi::vehicle_length::{TrailerPresence, VehicleLength};
    use crate::exchange::etsi::yaw_rate::YawRate;
    use crate::mobility::mobile::Mobile;

    fn minimal_cam() -> &'static str {
        r#"{
            "protocol_version": 255,
            "station_id": 4294967295,
            "generation_delta_time": 65535,
            "basic_container": {
                "station_type": 255,
                "reference_position": {
                    "latitude": 900000001,
                    "longitude": 1800000001,
                     "altitude": {
                        "value": 800001,
                        "confidence": 15
                    },
                    "position_confidence_ellipse": {
                        "semi_major": 4095,
                        "semi_minor": 0,
                        "semi_major_orientation": 3601
                    }                                        
                }
            },
            "high_frequency_container": {
                "basic_vehicle_container_high_frequency": {
                    "heading": {
                        "value": 3601,
                        "confidence": 127
                    },
                    "speed": {
                        "value": 16383,
                        "confidence": 127
                    },
                    "drive_direction": 2,
                    "vehicle_length": {
                        "value": 1023,
                        "confidence": 4
                    },
                    "vehicle_width": 62,
                    "longitudinal_acceleration": {
                        "value": 161,
                        "confidence": 102
                    },
                    "curvature": {
                        "value": 1023,
                        "confidence": 7
                    },
                    "curvature_calculation_mode": 2,
                    "yaw_rate": {
                        "value": 32767,
                        "confidence": 8
                    }                    
                }
           }
        }"#
    }

    fn standard_cam() -> &'static str {
        r#"{
            "protocol_version": 255,
            "station_id": 4294967295,
            "generation_delta_time": 65535,
            "basic_container": {
                "station_type": 255,
                "reference_position": {
                    "latitude": 900000001,
                    "longitude": 1800000001,
                    "altitude": {
                        "value": 800001,
                        "confidence": 15
                    },
                    "position_confidence_ellipse": {
                        "semi_major": 4095,
                        "semi_minor": 0,
                        "semi_major_orientation": 3601
                    }                                           
                }
            },
            "high_frequency_container": {
                "basic_vehicle_container_high_frequency": {
                    "heading": {
                        "value": 3601,
                        "confidence": 127
                    },
                    "speed": {
                        "value": 16383,
                        "confidence": 127
                    },
                    "drive_direction": 2,
                    "vehicle_length": {
                        "value": 1023,
                        "confidence": 4
                    },
                    "vehicle_width": 62,
                    "longitudinal_acceleration": {
                        "value": 161,
                        "confidence": 102
                    },
                    "curvature": {
                        "value": 1023,
                        "confidence": 7
                    },
                    "curvature_calculation_mode": 2,
                    "yaw_rate": {
                        "value": 32767,
                        "confidence": 8
                    },
                    "acceleration_control": {             
                        "brake_pedal_engaged": true,
                        "gas_pedal_engaged": true,
                        "emergency_brake_engaged": true,
                        "collision_warning_engaged": true,
                        "acc_engaged": true,
                        "cruise_control_engaged": true,
                        "speed_limiter_engaged": true
                    },
                    "lateral_acceleration": {
                        "value": -160,
                        "confidence": 0
                    }
                }
           },
            "low_frequency_container": {
                "basic_vehicle_container_low_frequency": {
                    "vehicle_role": 15,
                    "exterior_lights": {
                        "low_beam_headlights_on": true,
                        "high_beam_headlights_on": false,
                        "left_turn_signal_on": true,
                        "right_turn_signal_on": false,
                        "daytime_running_lights_on": true,
                        "reverse_light_on": false,
                        "fog_light_on": false,
                        "parking_lights_on": true
                    },
                    "path_history": []
                }
            }
        }"#
    }

    fn full_cam() -> &'static str {
        r#"{
            "protocol_version": 255,
            "station_id": 4294967295,
            "generation_delta_time": 65535,
            "basic_container": {
                "station_type": 255,
                "reference_position": {
                    "latitude": 900000001,
                    "longitude": 1800000001,
                    "altitude": {
                        "value": 800001,
                        "confidence": 15
                    },
                    "position_confidence_ellipse": {
                        "semi_major": 4095,
                        "semi_minor": 0,
                        "semi_major_orientation": 3601
                    }                                           
                }
            },
            "high_frequency_container": {
                "basic_vehicle_container_high_frequency": {
                    "heading": {
                        "value": 3601,
                        "confidence": 127
                    },
                    "speed": {
                        "value": 16383,
                        "confidence": 127
                    },
                    "drive_direction": 2,
                    "vehicle_length": {
                        "value": 1023,
                        "confidence": 4
                    },
                    "vehicle_width": 62,
                    "longitudinal_acceleration": {
                        "value": 161,
                        "confidence": 102
                    },
                    "curvature": {
                        "value": 1023,
                        "confidence": 7
                    },
                    "curvature_calculation_mode": 2,
                    "yaw_rate": {
                        "value": 32767,
                        "confidence": 8
                    },
                    "acceleration_control": {             
                        "brake_pedal_engaged": true,
                        "gas_pedal_engaged": true,
                        "emergency_brake_engaged": true,
                        "collision_warning_engaged": true,
                        "acc_engaged": true,
                        "cruise_control_engaged": true,
                        "speed_limiter_engaged": true
                    },
                    "lane_position": 14,
                    "steering_wheel_angle": {
                        "value": 512,
                        "confidence": 127
                    },
                    "lateral_acceleration": {
                        "value": -160,
                        "confidence": 0
                    },
                    "vertical_acceleration": {
                        "value": 0,
                        "confidence": 63
                    },
                    "performance_class": 2,
                    "cen_dsrc_tolling_zone": {
                        "protected_zone_latitude": 900000001,
                        "protected_zone_longitude": 1800000001,
                        "cen_dsrc_tolling_zone_id": 134217727
                    }
                }
           },
            "low_frequency_container": {
                "basic_vehicle_container_low_frequency": {
                    "vehicle_role": 15,
                    "exterior_lights": {
                        "low_beam_headlights_on": true,
                        "high_beam_headlights_on": false,
                        "left_turn_signal_on": true,
                        "right_turn_signal_on": false,
                        "daytime_running_lights_on": true,
                        "reverse_light_on": false,
                        "fog_light_on": false,
                        "parking_lights_on": true
                    },
                    "path_history": [
                        {
                            "path_position": {
                                "delta_latitude": 131072,
                                "delta_longitude": -131071,
                                "delta_altitude": 12800
                            },
                            "path_delta_time": 65535
                        },
                        {
                            "path_position": {
                                "delta_latitude": -131071,
                                "delta_longitude": 131072,
                                "delta_altitude": -12700
                            },
                            "path_delta_time": 1
                        }
                    ]
                }
            },
            "special_vehicle_container": {
                "public_transport_container": {
                    "embarkation_status": true,
                    "pt_activation": {
                        "pt_activation_type": 255,
                        "pt_activation_data": "abcdefghijklmnopqrst"
                    }
                }
            }
        }"#
    }

    #[test]
    fn test_deserialize_minimal_cam() {
        let data = minimal_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        assert_eq!(cam.protocol_version, 255);
        assert_eq!(cam.station_id, 4294967295);
        assert_eq!(cam.generation_delta_time, 65535);
        assert_eq!(cam.basic_container.station_type, 255);
        assert_eq!(cam.basic_container.reference_position.latitude, 900000001);
        assert_eq!(cam.basic_container.reference_position.longitude, 1800000001);
        assert_eq!(
            cam.basic_container.reference_position.altitude.value,
            800001
        );
        assert_eq!(
            cam.basic_container.reference_position.altitude.confidence,
            15
        );
        let basic_vehicle_container_high_frequency = cam
            .high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .unwrap();
        assert_eq!(basic_vehicle_container_high_frequency.heading.value, 3601);
        assert_eq!(
            basic_vehicle_container_high_frequency.heading.confidence,
            127
        );
        assert_eq!(basic_vehicle_container_high_frequency.speed.value, 16383);
        assert_eq!(basic_vehicle_container_high_frequency.speed.confidence, 127);
        assert_eq!(basic_vehicle_container_high_frequency.drive_direction, 2);
        assert_eq!(
            basic_vehicle_container_high_frequency.vehicle_length.value,
            1023
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .vehicle_length
                .confidence,
            TrailerPresence::Unavailable
        );
        assert_eq!(basic_vehicle_container_high_frequency.vehicle_width, 62);
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .value,
            161
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .confidence,
            102
        );
        assert_eq!(basic_vehicle_container_high_frequency.curvature.value, 1023);
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature.confidence,
            CurvatureConfidence::Unavailable
        );
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature_calculation_mode,
            2
        );
        assert_eq!(basic_vehicle_container_high_frequency.yaw_rate.value, 32767);
        assert_eq!(
            basic_vehicle_container_high_frequency.yaw_rate.confidence,
            8
        );
    }

    #[test]
    fn test_deserialize_standard_cam() {
        let data = standard_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        assert_eq!(cam.protocol_version, 255);
        assert_eq!(cam.station_id, 4294967295);
        assert_eq!(cam.generation_delta_time, 65535);
        assert_eq!(cam.basic_container.station_type, 255);
        assert_eq!(cam.basic_container.reference_position.latitude, 900000001);
        assert_eq!(cam.basic_container.reference_position.longitude, 1800000001);
        assert_eq!(
            cam.basic_container.reference_position.altitude.value,
            800001
        );
        assert_eq!(
            cam.basic_container.reference_position.altitude.confidence,
            15
        );
        let basic_vehicle_container_high_frequency = cam
            .high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .unwrap();
        assert_eq!(basic_vehicle_container_high_frequency.heading.value, 3601);
        assert_eq!(
            basic_vehicle_container_high_frequency.heading.confidence,
            127
        );
        assert_eq!(basic_vehicle_container_high_frequency.speed.value, 16383);
        assert_eq!(basic_vehicle_container_high_frequency.speed.confidence, 127);
        assert_eq!(basic_vehicle_container_high_frequency.drive_direction, 2);
        assert_eq!(
            basic_vehicle_container_high_frequency.vehicle_length.value,
            1023
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .vehicle_length
                .confidence,
            TrailerPresence::Unavailable
        );
        assert_eq!(basic_vehicle_container_high_frequency.vehicle_width, 62);
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .value,
            161
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .confidence,
            102
        );
        assert_eq!(basic_vehicle_container_high_frequency.curvature.value, 1023);
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature.confidence,
            CurvatureConfidence::Unavailable
        );
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature_calculation_mode,
            2
        );
        assert_eq!(basic_vehicle_container_high_frequency.yaw_rate.value, 32767);
        assert_eq!(
            basic_vehicle_container_high_frequency.yaw_rate.confidence,
            8
        );
        assert!(
            basic_vehicle_container_high_frequency
                .acceleration_control
                .is_some()
        );
        let acceleration_control = basic_vehicle_container_high_frequency
            .acceleration_control
            .as_ref()
            .unwrap();
        assert!(acceleration_control.brake_pedal_engaged);
        assert!(acceleration_control.gas_pedal_engaged);
        assert!(acceleration_control.emergency_brake_engaged);
        assert!(acceleration_control.collision_warning_engaged);
        assert!(acceleration_control.acc_engaged);
        assert!(acceleration_control.cruise_control_engaged);
        assert!(acceleration_control.speed_limiter_engaged);
        assert!(
            basic_vehicle_container_high_frequency
                .lateral_acceleration
                .is_some()
        );
        let lateral_acceleration = basic_vehicle_container_high_frequency
            .lateral_acceleration
            .as_ref()
            .unwrap();
        assert_eq!(lateral_acceleration.value, -160);
        assert_eq!(lateral_acceleration.confidence, 0);
        assert!(cam.low_frequency_container.is_some());
        let basic_vehicle_container_low_frequency = cam
            .low_frequency_container
            .as_ref()
            .unwrap()
            .basic_vehicle_container_low_frequency
            .as_ref()
            .unwrap();
        assert_eq!(basic_vehicle_container_low_frequency.vehicle_role, 15);
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .low_beam_headlights_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .high_beam_headlights_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .left_turn_signal_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .right_turn_signal_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .daytime_running_lights_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .reverse_light_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .fog_light_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .parking_lights_on
        );
    }

    #[test]
    fn test_deserialize_full_cam() {
        let data = full_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        assert_eq!(cam.protocol_version, 255);
        assert_eq!(cam.station_id, 4294967295);
        assert_eq!(cam.generation_delta_time, 65535);
        assert_eq!(cam.basic_container.station_type, 255);
        assert_eq!(cam.basic_container.reference_position.latitude, 900000001);
        assert_eq!(cam.basic_container.reference_position.longitude, 1800000001);
        assert_eq!(
            cam.basic_container.reference_position.altitude.value,
            800001
        );
        assert_eq!(
            cam.basic_container.reference_position.altitude.confidence,
            15
        );
        let basic_vehicle_container_high_frequency = cam
            .high_frequency_container
            .basic_vehicle_container_high_frequency
            .as_ref()
            .unwrap();
        assert_eq!(basic_vehicle_container_high_frequency.heading.value, 3601);
        assert_eq!(
            basic_vehicle_container_high_frequency.heading.confidence,
            127
        );
        assert_eq!(basic_vehicle_container_high_frequency.speed.value, 16383);
        assert_eq!(basic_vehicle_container_high_frequency.speed.confidence, 127);
        assert_eq!(basic_vehicle_container_high_frequency.drive_direction, 2);
        assert_eq!(
            basic_vehicle_container_high_frequency.vehicle_length.value,
            1023
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .vehicle_length
                .confidence,
            TrailerPresence::Unavailable
        );
        assert_eq!(basic_vehicle_container_high_frequency.vehicle_width, 62);
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .value,
            161
        );
        assert_eq!(
            basic_vehicle_container_high_frequency
                .longitudinal_acceleration
                .confidence,
            102
        );
        assert_eq!(basic_vehicle_container_high_frequency.curvature.value, 1023);
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature.confidence,
            CurvatureConfidence::Unavailable
        );
        assert_eq!(
            basic_vehicle_container_high_frequency.curvature_calculation_mode,
            2
        );
        assert_eq!(basic_vehicle_container_high_frequency.yaw_rate.value, 32767);
        assert_eq!(
            basic_vehicle_container_high_frequency.yaw_rate.confidence,
            8
        );
        assert!(
            basic_vehicle_container_high_frequency
                .acceleration_control
                .is_some()
        );
        let acceleration_control = basic_vehicle_container_high_frequency
            .acceleration_control
            .as_ref()
            .unwrap();
        assert!(acceleration_control.brake_pedal_engaged);
        assert!(acceleration_control.gas_pedal_engaged);
        assert!(acceleration_control.emergency_brake_engaged);
        assert!(acceleration_control.collision_warning_engaged);
        assert!(acceleration_control.acc_engaged);
        assert!(acceleration_control.cruise_control_engaged);
        assert!(acceleration_control.speed_limiter_engaged);
        assert!(
            basic_vehicle_container_high_frequency
                .lane_position
                .is_some()
        );
        let lane_position = basic_vehicle_container_high_frequency
            .lane_position
            .unwrap();
        assert_eq!(lane_position, 14);
        assert!(
            basic_vehicle_container_high_frequency
                .steering_wheel_angle
                .is_some()
        );
        let steering_wheel_angle = basic_vehicle_container_high_frequency
            .steering_wheel_angle
            .as_ref()
            .unwrap();
        assert_eq!(steering_wheel_angle.value, 512);
        assert_eq!(steering_wheel_angle.confidence, 127);
        assert!(
            basic_vehicle_container_high_frequency
                .lateral_acceleration
                .is_some()
        );
        let lateral_acceleration = basic_vehicle_container_high_frequency
            .lateral_acceleration
            .as_ref()
            .unwrap();
        assert_eq!(lateral_acceleration.value, -160);
        assert_eq!(lateral_acceleration.confidence, 0);
        assert!(
            basic_vehicle_container_high_frequency
                .vertical_acceleration
                .is_some()
        );
        let vertical_acceleration = basic_vehicle_container_high_frequency
            .vertical_acceleration
            .as_ref()
            .unwrap();
        assert_eq!(vertical_acceleration.value, 0);
        assert_eq!(vertical_acceleration.confidence, 63);
        assert!(
            basic_vehicle_container_high_frequency
                .performance_class
                .is_some()
        );
        let performance_class = basic_vehicle_container_high_frequency
            .performance_class
            .as_ref()
            .unwrap();
        assert_eq!(*performance_class, PerformanceClass::B);
        assert!(
            basic_vehicle_container_high_frequency
                .cen_dsrc_tolling_zone
                .is_some()
        );
        let cen_dsrc_tolling_zone = basic_vehicle_container_high_frequency
            .cen_dsrc_tolling_zone
            .as_ref()
            .unwrap();
        assert_eq!(cen_dsrc_tolling_zone.protected_zone_latitude, 900000001);
        assert_eq!(cen_dsrc_tolling_zone.protected_zone_longitude, 1800000001);
        assert_eq!(
            cen_dsrc_tolling_zone.cen_dsrc_tolling_zone_id,
            Some(134217727)
        );
        assert!(cam.low_frequency_container.is_some());
        let basic_vehicle_container_low_frequency = cam
            .low_frequency_container
            .as_ref()
            .unwrap()
            .basic_vehicle_container_low_frequency
            .as_ref()
            .unwrap();
        assert_eq!(basic_vehicle_container_low_frequency.vehicle_role, 15);
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .low_beam_headlights_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .high_beam_headlights_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .left_turn_signal_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .right_turn_signal_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .daytime_running_lights_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .reverse_light_on
        );
        assert!(
            !basic_vehicle_container_low_frequency
                .exterior_lights
                .fog_light_on
        );
        assert!(
            basic_vehicle_container_low_frequency
                .exterior_lights
                .parking_lights_on
        );
        assert_eq!(basic_vehicle_container_low_frequency.path_history.len(), 2);
        let path_point_1 = &basic_vehicle_container_low_frequency.path_history[0];
        assert_eq!(path_point_1.path_position.delta_latitude, 131072);
        assert_eq!(path_point_1.path_position.delta_longitude, -131071);
        assert_eq!(path_point_1.path_position.delta_altitude, 12800);
        assert!(path_point_1.path_delta_time.is_some());
        assert_eq!(path_point_1.path_delta_time.unwrap(), 65535);
        let path_point_2 = &basic_vehicle_container_low_frequency.path_history[1];
        assert_eq!(path_point_2.path_position.delta_latitude, -131071);
        assert_eq!(path_point_2.path_position.delta_longitude, 131072);
        assert_eq!(path_point_2.path_position.delta_altitude, -12700);
        assert!(path_point_2.path_delta_time.is_some());
        assert_eq!(path_point_2.path_delta_time.unwrap(), 1);
        assert!(cam.special_vehicle_container.is_some());
        let special_vehicle_container = cam.special_vehicle_container.as_ref().unwrap();
        assert!(
            special_vehicle_container
                .public_transport_container
                .is_some()
        );
        let public_transport_container = special_vehicle_container
            .public_transport_container
            .as_ref()
            .unwrap();
        assert!(public_transport_container.embarkation_status);
        assert!(public_transport_container.pt_activation.is_some());
        let pt_activation = public_transport_container.pt_activation.as_ref().unwrap();
        assert_eq!(pt_activation.pt_activation_type, 255);
        assert_eq!(pt_activation.pt_activation_data, "abcdefghijklmnopqrst");
    }

    #[test]
    fn test_reserialize_minimal_cam() {
        let data = minimal_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        let serialized = serde_json::to_string(&cam).unwrap();
        assert_eq!(
            serialized,
            data.replace("\n", "").replace(" ", "").replace("\t", "")
        );
    }

    #[test]
    fn test_reserialize_standard_cam() {
        let data = standard_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        let serialized = serde_json::to_string(&cam).unwrap();
        assert_eq!(
            serialized,
            data.replace("\n", "").replace(" ", "").replace("\t", "")
        );
    }

    #[test]
    fn test_reserialize_full_cam() {
        let data = full_cam();

        let cam = serde_json::from_str::<CooperativeAwarenessMessage>(data).unwrap();
        let serialized = serde_json::to_string(&cam).unwrap();
        assert_eq!(
            serialized,
            data.replace("\n", "").replace(" ", "").replace("\t", "")
        );
    }

    #[test]
    fn test_cooperative_awareness_message_as_position() {
        let reference_position = ReferencePosition {
            latitude: 123456789,
            longitude: 987654321,
            altitude: Altitude {
                value: 1000,
                confidence: Default::default(),
            },
            position_confidence_ellipse: Default::default(),
        };
        let cam = CooperativeAwarenessMessage {
            protocol_version: 1,
            station_id: 12345,
            generation_delta_time: 100,
            basic_container: BasicContainer {
                station_type: 1,
                reference_position,
            },
            high_frequency_container: HighFrequencyContainer {
                basic_vehicle_container_high_frequency: Some(BasicVehicleContainerHighFrequency {
                    heading: Heading {
                        value: 1800,
                        confidence: Default::default(),
                    },
                    speed: Speed {
                        value: 500,
                        confidence: Default::default(),
                    },
                    drive_direction: 0,
                    vehicle_length: VehicleLength {
                        value: 500,
                        confidence: Default::default(),
                    },
                    vehicle_width: 200,
                    longitudinal_acceleration: Acceleration {
                        value: 100,
                        confidence: Default::default(),
                    },
                    curvature: Curvature {
                        value: 50,
                        confidence: Default::default(),
                    },
                    curvature_calculation_mode: 0,
                    yaw_rate: YawRate {
                        value: 10,
                        confidence: Default::default(),
                    },
                    acceleration_control: None,
                    lane_position: None,
                    steering_wheel_angle: None,
                    lateral_acceleration: None,
                    vertical_acceleration: None,
                    performance_class: None,
                    cen_dsrc_tolling_zone: None,
                }),
                rsu_container_high_frequency: None,
            },
            low_frequency_container: None,
            special_vehicle_container: None,
        };

        let position = cam.position();
        assert_eq!(position.latitude, 12.3456789_f64.to_radians());
        assert_eq!(position.longitude, 98.7654321_f64.to_radians());
        assert_eq!(position.altitude, 10.00);
    }

    #[test]
    fn test_cam_defaults() {
        let cam = CooperativeAwarenessMessage::default();
        assert_eq!(cam.protocol_version, 0);
        assert_eq!(cam.station_id, 0);
        assert_eq!(cam.generation_delta_time, 0);
        assert!(cam.low_frequency_container.is_none());
        assert!(cam.special_vehicle_container.is_none());
    }
}