Skip to content

Commit 11bf12f

Browse files
committed
Fix NFC receive over-wait with explicit min_rx_len
1 parent 1cf5759 commit 11bf12f

6 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/nfc/layer/b/nfc_layer_b.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ bool NFCLayerB::deselect(const uint8_t pupi[4], const uint8_t cid, const uint32_
193193
cmd[1] = cid;
194194
}
195195
uint8_t rx[2 + 2]{}; // payload (1 or 2) + 2 byte CRC_B
196-
uint16_t rx_len =
197-
cmd_len + 2; // Match actual response size to keep wait_for_FIFO fallback equivalent to the old behavior
196+
uint16_t rx_len = sizeof(rx);
198197

199198
if (!transceive(rx, rx_len, cmd, cmd_len, timeout_ms) || rx_len < cmd_len) {
200199
M5_LIB_LOGE("Failed to deselecte %02X:%02X", cmd[0], cmd[1]);

src/unit/unit_ST25R3916.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,11 @@ class UnitST25R3916 : public Component {
17471747
@retval != 0 Upper 16 bits: Number of bits read Lower 16 bits: Number of bytes read
17481748
*/
17491749
uint32_t nfcaTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
1750-
const uint32_t timeout_ms);
1750+
const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
17511751
bool nfcaTransmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms);
17521752
//! @brief Transmit in emulation (PICC) mode with minimal I2C overhead
17531753
bool nfcaEmulationTransmit(const uint8_t* tx, const uint16_t tx_len);
1754-
bool nfcaReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms);
1754+
bool nfcaReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
17551755

17561756
/*!
17571757
@brief Request for idle PICC
@@ -1860,7 +1860,7 @@ class UnitST25R3916 : public Component {
18601860
@return True if successful
18611861
*/
18621862
bool nfcbTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
1863-
const uint32_t timeout_ms);
1863+
const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
18641864
/*!
18651865
@brief Transmit to NFC-B PICC
18661866
@param tx Send buffer
@@ -1876,7 +1876,7 @@ class UnitST25R3916 : public Component {
18761876
@param timeout_ms Timeout(ms)
18771877
@return True if successful
18781878
*/
1879-
bool nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms);
1879+
bool nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
18801880
///@}
18811881

18821882
// ----------------------------------------------------------------------------------------------
@@ -1892,7 +1892,7 @@ class UnitST25R3916 : public Component {
18921892
@return True if successful
18931893
*/
18941894
bool nfcfTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
1895-
const uint32_t timeout_ms);
1895+
const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
18961896
/*!
18971897
@param Transmit to NFC-F PICC
18981898
@param tx Send buffer
@@ -1910,7 +1910,7 @@ class UnitST25R3916 : public Component {
19101910
@param timeout_ms Timeout(ms)
19111911
@return True if successful
19121912
*/
1913-
bool nfcfReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms);
1913+
bool nfcfReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
19141914
///@}
19151915

19161916
// ----------------------------------------------------------------------------------------------
@@ -1929,7 +1929,8 @@ class UnitST25R3916 : public Component {
19291929
*/
19301930
bool nfcvTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_bytes,
19311931
const uint32_t timeout_ms,
1932-
const m5::nfc::v::ModulationMode mode = m5::nfc::v::ModulationMode::OneOf4);
1932+
const m5::nfc::v::ModulationMode mode = m5::nfc::v::ModulationMode::OneOf4,
1933+
const uint16_t min_rx_len = 0);
19331934
/*!
19341935
@param Transmit to NFC-V PICC
19351936
@param tx Send buffer
@@ -1947,7 +1948,7 @@ class UnitST25R3916 : public Component {
19471948
@param timeout_ms Timeout(ms)
19481949
@return True if successful
19491950
*/
1950-
bool nfcvReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms);
1951+
bool nfcvReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len = 0);
19511952
///@}
19521953

19531954
///@name PT_MEMORY

src/unit/unit_ST25R3916_nfca.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,13 @@ bool UnitST25R3916::configure_emulation_a()
178178
}
179179

180180
uint32_t UnitST25R3916::nfcaTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
181-
const uint32_t timeout_ms)
181+
const uint32_t timeout_ms, const uint16_t min_rx_len)
182182
{
183183
if (!nfcaTransmit(tx, tx_len, timeout_ms)) {
184184
M5_LIB_LOGE("nfcaTransmit FAILED tx_len=%u", tx_len);
185185
return false;
186186
}
187-
return nfcaReceive(rx, rx_len, timeout_ms);
187+
return nfcaReceive(rx, rx_len, timeout_ms, min_rx_len);
188188
}
189189

