|
| 1 | +use crate::types::*; |
| 2 | +use crate::Result; |
| 3 | +use anyhow::{anyhow, Context}; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +/// Parse BBL file and return all logs (for CLI and multi-log processing) |
| 7 | +pub fn parse_bbl_file_all_logs( |
| 8 | + file_path: &Path, |
| 9 | + export_options: crate::ExportOptions, |
| 10 | + debug: bool, |
| 11 | +) -> Result<Vec<BBLLog>> { |
| 12 | + if debug { |
| 13 | + println!("=== PARSING BBL FILE ==="); |
| 14 | + let metadata = std::fs::metadata(file_path)?; |
| 15 | + println!( |
| 16 | + "File size: {} bytes ({:.2} MB)", |
| 17 | + metadata.len(), |
| 18 | + metadata.len() as f64 / 1024.0 / 1024.0 |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + let file_data = std::fs::read(file_path) |
| 23 | + .with_context(|| format!("Failed to read BBL file: {:?}", file_path))?; |
| 24 | + |
| 25 | + parse_bbl_bytes_all_logs(&file_data, export_options, debug) |
| 26 | +} |
| 27 | + |
| 28 | +/// Parse BBL file and return first log (for library API compatibility) |
| 29 | +pub fn parse_bbl_file( |
| 30 | + file_path: &Path, |
| 31 | + export_options: crate::ExportOptions, |
| 32 | + debug: bool, |
| 33 | +) -> Result<BBLLog> { |
| 34 | + let logs = parse_bbl_file_all_logs(file_path, export_options, debug)?; |
| 35 | + logs.into_iter() |
| 36 | + .next() |
| 37 | + .ok_or_else(|| anyhow!("No logs found in BBL file")) |
| 38 | +} |
| 39 | + |
| 40 | +/// Parse BBL data from memory and return all logs |
| 41 | +pub fn parse_bbl_bytes_all_logs( |
| 42 | + data: &[u8], |
| 43 | + _export_options: crate::ExportOptions, |
| 44 | + debug: bool, |
| 45 | +) -> Result<Vec<BBLLog>> { |
| 46 | + if debug { |
| 47 | + println!("=== PARSING BBL DATA ==="); |
| 48 | + println!("Data size: {} bytes", data.len()); |
| 49 | + } |
| 50 | + |
| 51 | + // Look for multiple logs by searching for log start markers |
| 52 | + let log_start_marker = b"H Product:Blackbox flight data recorder by Nicholas Sherlock"; |
| 53 | + let mut log_positions = Vec::new(); |
| 54 | + |
| 55 | + // Find all log start positions |
| 56 | + for i in 0..data.len() { |
| 57 | + if i + log_start_marker.len() <= data.len() |
| 58 | + && &data[i..i + log_start_marker.len()] == log_start_marker |
| 59 | + { |
| 60 | + log_positions.push(i); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if log_positions.is_empty() { |
| 65 | + return Err(anyhow!("No blackbox log headers found in data")); |
| 66 | + } |
| 67 | + |
| 68 | + if debug { |
| 69 | + println!("Found {} log(s) in data", log_positions.len()); |
| 70 | + } |
| 71 | + |
| 72 | + // Parse all logs |
| 73 | + let mut logs = Vec::new(); |
| 74 | + for (log_index, &start_pos) in log_positions.iter().enumerate() { |
| 75 | + if debug { |
| 76 | + println!( |
| 77 | + "Parsing log {} of {} (starting at position {})", |
| 78 | + log_index + 1, |
| 79 | + log_positions.len(), |
| 80 | + start_pos |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + let end_pos = log_positions |
| 85 | + .get(log_index + 1) |
| 86 | + .copied() |
| 87 | + .unwrap_or(data.len()); |
| 88 | + let log_data = &data[start_pos..end_pos]; |
| 89 | + |
| 90 | + let log = parse_single_log(log_data, log_index + 1, log_positions.len(), debug)?; |
| 91 | + logs.push(log); |
| 92 | + } |
| 93 | + |
| 94 | + Ok(logs) |
| 95 | +} |
| 96 | + |
| 97 | +/// Parse BBL data from memory (returns first log for library API compatibility) |
| 98 | +pub fn parse_bbl_bytes( |
| 99 | + data: &[u8], |
| 100 | + export_options: crate::ExportOptions, |
| 101 | + debug: bool, |
| 102 | +) -> Result<BBLLog> { |
| 103 | + let logs = parse_bbl_bytes_all_logs(data, export_options, debug)?; |
| 104 | + logs.into_iter() |
| 105 | + .next() |
| 106 | + .ok_or_else(|| anyhow!("No logs found in BBL data")) |
| 107 | +} |
| 108 | + |
| 109 | +// Note: The rest of the parsing functions will be migrated from src/main.rs |
| 110 | +// This is a placeholder for the systematic migration process |
| 111 | + |
| 112 | +/// Internal function to parse a single BBL log from binary data |
| 113 | +fn parse_single_log( |
| 114 | + log_data: &[u8], |
| 115 | + log_number: usize, |
| 116 | + total_logs: usize, |
| 117 | + debug: bool, |
| 118 | +) -> Result<BBLLog> { |
| 119 | + // Find where headers end and binary data begins |
| 120 | + let mut header_end = 0; |
| 121 | + for i in 1..log_data.len() { |
| 122 | + if log_data[i - 1] == b'\n' && log_data[i] != b'H' { |
| 123 | + header_end = i; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if header_end == 0 { |
| 129 | + header_end = log_data.len(); |
| 130 | + } |
| 131 | + |
| 132 | + // Parse headers from the text section |
| 133 | + let header_text = std::str::from_utf8(&log_data[0..header_end])?; |
| 134 | + let header = crate::parser::header::parse_headers_from_text(header_text, debug)?; |
| 135 | + |
| 136 | + // Parse binary frame data |
| 137 | + let binary_data = &log_data[header_end..]; |
| 138 | + let (mut stats, sample_frames, debug_frames) = |
| 139 | + crate::parser::frame::parse_frames(binary_data, &header, debug)?; |
| 140 | + |
| 141 | + // Update frame stats timing from actual frame data |
| 142 | + if !sample_frames.is_empty() { |
| 143 | + stats.start_time_us = sample_frames.first().unwrap().timestamp_us; |
| 144 | + stats.end_time_us = sample_frames.last().unwrap().timestamp_us; |
| 145 | + } |
| 146 | + |
| 147 | + let log = BBLLog { |
| 148 | + log_number, |
| 149 | + total_logs, |
| 150 | + header, |
| 151 | + stats, |
| 152 | + sample_frames, |
| 153 | + debug_frames, |
| 154 | + gps_coordinates: Vec::new(), // TODO: Extract from parsed frames |
| 155 | + home_coordinates: Vec::new(), // TODO: Extract from parsed frames |
| 156 | + event_frames: Vec::new(), // TODO: Extract from parsed frames |
| 157 | + }; |
| 158 | + |
| 159 | + Ok(log) |
| 160 | +} |
0 commit comments