mdf4-rs 0.3.3

mdf4-rs is a Rust library for working with Measurement Data Format (ASAM MDF4) files.
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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
//! High-level DBC + MDF Logger with full metadata support.
//!
//! This module provides [`CanDbcLogger`], a high-performance logger that combines
//! DBC signal definitions with MDF4 file writing. It supports:
//!
//! - Full metadata preservation (units, conversions, limits)
//! - Raw value storage with conversion blocks for maximum precision
//! - Physical value storage for compatibility
//! - Multiplexed signal support with separate channel groups per mux value
//! - **Zero-allocation hot path** using `FastDbc` and pre-computed signal mappings
//!
//! # Multiplexed Signals
//!
//! For DBC messages with multiplexed signals (using M/m0/m1/etc. notation),
//! the logger creates separate MDF4 channel groups for each multiplexor value.
//! For example, a message "DiagResponse" with mux values 0, 1, 2 will create:
//! - "DiagResponse_Mux0"
//! - "DiagResponse_Mux1"
//! - "DiagResponse_Mux2"
//!
//! Each group contains only the signals active for that mux value, plus the
//! multiplexor switch signal itself. Non-multiplexed signals are included in
//! all groups.
//!
//! # Storage Modes
//!
//! The logger supports two storage modes:
//!
//! 1. **Physical Values** (default): Stores decoded physical values as 64-bit floats.
//!    This is simpler but loses some precision for integer signals.
//!
//! 2. **Raw Values**: Stores raw integer values with MDF4 conversion blocks.
//!    This preserves full precision and allows MDF4 viewers to show both
//!    raw and physical values.

mod builder;

pub use builder::{CanDbcLoggerBuilder, CanDbcLoggerConfig};

use alloc::collections::BTreeMap;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::vec::Vec;

use super::dbc_compat::SignalInfo;

/// Key for identifying a specific channel group buffer.
/// For non-multiplexed messages: (can_id, None)
/// For multiplexed messages: (can_id, Some(mux_value))
type BufferKey = (u32, Option<u64>);

/// Buffer for a single message's decoded data.
#[derive(Debug)]
struct MessageBuffer {
    /// Signal information extracted from DBC
    signals: Vec<SignalInfo>,
    /// Mapping from buffer signal index to message signal index
    /// Used for zero-alloc decode: decode_buf[msg_idx] -> buffer signal
    signal_indices: Vec<usize>,
    /// Timestamps for each frame (microseconds)
    timestamps: Vec<u64>,
    /// Raw values per signal (outer vec = signals, inner vec = samples)
    raw_values: Vec<Vec<i64>>,
    /// Physical values per signal (outer vec = signals, inner vec = samples)
    physical_values: Vec<Vec<f64>>,
}

impl MessageBuffer {
    fn new(signals: Vec<SignalInfo>, signal_indices: Vec<usize>) -> Self {
        let num_signals = signals.len();
        Self {
            signals,
            signal_indices,
            timestamps: Vec::new(),
            raw_values: (0..num_signals).map(|_| Vec::new()).collect(),
            physical_values: (0..num_signals).map(|_| Vec::new()).collect(),
        }
    }

    /// Append physical samples straight from the shared decode buffer, selecting
    /// this buffer's signals via `signal_indices`. Keeps the hot path allocation-free
    /// (no intermediate `Vec` per frame).
    fn push_physical_indexed(&mut self, timestamp_us: u64, decode_buf: &[f64]) {
        self.timestamps.push(timestamp_us);
        for (out, &idx) in self
            .physical_values
            .iter_mut()
            .zip(self.signal_indices.iter())
        {
            out.push(decode_buf.get(idx).copied().unwrap_or(0.0));
        }
    }

    /// Append raw samples straight from the shared decode buffer (see
    /// [`Self::push_physical_indexed`]).
    fn push_raw_indexed(&mut self, timestamp_us: u64, decode_buf: &[i64]) {
        self.timestamps.push(timestamp_us);
        for (out, &idx) in self.raw_values.iter_mut().zip(self.signal_indices.iter()) {
            out.push(decode_buf.get(idx).copied().unwrap_or(0));
        }
    }

    fn clear(&mut self) {
        self.timestamps.clear();
        for v in &mut self.raw_values {
            v.clear();
        }
        for v in &mut self.physical_values {
            v.clear();
        }
    }

    fn frame_count(&self) -> usize {
        self.timestamps.len()
    }
}

/// Channel IDs stored after MDF initialization.
/// Reserved for future use (e.g., updating channel metadata after initialization).
#[allow(dead_code)]
struct ChannelIds {
    time_channel: String,
    signal_channels: Vec<String>,
}

/// Information about a multiplexed message.
#[derive(Debug)]
struct MultiplexInfo {
    /// Index of the multiplexor switch signal in the message
    switch_index: usize,
    /// All mux values used by signals in this message
    mux_values: BTreeSet<u64>,
}

