Fix streaming bug affecting mostly the pm5

Bug triggered with:
lf read -s 50000
hw ping

ReadLF_realtime() in lfsampling.c never knew how many samples the client wanted — for the realtime path, lf_sample_payload_t.samples was never populated on the client side, and the ARM handler in appmain.c (CMD_LF_ACQ_RAW_ADC/CMD_LF_SNIFF_RAW_ADC) ignored it anyway. So the device just streamed samples indefinitely, only checking for a stop request roughly once per 64-sample USB burst, and even after noticing it, flushed one more trailing partial burst via async_usb_write_stop().

Meanwhile the client (WaitForRawDataTimeout in comms.c) stops listening in raw mode the instant it has collected exactly the number of bytes it asked for (e.g. 50000), then switches back to normal framed-packet parsing. Since the device kept sending more bytes past that point, those extra bytes sat unread in the pipe and got misinterpreted as a PacketResponseNGPreamble/OLD-frame header, corrupting every subsequent USB exchange (hw ping, etc.) — reproducible even with a plain lf read -s 50000, no trigger/COTAG needed.

Moreover, we make sure the client doesn't stop listening, even briefly, during stream reception.
This commit is contained in:
Philippe Teuwen committed 2026-08-26 20:16:46 +02:00
1 parent 727a7c879f
commit 7fb8927523
6 files changed
+37 -18

No files matched your search

+8 -1
View File
@@ -455,7 +455,7 @@ uint32_t SampleLF(bool verbose, uint32_t sample_size, bool ledcontrol, bool cota
* @param reader_field - true for reading tags, false for sniffing
* @return sampling result
**/
int ReadLF_realtime(bool reader_field, bool cotag) {
int ReadLF_realtime(bool reader_field, bool cotag, uint32_t sample_limit) {
// parameters from config and constants
const uint8_t bits_per_sample = config.bits_per_sample;
const int16_t trigger_threshold = config.trigger_threshold;
@@ -469,6 +469,8 @@ int ReadLF_realtime(bool reader_field, bool cotag) {
uint8_t last_byte = 0;
uint8_t curr_byte = 0;
int return_value = PM3_SUCCESS;
// Total samples streamed so far
uint32_t total_logged = 0;
uint32_t usb_buffer_len = 0, sample_buffer_len;
usb_get_ep_size(NULL, &usb_buffer_len, NULL);
@@ -536,6 +538,7 @@ int ReadLF_realtime(bool reader_field, bool cotag) {
curr_byte = data.numbits >> 3;
if (curr_byte > last_byte) {
async_usb_write_pushByte(data.buffer[last_byte]);
total_logged++;
}
last_byte = curr_byte;
@@ -558,6 +561,10 @@ int ReadLF_realtime(bool reader_field, bool cotag) {
break;
}
}
if (sample_limit > 0 && total_logged >= sample_limit) {
break;
}
}
}