|
| 1 | +//! Event frame parsing helper module |
| 2 | +//! |
| 3 | +//! Contains functions for parsing E-frames (Event data) from blackbox log data. |
| 4 | +//! These helpers are used by both the library parser and CLI binary. |
| 5 | +
|
| 6 | +use crate::parser::stream::BBLDataStream; |
| 7 | +use crate::types::EventFrame; |
| 8 | +use crate::Result; |
| 9 | + |
| 10 | +/// Helper function to parse inflight adjustment events (types 4 and 13) |
| 11 | +/// Returns the event description string |
| 12 | +fn parse_inflight_adjustment( |
| 13 | + stream: &mut BBLDataStream, |
| 14 | + event_data: &mut Vec<u8>, |
| 15 | +) -> Result<String> { |
| 16 | + let adjustment_function = stream.read_byte()?; |
| 17 | + event_data.extend_from_slice(&[adjustment_function]); |
| 18 | + if adjustment_function > 127 { |
| 19 | + let new_value = stream.read_unsigned_vb()? as f32; |
| 20 | + Ok(format!( |
| 21 | + "Inflight adjustment - Function: {}, New value: {:.3}", |
| 22 | + adjustment_function, new_value |
| 23 | + )) |
| 24 | + } else { |
| 25 | + let new_value = stream.read_signed_vb()?; |
| 26 | + Ok(format!( |
| 27 | + "Inflight adjustment - Function: {}, New value: {}", |
| 28 | + adjustment_function, new_value |
| 29 | + )) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Parse E-frame (Event frame) data from the stream |
| 34 | +/// |
| 35 | +/// E-frames contain various event types such as sync beeps, autotune cycles, |
| 36 | +/// inflight adjustments, logging resume, disarm, flight mode changes, and log end. |
| 37 | +/// Each event type has its own data format that this function decodes. |
| 38 | +pub fn parse_e_frame(stream: &mut BBLDataStream, debug: bool) -> Result<EventFrame> { |
| 39 | + if debug { |
| 40 | + println!("Parsing E frame (Event frame)"); |
| 41 | + } |
| 42 | + |
| 43 | + // Read event type (1 byte) |
| 44 | + let event_type = stream.read_byte()?; |
| 45 | + |
| 46 | + // Read event data - the length depends on the event type |
| 47 | + let mut event_data = Vec::new(); |
| 48 | + let event_name = match event_type { |
| 49 | + 0 => { |
| 50 | + // FLIGHT_LOG_EVENT_SYNC_BEEP |
| 51 | + "Sync beep".to_string() |
| 52 | + } |
| 53 | + 1 => { |
| 54 | + // FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_START |
| 55 | + "Autotune cycle start".to_string() |
| 56 | + } |
| 57 | + 2 => { |
| 58 | + // FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_RESULT |
| 59 | + let axis = stream.read_byte()?; |
| 60 | + let p_gain = stream.read_signed_vb()? as f32 / 1000.0; |
| 61 | + let i_gain = stream.read_signed_vb()? as f32 / 1000.0; |
| 62 | + let d_gain = stream.read_signed_vb()? as f32 / 1000.0; |
| 63 | + event_data.extend_from_slice(&[axis]); |
| 64 | + format!( |
| 65 | + "Autotune cycle result - Axis: {}, P: {:.3}, I: {:.3}, D: {:.3}", |
| 66 | + axis, p_gain, i_gain, d_gain |
| 67 | + ) |
| 68 | + } |
| 69 | + 3 => { |
| 70 | + // FLIGHT_LOG_EVENT_AUTOTUNE_TARGETS |
| 71 | + let current_angle = stream.read_signed_vb()?; |
| 72 | + let target_angle = stream.read_signed_vb()?; |
| 73 | + let target_angle_at_peak = stream.read_signed_vb()?; |
| 74 | + let first_peak_angle = stream.read_signed_vb()?; |
| 75 | + let second_peak_angle = stream.read_signed_vb()?; |
| 76 | + format!( |
| 77 | + "Autotune targets - Current: {}, Target: {}, Peak target: {}, First peak: {}, Second peak: {}", |
| 78 | + current_angle, target_angle, target_angle_at_peak, first_peak_angle, second_peak_angle |
| 79 | + ) |
| 80 | + } |
| 81 | + 4 => { |
| 82 | + // FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT |
| 83 | + parse_inflight_adjustment(stream, &mut event_data)? |
| 84 | + } |
| 85 | + 5 => { |
| 86 | + // FLIGHT_LOG_EVENT_LOGGING_RESUME |
| 87 | + // Note: Event type 14 has identical implementation - both use this newer numbering |
| 88 | + let log_iteration = stream.read_unsigned_vb()?; |
| 89 | + let current_time = stream.read_unsigned_vb()?; |
| 90 | + format!( |
| 91 | + "Logging resume - Iteration: {}, Time: {}", |
| 92 | + log_iteration, current_time |
| 93 | + ) |
| 94 | + } |
| 95 | + 6 => { |
| 96 | + // FLIGHT_LOG_EVENT_LOG_END (old numbering) |
| 97 | + // Read end message bytes |
| 98 | + for _ in 0..4 { |
| 99 | + if !stream.eof { |
| 100 | + event_data.push(stream.read_byte()?); |
| 101 | + } |
| 102 | + } |
| 103 | + "Log end".to_string() |
| 104 | + } |
| 105 | + 10 => { |
| 106 | + // FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_START (UNUSED) |
| 107 | + "Autotune cycle start (unused)".to_string() |
| 108 | + } |
| 109 | + 11 => { |
| 110 | + // FLIGHT_LOG_EVENT_AUTOTUNE_CYCLE_RESULT (UNUSED) |
| 111 | + "Autotune cycle result (unused)".to_string() |
| 112 | + } |
| 113 | + 12 => { |
| 114 | + // FLIGHT_LOG_EVENT_AUTOTUNE_TARGETS (UNUSED) |
| 115 | + "Autotune targets (unused)".to_string() |
| 116 | + } |
| 117 | + 13 => { |
| 118 | + // FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT |
| 119 | + parse_inflight_adjustment(stream, &mut event_data)? |
| 120 | + } |
| 121 | + 14 => { |
| 122 | + // FLIGHT_LOG_EVENT_LOGGING_RESUME (newer numbering, same as type 5) |
| 123 | + // Both event type numbers are used in different firmware versions |
| 124 | + let log_iteration = stream.read_unsigned_vb()?; |
| 125 | + let current_time = stream.read_unsigned_vb()?; |
| 126 | + format!( |
| 127 | + "Logging resume - Iteration: {}, Time: {}", |
| 128 | + log_iteration, current_time |
| 129 | + ) |
| 130 | + } |
| 131 | + 15 => { |
| 132 | + // FLIGHT_LOG_EVENT_DISARM |
| 133 | + "Disarm".to_string() |
| 134 | + } |
| 135 | + 30 => { |
| 136 | + // FLIGHT_LOG_EVENT_FLIGHTMODE - flight mode status event |
| 137 | + // Read flight mode data |
| 138 | + for _ in 0..4 { |
| 139 | + if !stream.eof { |
| 140 | + event_data.push(stream.read_byte()?); |
| 141 | + } |
| 142 | + } |
| 143 | + "Flight mode change".to_string() |
| 144 | + } |
| 145 | + 255 => { |
| 146 | + // FLIGHT_LOG_EVENT_LOG_END |
| 147 | + "Log end".to_string() |
| 148 | + } |
| 149 | + _ => { |
| 150 | + // Unknown event type - read a few bytes as data |
| 151 | + for _ in 0..8 { |
| 152 | + if stream.eof { |
| 153 | + break; |
| 154 | + } |
| 155 | + event_data.push(stream.read_byte()?); |
| 156 | + } |
| 157 | + format!("Unknown event type: {}", event_type) |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + if debug { |
| 162 | + println!( |
| 163 | + "DEBUG: Event - Type: {}, Description: {}", |
| 164 | + event_type, event_name |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + Ok(EventFrame { |
| 169 | + timestamp_us: 0, // Will be set later from context |
| 170 | + event_type, |
| 171 | + event_data, |
| 172 | + event_name, |
| 173 | + }) |
| 174 | +} |
0 commit comments