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
#![doc = include_str!("../readme.md")]
mod bds;
mod beast;
mod cat48;
mod error;
mod file;
use beast::{bds_code_from_decoded, extract_beast_frame};
use clap::{Parser, Subcommand};
use deku::DekuContainerRead;
use rs1090::decode::bds::infer_bds;
use rs1090::decode::cpr::Position;
use rs1090::prelude::*;
use tokio::fs;
use tokio::io::AsyncWriteExt;
#[derive(Debug, Parser)]
#[command(
name = "decode1090",
version,
author = "xoolive",
about = "Decode Mode S demodulated raw messages to JSON format"
)]
struct Options {
#[command(subcommand)]
command: Option<Commands>,
/// Output file instead of stdout
#[arg(long, short, default_value=None)]
output: Option<String>,
/// Individual messages to decode
msgs: Vec<String>,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Decode ASTERIX CAT48 files (.raw, .da1) to JSON
Cat48 {
/// Input files or glob patterns (e.g., "*.da1", "data/*.raw")
#[arg(required = true)]
inputs: Vec<String>,
/// Output file instead of stdout (JSONL format)
#[arg(long, short)]
output: Option<String>,
/// Output one JSON array instead of JSONL (one record per line)
#[arg(long)]
array: bool,
/// Filter: only include rollcall records (typ field)
#[arg(long)]
only_rollcall: bool,
/// Filter: only include records with BDS payloads
#[arg(long)]
with_bds: bool,
/// Filter: exclude records with all-zero BDS payload
#[arg(long)]
exclude_zero: bool,
/// Filter: only include records with specific declared BDS codes
/// Comma-separated hex codes, e.g. "44,45" or "40,50,60"
#[arg(long)]
filter_bds: Option<String>,
/// Radar position used to cross-check locally decoded CPR candidates
#[arg(long)]
radar: Option<Position>,
},
/// Decode BDS payload (7 bytes)
Bds {
/// BDS payload as hexadecimal string (56 bits = 7 bytes)
#[arg(required = true)]
payload: String,
/// BDS code (optional, e.g., "40", "50", "60")
/// If provided, decode strictly as this BDS code.
/// If not provided, infer all possible BDS codes.
#[arg(long, short)]
bds: Option<String>,
},
/// Decode Mode S messages from files (JSONL, CSV; plain, .gz, or .7z)
File {
/// Input files (JSONL or CSV Beast format; .jsonl.7z and .csv.7z are supported directly)
#[arg(required = true)]
inputs: Vec<String>,
/// Reference coordinates for the decoding
/// (e.g. --reference LFPG for major airports,
/// --reference 43.3,1.35 or --reference ' -34,18.6' if negative)
#[arg(long, short)]
reference: Option<Position>,
/// Specify input format explicitly (jsonl, csv)
/// If not provided, format is auto-detected
#[arg(long)]
format: Option<String>,
/// Output file instead of stdout
#[arg(long, short)]
output: Option<String>,
/// Deduplication threshold (in ms)
#[arg(long, default_value = "400")]
deduplication: u128,
},
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let options = Options::parse();
// Handle subcommands
match options.command {
Some(Commands::Cat48 {
inputs,
output,
array,
only_rollcall,
with_bds,
exclude_zero,
filter_bds,
radar,
}) => {
return cat48::process_cat48(
inputs,
output,
array,
only_rollcall,
with_bds,
exclude_zero,
filter_bds,
radar,
)
.await;
}
Some(Commands::Bds { payload, bds }) => {
return bds::process_bds_decode(&payload, bds);
}
Some(Commands::File {
inputs,
reference,
format,
output,
deduplication,
}) => {
return file::process_file_decode(
inputs,
reference,
format,
output,
deduplication,
)
.await;
}
None => {}
}
// Default behavior: decode raw Mode S / BDS messages
let mut output_file = if let Some(output_path) = options.output {
Some(
fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(output_path)
.await?,
)
} else {
None
};
if !options.msgs.is_empty() {
for msg in options.msgs {
let bytes = hex::decode(&msg).unwrap();
let json = match bytes.len() {
// 7 bytes: try Mode S short first, fallback to BDS inference
7 => {
if let Ok(message) = Message::try_from(bytes.as_slice()) {
serde_json::to_string(&message).unwrap()
} else {
// BDS inference fallback
let inferred = infer_bds(&bytes, None);
let mut decoded_map = serde_json::json!({});
for decoded in inferred.iter() {
let bds_code = bds_code_from_decoded(decoded);
decoded_map[format!("{:02x}", bds_code)] =
serde_json::to_value(decoded)
.unwrap_or(serde_json::json!(null));
}
serde_json::to_string(&serde_json::json!({
"payload": hex::encode(&bytes),
"mode": "infer",
"decoded": decoded_map
}))
.unwrap()
}
}
// 14 bytes: Mode S long message
14 => {
let message = Message::try_from(bytes.as_slice()).unwrap();
serde_json::to_string(&message).unwrap()
}
// 16 bytes: Beast Mode S short (1a32 + 6 ts + 1 signal + 7 payload)
// 23 bytes: Beast Mode S long (1a33 + 6 ts + 1 signal + 14 payload)
16 | 23
if bytes[0] == 0x1a
&& (bytes[1] == 0x32 || bytes[1] == 0x33) =>
{
if let Some((timestamp, frame)) =
extract_beast_frame(&bytes)
{
let message = Message::from_bytes((&frame, 0))
.ok()
.map(|(_, m)| m);
let tmsg = TimedMessage {
timestamp,
frame,
message,
metadata: vec![],
decode_time: None,
};
serde_json::to_string(&tmsg).unwrap()
} else {
continue;
}
}
_ => {
// Try generic Mode S decode for other lengths
if let Ok(message) = Message::try_from(bytes.as_slice()) {
serde_json::to_string(&message).unwrap()
} else {
continue;
}
}
};
if let Some(file) = &mut output_file {
file.write_all(json.as_bytes()).await?;
file.write_all("\n".as_bytes()).await?;
} else {
println!("{json}");
}
}
}
Ok(())
}