rs1090 0.6.0

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
use deku::prelude::*;
use serde::{Deserialize, Serialize};

/**
 * ## Meteorological Routine Air Report (BDS 4,4)
 *
 * Comm-B message providing meteorological data collected from aircraft sensors.  
 * Per ICAO Doc 9871 Table A-2-68: BDS code 4,4 — Meteorological routine air report
 *
 * Purpose: Allows meteorological data collection by ground systems for weather
 * forecasting and analysis.
 *
 * Message Structure (56 bits):
 * | FOM | WIND SPD | WIND DIR | TEMP | PRESS | TURB | HUMID | RSVD |
 * |-----|----------|----------|------|-------|------|-------|------|
 * | 4   | 1+9      | 9        | 11   | 1+11  | 1+2  | 1+6   | 4    |
 *
 * Field Encoding per ICAO Doc 9871:
 *
 * **FOM/Source** (bits 1-4): Figure of Merit / Navigation source
 *   - 0: Invalid
 *   - 1: INS (Inertial Navigation System)
 *   - 2: GNSS (GPS, etc.)
 *   - 3: DME/DME
 *   - 4: VOR/DME
 *   - 5-15: Reserved
 *
 * **Wind Speed** (bits 5-14):
 *   - Bit 5: Status (0=invalid, 1=valid)
 *   - Bits 6-14: 9-bit wind speed
 *     * LSB = 1 kt, MSB = 256 kt
 *     * Range: [0, 511] kt (Annex 3 requires [0, 250] kt)
 *
 * **Wind Direction** (bits 15-23):
 *   - 9-bit direction (true north)
 *   - MSB = 180 degrees, LSB = 180/256 degrees (≈0.703°)
 *   - Range: [0, 360] degrees
 *   - Only valid if wind_speed status is valid
 *
 * **Static Air Temperature** (bits 24-34):
 *   - Bit 24: Sign (0=positive, 1=negative)
 *   - Bits 25-34: 10-bit temperature value
 *     * MSB = 64°C, LSB = 0.25°C
 *     * Range: [-128, +128]°C (Annex 3 requires [-80, +60]°C)
 *     * Two's complement encoding
 *
 * **Average Static Pressure** (bits 35-46):
 *   - Bit 35: Status (0=invalid, 1=valid)
 *   - Bits 36-46: 11-bit pressure value
 *     * MSB = 1024 hPa, LSB = 1 hPa
 *     * **Direct encoding**: value = pressure in hPa (no offset)
 *     * Range: [0, 2048] hPa
 *   - Note: Not an Annex 3 requirement
 *
 * **Turbulence** (bits 47-49):
 *   - Bit 47: Status (0=invalid, 1=valid)
 *   - Bits 48-49: 2-bit turbulence level
 *     * 00: Nil, 01: Light, 10: Moderate, 11: Severe
 *     * Definitions per PANS-ATM (Doc 4444)
 *
 * **Humidity** (bits 50-56):
 *   - Bit 50: Status (0=invalid, 1=valid)
 *   - Bits 51-56: 6-bit humidity percentage
 *     * MSB = 100%, LSB = 100/64% (≈1.5625%)
 *     * Range: [0, 100]%
 *
 * **Reserved** (bits 57-60): 4 bits reserved
 *
 * Note: Two's complement coding used for all signed fields (§A.2.2.2)
 */

#[derive(Debug, PartialEq, Serialize, Deserialize, DekuRead, Clone)]
#[serde(tag = "bds", rename = "44")]
pub struct MeteorologicalRoutineAirReport {
    /// Figure of Merit / Source (bits 1-4): Per ICAO Doc 9871 Table A-2-68  
    /// Indicates navigation data source quality:
    ///   - 0: Invalid
    ///   - 1: INS (Inertial Navigation System)
    ///   - 2: GNSS (Global Navigation Satellite System)
    ///   - 3: DME/DME (Distance Measuring Equipment)
    ///   - 4: VOR/DME (VHF Omnidirectional Range / DME)
    ///   - 5-15: Reserved
    #[deku(bits = 4)]
    #[deku(reader = "read_figure_of_merit(deku::reader)")]
    pub figure_of_merit: FigureOfMerit,

    #[deku(reader = "read_wind_speed(deku::reader)")]
    /// Wind Speed (bits 5-14): Per ICAO Doc 9871 Table A-2-68  
    /// 9-bit wind speed in knots (LSB=1 kt, range [0, 511] kt)  
    /// Returns None if status bit (bit 5) is 0.
    pub wind_speed: Option<u16>,

