From 683bd6e10ee73ce0d56ed13b91dc9fbe0dda4c79 Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Tue, 20 Jun 2017 12:57:24 -0400 Subject: [PATCH 1/3] ieee802154: Adds some missing elements to start support for beacon-enabled networks --- drivers/wireless/ieee802154/mrf24j40.c | 235 +++- drivers/wireless/ieee802154/mrf24j40.h | 39 + .../wireless/ieee802154/ieee802154_mac.h | 41 +- .../wireless/ieee802154/ieee802154_radio.h | 20 + wireless/ieee802154/mac802154.c | 235 +++- wireless/ieee802154/mac802154_getset.c | 7 + wireless/ieee802154/mac802154_internal.h | 1031 +++++++++-------- wireless/ieee802154/mac802154_reset.c | 18 +- wireless/ieee802154/mac802154_start.c | 67 +- 9 files changed, 1106 insertions(+), 587 deletions(-) diff --git a/drivers/wireless/ieee802154/mrf24j40.c b/drivers/wireless/ieee802154/mrf24j40.c index 6d6f814066..2a553c9cb3 100644 --- a/drivers/wireless/ieee802154/mrf24j40.c +++ b/drivers/wireless/ieee802154/mrf24j40.c @@ -103,6 +103,24 @@ #define MRF24J40_GTS_SLOTS 2 +/* Clock configuration macros */ + +#define MRF24J40_SLPCLKPER_100KHZ ((1000 * 1000 * 1000)/100000) /* 10ns */ +#define MRF24J40_SLPCLKPER_32KHZ ((1000 * 1000 * 1000)/32000) /* 31.25ns */ + +#define MRF24J40_BEACONINTERVAL_NSEC(beaconorder) \ + (IEEE802154_BASE_SUPERFRAME_DURATION * (1 << beaconorder) * (16 *1000)) + +/* For now I am just setting the REMCNT to the maximum while staying in multiples + * of 10000 (100khz period) */ + +#define MRF24J40_REMCNT 60000 +#define MRF24J40_REMCNT_NSEC (MRF24J40_REMCNT * 50) + +#define MRF24J40_MAINCNT(bo, clkper) \ + ((MRF24J40_BEACONINTERVAL_NSEC(bo) - MRF24J40_REMCNT_NSEC) / \ + clkper) + /* Formula for calculating default macMaxFrameWaitTime is on pg. 130 * * For PHYs other than CSS and UWB, the attribute phyMaxFrameDuration is given by: @@ -197,12 +215,12 @@ static int mrf24j40_interrupt(int irq, FAR void *context, FAR void *arg); static void mrf24j40_dopoll_csma(FAR void *arg); static void mrf24j40_dopoll_gts(FAR void *arg); -static int mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, +static void mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, FAR struct iob_s *frame, bool csma); -static int mrf24j40_gts_setup(FAR struct mrf24j40_radio_s *dev, uint8_t gts, +static void mrf24j40_gts_setup(FAR struct mrf24j40_radio_s *dev, uint8_t gts, FAR struct iob_s *frame); -static int mrf24j40_setup_fifo(FAR struct mrf24j40_radio_s *dev, - FAR struct iob_s *frame, uint32_t fifo_addr); +static void mrf24j40_setup_fifo(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *buf, uint8_t length, uint32_t fifo_addr); static inline void mrf24j40_norm_trigger(FAR struct mrf24j40_radio_s *dev); @@ -242,6 +260,12 @@ static int mrf24j40_set_attr(FAR struct ieee802154_radio_s *radio, static int mrf24j40_rxenable(FAR struct ieee802154_radio_s *dev, bool enable); static int mrf24j40_req_rxenable(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_rxenable_req_s *req); +static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sf_spec, + FAR struct ieee802154_beaconframe_s *beacon); +static int mrf24j40_beaconupdate(FAR struct ieee802154_radio_s *radio, + FAR struct ieee802154_beaconframe_s *beacon); +static int mrf24j40_beaconstop(FAR struct ieee802154_radio_s *radio); /**************************************************************************** * Private Data @@ -518,6 +542,150 @@ static int mrf24j40_req_rxenable(FAR struct ieee802154_radio_s *radio, return -ENOTTY; } +static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sf_spec, + FAR struct ieee802154_beaconframe_s *beacon) +{ + FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)radio; + uint32_t maincnt = 0; + uint32_t slpcal = 0; + int reg; + + if (sf_spec->pancoord) + { + /* Set the PANCOORD (RXMCR 0x00<3>) bit = 1to configure as PAN coordinator */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_RXMCR); + reg |= MRF24J40_RXMCR_PANCOORD; + mrf24j40_setreg(dev->spi, MRF24J40_RXMCR, reg); + + /* Set the SLOTTED (TXMCR 0x11<5>) bit = 1 to use Slotted CSMA-CA mode */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_TXMCR); + reg |= MRF24J40_TXMCR_SLOTTED; + mrf24j40_setreg(dev->spi, MRF24J40_TXMCR, reg); + + /* Load the beacon frame into the TXBFIFO (0x080-0x0FF). */ + + mrf24j40_setup_fifo(dev, beacon->bf_data, beacon->bf_len, MRF24J40_BEACON_FIFO); + + /* Set the TXBMSK (TXBCON1 0x25<7>) bit = 1 to mask the beacon interrupt + * mask + */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_TXBCON1); + reg |= MRF24J40_TXBCON1_TXBMSK; + mrf24j40_setreg(dev->spi, MRF24J40_TXBCON1, reg); + + /* Set INTL (WAKECON 0x22<5:0>) value to 0x03. */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_WAKECON); + reg &= ~MRF24J40_WAKECON_INTL; + reg |= 0x03 & MRF24J40_WAKECON_INTL; + mrf24j40_setreg(dev->spi, MRF24J40_WAKECON, reg); + + /* Program the CAP end slot (ESLOTG1 0x13<3:0>) value. */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_ESLOTG1); + reg &= ~MRF24J40_ESLOTG1_CAP; + reg |= sf_spec->final_capslot & MRF24J40_ESLOTG1_CAP; + mrf24j40_setreg(dev->spi, MRF24J40_ESLOTG1, reg); + + /* TODO: Add GTS related code. See pg 100 of datasheet */ + + /* Calibrate the Sleep Clock (SLPCLK) frequency. Refer to Section 3.15.1.2 + * “Sleep Clock Calibration”. + */ + + /* If the Sleep Clock Selection, SLPCLKSEL (0x207<7:6), is the internal + * oscillator (100 kHz), set SLPCLKDIV to a minimum value of 0x01. + */ + + mrf24j40_setreg(dev->spi, MRF24J40_SLPCON1, 0x01); + + /* Select the source of SLPCLK (internal 100kHz) */ + + mrf24j40_setreg(dev->spi, MRF24J40_RFCON7, MRF24J40_RFCON7_SEL_100KHZ); + + /* Begin calibration by setting the SLPCALEN bit (SLPCAL2 0x20B<4>) to + * ‘1’. Sixteen samples of the SLPCLK are counted and stored in the + * SLPCAL register. No need to mask, this is the only writable bit + */ + + mrf24j40_setreg(dev->spi, MRF24J40_SLPCAL2, MRF24J40_SLPCAL2_SLPCALEN); + + /* Calibration is complete when the SLPCALRDY bit (SLPCAL2 0x20B<7>) is + * set to ‘1’. + */ + + while (!(mrf24j40_getreg(dev->spi, MRF24J40_SLPCAL2) & + MRF24J40_SLPCAL2_SLPCALRDY)) + { + usleep(1); + } + + slpcal = mrf24j40_getreg(dev->spi, MRF24J40_SLPCAL0); + slpcal |= (mrf24j40_getreg(dev->spi, MRF24J40_SLPCAL1) << 8); + slpcal |= ((mrf24j40_getreg(dev->spi, MRF24J40_SLPCAL2) << 16) & 0x0F); + + /* Set WAKECNT (SLPACK 0x35<6:0>) value = 0x5F to set the main oscillator + * (20 MHz) start-up timer value. + */ + + mrf24j40_setreg(dev->spi, MRF24J40_SLPACK, 0x5F); + + /* Program the Beacon Interval into the Main Counter, MAINCNT (0x229<1:0>, + * 0x228, 0x227, 0x226), and Remain Counter, REMCNT (0x225, 0x224), + * according to BO and SO values. Refer to Section 3.15.1.3 “Sleep Mode + * Counters” + */ + + mrf24j40_setreg(dev->spi, MRF24J40_REMCNTL, (MRF24J40_REMCNT & 0xFF)); + mrf24j40_setreg(dev->spi, MRF24J40_REMCNTH, ((MRF24J40_REMCNT >> 8) & 0xFF)); + + maincnt = MRF24J40_MAINCNT(sf_spec->beaconorder, (slpcal * 50 / 16)); + + mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT0, (maincnt & 0xFF)); + mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT1, ((maincnt >> 8) & 0xFF)); + mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT2, ((maincnt >> 16) & 0xFF)); + mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT3, ((maincnt >> 24) & 0x03)); + + /* Enable the SLPIF and WAKEIF flags */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_INTCON); + reg &= ~(MRF24J40_INTCON_SLPIE | MRF24J40_INTCON_WAKEIE); + mrf24j40_setreg(dev->spi, MRF24J40_INTCON, reg); + + /* Configure the BO (ORDER 0x10<7:4>) and SO (ORDER 0x10<3:0>) values. + * After configuring BO and SO, the beacon frame will be sent immediately. + */ + + mrf24j40_setreg(dev->spi, MRF24J40_ORDER, + ((sf_spec->beaconorder << 4) & 0xF0) | (sf_spec->sforder & 0x0F)); + } + else + { + return -ENOTTY; + } + + return OK; +} + +static int mrf24j40_beaconupdate(FAR struct ieee802154_radio_s *radio, + FAR struct ieee802154_beaconframe_s *beacon) +{ + FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)radio; + + mrf24j40_setup_fifo(dev, beacon->bf_data, beacon->bf_len, MRF24J40_BEACON_FIFO); + + return OK; +} + +static int mrf24j40_beaconstop(FAR struct ieee802154_radio_s *radio) +{ + return -ENOTTY; +} + /**************************************************************************** * Internal Functions ****************************************************************************/ @@ -1303,11 +1471,10 @@ static int mrf24j40_energydetect(FAR struct mrf24j40_radio_s *dev, * ****************************************************************************/ -static int mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, - FAR struct iob_s *frame, bool csma) +static void mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, + FAR struct iob_s *frame, bool csma) { uint8_t reg; - int ret; /* Enable tx int */ @@ -1332,7 +1499,7 @@ static int mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, /* Setup the FIFO */ - ret = mrf24j40_setup_fifo(dev, frame, MRF24J40_TXNORM_FIFO); + mrf24j40_setup_fifo(dev, frame->io_data, frame->io_len, MRF24J40_TXNORM_FIFO); /* If the frame control field contains an acknowledgment request, set the * TXNACKREQ bit. See IEEE 802.15.4/2003 7.2.1.1 page 112 for info. @@ -1350,8 +1517,6 @@ static int mrf24j40_norm_setup(FAR struct mrf24j40_radio_s *dev, } mrf24j40_setreg(dev->spi, MRF24J40_TXNCON, reg); - - return ret; } /**************************************************************************** @@ -1381,10 +1546,10 @@ static inline void mrf24j40_norm_trigger(FAR struct mrf24j40_radio_s *dev) * ****************************************************************************/ -static int mrf24j40_gts_setup(FAR struct mrf24j40_radio_s *dev, uint8_t fifo, - FAR struct iob_s *frame) +static void mrf24j40_gts_setup(FAR struct mrf24j40_radio_s *dev, uint8_t fifo, + FAR struct iob_s *frame) { - return -ENOTTY; + } /**************************************************************************** @@ -1394,17 +1559,18 @@ static int mrf24j40_gts_setup(FAR struct mrf24j40_radio_s *dev, uint8_t fifo, * ****************************************************************************/ -static int mrf24j40_setup_fifo(FAR struct mrf24j40_radio_s *dev, - FAR struct iob_s *frame, uint32_t fifo_addr) +static void mrf24j40_setup_fifo(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *buf, uint8_t length, + uint32_t fifo_addr) { - int ret; - int hlen = 3; /* Include frame control and seq number */ + int hlen = 3; /* Include frame control and seq number */ + int i; uint16_t frame_ctrl; /* Analyze frame control to compute header length */ - frame_ctrl = frame->io_data[0]; - frame_ctrl |= (frame->io_data[1] << 8); + frame_ctrl = buf[0]; + frame_ctrl |= (buf[1] << 8); if ((frame_ctrl & IEEE802154_FRAMECTRL_DADDR)== IEEE802154_ADDRMODE_SHORT) { @@ -1435,16 +1601,14 @@ static int mrf24j40_setup_fifo(FAR struct mrf24j40_radio_s *dev, /* Frame length */ - mrf24j40_setreg(dev->spi, fifo_addr++, frame->io_len); + mrf24j40_setreg(dev->spi, fifo_addr++, length); /* Frame data */ - for (ret = 0; ret < frame->io_len; ret++) /* this sets the correct val for ret */ + for (i = 0; i < length; i++) { - mrf24j40_setreg(dev->spi, fifo_addr++, frame->io_data[ret]); + mrf24j40_setreg(dev->spi, fifo_addr++, buf[i]); } - - return ret; } /**************************************************************************** @@ -1723,7 +1887,8 @@ done: static void mrf24j40_irqworker(FAR void *arg) { FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)arg; - uint8_t intstat, intcon; + uint8_t intstat; + uint8_t reg; DEBUGASSERT(dev); DEBUGASSERT(dev->spi); @@ -1749,9 +1914,9 @@ static void mrf24j40_irqworker(FAR void *arg) /* Timers are one-shot, so disable the interrupt */ - intcon = mrf24j40_getreg(dev->spi, MRF24J40_INTCON); - intcon |= MRF24J40_INTCON_HSYMTMRIE; - mrf24j40_setreg(dev->spi, MRF24J40_INTCON, intcon); + reg = mrf24j40_getreg(dev->spi, MRF24J40_INTCON); + reg |= MRF24J40_INTCON_HSYMTMRIE; + mrf24j40_setreg(dev->spi, MRF24J40_INTCON, reg); } if ((intstat & MRF24J40_INTSTAT_RXIF)) @@ -1782,6 +1947,17 @@ static void mrf24j40_irqworker(FAR void *arg) mrf24j40_irqwork_txgts(dev, 1); } + if ((intstat & MRF24J40_INTSTAT_SLPIF)) + { + dev->radiocb->sfevent(dev->radiocb, IEEE802154_SFEVENT_ENDOFACTIVE); + + /* Acknowledge the alert and put the device to sleep */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_SLPACK); + reg |= MRF24J40_SLPACK_SLPACK; + mrf24j40_setreg(dev->spi, MRF24J40_SLPACK, reg); + } + /* Unlock the radio device */ sem_post(&dev->exclsem); @@ -1878,6 +2054,9 @@ FAR struct ieee802154_radio_s *mrf24j40_init(FAR struct spi_dev_s *spi, dev->radio.set_attr = mrf24j40_set_attr; dev->radio.rxenable = mrf24j40_rxenable; dev->radio.req_rxenable = mrf24j40_req_rxenable; + dev->radio.beaconstart = mrf24j40_beaconstart; + dev->radio.beaconupdate = mrf24j40_beaconupdate; + dev->radio.beaconstop = mrf24j40_beaconstop; dev->lower = lower; dev->spi = spi; diff --git a/drivers/wireless/ieee802154/mrf24j40.h b/drivers/wireless/ieee802154/mrf24j40.h index 397b9f4e39..9d5cd070ff 100644 --- a/drivers/wireless/ieee802154/mrf24j40.h +++ b/drivers/wireless/ieee802154/mrf24j40.h @@ -226,4 +226,43 @@ #define MRF24J40_TXSTAT_X_SHIFT 6 #define MRF24J40_TXSTAT_X_MASK (3 << MRF24J40_TXSTAT_X_SHIFT) +/* TXBCON1 bits */ + +#define MRF24J40_TXBCON1_RSSINUM 0x30 +#define MRF24J40_TXBCON1_NWU_BCN 0x40 +#define MRF24J40_TXBCON1_TXBMSK 0x80 + +/* WAKECON bits */ + +#define MRF24J40_WAKECON_INTL 0x3F +#define MRF24J40_WAKECON_REGWAKE 0x40 +#define MRF24J40_WAKECON_IMMWAKE 0x80 + +/* WAKECON bits */ + +#define MRF24J40_WAKECON_INTL 0x3F +#define MRF24J40_WAKECON_REGWAKE 0x40 +#define MRF24J40_WAKECON_IMMWAKE 0x80 + +/* ESLOTG1 bits */ + +#define MRF24J40_ESLOTG1_CAP 0x0F +#define MRF24J40_ESLOTG1_GTS1 0xF0 + +/* SLPCAL2 bits */ + +#define MRF24J40_SLPCAL2_SLPCAL 0x0F +#define MRF24J40_SLPCAL2_SLPCALEN 0x10 +#define MRF24J40_SLPCAL2_SLPCALRDY 0x80 + +/* RFCON7 bits */ + +#define MRF24J40_RFCON7_SEL_32KHZ 0x40 +#define MRF24J40_RFCON7_SEL_100KHZ 0x80 + +/* SLPACK bits */ + +#define MRF24J40_SLPACK_WAKECNT0_6 0x7F +#define MRF24J40_SLPACK_SLPACK 0x80 + #endif /* __DRIVERS_WIRELESS_IEEE802154_MRF24J40_H */ diff --git a/include/nuttx/wireless/ieee802154/ieee802154_mac.h b/include/nuttx/wireless/ieee802154/ieee802154_mac.h index e128bdc1ab..e2198d54cc 100644 --- a/include/nuttx/wireless/ieee802154/ieee802154_mac.h +++ b/include/nuttx/wireless/ieee802154/ieee802154_mac.h @@ -160,6 +160,24 @@ #define IEEE802154_FRAMECTRL_SHIFT_VERSION 12 /* Source addressing mode, bits 12-13 */ #define IEEE802154_FRAMECTRL_SHIFT_SADDR 14 /* Source addressing mode, bits 14-15 */ +/* Superframe Specification field masks, 2 bytes + * Seee IEEE 802.15.4/2011 5.2.2.1.2 page 62 + */ + +#define IEEE802154_SFSPEC_BEACONORDER 0x000F /* Beacon order, bits 0-3 */ +#define IEEE802154_SFSPEC_SFORDER 0x00F0 /* Superframe Order, bit 4-7 */ +#define IEEE802154_SFSPEC_FINCAPSLOT 0x0F00 /* Final CAP Slot, bit 8-11 */ +#define IEEE802154_SFSPEC_BLE 0x1000 /* Battery Life Ext, bit 12 */ +#define IEEE802154_SFSPEC_PANCOORD 0x4000 /* PAN Coordinator, bit 14 */ +#define IEEE802154_SFSPEC_ASSOCPERMIT 0x8000 /* Association Permit, bit 15 */ + +#define IEEE802154_SFSPEC_SHIFT_BEACONORDER 0 /* Beacon order, bits 0-3 */ +#define IEEE802154_SFSPEC_SHIFT_SFORDER 4 /* Superframe order, bit 4-7 */ +#define IEEE802154_SFSPEC_SHIFT_FINCAPSLOT 8 /* Final CAP Slot, bit 8-11 */ +#define IEEE802154_SFSPEC_SHIFT_BLE 12 /* Battery Life Ext, bit 12 */ +#define IEEE802154_SFSPEC_SHIFT_PANCOORD 14 /* PAN Coordinator, bit 14 */ +#define IEEE802154_SFSPEC_SHIFT_ASSOCPERMIT 15 /* Association Permit, bit 15 */ + /* Capability Information Bitfield * */ @@ -203,7 +221,6 @@ #define IEEE802154_MAX_MPDU_UNSEC_OVERHEAD \ (IEEE802154_MAX_UNSEC_MHR_OVERHEAD + IEEE802154_MFR_LENGTH) - #define IEEE802154_MAX_SAFE_MAC_PAYLOAD_SIZE \ (IEEE802154_MAX_PHY_PACKET_SIZE - IEEE802154_MAX_MPDU_UNSEC_OVERHEAD) @@ -511,15 +528,15 @@ struct ieee802154_capability_info_s * 0=otherwise */ }; -struct ieee802154_superframe_spec_s +struct ieee802154_superframespec_s { - uint16_t beacon_order : 4; /* Transmission interval of beacon */ - uint16_t superframe_order : 4; /* Length of superframe */ - uint16_t final_cap_slot : 4; /* Last slot utilized by CAP */ - uint16_t ble : 1; /* Battery Life Extension (BLE) */ - uint16_t reserved : 1; /* Reserved bit */ - uint16_t pan_coordinator : 1; /* 1 if beacon sent by pan coordinator */ - uint16_t assoc_permit : 1; /* 1 if coordinator is accepting associaton */ + uint16_t beaconorder : 4; /* Transmission interval of beacon */ + uint16_t sforder : 4; /* Length of active portion of superframe */ + uint16_t final_capslot : 4; /* Last slot utilized by CAP */ + uint16_t ble : 1; /* Battery Life Extension (BLE) */ + uint16_t reserved : 1; /* Reserved bit */ + uint16_t pancoord : 1; /* 1 if beacon sent by pan coordinator */ + uint16_t assocpermit : 1; /* 1 if coordinator is accepting associaton */ }; struct ieee802154_pan_desc_s @@ -533,7 +550,7 @@ struct ieee802154_pan_desc_s /* The superframe specifications received in the beacon frame */ - struct ieee802154_superframe_spec_s superframe_spec; + struct ieee802154_superframespec_s superframespec; uint8_t gts_permit; /* 0=No GTS requests allowed * 1=GTS request allowed */ @@ -580,7 +597,7 @@ union ieee802154_macattr_u enum ieee802154_devmode_e devmode; bool is_assoc; - bool assoc_permit; + bool assocpermit; bool auto_req; bool batt_life_ext; bool gts_permit; @@ -611,7 +628,7 @@ union ieee802154_macattr_u uint8_t beacon_order; uint32_t beacon_tx_time : 24; - uint8_t superframe_order; + uint8_t superframeorder; uint8_t bsn; uint8_t dsn; diff --git a/include/nuttx/wireless/ieee802154/ieee802154_radio.h b/include/nuttx/wireless/ieee802154/ieee802154_radio.h index 7e9cd14653..fe183a614a 100644 --- a/include/nuttx/wireless/ieee802154/ieee802154_radio.h +++ b/include/nuttx/wireless/ieee802154/ieee802154_radio.h @@ -89,6 +89,18 @@ struct ieee802154_txdesc_s /* TODO: Add slotting information for GTS transactions */ }; +struct ieee802154_beaconframe_s +{ + uint8_t bf_data[IEEE802154_MAX_PHY_PACKET_SIZE]; + uint8_t bf_len; + uint8_t bf_offset; +}; + +enum ieee802154_sfevent_e +{ + IEEE802154_SFEVENT_ENDOFACTIVE, +}; + /* IEEE802.15.4 Radio Interface Operations **********************************/ struct ieee802154_radiocb_s @@ -99,6 +111,8 @@ struct ieee802154_radiocb_s FAR struct ieee802154_txdesc_s *tx_desc); CODE void (*rxframe) (FAR const struct ieee802154_radiocb_s *radiocb, FAR struct ieee802154_data_ind_s *ind); + CODE void (*sfevent) (FAR const struct ieee802154_radiocb_s *radiocb, + enum ieee802154_sfevent_e sfevent); }; struct ieee802154_radio_s @@ -119,6 +133,12 @@ struct ieee802154_radio_s CODE int (*rxenable) (FAR struct ieee802154_radio_s *radio, bool enable); CODE int (*req_rxenable)(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_rxenable_req_s *req); + CODE int (*beaconstart)(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sf_spec, + FAR struct ieee802154_beaconframe_s *beacon); + CODE int (*beaconupdate)(FAR struct ieee802154_radio_s *radio, + FAR struct ieee802154_beaconframe_s *beacon); + CODE int (*beaconstop)(FAR struct ieee802154_radio_s *radio); }; #ifdef __cplusplus diff --git a/wireless/ieee802154/mac802154.c b/wireless/ieee802154/mac802154.c index af54acbaf6..0cf0a735c0 100644 --- a/wireless/ieee802154/mac802154.c +++ b/wireless/ieee802154/mac802154.c @@ -86,10 +86,8 @@ static void mac802154_rxframe(FAR const struct ieee802154_radiocb_s *radiocb, FAR struct ieee802154_data_ind_s *ind); static void mac802154_rxframe_worker(FAR void *arg); -static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_data_ind_s *ind); -static void mac802154_rx_dataframe(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_data_ind_s *ind); +static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, + enum ieee802154_sfevent_e sfevent); static void mac802154_purge_worker(FAR void *arg); @@ -100,6 +98,11 @@ static void mac802154_timeout_expiry(int argc, wdparm_t arg, ...); static uint32_t mac802154_symtoticks(FAR struct ieee802154_privmac_s *priv, uint32_t symbols); +static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind); +static void mac802154_rx_dataframe(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind); + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -353,6 +356,163 @@ void mac802154_createdatareq(FAR struct ieee802154_privmac_s *priv, txdesc->frame = iob; } +/**************************************************************************** + * Name: mac802154_updatebeacon + * + * Description: + * This function is called in the following scenarios: + * - The MAC receives a START.request primitive + * - Upon receiving the IEEE802154_SFEVENT_ENDOFACTIVE event from the + * radio layer, the MAC checks the bf_update flag and if set calls this + * function. The bf_update flag is set when various attributes that + * effect the beacon are updated. + * + * Internal function used by various parts of the MAC layer. This function + * uses the various MAC attributes to update the beacon frame. It loads the + * inactive beacon frame structure and then notifies the radio layer of the + * new frame. the provided tx descriptor in the indirect list and manages the + * scheduling for purging the transaction if it does not get extracted in + * time. + * + * Assumptions: + * Called with the MAC locked + * + ****************************************************************************/ + +void mac802154_updatebeacon(FAR struct ieee802154_privmac_s *priv) +{ + FAR struct ieee802154_txdesc_s *txdesc; + uint8_t pendaddrspec_ind; + uint8_t pendeaddr = 0; + uint8_t pendsaddr = 0; + + /* Switch the buffer */ + + priv->bf_ind = !priv->bf_ind; + + /* Get a local reference to the beacon frame */ + + FAR struct ieee802154_beaconframe_s *beacon = &priv->beaconframe[priv->bf_ind]; + + /* Clear the frame control fields */ + + beacon->bf_data[0] = 0; + beacon->bf_data[1] = 0; + beacon->bf_len = 2; + + IEEE802154_SETFTYPE(beacon->bf_data, 0, IEEE802154_FRAME_BEACON); + + /* Check if there is a broadcast message pending, if there is, we must set + * the frame pending bit to 1. + */ + + /* TODO: handle broadcast frame */ + + DEBUGASSERT(priv->addr.mode != IEEE802154_ADDRMODE_NONE); + + IEEE802154_SETDADDRMODE(beacon->bf_data, 0, IEEE802154_ADDRMODE_NONE); + IEEE802154_SETSADDRMODE(beacon->bf_data, 0, priv->addr.mode); + IEEE802154_SETVERSION(beacon->bf_data, 0, 1); + + /* Copy in and increment the beacon sequence number */ + + beacon->bf_data[beacon->bf_len++] = priv->bsn++; + + IEEE802154_PANIDCOPY(&beacon->bf_data[beacon->bf_len], priv->addr.panid); + beacon->bf_len += IEEE802154_PANIDSIZE; + + if (priv->addr.mode == IEEE802154_ADDRMODE_SHORT) + { + IEEE802154_SADDRCOPY(&beacon->bf_data[beacon->bf_len], priv->addr.saddr); + beacon->bf_len += IEEE802154_SADDRSIZE; + } + else + { + IEEE802154_EADDRCOPY(&beacon->bf_data[beacon->bf_len], priv->addr.eaddr); + beacon->bf_len += IEEE802154_EADDRSIZE; + } + + /* Clear the superframe specification, then set the appropriate bits */ + + beacon->bf_data[beacon->bf_len] = 0; + beacon->bf_data[beacon->bf_len + 1] = 0; + + IEEE802154_SETBEACONORDER(beacon->bf_data, beacon->bf_len, + priv->sf_spec.beaconorder); + IEEE802154_SETSFORDER(beacon->bf_data, beacon->bf_len, + priv->sf_spec.sforder); + IEEE802154_SETFINCAPSLOT(beacon->bf_data, beacon->bf_len, + priv->sf_spec.final_capslot); + if (priv->sf_spec.ble) + { + IEEE802154_SETBLE(beacon->bf_data, beacon->bf_len); + } + if (priv->sf_spec.pancoord) + { + IEEE802154_SETPANCOORD(beacon->bf_data, beacon->bf_len); + } + if (priv->sf_spec.assocpermit) + { + IEEE802154_SETASSOCPERMIT(beacon->bf_data, beacon->bf_len); + } + + beacon->bf_len += 2; + + /* TODO: Handle GTS properly, for now, we just set the descriptor count to + * zero and specify that we do not permit GTS requests */ + + beacon->bf_data[beacon->bf_len++] = 0; + + /* TODO: Add GTS List here */ + + /* Skip the pending address specification field for now */ + + pendaddrspec_ind = beacon->bf_len++; + + txdesc = (FAR struct ieee802154_txdesc_s *)sq_peek(&priv->indirect_queue); + + while(txdesc != NULL) + { + if (txdesc->destaddr.mode == IEEE802154_ADDRMODE_SHORT) + { + pendsaddr++; + IEEE802154_SADDRCOPY(&beacon->bf_data[beacon->bf_len], txdesc->destaddr.saddr); + beacon->bf_len += IEEE802154_SADDRSIZE; + } + else if (txdesc->destaddr.mode == IEEE802154_ADDRMODE_EXTENDED) + { + pendeaddr++; + IEEE802154_EADDRCOPY(&beacon->bf_data[beacon->bf_len], txdesc->destaddr.eaddr); + beacon->bf_len += IEEE802154_EADDRSIZE; + } + + /* Check if we are up to 7 addresses yet */ + + if ((pendsaddr + pendeaddr) == 7) + { + break; + } + + /* Get the next pending indirect transation */ + + txdesc = (FAR struct ieee802154_txdesc_s *)sq_next((FAR sq_entry_t *)txdesc); + } + + /* At this point, we know how many of each transaction we have, we can setup + * the Pending Address Specification field + */ + + beacon->bf_data[pendaddrspec_ind] = (pendsaddr & 0x07) | ((pendeaddr << 4) & 0x70); + + /* Copy in the beacon payload */ + + memcpy(&beacon->bf_data[beacon->bf_len], priv->beaconpayload, + priv->beaconpayloadlength); + beacon->bf_len += priv->beaconpayloadlength; + + priv->beaconupdate = false; +} + /**************************************************************************** * Name: mac802154_setupindirect * @@ -386,10 +546,10 @@ void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, * aBaseSuperframeDuration. [1] pg. 129 */ - if (priv->beaconorder < 15) + if (priv->sf_spec.beaconorder < 15) { symbols = priv->trans_persisttime * - (IEEE802154_BASE_SUPERFRAME_DURATION * (1 << priv->beaconorder)); + (IEEE802154_BASE_SUPERFRAME_DURATION * (1 << priv->sf_spec.beaconorder)); } else { @@ -400,6 +560,13 @@ void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, txdesc->purge_time = clock_systimer() + ticks; + /* Make sure the beacon gets updated */ + + if (priv->sf_spec.beaconorder < 15) + { + priv->beaconupdate = true; + } + /* Check to see if the purge indirect timer is scheduled. If it is, when the * timer fires, it will schedule the next purge timer event. Inherently, the * queue will be in order of which transaction needs to be purged next. @@ -469,6 +636,7 @@ static void mac802154_purge_worker(FAR void *arg) ((FAR struct mac802154_notif_s *)txdesc->conf)->flink = priv->notif_free; priv->notif_free = ((FAR struct mac802154_notif_s *)txdesc->conf); mac802154_txdesc_free(priv, txdesc); + priv->beaconupdate = true; wlinfo("Indirect TX purged"); } @@ -1189,12 +1357,7 @@ static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, txdesc = (FAR struct ieee802154_txdesc_s *)sq_peek(&priv->indirect_queue); - if (txdesc == NULL) - { - goto no_data; - } - - do + while(txdesc != NULL) { if (txdesc->destaddr.mode == ind->src.mode) { @@ -1209,7 +1372,9 @@ static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, /* The addresses match, send the transaction immediately */ priv->radio->txdelayed(priv->radio, txdesc, 0); - break; + priv->beaconupdate = true; + mac802154_givesem(&priv->exclsem); + return; } } else if (txdesc->destaddr.mode == IEEE802154_ADDRMODE_EXTENDED) @@ -1224,7 +1389,9 @@ static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, /* The addresses match, send the transaction immediately */ priv->radio->txdelayed(priv->radio, txdesc, 0); - break; + priv->beaconupdate = true; + mac802154_givesem(&priv->exclsem); + return; } } else @@ -1234,18 +1401,7 @@ static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, } txdesc = (FAR struct ieee802154_txdesc_s *)sq_next((FAR sq_entry_t *)txdesc); - - if (txdesc == NULL) - { - goto no_data; - } } - while (1); - - mac802154_givesem(&priv->exclsem); - return; - -no_data: /* If there is no data frame pending for the requesting device, the coordinator * shall send a data frame without requesting acknowledgment to the device @@ -1345,6 +1501,34 @@ no_data: priv->radio->txdelayed(priv->radio, txdesc, 0); } +static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, + enum ieee802154_sfevent_e sfevent) +{ + FAR struct mac802154_radiocb_s *cb = + (FAR struct mac802154_radiocb_s *)radiocb; + FAR struct ieee802154_privmac_s *priv; + + DEBUGASSERT(cb != NULL && cb->priv != NULL); + priv = cb->priv; + + /* Get exclusive access to the driver structure. We don't care about any + * signals so if we see one, just go back to trying to get access again. + */ + + mac802154_takesem(&priv->exclsem, false); + + /* Check if there is any reason to update the beacon */ + + if (priv->beaconupdate) + { + mac802154_updatebeacon(priv); + + priv->radio->beaconupdate(priv->radio, &priv->beaconframe[priv->bf_ind]); + } + + mac802154_givesem(&priv->exclsem); +} + /**************************************************************************** * Name: mac802154_symtoticks * @@ -1534,6 +1718,7 @@ MACHANDLE mac802154_create(FAR struct ieee802154_radio_s *radiodev) radiocb->poll = mac802154_radiopoll; radiocb->txdone = mac802154_txdone; radiocb->rxframe = mac802154_rxframe; + radiocb->sfevent = mac802154_sfevent; /* Bind our callback structure */ diff --git a/wireless/ieee802154/mac802154_getset.c b/wireless/ieee802154/mac802154_getset.c index 083086b66b..bc666aad44 100644 --- a/wireless/ieee802154/mac802154_getset.c +++ b/wireless/ieee802154/mac802154_getset.c @@ -166,6 +166,13 @@ int mac802154_req_set(MACHANDLE mac, enum ieee802154_attr_e attr, ret = IEEE802154_STATUS_SUCCESS; } break; + case IEEE802154_ATTR_MAC_ASSOCIATION_PERMIT: + { + priv->sf_spec.assocpermit = attrval->mac.assocpermit; + priv->beaconupdate = true; + ret = IEEE802154_STATUS_SUCCESS; + } + break; default: { /* The attribute may be handled soley in the radio driver, so pass diff --git a/wireless/ieee802154/mac802154_internal.h b/wireless/ieee802154/mac802154_internal.h index 99fffc6282..90f4b71411 100644 --- a/wireless/ieee802154/mac802154_internal.h +++ b/wireless/ieee802154/mac802154_internal.h @@ -1,499 +1,532 @@ -/**************************************************************************** - * wireless/ieee802154/mac802154_internal.h - * - * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. - * Copyright (C) 2017 Verge Inc. All rights reserved. - * Copyright (C) 2017 Gregory Nutt. All rights reserved. - * - * Author: Sebastien Lorquet - * Author: Anthony Merlino - * - * The naming and comments for various fields are taken directly - * from the IEEE 802.15.4 2011 standard. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -#ifndef __WIRELESS_IEEE802154__MAC802154_INTERNAL_H -#define __WIRELESS_IEEE802154__MAC802154_INTERNAL_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include "mac802154_notif.h" - -#include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define mac802154_putpanid(iob, panid) \ - do \ - { \ - IEEE802154_PANIDCOPY(&iob->io_data[iob->io_len], panid); \ - iob->io_len += IEEE802154_PANIDSIZE; \ - } \ - while(0) - -#define mac802154_putsaddr(iob, saddr) \ - do \ - { \ - IEEE802154_SADDRCOPY(&iob->io_data[iob->io_len], saddr); \ - iob->io_len += IEEE802154_SADDRSIZE; \ - } \ - while(0) - -#define mac802154_puteaddr(iob, eaddr) \ - do \ - { \ - IEEE802154_EADDRCOPY(&iob->io_data[iob->io_len], eaddr); \ - iob->io_len += IEEE802154_EADDRSIZE; \ - } \ - while(0) - -#define mac802154_takepanid(iob, panid) \ - do \ - { \ - IEEE802154_PANIDCOPY(panid, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_PANIDSIZE; \ - } \ - while(0) - -#define mac802154_takesaddr(iob, saddr) \ - do \ - { \ - IEEE802154_SADDRCOPY(saddr, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_SADDRSIZE; \ - } \ - while(0) - -#define mac802154_takeeaddr(iob, eaddr) \ - do \ - { \ - IEEE802154_EADDRCOPY(eaddr, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_EADDRSIZE; \ - } \ - while(0) - -/* General helper macros ****************************************************/ - -/* GET 16-bit data: source in network order, result in host order */ - -#define GETHOST16(ptr,index) \ - ((((uint16_t)((ptr)[index])) << 8) | ((uint16_t)(((ptr)[(index) + 1])))) - -/* GET 16-bit data: source in network order, result in network order */ - -#define GETNET16(ptr,index) \ - ((((uint16_t)((ptr)[(index) + 1])) << 8) | ((uint16_t)(((ptr)[index])))) - -/* Set 16-bit data: source in host order, result in network order. */ - -#define IEEE802154_SETBITS_U16(ptr,index,value) \ - do \ - { \ - (ptr)[index] |= (uint16_t)(value) & 0xff; \ - (ptr)[index + 1] |= ((uint16_t)(value) >> 8) & 0xff; \ - } \ - while(0) - -#define IEEE802154_SETFTYPE(ptr, index, ftype) \ - IEEE802154_SETBITS_U16(ptr, index, (ftype << IEEE802154_FRAMECTRL_SHIFT_FTYPE)) - -#define IEEE802154_SETACKREQ(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_ACKREQ) - -#define IEEE802154_SETDADDRMODE(ptr, index, mode) \ - IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_DADDR)) - -#define IEEE802154_SETSADDRMODE(ptr, index, mode) \ - IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_SADDR)) - -#define IEEE802154_SETPANIDCOMP(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_PANIDCOMP) - -/* Configuration ************************************************************/ -/* If processing is not done at the interrupt level, then work queue support - * is required. - */ - -#if !defined(CONFIG_SCHED_WORKQUEUE) -# error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE) -#else - - /* Use the low priority work queue if possible */ - -# if defined(CONFIG_MAC802154_HPWORK) -# define MAC802154_WORK HPWORK -# elif defined(CONFIG_MAC802154_LPWORK) -# define MAC802154_WORK LPWORK -# else -# error Neither CONFIG_MAC802154_HPWORK nor CONFIG_MAC802154_LPWORK defined -# endif -#endif - -#if !defined(CONFIG_MAC802154_NNOTIF) || CONFIG_MAC802154_NNOTIF <= 0 -# undef CONFIG_MAC802154_NNOTIF -# define CONFIG_MAC802154_NNOTIF 6 -#endif - -#if !defined(CONFIG_MAC802154_NTXDESC) || CONFIG_MAC802154_NTXDESC <= 0 -# undef CONFIG_MAC802154_NTXDESC -# define CONFIG_MAC802154_NTXDESC 3 -#endif - -#if CONFIG_MAC802154_NTXDESC > CONFIG_MAC802154_NNOTIF -# error CONFIG_MAC802154_NNOTIF must be greater than CONFIG_MAC802154_NTXDESC -#endif - -#if !defined(CONFIG_IEEE802154_DEFAULT_EADDR) -# define CONFIG_IEEE802154_DEFAULT_EADDR 0xFFFFFFFFFFFFFFFF -#endif - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/* Map between ieee802154_addrmode_e enum and actual address length */ - -static const uint8_t mac802154_addr_length[4] = {0, 0, 2, 8}; - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct mac802154_radiocb_s -{ - struct ieee802154_radiocb_s cb; - FAR struct ieee802154_privmac_s *priv; -}; - -/* Enumeration for representing what operation the MAC layer is currently doing. - * There can only be one command being handled at any given time, but certain - * operations such as association requires more than one command to be sent. - * Therefore, the need to track not only what command is currently active, but - * also, what overall operation the command is apart of is necessary. - */ - -enum mac802154_operation_e -{ - MAC802154_OP_NONE, - MAC802154_OP_ASSOC, - MAC802154_OP_POLL -}; - -struct ieee802154_privmac_s; /* Forward Reference */ -typedef void (*mac802154_worker_t)(FAR struct ieee802154_privmac_s *priv); - -/* The privmac structure holds the internal state of the MAC and is the - * underlying represention of the opaque MACHANDLE. It contains storage for - * the IEEE802.15.4 MIB attributes. - */ - -struct ieee802154_privmac_s -{ - FAR struct ieee802154_radio_s *radio; /* Contained IEEE802.15.4 radio dev */ - FAR struct mac802154_maccb_s *cb; /* Head of a list of MAC callbacks */ - FAR struct mac802154_radiocb_s radiocb; /* Interface to bind to radio */ - - sem_t exclsem; /* Support exclusive access */ - uint8_t nclients; /* Number of notification clients */ - uint8_t nnotif; /* Number of remaining notifications */ - - /* Only support a single command at any given time. As of now I see no - * condition where you need to have more than one command frame simultaneously - */ - - sem_t opsem; /* Exclusive operations */ - enum mac802154_operation_e curr_op; /* The current overall operation */ - enum ieee802154_cmdid_e curr_cmd; /* Type of the current cmd */ - FAR struct ieee802154_txdesc_s *cmd_desc; /* TX descriptor for current cmd */ - - /* Pre-allocated notifications to be passed to the registered callback. These - * need to be freed by the application using mac802154_xxxxnotif_free when - * the callee layer is finished with it's use. - */ - - FAR struct mac802154_notif_s *notif_free; - struct mac802154_notif_s notif_pool[CONFIG_MAC802154_NNOTIF]; - sem_t notif_sem; - - struct ieee802154_txdesc_s txdesc_pool[CONFIG_MAC802154_NTXDESC]; - sem_t txdesc_sem; - sq_queue_t txdesc_queue; - sq_queue_t txdone_queue; - - - /* Support a singly linked list of transactions that will be sent using the - * CSMA algorithm. On a non-beacon enabled PAN, these transactions will be - * sent whenever. On a beacon-enabled PAN, these transactions will be sent - * during the CAP of the Coordinator's superframe. - */ - - sq_queue_t csma_queue; - sq_queue_t gts_queue; - - /* Support a singly linked list of transactions that will be sent indirectly. - * This list should only be used by a MAC acting as a coordinator. These - * transactions will stay here until the data is extracted by the destination - * device sending a Data Request MAC command or if too much time passes. This - * list should also be used to populate the address list of the outgoing - * beacon frame. - */ - - sq_queue_t indirect_queue; - - /* Support a singly linked list of frames received */ - - sq_queue_t dataind_queue; - - /* Work structures for offloading aynchronous work */ - - struct work_s tx_work; - struct work_s rx_work; - - struct work_s timeout_work; - WDOG_ID timeout; /* Timeout watchdog */ - mac802154_worker_t timeout_worker; - - struct work_s purge_work; - - /* MAC PIB attributes, grouped to save memory */ - - /* Holds all address information (Extended, Short, and PAN ID) for the MAC. */ - - struct ieee802154_addr_s addr; - - /* Holds all address information (Extended, Short) for Coordinator */ - - struct ieee802154_addr_s coordaddr; - - /* The maximum number of symbols to wait for an acknowledgement frame to - * arrive following a transmitted data frame. [1] pg. 126 - * - * NOTE: This may be able to be a 16-bit or even an 8-bit number. I wasn't - * sure at the time what the range of reasonable values was. - */ - - uint32_t ack_waitdur; - - /* The maximum time to wait either for a frame intended as a response to a - * data request frame or for a broadcast frame following a beacon with the - * Frame Pending field set to one. [1] pg. 127 - * - * NOTE: This may be able to be a 16-bit or even an 8-bit number. I wasn't - * sure at the time what the range of reasonable values was. - */ - - uint32_t max_frame_waittime; - - /* The maximum time (in unit periods) that a transaction is stored by a - * coordinator and indicated in its beacon. - */ - - uint16_t trans_persisttime; - - /* Contents of beacon payload */ - - uint8_t beacon_payload[IEEE802154_MAX_BEACON_PAYLOAD_LEN]; - uint8_t beacon_payload_len; /* Length of beacon payload */ - - uint8_t battlifeext_periods; /* # of backoff periods during which rx is - * enabled after the IFS following beacon */ - - uint8_t bsn; /* Seq. num added to tx beacon frame */ - uint8_t dsn; /* Seq. num added to tx data or MAC frame */ - uint8_t maxretries; /* Max # of retries alloed after tx failure */ - - /* The maximum time, in multiples of aBaseSuperframeDuration, a device shall - * wait for a response command frame to be available following a request - * command frame. [1] 128. - */ - - uint8_t resp_waittime; - - /* The total transmit duration (including PHY header and FCS) specified in - * symbols. [1] pg. 129. - */ - - uint32_t tx_totaldur; - - /* Start of 32-bit bitfield */ - - uint32_t trackingbeacon : 1; /* Are we tracking the beacon */ - uint32_t isassoc : 1; /* Are we associated to the PAN */ - uint32_t assocpermit : 1; /* Are we allowing assoc. as a coord. */ - uint32_t autoreq : 1; /* Automatically send data req. if addr - * addr is in the beacon frame */ - - uint32_t battlifeext : 1; /* Is BLE enabled */ - uint32_t gtspermit : 1; /* Is PAN Coord. accepting GTS reqs. */ - uint32_t promisc : 1; /* Is promiscuous mode on? */ - uint32_t rngsupport : 1; /* Does MAC sublayer support ranging */ - uint32_t sec_enabled : 1; /* Does MAC sublayer have security en. */ - uint32_t timestamp_support : 1; /* Does MAC layer supports timestamping */ - - /* 2 available bits */ - - uint32_t beaconorder : 4; /* Freq. that beacon is transmitted */ - - uint32_t superframeorder : 4; /* Length of active portion of outgoing - * superframe, including the beacon */ - - /* The offset, measured is symbols, between the symbol boundary at which the - * MLME captures the timestamp of each transmitted and received frame, and - * the onset of the first symbol past the SFD, namely the first symbol of - * the frames [1] pg. 129. - */ - - uint32_t sync_symboffset : 12; - - /* End of 32-bit bitfield */ - - /* Start of 32-bit bitfield */ - - uint32_t beacon_txtime : 24; /* Time of last beacon transmit */ - uint32_t minbe : 4; /* Min value of backoff exponent (BE) */ - uint32_t maxbe : 4; /* Max value of backoff exponent (BE) */ - - /* End of 32-bit bitfield */ - - /* Start of 32-bit bitfield */ - - uint32_t txctrl_activedur : 17; /* Duration for which tx is permitted to - * be active */ - uint32_t txctrl_pausedur : 1; /* Duration after tx before another tx is - * permitted. 0=2000, 1= 10000 */ - - /* What type of device is this node acting as */ - - enum ieee802154_devmode_e devmode : 2; - - uint32_t max_csmabackoffs : 3; /* Max num backoffs for CSMA algorithm - * before declaring ch access failure */ - - /* 9-bits remaining */ - - /* End of 32-bit bitfield. */ - - /* TODO: Add Security-related MAC PIB attributes */ -}; - -/**************************************************************************** - * Inline Functions - ****************************************************************************/ - -#define mac802154_givesem(s) sem_post(s); - -static inline int mac802154_takesem(sem_t *sem, bool allowinterrupt) -{ - int ret; - do - { - /* Take a count from the semaphore, possibly waiting */ - - ret = sem_wait(sem); - if (ret < 0) - { - /* EINTR is the only error that we expect */ - - DEBUGASSERT(get_errno() == EINTR); - - if (allowinterrupt) - { - return -EINTR; - } - } - } - while (ret != OK); - - return OK; -} - -static inline void mac802154_txdesc_free(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s *txdesc) -{ - sq_addlast((FAR sq_entry_t *)txdesc, &priv->txdesc_queue); - mac802154_givesem(&priv->txdesc_sem); -} - -/**************************************************************************** - * Name: mac802154_timercancel - * - * Description: - * Cancel timer and remove reference to callback function - * - * Assumptions: - * priv MAC struct is locked when calling. - * - ****************************************************************************/ - -static inline int mac802154_timercancel(FAR struct ieee802154_privmac_s *priv) -{ - wd_cancel(priv->timeout); - priv->timeout_worker = NULL; - return OK; -} - -/**************************************************************************** - * Function Prototypes - ****************************************************************************/ - - -int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s **txdesc, - bool allow_interrupt); - -int mac802154_timerstart(FAR struct ieee802154_privmac_s *priv, - uint32_t numsymbols, mac802154_worker_t); - -void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s *txdesc); - -void mac802154_createdatareq(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_addr_s *coordaddr, - enum ieee802154_addrmode_e srcmode, - FAR struct ieee802154_txdesc_s *txdesc); - - -#endif /* __WIRELESS_IEEE802154__MAC802154_INTERNAL_H */ +/**************************************************************************** + * wireless/ieee802154/mac802154_internal.h + * + * Copyright (C) 2016 Sebastien Lorquet. All rights reserved. + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * + * Author: Sebastien Lorquet + * Author: Anthony Merlino + * + * The naming and comments for various fields are taken directly + * from the IEEE 802.15.4 2011 standard. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __WIRELESS_IEEE802154__MAC802154_INTERNAL_H +#define __WIRELESS_IEEE802154__MAC802154_INTERNAL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include "mac802154_notif.h" + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define mac802154_putpanid(iob, panid) \ + do \ + { \ + IEEE802154_PANIDCOPY(&iob->io_data[iob->io_len], panid); \ + iob->io_len += IEEE802154_PANIDSIZE; \ + } \ + while(0) + +#define mac802154_putsaddr(iob, saddr) \ + do \ + { \ + IEEE802154_SADDRCOPY(&iob->io_data[iob->io_len], saddr); \ + iob->io_len += IEEE802154_SADDRSIZE; \ + } \ + while(0) + +#define mac802154_puteaddr(iob, eaddr) \ + do \ + { \ + IEEE802154_EADDRCOPY(&iob->io_data[iob->io_len], eaddr); \ + iob->io_len += IEEE802154_EADDRSIZE; \ + } \ + while(0) + +#define mac802154_takepanid(iob, panid) \ + do \ + { \ + IEEE802154_PANIDCOPY(panid, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_PANIDSIZE; \ + } \ + while(0) + +#define mac802154_takesaddr(iob, saddr) \ + do \ + { \ + IEEE802154_SADDRCOPY(saddr, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_SADDRSIZE; \ + } \ + while(0) + +#define mac802154_takeeaddr(iob, eaddr) \ + do \ + { \ + IEEE802154_EADDRCOPY(eaddr, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_EADDRSIZE; \ + } \ + while(0) + +/* General helper macros ****************************************************/ + +/* GET 16-bit data: source in network order, result in host order */ + +#define GETHOST16(ptr,index) \ + ((((uint16_t)((ptr)[index])) << 8) | ((uint16_t)(((ptr)[(index) + 1])))) + +/* GET 16-bit data: source in network order, result in network order */ + +#define GETNET16(ptr,index) \ + ((((uint16_t)((ptr)[(index) + 1])) << 8) | ((uint16_t)(((ptr)[index])))) + +/* PUT 16-bit data: source in host order, result in network order */ + +#define PUTHOST16(ptr,index,value) \ + do \ + { \ + (ptr)[index] = (uint16_t)(value) & 0xff; \ + (ptr)[index + 1] = ((uint16_t)(value) >> 8) & 0xff; \ + } \ + while(0) + +/* Set bit in 16-bit value: source in host order, result in network order. */ + +#define IEEE802154_SETBITS_U16(ptr,index,value) \ + do \ + { \ + (ptr)[index] |= (uint16_t)(value) & 0xff; \ + (ptr)[index + 1] |= ((uint16_t)(value) >> 8) & 0xff; \ + } \ + while(0) + +#define IEEE802154_SETFTYPE(ptr, index, ftype) \ + IEEE802154_SETBITS_U16(ptr, index, (ftype << IEEE802154_FRAMECTRL_SHIFT_FTYPE)) + +#define IEEE802154_SETACKREQ(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_ACKREQ) + +#define IEEE802154_SETDADDRMODE(ptr, index, mode) \ + IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_DADDR)) + +#define IEEE802154_SETSADDRMODE(ptr, index, mode) \ + IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_SADDR)) + +#define IEEE802154_SETPANIDCOMP(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_PANIDCOMP) + +#define IEEE802154_SETVERSION(ptr, index, version) \ + IEEE802154_SETBITS_U16(ptr, index, (version << IEEE802154_FRAMECTRL_SHIFT_VERSION)) + +/* Helper macros for setting/receiving bits for superframe specification */ + +#define IEEE802154_SETBEACONORDER(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_BEACONORDER)) + +#define IEEE802154_SETSFORDER(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_SFORDER)) + +#define IEEE802154_SETFINCAPSLOT(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_FINCAPSLOT)) + +#define IEEE802154_SETBLE(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_BLE) + +#define IEEE802154_SETPANCOORD(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_PANCOORD) + +#define IEEE802154_SETASSOCPERMIT(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_ASSOCPERMIT) + +/* Configuration ************************************************************/ +/* If processing is not done at the interrupt level, then work queue support + * is required. + */ + +#if !defined(CONFIG_SCHED_WORKQUEUE) +# error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE) +#else + + /* Use the low priority work queue if possible */ + +# if defined(CONFIG_MAC802154_HPWORK) +# define MAC802154_WORK HPWORK +# elif defined(CONFIG_MAC802154_LPWORK) +# define MAC802154_WORK LPWORK +# else +# error Neither CONFIG_MAC802154_HPWORK nor CONFIG_MAC802154_LPWORK defined +# endif +#endif + +#if !defined(CONFIG_MAC802154_NNOTIF) || CONFIG_MAC802154_NNOTIF <= 0 +# undef CONFIG_MAC802154_NNOTIF +# define CONFIG_MAC802154_NNOTIF 6 +#endif + +#if !defined(CONFIG_MAC802154_NTXDESC) || CONFIG_MAC802154_NTXDESC <= 0 +# undef CONFIG_MAC802154_NTXDESC +# define CONFIG_MAC802154_NTXDESC 3 +#endif + +#if CONFIG_MAC802154_NTXDESC > CONFIG_MAC802154_NNOTIF +# error CONFIG_MAC802154_NNOTIF must be greater than CONFIG_MAC802154_NTXDESC +#endif + +#if !defined(CONFIG_IEEE802154_DEFAULT_EADDR) +# define CONFIG_IEEE802154_DEFAULT_EADDR 0xFFFFFFFFFFFFFFFF +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Map between ieee802154_addrmode_e enum and actual address length */ + +static const uint8_t mac802154_addr_length[4] = {0, 0, 2, 8}; + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct mac802154_radiocb_s +{ + struct ieee802154_radiocb_s cb; + FAR struct ieee802154_privmac_s *priv; +}; + +/* Enumeration for representing what operation the MAC layer is currently doing. + * There can only be one command being handled at any given time, but certain + * operations such as association requires more than one command to be sent. + * Therefore, the need to track not only what command is currently active, but + * also, what overall operation the command is apart of is necessary. + */ + +enum mac802154_operation_e +{ + MAC802154_OP_NONE, + MAC802154_OP_ASSOC, + MAC802154_OP_POLL +}; + +struct ieee802154_privmac_s; /* Forward Reference */ +typedef void (*mac802154_worker_t)(FAR struct ieee802154_privmac_s *priv); + +/* The privmac structure holds the internal state of the MAC and is the + * underlying represention of the opaque MACHANDLE. It contains storage for + * the IEEE802.15.4 MIB attributes. + */ + +struct ieee802154_privmac_s +{ + FAR struct ieee802154_radio_s *radio; /* Contained IEEE802.15.4 radio dev */ + FAR struct mac802154_maccb_s *cb; /* Head of a list of MAC callbacks */ + FAR struct mac802154_radiocb_s radiocb; /* Interface to bind to radio */ + + sem_t exclsem; /* Support exclusive access */ + uint8_t nclients; /* Number of notification clients */ + uint8_t nnotif; /* Number of remaining notifications */ + + /* Only support a single command at any given time. As of now I see no + * condition where you need to have more than one command frame simultaneously + */ + + sem_t opsem; /* Exclusive operations */ + enum mac802154_operation_e curr_op; /* The current overall operation */ + enum ieee802154_cmdid_e curr_cmd; /* Type of the current cmd */ + FAR struct ieee802154_txdesc_s *cmd_desc; /* TX descriptor for current cmd */ + + /* Pre-allocated notifications to be passed to the registered callback. These + * need to be freed by the application using mac802154_xxxxnotif_free when + * the callee layer is finished with it's use. + */ + + FAR struct mac802154_notif_s *notif_free; + struct mac802154_notif_s notif_pool[CONFIG_MAC802154_NNOTIF]; + sem_t notif_sem; + + struct ieee802154_txdesc_s txdesc_pool[CONFIG_MAC802154_NTXDESC]; + sem_t txdesc_sem; + sq_queue_t txdesc_queue; + sq_queue_t txdone_queue; + + /* Support a singly linked list of transactions that will be sent using the + * CSMA algorithm. On a non-beacon enabled PAN, these transactions will be + * sent whenever. On a beacon-enabled PAN, these transactions will be sent + * during the CAP of the Coordinator's superframe. + */ + + sq_queue_t csma_queue; + sq_queue_t gts_queue; + + /* Support a singly linked list of transactions that will be sent indirectly. + * This list should only be used by a MAC acting as a coordinator. These + * transactions will stay here until the data is extracted by the destination + * device sending a Data Request MAC command or if too much time passes. This + * list should also be used to populate the address list of the outgoing + * beacon frame. + */ + + sq_queue_t indirect_queue; + + /* Support a singly linked list of frames received */ + + sq_queue_t dataind_queue; + + /* Work structures for offloading aynchronous work */ + + struct work_s tx_work; + struct work_s rx_work; + + struct work_s timeout_work; + WDOG_ID timeout; /* Timeout watchdog */ + mac802154_worker_t timeout_worker; + + struct work_s purge_work; + + /* MAC PIB attributes, grouped to save memory */ + + /* Holds all address information (Extended, Short, and PAN ID) for the MAC. */ + + struct ieee802154_addr_s addr; + + /* Holds all address information (Extended, Short) for Coordinator */ + + struct ieee802154_addr_s coordaddr; + + /* Holds attributes pertaining to the superframe specification */ + + struct ieee802154_superframespec_s sf_spec; + + /* We use 2 beacon frame structures so that we can ping-pong between them + * while updating the beacon + */ + + struct ieee802154_beaconframe_s beaconframe[2]; + + /* The maximum number of symbols to wait for an acknowledgement frame to + * arrive following a transmitted data frame. [1] pg. 126 + * + * NOTE: This may be able to be a 16-bit or even an 8-bit number. I wasn't + * sure at the time what the range of reasonable values was. + */ + + uint32_t ack_waitdur; + + /* The maximum time to wait either for a frame intended as a response to a + * data request frame or for a broadcast frame following a beacon with the + * Frame Pending field set to one. [1] pg. 127 + * + * NOTE: This may be able to be a 16-bit or even an 8-bit number. I wasn't + * sure at the time what the range of reasonable values was. + */ + + uint32_t max_frame_waittime; + + /* The maximum time (in unit periods) that a transaction is stored by a + * coordinator and indicated in its beacon. + */ + + uint16_t trans_persisttime; + + /* Contents of beacon payload */ + + uint8_t beaconpayload[IEEE802154_MAX_BEACON_PAYLOAD_LEN]; + uint8_t beaconpayloadlength; /* Length of beacon payload */ + uint8_t bsn; /* Seq. num added to tx beacon frame */ + uint8_t dsn; /* Seq. num added to tx data or MAC frame */ + + /* The maximum time, in multiples of aBaseSuperframeDuration, a device shall + * wait for a response command frame to be available following a request + * command frame. [1] 128. + */ + + uint8_t resp_waittime; + + /* The total transmit duration (including PHY header and FCS) specified in + * symbols. [1] pg. 129. + */ + + uint32_t tx_totaldur; + + /* Start of 8-bit bitfield */ + + uint32_t trackingbeacon : 1; /* Are we tracking the beacon */ + uint32_t isassoc : 1; /* Are we associated to the PAN */ + uint32_t autoreq : 1; /* Automatically send data req. if addr + * addr is in the beacon frame */ + + uint32_t gtspermit : 1; /* Is PAN Coord. accepting GTS reqs. */ + uint32_t promisc : 1; /* Is promiscuous mode on? */ + uint32_t rngsupport : 1; /* Does MAC sublayer support ranging */ + uint32_t sec_enabled : 1; /* Does MAC sublayer have security en. */ + uint32_t timestamp_support : 1; /* Does MAC layer supports timestamping */ + + /* End of 8-bit bitfield */ + + /* Start of 32-bit bitfield */ + + /* The offset, measured is symbols, between the symbol boundary at which the + * MLME captures the timestamp of each transmitted and received frame, and + * the onset of the first symbol past the SFD, namely the first symbol of + * the frames [1] pg. 129. + */ + + uint32_t sync_symboffset : 12; + + uint32_t txctrl_activedur : 17; /* Duration for which tx is permitted to + * be active */ + uint32_t txctrl_pausedur : 1; /* Duration after tx before another tx is + * permitted. 0=2000, 1= 10000 */ + + /* What type of device is this node acting as */ + + enum ieee802154_devmode_e devmode : 2; + + /* End of 32-bit bitfield */ + + /* Start of 32-bit bitfield */ + + uint32_t beacon_txtime : 24; /* Time of last beacon transmit */ + uint32_t minbe : 4; /* Min value of backoff exponent (BE) */ + uint32_t maxbe : 4; /* Max value of backoff exponent (BE) */ + + /* End of 32-bit bitfield */ + + /* Start of 8-bit bitfield */ + + uint8_t bf_ind : 1; /* Ping-pong index for beacon frame */ + uint8_t beaconupdate : 1; /* Does the beacon frame need to be updated */ + uint8_t max_csmabackoffs : 3; /* Max num backoffs for CSMA algorithm + * before declaring ch access failure */ + uint8_t maxretries : 3; /* Max # of retries allowed after tx fail */ + /* End of 8-bit bitfield. */ + + /* TODO: Add Security-related MAC PIB attributes */ +}; + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +#define mac802154_givesem(s) sem_post(s); + +static inline int mac802154_takesem(sem_t *sem, bool allowinterrupt) +{ + int ret; + do + { + /* Take a count from the semaphore, possibly waiting */ + + ret = sem_wait(sem); + if (ret < 0) + { + /* EINTR is the only error that we expect */ + + DEBUGASSERT(get_errno() == EINTR); + + if (allowinterrupt) + { + return -EINTR; + } + } + } + while (ret != OK); + + return OK; +} + +static inline void mac802154_txdesc_free(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_txdesc_s *txdesc) +{ + sq_addlast((FAR sq_entry_t *)txdesc, &priv->txdesc_queue); + mac802154_givesem(&priv->txdesc_sem); +} + +/**************************************************************************** + * Name: mac802154_timercancel + * + * Description: + * Cancel timer and remove reference to callback function + * + * Assumptions: + * priv MAC struct is locked when calling. + * + ****************************************************************************/ + +static inline int mac802154_timercancel(FAR struct ieee802154_privmac_s *priv) +{ + wd_cancel(priv->timeout); + priv->timeout_worker = NULL; + return OK; +} + +/**************************************************************************** + * Function Prototypes + ****************************************************************************/ + + +int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_txdesc_s **txdesc, + bool allow_interrupt); + +int mac802154_timerstart(FAR struct ieee802154_privmac_s *priv, + uint32_t numsymbols, mac802154_worker_t); + +void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_txdesc_s *txdesc); + +void mac802154_createdatareq(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_addr_s *coordaddr, + enum ieee802154_addrmode_e srcmode, + FAR struct ieee802154_txdesc_s *txdesc); + +void mac802154_updatebeacon(FAR struct ieee802154_privmac_s *priv); + +#endif /* __WIRELESS_IEEE802154__MAC802154_INTERNAL_H */ diff --git a/wireless/ieee802154/mac802154_reset.c b/wireless/ieee802154/mac802154_reset.c index 23b9706dc7..8e72596363 100644 --- a/wireless/ieee802154/mac802154_reset.c +++ b/wireless/ieee802154/mac802154_reset.c @@ -86,15 +86,15 @@ int mac802154_req_reset(MACHANDLE mac, bool rst_pibattr) if (rst_pibattr) { - priv->isassoc = false; /* Not associated with a PAN */ - priv->trackingbeacon = false; /* Not tracking beacon by default */ - priv->assocpermit = false; /* Device (if coord) not accepting ssociation */ - priv->autoreq = true; /* Auto send data req if addr. in beacon */ - priv->battlifeext = false; /* BLE disabled */ - priv->beacon_payload_len = 0; /* Beacon payload NULL */ - priv->beaconorder = 15; /* Non-beacon enabled network */ - priv->superframeorder = 15; /* Length of active portion of outgoing SF */ - priv->beacon_txtime = 0; /* Device never sent a beacon */ + priv->isassoc = false; /* Not associated with a PAN */ + priv->trackingbeacon = false; /* Not tracking beacon by default */ + priv->sf_spec.assocpermit = false; /* Device (if coord) not accepting ssociation */ + priv->autoreq = true; /* Auto send data req if addr. in beacon */ + priv->sf_spec.ble = false; /* BLE disabled */ + priv->beaconpayloadlength = 0; /* Beacon payload NULL */ + priv->sf_spec.beaconorder = 15; /* Non-beacon enabled network */ + priv->sf_spec.sforder = 15; /* Length of active portion of outgoing SF */ + priv->beacon_txtime = 0; /* Device never sent a beacon */ #warning Set BSN and DSN to random values! priv->bsn = 0; priv->dsn = 0; diff --git a/wireless/ieee802154/mac802154_start.c b/wireless/ieee802154/mac802154_start.c index 16e33484db..bd734daa99 100644 --- a/wireless/ieee802154/mac802154_start.c +++ b/wireless/ieee802154/mac802154_start.c @@ -101,13 +101,26 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_PANID, (FAR const union ieee802154_attr_u *)req->panid); - /* Set the radio attributes */ + /* Tell the radio layer to set the channel number and channel page */ + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_CHANNEL, (FAR const union ieee802154_attr_u *)&req->chnum); - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_PAGE, (FAR const union ieee802154_attr_u *)&req->chpage); + /* The address used in the Source Address field of the beacon frame shall + * contain the value of macExtendedAddress if macShortAddress is equal to + * 0xfffe or macShortAddress otherwise. [1] pg. 32 + */ + + if (IEEE802154_SADDRCMP(priv->addr.saddr, &IEEE802154_SADDR_BCAST)) + { + priv->addr.mode = IEEE802154_ADDRMODE_EXTENDED; + } + else + { + priv->addr.mode = IEEE802154_ADDRMODE_SHORT; + } /* Set the beacon order */ @@ -117,11 +130,11 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) goto errout; } - priv->beaconorder = req->beaconorder; + priv->sf_spec.beaconorder = req->beaconorder; /* The value of macSuperframeOrder shall be ignored if macBeaconOrder = 15. pg. 19 */ - if (priv->beaconorder < 15) + if (priv->sf_spec.beaconorder < 15) { /* Set the superframe order */ @@ -131,7 +144,7 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) goto errout; } - priv->superframeorder = req->superframeorder; + priv->sf_spec.sforder = req->superframeorder; } if (req->pancoord) @@ -143,18 +156,44 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) priv->devmode = IEEE802154_DEVMODE_COORD; } - /* If the BeaconOrder parameter is less than 15, the MLME sets macBattLifeExt to - * the value of the BatteryLifeExtension parameter. If the BeaconOrder parameter - * equals 15, the value of the BatteryLifeExtension parameter is ignored. - * [1] pg. 106 - */ + priv->sf_spec.pancoord = req->pancoord; - if (priv->beaconorder < 15) + if (priv->sf_spec.beaconorder < 15) { - priv->battlifeext = req->battlifeext; + /* If the BeaconOrder parameter is less than 15, the MLME sets macBattLifeExt to + * the value of the BatteryLifeExtension parameter. If the BeaconOrder parameter + * equals 15, the value of the BatteryLifeExtension parameter is ignored. + * [1] pg. 106 + */ - /* TODO: Finish starting beacon enabled network */ - return -ENOTTY; + priv->sf_spec.ble = req->battlifeext; + + /* For now we just set the CAP Slot to 15 */ + + priv->sf_spec.final_capslot = 15; + + /* If the PAN coordinator parameter is set to TRUE, the MLME ignores the + * StartTime parameter and begins beacon transmissions immediately. + */ + + if (req->pancoord) + { + /* Update the beacon frame to start sending */ + + mac802154_updatebeacon(priv); + + /* Tell the radio to start transmitting beacon frames */ + + priv->radio->beaconstart(priv->radio, &priv->sf_spec, + &priv->beaconframe[priv->bf_ind]); + } + else + { + /* TODO: Finish non-PAN coordinator delayed start */ + + ret = -ENOTTY; + goto errout; + } } mac802154_givesem(&priv->exclsem); From bdf4836ac771fafac592d282e6ab618e1a10e550 Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Thu, 22 Jun 2017 00:45:34 -0400 Subject: [PATCH 2/3] ieee802154: Stubs out some SCAN.request code --- wireless/ieee802154/mac802154_scan.c | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/wireless/ieee802154/mac802154_scan.c b/wireless/ieee802154/mac802154_scan.c index 1ba9932288..d8121f1021 100644 --- a/wireless/ieee802154/mac802154_scan.c +++ b/wireless/ieee802154/mac802154_scan.c @@ -77,6 +77,71 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) { FAR struct ieee802154_privmac_s *priv = (FAR struct ieee802154_privmac_s *)mac; - return -ENOTTY; + int ret; + + if (req->duration > 15) + { + ret = -EINVAL; + goto errout; + } + + /* Need to get access to the ops semaphore since operations are serial. This + * must be done before locking the MAC so that we don't hold the MAC + */ + + ret = mac802154_takesem(&priv->opsem, true); + if (ret < 0) + { + ret = -EINVAL; + goto errout; + } + + ret = mac802154_takesem(&priv->exclsem, true); + if (ret < 0) + { + ret = -EINVAL; + goto errout; + } + + + switch (req->type) + { + case IEEE802154_SCANTYPE_PASSIVE: + { + + } + break; + case IEEE802154_SCANTYPE_ACTIVE: + { + ret = -ENOTTY; + goto errout_with_sem; + } + break; + case IEEE802154_SCANTYPE_ED: + { + ret = -ENOTTY; + goto errout_with_sem; + } + break; + case IEEE802154_SCANTYPE_ORPHAN: + { + ret = -ENOTTY; + goto errout_with_sem; + } + break; + default: + { + ret = -EINVAL; + goto errout_with_sem; + } + break; + } + +return OK; + +errout_with_sem; + mac802154_givesem(&priv->opsem); +errout: + return ret; } From da8afe9d9e32314e149d5e9fd1df5c5057ef0eed Mon Sep 17 00:00:00 2001 From: Anthony Merlino Date: Sun, 25 Jun 2017 18:07:50 -0400 Subject: [PATCH 3/3] ieee802154: Finishes transmission of beacon frames, and performing a passive scan --- configs/clicker2-stm32/mrf24j40-mac/defconfig | 5 +- drivers/wireless/ieee802154/mrf24j40.c | 176 ++++++- drivers/wireless/ieee802154/mrf24j40.h | 86 +-- .../wireless/ieee802154/ieee802154_mac.h | 133 +++-- .../wireless/ieee802154/ieee802154_radio.h | 4 +- wireless/ieee802154/Kconfig | 10 + wireless/ieee802154/README.txt | 8 + wireless/ieee802154/mac802154.c | 250 +++++++-- wireless/ieee802154/mac802154_assoc.c | 52 +- wireless/ieee802154/mac802154_getset.c | 47 +- wireless/ieee802154/mac802154_internal.h | 492 ++++++++++++------ wireless/ieee802154/mac802154_loopback.c | 8 +- wireless/ieee802154/mac802154_netdev.c | 4 +- wireless/ieee802154/mac802154_poll.c | 10 +- wireless/ieee802154/mac802154_reset.c | 16 +- wireless/ieee802154/mac802154_scan.c | 145 +++++- wireless/ieee802154/mac802154_scan.h | 66 +++ wireless/ieee802154/mac802154_start.c | 24 +- 18 files changed, 1163 insertions(+), 373 deletions(-) create mode 100644 wireless/ieee802154/README.txt create mode 100644 wireless/ieee802154/mac802154_scan.h diff --git a/configs/clicker2-stm32/mrf24j40-mac/defconfig b/configs/clicker2-stm32/mrf24j40-mac/defconfig index 1cef0e395c..7ee79a1d4a 100644 --- a/configs/clicker2-stm32/mrf24j40-mac/defconfig +++ b/configs/clicker2-stm32/mrf24j40-mac/defconfig @@ -984,8 +984,9 @@ CONFIG_WIRELESS=y CONFIG_WIRELESS_IEEE802154=y CONFIG_IEEE802154_DEFAULT_EADDR=0x00fade00deadbeef CONFIG_MAC802154_HPWORK=y -CONFIG_MAC802154_NNOTIF=6 CONFIG_MAC802154_NTXDESC=3 +CONFIG_MAC802154_NNOTIF=6 +CONFIG_MAC802154_NPANDESC=5 CONFIG_IEEE802154_IND_PREALLOC=20 CONFIG_IEEE802154_IND_IRQRESERVE=10 CONFIG_IEEE802154_MACDEV=y @@ -1167,10 +1168,10 @@ CONFIG_BUILTIN_PROXY_STACKSIZE=1024 CONFIG_EXAMPLES_NSH=y CONFIG_EXAMPLES_NSH_CXXINITIALIZE=y # CONFIG_EXAMPLES_NULL is not set -# CONFIG_EXAMPLES_NX is not set # CONFIG_EXAMPLES_NXFFS is not set # CONFIG_EXAMPLES_NXHELLO is not set # CONFIG_EXAMPLES_NXIMAGE is not set +# CONFIG_EXAMPLES_NX is not set # CONFIG_EXAMPLES_NXLINES is not set # CONFIG_EXAMPLES_NXTERM is not set # CONFIG_EXAMPLES_NXTEXT is not set diff --git a/drivers/wireless/ieee802154/mrf24j40.c b/drivers/wireless/ieee802154/mrf24j40.c index 2a553c9cb3..7f91a09e20 100644 --- a/drivers/wireless/ieee802154/mrf24j40.c +++ b/drivers/wireless/ieee802154/mrf24j40.c @@ -164,7 +164,7 @@ struct mrf24j40_radio_s struct ieee802154_addr_s addr; - uint8_t channel; /* 11 to 26 for the 2.4 GHz band */ + uint8_t chan; /* 11 to 26 for the 2.4 GHz band */ uint8_t devmode; /* device mode: device, coord, pancoord */ uint8_t paenabled; /* enable usage of PA */ uint8_t rxmode; /* Reception mode: Main, no CRC, promiscuous */ @@ -181,6 +181,8 @@ struct mrf24j40_radio_s bool csma_busy : 1; bool reschedule_csma : 1; + bool rxenabled : 1; + struct ieee802154_txdesc_s *gts_desc[MRF24J40_GTS_SLOTS]; bool gts_busy[MRF24J40_GTS_SLOTS]; }; @@ -232,6 +234,10 @@ static int mrf24j40_setsaddr(FAR struct mrf24j40_radio_s *dev, FAR const uint8_t *saddr); static int mrf24j40_seteaddr(FAR struct mrf24j40_radio_s *dev, FAR const uint8_t *eaddr); +static int mrf24j40_setcoordsaddr(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *saddr); +static int mrf24j40_setcoordeaddr(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *eaddr); static int mrf24j40_setdevmode(FAR struct mrf24j40_radio_s *dev, uint8_t mode); static int mrf24j40_settxpower(FAR struct mrf24j40_radio_s *dev, @@ -261,11 +267,13 @@ static int mrf24j40_rxenable(FAR struct ieee802154_radio_s *dev, bool enable); static int mrf24j40_req_rxenable(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_rxenable_req_s *req); static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, - FAR const struct ieee802154_superframespec_s *sf_spec, + FAR const struct ieee802154_superframespec_s *sfspec, FAR struct ieee802154_beaconframe_s *beacon); static int mrf24j40_beaconupdate(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_beaconframe_s *beacon); static int mrf24j40_beaconstop(FAR struct ieee802154_radio_s *radio); +static int mrf24j40_sfupdate(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sfspec); /**************************************************************************** * Private Data @@ -447,7 +455,7 @@ static int mrf24j40_get_attr(FAR struct ieee802154_radio_s *radio, switch (attr) { - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: { memcpy(&attrval->mac.eaddr[0], &dev->addr.eaddr[0], 8); ret = IEEE802154_STATUS_SUCCESS; @@ -467,6 +475,12 @@ static int mrf24j40_get_attr(FAR struct ieee802154_radio_s *radio, ret = IEEE802154_STATUS_SUCCESS; } break; + + case IEEE802154_ATTR_PHY_CHAN: + { + attrval->phy.chan = dev->chan; + ret = IEEE802154_STATUS_SUCCESS; + } default: ret = IEEE802154_STATUS_UNSUPPORTED_ATTRIBUTE; @@ -491,16 +505,30 @@ static int mrf24j40_set_attr(FAR struct ieee802154_radio_s *radio, } break; - case IEEE802154_ATTR_MAC_SHORT_ADDRESS: + case IEEE802154_ATTR_MAC_SADDR: { mrf24j40_setsaddr(dev, attrval->mac.saddr); ret = IEEE802154_STATUS_SUCCESS; } break; - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: { - mrf24j40_seteaddr(dev, &attrval->mac.eaddr[0]); + mrf24j40_seteaddr(dev, attrval->mac.eaddr); + ret = IEEE802154_STATUS_SUCCESS; + } + break; + + case IEEE802154_ATTR_MAC_COORD_SADDR: + { + mrf24j40_setcoordsaddr(dev, attrval->mac.coordsaddr); + ret = IEEE802154_STATUS_SUCCESS; + } + break; + + case IEEE802154_ATTR_MAC_COORD_EADDR: + { + mrf24j40_setcoordeaddr(dev, attrval->mac.coordeaddr); ret = IEEE802154_STATUS_SUCCESS; } break; @@ -527,6 +555,13 @@ static int mrf24j40_set_attr(FAR struct ieee802154_radio_s *radio, ret = IEEE802154_STATUS_SUCCESS; } break; + + case IEEE802154_ATTR_PHY_CHAN: + { + mrf24j40_setchannel(dev, attrval->phy.chan); + ret = IEEE802154_STATUS_SUCCESS; + } + break; default: ret = IEEE802154_STATUS_UNSUPPORTED_ATTRIBUTE; @@ -543,7 +578,7 @@ static int mrf24j40_req_rxenable(FAR struct ieee802154_radio_s *radio, } static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, - FAR const struct ieee802154_superframespec_s *sf_spec, + FAR const struct ieee802154_superframespec_s *sfspec, FAR struct ieee802154_beaconframe_s *beacon) { FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)radio; @@ -551,7 +586,7 @@ static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, uint32_t slpcal = 0; int reg; - if (sf_spec->pancoord) + if (sfspec->pancoord) { /* Set the PANCOORD (RXMCR 0x00<3>) bit = 1to configure as PAN coordinator */ @@ -588,7 +623,7 @@ static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, reg = mrf24j40_getreg(dev->spi, MRF24J40_ESLOTG1); reg &= ~MRF24J40_ESLOTG1_CAP; - reg |= sf_spec->final_capslot & MRF24J40_ESLOTG1_CAP; + reg |= sfspec->final_capslot & MRF24J40_ESLOTG1_CAP; mrf24j40_setreg(dev->spi, MRF24J40_ESLOTG1, reg); /* TODO: Add GTS related code. See pg 100 of datasheet */ @@ -643,7 +678,7 @@ static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, mrf24j40_setreg(dev->spi, MRF24J40_REMCNTL, (MRF24J40_REMCNT & 0xFF)); mrf24j40_setreg(dev->spi, MRF24J40_REMCNTH, ((MRF24J40_REMCNT >> 8) & 0xFF)); - maincnt = MRF24J40_MAINCNT(sf_spec->beaconorder, (slpcal * 50 / 16)); + maincnt = MRF24J40_MAINCNT(sfspec->beaconorder, (slpcal * 50 / 16)); mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT0, (maincnt & 0xFF)); mrf24j40_setreg(dev->spi, MRF24J40_MAINCNT1, ((maincnt >> 8) & 0xFF)); @@ -661,7 +696,7 @@ static int mrf24j40_beaconstart(FAR struct ieee802154_radio_s *radio, */ mrf24j40_setreg(dev->spi, MRF24J40_ORDER, - ((sf_spec->beaconorder << 4) & 0xF0) | (sf_spec->sforder & 0x0F)); + ((sfspec->beaconorder << 4) & 0xF0) | (sfspec->sforder & 0x0F)); } else { @@ -686,6 +721,41 @@ static int mrf24j40_beaconstop(FAR struct ieee802154_radio_s *radio) return -ENOTTY; } +static int mrf24j40_sfupdate(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sfspec) +{ + FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)radio; + int reg; + + reg = mrf24j40_getreg(dev->spi, MRF24J40_RXMCR); + + if (sfspec->pancoord) + { + reg |= MRF24J40_RXMCR_PANCOORD; + } + else + { + reg &= ~MRF24J40_RXMCR_PANCOORD; + } + mrf24j40_setreg(dev->spi, MRF24J40_RXMCR, reg); + + /* Program the CAP end slot (ESLOTG1 0x13<3:0>) value. */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_ESLOTG1); + reg &= ~MRF24J40_ESLOTG1_CAP; + reg |= sfspec->final_capslot & MRF24J40_ESLOTG1_CAP; + mrf24j40_setreg(dev->spi, MRF24J40_ESLOTG1, reg); + + /* Configure the BO (ORDER 0x10<7:4>) and SO (ORDER 0x10<3:0>) values. + * After configuring BO and SO, the beacon frame will be sent immediately. + */ + + mrf24j40_setreg(dev->spi, MRF24J40_ORDER, + ((sfspec->beaconorder << 4) & 0xF0) | (sfspec->sforder & 0x0F)); + + return OK; +} + /**************************************************************************** * Internal Functions ****************************************************************************/ @@ -899,7 +969,7 @@ static void mrf24j40_setreg(FAR struct spi_dev_s *spi, uint32_t addr, * Name: mrf24j40_getreg * * Description: - * Return the value of an MRF24J40 device register + * Return the value of an MRF24J40 device register* * ****************************************************************************/ @@ -1117,8 +1187,8 @@ static int mrf24j40_setchannel(FAR struct mrf24j40_radio_s *dev, uint8_t chan) mrf24j40_resetrfsm(dev); - dev->channel = chan; - //wlinfo("%u\n", (unsigned)chan); + dev->chan = chan; + wlinfo("%u\n", (unsigned)chan); return OK; } @@ -1188,6 +1258,51 @@ static int mrf24j40_seteaddr(FAR struct mrf24j40_radio_s *dev, return OK; } +/**************************************************************************** + * Name: mrf24j40_setcoordsaddr + * + * Description: + * Define the coordinator short address. The following addresses are special: + * FFFEh : Broadcast + * FFFFh : Unspecified + * + ****************************************************************************/ + +static int mrf24j40_setcoordsaddr(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *saddr) +{ + mrf24j40_setreg(dev->spi, MRF24J40_ASSOSADR0, saddr[0]); + mrf24j40_setreg(dev->spi, MRF24J40_ASSOSADR1, saddr[1]); + + IEEE802154_SADDRCOPY(dev->addr.saddr, saddr); + + wlinfo("%02X:%02X\n", saddr[1], saddr[0]); + return OK; +} + +/**************************************************************************** + * Name: mrf24j40_setcoordeaddr + * + * Description: + * Define the coordinator extended address. The following addresses are special: + * FFFFFFFFFFFFFFFFh : Unspecified + * + ****************************************************************************/ + +static int mrf24j40_setcoordeaddr(FAR struct mrf24j40_radio_s *dev, + FAR const uint8_t *eaddr) +{ + int i; + + for (i = 0; i < 8; i++) + { + mrf24j40_setreg(dev->spi, MRF24J40_ASSOEADR0 + i, eaddr[i]); + dev->addr.eaddr[i] = eaddr[i]; + } + + return OK; +} + /**************************************************************************** * Name: mrf24j40_setdevmode * @@ -1763,13 +1878,30 @@ static int mrf24j40_rxenable(FAR struct ieee802154_radio_s *radio, bool enable) FAR struct mrf24j40_radio_s *dev = (FAR struct mrf24j40_radio_s *)radio; uint8_t reg; + dev->rxenabled = enable; + + if (enable) { + /* Disable packet reception. See pg. 109 of datasheet */ + + mrf24j40_setreg(dev->spi, MRF24J40_BBREG1, MRF24J40_BBREG1_RXDECINV); + /* Enable rx int */ reg = mrf24j40_getreg(dev->spi, MRF24J40_INTCON); reg &= ~MRF24J40_INTCON_RXIE; mrf24j40_setreg(dev->spi, MRF24J40_INTCON, reg); + + /* Purge the RX buffer */ + + reg = mrf24j40_getreg(dev->spi, MRF24J40_RXFLUSH); + reg |= MRF24J40_RXFLUSH_RXFLUSH; + mrf24j40_setreg(dev->spi, MRF24J40_RXFLUSH, reg); + + /* Re-enable packet reception. See pg. 109 of datasheet */ + + mrf24j40_setreg(dev->spi, MRF24J40_BBREG1, 0); } else { @@ -1853,14 +1985,14 @@ done: mrf24j40_setreg(dev->spi, MRF24J40_RXFLUSH, 1); - /* Enable packet reception */ - - mrf24j40_setreg(dev->spi, MRF24J40_BBREG1, 0); - /* Only enable RX interrupt if we are to be listening when IDLE */ - if (dev->rxonidle) + if (dev->rxenabled) { + /* Enable packet reception */ + + mrf24j40_setreg(dev->spi, MRF24J40_BBREG1, 0); + reg = mrf24j40_getreg(dev->spi, MRF24J40_INTCON); reg &= ~MRF24J40_INTCON_RXIE; mrf24j40_setreg(dev->spi, MRF24J40_INTCON, reg); @@ -1900,7 +2032,6 @@ static void mrf24j40_irqworker(FAR void *arg) /* Read and store INTSTAT - this clears the register. */ intstat = mrf24j40_getreg(dev->spi, MRF24J40_INTSTAT); - wlinfo("INT%02X\n", intstat); /* Do work according to the pending interrupts */ @@ -1919,7 +2050,7 @@ static void mrf24j40_irqworker(FAR void *arg) mrf24j40_setreg(dev->spi, MRF24J40_INTCON, reg); } - if ((intstat & MRF24J40_INTSTAT_RXIF)) + if ((intstat & MRF24J40_INTSTAT_RXIF) && dev->rxenabled) { /* A packet was received, retrieve it */ @@ -2057,10 +2188,13 @@ FAR struct ieee802154_radio_s *mrf24j40_init(FAR struct spi_dev_s *spi, dev->radio.beaconstart = mrf24j40_beaconstart; dev->radio.beaconupdate = mrf24j40_beaconupdate; dev->radio.beaconstop = mrf24j40_beaconstop; + dev->radio.sfupdate = mrf24j40_sfupdate; dev->lower = lower; dev->spi = spi; + + dev->rxenabled = false; mrf24j40_initialize(dev); mrf24j40_setchannel(dev, 11); diff --git a/drivers/wireless/ieee802154/mrf24j40.h b/drivers/wireless/ieee802154/mrf24j40.h index 9d5cd070ff..e4e4e8ba5e 100644 --- a/drivers/wireless/ieee802154/mrf24j40.h +++ b/drivers/wireless/ieee802154/mrf24j40.h @@ -118,41 +118,41 @@ #define MRF24J40_SLPCAL1 (MRF24J40_LONGREG_BASE + 0x0A) #define MRF24J40_SLPCAL2 (MRF24J40_LONGREG_BASE + 0x0B) #define MRF24J40_RFSTATE (MRF24J40_LONGREG_BASE + 0x0F) -#define MRF24J40_RSSI 0x80000210 -#define MRF24J40_SLPCON0 0x80000211 -#define MRF24J40_SLPCON1 0x80000220 -#define MRF24J40_WAKETIMEL 0x80000222 -#define MRF24J40_WAKETIMEH 0x80000223 -#define MRF24J40_REMCNTL 0x80000224 -#define MRF24J40_REMCNTH 0x80000225 -#define MRF24J40_MAINCNT0 0x80000226 -#define MRF24J40_MAINCNT1 0x80000227 -#define MRF24J40_MAINCNT2 0x80000228 -#define MRF24J40_MAINCNT3 0x80000229 -#define MRF24J40_TESTMODE 0x8000022F -#define MRF24J40_ASSOEADR0 0x80000230 -#define MRF24J40_ASSOEADR1 0x80000231 -#define MRF24J40_ASSOEADR2 0x80000232 -#define MRF24J40_ASSOEADR3 0x80000233 -#define MRF24J40_ASSOEADR4 0x80000234 -#define MRF24J40_ASSOEADR5 0x80000235 -#define MRF24J40_ASSOEADR6 0x80000236 -#define MRF24J40_ASSOEADR7 0x80000237 -#define MRF24J40_ASSOSADR0 0x80000238 -#define MRF24J40_ASSOSADR1 0x80000239 -#define MRF24J40_UPNONCE0 0x80000240 -#define MRF24J40_UPNONCE1 0x80000241 -#define MRF24J40_UPNONCE2 0x80000242 -#define MRF24J40_UPNONCE3 0x80000243 -#define MRF24J40_UPNONCE4 0x80000244 -#define MRF24J40_UPNONCE5 0x80000245 -#define MRF24J40_UPNONCE6 0x80000246 -#define MRF24J40_UPNONCE7 0x80000247 -#define MRF24J40_UPNONCE8 0x80000248 -#define MRF24J40_UPNONCE9 0x80000249 -#define MRF24J40_UPNONCE10 0x8000024A -#define MRF24J40_UPNONCE11 0x8000024B -#define MRF24J40_UPNONCE12 0x8000024C +#define MRF24J40_RSSI (MRF24J40_LONGREG_BASE + 0x10) +#define MRF24J40_SLPCON0 (MRF24J40_LONGREG_BASE + 0x11) +#define MRF24J40_SLPCON1 (MRF24J40_LONGREG_BASE + 0x20) +#define MRF24J40_WAKETIMEL (MRF24J40_LONGREG_BASE + 0x22) +#define MRF24J40_WAKETIMEH (MRF24J40_LONGREG_BASE + 0x23) +#define MRF24J40_REMCNTL (MRF24J40_LONGREG_BASE + 0x24) +#define MRF24J40_REMCNTH (MRF24J40_LONGREG_BASE + 0x25) +#define MRF24J40_MAINCNT0 (MRF24J40_LONGREG_BASE + 0x26) +#define MRF24J40_MAINCNT1 (MRF24J40_LONGREG_BASE + 0x27) +#define MRF24J40_MAINCNT2 (MRF24J40_LONGREG_BASE + 0x28) +#define MRF24J40_MAINCNT3 (MRF24J40_LONGREG_BASE + 0x29) +#define MRF24J40_TESTMODE (MRF24J40_LONGREG_BASE + 0x2F) +#define MRF24J40_ASSOEADR0 (MRF24J40_LONGREG_BASE + 0x30) +#define MRF24J40_ASSOEADR1 (MRF24J40_LONGREG_BASE + 0x31) +#define MRF24J40_ASSOEADR2 (MRF24J40_LONGREG_BASE + 0x32) +#define MRF24J40_ASSOEADR3 (MRF24J40_LONGREG_BASE + 0x33) +#define MRF24J40_ASSOEADR4 (MRF24J40_LONGREG_BASE + 0x34) +#define MRF24J40_ASSOEADR5 (MRF24J40_LONGREG_BASE + 0x35) +#define MRF24J40_ASSOEADR6 (MRF24J40_LONGREG_BASE + 0x36) +#define MRF24J40_ASSOEADR7 (MRF24J40_LONGREG_BASE + 0x37) +#define MRF24J40_ASSOSADR0 (MRF24J40_LONGREG_BASE + 0x38) +#define MRF24J40_ASSOSADR1 (MRF24J40_LONGREG_BASE + 0x39) +#define MRF24J40_UPNONCE0 (MRF24J40_LONGREG_BASE + 0x40) +#define MRF24J40_UPNONCE1 (MRF24J40_LONGREG_BASE + 0x41) +#define MRF24J40_UPNONCE2 (MRF24J40_LONGREG_BASE + 0x42) +#define MRF24J40_UPNONCE3 (MRF24J40_LONGREG_BASE + 0x43) +#define MRF24J40_UPNONCE4 (MRF24J40_LONGREG_BASE + 0x44) +#define MRF24J40_UPNONCE5 (MRF24J40_LONGREG_BASE + 0x45) +#define MRF24J40_UPNONCE6 (MRF24J40_LONGREG_BASE + 0x46) +#define MRF24J40_UPNONCE7 (MRF24J40_LONGREG_BASE + 0x47) +#define MRF24J40_UPNONCE8 (MRF24J40_LONGREG_BASE + 0x48) +#define MRF24J40_UPNONCE9 (MRF24J40_LONGREG_BASE + 0x49) +#define MRF24J40_UPNONCE10 (MRF24J40_LONGREG_BASE + 0x4A) +#define MRF24J40_UPNONCE11 (MRF24J40_LONGREG_BASE + 0x4B) +#define MRF24J40_UPNONCE12 (MRF24J40_LONGREG_BASE + 0x4C) /* INTSTAT bits */ @@ -265,4 +265,20 @@ #define MRF24J40_SLPACK_WAKECNT0_6 0x7F #define MRF24J40_SLPACK_SLPACK 0x80 +/* RXFLUSH bits */ + +#define MRF24J40_RXFLUSH_RXFLUSH 0x01 +#define MRF24J40_RXFLUSH_BCNONLY 0x02 +#define MRF24J40_RXFLUSH_DATAONLY 0x04 +#define MRF24J40_RXFLUSH_CMDONLY 0x08 +#define MRF24J40_RXFLUSH_WAKEPAD 0x20 +#define MRF24J40_RXFLUSH_WAKEPOL 0x40 + +#define MRF24J40_RXFLUSH_SHIFT_RXFLUSH 0 +#define MRF24J40_RXFLUSH_SHIFT_BCNONLY 1 +#define MRF24J40_RXFLUSH_SHIFT_DATAONLY 2 +#define MRF24J40_RXFLUSH_SHIFT_CMDONLY 3 +#define MRF24J40_RXFLUSH_SHIFT_WAKEPAD 5 +#define MRF24J40_RXFLUSH_SHIFT_WAKEPOL 6 + #endif /* __DRIVERS_WIRELESS_IEEE802154_MRF24J40_H */ diff --git a/include/nuttx/wireless/ieee802154/ieee802154_mac.h b/include/nuttx/wireless/ieee802154/ieee802154_mac.h index e2198d54cc..c22c0372ad 100644 --- a/include/nuttx/wireless/ieee802154/ieee802154_mac.h +++ b/include/nuttx/wireless/ieee802154/ieee802154_mac.h @@ -60,6 +60,14 @@ /**************************************************************************** * Pre-Processor Definitions ****************************************************************************/ +/* Configuration ************************************************************/ + +#if !defined(CONFIG_MAC802154_NPANDESC) || CONFIG_MAC802154_NPANDESC <= 0 +# undef CONFIG_MAC802154_NPANDESC +# define CONFIG_MAC802154_NPANDESC 5 +#endif + +#define MAC802154_NPANDESC CONFIG_MAC802154_NPANDESC /* IEEE 802.15.4 address macros */ /* Copy a an IEEE 802.15.4 address */ @@ -97,8 +105,6 @@ #define IEEE802154_SADDR_BCAST ((uint8_t[]){0xFE,0xFF}) #define IEEE802154_EADDR_UNSPEC ((uint8_t[]){0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}) -/* Configuration ************************************************************/ -/* None at the moment */ /* IEEE 802.15.4 MAC Character Driver IOCTL Commands ************************/ @@ -111,9 +117,9 @@ * - Response * - Confirm * - * Of these, Request and Response primitives are sent from the next highest layer - * to the MLME. Indication and Confirm primitives are used to notify the next - * highest layer of changes or actions that have taken place. + * Of these, Request and Response primitives are sent from the next highest + * layer to the MLME. Indication and Confirm primitives are used to notify the + * next highest layer of changes or actions that have taken place. * * The MAC802154 character driver exposed here provides IOCTL hooks for all * Request and Response primitives. @@ -178,8 +184,35 @@ #define IEEE802154_SFSPEC_SHIFT_PANCOORD 14 /* PAN Coordinator, bit 14 */ #define IEEE802154_SFSPEC_SHIFT_ASSOCPERMIT 15 /* Association Permit, bit 15 */ +/* GTS Specification field masks, 1 byte + * Seee IEEE 802.15.4/2011 5.2.2.1.3 page 63 + */ + +#define IEEE802154_GTSSPEC_DESCCOUNT 0x07 /* GTS Desc. count, bits 0-2 */ +#define IEEE802154_GTSSPEC_PERMIT 0x80 /* GTS Desc. count, bit 7 */ + +#define IEEE802154_GTSSPEC_SHIFT_DESCCOUNT 0 /* GTS Desc. count, bits 0-2 */ +#define IEEE802154_GTSSPEC_SHIFT_PERMIT 7 /* GTS Desc. count, bit 7 */ + +/* GTS Directions field masks, 1 byte + * Seee IEEE 802.15.4/2011 5.2.2.1.3 page 63 + */ + +#define IEEE802154_GTSDIR_MASK 0x7F /* GTS Directions Mask, bits 0-6 */ + +#define IEEE802154_GTSDIR_SHIFT_MASK 0 /* GTS Directions Mask, bits 0-6 */ + +/* Pending address specifications field masks, 1 byte + * See IEEE 802.15.4/2011 5.2.2.1.6 page 64 + */ + +#define IEEE802154_PENDADDR_NSADDR 0x07 /* # of short addresses, bits 0-2 */ +#define IEEE802154_PENDADDR_NEADDR 0x70 /* # of extended addresses, bits 4-6 */ + +#define IEEE802154_PENDADDR_SHIFT_NSADDR 0 /* # of short addresses, bits 0-2 */ +#define IEEE802154_PENDADDR_SHIFT_NEADDR 4 /* # of extended addresses, bits 4-6 */ + /* Capability Information Bitfield - * */ #define IEEE802154_CAPABILITY_DEVTYPE 0x02 @@ -237,7 +270,6 @@ #define MAX_ORPHAN_ADDR 32 /* REVISIT */ - /**************************************************************************** * Public Types ****************************************************************************/ @@ -282,6 +314,7 @@ enum ieee802154_status_e IEEE802154_STATUS_TX_ACTIVE, IEEE802154_STATUS_UNAVAILABLE_KEY, IEEE802154_STATUS_UNSUPPORTED_ATTRIBUTE, + IEEE802154_STATUS_LIMITREACHED, }; static const char *IEEE802154_STATUS_STRING[] = @@ -309,6 +342,7 @@ static const char *IEEE802154_STATUS_STRING[] = "Tx active", "Unavailable key", "Unsupported attribute", + "Limit reached", }; /* IEEE 802.15.4 PHY/MAC PIB attributes IDs */ @@ -317,7 +351,7 @@ enum ieee802154_attr_e { /* PHY PIB Attributes */ - IEEE802154_ATTR_PHY_CURRENT_CHANNEL = 0x00, + IEEE802154_ATTR_PHY_CHAN = 0x00, IEEE802154_ATTR_PHY_CHANNELS_SUPPORTED, IEEE802154_ATTR_PHY_TX_POWER_TOLERANCE, IEEE802154_ATTR_PHY_TX_POWER, @@ -357,7 +391,7 @@ enum ieee802154_attr_e /* MAC PIB Attributes */ - IEEE802154_ATTR_MAC_EXTENDED_ADDR = 0x40, + IEEE802154_ATTR_MAC_EADDR = 0x40, IEEE802154_ATTR_MAC_ACK_WAIT_DUR, IEEE802154_ATTR_MAC_ASSOCIATED_PANCOORD, IEEE802154_ATTR_MAC_ASSOCIATION_PERMIT, @@ -369,8 +403,8 @@ enum ieee802154_attr_e IEEE802154_ATTR_MAC_BEACON_ORDER, IEEE802154_ATTR_MAC_BEACON_TX_TIME, IEEE802154_ATTR_MAC_BSN, - IEEE802154_ATTR_MAC_COORD_EXT_ADDR, - IEEE802154_ATTR_MAC_COORD_SHORT_ADDR, + IEEE802154_ATTR_MAC_COORD_EADDR, + IEEE802154_ATTR_MAC_COORD_SADDR, IEEE802154_ATTR_MAC_DSN, IEEE802154_ATTR_MAC_GTS_PERMIT, IEEE802154_ATTR_MAC_MAX_BE, @@ -386,7 +420,7 @@ enum ieee802154_attr_e IEEE802154_ATTR_MAC_RESPONSE_WAIT_TIME, IEEE802154_ATTR_MAC_RX_ON_WHEN_IDLE, IEEE802154_ATTR_MAC_SECURITY_ENABLED, - IEEE802154_ATTR_MAC_SHORT_ADDRESS, + IEEE802154_ATTR_MAC_SADDR, IEEE802154_ATTR_MAC_SUPERFRAME_ORDER, IEEE802154_ATTR_MAC_SYNC_SYMBOL_OFFSET, IEEE802154_PIB_MAC_TIMESTAMP_SUPPORT, @@ -539,20 +573,20 @@ struct ieee802154_superframespec_s uint16_t assocpermit : 1; /* 1 if coordinator is accepting associaton */ }; -struct ieee802154_pan_desc_s +struct ieee802154_pandesc_s { /* The coordinator address of the received beacon frame */ - struct ieee802154_addr_s coord_addr; + struct ieee802154_addr_s coordaddr; - uint8_t channel; /* current channel occupied by the network */ - uint8_t channel_page; /* current channel page occupied by the network */ + uint8_t chan; /* current channel occupied by the network */ + uint8_t chpage; /* current channel page occupied by the network */ /* The superframe specifications received in the beacon frame */ - struct ieee802154_superframespec_s superframespec; + struct ieee802154_superframespec_s sfspec; - uint8_t gts_permit; /* 0=No GTS requests allowed + uint8_t gtspermit; /* 0=No GTS requests allowed * 1=GTS request allowed */ uint8_t lqi; /* Link Quality Indication of the beacon */ uint32_t timestamp; /* Time at which the beacon frame was received @@ -591,8 +625,8 @@ union ieee802154_macattr_u uint8_t saddr[IEEE802154_SADDRSIZE]; uint8_t panid[IEEE802154_PANIDSIZE]; - uint8_t coord_eaddr[IEEE802154_EADDRSIZE]; - uint8_t coord_saddr[IEEE802154_SADDRSIZE]; + uint8_t coordeaddr[IEEE802154_EADDRSIZE]; + uint8_t coordsaddr[IEEE802154_SADDRSIZE]; enum ieee802154_devmode_e devmode; @@ -636,7 +670,7 @@ union ieee802154_macattr_u union ieee802154_phyattr_u { - uint8_t channel; + uint8_t chan; int32_t txpwr; uint32_t symdur_picosec; /* TODO: Fill this out as we implement supported get/set commands */ @@ -862,8 +896,38 @@ struct ieee802154_purge_req_s struct ieee802154_assoc_req_s { - uint8_t chnum; /* Channel number to attempt association */ - uint8_t chpage; /* Channel page to attempt association */ + uint8_t chan; /* Channel number to attempt association */ + uint8_t chpage; /* Channel page to attempt association */ + + /* TODO: + * This is a non-standard field. I believe there is a catch 22 in the + * standard and until I can figure it out, I'm adding this boolean to let the + * application tell the MAC whether it is trying to assocaite with a beacon + * enabled PAN or non-beacon enabled PAN. If it is beacon-enabled, the MAC + * will track the beacon first before transmitting the association. This can + * take some time depending on the beacon interval. If the PAN is non-beacon + * enabled, the association request is sent immediately via CSMA. + * + * The catch 22: The standard outlines the procedure for associating: reset + * the MAC, scan to find PAN's and pass coordinator address info to + * application, application calls associate passing address info of + * coordinator. Which sounds good. The problem is that the primitive has no + * field for determining if the PAN we are trying to join is beacon enabled + * or not. Which means we don't know whether to tranmsit immediately or try + * to track the beacon. The standard does say that ALL command frames should + * be sent during the Contention Access Period (CAP), but how could you send + * it at the rigth tiem, if you are not tracking the beacon. What's worse is + * in the association section, it says if you are tracking the beacon, to + * send the association request during the CAP. But how can you track the + * beacon if you are not associated. Normally tracking the beacon would be + * triggered by the SYNC.request primitive. But from my understanding that + * primitive is intended to be used AFTER association since it requires the + * MAC to already have a coordinator address and PAN ID so that it can track + * the beacon frames properly. Which, of course, how could the MAC have that + * info if it is not associated. + */ + + bool beacon; /* Coordinator Address with which to associate */ @@ -1054,7 +1118,7 @@ struct ieee802154_beaconnotify_ind_s /* PAN descriptor for the received beacon */ - struct ieee802154_pan_desc_s pan_desc; + struct ieee802154_pandesc_s pandesc; /* Beacon pending addresses */ @@ -1255,20 +1319,17 @@ struct ieee802154_scan_req_s { enum ieee802154_scantype_e type; uint8_t duration; - uint8_t ch_page; + uint8_t chpage; + uint8_t channels[15]; + uint8_t numchan; #ifdef CONFIG_IEEE802154_SECURITY /* Security information if enabled */ struct ieee802154_security_s security; #endif - - uint8_t channels[1]; }; -#define SIZEOF_IEEE802154_SCAN_REQ_S(n) \ - (sizeof(struct ieee802154_scan_req_s) + (n) - 1) - /***************************************************************************** * Primitive: MLME-SCAN.confirm * @@ -1281,10 +1342,12 @@ struct ieee802154_scan_conf_s { enum ieee802154_status_e status; enum ieee802154_scantype_e type; - uint8_t ch_page; - uint8_t num_channels; - - /* TODO: Figure out how to handle missing primitive semantics. See standard. */ + uint8_t chpage; + uint8_t unscanned[15]; + uint8_t numunscanned; + uint8_t numdesc; + struct ieee802154_pandesc_s pandescs[MAC802154_NPANDESC]; + uint8_t edlist[MAC802154_NPANDESC]; }; /***************************************************************************** @@ -1335,7 +1398,7 @@ struct ieee802154_set_req_s struct ieee802154_start_req_s { uint8_t panid[IEEE802154_PANIDSIZE]; - uint8_t chnum; + uint8_t chan; uint8_t chpage; uint32_t starttime : 24; diff --git a/include/nuttx/wireless/ieee802154/ieee802154_radio.h b/include/nuttx/wireless/ieee802154/ieee802154_radio.h index fe183a614a..aeab4f9889 100644 --- a/include/nuttx/wireless/ieee802154/ieee802154_radio.h +++ b/include/nuttx/wireless/ieee802154/ieee802154_radio.h @@ -134,11 +134,13 @@ struct ieee802154_radio_s CODE int (*req_rxenable)(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_rxenable_req_s *req); CODE int (*beaconstart)(FAR struct ieee802154_radio_s *radio, - FAR const struct ieee802154_superframespec_s *sf_spec, + FAR const struct ieee802154_superframespec_s *sfspec, FAR struct ieee802154_beaconframe_s *beacon); CODE int (*beaconupdate)(FAR struct ieee802154_radio_s *radio, FAR struct ieee802154_beaconframe_s *beacon); CODE int (*beaconstop)(FAR struct ieee802154_radio_s *radio); + CODE int (*sfupdate)(FAR struct ieee802154_radio_s *radio, + FAR const struct ieee802154_superframespec_s *sfspec); }; #ifdef __cplusplus diff --git a/wireless/ieee802154/Kconfig b/wireless/ieee802154/Kconfig index f6a93d709a..f01ae562d7 100644 --- a/wireless/ieee802154/Kconfig +++ b/wireless/ieee802154/Kconfig @@ -73,6 +73,16 @@ config MAC802154_NNOTIF receiver with an allocated notification structure indicating the event. The events are primitives such as Association Indication etc. +config MAC802154_NPANDESC + int "Number of PAN descriptors" + default 5 + ---help--- + Configured number of PAN descriptors Default: 5 + + When performing a scan to find a suitable PAN, the MAC must store + information for all unique beacons received. This is the number of unique + descriptors that can be held before the scan cancels with LIMIT_REACHED. + config IEEE802154_IND_PREALLOC int "Number of pre-allocated meta-data structures" default 20 diff --git a/wireless/ieee802154/README.txt b/wireless/ieee802154/README.txt new file mode 100644 index 0000000000..3970638ec3 --- /dev/null +++ b/wireless/ieee802154/README.txt @@ -0,0 +1,8 @@ +This will eventually hold some general documentation for the IEEE 802.15.4 +MAC implementation. + +NOTE: Throughout the code, excerpts from the standard are quoted as to make +the code thoroughly readable. I have tried, to always put a reference [1] and +page number. Below is the references that correspond with the reference number + +[1] \ No newline at end of file diff --git a/wireless/ieee802154/mac802154.c b/wireless/ieee802154/mac802154.c index 0cf0a735c0..248dde8082 100644 --- a/wireless/ieee802154/mac802154.c +++ b/wireless/ieee802154/mac802154.c @@ -59,6 +59,7 @@ #include "mac802154_notif.h" #include "mac802154_internal.h" #include "mac802154_assoc.h" +#include "mac802154_scan.h" #include "mac802154_data.h" #include "mac802154_poll.h" @@ -98,10 +99,12 @@ static void mac802154_timeout_expiry(int argc, wdparm_t arg, ...); static uint32_t mac802154_symtoticks(FAR struct ieee802154_privmac_s *priv, uint32_t symbols); -static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_data_ind_s *ind); -static void mac802154_rx_dataframe(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_data_ind_s *ind); +static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind); +static void mac802154_rxdataframe(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind); +static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind); /**************************************************************************** * Private Functions @@ -438,20 +441,20 @@ void mac802154_updatebeacon(FAR struct ieee802154_privmac_s *priv) beacon->bf_data[beacon->bf_len + 1] = 0; IEEE802154_SETBEACONORDER(beacon->bf_data, beacon->bf_len, - priv->sf_spec.beaconorder); + priv->sfspec.beaconorder); IEEE802154_SETSFORDER(beacon->bf_data, beacon->bf_len, - priv->sf_spec.sforder); + priv->sfspec.sforder); IEEE802154_SETFINCAPSLOT(beacon->bf_data, beacon->bf_len, - priv->sf_spec.final_capslot); - if (priv->sf_spec.ble) + priv->sfspec.final_capslot); + if (priv->sfspec.ble) { IEEE802154_SETBLE(beacon->bf_data, beacon->bf_len); } - if (priv->sf_spec.pancoord) + if (priv->sfspec.pancoord) { IEEE802154_SETPANCOORD(beacon->bf_data, beacon->bf_len); } - if (priv->sf_spec.assocpermit) + if (priv->sfspec.assocpermit) { IEEE802154_SETASSOCPERMIT(beacon->bf_data, beacon->bf_len); } @@ -546,10 +549,10 @@ void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, * aBaseSuperframeDuration. [1] pg. 129 */ - if (priv->sf_spec.beaconorder < 15) + if (priv->sfspec.beaconorder < 15) { symbols = priv->trans_persisttime * - (IEEE802154_BASE_SUPERFRAME_DURATION * (1 << priv->sf_spec.beaconorder)); + (IEEE802154_BASE_SUPERFRAME_DURATION * (1 << priv->sfspec.beaconorder)); } else { @@ -562,7 +565,7 @@ void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, /* Make sure the beacon gets updated */ - if (priv->sf_spec.beaconorder < 15) + if (priv->sfspec.beaconorder < 15) { priv->beaconupdate = true; } @@ -1056,7 +1059,7 @@ static void mac802154_rxframe_worker(FAR void *arg) { case IEEE802154_FRAME_DATA: { - mac802154_rx_dataframe(priv, ind); + mac802154_rxdataframe(priv, ind); } break; @@ -1072,33 +1075,43 @@ static void mac802154_rxframe_worker(FAR void *arg) switch (cmdtype) { case IEEE802154_CMD_ASSOC_REQ: + wlinfo("Assoc request received\n"); mac802154_rx_assocreq(priv, ind); break; case IEEE802154_CMD_ASSOC_RESP: + wlinfo("Assoc response received\n"); mac802154_rx_assocresp(priv, ind); break; case IEEE802154_CMD_DISASSOC_NOT: + wlinfo("Disassoc notif received\n"); break; case IEEE802154_CMD_DATA_REQ: - mac802154_rx_datareq(priv, ind); + wlinfo("Data request received\n"); + mac802154_rxdatareq(priv, ind); break; case IEEE802154_CMD_PANID_CONF_NOT: + wlinfo("PAN ID Conflict notif received\n"); break; case IEEE802154_CMD_ORPHAN_NOT: + wlinfo("Orphan notif received\n"); + break; break; case IEEE802154_CMD_BEACON_REQ: + wlinfo("Beacon request received\n"); break; case IEEE802154_CMD_COORD_REALIGN: + wlinfo("Coord realign received\n"); break; case IEEE802154_CMD_GTS_REQ: + wlinfo("GTS request received\n"); break; } @@ -1110,15 +1123,9 @@ static void mac802154_rxframe_worker(FAR void *arg) case IEEE802154_FRAME_BEACON: { - /* TODO: Add logic here to handle extracting association response from - * coordinator if beacon tracking was enabled during the Association - * operation. - * - * mac802154_txdesc_alloc(priv, &respdec, false); - * mac802154_createdatareq(priv, &req->coordaddr, - * IEEE802154_ADDRMODE_EXTENDED, respdesc); - * sq_addlast((FAR sq_entry_t *)respdesc, &priv->csma_queue); - */ + wlinfo("Beacon frame received\n"); + mac802154_rxbeaconframe(priv, ind); + ieee802154_ind_free(ind); } break; @@ -1137,7 +1144,7 @@ static void mac802154_rxframe_worker(FAR void *arg) } /**************************************************************************** - * Name: mac802154_rx_dataframe + * Name: mac802154_rxdataframe * * Description: * Function called from the generic RX Frame worker to parse and handle the @@ -1145,8 +1152,8 @@ static void mac802154_rxframe_worker(FAR void *arg) * ****************************************************************************/ -static void mac802154_rx_dataframe(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_data_ind_s *ind) +static void mac802154_rxdataframe(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind) { FAR struct ieee802154_notif_s *notif; @@ -1326,7 +1333,7 @@ notify_without_lock: } /**************************************************************************** - * Name: mac802154_rx_datareq + * Name: mac802154_rxdatareq * * Description: * Function called from the generic RX Frame worker to parse and handle the @@ -1334,7 +1341,7 @@ notify_without_lock: * ****************************************************************************/ -static void mac802154_rx_datareq(FAR struct ieee802154_privmac_s *priv, +static void mac802154_rxdatareq(FAR struct ieee802154_privmac_s *priv, FAR struct ieee802154_data_ind_s *ind) { FAR struct ieee802154_txdesc_s *txdesc; @@ -1529,6 +1536,189 @@ static void mac802154_sfevent(FAR const struct ieee802154_radiocb_s *radiocb, mac802154_givesem(&priv->exclsem); } +/**************************************************************************** + * Name: mac802154_rxbeaconframe + * + * Description: + * Function called from the generic RX Frame worker to parse and handle the + * reception of a beacon frame. + * + ****************************************************************************/ + +static void mac802154_rxbeaconframe(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_data_ind_s *ind) +{ + FAR struct iob_s *iob = ind->frame; + struct ieee802154_pandesc_s pandesc; + FAR struct ieee802154_txdesc_s *respdesc; + uint8_t numgtsdesc; + uint8_t gtsdirmask; + uint8_t npendsaddr; + uint8_t npendeaddr; + int i; + + /* Copy the coordinator address and channel info into the pan descriptor */ + + memcpy(&pandesc.coordaddr, &ind->src, sizeof(struct ieee802154_addr_s)); + pandesc.chan = priv->currscan.channels[priv->scanindex]; + pandesc.chpage = priv->currscan.chpage; + pandesc.lqi = ind->lqi; + pandesc.timestamp = ind->timestamp; + + /* Parse the superframe specification field */ + + pandesc.sfspec.beaconorder = IEEE802154_GETBEACONORDER(iob->io_data, + iob->io_offset); + + pandesc.sfspec.sforder = IEEE802154_GETSFORDER(iob->io_data, iob->io_offset); + pandesc.sfspec.final_capslot = IEEE802154_GETFINCAPSLOT(iob->io_data, + iob->io_offset); + pandesc.sfspec.ble = IEEE802154_GETBLE(iob->io_data, iob->io_offset); + pandesc.sfspec.pancoord = IEEE802154_GETPANCOORD(iob->io_data, iob->io_offset); + pandesc.sfspec.assocpermit = IEEE802154_GETASSOCPERMIT(iob->io_data, + iob->io_offset); + iob->io_offset += 2; + + /* Parse the GTS Specification field */ + + numgtsdesc = IEEE802154_GETGTSDESCCOUNT(iob->io_data, iob->io_offset); + pandesc.gtspermit = IEEE802154_GETGTSPERMIT(iob->io_data, iob->io_offset); + iob->io_offset++; + + /* We only need to parse the rest of the frame if we are not performing a + * scan + */ + + if (priv->curr_op == MAC802154_OP_SCAN) + { + /* Check to see if we already have a frame from this coordinator */ + + for (i = 0; i < priv->npandesc; i++) + { + if (priv->currscan.channels[priv->scanindex] != priv->pandescs[i].chan) + { + continue; + } + + if (memcmp(&ind->src, &priv->pandescs[i].coordaddr, + sizeof(struct ieee802154_addr_s))) + { + continue; + } + + /* The beacon is the same as another, so discard it */ + + return; + } + + /* Copy the pan desc to the list of pan desc */ + + memcpy(&priv->pandescs[priv->npandesc], &pandesc, + sizeof(struct ieee802154_pandesc_s)); + priv->npandesc++; + + if (priv->npandesc == MAC802154_NPANDESC) + { + mac802154_scanfinish(priv, IEEE802154_STATUS_LIMITREACHED); + } + } + else + { + /* If there are any GTS descriptors, handle the GTS Directions and + * GTS List fields + */ + + if (numgtsdesc > 0) + { + gtsdirmask = IEEE802154_GETGTSDIRMASK(iob->io_data, iob->io_offset); + iob->io_offset++; + + for (i = 0; i < numgtsdesc; i++) + { + /* For now we just discard the data by skipping over it */ + + iob->io_offset += 3; + } + } + + /* Pending address fields. Min 1 byte, the Pending Address Specification */ + + npendsaddr = IEEE802154_GETNPENDSADDR(iob->io_data, iob->io_offset); + npendeaddr = IEEE802154_GETNPENDEADDR(iob->io_data, iob->io_offset); + iob->io_offset++; + + /* The pending address field tells us whether or not there is any data + * pending for us. + */ + + for (i = 0; i < npendsaddr; i++) + { + /* If the short address matches our short address */ + + if (IEEE802154_SADDRCMP(&iob->io_data[iob->io_offset], priv->addr.saddr)) + { + /* TODO: Handle data pending in coordinator for us */ + } + iob->io_offset += IEEE802154_SADDRSIZE; + } + + for (i = 0; i < npendeaddr; i++) + { + /* If the extended address matches our extended address */ + + if (IEEE802154_EADDRCMP(&iob->io_data[iob->io_offset], priv->addr.eaddr)) + { + /* If we are associating, polling, or if macAutoRequest is TRUE, + * extract the data. + */ + + if ((priv->autoreq) || (priv->curr_op == MAC802154_OP_ASSOC) || + (priv->curr_op == MAC802154_OP_POLL)) + { + mac802154_txdesc_alloc(priv, &respdesc, false); + + mac802154_createdatareq(priv, &priv->pandesc.coordaddr, + IEEE802154_ADDRMODE_EXTENDED, respdesc); + + if (priv->curr_op == MAC802154_OP_ASSOC || + priv->curr_op == MAC802154_OP_POLL) + { + priv->curr_cmd = IEEE802154_CMD_DATA_REQ; + } + + /* Link the transaction into the CSMA transaction list */ + + sq_addlast((FAR sq_entry_t *)respdesc, &priv->csma_queue); + + /* Notify the radio driver that there is data available */ + + priv->radio->txnotify(priv->radio, false); + } + } + iob->io_offset += IEEE802154_EADDRSIZE; + } + + /* TODO: Process incoming beacon payload + * If there is anything left in the frame, process it as the beacon payload + */ + + /* Check the superframe structure and update the appropriate attributes. */ + + if (memcmp(&priv->sfspec, &pandesc.sfspec, + sizeof(struct ieee802154_superframespec_s)) != 0) + { + /* Copy in the new superframe spec */ + + memcpy(&priv->sfspec, &pandesc.sfspec, + sizeof(struct ieee802154_superframespec_s)); + + /* Tell the radio layer about the superframe spec update */ + + priv->radio->sfupdate(priv->radio, &pandesc.sfspec); + } + } +} + /**************************************************************************** * Name: mac802154_symtoticks * @@ -1737,7 +1927,7 @@ MACHANDLE mac802154_create(FAR struct ieee802154_radio_s *radiodev) } IEEE802154_EADDRCOPY(mac->addr.eaddr, eaddr); - mac->radio->set_attr(mac->radio, IEEE802154_ATTR_MAC_EXTENDED_ADDR, + mac->radio->set_attr(mac->radio, IEEE802154_ATTR_MAC_EADDR, (union ieee802154_attr_u *)&eaddr[0]); return (MACHANDLE)mac; diff --git a/wireless/ieee802154/mac802154_assoc.c b/wireless/ieee802154/mac802154_assoc.c index cd33e382d7..f0123200ca 100644 --- a/wireless/ieee802154/mac802154_assoc.c +++ b/wireless/ieee802154/mac802154_assoc.c @@ -58,7 +58,7 @@ * Private Function Prototypes ****************************************************************************/ -static void mac802154_timeout_assoc(FAR struct ieee802154_privmac_s *priv); +static void mac802154_assoctimeout(FAR struct ieee802154_privmac_s *priv); /**************************************************************************** * Public MAC Functions @@ -120,22 +120,18 @@ int mac802154_req_associate(MACHANDLE mac, /* Set the channel and channel page of the PHY layer */ - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_CHANNEL, - (FAR const union ieee802154_attr_u *)&req->chnum); - - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_PAGE, - (FAR const union ieee802154_attr_u *)&req->chpage); + mac802154_setchannel(priv, req->chan); + mac802154_setchpage(priv, req->chpage); /* Set the coordinator address attributes */ - memcpy(&priv->coordaddr, &req->coordaddr, sizeof(struct ieee802154_addr_s)); + mac802154_setcoordaddr(priv, &req->coordaddr); + + /* TODO: Need to send coordinator address to radio layer */ /* Copy the coordinator PAN ID to our PAN ID */ - IEEE802154_PANIDCOPY(priv->addr.panid, req->coordaddr.panid); - - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_PANID, - (FAR const union ieee802154_attr_u *)req->coordaddr.panid); + mac802154_setpanid(priv, req->coordaddr.panid); /* Copy in the capabilities information bitfield */ @@ -181,7 +177,7 @@ int mac802154_req_associate(MACHANDLE mac, IEEE802154_SETACKREQ(iob->io_data, 0); IEEE802154_SETFTYPE(iob->io_data, 0, IEEE802154_FRAME_COMMAND); - IEEE802154_SETDADDRMODE(iob->io_data, 0, priv->coordaddr.mode); + IEEE802154_SETDADDRMODE(iob->io_data, 0, priv->pandesc.coordaddr.mode); IEEE802154_SETSADDRMODE(iob->io_data, 0, IEEE802154_ADDRMODE_EXTENDED); iob->io_len = 2; @@ -197,20 +193,20 @@ int mac802154_req_associate(MACHANDLE mac, * PAN to which to associate. [1] pg. 68 */ - mac802154_putpanid(iob, priv->coordaddr.panid); + mac802154_putpanid(iob, priv->pandesc.coordaddr.panid); /* The Destination Address field shall contain the address from the beacon * frame that was transmitted by the coordinator to which the association * request command is being sent. [1] pg. 68 */ - if (priv->coordaddr.mode == IEEE802154_ADDRMODE_SHORT) + if (priv->pandesc.coordaddr.mode == IEEE802154_ADDRMODE_SHORT) { - mac802154_putsaddr(iob, priv->coordaddr.saddr); + mac802154_putsaddr(iob, priv->pandesc.coordaddr.saddr); } - else if (priv->coordaddr.mode == IEEE802154_ADDRMODE_EXTENDED) + else if (priv->pandesc.coordaddr.mode == IEEE802154_ADDRMODE_EXTENDED) { - mac802154_puteaddr(iob, priv->coordaddr.eaddr); + mac802154_puteaddr(iob, priv->pandesc.coordaddr.eaddr); } /* The Source PAN Identifier field shall contain the broadcast PAN identifier.*/ @@ -258,7 +254,7 @@ int mac802154_req_associate(MACHANDLE mac, mac802154_givesem(&priv->exclsem); - /* Association Request commands get sent out immediately */ + /* Association Request command gets sent out immediately */ priv->radio->txdelayed(priv->radio, txdesc, 0); @@ -464,7 +460,7 @@ void mac802154_txdone_assocreq(FAR struct ieee802154_privmac_s *priv, * the coordinator after macResponseWaitTime. [1] pg. 34 */ - if (priv->trackingbeacon) + if (priv->sfspec.beaconorder < 15) { /* We are tracking the beacon, so we should see our address in the * beacon frame within macResponseWaitTime if the coordinator is going @@ -475,7 +471,7 @@ void mac802154_txdone_assocreq(FAR struct ieee802154_privmac_s *priv, mac802154_timerstart(priv, (priv->resp_waittime * IEEE802154_BASE_SUPERFRAME_DURATION), - mac802154_timeout_assoc); + mac802154_assoctimeout); } else { @@ -484,7 +480,7 @@ void mac802154_txdone_assocreq(FAR struct ieee802154_privmac_s *priv, * extended */ - DEBUGASSERT(priv->coordaddr.mode != IEEE802154_ADDRMODE_NONE); + DEBUGASSERT(priv->pandesc.coordaddr.mode != IEEE802154_ADDRMODE_NONE); /* Send the Data Request MAC command after macResponseWaitTime to * extract the data from the coordinator. @@ -492,7 +488,7 @@ void mac802154_txdone_assocreq(FAR struct ieee802154_privmac_s *priv, mac802154_txdesc_alloc(priv, &respdesc, false); - mac802154_createdatareq(priv, &priv->coordaddr, + mac802154_createdatareq(priv, &priv->pandesc.coordaddr, IEEE802154_ADDRMODE_EXTENDED, respdesc); priv->curr_cmd = IEEE802154_CMD_DATA_REQ; @@ -589,7 +585,7 @@ void mac802154_txdone_datareq_assoc(FAR struct ieee802154_privmac_s *priv, * the corresponding data frame from the coordinator. [1] pg.43 */ - priv->radio->rxenable(priv->radio, true); + mac802154_rxenable(priv); /* Start a timer, if we receive the data frame, we will cancel * the timer, otherwise it will expire and we will notify the @@ -597,7 +593,7 @@ void mac802154_txdone_datareq_assoc(FAR struct ieee802154_privmac_s *priv, */ mac802154_timerstart(priv, priv->max_frame_waittime, - mac802154_timeout_assoc); + mac802154_assoctimeout); /* We can deallocate the data conf notification as it is no longer * needed. We can't use the public function here since we already @@ -724,7 +720,7 @@ void mac802154_rx_assocresp(FAR struct ieee802154_privmac_s *priv, /* Inform the radio of the address change */ - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_SHORT_ADDRESS, + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_SADDR, (FAR union ieee802154_attr_u *)priv->addr.saddr); /* A Short Address field value equal to 0xfffe shall indicate that the device @@ -775,7 +771,7 @@ void mac802154_rx_assocresp(FAR struct ieee802154_privmac_s *priv, ****************************************************************************/ /**************************************************************************** - * Name: mac802154_timeout_assoc + * Name: mac802154_assoctimeout * * Description: * Function registered with MAC timer that gets called via the work queue to @@ -783,7 +779,7 @@ void mac802154_rx_assocresp(FAR struct ieee802154_privmac_s *priv, * ****************************************************************************/ -static void mac802154_timeout_assoc(FAR struct ieee802154_privmac_s *priv) +static void mac802154_assoctimeout(FAR struct ieee802154_privmac_s *priv) { FAR struct ieee802154_notif_s *notif; @@ -805,7 +801,7 @@ static void mac802154_timeout_assoc(FAR struct ieee802154_privmac_s *priv) /* We are no longer performing the association operation */ priv->curr_op = MAC802154_OP_NONE; - priv->cmd_desc = NULL; + priv->cmd_desc = NULL; mac802154_givesem(&priv->opsem); /* Release the MAC */ diff --git a/wireless/ieee802154/mac802154_getset.c b/wireless/ieee802154/mac802154_getset.c index bc666aad44..ba0f5f676a 100644 --- a/wireless/ieee802154/mac802154_getset.c +++ b/wireless/ieee802154/mac802154_getset.c @@ -86,10 +86,10 @@ int mac802154_req_get(MACHANDLE mac, enum ieee802154_attr_e attr, case IEEE802154_ATTR_MAC_PANID: IEEE802154_PANIDCOPY(attrval->mac.panid, priv->addr.panid); break; - case IEEE802154_ATTR_MAC_SHORT_ADDRESS: + case IEEE802154_ATTR_MAC_SADDR: IEEE802154_SADDRCOPY(attrval->mac.saddr, priv->addr.saddr); break; - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: IEEE802154_EADDRCOPY(attrval->mac.eaddr, priv->addr.eaddr); break; case IEEE802154_ATTR_MAC_DEVMODE: @@ -100,7 +100,7 @@ int mac802154_req_get(MACHANDLE mac, enum ieee802154_attr_e attr, * it along. */ - ret = priv->radio->set_attr(priv->radio, attr, attrval); + ret = priv->radio->get_attr(priv->radio, attr, attrval); break; } @@ -133,42 +133,37 @@ int mac802154_req_set(MACHANDLE mac, enum ieee802154_attr_e attr, { case IEEE802154_ATTR_MAC_PANID: { - IEEE802154_PANIDCOPY(priv->addr.panid, attrval->mac.panid); - - /* Tell the radio about the attribute */ - - priv->radio->set_attr(priv->radio, attr, attrval); - + mac802154_setpanid(priv, attrval->mac.panid); ret = IEEE802154_STATUS_SUCCESS; } break; - case IEEE802154_ATTR_MAC_SHORT_ADDRESS: + case IEEE802154_ATTR_MAC_SADDR: { - IEEE802154_SADDRCOPY(priv->addr.saddr, attrval->mac.saddr); - - /* Tell the radio about the attribute */ - - priv->radio->set_attr(priv->radio, attr, attrval); - + mac802154_setsaddr(priv, attrval->mac.saddr); ret = IEEE802154_STATUS_SUCCESS; } break; - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: { - /* Set the MAC copy of the address in the table */ - - IEEE802154_EADDRCOPY(priv->addr.eaddr, attrval->mac.eaddr); - - /* Tell the radio about the attribute */ - - priv->radio->set_attr(priv->radio, attr, attrval); - + mac802154_seteaddr(priv, attrval->mac.eaddr); + ret = IEEE802154_STATUS_SUCCESS; + } + break; + case IEEE802154_ATTR_MAC_COORD_SADDR: + { + mac802154_setcoordsaddr(priv, attrval->mac.coordsaddr); + ret = IEEE802154_STATUS_SUCCESS; + } + break; + case IEEE802154_ATTR_MAC_COORD_EADDR: + { + mac802154_setcoordeaddr(priv, attrval->mac.coordeaddr); ret = IEEE802154_STATUS_SUCCESS; } break; case IEEE802154_ATTR_MAC_ASSOCIATION_PERMIT: { - priv->sf_spec.assocpermit = attrval->mac.assocpermit; + priv->sfspec.assocpermit = attrval->mac.assocpermit; priv->beaconupdate = true; ret = IEEE802154_STATUS_SUCCESS; } diff --git a/wireless/ieee802154/mac802154_internal.h b/wireless/ieee802154/mac802154_internal.h index 90f4b71411..aa19f79f60 100644 --- a/wireless/ieee802154/mac802154_internal.h +++ b/wireless/ieee802154/mac802154_internal.h @@ -67,124 +67,6 @@ * Pre-processor Definitions ****************************************************************************/ -#define mac802154_putpanid(iob, panid) \ - do \ - { \ - IEEE802154_PANIDCOPY(&iob->io_data[iob->io_len], panid); \ - iob->io_len += IEEE802154_PANIDSIZE; \ - } \ - while(0) - -#define mac802154_putsaddr(iob, saddr) \ - do \ - { \ - IEEE802154_SADDRCOPY(&iob->io_data[iob->io_len], saddr); \ - iob->io_len += IEEE802154_SADDRSIZE; \ - } \ - while(0) - -#define mac802154_puteaddr(iob, eaddr) \ - do \ - { \ - IEEE802154_EADDRCOPY(&iob->io_data[iob->io_len], eaddr); \ - iob->io_len += IEEE802154_EADDRSIZE; \ - } \ - while(0) - -#define mac802154_takepanid(iob, panid) \ - do \ - { \ - IEEE802154_PANIDCOPY(panid, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_PANIDSIZE; \ - } \ - while(0) - -#define mac802154_takesaddr(iob, saddr) \ - do \ - { \ - IEEE802154_SADDRCOPY(saddr, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_SADDRSIZE; \ - } \ - while(0) - -#define mac802154_takeeaddr(iob, eaddr) \ - do \ - { \ - IEEE802154_EADDRCOPY(eaddr, &iob->io_data[iob->io_offset]); \ - iob->io_offset += IEEE802154_EADDRSIZE; \ - } \ - while(0) - -/* General helper macros ****************************************************/ - -/* GET 16-bit data: source in network order, result in host order */ - -#define GETHOST16(ptr,index) \ - ((((uint16_t)((ptr)[index])) << 8) | ((uint16_t)(((ptr)[(index) + 1])))) - -/* GET 16-bit data: source in network order, result in network order */ - -#define GETNET16(ptr,index) \ - ((((uint16_t)((ptr)[(index) + 1])) << 8) | ((uint16_t)(((ptr)[index])))) - -/* PUT 16-bit data: source in host order, result in network order */ - -#define PUTHOST16(ptr,index,value) \ - do \ - { \ - (ptr)[index] = (uint16_t)(value) & 0xff; \ - (ptr)[index + 1] = ((uint16_t)(value) >> 8) & 0xff; \ - } \ - while(0) - -/* Set bit in 16-bit value: source in host order, result in network order. */ - -#define IEEE802154_SETBITS_U16(ptr,index,value) \ - do \ - { \ - (ptr)[index] |= (uint16_t)(value) & 0xff; \ - (ptr)[index + 1] |= ((uint16_t)(value) >> 8) & 0xff; \ - } \ - while(0) - -#define IEEE802154_SETFTYPE(ptr, index, ftype) \ - IEEE802154_SETBITS_U16(ptr, index, (ftype << IEEE802154_FRAMECTRL_SHIFT_FTYPE)) - -#define IEEE802154_SETACKREQ(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_ACKREQ) - -#define IEEE802154_SETDADDRMODE(ptr, index, mode) \ - IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_DADDR)) - -#define IEEE802154_SETSADDRMODE(ptr, index, mode) \ - IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_SADDR)) - -#define IEEE802154_SETPANIDCOMP(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_PANIDCOMP) - -#define IEEE802154_SETVERSION(ptr, index, version) \ - IEEE802154_SETBITS_U16(ptr, index, (version << IEEE802154_FRAMECTRL_SHIFT_VERSION)) - -/* Helper macros for setting/receiving bits for superframe specification */ - -#define IEEE802154_SETBEACONORDER(ptr, index, val) \ - IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_BEACONORDER)) - -#define IEEE802154_SETSFORDER(ptr, index, val) \ - IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_SFORDER)) - -#define IEEE802154_SETFINCAPSLOT(ptr, index, val) \ - IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_FINCAPSLOT)) - -#define IEEE802154_SETBLE(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_BLE) - -#define IEEE802154_SETPANCOORD(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_PANCOORD) - -#define IEEE802154_SETASSOCPERMIT(ptr, index) \ - IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_ASSOCPERMIT) - /* Configuration ************************************************************/ /* If processing is not done at the interrupt level, then work queue support * is required. @@ -252,7 +134,8 @@ enum mac802154_operation_e { MAC802154_OP_NONE, MAC802154_OP_ASSOC, - MAC802154_OP_POLL + MAC802154_OP_POLL, + MAC802154_OP_SCAN }; struct ieee802154_privmac_s; /* Forward Reference */ @@ -265,6 +148,8 @@ typedef void (*mac802154_worker_t)(FAR struct ieee802154_privmac_s *priv); struct ieee802154_privmac_s { + /*************************** General Fields *********************************/ + FAR struct ieee802154_radio_s *radio; /* Contained IEEE802.15.4 radio dev */ FAR struct mac802154_maccb_s *cb; /* Head of a list of MAC callbacks */ FAR struct mac802154_radiocb_s radiocb; /* Interface to bind to radio */ @@ -278,9 +163,26 @@ struct ieee802154_privmac_s */ sem_t opsem; /* Exclusive operations */ + + /******************* Fields related to MAC operations ***********************/ + enum mac802154_operation_e curr_op; /* The current overall operation */ enum ieee802154_cmdid_e curr_cmd; /* Type of the current cmd */ FAR struct ieee802154_txdesc_s *cmd_desc; /* TX descriptor for current cmd */ + uint8_t nrxusers; + + /******************* Fields related to SCAN operation ***********************/ + + /* List of PAN descriptors to track during scan procedures */ + + uint8_t scanindex; + uint8_t npandesc; + struct ieee802154_pandesc_s pandescs[MAC802154_NPANDESC]; + uint8_t panidbeforescan[IEEE802154_PANIDSIZE]; + struct ieee802154_scan_req_s currscan; + uint32_t scansymdur; + + /******************* Fields related to notifications ************************/ /* Pre-allocated notifications to be passed to the registered callback. These * need to be freed by the application using mac802154_xxxxnotif_free when @@ -291,6 +193,8 @@ struct ieee802154_privmac_s struct mac802154_notif_s notif_pool[CONFIG_MAC802154_NNOTIF]; sem_t notif_sem; + /******************* Tx descriptor queues and pools *************************/ + struct ieee802154_txdesc_s txdesc_pool[CONFIG_MAC802154_NTXDESC]; sem_t txdesc_sem; sq_queue_t txdesc_queue; @@ -319,6 +223,35 @@ struct ieee802154_privmac_s sq_queue_t dataind_queue; + /************* Fields related to addressing and coordinator *****************/ + + /* Holds all address information (Extended, Short, and PAN ID) for the MAC. */ + + struct ieee802154_addr_s addr; + + struct ieee802154_pandesc_s pandesc; + + /*************** Fields related to beacon-enabled networks ******************/ + + uint8_t bsn; /* Seq. num added to tx beacon frame */ + + /* Holds attributes pertaining to the superframe specification */ + + struct ieee802154_superframespec_s sfspec; + + /* We use 2 beacon frame structures so that we can ping-pong between them + * while updating the beacon + */ + + struct ieee802154_beaconframe_s beaconframe[2]; + + /* Contents of beacon payload */ + + uint8_t beaconpayload[IEEE802154_MAX_BEACON_PAYLOAD_LEN]; + uint8_t beaconpayloadlength; + + /****************** Fields related to offloading work ***********************/ + /* Work structures for offloading aynchronous work */ struct work_s tx_work; @@ -330,25 +263,7 @@ struct ieee802154_privmac_s struct work_s purge_work; - /* MAC PIB attributes, grouped to save memory */ - - /* Holds all address information (Extended, Short, and PAN ID) for the MAC. */ - - struct ieee802154_addr_s addr; - - /* Holds all address information (Extended, Short) for Coordinator */ - - struct ieee802154_addr_s coordaddr; - - /* Holds attributes pertaining to the superframe specification */ - - struct ieee802154_superframespec_s sf_spec; - - /* We use 2 beacon frame structures so that we can ping-pong between them - * while updating the beacon - */ - - struct ieee802154_beaconframe_s beaconframe[2]; + /****************** Uncategorized MAC PIB attributes ***********************/ /* The maximum number of symbols to wait for an acknowledgement frame to * arrive following a transmitted data frame. [1] pg. 126 @@ -375,11 +290,6 @@ struct ieee802154_privmac_s uint16_t trans_persisttime; - /* Contents of beacon payload */ - - uint8_t beaconpayload[IEEE802154_MAX_BEACON_PAYLOAD_LEN]; - uint8_t beaconpayloadlength; /* Length of beacon payload */ - uint8_t bsn; /* Seq. num added to tx beacon frame */ uint8_t dsn; /* Seq. num added to tx data or MAC frame */ /* The maximum time, in multiples of aBaseSuperframeDuration, a device shall @@ -452,9 +362,204 @@ struct ieee802154_privmac_s }; /**************************************************************************** - * Inline Functions + * Function Prototypes ****************************************************************************/ +int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_txdesc_s **txdesc, + bool allow_interrupt); + +int mac802154_timerstart(FAR struct ieee802154_privmac_s *priv, + uint32_t numsymbols, mac802154_worker_t); + +void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_txdesc_s *txdesc); + +void mac802154_createdatareq(FAR struct ieee802154_privmac_s *priv, + FAR struct ieee802154_addr_s *coordaddr, + enum ieee802154_addrmode_e srcmode, + FAR struct ieee802154_txdesc_s *txdesc); + +void mac802154_updatebeacon(FAR struct ieee802154_privmac_s *priv); + + + +/**************************************************************************** + * Helper Macros/Inline Functions + ****************************************************************************/ + +#define mac802154_putpanid(iob, panid) \ + do \ + { \ + IEEE802154_PANIDCOPY(&iob->io_data[iob->io_len], panid); \ + iob->io_len += IEEE802154_PANIDSIZE; \ + } \ + while(0) + +#define mac802154_putsaddr(iob, saddr) \ + do \ + { \ + IEEE802154_SADDRCOPY(&iob->io_data[iob->io_len], saddr); \ + iob->io_len += IEEE802154_SADDRSIZE; \ + } \ + while(0) + +#define mac802154_puteaddr(iob, eaddr) \ + do \ + { \ + IEEE802154_EADDRCOPY(&iob->io_data[iob->io_len], eaddr); \ + iob->io_len += IEEE802154_EADDRSIZE; \ + } \ + while(0) + +#define mac802154_takepanid(iob, panid) \ + do \ + { \ + IEEE802154_PANIDCOPY(panid, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_PANIDSIZE; \ + } \ + while(0) + +#define mac802154_takesaddr(iob, saddr) \ + do \ + { \ + IEEE802154_SADDRCOPY(saddr, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_SADDRSIZE; \ + } \ + while(0) + +#define mac802154_takeeaddr(iob, eaddr) \ + do \ + { \ + IEEE802154_EADDRCOPY(eaddr, &iob->io_data[iob->io_offset]); \ + iob->io_offset += IEEE802154_EADDRSIZE; \ + } \ + while(0) + +/* General helper macros ****************************************************/ + +/* GET 16-bit data: source in network order, result in host order */ + +#define GETHOST16(ptr,index) \ + ((((uint16_t)((ptr)[(index) + 1])) << 8) | ((uint16_t)(((ptr)[index])))) + +/* GET 16-bit data: source in network order, result in network order */ + +#define GETNET16(ptr,index) \ + ((((uint16_t)((ptr)[index])) << 8) | ((uint16_t)(((ptr)[(index) + 1])))) + +/* PUT 16-bit data: source in host order, result in network order */ + +#define PUTHOST16(ptr,index,value) \ + do \ + { \ + (ptr)[index] = (uint16_t)(value) & 0xff; \ + (ptr)[index + 1] = ((uint16_t)(value) >> 8) & 0xff; \ + } \ + while(0) + +/* Set bit in 16-bit value: source in host order, result in network order. */ + +#define IEEE802154_SETBITS_U16(ptr,index,value) \ + do \ + { \ + (ptr)[index] |= (uint16_t)(value) & 0xff; \ + (ptr)[index + 1] |= ((uint16_t)(value) >> 8) & 0xff; \ + } \ + while(0) + +/* Helper macros for setting/receiving bits for frame control field */ + +#define IEEE802154_SETFTYPE(ptr, index, ftype) \ + IEEE802154_SETBITS_U16(ptr, index, (ftype << IEEE802154_FRAMECTRL_SHIFT_FTYPE)) + +#define IEEE802154_SETACKREQ(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_ACKREQ) + +#define IEEE802154_SETDADDRMODE(ptr, index, mode) \ + IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_DADDR)) + +#define IEEE802154_SETSADDRMODE(ptr, index, mode) \ + IEEE802154_SETBITS_U16(ptr, index, (mode << IEEE802154_FRAMECTRL_SHIFT_SADDR)) + +#define IEEE802154_SETPANIDCOMP(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_FRAMECTRL_PANIDCOMP) + +#define IEEE802154_SETVERSION(ptr, index, version) \ + IEEE802154_SETBITS_U16(ptr, index, (version << IEEE802154_FRAMECTRL_SHIFT_VERSION)) + +/* Helper macros for setting/receiving bits for superframe specification */ + +#define IEEE802154_SETBEACONORDER(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_BEACONORDER)) + +#define IEEE802154_SETSFORDER(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_SFORDER)) + +#define IEEE802154_SETFINCAPSLOT(ptr, index, val) \ + IEEE802154_SETBITS_U16(ptr, index, (val << IEEE802154_SFSPEC_SHIFT_FINCAPSLOT)) + +#define IEEE802154_SETBLE(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_BLE) + +#define IEEE802154_SETPANCOORD(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_PANCOORD) + +#define IEEE802154_SETASSOCPERMIT(ptr, index) \ + IEEE802154_SETBITS_U16(ptr, index, IEEE802154_SFSPEC_ASSOCPERMIT) + +#define IEEE802154_GETBEACONORDER(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_BEACONORDER) >> \ + IEEE802154_SFSPEC_SHIFT_BEACONORDER) + +#define IEEE802154_GETSFORDER(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_SFORDER) >> \ + IEEE802154_SFSPEC_SHIFT_SFORDER) + +#define IEEE802154_GETFINCAPSLOT(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_FINCAPSLOT) >> \ + IEEE802154_SFSPEC_SHIFT_FINCAPSLOT) + +#define IEEE802154_GETBLE(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_BLE) >> \ + IEEE802154_SFSPEC_SHIFT_BLE) + +#define IEEE802154_GETPANCOORD(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_PANCOORD) >> \ + IEEE802154_SFSPEC_SHIFT_PANCOORD) + +#define IEEE802154_GETASSOCPERMIT(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_SFSPEC_ASSOCPERMIT) >> \ + IEEE802154_SFSPEC_SHIFT_ASSOCPERMIT) + +/* Helper macros for setting/receiving bits for GTS specification */ + +#define IEEE802154_GETGTSDESCCOUNT(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_GTSSPEC_DESCCOUNT) >> \ + IEEE802154_GTSSPEC_SHIFT_DESCCOUNT) + +#define IEEE802154_GETGTSPERMIT(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_GTSSPEC_PERMIT) >> \ + IEEE802154_GTSSPEC_SHIFT_PERMIT) + +/* Helper macros for setting/receiving bits for GTS Directions */ + +#define IEEE802154_GETGTSDIRMASK(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_GTSDIR_MASK) >> \ + IEEE802154_GTSDIR_SHIFT_MASK) + +/* Helper macros for setting/receiving bits for Pending Address Specification */ + +#define IEEE802154_GETNPENDSADDR(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_PENDADDR_NSADDR) >> \ + IEEE802154_PENDADDR_SHIFT_NSADDR) + +#define IEEE802154_GETNPENDEADDR(ptr, index) \ + ((GETHOST16(ptr, index) & IEEE802154_PENDADDR_NEADDR) >> \ + IEEE802154_PENDADDR_SHIFT_NEADDR) + +/* General helper macros ****************************************************/ + #define mac802154_givesem(s) sem_post(s); static inline int mac802154_takesem(sem_t *sem, bool allowinterrupt) @@ -507,26 +612,95 @@ static inline int mac802154_timercancel(FAR struct ieee802154_privmac_s *priv) return OK; } -/**************************************************************************** - * Function Prototypes - ****************************************************************************/ +static inline void mac802154_rxenable(FAR struct ieee802154_privmac_s *priv) +{ + priv->nrxusers++; + /* If this is the first user, actually enable the receiver */ + + if (priv->nrxusers == 1) + { + wlinfo("receiver enabled\n"); + priv->radio->rxenable(priv->radio, true); + } +} -int mac802154_txdesc_alloc(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s **txdesc, - bool allow_interrupt); +static inline void mac802154_rxdisable(FAR struct ieee802154_privmac_s *priv) +{ + priv->nrxusers--; -int mac802154_timerstart(FAR struct ieee802154_privmac_s *priv, - uint32_t numsymbols, mac802154_worker_t); + /* If this is the first user, actually enable the receiver */ + + if (priv->nrxusers == 0) + { + wlinfo("receiver disabled\n"); + priv->radio->rxenable(priv->radio, true); + priv->radio->rxenable(priv->radio, false); + } +} -void mac802154_setupindirect(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_txdesc_s *txdesc); +static inline void mac802154_setchannel(FAR struct ieee802154_privmac_s *priv, + uint8_t channel) +{ + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CHAN, + (FAR const union ieee802154_attr_u *)&channel); +} -void mac802154_createdatareq(FAR struct ieee802154_privmac_s *priv, - FAR struct ieee802154_addr_s *coordaddr, - enum ieee802154_addrmode_e srcmode, - FAR struct ieee802154_txdesc_s *txdesc); +static inline void mac802154_setchpage(FAR struct ieee802154_privmac_s *priv, + uint8_t chpage) +{ + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_PAGE, + (FAR const union ieee802154_attr_u *)&chpage); +} -void mac802154_updatebeacon(FAR struct ieee802154_privmac_s *priv); +static inline void mac802154_setpanid(FAR struct ieee802154_privmac_s *priv, + const uint8_t *panid) +{ + IEEE802154_PANIDCOPY(priv->addr.panid, panid); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_PANID, + (FAR const union ieee802154_attr_u *)panid); +} + +static inline void mac802154_setsaddr(FAR struct ieee802154_privmac_s *priv, + const uint8_t *saddr) +{ + IEEE802154_SADDRCOPY(priv->addr.saddr, saddr); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_SADDR, + (FAR const union ieee802154_attr_u *)saddr); +} + +static inline void mac802154_seteaddr(FAR struct ieee802154_privmac_s *priv, + const uint8_t *eaddr) +{ + IEEE802154_EADDRCOPY(priv->addr.eaddr, eaddr); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_EADDR, + (FAR const union ieee802154_attr_u *)eaddr); +} + +static inline void mac802154_setcoordsaddr(FAR struct ieee802154_privmac_s *priv, + const uint8_t *saddr) +{ + IEEE802154_SADDRCOPY(priv->pandesc.coordaddr.saddr, saddr); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_COORD_SADDR, + (FAR const union ieee802154_attr_u *)saddr); +} + +static inline void mac802154_setcoordeaddr(FAR struct ieee802154_privmac_s *priv, + const uint8_t *eaddr) +{ + IEEE802154_EADDRCOPY(priv->pandesc.coordaddr.eaddr, eaddr); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_COORD_EADDR, + (FAR const union ieee802154_attr_u *)eaddr); +} + +static inline void mac802154_setcoordaddr(FAR struct ieee802154_privmac_s *priv, + FAR const struct ieee802154_addr_s *addr) +{ + memcpy(&priv->pandesc.coordaddr, addr, sizeof(struct ieee802154_addr_s)); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_COORD_EADDR, + (FAR const union ieee802154_attr_u *)addr->eaddr); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_COORD_SADDR, + (FAR const union ieee802154_attr_u *)addr->saddr); +} #endif /* __WIRELESS_IEEE802154__MAC802154_INTERNAL_H */ diff --git a/wireless/ieee802154/mac802154_loopback.c b/wireless/ieee802154/mac802154_loopback.c index 81b81e4b44..63ec1bd418 100644 --- a/wireless/ieee802154/mac802154_loopback.c +++ b/wireless/ieee802154/mac802154_loopback.c @@ -735,14 +735,14 @@ static int lo_ioctl(FAR struct net_driver_s *dev, int cmd, IEEE802154_PANIDCOPY(g_panid, setreq->attrval.mac.panid); break; - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: IEEE802154_EADDRCOPY(g_eaddr, setreq->attrval.mac.eaddr); #ifdef CONFIG_NET_6LOWPAN_EXTENDEDADDR lo_addr2ip(dev); #endif break; - case IEEE802154_ATTR_MAC_SHORT_ADDRESS: + case IEEE802154_ATTR_MAC_SADDR: IEEE802154_SADDRCOPY(g_saddr, setreq->attrval.mac.saddr); #ifndef CONFIG_NET_6LOWPAN_EXTENDEDADDR lo_addr2ip(dev); @@ -765,11 +765,11 @@ static int lo_ioctl(FAR struct net_driver_s *dev, int cmd, IEEE802154_PANIDCOPY(getreq->attrval.mac.panid, g_panid); break; - case IEEE802154_ATTR_MAC_EXTENDED_ADDR: + case IEEE802154_ATTR_MAC_EADDR: IEEE802154_EADDRCOPY(getreq->attrval.mac.eaddr, g_eaddr); break; - case IEEE802154_ATTR_MAC_SHORT_ADDRESS: + case IEEE802154_ATTR_MAC_SADDR: IEEE802154_SADDRCOPY(getreq->attrval.mac.saddr, g_saddr); break; diff --git a/wireless/ieee802154/mac802154_netdev.c b/wireless/ieee802154/mac802154_netdev.c index 179238bbb9..0f49102c5e 100644 --- a/wireless/ieee802154/mac802154_netdev.c +++ b/wireless/ieee802154/mac802154_netdev.c @@ -248,7 +248,7 @@ static int macnet_advertise(FAR struct net_driver_s *dev) /* Get the eaddr from the MAC */ memcpy(arg.ifr_name, dev->d_ifname, IFNAMSIZ); - arg.u.getreq.attr = IEEE802154_ATTR_MAC_EXTENDED_ADDR; + arg.u.getreq.attr = IEEE802154_ATTR_MAC_EADDR; ret = dev->d_ioctl(dev, MAC802154IOC_MLME_GET_REQUEST, (unsigned long)((uintptr_t)&arg)); if (ret < 0) @@ -283,7 +283,7 @@ static int macnet_advertise(FAR struct net_driver_s *dev) /* Get the saddr from the MAC */ memcpy(arg.ifr_name, dev->d_ifname, IFNAMSIZ); - arg.u.getreq.attr = IEEE802154_ATTR_MAC_SHORT_ADDRESS; + arg.u.getreq.attr = IEEE802154_ATTR_MAC_SADDR; ret = dev->d_ioctl(dev, MAC802154IOC_MLME_GET_REQUEST, (unsigned long)((uintptr_t)&arg)); if (ret < 0) diff --git a/wireless/ieee802154/mac802154_poll.c b/wireless/ieee802154/mac802154_poll.c index ecc74fbd9b..6e132ca155 100644 --- a/wireless/ieee802154/mac802154_poll.c +++ b/wireless/ieee802154/mac802154_poll.c @@ -60,7 +60,7 @@ * Private Function Prototypes ****************************************************************************/ -static void mac802154_timeout_poll(FAR struct ieee802154_privmac_s *priv); +static void mac802154_polltimeout(FAR struct ieee802154_privmac_s *priv); /**************************************************************************** * Public MAC Functions @@ -235,7 +235,7 @@ void mac802154_txdone_datareq_poll(FAR struct ieee802154_privmac_s *priv, * the corresponding data frame from the coordinator. [1] pg.43 */ - priv->radio->rxenable(priv->radio, true); + mac802154_rxenable(priv); /* Start a timer, if we receive the data frame, we will cancel * the timer, otherwise it will expire and we will notify the @@ -243,7 +243,7 @@ void mac802154_txdone_datareq_poll(FAR struct ieee802154_privmac_s *priv, */ mac802154_timerstart(priv, priv->max_frame_waittime, - mac802154_timeout_poll); + mac802154_polltimeout); /* We can deallocate the data conf notification as it is no longer * needed. We can't use the public function here since we already @@ -256,7 +256,7 @@ void mac802154_txdone_datareq_poll(FAR struct ieee802154_privmac_s *priv, } /**************************************************************************** - * Name: mac802154_timeout_poll + * Name: mac802154_polltimeout * * Description: * Function registered with MAC timer that gets called via the work queue to @@ -264,7 +264,7 @@ void mac802154_txdone_datareq_poll(FAR struct ieee802154_privmac_s *priv, * ****************************************************************************/ -void mac802154_timeout_poll(FAR struct ieee802154_privmac_s *priv) +void mac802154_polltimeout(FAR struct ieee802154_privmac_s *priv) { FAR struct ieee802154_notif_s *notif; diff --git a/wireless/ieee802154/mac802154_reset.c b/wireless/ieee802154/mac802154_reset.c index 8e72596363..759a44e31e 100644 --- a/wireless/ieee802154/mac802154_reset.c +++ b/wireless/ieee802154/mac802154_reset.c @@ -88,12 +88,12 @@ int mac802154_req_reset(MACHANDLE mac, bool rst_pibattr) { priv->isassoc = false; /* Not associated with a PAN */ priv->trackingbeacon = false; /* Not tracking beacon by default */ - priv->sf_spec.assocpermit = false; /* Device (if coord) not accepting ssociation */ + priv->sfspec.assocpermit = false; /* Device (if coord) not accepting ssociation */ priv->autoreq = true; /* Auto send data req if addr. in beacon */ - priv->sf_spec.ble = false; /* BLE disabled */ + priv->sfspec.ble = false; /* BLE disabled */ priv->beaconpayloadlength = 0; /* Beacon payload NULL */ - priv->sf_spec.beaconorder = 15; /* Non-beacon enabled network */ - priv->sf_spec.sforder = 15; /* Length of active portion of outgoing SF */ + priv->sfspec.beaconorder = 15; /* Non-beacon enabled network */ + priv->sfspec.sforder = 15; /* Length of active portion of outgoing SF */ priv->beacon_txtime = 0; /* Device never sent a beacon */ #warning Set BSN and DSN to random values! priv->bsn = 0; @@ -113,10 +113,10 @@ int mac802154_req_reset(MACHANDLE mac, bool rst_pibattr) /* Reset the Coordinator address */ - priv->coordaddr.mode = IEEE802154_ADDRMODE_NONE; - IEEE802154_PANIDCOPY(priv->coordaddr.panid, &IEEE802154_PANID_UNSPEC); - IEEE802154_SADDRCOPY(priv->coordaddr.saddr, &IEEE802154_SADDR_UNSPEC); - IEEE802154_EADDRCOPY(priv->coordaddr.eaddr, &IEEE802154_EADDR_UNSPEC); + priv->pandesc.coordaddr.mode = IEEE802154_ADDRMODE_NONE; + IEEE802154_PANIDCOPY(priv->pandesc.coordaddr.panid, &IEEE802154_PANID_UNSPEC); + IEEE802154_SADDRCOPY(priv->pandesc.coordaddr.saddr, &IEEE802154_SADDR_UNSPEC); + IEEE802154_EADDRCOPY(priv->pandesc.coordaddr.eaddr, &IEEE802154_EADDR_UNSPEC); /* Reset the device's address */ diff --git a/wireless/ieee802154/mac802154_scan.c b/wireless/ieee802154/mac802154_scan.c index d8121f1021..b24ae4b1ff 100644 --- a/wireless/ieee802154/mac802154_scan.c +++ b/wireless/ieee802154/mac802154_scan.c @@ -51,9 +51,17 @@ #include #include "mac802154.h" +#include "mac802154_internal.h" +#include "mac802154_scan.h" #include +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void mac802154_scantimeout(FAR struct ieee802154_privmac_s *priv); + /**************************************************************************** * Public MAC Functions ****************************************************************************/ @@ -79,7 +87,7 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) (FAR struct ieee802154_privmac_s *)mac; int ret; - if (req->duration > 15) + if (req->duration > 15 || req->numchan < 0 || req->numchan > 15) { ret = -EINVAL; goto errout; @@ -92,23 +100,60 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) ret = mac802154_takesem(&priv->opsem, true); if (ret < 0) { - ret = -EINVAL; + ret = -EINTR; goto errout; } + + priv->curr_op = MAC802154_OP_SCAN; + + /* Get exclusive access to the MAC */ ret = mac802154_takesem(&priv->exclsem, true); if (ret < 0) { - ret = -EINVAL; + mac802154_givesem(&priv->opsem); + ret = -EINTR; goto errout; } + + /* Copy the request so we have a reference */ + memcpy(&priv->currscan, req, sizeof(struct ieee802154_scan_req_s)); + priv->scanindex = 0; + priv->npandesc = 0; switch (req->type) { case IEEE802154_SCANTYPE_PASSIVE: { + /* Set the channel to the first channel in the list */ + mac802154_setchannel(priv, req->channels[priv->scanindex]); + mac802154_setchpage(priv, req->chpage); + + /* Before commencing an active or passive scan, the MAC sublayer shall + * store the value of macPANId and then set it to 0xffff for the + * duration of the scan. This enables the receive filter to accept all + * beacons rather than just the beacons from its current PAN, as + * described in 5.1.6.2. On completion of the scan, the MAC sublayer + * shall restore the value of macPANId to the value stored before the + * scan began. [1] pg. 24 + */ + + IEEE802154_PANIDCOPY(priv->panidbeforescan, priv->addr.panid); + mac802154_setpanid(priv, (const uint8_t *)&IEEE802154_PANID_UNSPEC); + + /* ...after switching to the channel for a passive scan, the device + * shall enable its receiver for at most + * [aBaseSuperframeDuration × (2 * n + 1)], + * where n is the value of the ScanDuration parameter. [1] pg. 25 + */ + + mac802154_rxenable(priv); + + priv->scansymdur = IEEE802154_BASE_SUPERFRAME_DURATION * + ((1 << req->duration) + 1); + mac802154_timerstart(priv, priv->scansymdur, mac802154_scantimeout); } break; case IEEE802154_SCANTYPE_ACTIVE: @@ -137,11 +182,103 @@ int mac802154_req_scan(MACHANDLE mac, FAR struct ieee802154_scan_req_s *req) break; } + mac802154_givesem(&priv->exclsem); return OK; -errout_with_sem; +errout_with_sem: + mac802154_givesem(&priv->exclsem); mac802154_givesem(&priv->opsem); errout: return ret; } +/**************************************************************************** + * Internal MAC Functions + ****************************************************************************/ + +void mac802154_scanfinish(FAR struct ieee802154_privmac_s *priv, + enum ieee802154_status_e status) +{ + FAR struct ieee802154_notif_s * notif; + + mac802154_takesem(&priv->exclsem, false); + mac802154_notif_alloc(priv, ¬if, false); + + priv->curr_op = MAC802154_OP_NONE; + mac802154_givesem(&priv->opsem); + + notif->notiftype = IEEE802154_NOTIFY_CONF_SCAN; + notif->u.scanconf.type = priv->currscan.type; + notif->u.scanconf.chpage = priv->currscan.chpage; + + /* Copy in the channels that did not get scanned */ + + if (priv->scanindex != priv->currscan.numchan) + { + notif->u.scanconf.numunscanned = priv->currscan.numchan - priv->scanindex; + memcpy(notif->u.scanconf.unscanned, &priv->currscan.channels[priv->scanindex], + notif->u.scanconf.numunscanned); + } + + notif->u.scanconf.numdesc = priv->npandesc; + memcpy(notif->u.scanconf.pandescs, priv->pandescs, + sizeof(struct ieee802154_pandesc_s) * priv->npandesc); + notif->u.scanconf.status = status; + + /* Reset the PAN ID to the setting before the scan started */ + + mac802154_setpanid(priv, priv->panidbeforescan); + + mac802154_givesem(&priv->exclsem); + + mac802154_notify(priv, notif); +} + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mac802154_scantimeout + * + * Description: + * Function registered with MAC timer that gets called via the work queue to + * handle a timeout for performing a scan operation. + * + ****************************************************************************/ + +static void mac802154_scantimeout(FAR struct ieee802154_privmac_s *priv) +{ + DEBUGASSERT(priv->curr_op == MAC802154_OP_SCAN); + + /* If we got here it means we are done scanning that channel */ + + mac802154_rxdisable(priv); + priv->scanindex++; + + /* Check to see if this was the last channel to scan */ + + if (priv->scanindex == priv->currscan.numchan) + { + if (priv->npandesc > 0) + { + mac802154_scanfinish(priv, IEEE802154_STATUS_SUCCESS); + } + else + { + mac802154_scanfinish(priv, IEEE802154_STATUS_NO_BEACON); + } + return; + } + + mac802154_setchannel(priv, priv->currscan.channels[priv->scanindex]); + + /* ...after switching to the channel for a passive scan, the device + * shall enable its receiver for at most + * [aBaseSuperframeDuration × (2 * n + 1)], + * where n is the value of the ScanDuration parameter. [1] pg. 25 + */ + + mac802154_rxenable(priv); + mac802154_timerstart(priv, priv->scansymdur, mac802154_scantimeout); +} diff --git a/wireless/ieee802154/mac802154_scan.h b/wireless/ieee802154/mac802154_scan.h new file mode 100644 index 0000000000..985d0953d5 --- /dev/null +++ b/wireless/ieee802154/mac802154_scan.h @@ -0,0 +1,66 @@ +/**************************************************************************** + * wireless/ieee802154/mac802154_scan.h + * + * Copyright (C) 2017 Verge Inc. All rights reserved. + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * + * Author: Anthony Merlino + * Author: Gregory Nutt + * + * The naming and comments for various fields are taken directly + * from the IEEE 802.15.4 2011 standard. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __WIRELESS_IEEE802154__MAC802154_SCAN_H +#define __WIRELESS_IEEE802154__MAC802154_SCAN_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include + +/**************************************************************************** + * Function Prototypes + ****************************************************************************/ + +struct ieee802154_privmac_s; /* Forward Reference */ + +void mac802154_scanfinish(FAR struct ieee802154_privmac_s *priv, + enum ieee802154_status_e status); + +#endif /* __WIRELESS_IEEE802154__MAC802154_SCAN_H */ \ No newline at end of file diff --git a/wireless/ieee802154/mac802154_start.c b/wireless/ieee802154/mac802154_start.c index bd734daa99..202dcb3a4d 100644 --- a/wireless/ieee802154/mac802154_start.c +++ b/wireless/ieee802154/mac802154_start.c @@ -97,14 +97,12 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) /* Set the PANID attribute */ - IEEE802154_PANIDCOPY(priv->addr.panid, req->panid); - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_MAC_PANID, - (FAR const union ieee802154_attr_u *)req->panid); + mac802154_setpanid(priv, req->panid); /* Tell the radio layer to set the channel number and channel page */ - priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_CHANNEL, - (FAR const union ieee802154_attr_u *)&req->chnum); + priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CHAN, + (FAR const union ieee802154_attr_u *)&req->chan); priv->radio->set_attr(priv->radio, IEEE802154_ATTR_PHY_CURRENT_PAGE, (FAR const union ieee802154_attr_u *)&req->chpage); @@ -130,11 +128,11 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) goto errout; } - priv->sf_spec.beaconorder = req->beaconorder; + priv->sfspec.beaconorder = req->beaconorder; /* The value of macSuperframeOrder shall be ignored if macBeaconOrder = 15. pg. 19 */ - if (priv->sf_spec.beaconorder < 15) + if (priv->sfspec.beaconorder < 15) { /* Set the superframe order */ @@ -144,7 +142,7 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) goto errout; } - priv->sf_spec.sforder = req->superframeorder; + priv->sfspec.sforder = req->superframeorder; } if (req->pancoord) @@ -156,9 +154,9 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) priv->devmode = IEEE802154_DEVMODE_COORD; } - priv->sf_spec.pancoord = req->pancoord; + priv->sfspec.pancoord = req->pancoord; - if (priv->sf_spec.beaconorder < 15) + if (priv->sfspec.beaconorder < 15) { /* If the BeaconOrder parameter is less than 15, the MLME sets macBattLifeExt to * the value of the BatteryLifeExtension parameter. If the BeaconOrder parameter @@ -166,11 +164,11 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) * [1] pg. 106 */ - priv->sf_spec.ble = req->battlifeext; + priv->sfspec.ble = req->battlifeext; /* For now we just set the CAP Slot to 15 */ - priv->sf_spec.final_capslot = 15; + priv->sfspec.final_capslot = 15; /* If the PAN coordinator parameter is set to TRUE, the MLME ignores the * StartTime parameter and begins beacon transmissions immediately. @@ -184,7 +182,7 @@ int mac802154_req_start(MACHANDLE mac, FAR struct ieee802154_start_req_s *req) /* Tell the radio to start transmitting beacon frames */ - priv->radio->beaconstart(priv->radio, &priv->sf_spec, + priv->radio->beaconstart(priv->radio, &priv->sfspec, &priv->beaconframe[priv->bf_ind]); } else