rs1090 0.5.2

Rust library to decode Mode S and ADS-B signals
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
use deku::prelude::*;
use serde::{Deserialize, Serialize};

/**
 * ## Heading and Speed Report (BDS 6,0)
 *
 * Comm-B message providing aircraft heading and speed data.
 * Per ICAO Doc 9871 Table A-2-96: BDS code 6,0 — Heading and speed report
 *
 * Purpose: Provides heading and speed data to ground systems for improved
 * trajectory prediction and conflict detection.
 *
 * Message Structure (56 bits):
 * | HDG   | IAS  | MACH | BARO | INER |
 * |-------|------|------|------|------|
 * | 1+1+10| 1+10 | 1+10 | 1+1+9| 1+1+9|
 *
 * Field Encoding per ICAO Doc 9871:
 *
 * **Magnetic Heading** (bits 1-12):
 *   - Bit 1: Status (0=invalid, 1=valid)
 *   - Bit 2: Sign (0=east, 1=west)
 *   - Bits 3-12: 10-bit heading magnitude
 *     * MSB = 90 degrees
 *     * LSB = 90/512 degrees (≈0.1758°)
 *     * Range: [-180, +180] degrees (two's complement)
 *     * Converted to [0, 360]° for display (e.g., 315° = -45°)
 *
 * **Indicated Airspeed (IAS)** (bits 13-23):
 *   - Bit 13: Status (0=invalid, 1=valid)
 *   - Bits 14-23: 10-bit IAS value
 *     * MSB = 512 kt
 *     * LSB = 1 kt
 *     * Range: [0, 1023] kt
 *     * Formula: IAS = value × 1 kt
 *
 * **Mach Number** (bits 24-34):
 *   - Bit 24: Status (0=invalid, 1=valid)
 *   - Bits 25-34: 10-bit Mach value
 *     * MSB = 2.048 Mach
 *     * LSB = 2.048/512 Mach (≈0.004 Mach)
 *     * Range: [0, 4.092] Mach
 *     * Formula: Mach = value × (2.048/512)
 *
 * **Barometric Altitude Rate** (bits 35-45):
 *   - Bit 35: Status (0=invalid, 1=valid)
 *   - Bit 36: Sign (0=climbing, 1=descending/"below")
 *   - Bits 37-45: 9-bit vertical rate value
 *     * MSB = 8,192 ft/min
 *     * LSB = 32 ft/min (8192/256)
 *     * Range: [-16,384, +16,352] ft/min
 *     * Formula: rate = value × 32 ft/min (two's complement)
 *     * Special values: 0 or 511 = no rate information (returns 0)
 *
 * **Inertial Vertical Velocity** (bits 46-56):
 *   - Bit 46: Status (0=invalid, 1=valid)
 *   - Bit 47: Sign (0=climbing, 1=descending/"below")
 *   - Bits 48-56: 9-bit vertical velocity value
 *     * MSB = 8,192 ft/min
 *     * LSB = 32 ft/min (8192/256)
 *     * Range: [-16,384, +16,352] ft/min
 *     * Formula: velocity = value × 32 ft/min (two's complement)
 *     * Special values: 0 or 511 = no velocity information (returns 0)
 *
 * Data Source Notes per ICAO Doc 9871:
 * - **Barometric Altitude Rate**: Solely derived from barometric measurement
 *   (Air Data System or IRS/FMS). Usually unsteady and may suffer from
 *   barometric instrument inertia.
 * - **Inertial Vertical Velocity**: Derived from inertial equipment (IRS, AHRS)
 *   using different sources than barometric (FMC/GNSS integrated, FMC general,
 *   or IRS/FMS). More filtered and smooth parameter. When barometric altitude
 *   rate is integrated and smoothed with inertial data (baro-inertial), it
 *   shall be transmitted in this field.
 *
 * Validation Rules per ICAO Doc 9871:
 * - If parameter exceeds range, use maximum allowable value (requires GFM intervention)
 * - Data should be from sources controlling the aircraft (when possible)
 * - LSB values obtained by rounding
 * - If parameter unavailable, all bits set to ZERO by GFM
 *
 * Implementation Validation:
 * - IAS must be in range (0, 500] kt (operational validation)
 * - Mach must be in range (0, 1] (subsonic aircraft assumption)
 * - IAS and Mach cross-validation:
 *   * If IAS > 250 kt, then Mach must be ≥ 0.4 (250 kt ≈ Mach 0.45 at 10,000 ft)
 *   * If IAS < 150 kt, then Mach must be ≤ 0.5 (150 kt ≈ Mach 0.5 at FL400)
 * - Vertical rate/velocity abs value ≤ 6,000 ft/min (typical operational limit)
 *
 * Note: Two's complement coding used for all signed fields (§A.2.2.2)
 * Additional implementation guidelines in ICAO Doc 9871 §D.2.4.6
 */