    #[deku(reader = "read_wind_direction(deku::reader, *wind_speed)")]
    /// Wind Direction (bits 15-23): Per ICAO Doc 9871 Table A-2-68  
    /// 9-bit direction from true north (LSB=180/256°, range [0, 360]°)  
    /// Only valid if wind_speed is Some.
    pub wind_direction: Option<f64>,

    #[deku(reader = "read_temperature(deku::reader)")]
    /// Static Air Temperature (bits 24-34): Per ICAO Doc 9871 Table A-2-68  
    /// 11-bit signed temperature (LSB=0.25°C, range [-128, +128]°C)  
    /// Two's complement encoding.
    pub temperature: f64,

    #[deku(reader = "read_pressure(deku::reader)")]
    /// Average Static Pressure (bits 35-46): Per ICAO Doc 9871 Table A-2-68  
    /// 11-bit pressure in hPa (LSB=1 hPa, direct encoding, range [0, 2048] hPa)  
    /// Returns None if status bit (bit 35) is 0.
    pub pressure: Option<u16>,

    #[deku(reader = "read_turbulence(deku::reader)")]
    /// Turbulence (bits 47-49): Per ICAO Doc 9871 Table A-2-68  
    /// 2-bit turbulence level (Nil, Light, Moderate, Severe)  
    /// Definitions per PANS-ATM (Doc 4444).  
    /// Returns None if status bit (bit 47) is 0.  
    /// Operationally never populated in civil-airliner transmissions.
    pub turbulence: Option<Turbulence>,

    #[deku(reader = "read_humidity(deku::reader)")]
    /// Humidity (bits 50-56): Per ICAO Doc 9871 Table A-2-68  
    /// 6-bit humidity percentage (LSB=100/64%, range [0, 100]%)  
    /// Returns None if status bit (bit 50) is 0.  
    /// Operationally never populated in civil-airliner transmissions.
    pub humidity: Option<f64>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum FigureOfMerit {
    /// Invalid
    Invalid,
    /// Inertial Navigation System
    INS,
    /// Global Navigation Satellite System
    GNSS,
    /// DME (Distance Measuring Equipment) / DME
    DMEDME,
    /// VHF Omnidirectional Range / DME
    VORDME,
    Reserved(u8),
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum Turbulence {
    Nil,
    Light,
    Moderate,
    Severe,
}

fn read_figure_of_merit<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
) -> Result<FigureOfMerit, DekuError> {
    let value = u8::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(4)),
    )?;

    let fom = match value {
        0 => FigureOfMerit::Invalid,
        1 => FigureOfMerit::INS,
        2 => FigureOfMerit::GNSS,
        3 => FigureOfMerit::DMEDME,
        4 => FigureOfMerit::VORDME,
        n @ 5..=15 => FigureOfMerit::Reserved(n),
        _ => unreachable!(),
    };
    // hard-reject: the reserved FOM range (5–15) appears in 99.47 % of
    // phantom candidates but only 0.01 % of real BDS 4,4 records.
    // Invalid (0), DME/DME (3) and VOR/DME (4) are also rejected because
    // they are never observed in operational civil-airliner transmissions.
    #[cfg(feature = "bds-infer")]
    if !matches!(fom, FigureOfMerit::INS | FigureOfMerit::GNSS) {
        return Err(DekuError::Assertion(
            format!("BDS 4,4 figure of merit {value} not in {{INS=1, GNSS=2}}")
                .into(),
        ));
    }
    Ok(fom)
}
fn read_wind_speed<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(9)),
    )?;

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion(
                "Invalid wind speed value".into(),
            ));
        } else {
            return Ok(None);
        }
    }
    // hard-reject: wind speed > 250 kt is
    // outside operational envelopes (worst recorded jet-stream cores ~200 kt).
    #[cfg(feature = "bds-infer")]
    if value > 250 {
        let msg = format!("Invalid wind speed {value} kts > 250 kts");
        return Err(DekuError::Assertion(msg.into()));
    }

    Ok(Some(value))
}

