Skip to content

Instantly share code, notes, and snippets.

@jia200x
Created Sep 27, 2019
Embed
What would you like to do?
diff --git a/drivers/at86rf2xx/at86rf2xx_netdev.c b/drivers/at86rf2xx/at86rf2xx_netdev.c
index c988ac72b..d20de8e9e 100644
--- a/drivers/at86rf2xx/at86rf2xx_netdev.c
+++ b/drivers/at86rf2xx/at86rf2xx_netdev.c
@@ -33,6 +33,7 @@
#include "net/ieee802154.h"
#include "net/netdev.h"
#include "net/netdev/ieee802154.h"
+#include "net/netbuf.h"
#include "at86rf2xx.h"
#include "at86rf2xx_netdev.h"
@@ -122,9 +123,11 @@ static int _send(netdev_t *netdev, const iolist_t *iolist)
static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
{
+ (void) len;
at86rf2xx_t *dev = (at86rf2xx_t *)netdev;
uint8_t phr;
size_t pkt_len;
+ netbuf_t *netbuf = buf;
/* frame buffer protection will be unlocked as soon as at86rf2xx_fb_stop() is called,
* Set receiver to PLL_ON state to be able to free the SPI bus and avoid loosing data. */
@@ -139,23 +142,9 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
/* ignore MSB (refer p.80) and substract length of FCS field */
pkt_len = (phr & 0x7f) - 2;
- /* return length when buf == NULL */
- if (buf == NULL) {
- /* release SPI bus */
- at86rf2xx_fb_stop(dev);
-
- /* drop packet, continue receiving */
- if (len > 0) {
- /* set device back in operation state which was used before last transmission.
- * This state is saved in at86rf2xx.c/at86rf2xx_tx_prepare() e.g RX_AACK_ON */
- at86rf2xx_set_state(dev, dev->idle_state);
- }
-
- return pkt_len;
- }
-
/* not enough space in buf */
- if (pkt_len > len) {
+ uint8_t *rx_buf = netbuf_alloc(netbuf, pkt_len);
+ if (!rx_buf) {
at86rf2xx_fb_stop(dev);
/* set device back in operation state which was used before last transmission.
* This state is saved in at86rf2xx.c/at86rf2xx_tx_prepare() e.g RX_AACK_ON */
@@ -163,7 +152,7 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
return -ENOBUFS;
}
/* copy payload */
- at86rf2xx_fb_read(dev, (uint8_t *)buf, pkt_len);
+ at86rf2xx_fb_read(dev, (uint8_t *)rx_buf, pkt_len);
/* Ignore FCS but advance fb read - we must give a temporary buffer here,
* as we are not allowed to issue SPI transfers without any buffer */
diff --git a/sys/net/gnrc/netif/ieee802154/gnrc_netif_ieee802154.c b/sys/net/gnrc/netif/ieee802154/gnrc_netif_ieee802154.c
index 0bb39366b..ae2ae5e8b 100644
--- a/sys/net/gnrc/netif/ieee802154/gnrc_netif_ieee802154.c
+++ b/sys/net/gnrc/netif/ieee802154/gnrc_netif_ieee802154.c
@@ -16,6 +16,7 @@
#include "net/gnrc.h"
#include "net/gnrc/netif/ieee802154.h"
#include "net/netdev/ieee802154.h"
+#include "net/netbuf.h"
#ifdef MODULE_GNRC_IPV6
#include "net/ipv6/hdr.h"
@@ -92,126 +93,119 @@ static inline bool _already_received(gnrc_netif_t *netif,
}
#endif /* MODULE_GNRC_NETIF_DEDUP */
+void *netbuf_stack_alloc(netbuf_t *netbuf, size_t size)
+{
+ gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, NULL, size, GNRC_NETTYPE_UNDEF);
+ if(!pkt) {
+ return NULL;
+ }
+ netbuf->ctx = pkt;
+ netbuf->data = pkt->data;
+ netbuf->size = pkt->size;
+ return pkt->data;
+}
+
static gnrc_pktsnip_t *_recv(gnrc_netif_t *netif)
{
netdev_t *dev = netif->dev;
netdev_ieee802154_rx_info_t rx_info;
- gnrc_pktsnip_t *pkt = NULL;
- int bytes_expected = dev->driver->recv(dev, NULL, 0, NULL);
- if (bytes_expected >= (int)IEEE802154_MIN_FRAME_LEN) {
- int nread;
+ netbuf_t netbuf;
+ netbuf_init(&netbuf, NULL, 0);
+ int nread = dev->driver->recv(dev, &netbuf, 0, &rx_info);
+ gnrc_pktsnip_t *pkt = netbuf.ctx;
- pkt = gnrc_pktbuf_add(NULL, NULL, bytes_expected, GNRC_NETTYPE_UNDEF);
- if (pkt == NULL) {
- DEBUG("_recv_ieee802154: cannot allocate pktsnip.\n");
- /* Discard packet on netdev device */
- dev->driver->recv(dev, NULL, bytes_expected, NULL);
- return NULL;
- }
- nread = dev->driver->recv(dev, pkt->data, bytes_expected, &rx_info);
- if (nread <= 0) {
- gnrc_pktbuf_release(pkt);
- return NULL;
- }
#ifdef MODULE_NETSTATS_L2
- netif->stats.rx_count++;
- netif->stats.rx_bytes += nread;
+ netif->stats.rx_count++;
+ netif->stats.rx_bytes += nread;
#endif
- if (netif->flags & GNRC_NETIF_FLAGS_RAWMODE) {
- /* Raw mode, skip packet processing, but provide rx_info via
- * GNRC_NETTYPE_NETIF */
- gnrc_pktsnip_t *netif_snip = gnrc_netif_hdr_build(NULL, 0, NULL, 0);
- if (netif_snip == NULL) {
- DEBUG("_recv_ieee802154: no space left in packet buffer\n");
- gnrc_pktbuf_release(pkt);
- return NULL;
- }
- gnrc_netif_hdr_t *hdr = netif_snip->data;
- hdr->lqi = rx_info.lqi;
- hdr->rssi = rx_info.rssi;
- hdr->if_pid = netif->pid;
- LL_APPEND(pkt, netif_snip);
+ if (netif->flags & GNRC_NETIF_FLAGS_RAWMODE) {
+ /* Raw mode, skip packet processing, but provide rx_info via
+ * GNRC_NETTYPE_NETIF */
+ gnrc_pktsnip_t *netif_snip = gnrc_netif_hdr_build(NULL, 0, NULL, 0);
+ if (netif_snip == NULL) {
+ DEBUG("_recv_ieee802154: no space left in packet buffer\n");
+ gnrc_pktbuf_release(pkt);
+ return NULL;
}
- else {
- /* Normal mode, try to parse the frame according to IEEE 802.15.4 */
- gnrc_pktsnip_t *ieee802154_hdr, *netif_hdr;
- gnrc_netif_hdr_t *hdr;
+ gnrc_netif_hdr_t *hdr = netif_snip->data;
+ hdr->lqi = rx_info.lqi;
+ hdr->rssi = rx_info.rssi;
+ hdr->if_pid = netif->pid;
+ LL_APPEND(pkt, netif_snip);
+ }
+ else {
+ /* Normal mode, try to parse the frame according to IEEE 802.15.4 */
+ gnrc_pktsnip_t *ieee802154_hdr, *netif_hdr;
+ gnrc_netif_hdr_t *hdr;
#if ENABLE_DEBUG
- char src_str[GNRC_NETIF_HDR_L2ADDR_PRINT_LEN];
+ char src_str[GNRC_NETIF_HDR_L2ADDR_PRINT_LEN];
#endif
- size_t mhr_len = ieee802154_get_frame_hdr_len(pkt->data);
+ size_t mhr_len = ieee802154_get_frame_hdr_len(pkt->data);
- /* nread was checked for <= 0 before so we can safely cast it to
- * unsigned */
- if ((mhr_len == 0) || ((size_t)nread < mhr_len)) {
- DEBUG("_recv_ieee802154: illegally formatted frame received\n");
- gnrc_pktbuf_release(pkt);
- return NULL;
- }
- nread -= mhr_len;
- /* mark IEEE 802.15.4 header */
- ieee802154_hdr = gnrc_pktbuf_mark(pkt, mhr_len, GNRC_NETTYPE_UNDEF);
- if (ieee802154_hdr == NULL) {
- DEBUG("_recv_ieee802154: no space left in packet buffer\n");
- gnrc_pktbuf_release(pkt);
- return NULL;
- }
- netif_hdr = _make_netif_hdr(ieee802154_hdr->data);
- if (netif_hdr == NULL) {
- DEBUG("_recv_ieee802154: no space left in packet buffer\n");
- gnrc_pktbuf_release(pkt);
- return NULL;
- }
+ /* nread was checked for <= 0 before so we can safely cast it to
+ * unsigned */
+ if ((mhr_len == 0) || ((size_t)nread < mhr_len)) {
+ DEBUG("_recv_ieee802154: illegally formatted frame received\n");
+ gnrc_pktbuf_release(pkt);
+ return NULL;
+ }
+ nread -= mhr_len;
+ /* mark IEEE 802.15.4 header */
+ ieee802154_hdr = gnrc_pktbuf_mark(pkt, mhr_len, GNRC_NETTYPE_UNDEF);
+ if (ieee802154_hdr == NULL) {
+ DEBUG("_recv_ieee802154: no space left in packet buffer\n");
+ gnrc_pktbuf_release(pkt);
+ return NULL;
+ }
+ netif_hdr = _make_netif_hdr(ieee802154_hdr->data);
+ if (netif_hdr == NULL) {
+ DEBUG("_recv_ieee802154: no space left in packet buffer\n");
+ gnrc_pktbuf_release(pkt);
+ return NULL;
+ }
- hdr = netif_hdr->data;
+ hdr = netif_hdr->data;
#ifdef MODULE_L2FILTER
- if (!l2filter_pass(dev->filter, gnrc_netif_hdr_get_src_addr(hdr),
- hdr->src_l2addr_len)) {
- gnrc_pktbuf_release(pkt);
- gnrc_pktbuf_release(netif_hdr);
- DEBUG("_recv_ieee802154: packet dropped by l2filter\n");
- return NULL;
- }
+ if (!l2filter_pass(dev->filter, gnrc_netif_hdr_get_src_addr(hdr),
+ hdr->src_l2addr_len)) {
+ gnrc_pktbuf_release(pkt);
+ gnrc_pktbuf_release(netif_hdr);
+ DEBUG("_recv_ieee802154: packet dropped by l2filter\n");
+ return NULL;
+ }
#endif
#ifdef MODULE_GNRC_NETIF_DEDUP
- if (_already_received(netif, hdr, ieee802154_hdr->data)) {
- gnrc_pktbuf_release(pkt);
- gnrc_pktbuf_release(netif_hdr);
- DEBUG("_recv_ieee802154: packet dropped by deduplication\n");
- return NULL;
- }
- memcpy(netif->last_pkt.src, gnrc_netif_hdr_get_src_addr(hdr),
- hdr->src_l2addr_len);
- netif->last_pkt.src_len = hdr->src_l2addr_len;
- netif->last_pkt.seq = ieee802154_get_seq(ieee802154_hdr->data);
+ if (_already_received(netif, hdr, ieee802154_hdr->data)) {
+ gnrc_pktbuf_release(pkt);
+ gnrc_pktbuf_release(netif_hdr);
+ DEBUG("_recv_ieee802154: packet dropped by deduplication\n");
+ return NULL;
+ }
+ memcpy(netif->last_pkt.src, gnrc_netif_hdr_get_src_addr(hdr),
+ hdr->src_l2addr_len);
+ netif->last_pkt.src_len = hdr->src_l2addr_len;
+ netif->last_pkt.seq = ieee802154_get_seq(ieee802154_hdr->data);
#endif /* MODULE_GNRC_NETIF_DEDUP */
- hdr->lqi = rx_info.lqi;
- hdr->rssi = rx_info.rssi;
- hdr->if_pid = thread_getpid();
- dev->driver->get(dev, NETOPT_PROTO, &pkt->type, sizeof(pkt->type));
+ hdr->lqi = rx_info.lqi;
+ hdr->rssi = rx_info.rssi;
+ hdr->if_pid = thread_getpid();
+ dev->driver->get(dev, NETOPT_PROTO, &pkt->type, sizeof(pkt->type));
#if ENABLE_DEBUG
- DEBUG("_recv_ieee802154: received packet from %s of length %u\n",
- gnrc_netif_addr_to_str(gnrc_netif_hdr_get_src_addr(hdr),
- hdr->src_l2addr_len,
- src_str),
- nread);
+ DEBUG("_recv_ieee802154: received packet from %s of length %u\n",
+ gnrc_netif_addr_to_str(gnrc_netif_hdr_get_src_addr(hdr),
+ hdr->src_l2addr_len,
+ src_str),
+ nread);
#if defined(MODULE_OD)
- od_hex_dump(pkt->data, nread, OD_WIDTH_DEFAULT);
+ od_hex_dump(pkt->data, nread, OD_WIDTH_DEFAULT);
#endif
#endif
- gnrc_pktbuf_remove_snip(pkt, ieee802154_hdr);
- LL_APPEND(pkt, netif_hdr);
- }
-
- DEBUG("_recv_ieee802154: reallocating.\n");
- gnrc_pktbuf_realloc_data(pkt, nread);
- } else if (bytes_expected > 0) {
- DEBUG("_recv_ieee802154: received frame is too short\n");
- dev->driver->recv(dev, NULL, bytes_expected, NULL);
+ gnrc_pktbuf_remove_snip(pkt, ieee802154_hdr);
+ LL_APPEND(pkt, netif_hdr);
}
return pkt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment