microzig icon indicating copy to clipboard operation
microzig copied to clipboard

Bug: USB packet split issue for 3 or more packets

Open arkadiuszwojcik opened this issue 8 months ago • 0 comments

This is a self-reported bug for my recent code change in the USB driver that introduced chunked data (packet split). The current implementation allows for max of 2 packets of response data, which is enforced by the TMP buffer size of 128 bytes (2 * 64). This is probably enough for all command response cases. However, there is a bug that could manifest if the buffer size is extended in the future to support 3 or more packets.

Short version: Bug is one liner, we should use try_peek instead of try_read when sending next chunk of data

Long version: I reproduced this bug in this branch where last commit is solution, and previous commit is bug introduction. To enforce 3 packets in USB hand shake I extended buffor size and put a lot of redundant data. In first case (without fix) bug is reproduced and Windows generates error on USB hand shake. After fix everything is OK, device is detected correctly. We should use try_peek that returns data to consume and don't advance buffor read cursor, because we do that manualy after we receive confirmation that chunk/packet of data was sent.

I'm sorry for that mistake. I will create PR shortly.

CC: @r4gus

arkadiuszwojcik avatar Jun 09 '24 17:06 arkadiuszwojcik