#[derive(Debug, PartialEq, Serialize, Deserialize, DekuRead, Clone)]
#[serde(tag = "bds", rename = "60")]
pub struct HeadingAndSpeedReport {
    /// Magnetic Heading (bits 1-12): Per ICAO Doc 9871 Table A-2-96  
    /// Aircraft magnetic heading in degrees (magnetic north reference).  
    /// Encoding details:
    ///   - Bit 1: Status (0=invalid, 1=valid)
    ///   - Bit 2: Sign (0=east, 1=west)
    ///   - Bits 3-12: 10-bit heading magnitude
    ///   - MSB = 90 degrees
    ///   - LSB = 90/512 degrees (≈0.1758°)
    ///   - Formula: heading = value × (90/512) degrees (two's complement)
    ///   - Range: [-180, +180] degrees (internal), converted to [0, 360]° for output
    ///   - Example: 315° encoded as -45°
    ///
    /// Returns None if status bit is 0.
    #[deku(reader = "read_heading(deku::reader)")]
    #[serde(rename = "heading", skip_serializing_if = "Option::is_none")]
    pub magnetic_heading: Option<f64>,

    /// Indicated Airspeed (bits 13-23): Per ICAO Doc 9871 Table A-2-96  
    /// Aircraft indicated airspeed in knots.  
    /// Encoding details:
    ///   - Bit 13: Status (0=invalid, 1=valid)
    ///   - Bits 14-23: 10-bit IAS value
    ///   - MSB = 512 kt
    ///   - LSB = 1 kt
    ///   - Formula: IAS = value × 1 kt
    ///   - Range: [0, 1023] kt
    ///
    /// Returns None if status bit is 0.
    /// Implementation validates IAS ∈ (0, 500] kt (operational range).  
    /// Note: TAS (True Airspeed) is available in BDS 5,0.
    #[deku(reader = "read_ias(deku::reader)")]
    #[serde(rename = "IAS", skip_serializing_if = "Option::is_none")]
    pub indicated_airspeed: Option<u16>,

    /// Mach Number (bits 24-34): Per ICAO Doc 9871 Table A-2-96  
    /// Aircraft Mach number (ratio of aircraft speed to speed of sound).  
    /// Encoding details:
    ///   - Bit 24: Status (0=invalid, 1=valid)
    ///   - Bits 25-34: 10-bit Mach value
    ///   - MSB = 2.048 Mach
    ///   - LSB = 2.048/512 Mach (≈0.004 Mach)
    ///   - Formula: Mach = value × (2.048/512)
    ///   - Range: [0, 4.092] Mach
    ///
    /// Returns None if status bit is 0.  
    /// Implementation validates:
    ///   - Mach ∈ (0, 1] (subsonic aircraft)
    ///   - Cross-validation with IAS:
    ///     * If IAS > 250 kt: Mach ≥ 0.4 (250 kt ≈ Mach 0.45 at 10,000 ft)
    ///     * If IAS < 150 kt: Mach ≤ 0.5 (150 kt ≈ Mach 0.5 at FL400)
    #[deku(reader = "read_mach(deku::reader, *indicated_airspeed)")]
    #[serde(rename = "Mach", skip_serializing_if = "Option::is_none")]
    pub mach_number: Option<f64>,

    /// Barometric Altitude Rate (bits 35-45): Per ICAO Doc 9871 Table A-2-96  
    /// Vertical rate derived solely from barometric measurement in ft/min.  
    /// Encoding details:
    ///   - Bit 35: Status (0=invalid, 1=valid)
    ///   - Bit 36: Sign (0=climbing, 1=descending)
    ///   - Bits 37-45: 9-bit vertical rate magnitude
    ///   - MSB = 8,192 ft/min
    ///   - LSB = 32 ft/min (8192/256)
    ///   - Formula: rate = value × 32 ft/min (two's complement)
    ///   - Range: [-16,384, +16,352] ft/min
    ///   - Special values: 0 or 511 = no rate information (returns 0)
    ///
    /// Returns None if status bit is 0, returns Some(0) if value is 0 or 511.  
    /// Implementation validates abs(rate) ≤ 6,000 ft/min.  
    /// Source: Air Data System or Inertial Reference System/FMS.  
    /// Note: Usually unsteady and may suffer from barometric instrument inertia.
    #[deku(reader = "read_vertical(deku::reader)")]
    #[serde(
        rename = "vrate_barometric",
        skip_serializing_if = "Option::is_none"
    )]
    pub barometric_altitude_rate: Option<i16>,

    /// Inertial Vertical Velocity (bits 46-56): Per ICAO Doc 9871 Table A-2-96  
    /// Vertical velocity from inertial/navigational equipment in ft/min.  
    /// Encoding details:
    ///   - Bit 46: Status (0=invalid, 1=valid)
    ///   - Bit 47: Sign (0=climbing, 1=descending)
    ///   - Bits 48-56: 9-bit vertical velocity magnitude
    ///   - MSB = 8,192 ft/min
    ///   - LSB = 32 ft/min (8192/256)
    ///   - Formula: velocity = value × 32 ft/min (two's complement)
    ///   - Range: [-16,384, +16,352] ft/min
    ///   - Special values: 0 or 511 = no velocity information (returns 0)
    ///
    /// Returns None if status bit is 0, returns Some(0) if value is 0 or 511.  
    /// Implementation validates abs(velocity) ≤ 6,000 ft/min.  
    /// Sources: FMC/GNSS integrated, FMC (General), or IRS/FMS.  
    /// Note: More filtered and smooth than barometric rate. When barometric
    /// altitude rate is integrated and smoothed with inertial data
    /// (baro-inertial), it shall be transmitted in this field.
    #[deku(reader = "read_vertical(deku::reader)")]
    #[serde(
        rename = "vrate_inertial",
        skip_serializing_if = "Option::is_none"
    )]
    pub inertial_vertical_velocity: Option<i16>,
}