/// High-level CAN logger that combines DBC signal definitions with MDF writing.
///
/// This provides a simple API for logging CAN bus data to MDF files using
/// signal definitions from a DBC file. Uses `FastDbc` for O(1) message lookup
/// and zero-allocation decoding via `Message::decode_into()`.
///
/// # Features
///
/// - Full metadata preservation (units, conversions, limits)
/// - Raw value storage with conversion blocks for maximum precision
/// - Physical value storage for compatibility
/// - Support for standard and extended CAN IDs
/// - **Zero-allocation hot path** for high-speed logging
///
/// # Example
///
/// ```ignore
/// use mdf4_rs::can::CanDbcLogger;
///
/// let dbc = dbc_rs::Dbc::parse(dbc_content)?;
///
/// // Simple usage (stores physical values)
/// let mut logger = CanDbcLogger::new(dbc)?;
///
/// // Or with builder for raw value storage
/// let mut logger = CanDbcLogger::builder(dbc)
///     .store_raw_values(true)
///     .build()?;
///
/// // Log CAN frames
/// logger.log(0x100, timestamp_us, &frame_data);
///
/// // Get MDF bytes
/// let mdf_bytes = logger.finalize()?;
/// ```
pub struct CanDbcLogger<W: crate::writer::MdfWrite> {
    /// Fast DBC wrapper for O(1) message lookup
    fast_dbc: dbc_rs::FastDbc,
    config: CanDbcLoggerConfig,
    /// Buffers keyed by (can_id, Option<mux_value>)
    buffers: BTreeMap<BufferKey, MessageBuffer>,
    writer: crate::MdfWriter<W>,
    /// Channel groups keyed by (can_id, Option<mux_value>)
    channel_groups: BTreeMap<BufferKey, String>,
    /// Channel IDs keyed by (can_id, Option<mux_value>)
    channel_ids: BTreeMap<BufferKey, ChannelIds>,
    /// Multiplexed message info keyed by can_id
    mux_info: BTreeMap<u32, MultiplexInfo>,
    /// Pre-allocated decode buffer for physical values
    decode_buf: Vec<f64>,
    /// Pre-allocated decode buffer for raw values
    decode_raw_buf: Vec<i64>,
    initialized: bool,
}

impl CanDbcLogger<crate::writer::VecWriter> {
    /// Create a new DBC MDF logger with in-memory output.
    ///
    /// Uses signal definitions from the provided DBC file.
    /// Stores physical values by default; use `builder()` for raw value mode.
    pub fn new(dbc: dbc_rs::Dbc) -> crate::Result<Self> {
        let writer = crate::MdfWriter::from_writer(crate::writer::VecWriter::new());
        Ok(Self::with_config(
            dbc,
            writer,
            CanDbcLoggerConfig::default(),
        ))
    }

    /// Create a new DBC MDF logger with pre-allocated capacity.
    pub fn with_capacity(dbc: dbc_rs::Dbc, capacity: usize) -> crate::Result<Self> {
        let writer =
            crate::MdfWriter::from_writer(crate::writer::VecWriter::with_capacity(capacity));
        Ok(Self::with_config(
            dbc,
            writer,
            CanDbcLoggerConfig::default(),
        ))
    }

    /// Create a builder for configuring the logger.
    pub fn builder(dbc: dbc_rs::Dbc) -> CanDbcLoggerBuilder {
        CanDbcLoggerBuilder::new(dbc)
    }

    /// Finalize the MDF file and return the bytes.
    pub fn finalize(mut self) -> crate::Result<Vec<u8>> {
        self.flush_and_finalize()?;
        Ok(self.writer.into_inner().into_inner())
    }

    /// Load an existing MDF4 file containing raw CAN frames for appending.
    ///
    /// This reads raw frames from an MDF4 file (created by RawCanLogger) and
    /// decodes them using the provided DBC, allowing you to append new frames.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use mdf4_rs::can::CanDbcLogger;
    ///
    /// let dbc = dbc_rs::Dbc::from_file("vehicle.dbc")?;
    ///
    /// // Load existing raw capture and decode with DBC
    /// let mut logger = CanDbcLogger::from_raw_mdf4("existing.mf4", dbc)?;
    ///
    /// // Append new frames
    /// let last_ts = logger.last_timestamp_us();
    /// logger.log(0x100, last_ts + 1000, &[0x01, 0x02]);
    ///
    /// // Save decoded output
    /// let bytes = logger.finalize()?;
    /// std::fs::write("decoded.mf4", bytes)?;
    /// ```
    #[cfg(feature = "std")]
    pub fn from_raw_mdf4(path: &str, dbc: dbc_rs::Dbc) -> crate::Result<Self> {
        Self::from_raw_mdf4_with_config(path, dbc, CanDbcLoggerConfig::default())
    }

