From 70d480c301a891fb063bb51da99b05be1f876d80 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 18 Sep 2020 21:00:16 +0200 Subject: [PATCH] sys/usb/usbus/cdc_ecm_netdev: fix _recv() return values Previously, the function would always return the max_len parameter. This poses two issues: 1. the API requires to return the actual packet size 2. the API requires that if the packet is larger than max_len, the packet is flushed and -ENOBUFS is returned 3. this basically bypasses the packet flushing, consequtive _recv() would return the last packet again This commit fixes those issues. --- sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c b/sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c index 766e97a45e..b2d2ced4c4 100644 --- a/sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c +++ b/sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c @@ -128,19 +128,23 @@ static int _send(netdev_t *netdev, const iolist_t *iolist) static int _recv(netdev_t *netdev, void *buf, size_t max_len, void *info) { + (void)info; usbus_cdcecm_device_t *cdcecm = _netdev_to_cdcecm(netdev); - (void)info; + size_t pktlen = cdcecm->len; + if (max_len == 0 && buf == NULL) { - return cdcecm->len; + return pktlen; } if (max_len && buf == NULL) { _signal_rx_flush(cdcecm); - return cdcecm->len; + return pktlen; + } + if (pktlen <= max_len) { + memcpy(buf, cdcecm->in_buf, pktlen); } - memcpy(buf, cdcecm->in_buf, max_len); _signal_rx_flush(cdcecm); - return max_len; + return (pktlen <= max_len) ? (int)pktlen : -ENOBUFS; } static int _init(netdev_t *netdev)