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
use byteorder::{ByteOrder, LittleEndian};
use crate::blocks::common::BlockHeader;
use crate::blocks::common::BlockParse;
use crate::blocks::channel_block::ChannelBlock;
use crate::error::MdfError;
#[derive(Debug)]
pub struct ChannelGroupBlock {
pub header: BlockHeader, // Common header
pub next_cg_addr: u64, // 8 bytes
pub first_ch_addr: u64, // 8 bytes
pub acq_name_addr: u64, // 8 bytes
pub acq_source_addr: u64, // 8 bytes
pub first_sample_reduction_addr: u64, // 8 bytes
pub comment_addr: u64, // 8 bytes
pub record_id: u64, // 8 bytes
pub cycles_nr: u64, // 8 bytes
pub flags: u16, // 2 bytes
pub path_separator: u16, // 2 bytes
pub reserved1: u32, // 4 bytes
pub samples_byte_nr: u32, // 4 bytes
pub invalidation_bytes_nr: u32, // 4 bytes
}
impl BlockParse<'_> for ChannelGroupBlock {
const ID: &'static str = "##CG";
/// Creates a ChannelGroupBlock from a 104-byte slice.
fn from_bytes(bytes: &[u8]) -> Result<Self, MdfError> {
let header = Self::parse_header(bytes)?;
let expected_bytes = 104;
if bytes.len() < expected_bytes {
return Err(MdfError::TooShortBuffer {
actual: bytes.len(),
expected: expected_bytes,
file: file!(),
line: line!(),
});
}
Ok(Self {
header: header,
next_cg_addr: LittleEndian::read_u64(&bytes[24..32]),
first_ch_addr: LittleEndian::read_u64(&bytes[32..40]),
acq_name_addr: LittleEndian::read_u64(&bytes[40..48]),
acq_source_addr: LittleEndian::read_u64(&bytes[48..56]),
first_sample_reduction_addr: LittleEndian::read_u64(&bytes[56..64]),
comment_addr: LittleEndian::read_u64(&bytes[64..72]),
record_id: LittleEndian::read_u64(&bytes[72..80]),
cycles_nr: LittleEndian::read_u64(&bytes[80..88]),
flags: LittleEndian::read_u16(&bytes[88..90]),
path_separator: LittleEndian::read_u16(&bytes[90..92]),
reserved1: LittleEndian::read_u32(&bytes[92..96]),
samples_byte_nr: LittleEndian::read_u32(&bytes[96..100]),
invalidation_bytes_nr: LittleEndian::read_u32(&bytes[100..104]),
})
}
}
impl ChannelGroupBlock {
/// Serializes the ChannelGroupBlock to bytes according to MDF 4.1 specification.
///
/// # Structure (104 bytes total):
/// - BlockHeader (24 bytes): Standard block header with id="##CG"
/// - Link section (48 bytes): Six 8-byte links to other blocks
/// * next_cg_addr: Link to next channel group block
/// * first_ch_addr: Link to first channel block
/// * acq_name_addr: Link to acquisition name text block
/// * acq_source_addr: Link to acquisition source block
/// * first_sample_reduction_addr: Link to first sample reduction block
/// * comment_addr: Link to comment text block
/// - Data section (32 bytes): Information about the channel group data
/// * record_id: Record ID (u64)
/// * cycles_nr: Number of cycles (u64)
/// * flags: Flags (u16)
/// * path_separator: Path separator character (u16)
/// * reserved1: Reserved space (u32)
/// * samples_byte_nr: Number of bytes for samples (u32)
/// * invalidation_bytes_nr: Number of bytes for invalidation bits (u32)
///
/// # Returns
/// - `Ok(Vec<u8>)` containing the serialized channel group block
/// - `Err(MdfError)` if serialization fails
pub fn to_bytes(&self) -> Result<Vec<u8>, MdfError> {
// Validate the header before serializing
if self.header.id != "##CG" {
return Err(MdfError::BlockSerializationError(
format!("ChannelGroupBlock must have ID '##CG', found '{}'", self.header.id)
));
}
if self.header.block_len != 104 {
return Err(MdfError::BlockSerializationError(
format!("ChannelGroupBlock must have block_len=104, found {}", self.header.block_len)
));
}
// Create a buffer with exact capacity for efficiency
let mut buffer = Vec::with_capacity(104);
// 1. Write the block header (24 bytes)
buffer.extend_from_slice(&self.header.to_bytes()?);
// 2. Write the link addresses (48 bytes total, 6 links at 8 bytes each)
buffer.extend_from_slice(&self.next_cg_addr.to_le_bytes()); // Next channel group
buffer.extend_from_slice(&self.first_ch_addr.to_le_bytes()); // First channel
buffer.extend_from_slice(&self.acq_name_addr.to_le_bytes()); // Acquisition name
buffer.extend_from_slice(&self.acq_source_addr.to_le_bytes()); // Acquisition source
buffer.extend_from_slice(&self.first_sample_reduction_addr.to_le_bytes()); // Sample reduction
buffer.extend_from_slice(&self.comment_addr.to_le_bytes()); // Comment
// 3. Write the data section (32 bytes)
buffer.extend_from_slice(&self.record_id.to_le_bytes()); // Record ID (8 bytes)
buffer.extend_from_slice(&self.cycles_nr.to_le_bytes()); // Cycles count (8 bytes)
buffer.extend_from_slice(&self.flags.to_le_bytes()); // Flags (2 bytes)
buffer.extend_from_slice(&self.path_separator.to_le_bytes()); // Path separator (2 bytes)
buffer.extend_from_slice(&self.reserved1.to_le_bytes()); // Reserved (4 bytes)
buffer.extend_from_slice(&self.samples_byte_nr.to_le_bytes()); // Sample bytes (4 bytes)
buffer.extend_from_slice(&self.invalidation_bytes_nr.to_le_bytes()); // Invalidation bytes (4 bytes)
// Verify the buffer is exactly 104 bytes
if buffer.len() != 104 {
return Err(MdfError::BlockSerializationError(
format!("ChannelGroupBlock must be exactly 104 bytes, got {}", buffer.len())
));
}
// Ensure 8-byte alignment (should always be true since 104 is divisible by 8)
debug_assert_eq!(buffer.len() % 8, 0, "ChannelGroupBlock size is not 8-byte aligned");
Ok(buffer)
}
/// Read all channels linked to this channel group.
///
/// # Arguments
/// * `mmap` - Memory mapped MDF data used to follow the channel chain.
///
/// # Returns
/// A vector of fully parsed [`ChannelBlock`]s or an [`MdfError`] if any
/// channel cannot be decoded.
pub fn read_channels(&mut self, mmap: &[u8]) -> Result<Vec<ChannelBlock>, MdfError> {
let mut channels = Vec::new();
let mut current_ch_addr = self.first_ch_addr;
while current_ch_addr != 0 {
let ch_offset = current_ch_addr as usize;
let mut channel = ChannelBlock::from_bytes(&mmap[ch_offset..])?;
channel.resolve_conversion(mmap)?;
current_ch_addr = channel.next_ch_addr;
channels.push(channel);
}
Ok(channels)
}
}
impl Default for ChannelGroupBlock {
fn default() -> Self {
let header = BlockHeader {
id: String::from("##CG"),
reserved0: 0,
block_len: 104,
links_nr: 6,
};
ChannelGroupBlock {
header,
next_cg_addr: 0,
first_ch_addr: 0,
acq_name_addr: 0,
acq_source_addr: 0,
first_sample_reduction_addr: 0,
comment_addr: 0,
record_id: 0,
cycles_nr: 0,
flags: 0,
path_separator: 0,
reserved1: 0,
samples_byte_nr: 0,
invalidation_bytes_nr: 0,
}
}
}