Skip to content

Commit b43749a

Browse files
committed
usb-device-cdc: Fix lost data in read() path if short reads happened.
If the CDC receive buffer was full and some code read less than 64 bytes (wMaxTransferSize), the CDC code would submit an OUT transfer with N<64 bytes length to fill the buffer back up. However if the host had more than N bytes to send then it would still send the full 64 bytes (correctly) in the transfer. The remaining (64-N) bytes would be lost. Adds the restriction that CDCInterface rxbuf has to be at least 64 bytes. Closes micropython#885. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 50ed36f commit b43749a

File tree

1 file changed

+7
-3
lines changed
  • micropython/usb/usb-device-cdc/usb/device

1 file changed

+7
-3
lines changed

micropython/usb/usb-device-cdc/usb/device/cdc.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def init(
144144
if flow != 0:
145145
raise NotImplementedError # UART flow control currently not supported
146146

147-
if not (txbuf and rxbuf):
148-
raise ValueError # Buffer sizes are required
147+
if not (txbuf and rxbuf >= _BULK_EP_LEN):
148+
raise ValueError # Buffer sizes are required, rxbuf must be at least one EP
149149

150150
self._timeout = timeout
151151
self._wb = Buffer(txbuf)
@@ -330,7 +330,11 @@ def _wr_cb(self, ep, res, num_bytes):
330330
def _rd_xfer(self):
331331
# Keep an active data OUT transfer to read data from the host,
332332
# whenever the receive buffer has room for new data
333-
if self.is_open() and not self.xfer_pending(self.ep_d_out) and self._rb.writable():
333+
if (
334+
self.is_open()
335+
and not self.xfer_pending(self.ep_d_out)
336+
and self._rb.writable() >= _BULK_EP_LEN
337+
):
334338
# Can only submit up to the endpoint length per transaction, otherwise we won't
335339
# get any transfer callback until the full transaction completes.
336340
self.submit_xfer(self.ep_d_out, self._rb.pend_write(_BULK_EP_LEN), self._rd_cb)

0 commit comments

Comments
 (0)