Skip to content

Commit

Permalink
fixup! ieee802154_submac: add initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jia200x committed Sep 8, 2020
1 parent caba86e commit 837f945
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
54 changes: 53 additions & 1 deletion sys/include/net/ieee802154/submac.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ extern "C" {
*/
typedef struct ieee802154_submac ieee802154_submac_t;

/**
* @brief SubMAC states
*/
typedef enum {
/**
* @brief SubMAC and network devices are off.
*
* The corresponding network device is put in a state with the
* lowest energy consumption.
*/
IEEE802154_STATE_OFF,

/**
* @brief SubMAC is ready to be used.
*/
IEEE802154_STATE_IDLE,

/**
* @brief SubMAC is ready to be used and listening to incoming frames.
*/
IEEE802154_STATE_LISTEN,
} ieee802154_submac_state_t;

/**
* @brief IEEE 802.15.4 SubMAC callbacks.
*/
Expand All @@ -53,8 +76,10 @@ typedef struct {
* This function is called from the SubMAC to indicate a IEEE 802.15.4
* frame is ready to be fetched from the device.
*
* @note ACK frames are automatically handled and discarded by the SubMAC.
* If @ref ieee802154_submac_t::state is @ref NETOPT_STATE_LISTEN, the
* SubMAC is ready to receive frames.
*
* @note ACK frames are automatically handled and discarded by the SubMAC.
* @param[in] submac pointer to the SubMAC descriptor
*/
void (*rx_done)(ieee802154_submac_t *submac);
Expand All @@ -64,6 +89,9 @@ typedef struct {
* This function is called from the SubMAC to indicate that the TX
* procedure finished.
*
* If @ref ieee802154_submac_t::state is @ref NETOPT_STATE_LISTEN, the
* SubMAC is ready to receive frames.
*
* @param[in] submac pointer to the SubMAC descriptor
* @param[out] info TX information associated to the transmission (status,
* number of retransmissions, pending bit, etc).
Expand Down Expand Up @@ -91,8 +119,32 @@ struct ieee802154_submac {
uint8_t backoff_mask; /**< internal value used for random backoff calculation */
uint8_t csma_retries; /**< maximum number of CSMA-CA retries */
int8_t tx_pow; /**< Transmission power (in dBm) */
ieee802154_submac_state_t state; /**< State of the SubMAC */
};

/**
* @brief Get the internal state of the SubMAC
*
* @param[in] submac pointer to the SubMAC descriptor
*
* @return the SubMAC state
*/
static inline ieee802154_submac_state_t ieee802154_get_state(ieee802154_submac_t *submac)
{
return submac->state;
}

/**
* @brief Set the internal state of the SubMAC
*
* @param[in] submac pointer to the SubMAC descriptor
* @param[in] state the desired state
*
* @return 0 on success
* @return negative errno on error.
*/
int ieee802154_set_state(ieee802154_submac_t *submac, ieee802154_submac_state_t state);

/**
* @brief Transmit an IEEE 802.15.4 PSDU
*
Expand Down
50 changes: 49 additions & 1 deletion sys/net/link_layer/ieee802154/submac.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ static void _tx_end(ieee802154_submac_t *submac, int status,
{
ieee802154_dev_t *dev = submac->dev;

ieee802154_radio_request_set_trx_state(dev, IEEE802154_TRX_STATE_RX_ON);
ieee802154_radio_request_set_trx_state(dev, submac->state == IEEE802154_STATE_LISTEN ? IEEE802154_TRX_STATE_RX_ON : IEEE802154_TRX_STATE_TRX_OFF);

submac->tx = false;
while (ieee802154_radio_confirm_set_trx_state(dev) == -EAGAIN) {}
submac->cb->tx_done(submac, status, info);
Expand Down Expand Up @@ -242,6 +243,10 @@ int ieee802154_send(ieee802154_submac_t *submac, const iolist_t *iolist)
uint8_t *buf = iolist->iol_base;
bool cnf = buf[0] & IEEE802154_FCF_ACK_REQ;

if (submac->state == IEEE802154_STATE_OFF) {
return -ENETDOWN;
}

if (submac->tx ||
ieee802154_radio_request_set_trx_state(dev,
IEEE802154_TRX_STATE_TX_ON) <
Expand All @@ -266,6 +271,7 @@ int ieee802154_submac_init(ieee802154_submac_t *submac)
ieee802154_dev_t *dev = submac->dev;

submac->tx = false;
submac->state = IEEE802154_STATE_LISTEN;

ieee802154_radio_request_on(dev);

Expand Down Expand Up @@ -321,6 +327,11 @@ int ieee802154_set_phy_conf(ieee802154_submac_t *submac, uint16_t channel_num,
ieee802154_dev_t *dev = submac->dev;
ieee802154_phy_conf_t conf =
{ .channel = channel_num, .page = channel_page, .pow = tx_pow };

if (submac->state == IEEE802154_STATE_OFF) {
return -ENETDOWN;
}

int res = ieee802154_radio_config_phy(dev, &conf);

if (res >= 0) {
Expand All @@ -331,6 +342,43 @@ int ieee802154_set_phy_conf(ieee802154_submac_t *submac, uint16_t channel_num,

return res;
}

int ieee802154_set_state(ieee802154_submac_t *submac, ieee802154_submac_state_t state)
{
int res;

ieee802154_dev_t *dev = submac->dev;

if (submac->tx) {
return -EBUSY;
}

if (state == submac->state) {
return -EALREADY;
}

/* Wake up the radio if it was off */
if (submac->state == IEEE802154_STATE_OFF) {
if ((res = ieee802154_radio_request_on(dev)) < 0) {
return res;
}
while (ieee802154_radio_confirm_on(dev) == -EAGAIN);
}

if (state == IEEE802154_STATE_OFF) {
res = ieee802154_radio_off(dev);
}
else {
if ((res = ieee802154_radio_request_set_trx_state(dev, state == IEEE802154_STATE_IDLE ? IEEE802154_TRX_STATE_TRX_OFF : IEEE802154_TRX_STATE_RX_ON)) < 0) {
return res;
}
while (ieee802154_radio_confirm_set_trx_state(dev) == -EAGAIN);
}

submac->state = state;
return res;
}

#else
int dont_be_pedantic;
#endif

0 comments on commit 837f945

Please sign in to comment.