-
Notifications
You must be signed in to change notification settings - Fork 2.1k
sys/usb/cdc_acm: fix garbage input when disconnected #21890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Does this fix #20544 ? |
It certainly fixes #20305, I tried it with a Feather-M0. I'll try to check with an nRF52840DK tomorrow. Behavior with Behavior with this PR: I had to create an UF2 file because the Feather-M0 and WSL don't really play together that well. |
|
It might therefore also fix #21231. |
|
No, it does not fix #21231: |
I remembered that I have a Seeedstudio Xiao nRF52840 Sense laying around, which is also based on the nRF52840 as is the Arduino Nano 33 BLE. Behavior on Behavior with this PR: |
|
This cannot easily be backported, as the thread unsafe ring buffer did not make it into the release. Maybe we should just leave this for next release? Or should we port this to the old state of the CDC ACM driver? |
| if (cdcacm->state == USBUS_CDC_ACM_LINE_STATE_DISCONNECTED) { | ||
| return len; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this rather return 0, as no bytes were actually added to the ring buffer? See
RIOT/sys/include/usb/usbus/cdc/acm.h
Lines 194 to 204 in bcd09d3
| /** | |
| * @brief Submit bytes to the CDC ACM handler | |
| * | |
| * @param[in] cdcacm USBUS CDC ACM handler context | |
| * @param[in] buf buffer to submit | |
| * @param[in] len length of the submitted buffer | |
| * | |
| * @return Number of bytes added to the CDC ACM ring buffer | |
| */ | |
| size_t usbus_cdc_acm_submit(usbus_cdcacm_device_t *cdcacm, | |
| const uint8_t *buf, size_t len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would cause problems with stdio, as stdio_write() is expected to either return the number of bytes written in absence of an error, or an error if writing bytes won't work.
If it would return 0, the stdio implementation on top would just keep calling stdio_write() in a loop until all data has been written - which would be an endless loop here.
We should either change the doc to "number of bytes handled" with discarding being handled, or change the return type to ssize_t and return an error when discarding data.
Discarding the data here and not reporting an error doesn't really change the API contract, btw. Even if we would go for ssize_t, getting a success return code is no guarantee that the data will be transferred to the Linux host: They may have been successfully added to the ringbuffer and subsequently a disconnect may happen before the data left the ringbuffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I think I'd favor a telling error code over a silent drop. "bytes handled" does not really tell the user much.
Contribution description
usbus_cdc_acmtries to buffer output while disconnected, but fails hard at this.Testing procedure
Enable the
stdio_cdc_acmon any application that comes with a shell.I added an output of the received data to
cdc_acm_stdio.c:master
with this patch
Issues/PRs references
Fixes #20305.
Fixes #20544.