    /// Load an existing MDF4 file with custom configuration.
    #[cfg(feature = "std")]
    pub fn from_raw_mdf4_with_config(
        path: &str,
        dbc: dbc_rs::Dbc,
        config: CanDbcLoggerConfig,
    ) -> crate::Result<Self> {
        use crate::DecodedValue;
        use crate::index::{FileRangeReader, MdfIndex};

        let index = MdfIndex::from_file(path)?;
        let mut reader = FileRangeReader::new(path)?;

        let writer = crate::MdfWriter::from_writer(crate::writer::VecWriter::new());
        let mut logger = Self::with_config(dbc, writer, config);

        // Find ASAM CAN_DataFrame channel groups and read raw frames
        for (group_idx, group) in index.channel_groups.iter().enumerate() {
            let mut timestamp_ch = None;
            let mut dataframe_ch = None;

            for (ch_idx, channel) in group.channels.iter().enumerate() {
                if let Some(name) = &channel.name {
                    match name.as_str() {
                        "Timestamp" => timestamp_ch = Some(ch_idx),
                        "CAN_DataFrame" => dataframe_ch = Some(ch_idx),
                        _ => {}
                    }
                }
            }

            let (ts_ch, df_ch) = match (timestamp_ch, dataframe_ch) {
                (Some(t), Some(d)) => (t, d),
                _ => continue,
            };

            let timestamps = index.read_channel_values(group_idx, ts_ch, &mut reader)?;
            let dataframes = index.read_channel_values(group_idx, df_ch, &mut reader)?;

            for (ts_val, df_val) in timestamps.iter().zip(dataframes.iter()) {
                let timestamp_us = match ts_val {
                    Some(DecodedValue::Float(s)) => (*s * 1_000_000.0) as u64,
                    Some(DecodedValue::UnsignedInteger(us)) => *us,
                    _ => continue,
                };

                let bytes = match df_val {
                    Some(DecodedValue::ByteArray(b)) => b,
                    _ => continue,
                };

                if bytes.len() < 5 {
                    continue;
                }

                let raw_id = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
                let is_extended = (raw_id & 0x8000_0000) != 0;
                let can_id = raw_id & 0x1FFF_FFFF;
                let dlc = bytes[4];
                let data_len = super::fd::dlc_to_len(dlc).min(bytes.len() - 5);
                let data = &bytes[5..5 + data_len];

                // Log through DBC decoder
                if is_extended {
                    logger.log_extended(can_id, timestamp_us, data);
                } else {
                    logger.log(can_id, timestamp_us, data);
                }
            }
        }

        Ok(logger)
    }

    /// Get the last timestamp in microseconds from loaded frames.
    ///
    /// Returns 0 if no frames have been logged.
    pub fn last_timestamp_us(&self) -> u64 {
        self.buffers
            .values()
            .flat_map(|buf| buf.timestamps.iter())
            .copied()
            .max()
            .unwrap_or(0)
    }

    /// Get the total number of decoded frames.
    pub fn total_frame_count(&self) -> usize {
        self.buffers.values().map(|buf| buf.frame_count()).sum()
    }
}

#[cfg(feature = "std")]
impl CanDbcLogger<crate::writer::FileWriter> {
    /// Create a new DBC MDF logger that writes to a file.
    pub fn new_file(dbc: dbc_rs::Dbc, path: &str) -> crate::Result<Self> {
        let writer = crate::MdfWriter::new(path)?;
        Ok(Self::with_config(
            dbc,
            writer,
            CanDbcLoggerConfig::default(),
        ))
    }

    /// Create a builder for configuring the logger with file output.
    pub fn builder_file(dbc: dbc_rs::Dbc) -> CanDbcLoggerBuilder {
        CanDbcLoggerBuilder::new(dbc)
    }

    /// Finalize and close the MDF file.
    pub fn finalize_file(mut self) -> crate::Result<()> {
        self.flush_and_finalize()
    }
}

impl<W: crate::writer::MdfWrite> CanDbcLogger<W> {
    /// Create a logger with custom configuration.
    pub(crate) fn with_config(
        dbc: dbc_rs::Dbc,
        writer: crate::MdfWriter<W>,
        config: CanDbcLoggerConfig,
    ) -> Self {
        let fast_dbc = dbc_rs::FastDbc::new(dbc);
        let mut buffers = BTreeMap::new();
        let mut mux_info = BTreeMap::new();

        for message in fast_dbc.dbc().messages().iter() {
            let can_id = message.id();
            let signals = message.signals();

            // Build signal name to index map for this message
            let mut signal_name_to_idx: BTreeMap<&str, usize> = BTreeMap::new();
            for (idx, signal) in signals.iter().enumerate() {
                signal_name_to_idx.insert(signal.name(), idx);
            }

            // Check if this message has multiplexed signals
            let mut switch_name: Option<&str> = None;
            let mut switch_index: Option<usize> = None;
            let mut mux_values: BTreeSet<u64> = BTreeSet::new();

            for (idx, signal) in signals.iter().enumerate() {
                if signal.is_multiplexer_switch() {
                    switch_name = Some(signal.name());
                    switch_index = Some(idx);
                }
                if let Some(mux_val) = signal.multiplexer_switch_value() {
                    mux_values.insert(mux_val);
                }
            }

            if let (Some(_switch), Some(sw_idx)) = (switch_name, switch_index) {
                if !mux_values.is_empty() {
                    // This is a multiplexed message - create separate buffers per mux value
                    mux_info.insert(
                        can_id,
                        MultiplexInfo {
                            switch_index: sw_idx,
                            mux_values: mux_values.clone(),
                        },
                    );

                    for mux_val in &mux_values {
                        // Collect signals for this mux value:
                        // - The multiplexor switch signal
                        // - Non-multiplexed signals
                        // - Signals with this specific mux value
                        let mut mux_signals: Vec<SignalInfo> = Vec::new();
                        let mut signal_indices: Vec<usize> = Vec::new();

                        for (idx, signal) in signals.iter().enumerate() {
                            let include = signal.is_multiplexer_switch()
                                || signal.multiplexer_switch_value() == Some(*mux_val)
                                || (signal.multiplexer_switch_value().is_none()
                                    && !signal.is_multiplexer_switch());

                            if include {
                                mux_signals.push(SignalInfo::from_signal(signal));
                                signal_indices.push(idx);
                            }
                        }

                        if !mux_signals.is_empty() {
                            buffers.insert(
                                (can_id, Some(*mux_val)),
                                MessageBuffer::new(mux_signals, signal_indices),
                            );
                        }
                    }
                    continue;
                }
            }

            // Non-multiplexed message - single buffer
            let all_signals: Vec<SignalInfo> =
                signals.iter().map(SignalInfo::from_signal).collect();
            let signal_indices: Vec<usize> = (0..signals.len()).collect();
            if !all_signals.is_empty() {
                buffers.insert(
                    (can_id, None),
                    MessageBuffer::new(all_signals, signal_indices),
                );
            }
        }

        // Pre-allocate decode buffers based on max signals
        let max_signals = fast_dbc.max_signals();
        let decode_buf = vec![0.0f64; max_signals];
        let decode_raw_buf = vec![0i64; max_signals];

        Self {
            fast_dbc,
            config,
            buffers,
            writer,
            channel_groups: BTreeMap::new(),
            channel_ids: BTreeMap::new(),
            mux_info,
            decode_buf,
            decode_raw_buf,
            initialized: false,
        }
    }

