1use crate::records::{EntityType, ExerciseIdentifier, Timestamp};
8use crate::siso::enums::{
9 DISLiveEntitySubprotocolNumber, DeadReckoningAlgorithm, MunitionDescriptorFuse,
10 MunitionDescriptorWarhead, PduType, ProtocolFamily, ProtocolVersion,
11};
12
13macro_rules! fixed_binary {
14 ($name:ident, $raw:ty, $bits:literal) => {
15 #[doc = concat!(
16 "A ", stringify!($bits), "-bit signed fixed binary number with `FRAC` fractional bits.\n\n",
17 "On the wire it's a two's complement `", stringify!($raw), "`; its value is that integer ",
18 "divided by 2^`FRAC`. Each Live Entity field defines its own `FRAC` and units, so use the ",
19 "type alias for the field rather than choosing `FRAC` yourself."
20 )]
21 #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash, deku::DekuRead, deku::DekuWrite)]
22 #[deku(
23 endian = "endian",
24 ctx = "endian: deku::ctx::Endian",
25 ctx_default = "deku::ctx::Endian::Big"
26 )]
27 pub struct $name<const FRAC: u32> {
28 raw: $raw,
29 }
30
31 impl<const FRAC: u32> $name<FRAC> {
32 const VALID: () = assert!(FRAC < $bits, "FRAC must be less than the number of bits");
34
35 #[allow(clippy::as_conversions, reason = "u32 to f64 is exact, and `From` isn't const")]
36 const SCALE: f64 = (1u32 << FRAC) as f64;
37
38 pub const RESOLUTION: f64 = 1.0 / Self::SCALE;
40
41 #[allow(clippy::as_conversions, reason = "integer to f64 is exact, and `From` isn't const")]
43 pub const MIN: f64 = <$raw>::MIN as f64 / Self::SCALE;
44
45 #[allow(clippy::as_conversions, reason = "integer to f64 is exact, and `From` isn't const")]
47 pub const MAX: f64 = <$raw>::MAX as f64 / Self::SCALE;
48
49 #[must_use]
51 pub fn new(value: f64) -> Option<Self> {
52 let () = Self::VALID;
53 let scaled = (value * Self::SCALE).round();
54 (f64::from(<$raw>::MIN)..=f64::from(<$raw>::MAX))
55 .contains(&scaled)
56 .then(|| Self::from_scaled(scaled))
57 }
58
59 #[must_use]
62 pub fn saturating(value: f64) -> Self {
63 let () = Self::VALID;
64 Self::from_scaled((value * Self::SCALE).round())
65 }
66
67 #[must_use]
69 pub fn to_f64(self) -> f64 {
70 f64::from(self.raw) / Self::SCALE
71 }
72
73 #[must_use]
75 pub const fn from_raw(raw: $raw) -> Self {
76 Self { raw }
77 }
78
79 #[must_use]
81 pub const fn to_raw(self) -> $raw {
82 self.raw
83 }
84
85 #[allow(
86 clippy::as_conversions,
87 clippy::cast_possible_truncation,
88 reason = "float-to-integer `as` saturates and maps NaN to 0, which is the behavior wanted"
89 )]
90 const fn from_scaled(scaled: f64) -> Self {
91 Self { raw: scaled as $raw }
92 }
93 }
94 };
95}
96
97fixed_binary!(FixedBinary8, i8, 8);
98fixed_binary!(FixedBinary16, i16, 16);
99
100#[derive(Copy, Clone, Debug, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
103#[deku(
104 endian = "endian",
105 ctx = "endian: deku::ctx::Endian",
106 ctx_default = "deku::ctx::Endian::Big"
107)]
108pub struct LiveEntityPduHeader {
109 pub protocol_version: ProtocolVersion,
111 pub exercise_id: ExerciseIdentifier,
113 pub(crate) pdu_type: PduType,
116 pub(crate) protocol_family: ProtocolFamily,
119 pub timestamp: Timestamp,
121 pub(crate) length: u16,
124 pub subprotocol_number: DISLiveEntitySubprotocolNumber,
126 padding: u8,
128}
129
130impl Default for LiveEntityPduHeader {
131 fn default() -> Self {
132 Self {
133 protocol_version: ProtocolVersion::IEEE1278_1_2012,
134 exercise_id: ExerciseIdentifier::new(1),
135 pdu_type: PduType::default(),
136 protocol_family: ProtocolFamily::default(),
137 timestamp: Timestamp::now(),
138 length: 0,
139 subprotocol_number: DISLiveEntitySubprotocolNumber::default(),
140 padding: 0,
141 }
142 }
143}
144
145impl LiveEntityPduHeader {
146 #[must_use]
148 pub const fn pdu_type(&self) -> PduType {
149 self.pdu_type
150 }
151
152 #[must_use]
154 pub const fn protocol_family(&self) -> ProtocolFamily {
155 self.protocol_family
156 }
157
158 #[must_use]
160 pub const fn length(&self) -> u16 {
161 self.length
162 }
163
164 pub fn serialize(&self, buf: &mut Vec<u8>) {
166 let _ = crate::wire::write(self, buf);
167 }
168
169 pub fn deserialize(buf: &mut &[u8]) -> Result<Self, crate::DISError> {
177 crate::wire::read(buf).map_err(crate::DISError::from_wire)
178 }
179}
180
181impl crate::pdu::DerivedFields for LiveEntityPduHeader {
182 #[cfg(test)]
183 fn length(&self) -> u16 {
184 self.length
185 }
186
187 fn set_pdu_type(&mut self, value: PduType) {
188 self.pdu_type = value;
189 }
190
191 fn set_protocol_family(&mut self, value: ProtocolFamily) {
192 self.protocol_family = value;
193 }
194
195 fn set_length(&mut self, value: u16) {
196 self.length = value;
197 }
198}
199
200#[derive(Copy, Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
202#[deku(
203 endian = "endian",
204 ctx = "endian: deku::ctx::Endian",
205 ctx_default = "deku::ctx::Endian::Big"
206)]
207pub struct LEDeadReckoningParameters {
208 pub dead_reckoning_algorithm: DeadReckoningAlgorithm,
209 pub entity_linear_acceleration: LEEntityLinearAcceleration,
210 pub entity_angular_velocity: LEEntityAngularVelocity,
211}
212
213impl LEDeadReckoningParameters {
214 #[must_use]
215 pub const fn new(
216 dead_reckoning_algorithm: DeadReckoningAlgorithm,
217 entity_linear_acceleration: LEEntityLinearAcceleration,
218 entity_angular_velocity: LEEntityAngularVelocity,
219 ) -> Self {
220 Self {
221 dead_reckoning_algorithm,
222 entity_linear_acceleration,
223 entity_angular_velocity,
224 }
225 }
226}
227
228pub type LEAngularVelocityComponent = FixedBinary8<0>;
230
231#[derive(Copy, Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
232#[deku(
233 endian = "endian",
234 ctx = "endian: deku::ctx::Endian",
235 ctx_default = "deku::ctx::Endian::Big"
236)]
237pub struct LEEntityAngularVelocity {
240 pub rate_about_x_axis: LEAngularVelocityComponent,
242 pub rate_about_y_axis: LEAngularVelocityComponent,
244 pub rate_about_z_axis: LEAngularVelocityComponent,
246}
247
248impl LEEntityAngularVelocity {
249 #[must_use]
251 pub fn new() -> Self {
252 Self::default()
253 }
254}
255
256#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
257#[deku(
258 endian = "endian",
259 ctx = "endian: deku::ctx::Endian",
260 ctx_default = "deku::ctx::Endian::Big"
261)]
262pub struct LEEntityId {
264 pub site_number: u8,
266 pub application_number: u8,
268 pub entity_number: u16,
270}
271
272impl LEEntityId {
273 #[must_use]
274 pub const fn new(site_number: u8, application_number: u8, entity_number: u16) -> Self {
275 Self {
276 site_number,
277 application_number,
278 entity_number,
279 }
280 }
281}
282
283pub type LELinearAccelerationComponent = FixedBinary8<0>;
285
286#[derive(Copy, Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
288#[deku(
289 endian = "endian",
290 ctx = "endian: deku::ctx::Endian",
291 ctx_default = "deku::ctx::Endian::Big"
292)]
293pub struct LEEntityLinearAcceleration {
294 pub first_vector_component: LELinearAccelerationComponent,
295 pub second_vector_component: LELinearAccelerationComponent,
296 pub third_vector_component: LELinearAccelerationComponent,
297}
298
299impl LEEntityLinearAcceleration {
300 #[must_use]
301 pub const fn new(
302 x: LELinearAccelerationComponent,
303 y: LELinearAccelerationComponent,
304 z: LELinearAccelerationComponent,
305 ) -> Self {
306 Self {
307 first_vector_component: x,
308 second_vector_component: y,
309 third_vector_component: z,
310 }
311 }
312}
313
314pub type LEEulerAnglesComponent = FixedBinary8<0>;
316
317#[derive(Copy, Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
319#[deku(
320 endian = "endian",
321 ctx = "endian: deku::ctx::Endian",
322 ctx_default = "deku::ctx::Endian::Big"
323)]
324pub struct LEEulerAngles {
325 pub psi: LEEulerAnglesComponent,
327 pub theta: LEEulerAnglesComponent,
329 pub phi: LEEulerAnglesComponent,
331}
332
333impl LEEulerAngles {
334 #[must_use]
335 #[allow(clippy::similar_names)]
336 pub const fn new(
337 psi: LEEulerAnglesComponent,
338 theta: LEEulerAnglesComponent,
339 phi: LEEulerAnglesComponent,
340 ) -> Self {
341 Self { psi, theta, phi }
342 }
343}
344
345#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
348#[deku(
349 endian = "endian",
350 ctx = "endian: deku::ctx::Endian",
351 ctx_default = "deku::ctx::Endian::Big"
352)]
353pub struct LiveEventIdentifier {
354 pub site_number: u8,
355 pub application_number: u8,
356 pub event_number: u16,
357}
358
359impl LiveEventIdentifier {
360 #[must_use]
361 pub const fn new(site_number: u8, application_number: u8, event_number: u16) -> Self {
362 Self {
363 site_number,
364 application_number,
365 event_number,
366 }
367 }
368}
369
370pub type LELinearVelocityComponent = FixedBinary16<0>;
372
373#[derive(Copy, Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
375#[deku(
376 endian = "endian",
377 ctx = "endian: deku::ctx::Endian",
378 ctx_default = "deku::ctx::Endian::Big"
379)]
380pub struct LELinearVelocity {
381 pub x_component: LELinearVelocityComponent,
382 pub y_component: LELinearVelocityComponent,
383 pub z_component: LELinearVelocityComponent,
384}
385
386impl LELinearVelocity {
387 #[must_use]
388 pub const fn new(
389 x_component: LELinearVelocityComponent,
390 y_component: LELinearVelocityComponent,
391 z_component: LELinearVelocityComponent,
392 ) -> Self {
393 Self {
394 x_component,
395 y_component,
396 z_component,
397 }
398 }
399}
400
401#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
403#[deku(
404 endian = "endian",
405 ctx = "endian: deku::ctx::Endian",
406 ctx_default = "deku::ctx::Endian::Big"
407)]
408pub struct LiveEntityOrientation16 {
409 pub psi: i16,
410 pub theta: i16,
411 pub phi: i16,
412}
413
414impl LiveEntityOrientation16 {
415 #[must_use]
416 pub const fn new(psi: i16, theta: i16, phi: i16) -> Self {
417 Self { psi, theta, phi }
418 }
419}
420
421pub type OrientationErrorComponent = FixedBinary16<8>;
423
424#[derive(Debug, Clone, Copy, Default, deku::DekuRead, deku::DekuWrite)]
426#[deku(
427 endian = "endian",
428 ctx = "endian: deku::ctx::Endian",
429 ctx_default = "deku::ctx::Endian::Big"
430)]
431pub struct OrientationError {
432 pub azimuth_error: OrientationErrorComponent,
433 pub elevation_error: OrientationErrorComponent,
434 pub rotation_error: OrientationErrorComponent,
435}
436
437impl OrientationError {
438 #[must_use]
439 pub const fn new(
440 azimuth_error: OrientationErrorComponent,
441 elevation_error: OrientationErrorComponent,
442 rotation_error: OrientationErrorComponent,
443 ) -> Self {
444 Self {
445 azimuth_error,
446 elevation_error,
447 rotation_error,
448 }
449 }
450}
451
452pub type PositionErrorComponent = FixedBinary16<3>;
454
455#[derive(Debug, Clone, Copy, Default, deku::DekuRead, deku::DekuWrite)]
457#[deku(
458 endian = "endian",
459 ctx = "endian: deku::ctx::Endian",
460 ctx_default = "deku::ctx::Endian::Big"
461)]
462pub struct PositionError {
463 pub horizontal_error: PositionErrorComponent,
464 pub vertical_error: PositionErrorComponent,
465}
466
467impl PositionError {
468 #[must_use]
469 pub const fn new(
470 horizontal_error: PositionErrorComponent,
471 vertical_error: PositionErrorComponent,
472 ) -> Self {
473 Self {
474 horizontal_error,
475 vertical_error,
476 }
477 }
478}
479
480pub type RelativeWorldCoordinatesComponent = FixedBinary16<3>;
482
483#[derive(Clone, Debug, Default, deku::DekuRead, deku::DekuWrite)]
485#[deku(
486 endian = "endian",
487 ctx = "endian: deku::ctx::Endian",
488 ctx_default = "deku::ctx::Endian::Big"
489)]
490pub struct RelativeWorldCoordinates {
491 pub reference_point: u16,
492 pub delta_x: RelativeWorldCoordinatesComponent,
493 pub delta_y: RelativeWorldCoordinatesComponent,
494 pub delta_z: RelativeWorldCoordinatesComponent,
495}
496
497impl RelativeWorldCoordinates {
498 #[must_use]
499 pub const fn new(
500 reference_point: u16,
501 delta_x: RelativeWorldCoordinatesComponent,
502 delta_y: RelativeWorldCoordinatesComponent,
503 delta_z: RelativeWorldCoordinatesComponent,
504 ) -> Self {
505 Self {
506 reference_point,
507 delta_x,
508 delta_y,
509 delta_z,
510 }
511 }
512}
513
514#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
516#[deku(
517 endian = "endian",
518 ctx = "endian: deku::ctx::Endian",
519 ctx_default = "deku::ctx::Endian::Big"
520)]
521pub struct LiveEntityRelativeLocation {
522 pub x: i16,
523 pub y: i16,
524 pub z: i16,
525}
526
527#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
535#[deku(
536 endian = "endian",
537 ctx = "endian: deku::ctx::Endian, warhead_and_fuse_included: bool, quantity_and_rate_included: bool"
538)]
539pub struct LiveMunitionDescriptor {
540 pub munition_type: EntityType,
541 #[deku(cond = "warhead_and_fuse_included")]
543 pub warhead_and_fuse: Option<WarheadAndFuse>,
544 #[deku(cond = "quantity_and_rate_included")]
547 pub quantity_and_rate: Option<QuantityAndRate>,
548}
549
550#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
552#[deku(
553 endian = "endian",
554 ctx = "endian: deku::ctx::Endian",
555 ctx_default = "deku::ctx::Endian::Big"
556)]
557pub struct WarheadAndFuse {
558 pub warhead: MunitionDescriptorWarhead,
559 pub fuse: MunitionDescriptorFuse,
560}
561
562#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, deku::DekuRead, deku::DekuWrite)]
564#[deku(
565 endian = "endian",
566 ctx = "endian: deku::ctx::Endian",
567 ctx_default = "deku::ctx::Endian::Big"
568)]
569pub struct QuantityAndRate {
570 pub quantity: u16,
571 pub rate: u16,
572}
573
574#[cfg(test)]
575#[allow(
576 clippy::float_cmp,
577 reason = "every value compared here is exactly representable"
578)]
579mod tests {
580 use super::*;
581
582 type Meters = FixedBinary16<3>;
583 type Fine = FixedBinary8<5>;
584
585 #[test]
586 fn converts_with_the_binary_point_from_the_type() {
587 assert_eq!(Meters::new(1.25).unwrap().to_raw(), 10);
588 assert_eq!(Meters::from_raw(10).to_f64(), 1.25);
589 assert_eq!(Fine::new(1.25).unwrap().to_raw(), 40);
590 assert_eq!(Fine::RESOLUTION, 1.0 / 32.0);
591 }
592
593 #[test]
594 fn rounds_to_the_nearest_step() {
595 assert_eq!(Meters::new(1.3).unwrap().to_f64(), 1.25);
596 assert_eq!(Meters::new(1.32).unwrap().to_f64(), 1.375);
597 assert_eq!(Meters::new(-0.0625).unwrap().to_f64(), -0.125); }
599
600 #[test]
601 fn range_matches_the_integer_range() {
602 assert_eq!(Meters::MAX, 4095.875);
603 assert_eq!(Meters::MIN, -4096.0);
604 assert_eq!(Fine::MAX, 127.0 / 32.0);
605 assert!(Meters::new(Meters::MAX).is_some());
606 assert!(Meters::new(4096.0).is_none());
607 assert!(Meters::new(f64::NAN).is_none());
608 assert!(Meters::new(f64::INFINITY).is_none());
609 }
610
611 #[test]
612 fn saturating_clamps_and_maps_nan_to_zero() {
613 assert_eq!(Meters::saturating(1e9).to_f64(), Meters::MAX);
614 assert_eq!(Meters::saturating(-1e9).to_f64(), Meters::MIN);
615 assert_eq!(Meters::saturating(f64::NAN).to_raw(), 0);
616 }
617
618 #[test]
619 fn encodes_as_the_big_endian_integer() {
620 let mut buf = Vec::new();
621 crate::wire::write(&Meters::from_raw(-2), &mut buf).unwrap();
622 assert_eq!(buf, [0xFF, 0xFE]);
623 let back: Meters = crate::wire::read(&mut buf.as_slice()).unwrap();
624 assert_eq!(back.to_f64(), -0.25);
625 }
626}