-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.rs
More file actions
642 lines (580 loc) · 25.3 KB
/
frame.rs
File metadata and controls
642 lines (580 loc) · 25.3 KB
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
use crate::parser::{decoder::*, event::parse_e_frame, gps::*, stream::BBLDataStream};
use crate::types::{
DecodedFrame, EventFrame, FrameDefinition, FrameHistory, FrameStats, GpsCoordinate,
GpsHomeCoordinate,
};
use anyhow::Result;
use std::collections::HashMap;
/// Parse frames from binary data
#[allow(clippy::type_complexity)]
pub fn parse_frames(
binary_data: &[u8],
header: &crate::types::BBLHeader,
debug: bool,
) -> Result<(
FrameStats,
Vec<DecodedFrame>,
Option<HashMap<char, Vec<DecodedFrame>>>,
Vec<GpsCoordinate>,
Vec<GpsHomeCoordinate>,
Vec<EventFrame>,
)> {
let mut stats = FrameStats::default();
let mut sample_frames = Vec::new();
let mut debug_frames: Option<HashMap<char, Vec<DecodedFrame>>> =
if debug { Some(HashMap::new()) } else { None };
// Collections for GPS and Event export
let mut gps_coordinates: Vec<GpsCoordinate> = Vec::new();
let mut home_coordinates: Vec<GpsHomeCoordinate> = Vec::new();
let mut event_frames: Vec<EventFrame> = Vec::new();
// GPS frame history for differential encoding (like P-frames)
let mut gps_frame_history: Vec<i32> = Vec::new();
// Track the last main frame timestamp for G/H/E frame timestamping
let mut last_main_frame_timestamp: u64 = 0;
if debug {
println!("Binary data size: {} bytes", binary_data.len());
if !binary_data.is_empty() {
println!(
"First 16 bytes: {:02X?}",
&binary_data[..16.min(binary_data.len())]
);
}
}
if binary_data.is_empty() {
return Ok((
stats,
sample_frames,
debug_frames,
gps_coordinates,
home_coordinates,
event_frames,
));
}
// Initialize frame history for proper P-frame parsing
let mut frame_history = FrameHistory::new(header.i_frame_def.count);
let mut stream = BBLDataStream::new(binary_data);
// Track the most recent S-frame data for merging (following JavaScript approach)
let mut last_slow_data: HashMap<String, i32> = HashMap::new();
// Main frame parsing loop - process frames as a stream
while !stream.eof {
let frame_start_pos = stream.pos;
match stream.read_byte() {
Ok(frame_type_byte) => {
let frame_type = match frame_type_byte as char {
'I' => 'I',
'P' => 'P',
'H' => 'H',
'G' => 'G',
'E' => 'E',
'S' => 'S',
_ => {
if debug && stats.failed_frames < 3 {
println!(
"Unknown frame type byte 0x{:02X} ('{:?}') at offset {}",
frame_type_byte, frame_type_byte as char, frame_start_pos
);
}
stats.failed_frames += 1;
continue;
}
};
if debug && stats.total_frames < 3 {
println!(
"Found frame type '{}' at offset {}",
frame_type, frame_start_pos
);
}
// Parse frame using proper streaming logic
let mut frame_data = HashMap::new();
let mut parsing_success = false;
match frame_type {
'I' => {
if header.i_frame_def.count > 0 {
// I-frames reset the prediction history
frame_history.current_frame.fill(0);
if parse_frame_data(
&mut stream,
&header.i_frame_def,
&mut frame_history.current_frame,
None, // I-frames don't use prediction
None,
0,
false, // Not raw
header.data_version,
&header.sysconfig,
)
.is_ok()
{
// Copy parsed data to frame_data HashMap
for (i, field_name) in
header.i_frame_def.field_names.iter().enumerate()
{
if i < frame_history.current_frame.len() {
frame_data.insert(
field_name.clone(),
frame_history.current_frame[i],
);
}
}
// Merge lastSlow data into I-frame (following JavaScript approach)
for (key, value) in &last_slow_data {
frame_data.insert(key.clone(), *value);
}
if debug && stats.i_frames <= 2 {
println!("DEBUG: I-frame merged lastSlow. rxSignalReceived: {:?}, rxFlightChannelsValid: {:?}",
frame_data.get("rxSignalReceived"), frame_data.get("rxFlightChannelsValid"));
}
// Update history for future P-frames
frame_history.update(frame_history.current_frame.clone());
parsing_success = true;
stats.i_frames += 1;
// Update last_main_frame_timestamp for G/H/E frame timestamping
if let Some(&time_val) = frame_data.get("time") {
if time_val > 0 {
last_main_frame_timestamp = time_val as u64;
}
}
}
}
}
'P' => {
if header.p_frame_def.count > 0 && frame_history.valid {
frame_history.current_frame.fill(0);
if parse_frame_data(
&mut stream,
&header.p_frame_def,
&mut frame_history.current_frame,
Some(&frame_history.previous_frame),
Some(&frame_history.previous2_frame),
0, // TODO: Calculate skipped frames properly
false, // Not raw
header.data_version,
&header.sysconfig,
)
.is_ok()
{
// Copy parsed data using I-frame field names (P-frames use I-frame structure)
for (i, field_name) in
header.i_frame_def.field_names.iter().enumerate()
{
if i < frame_history.current_frame.len() {
frame_data.insert(
field_name.clone(),
frame_history.current_frame[i],
);
}
}
// Merge lastSlow data into P-frame (following JavaScript approach)
for (key, value) in &last_slow_data {
frame_data.insert(key.clone(), *value);
}
if debug && stats.p_frames <= 2 {
println!("DEBUG: P-frame merged lastSlow. rxSignalReceived: {:?}, rxFlightChannelsValid: {:?}",
frame_data.get("rxSignalReceived"), frame_data.get("rxFlightChannelsValid"));
}
// Update history
frame_history.update(frame_history.current_frame.clone());
parsing_success = true;
stats.p_frames += 1;
// Update last_main_frame_timestamp for G/H/E frame timestamping
if let Some(&time_val) = frame_data.get("time") {
if time_val > 0 {
last_main_frame_timestamp = time_val as u64;
}
}
}
} else {
// Skip P-frame if we don't have valid I-frame history
skip_frame(&mut stream, frame_type, debug)?;
stats.failed_frames += 1;
}
}
'S' => {
if header.s_frame_def.count > 0 {
if let Ok(data) = parse_s_frame(&mut stream, &header.s_frame_def, debug)
{
// Following JavaScript approach: update lastSlow data
if debug {
println!("DEBUG: Processing S-frame with data: {:?}", data);
}
for (key, value) in &data {
last_slow_data.insert(key.clone(), *value);
}
if debug {
println!(
"DEBUG: S-frame data updated lastSlow: {:?}",
last_slow_data
);
}
frame_data = data;
parsing_success = true;
stats.s_frames += 1;
}
}
}
'G' | 'H' | 'E' => {
match frame_type {
'H' => {
// Parse H-frame (GPS home position)
if header.h_frame_def.count > 0 {
if let Ok(data) =
parse_h_frame(&mut stream, &header.h_frame_def, debug)
{
frame_data = data.clone();
parsing_success = true;
stats.h_frames += 1;
// Extract GPS home coordinates
let timestamp = last_main_frame_timestamp;
if let Some(home_coord) =
extract_home_coordinate(&frame_data, timestamp, debug)
{
home_coordinates.push(home_coord);
}
}
} else {
skip_frame(&mut stream, frame_type, debug)?;
stats.h_frames += 1;
parsing_success = true;
}
}
'G' => {
// Parse G-frame (GPS position data)
if header.g_frame_def.count > 0 {
if let Ok(data) = parse_g_frame(
&mut stream,
&header.g_frame_def,
&mut gps_frame_history,
header.data_version,
&header.sysconfig,
debug,
) {
frame_data = data.clone();
parsing_success = true;
stats.g_frames += 1;
// Extract GPS coordinates
let gps_time =
frame_data.get("time").copied().unwrap_or(0) as u64;
let timestamp = if gps_time > 0 {
gps_time
} else {
last_main_frame_timestamp
};
if let Some(coord) = extract_gps_coordinate(
&frame_data,
&home_coordinates,
timestamp,
&header.firmware_revision,
debug,
) {
gps_coordinates.push(coord);
}
}
} else {
skip_frame(&mut stream, frame_type, debug)?;
stats.g_frames += 1;
parsing_success = true;
}
}
'E' => {
// Parse E-frame (Event data)
if let Ok(mut event_frame) = parse_e_frame(&mut stream, debug) {
frame_data.insert(
"event_type".to_string(),
event_frame.event_type as i32,
);
parsing_success = true;
stats.e_frames += 1;
// Set timestamp and collect event frame
event_frame.timestamp_us = last_main_frame_timestamp;
event_frames.push(event_frame);
if debug && stats.e_frames <= 3 {
println!(
"DEBUG: Parsed E-frame - Type: {}",
frame_data.get("event_type").unwrap_or(&0)
);
}
} else {
skip_frame(&mut stream, frame_type, debug)?;
stats.e_frames += 1;
parsing_success = true;
}
}
_ => {}
}
}
_ => {}
};
if !parsing_success {
stats.failed_frames += 1;
}
stats.total_frames += 1;
// Show progress for large files
if debug && stats.total_frames % 50000 == 0 || stats.total_frames % 100000 == 0 {
println!("Parsed {} frames so far...", stats.total_frames);
}
// Store only a few sample frames for display purposes
if parsing_success && sample_frames.len() < 10 {
let decoded_frame = create_decoded_frame(frame_type, &frame_data);
sample_frames.push(decoded_frame.clone());
// Store debug frames if debug mode is enabled
if let Some(ref mut debug_map) = debug_frames {
let debug_frame_list = debug_map.entry(frame_type).or_insert_with(Vec::new);
debug_frame_list.push(decoded_frame);
}
} else if parsing_success {
// Even if we don't store in sample_frames, still store for debug if enabled
if let Some(ref mut debug_map) = debug_frames {
let debug_frame_list = debug_map.entry(frame_type).or_insert_with(Vec::new);
// Store frames strategically for the display pattern (first/middle/last)
if debug_frame_list.len() < 50 {
let decoded_frame = create_decoded_frame(frame_type, &frame_data);
debug_frame_list.push(decoded_frame);
}
}
}
// Update timing from first and last valid frames with time data
if parsing_success {
if let Some(time_us) = frame_data.get("time") {
let time_val = *time_us as u64;
if stats.start_time_us == 0 {
stats.start_time_us = time_val;
}
stats.end_time_us = time_val;
}
}
}
Err(_) => break,
}
// More aggressive safety limits to prevent hanging
if stats.total_frames > 1000000 || stats.failed_frames > 10000 {
if debug {
println!("Hit safety limit - stopping frame parsing");
}
break;
}
}
stats.total_bytes = binary_data.len() as u64;
if debug {
println!(
"Parsed {} frames: {} I, {} P, {} H, {} G, {} E, {} S",
stats.total_frames,
stats.i_frames,
stats.p_frames,
stats.h_frames,
stats.g_frames,
stats.e_frames,
stats.s_frames
);
println!("Failed to parse: {} frames", stats.failed_frames);
}
Ok((
stats,
sample_frames,
debug_frames,
gps_coordinates,
home_coordinates,
event_frames,
))
}
fn create_decoded_frame(frame_type: char, frame_data: &HashMap<String, i32>) -> DecodedFrame {
let timestamp_us = frame_data.get("time").copied().unwrap_or(0) as u64;
let loop_iteration = frame_data.get("loopIteration").copied().unwrap_or(0) as u32;
DecodedFrame {
frame_type,
timestamp_us,
loop_iteration,
data: frame_data.clone(),
}
}
/// Parse frame data using the specified frame definition
#[allow(clippy::too_many_arguments)]
pub fn parse_frame_data(
stream: &mut BBLDataStream,
frame_def: &FrameDefinition,
current_frame: &mut [i32],
previous_frame: Option<&[i32]>,
previous2_frame: Option<&[i32]>,
_skipped_frames: u32,
raw: bool,
_data_version: u8,
sysconfig: &HashMap<String, i32>,
) -> Result<()> {
let mut i = 0;
let mut values = [0i32; 8];
while i < frame_def.fields.len() {
let field = &frame_def.fields[i];
if field.predictor == PREDICT_INC {
current_frame[i] = apply_predictor(
field.predictor,
0,
i,
current_frame,
previous_frame.unwrap_or(&[]),
previous2_frame.unwrap_or(&[]),
sysconfig,
)?;
i += 1;
continue;
}
match field.encoding {
ENCODING_TAG8_4S16 => {
stream.read_tag8_4s16_v2(&mut values)?;
// Apply predictors for the 4 fields
for j in 0..4 {
if i + j >= frame_def.fields.len() {
break;
}
let predictor = if raw {
PREDICT_0
} else {
frame_def.fields[i + j].predictor
};
current_frame[i + j] = apply_predictor(
predictor,
values[j],
i + j,
current_frame,
previous_frame.unwrap_or(&[]),
previous2_frame.unwrap_or(&[]),
sysconfig,
)?;
}
i += 4;
continue;
}
ENCODING_TAG2_3S32 => {
stream.read_tag2_3s32(&mut values)?;
// Apply predictors for the 3 fields
for j in 0..3 {
if i + j >= frame_def.fields.len() {
break;
}
let predictor = if raw {
PREDICT_0
} else {
frame_def.fields[i + j].predictor
};
current_frame[i + j] = apply_predictor(
predictor,
values[j],
i + j,
current_frame,
previous_frame.unwrap_or(&[]),
previous2_frame.unwrap_or(&[]),
sysconfig,
)?;
}
i += 3;
continue;
}
ENCODING_TAG8_8SVB => {
stream.read_tag8_8svb(&mut values)?;
// Apply predictors for the 8 fields
for j in 0..8 {
if i + j >= frame_def.fields.len() {
break;
}
let predictor = if raw {
PREDICT_0
} else {
frame_def.fields[i + j].predictor
};
current_frame[i + j] = apply_predictor(
predictor,
values[j],
i + j,
current_frame,
previous_frame.unwrap_or(&[]),
previous2_frame.unwrap_or(&[]),
sysconfig,
)?;
}
i += 8;
continue;
}
_ => {
decode_field_value(stream, field.encoding, &mut values, 0)?;
let raw_value = values[0];
let predictor = if raw { PREDICT_0 } else { field.predictor };
current_frame[i] = apply_predictor(
predictor,
raw_value,
i,
current_frame,
previous_frame.unwrap_or(&[]),
previous2_frame.unwrap_or(&[]),
sysconfig,
)?;
}
}
i += 1;
}
Ok(())
}
fn parse_s_frame(
stream: &mut BBLDataStream,
frame_def: &FrameDefinition,
debug: bool,
) -> Result<HashMap<String, i32>> {
let mut data = HashMap::new();
// Parse each field according to the frame definition
for field in &frame_def.fields {
let _values = [0i32; 1];
let value = match field.encoding {
ENCODING_SIGNED_VB => stream.read_signed_vb()?,
ENCODING_UNSIGNED_VB => stream.read_unsigned_vb()? as i32,
ENCODING_NEG_14BIT => stream.read_neg_14bit()?,
ENCODING_NULL => 0,
_ => {
if debug {
println!(
"Unsupported S-frame encoding {} for field {}",
field.encoding, field.name
);
}
// For unsupported encodings, try to read as signed VB
stream.read_signed_vb().unwrap_or(0)
}
};
data.insert(field.name.clone(), value);
}
Ok(data)
}
fn skip_frame(stream: &mut BBLDataStream, frame_type: char, debug: bool) -> Result<()> {
if debug {
println!("Skipping {} frame", frame_type);
}
// Skip frame by reading a few bytes - this is a simple heuristic
match frame_type {
'E' => {
// Event frames - read event type and some data
let _event_type = stream.read_byte()?;
// Read up to 16 bytes of event data
for _ in 0..16 {
if stream.eof {
break;
}
let _ = stream.read_byte();
}
}
'G' | 'H' => {
// GPS frames - read several fields
for _ in 0..7 {
if stream.eof {
break;
}
let _ = stream.read_unsigned_vb();
}
}
_ => {
// Unknown frame type - read a few bytes
for _ in 0..8 {
if stream.eof {
break;
}
let _ = stream.read_byte();
}
}
}
Ok(())
}