    /// Get the current configuration.
    pub fn config(&self) -> &CanDbcLoggerConfig {
        &self.config
    }

    /// Get a reference to the underlying FastDbc.
    pub fn fast_dbc(&self) -> &dbc_rs::FastDbc {
        &self.fast_dbc
    }

    /// Log a CAN frame with timestamp.
    ///
    /// The frame is decoded using the DBC and buffered.
    /// Call `flush()` periodically or `finalize()` at the end to write data to MDF.
    ///
    /// Returns `true` if the message was recognized and logged, `false` otherwise.
    #[inline]
    pub fn log(&mut self, can_id: u32, timestamp_us: u64, data: &[u8]) -> bool {
        self.log_internal(can_id, timestamp_us, data, false)
    }

    /// Log a CAN frame with extended ID.
    ///
    /// Use this for 29-bit extended CAN IDs.
    #[inline]
    pub fn log_extended(&mut self, can_id: u32, timestamp_us: u64, data: &[u8]) -> bool {
        self.log_internal(can_id, timestamp_us, data, true)
    }

    /// Log a CAN FD frame (up to 64 bytes).
    ///
    /// This is the same as `log()` but accepts larger data payloads for CAN FD.
    /// The FD flags (BRS/ESI) are not stored since this logger focuses on
    /// decoded signal values rather than raw frame data.
    ///
    /// # Arguments
    /// * `can_id` - The CAN message ID (11-bit or 29-bit)
    /// * `timestamp_us` - Timestamp in microseconds
    /// * `data` - Raw frame data (up to 64 bytes for CAN FD)
    #[inline]
    pub fn log_fd(&mut self, can_id: u32, timestamp_us: u64, data: &[u8]) -> bool {
        self.log_internal(can_id, timestamp_us, data, false)
    }

    /// Log a CAN FD frame with extended ID.
    #[inline]
    pub fn log_fd_extended(&mut self, can_id: u32, timestamp_us: u64, data: &[u8]) -> bool {
        self.log_internal(can_id, timestamp_us, data, true)
    }

    /// Internal logging implementation - zero allocation hot path.
    #[inline]
    fn log_internal(
        &mut self,
        can_id: u32,
        timestamp_us: u64,
        data: &[u8],
        is_extended: bool,
    ) -> bool {
        // O(1) message lookup via FastDbc
        let msg = if is_extended {
            self.fast_dbc.get_extended(can_id)
        } else {
            self.fast_dbc.get(can_id)
        };

        let msg = match msg {
            Some(m) => m,
            None => return false,
        };

        let dbc_id = if is_extended {
            can_id | 0x8000_0000
        } else {
            can_id
        };

        // Zero-allocation decode into pre-allocated buffer
        let decoded_count = if self.config.store_raw_values {
            msg.decode_raw_into(data, &mut self.decode_raw_buf)
        } else {
            msg.decode_into(data, &mut self.decode_buf)
        };

        if decoded_count == 0 {
            return false;
        }

        // Determine the buffer key based on whether this is a multiplexed message
        let buffer_key = if let Some(mux) = self.mux_info.get(&dbc_id) {
            // Get the mux switch value from decoded values
            let mux_value = if self.config.store_raw_values {
                self.decode_raw_buf[mux.switch_index] as u64
            } else {
                self.decode_buf[mux.switch_index] as u64
            };

            if mux.mux_values.contains(&mux_value) {
                (dbc_id, Some(mux_value))
            } else {
                return false; // Unknown mux value
            }
        } else {
            (dbc_id, None)
        };

        let store_raw = self.config.store_raw_values;
        if let Some(buffer) = self.buffers.get_mut(&buffer_key) {
            // Read directly from the shared decode buffers using pre-computed
            // signal indices — no per-frame allocation.
            if store_raw {
                buffer.push_raw_indexed(timestamp_us, &self.decode_raw_buf);
            } else {
                buffer.push_physical_indexed(timestamp_us, &self.decode_buf);
            }
            return true;
        }

        false
    }