190190
bool UnitST25R3916::nfcaTransmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms)
@@ -215,7 +215,7 @@ bool UnitST25R3916::nfcaEmulationTransmit(const uint8_t* tx, const uint16_t tx_l
215215
writeNumberOfTransmittedBytes(tx_len, 0) && writeDirectCommand(CMD_TRANSMIT_WITH_CRC);
216216
}
217217

218-
bool UnitST25R3916::nfcaReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms)
218+
bool UnitST25R3916::nfcaReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len)
219219
{
220220
CHECK_MODE();
221221

@@ -225,7 +225,7 @@ bool UnitST25R3916::nfcaReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t ti
225225
return false;
226226
}
227227

228-
if (!wait_for_FIFO(timeout_ms, rx_len_org)) {
228+
if (!wait_for_FIFO(timeout_ms, min_rx_len)) {
229229
// M5_LIB_LOGE("nfcaReceive timeout rx_len=%u timeout_ms=%u", rx_len_org, timeout_ms);
230230
M5_LIB_LOGD("Timeout");
231231
return false;
@@ -392,7 +392,7 @@ bool UnitST25R3916::nfcaSelectWithAnticollision(bool& completed, PICC& picc, con
392392

393393
// Select
394394
uint16_t rx_len{3};
395-
if (!nfcaTransceive(rbuf, rx_len, select_frame, sizeof(select_frame), TIMEOUT_SELECT) || rx_len != 3) {
395+
if (!nfcaTransceive(rbuf, rx_len, select_frame, sizeof(select_frame), TIMEOUT_SELECT, 3) || rx_len != 3) {
396396
M5_LIB_LOGD("Failed to select");
397397
return false;
398398
}
@@ -461,7 +461,7 @@ bool UnitST25R3916::nfcaSelect(const PICC& picc)
461461

462462
// Select
463463
uint16_t rx_len{3};
464-
if (!nfcaTransceive(rbuf, rx_len, select_frame, sizeof(select_frame), TIMEOUT_SELECT) || rx_len != 3) {
464+
if (!nfcaTransceive(rbuf, rx_len, select_frame, sizeof(select_frame), TIMEOUT_SELECT, 3) || rx_len != 3) {
465465
M5_LIB_LOGD("Failed to select");
466466
return false;
467467
}
@@ -514,7 +514,7 @@ bool UnitST25R3916::nfcaReadBlock(uint8_t rx[16], const uint8_t addr)
514514
if (_encrypted) {
515515
return mifare_classic_transceive_encrypt(rx, rx_len, cmd, sizeof(cmd), TIMEOUT_READ, true, true);
516516
}
517-
return nfcaTransceive(rx, rx_len, cmd, sizeof(cmd), TIMEOUT_READ);
517+
return nfcaTransceive(rx, rx_len, cmd, sizeof(cmd), TIMEOUT_READ, 16);
518518
}
519519

520520
bool UnitST25R3916::nfcaWriteBlock(const uint8_t addr, const uint8_t tx[16])
@@ -553,9 +553,9 @@ bool UnitST25R3916::nfcaWriteBlock(const uint8_t addr, const uint8_t tx[16])
553553
}
554554

555555
//
556-
if (nfcaTransceive(rx, rx_len, cmd, sizeof(cmd), TIMEOUT_WRITE1) && rx[0] == ACK_NIBBLE) {
556+
if (nfcaTransceive(rx, rx_len, cmd, sizeof(cmd), TIMEOUT_WRITE1, 1) && rx[0] == ACK_NIBBLE) {
557557
rx_len = 1;
558-
if (nfcaTransceive(rx, rx_len, buf, sizeof(buf), TIMEOUT_WRITE2) && rx[0] == ACK_NIBBLE) {
558+
if (nfcaTransceive(rx, rx_len, buf, sizeof(buf), TIMEOUT_WRITE2, 1) && rx[0] == ACK_NIBBLE) {
559559
return true;
560560
}
561561
}
@@ -702,7 +702,7 @@ bool UnitST25R3916::mifare_classic_authenticate(const Command cmd, const PICC& p
702702
return false;
703703
}
704704
} else {
705-
if (!nfcaTransceive(RB, rlen, auth_frame, sizeof(auth_frame), TIMEOUT_AUTH1)) {
705+
if (!nfcaTransceive(RB, rlen, auth_frame, sizeof(auth_frame), TIMEOUT_AUTH1, 4)) {
706706
M5_LIB_LOGD("Failed to send AUTH1(plain) %u", rlen);
707707
return false;
708708
}

src/unit/unit_ST25R3916_nfcb.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ bool UnitST25R3916::configure_nfc_b()
147147
#endif
148148

149149
bool UnitST25R3916::nfcbTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
150-
const uint32_t timeout_ms)
150+
const uint32_t timeout_ms, const uint16_t min_rx_len)
151151
{
152-
return nfcbTransmit(tx, tx_len, timeout_ms) && nfcbReceive(rx, rx_len, timeout_ms);
152+
return nfcbTransmit(tx, tx_len, timeout_ms) && nfcbReceive(rx, rx_len, timeout_ms, min_rx_len);
153153
}
154154

155155
bool UnitST25R3916::nfcbTransmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms)
@@ -172,7 +172,7 @@ bool UnitST25R3916::nfcbTransmit(const uint8_t* tx, const uint16_t tx_len, const
172172
}
173173

174174
// Always with CRC_B
175-
bool UnitST25R3916::nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms)
175+
bool UnitST25R3916::nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len)
176176
{
177177
CHECK_MODE();
178178

@@ -182,7 +182,7 @@ bool UnitST25R3916::nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t ti
182182
return false;
183183
}
184184

185-
#if 0
185+
#if 0
186186
uint8_t rbuf[256]{};
187187
if (!wait_for_FIFO(timeout_ms, sizeof(rbuf))) {
188188
M5_LIB_LOGD("Timeout");
@@ -197,7 +197,7 @@ bool UnitST25R3916::nfcbReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t ti
197197
rx_len = std::min<uint16_t>(actual, rx_len_org);
198198
memcpy(rx, rbuf, rx_len);
199199
#else
200-
if (!wait_for_FIFO(timeout_ms, rx_len_org)) {
200+
if (!wait_for_FIFO(timeout_ms, min_rx_len)) {
201201
M5_LIB_LOGD("Timeout");
202202
return false;
203203
}

src/unit/unit_ST25R3916_nfcf.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ bool UnitST25R3916::configure_emulation_f()
7777
}
7878

7979
bool UnitST25R3916::nfcfTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
80-
const uint32_t timeout_ms)
80+
const uint32_t timeout_ms, const uint16_t min_rx_len)
8181
{
82-
return nfcfTransmit(tx, tx_len, timeout_ms) && nfcfReceive(rx, rx_len, timeout_ms);
82+
return nfcfTransmit(tx, tx_len, timeout_ms) && nfcfReceive(rx, rx_len, timeout_ms, min_rx_len);
8383
}
8484