fn read_heading<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
) -> Result<Option<f64>, DekuError> {
    let status = bool::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let sign = u8::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let value = u16::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(10)),
    )?;

    if !status {
        if (sign != 0) | (value != 0) {
            return Err(DekuError::Assertion(
                "Non-null value with invalid status: heading".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    let value = if sign == 1 {
        value as i16 - 1024
    } else {
        value as i16
    };
    let mut heading = value as f64 * 90. / 512.;
    if heading < 0. {
        heading += 360.
    }

    Ok(Some(heading))
}

fn read_ias<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
) -> Result<Option<u16>, DekuError> {
    let status = bool::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let value = u16::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(10)),
    )?;

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion(
                "Non-null value with invalid status: IAS".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    if (value == 0) | (value > 500) {
        return Err(DekuError::Assertion(
            format!("IAS value {value} is equal to 0 or greater than 500")
                .into(),
        ));
    }
    Ok(Some(value))
}

fn read_mach<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
    ias: Option<u16>,
) -> Result<Option<f64>, DekuError> {
    let status = bool::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let value = u16::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(10)),
    )?;

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion(
                "Non-null value with invalid status: Mach".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    let mach = value as f64 * 2.048 / 512.;

    if (mach == 0.) | (mach > 1.) {
        return Err(DekuError::Assertion(
            format!("Mach value {mach} equal to 0 or greater than 1 ").into(),
        ));
    }
    if let Some(ias) = ias {
        /*
         * >>> pitot.aero.cas2mach(250, 10000)
         * 0.45229071380275554
         *
         * Let's do this:
         * 10000 ft has IAS max to 250, i.e. Mach 0.45
         * forbid IAS > 250 and Mach < 0.5
         */
        if (ias > 250) & (mach < 0.4) {
            return Err(DekuError::Assertion(
                format!(
                    "IAS: {ias} and Mach: {mach} (250kts is Mach 0.45 at 10,000 ft)"
                )
                .into(),
            ));
        }
        // this one is easy IAS = 150 (close to take-off) at FL 400 is Mach 0.5
        if (ias < 150) & (mach > 0.5) {
            return Err(DekuError::Assertion(
                format!(
                    "IAS: {ias} and Mach: {mach} (150kts is Mach 0.5 at FL400)"
                )
                .into(),
            ));
        }
    }
    Ok(Some(mach))
}

fn read_vertical<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
) -> Result<Option<i16>, DekuError> {
    let status = bool::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let sign = u8::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(1)),
    )?;
    let value = u16::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(9)),
    )?;

    if !status {
        if (sign != 0) | (value != 0) {
            return Err(DekuError::Assertion(
                "Non-null value with invalid status: vertical rate".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    if (value == 0) | (value == 511) {
        // all zeros or all ones
        return Ok(Some(0));
    }
    let value = if sign == 1 {
        (value as i16 - 512) * 32
    } else {
        value as i16 * 32
    };

    if value.abs() > 6000 {
        Err(DekuError::Assertion(
            format!("Vertical rate absolute value {} > 6000", value.abs())
                .into(),
        ))
    } else {
        Ok(Some(value))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::*;
    use approx::assert_relative_eq;
    use hexlit::hex;

    #[test]
    fn test_valid_bds60() {
        let bytes = hex!("a80004aaa74a072bfdefc1d5cb4f");
        let (_, msg) = Message::from_bytes((&bytes, 0)).unwrap();
        if let CommBIdentityReply { bds, .. } = msg.df {
            let HeadingAndSpeedReport {
                magnetic_heading,
                indicated_airspeed,
                mach_number,
                barometric_altitude_rate,
                inertial_vertical_velocity,
            } = bds.bds60.unwrap();
            assert_relative_eq!(
                magnetic_heading.unwrap(),
                110.391,
                max_relative = 1e-3
            );
            assert_eq!(indicated_airspeed.unwrap(), 259);
            assert_relative_eq!(mach_number.unwrap(), 0.7, max_relative = 1e-3);
            assert_eq!(barometric_altitude_rate.unwrap(), -2144);
            assert_eq!(inertial_vertical_velocity.unwrap(), -2016);
        } else {
            unreachable!();
        }
    }
    #[test]
    fn test_invalid_bds60() {
        let bytes = hex!("a0000638fa81c10000000081a92f");
        let (_, msg) = Message::from_bytes((&bytes, 0)).unwrap();
        if let CommBAltitudeReply { bds, .. } = msg.df {
            assert_eq!(bds.bds60, None);
        } else {
            unreachable!();
        }
    }
}