fix(protocol): refine extra chunk fetching logic for accurate termination response

This commit is contained in:
2026-04-23 20:30:07 -04:00
parent fa887b85d9
commit ab2c11e9a9
2 changed files with 19 additions and 35 deletions
+10 -22
View File
@@ -652,13 +652,14 @@ class MiniMateProtocol:
# and primes the device to return a valid footer in the termination
# response. Without it, termination returns an empty ack with no
# footer bytes (confirmed 2026-04-23 from HxD comparison).
# Download extra chunks until we hit post-event silence (all-FF
# ADC values) or the cap. The device returns the footer in the
# termination response only when we stop at the right point —
# right after the last real data chunk, before silence starts.
# Silence detection: >80% of payload bytes are 0xFF.
log.debug("5A A5[%d] metadata found — fetching extra chunks until silence",
chunk_num)
# Download extra_chunks_after_metadata more chunks past the
# metadata. The caller calculates this from record_time and
# sample_rate so we download exactly the right amount of ADC
# data — no more, no less — before terminating.
# The device returns the footer in the termination response only
# after the right amount of data has been consumed.
log.debug("5A A5[%d] metadata found — fetching %d more chunk(s)",
chunk_num, extra_chunks_after_metadata)
for _extra_n in range(extra_chunks_after_metadata):
chunk_num += 1
counter = chunk_num * _BULK_COUNTER_STEP
@@ -666,25 +667,12 @@ class MiniMateProtocol:
self._send(build_5a_frame(_BULK_CHUNK_OFFSET, params))
try:
extra = self._recv_one(expected_sub=rsp_sub, timeout=10.0)
payload = extra.data[7:] # skip 7-byte frame header
ff_ratio = payload.count(0xFF) / max(len(payload), 1)
is_silence = ff_ratio > 0.8
log.debug(
"5A A5[%d] extra chunk page_key=0x%04X data_len=%d "
"ff_ratio=%.2f silence=%s",
chunk_num, extra.page_key, len(extra.data),
ff_ratio, is_silence,
)
log.debug("5A A5[%d] extra chunk page_key=0x%04X data_len=%d",
chunk_num, extra.page_key, len(extra.data))
if extra.page_key == 0x0000:
if include_terminator:
frames_data.append(extra)
return frames_data
if is_silence:
# Don't include the silence chunk — terminate here.
# The termination response will contain the footer.
log.debug("5A A5[%d] silence detected — stopping before this chunk",
chunk_num)
break
frames_data.append(extra)
except TimeoutError:
log.debug("5A extra chunk %d timed out — end of stream", _extra_n + 1)