    /// Log an embedded-can frame with timestamp.
    #[cfg(feature = "can")]
    #[inline]
    pub fn log_frame<F: embedded_can::Frame>(&mut self, timestamp_us: u64, frame: &F) -> bool {
        match frame.id() {
            embedded_can::Id::Standard(id) => {
                self.log(id.as_raw() as u32, timestamp_us, frame.data())
            }
            embedded_can::Id::Extended(id) => {
                self.log_extended(id.as_raw(), timestamp_us, frame.data())
            }
        }
    }

    /// Log a CAN FD frame using the FdFrame trait.
    ///
    /// This supports frames up to 64 bytes. The FD flags (BRS/ESI) are not
    /// stored since this logger focuses on decoded signal values.
    #[cfg(feature = "can")]
    #[inline]
    pub fn log_fd_frame<F: super::fd::FdFrame>(&mut self, timestamp_us: u64, frame: &F) -> bool {
        match frame.id() {
            embedded_can::Id::Standard(id) => {
                self.log_fd(id.as_raw() as u32, timestamp_us, frame.data())
            }
            embedded_can::Id::Extended(id) => {
                self.log_fd_extended(id.as_raw(), timestamp_us, frame.data())
            }
        }
    }

    /// Flush buffered data to the MDF writer.
    ///
    /// This writes all accumulated CAN data to the MDF file and clears the buffer.
    pub fn flush(&mut self) -> crate::Result<()> {
        if !self.initialized {
            self.initialize_mdf()?;
        }

        // Write data for each buffer key
        for buffer_key in self.buffers.keys().copied().collect::<Vec<_>>() {
            self.write_message_data(buffer_key)?;
        }

        // Clear all buffers
        for buffer in self.buffers.values_mut() {
            buffer.clear();
        }

        Ok(())
    }

    /// Initialize the MDF file structure with full metadata.
    fn initialize_mdf(&mut self) -> crate::Result<()> {
        use crate::DataType;

        self.writer.init_mdf_file()?;

        // Create a channel group for each buffer (message or mux-specific group)
        for (&buffer_key, buffer) in &self.buffers {
            let (can_id, mux_value) = buffer_key;
            let cg = self.writer.add_channel_group(None, |_| {})?;

            // Find the DBC message to get name and sender for channel group metadata
            if let Some(message) = self.fast_dbc.get(can_id) {
                // Set channel group name from DBC message name
                // For multiplexed messages, append "_Mux{value}"
                let msg_name = message.name();
                if !msg_name.is_empty() {
                    let group_name = match mux_value {
                        Some(val) => alloc::format!("{}_Mux{}", msg_name, val),
                        None => String::from(msg_name),
                    };
                    self.writer.set_channel_group_name(&cg, &group_name)?;
                }

                // Set channel group source from DBC message sender (ECU)
                let sender = message.sender();
                if !sender.is_empty() && sender != "Vector__XXX" {
                    self.writer.set_channel_group_source_name(&cg, sender)?;
                }
            }

            // Add timestamp channel
            let time_name = match mux_value {
                Some(val) => alloc::format!("Time_0x{:X}_Mux{}", can_id, val),
                None => alloc::format!("Time_0x{:X}", can_id),
            };
            let time_ch = self.writer.add_channel(&cg, None, |ch| {
                ch.data_type = DataType::UnsignedIntegerLE;
                ch.name = Some(time_name.clone());
                ch.bit_count = 64;
            })?;
            self.writer.set_time_channel(&time_ch)?;
            self.writer.set_channel_unit(&time_ch, "us")?;

            // Add signal channels with full metadata
            let mut prev_ch = time_ch.clone();
            let mut signal_channels = Vec::new();

            for info in &buffer.signals {
                let ch = if self.config.store_raw_values {
                    // Raw value mode: use appropriate integer type
                    self.writer.add_channel(&cg, Some(&prev_ch), |ch| {
                        ch.data_type = info.data_type;
                        ch.name = Some(info.name.clone());
                        ch.bit_count = info.bit_count;
                    })?
                } else {
                    // Physical value mode: use f64
                    self.writer.add_channel(&cg, Some(&prev_ch), |ch| {
                        ch.data_type = DataType::FloatLE;
                        ch.name = Some(info.name.clone());
                        ch.bit_count = 64;
                    })?
                };

                // Add unit if available and enabled
                if self.config.include_units {
                    if let Some(ref unit) = info.unit {
                        self.writer.set_channel_unit(&ch, unit)?;
                    }
                }

                // Add limits if enabled
                if self.config.include_limits && (info.min != 0.0 || info.max != 0.0) {
                    self.writer.set_channel_limits(&ch, info.min, info.max)?;
                }

                // Add conversion block if in raw mode and enabled
                if self.config.store_raw_values {
                    // Check for value descriptions first (they take precedence)
                    let has_value_desc = self.config.include_value_descriptions
                        && self
                            .fast_dbc
                            .dbc()
                            .value_descriptions_for_signal(can_id, &info.name)
                            .is_some();

                    if has_value_desc {
                        // Add ValueToText conversion from DBC value descriptions
                        if let Some(vd) = self
                            .fast_dbc
                            .dbc()
                            .value_descriptions_for_signal(can_id, &info.name)
                        {
                            let mapping: Vec<(i64, &str)> =
                                vd.iter().map(|(v, desc)| (v as i64, desc)).collect();
                            if !mapping.is_empty() {
                                self.writer.add_value_to_text_conversion(
                                    &mapping,
                                    "", // default text for unknown values
                                    Some(&ch),
                                )?;
                            }
                        }
                    } else if self.config.include_conversions {
                        // Fall back to linear conversion if available
                        if let Some(ref conv) = info.conversion {
                            self.writer.set_channel_conversion(&ch, conv)?;
                        }
                    }
                }

                signal_channels.push(ch.clone());
                prev_ch = ch;
            }

            self.channel_groups.insert(buffer_key, cg);
            self.channel_ids.insert(
                buffer_key,
                ChannelIds {
                    time_channel: time_ch,
                    signal_channels,
                },
            );
        }

        self.initialized = true;
        Ok(())
    }