8585
bool UnitST25R3916::nfcfTransmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms)
@@ -111,7 +111,7 @@ bool UnitST25R3916::nfcfEmulationTransmit(const uint8_t* tx, const uint16_t tx_l
111111
writeNumberOfTransmittedBytes(tx_len, 0) && writeDirectCommand(CMD_TRANSMIT_WITH_CRC);
112112
}
113113

114-
bool UnitST25R3916::nfcfReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms)
114+
bool UnitST25R3916::nfcfReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len)
115115
{
116116
CHECK_MODE();
117117

@@ -122,7 +122,7 @@ bool UnitST25R3916::nfcfReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t ti
122122
return false;
123123
}
124124

125-
if (!wait_for_FIFO(timeout_ms, rx_len_org)) {
125+
if (!wait_for_FIFO(timeout_ms, min_rx_len)) {
126126
M5_LIB_LOGD("Timeout");
127127
return false;
128128
}

src/unit/unit_ST25R3916_nfcv.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ bool UnitST25R3916::configure_nfc_v()
8888
}
8989

9090
bool UnitST25R3916::nfcvTransceive(uint8_t* rx, uint16_t& rx_len, const uint8_t* tx, const uint16_t tx_len,
91-
const uint32_t timeout_ms, const ModulationMode mode)
91+
const uint32_t timeout_ms, const ModulationMode mode, const uint16_t min_rx_len)
9292
{
9393
if (!nfcvTransmit(tx, tx_len, timeout_ms, mode)) {
9494
return false;
9595
}
96-
return nfcvReceive(rx, rx_len, timeout_ms);
96+
return nfcvReceive(rx, rx_len, timeout_ms, min_rx_len);
9797
}
9898

9999
bool UnitST25R3916::nfcvTransmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms,
@@ -129,7 +129,7 @@ bool UnitST25R3916::nfcvTransmit(const uint8_t* tx, const uint16_t tx_len, const
129129
return is_irq32_txe(irq);
130130
}
131131

132-
bool UnitST25R3916::nfcvReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms)
132+
bool UnitST25R3916::nfcvReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t timeout_ms, const uint16_t min_rx_len)
133133
{
134134
const auto rx_len_org = rx_len;
135135
rx_len = 0;
@@ -140,7 +140,7 @@ bool UnitST25R3916::nfcvReceive(uint8_t* rx, uint16_t& rx_len, const uint32_t ti
140140
CHECK_MODE();
141141

142142
uint8_t rbuf[256]{};
143-
if (!wait_for_FIFO(timeout_ms, sizeof(rbuf))) {
143+
if (!wait_for_FIFO(timeout_ms, min_rx_len)) {
144144
return false;
145145
}
146146
uint16_t actual{};

0 commit comments

Comments
 (0)