Skip to content

Commit 8ac9084

Browse files
committed
sys/usb/cdc_acm: Change API to report errors
- Change return type of `usbus_cdc_acm_submit` from `size_t` to `ssize_t` to be able to report errors - Change `stdio_cdc_acm` to make use of the new API - Change `stdio_cdc_acm` to not loop until all data is flushed, but just report what could be added to the buffer now and post the flush event - The layers above should already loop until all data is written - The layers above could opt to not loop when in IRQ context. This would allow using `stdio_usb_cdc_acm` from IRQ context.
1 parent 3a04a1d commit 8ac9084

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

sys/include/usb/usbus/cdc/acm.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242

4343
#include <stdint.h>
44+
#include <unistd.h>
45+
4446
#include "usb/cdc.h"
4547
#include "usb/usbus.h"
4648
#include "tsrb.h"
@@ -198,9 +200,10 @@ void usbus_cdc_acm_init(usbus_t *usbus, usbus_cdcacm_device_t *cdcacm,
198200
* @param[in] buf buffer to submit
199201
* @param[in] len length of the submitted buffer
200202
*
201-
* @return Number of bytes added to the CDC ACM ring buffer
203+
* @retval -ECONNRESET Failed to sub data
204+
* @retval >=0 Number of bytes added to the CDC ACM ring buffer
202205
*/
203-
size_t usbus_cdc_acm_submit(usbus_cdcacm_device_t *cdcacm,
206+
ssize_t usbus_cdc_acm_submit(usbus_cdcacm_device_t *cdcacm,
204207
const uint8_t *buf, size_t len);
205208

206209
/**

sys/usb/usbus/cdc/acm/cdc_acm.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define USB_H_USER_IS_RIOT_INTERNAL
2020

2121
#include <assert.h>
22+
#include <errno.h>
2223
#include <string.h>
2324

2425
#include "tsrb.h"
@@ -148,12 +149,12 @@ static size_t _gen_full_acm_descriptor(usbus_t *usbus, void *arg)
148149
}
149150

150151
/* Submit (ACM interface in) */
151-
size_t usbus_cdc_acm_submit(usbus_cdcacm_device_t *cdcacm, const uint8_t *buf, size_t len)
152+
ssize_t usbus_cdc_acm_submit(usbus_cdcacm_device_t *cdcacm, const uint8_t *buf, size_t len)
152153
{
153-
size_t n;
154+
ssize_t n;
154155
unsigned old;
155156
if (cdcacm->state == USBUS_CDC_ACM_LINE_STATE_DISCONNECTED) {
156-
return len;
157+
return -ECONNRESET;
157158
}
158159

159160
old = irq_disable();

sys/usb/usbus/cdc/acm/cdc_acm_stdio.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <stdio.h>
2525
#include <sys/types.h>
2626

27-
#include "log.h"
2827
#include "isrpipe.h"
2928
#include "stdio_base.h"
3029

@@ -36,15 +35,9 @@ static uint8_t _cdc_tx_buf_mem[CONFIG_USBUS_CDC_ACM_STDIO_BUF_SIZE];
3635

3736
static ssize_t _write(const void* buffer, size_t len)
3837
{
39-
const char *start = buffer;
40-
while (len) {
41-
size_t n = usbus_cdc_acm_submit(&cdcacm, buffer, len);
42-
usbus_cdc_acm_flush(&cdcacm);
43-
/* Use tsrb and flush */
44-
buffer = (char *)buffer + n;
45-
len -= n;
46-
}
47-
return (char *)buffer - start;
38+
ssize_t retval = usbus_cdc_acm_submit(&cdcacm, buffer, len);
39+
usbus_cdc_acm_flush(&cdcacm);
40+
return retval;
4841
}
4942

5043
static void _cdc_acm_rx_pipe(usbus_cdcacm_device_t *cdcacm,

0 commit comments

Comments
 (0)