    /// Write data for a specific buffer key (message or mux-specific group).
    fn write_message_data(&mut self, buffer_key: BufferKey) -> crate::Result<()> {
        use crate::DecodedValue;

        let cg = match self.channel_groups.get(&buffer_key) {
            Some(cg) => cg.clone(),
            None => return Ok(()),
        };

        let buffer = match self.buffers.get(&buffer_key) {
            Some(b) if !b.timestamps.is_empty() => b,
            _ => return Ok(()),
        };

        self.writer.start_data_block_for_cg(&cg, 0)?;

        if self.config.store_raw_values {
            // Write raw values
            for (record_idx, &ts) in buffer.timestamps.iter().enumerate() {
                let mut values = alloc::vec![DecodedValue::UnsignedInteger(ts)];

                for (sig_idx, info) in buffer.signals.iter().enumerate() {
                    if record_idx < buffer.raw_values[sig_idx].len() {
                        let raw = buffer.raw_values[sig_idx][record_idx];
                        // Use appropriate integer type based on signedness
                        if info.unsigned {
                            values.push(DecodedValue::UnsignedInteger(raw as u64));
                        } else {
                            values.push(DecodedValue::SignedInteger(raw));
                        }
                    }
                }

                self.writer.write_record(&cg, &values)?;
            }
        } else {
            // Write physical values
            for (record_idx, &ts) in buffer.timestamps.iter().enumerate() {
                let mut values = alloc::vec![DecodedValue::UnsignedInteger(ts)];

                for signal_values in &buffer.physical_values {
                    if record_idx < signal_values.len() {
                        values.push(DecodedValue::Float(signal_values[record_idx]));
                    }
                }

                self.writer.write_record(&cg, &values)?;
            }
        }

        self.writer.finish_data_block(&cg)?;
        Ok(())
    }

    /// Flush and finalize the MDF file.
    fn flush_and_finalize(&mut self) -> crate::Result<()> {
        self.flush()?;
        self.writer.finalize()
    }

    /// Get the number of frames logged for a specific CAN ID.
    ///
    /// For multiplexed messages, this returns the sum of frames across all mux values.
    pub fn frame_count(&self, can_id: u32) -> usize {
        self.buffers
            .iter()
            .filter(|((id, _), _)| *id == can_id)
            .map(|(_, b)| b.frame_count())
            .sum()
    }

    /// Get the number of frames logged for a specific CAN ID and mux value.
    ///
    /// For non-multiplexed messages, use `mux_value = None`.
    pub fn frame_count_mux(&self, can_id: u32, mux_value: Option<u64>) -> usize {
        self.buffers
            .get(&(can_id, mux_value))
            .map(|b| b.frame_count())
            .unwrap_or(0)
    }

    /// Get all unique CAN IDs being logged.
    pub fn can_ids(&self) -> impl Iterator<Item = u32> + '_ {
        let mut ids: BTreeSet<u32> = BTreeSet::new();
        for (id, _) in self.buffers.keys() {
            ids.insert(*id);
        }
        ids.into_iter()
    }

    /// Get the total number of channel groups being logged.
    ///
    /// For multiplexed messages, each mux value is counted as a separate group.
    pub fn channel_group_count(&self) -> usize {
        self.buffers.len()
    }

    /// Get the total number of signals across all channel groups.
    pub fn total_signal_count(&self) -> usize {
        self.buffers.values().map(|b| b.signals.len()).sum()
    }

    /// Check if a CAN ID has multiplexed signals.
    pub fn is_multiplexed(&self, can_id: u32) -> bool {
        self.mux_info.contains_key(&can_id)
    }

    /// Get the mux values for a multiplexed message.
    ///
    /// Returns `None` if the message is not multiplexed.
    pub fn mux_values(&self, can_id: u32) -> Option<impl Iterator<Item = u64> + '_> {
        self.mux_info
            .get(&can_id)
            .map(|info| info.mux_values.iter().copied())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dbc_mdf_logger() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // Log some frames (RPM = 2000, raw = 8000 = 0x1F40)
        let data = [0x40, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
        assert!(logger.log(256, 1000, &data));
        assert!(logger.log(256, 2000, &data));

        assert_eq!(logger.frame_count(256), 2);

        // Finalize and get MDF bytes
        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());

        // Verify MDF header
        assert_eq!(&mdf_bytes[0..3], b"MDF");
    }

    #[test]
    fn test_dbc_mdf_logger_raw_mode() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
 SG_ Temp : 16|8@1- (1,-40) [-40|215] "C" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::builder(dbc)
            .store_raw_values(true)
            .build()
            .unwrap();

        // RPM = 2000 (raw 8000), Temp = 50°C (raw 90)
        let data = [0x40, 0x1F, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00];
        assert!(logger.log(256, 1000, &data));

        assert_eq!(logger.frame_count(256), 1);
        assert_eq!(logger.total_signal_count(), 2);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());
    }

    #[test]
    fn test_dbc_mdf_logger_multiple_signals() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
 SG_ Temp : 16|8@1- (1,-40) [-40|215] "C" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // RPM = 2000 (raw 8000), Temp = 50°C (raw 90)
        let data = [0x40, 0x1F, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00];
        assert!(logger.log(256, 1000, &data));

        assert_eq!(logger.frame_count(256), 1);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());
    }

    #[test]
    fn test_dbc_mdf_logger_unknown_message() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // Try to log unknown message ID
        let data = [0x00; 8];
        assert!(!logger.log(999, 1000, &data));

        assert_eq!(logger.frame_count(999), 0);
    }

    #[test]
    fn test_builder_configuration() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"