fn read_wind_direction<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
    speed: Option<u16>,
) -> Result<Option<f64>, DekuError> {
    let value = u16::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(9)),
    )?;

    if speed.is_none() {
        if value != 0 {
            return Err(DekuError::Assertion(
                "Invalid wind direction value".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    Ok(Some(value as f64 * 180. / 256.))
}

fn read_temperature<R: deku::no_std_io::Read + deku::no_std_io::Seek>(
    reader: &mut Reader<R>,
) -> Result<f64, DekuError> {
    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)),
    )?;

    let temp = if sign == 1 {
        (value as f64 - 1024.) * 0.25
    } else {
        value as f64 * 0.25
    };

    // Fallback for avionics firmware bug: some transponders have the sign
    // bit stuck at 1, producing two's complement values < -80°C for what
    // are actually small positive temperatures encoded as sign-magnitude.
    // Adding 256°C (= 1024 × 0.25) recovers the correct value.
    // Verified against 500 paired BDS 44/45 records: 100% match within 0.5°C.
    let temp = if temp < -80.0 { temp + 256.0 } else { temp };

    // hard-reject: valid OAT at cruise is bounded by [-80, +60] °C.
    // Always-on after the wrap-around fallback above (which itself is a
    // bug-fix, not an inference choice).
    #[cfg(feature = "bds-infer")]
    if !(-80. ..=60.).contains(&temp) {
        let msg = "Invalid temperature value {}°C outside [-80, 60]";
        return Err(DekuError::Assertion(msg.into()));
    }
    Ok(temp)
}

fn read_pressure<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(11)),
    )?;

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion("Invalid pressure value".into()));
        } else {
            return Ok(None);
        }
    }

    // BDS 4,4 Average Static Pressure: 11-bit value represents pressure directly in hPa
    // Per ICAO Doc 9871 Table A-2-68:
    //   - Range: [0, 2048] hPa
    //   - LSB = 1 hPa (raw value = pressure in hPa)
    //   - MSB = 1024 hPa
    // No scaling formula needed (unlike BDS 4,0 which uses 800 + value * 0.1)
    Ok(Some(value))
}

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

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion(
                "Invalid turbulence status".into(),
            ));
        } else {
            return Ok(None);
        }
    }

    // hard-reject: turbulence is operationally never populated in civil-airliner
    // BDS 4,4 transmissions; a non-null value almost certainly means this
    // payload is a phantom candidate decoded from a different register.
    #[cfg(feature = "bds-infer")]
    return Err(DekuError::Assertion(
        "BDS 4,4 turbulence field is non-null".into(),
    ));

    #[cfg(not(feature = "bds-infer"))]
    {
        let value = match value {
            0 => Some(Turbulence::Nil),
            1 => Some(Turbulence::Light),
            2 => Some(Turbulence::Moderate),
            3 => Some(Turbulence::Severe),
            _ => unreachable!(),
        };
        Ok(value)
    }
}

fn read_humidity<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 value = u8::from_reader_with_ctx(
        reader,
        (deku::ctx::Endian::Big, deku::ctx::BitSize(6)),
    )?;

    if !status {
        if value != 0 {
            return Err(DekuError::Assertion("Invalid humidity value".into()));
        } else {
            return Ok(None);
        }
    }

    // hard-reject: humidity is operationally never populated in civil-airliner
    // BDS 4,4 transmissions; a non-null value almost certainly means this
    // payload is a phantom candidate decoded from a different register.
    #[cfg(feature = "bds-infer")]
    return Err(DekuError::Assertion(
        "BDS 4,4 humidity field is non-null".into(),
    ));

    #[cfg(not(feature = "bds-infer"))]
    Ok(Some(value as f64 * 100. / 64.))
}

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

    #[test]
    fn test_valid_bds44() {
        let bytes = hex!("a0001692185bd5cf400000dfc696");
        let (_, msg) = Message::from_bytes((&bytes, 0)).unwrap();
        if let CommBAltitudeReply { bds, .. } = msg.df {
            let MeteorologicalRoutineAirReport {
                wind_speed,
                wind_direction,
                temperature,
                pressure,
                humidity,
                ..
            } = bds.bds44.unwrap();
            assert_eq!(wind_speed.unwrap(), 22);
            assert_relative_eq!(
                wind_direction.unwrap(),
                344.5,
                max_relative = 1e-3
            );
            assert_relative_eq!(temperature, -48.75, max_relative = 1e-3);
            assert_eq!(pressure, None);
            assert_eq!(humidity, None);
        } else {
            unreachable!();
        }
    }
}