BU_:
 BO_ 100 TestMsg: 8 Vector__XXX
 SG_ TestSig : 0|16@1+ (1,0) [0|65535] "units" Vector__XXX
"#,
        )
        .unwrap();

        let logger = CanDbcLogger::builder(dbc)
            .store_raw_values(true)
            .include_units(true)
            .include_limits(true)
            .include_conversions(true)
            .with_capacity(1024)
            .build()
            .unwrap();

        assert!(logger.config().store_raw_values);
        assert!(logger.config().include_units);
        assert!(logger.config().include_limits);
        assert!(logger.config().include_conversions);
    }

    #[test]
    fn test_value_descriptions_to_text() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Transmission : 8 ECM
 SG_ GearPosition : 0|8@1+ (1,0) [0|5] "" Vector__XXX

VAL_ 256 GearPosition 0 "Park" 1 "Reverse" 2 "Neutral" 3 "Drive" 4 "Sport" ;
"#,
        )
        .unwrap();

        // Verify value descriptions are parsed
        let vd = dbc.value_descriptions_for_signal(256, "GearPosition");
        assert!(vd.is_some());
        let vd = vd.unwrap();
        assert_eq!(vd.get(0), Some("Park"));
        assert_eq!(vd.get(3), Some("Drive"));

        // Create logger with raw values and value descriptions
        let mut logger = CanDbcLogger::builder(dbc)
            .store_raw_values(true)
            .include_value_descriptions(true)
            .build()
            .unwrap();

        // Log some gear position changes
        assert!(logger.log(256, 1000, &[0, 0, 0, 0, 0, 0, 0, 0])); // Park
        assert!(logger.log(256, 2000, &[3, 0, 0, 0, 0, 0, 0, 0])); // Drive
        assert!(logger.log(256, 3000, &[2, 0, 0, 0, 0, 0, 0, 0])); // Neutral

        assert_eq!(logger.frame_count(256), 3);
        assert!(logger.config().include_value_descriptions);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());

        // Verify MDF header
        assert_eq!(&mdf_bytes[0..3], b"MDF");
    }

    #[test]
    fn test_channel_group_naming_and_source() {
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM TCM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (1,0) [0|8000] "rpm" Vector__XXX

 BO_ 512 Transmission : 8 TCM
 SG_ Gear : 0|8@1+ (1,0) [0|5] "" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // Log some data
        assert!(logger.log(256, 1000, &[0x00, 0x10, 0, 0, 0, 0, 0, 0]));
        assert!(logger.log(512, 1000, &[3, 0, 0, 0, 0, 0, 0, 0]));

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());

        // The MDF should contain channel groups named after messages
        // and sources named after senders (ECM, TCM)
        // This is verified by the fact that it compiles and runs without errors
    }

    #[test]
    fn test_multiplexed_signals() {
        // DBC with multiplexed signals
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 DiagResponse : 8 ECM
 SG_ ServiceID M : 0|8@1+ (1,0) [0|255] "" Vector__XXX
 SG_ SessionType m16 : 8|8@1+ (1,0) [0|255] "" Vector__XXX
 SG_ DataLength m34 : 8|8@1+ (1,0) [0|255] "" Vector__XXX
 SG_ DataValue m34 : 16|16@1+ (1,0) [0|65535] "" Vector__XXX
 SG_ NormalSignal : 56|8@1+ (1,0) [0|255] "" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // Verify multiplexed message detection
        assert!(logger.is_multiplexed(256));
        let mux_vals: Vec<u64> = logger.mux_values(256).unwrap().collect();
        assert_eq!(mux_vals.len(), 2); // m16 and m34
        assert!(mux_vals.contains(&16));
        assert!(mux_vals.contains(&34));

        // Should have 2 channel groups (one per mux value)
        assert_eq!(logger.channel_group_count(), 2);

        // Log frames with ServiceID=16 (SessionType available)
        // ServiceID=16, SessionType=1, NormalSignal=42
        assert!(logger.log(256, 1000, &[16, 1, 0, 0, 0, 0, 0, 42]));
        assert!(logger.log(256, 2000, &[16, 2, 0, 0, 0, 0, 0, 43]));

        // Log frames with ServiceID=34 (DataLength and DataValue available)
        // ServiceID=34, DataLength=4, DataValue=0x1234, NormalSignal=44
        assert!(logger.log(256, 3000, &[34, 4, 0x34, 0x12, 0, 0, 0, 44]));
        assert!(logger.log(256, 4000, &[34, 8, 0x78, 0x56, 0, 0, 0, 45]));

        // Unknown mux value should not be logged
        assert!(!logger.log(256, 5000, &[99, 0, 0, 0, 0, 0, 0, 0]));

        // Check frame counts per mux value
        assert_eq!(logger.frame_count_mux(256, Some(16)), 2);
        assert_eq!(logger.frame_count_mux(256, Some(34)), 2);
        assert_eq!(logger.frame_count(256), 4); // Total

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());
        assert_eq!(&mdf_bytes[0..3], b"MDF");

        // Write to temp file and read back
        let temp_path = std::env::temp_dir().join("mux_test.mf4");
        std::fs::write(&temp_path, &mdf_bytes).unwrap();

        let mdf = crate::MDF::from_file(temp_path.to_str().unwrap()).unwrap();
        let groups = mdf.channel_groups();

        // Should have 2 channel groups: DiagResponse_Mux16 and DiagResponse_Mux34
        assert_eq!(groups.len(), 2);

        for group in groups.iter() {
            let name = group.name().unwrap().unwrap_or_default();
            assert!(
                name.starts_with("DiagResponse_Mux"),
                "Expected DiagResponse_Mux*, got {}",
                name
            );
        }

        // Cleanup
        std::fs::remove_file(&temp_path).ok();
    }

    #[test]
    fn test_non_multiplexed_unchanged() {
        // Verify non-multiplexed messages still work correctly
        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
 SG_ Temp : 16|8@1+ (1,-40) [-40|215] "C" Vector__XXX
"#,
        )
        .unwrap();

        let mut logger = CanDbcLogger::new(dbc).unwrap();

        // Should not be multiplexed
        assert!(!logger.is_multiplexed(256));
        assert!(logger.mux_values(256).is_none());
        assert_eq!(logger.channel_group_count(), 1);

        // Log frames
        assert!(logger.log(256, 1000, &[0x40, 0x1F, 0x5A, 0, 0, 0, 0, 0]));
        assert!(logger.log(256, 2000, &[0x80, 0x3E, 0x64, 0, 0, 0, 0, 0]));

        assert_eq!(logger.frame_count(256), 2);
        assert_eq!(logger.frame_count_mux(256, None), 2);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());

        // Write to temp file and read back
        let temp_path = std::env::temp_dir().join("non_mux_test.mf4");
        std::fs::write(&temp_path, &mdf_bytes).unwrap();

        let mdf = crate::MDF::from_file(temp_path.to_str().unwrap()).unwrap();
        let groups = mdf.channel_groups();
        assert_eq!(groups.len(), 1);

        let name = groups[0].name().unwrap().unwrap_or_default();
        assert_eq!(name, "Engine");

        // Cleanup
        std::fs::remove_file(&temp_path).ok();
    }

    #[test]
    fn test_streaming_with_flush_policy() {
        use crate::FlushPolicy;

        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
"#,
        )
        .unwrap();

        // Create logger with flush policy
        let mut logger = CanDbcLogger::builder(dbc)
            .with_flush_policy(FlushPolicy::EveryNRecords(10))
            .build()
            .unwrap();

        // Log 25 frames - should trigger 2 auto-flushes (at 10 and 20)
        for i in 0..25 {
            let rpm_raw = (i * 100) as u16;
            let data = rpm_raw.to_le_bytes();
            let mut frame = [0u8; 8];
            frame[0..2].copy_from_slice(&data);
            assert!(logger.log(256, i as u64 * 1000, &frame));
        }

        assert_eq!(logger.frame_count(256), 25);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());
        assert_eq!(&mdf_bytes[0..3], b"MDF");
    }

    #[test]
    fn test_streaming_with_bytes_policy() {
        use crate::FlushPolicy;

        let dbc = dbc_rs::Dbc::parse(
            r#"VERSION "1.0"

BU_: ECM

 BO_ 256 Engine : 8 ECM
 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" Vector__XXX
"#,
        )
        .unwrap();

        // Create logger with byte-based flush policy
        let mut logger = CanDbcLogger::builder(dbc)
            .with_flush_policy(FlushPolicy::EveryNBytes(1024))
            .build()
            .unwrap();

        // Log frames
        for i in 0..100 {
            let rpm_raw = (i * 100) as u16;
            let data = rpm_raw.to_le_bytes();
            let mut frame = [0u8; 8];
            frame[0..2].copy_from_slice(&data);
            assert!(logger.log(256, i as u64 * 1000, &frame));
        }

        assert_eq!(logger.frame_count(256), 100);

        let mdf_bytes = logger.finalize().unwrap();
        assert!(!mdf_bytes.is_empty());
    }
}