submac.h
1 /*
2  * Copyright (C) 2020 HAW Hamburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
107 #ifndef NET_IEEE802154_SUBMAC_H
108 #define NET_IEEE802154_SUBMAC_H
109 
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113 
114 #include <stdio.h>
115 #include <string.h>
116 #include "assert.h"
117 
118 #include "net/ieee802154.h"
119 #include "net/ieee802154/radio.h"
120 
125 
129 typedef struct {
143  void (*rx_done)(ieee802154_submac_t *submac);
156  void (*tx_done)(ieee802154_submac_t *submac, int status,
157  ieee802154_tx_info_t *info);
159 
163 typedef enum {
172 
176 typedef enum {
187 
198  uint16_t ack_timeout_us;
199  uint16_t csma_backoff_us;
200  uint16_t panid;
201  uint16_t channel_num;
202  uint8_t channel_page;
203  uint8_t retrans;
204  uint8_t csma_retries_nb;
205  uint8_t backoff_mask;
206  uint8_t csma_retries;
207  int8_t tx_pow;
210  const iolist_t *psdu;
211 };
212 
229 
240  const network_uint16_t *short_addr)
241 {
242  int res = ieee802154_radio_config_addr_filter(&submac->dev,
244  short_addr);
245 
246  if (res >= 0) {
247  memcpy(&submac->short_addr, short_addr, IEEE802154_SHORT_ADDRESS_LEN);
248  }
249 
250  return res;
251 }
252 
263  const eui64_t *ext_addr)
264 {
265  int res = ieee802154_radio_config_addr_filter(&submac->dev,
267  ext_addr);
268 
269  if (res >= 0) {
270  memcpy(&submac->ext_addr, ext_addr, IEEE802154_LONG_ADDRESS_LEN);
271  }
272  return res;
273 }
274 
284 static inline int ieee802154_set_panid(ieee802154_submac_t *submac,
285  const uint16_t *panid)
286 {
287  int res = ieee802154_radio_config_addr_filter(&submac->dev,
289  panid);
290 
291  if (res >= 0) {
292  submac->panid = *panid;
293  }
294 
295  return res;
296 }
297 
306  ieee802154_submac_t *submac)
307 {
308  return submac->phy_mode;
309 }
310 
325 
342  uint16_t channel_num)
343 {
344  const ieee802154_phy_conf_t conf = {
346  .channel = channel_num,
347  .page = submac->channel_page,
348  .pow = submac->tx_pow,
349  };
350 
351  return ieee802154_set_phy_conf(submac, &conf);
352 }
353 
370  uint16_t channel_page)
371 {
372  const ieee802154_phy_conf_t conf = {
374  .channel = submac->channel_num,
375  .page = channel_page,
376  .pow = submac->tx_pow,
377  };
378 
379  return ieee802154_set_phy_conf(submac, &conf);
380 }
381 
398  int8_t tx_pow)
399 {
400  const ieee802154_phy_conf_t conf = {
402  .channel = submac->channel_num,
403  .page = submac->channel_page,
404  .pow = tx_pow,
405  };
406 
407  return ieee802154_set_phy_conf(submac, &conf);
408 }
409 
421 {
422  return ieee802154_radio_len(&submac->dev);
423 }
424 
441 static inline int ieee802154_read_frame(ieee802154_submac_t *submac, void *buf,
442  size_t len, ieee802154_rx_info_t *info)
443 {
444  return ieee802154_radio_read(&submac->dev, buf, len, info);
445 }
446 
460 
473 
483 {
484  return submac->fsm_state == IEEE802154_FSM_STATE_RX;
485 }
486 
496 {
497  return submac->fsm_state == IEEE802154_FSM_STATE_IDLE;
498 }
499 
513  const eui64_t *ext_addr);
514 
523 
532 
541 
554 
567 {
569 }
570 
577 {
579 }
580 
587 {
589 }
590 
597 {
599 }
600 
607 {
609 }
610 
611 #ifdef __cplusplus
612 }
613 #endif
614 
615 #endif /* NET_IEEE802154_SUBMAC_H */
POSIX.1-2008 compliant version of the assert macro.
static int ieee802154_radio_read(ieee802154_dev_t *dev, void *buf, size_t size, ieee802154_rx_info_t *info)
Shortcut to ieee802154_radio_ops::read.
Definition: radio.h:900
static int ieee802154_radio_len(ieee802154_dev_t *dev)
Shortcut to ieee802154_radio_ops::len.
Definition: radio.h:884
static int ieee802154_radio_config_addr_filter(ieee802154_dev_t *dev, ieee802154_af_cmd_t cmd, const void *value)
Shortcut to ieee802154_radio_ops::config_addr_filter.
Definition: radio.h:995
@ IEEE802154_AF_EXT_ADDR
Set extended IEEE 802.15.4 address (eui64_t)
Definition: radio.h:333
@ IEEE802154_AF_SHORT_ADDR
Set short IEEE 802.15.4 address (network_uint16_t)
Definition: radio.h:332
@ IEEE802154_AF_PANID
Set PAN ID (uint16_t)
Definition: radio.h:334
#define IEEE802154_SHORT_ADDRESS_LEN
IEEE 802.15.4 address lengths.
Definition: ieee802154.h:45
ieee802154_phy_mode_t
802.15.4 PHY modes
Definition: ieee802154.h:196
#define IEEE802154_LONG_ADDRESS_LEN
long address (EUI-64)
Definition: ieee802154.h:46
@ IEEE802154_PHY_NO_OP
don't change PHY configuration
Definition: ieee802154.h:205
ieee802154_fsm_state_t
Internal SubMAC FSM state machine states.
Definition: submac.h:163
static int ieee802154_get_frame_length(ieee802154_submac_t *submac)
Get the received frame length.
Definition: submac.h:420
void ieee802154_submac_ack_timer_cancel(ieee802154_submac_t *submac)
Cancel the ACK timeout timer.
static int ieee802154_set_channel_page(ieee802154_submac_t *submac, uint16_t channel_page)
Set IEEE 802.15.4 channel page.
Definition: submac.h:369
int ieee802154_send(ieee802154_submac_t *submac, const iolist_t *iolist)
Transmit an IEEE 802.15.4 PSDU.
ieee802154_fsm_state_t ieee802154_submac_process_ev(ieee802154_submac_t *submac, ieee802154_fsm_ev_t ev)
Process an FSM event.
static void ieee802154_submac_tx_done_cb(ieee802154_submac_t *submac)
Indicate the SubMAC that the device finished the transmission procedure.
Definition: submac.h:606
void ieee802154_submac_ack_timer_set(ieee802154_submac_t *submac)
Set the ACK timeout timer.
static int ieee802154_set_short_addr(ieee802154_submac_t *submac, const network_uint16_t *short_addr)
Set the IEEE 802.15.4 short address.
Definition: submac.h:239
static bool ieee802154_submac_state_is_rx(ieee802154_submac_t *submac)
Check whether the SubMAC is in RX state.
Definition: submac.h:482
int ieee802154_submac_init(ieee802154_submac_t *submac, const network_uint16_t *short_addr, const eui64_t *ext_addr)
Init the IEEE 802.15.4 SubMAC.
static int ieee802154_set_ext_addr(ieee802154_submac_t *submac, const eui64_t *ext_addr)
Set the IEEE 802.15.4 extended address.
Definition: submac.h:262
static int ieee802154_set_channel_number(ieee802154_submac_t *submac, uint16_t channel_num)
Set IEEE 802.15.4 channel number.
Definition: submac.h:341
int ieee802154_set_phy_conf(ieee802154_submac_t *submac, const ieee802154_phy_conf_t *conf)
Set IEEE 802.15.4 PHY configuration (channel, TX power)
ieee802154_fsm_ev_t
Internal SubMAC FSM state machine events.
Definition: submac.h:176
static void ieee802154_submac_bh_process(ieee802154_submac_t *submac)
Indicate the SubMAC that the BH should process an internal event.
Definition: submac.h:576
static bool ieee802154_submac_state_is_idle(ieee802154_submac_t *submac)
Check whether the SubMAC is in IDLE state.
Definition: submac.h:495
int ieee802154_set_rx(ieee802154_submac_t *submac)
Set the SubMAC to RX state.
void ieee802154_submac_bh_request(ieee802154_submac_t *submac)
ieee802154_submac_bh_process should be called as soon as possible.
int ieee802154_set_idle(ieee802154_submac_t *submac)
Set the SubMAC to IDLE state.
static ieee802154_phy_mode_t ieee802154_get_phy_mode(ieee802154_submac_t *submac)
Get IEEE 802.15.4 PHY mode.
Definition: submac.h:305
static int ieee802154_set_tx_power(ieee802154_submac_t *submac, int8_t tx_pow)
Set IEEE 802.15.4 transmission power.
Definition: submac.h:397
static int ieee802154_read_frame(ieee802154_submac_t *submac, void *buf, size_t len, ieee802154_rx_info_t *info)
Read the received frame.
Definition: submac.h:441
static void ieee802154_submac_ack_timeout_fired(ieee802154_submac_t *submac)
Indicate the SubMAC that the ACK timeout fired.
Definition: submac.h:566
static int ieee802154_set_panid(ieee802154_submac_t *submac, const uint16_t *panid)
Set the IEEE 802.15.4 PAN ID.
Definition: submac.h:284
static void ieee802154_submac_rx_done_cb(ieee802154_submac_t *submac)
Indicate the SubMAC that the device received a frame.
Definition: submac.h:586
static void ieee802154_submac_crc_error_cb(ieee802154_submac_t *submac)
Indicate the SubMAC that a frame with invalid CRC was received.
Definition: submac.h:596
@ IEEE802154_FSM_STATE_IDLE
The transceiver is off.
Definition: submac.h:166
@ IEEE802154_FSM_STATE_NUMOF
Number of SubMAC FSM states.
Definition: submac.h:170
@ IEEE802154_FSM_STATE_INVALID
Invalid state.
Definition: submac.h:164
@ IEEE802154_FSM_STATE_PREPARE
The SubMAC is preparing the next transmission.
Definition: submac.h:167
@ IEEE802154_FSM_STATE_TX
The SubMAC is currently transmitting a frame.
Definition: submac.h:168
@ IEEE802154_FSM_STATE_RX
SubMAC is ready to receive frames.
Definition: submac.h:165
@ IEEE802154_FSM_STATE_WAIT_FOR_ACK
The SubMAC is waiting for an ACK frame.
Definition: submac.h:169
@ IEEE802154_FSM_EV_BH
The Bottom Half should process an event.
Definition: submac.h:181
@ IEEE802154_FSM_EV_ACK_TIMEOUT
ACK timer fired.
Definition: submac.h:180
@ IEEE802154_FSM_EV_CRC_ERROR
Radio reports frame was received but CRC failed.
Definition: submac.h:179
@ IEEE802154_FSM_EV_REQUEST_TX
The upper layer requested to transmit a frame.
Definition: submac.h:182
@ IEEE802154_FSM_EV_TX_DONE
Radio reports frame was sent.
Definition: submac.h:177
@ IEEE802154_FSM_EV_REQUEST_SET_IDLE
The upper layer requested to go to IDLE.
Definition: submac.h:184
@ IEEE802154_FSM_EV_NUMOF
Number of SubMAC FSM events.
Definition: submac.h:185
@ IEEE802154_FSM_EV_RX_DONE
Radio reports frame was received.
Definition: submac.h:178
@ IEEE802154_FSM_EV_REQUEST_SET_RX_ON
The upper layer requested to go to RX.
Definition: submac.h:183
stdio wrapper to extend the C libs stdio
CSMA-CA exponential backoff parameters.
Definition: radio.h:371
the IEEE802.15.4 device descriptor
Definition: radio.h:415
Holder of the PHY configuration.
Definition: radio.h:455
ieee802154_phy_mode_t phy_mode
IEEE802.15.4 PHY mode.
Definition: radio.h:456
RX information associated to a frame.
Definition: radio.h:379
IEEE 802.15.4 SubMAC callbacks.
Definition: submac.h:129
IEEE 802.15.4 SubMAC descriptor.
Definition: submac.h:191
uint8_t backoff_mask
internal value used for random backoff calculation
Definition: submac.h:205
uint8_t csma_retries_nb
current number of CSMA-CA retries
Definition: submac.h:204
const ieee802154_submac_cb_t * cb
pointer to the SubMAC callbacks
Definition: submac.h:195
eui64_t ext_addr
IEEE 802.15.4 extended address.
Definition: submac.h:193
const iolist_t * psdu
stores the current PSDU
Definition: submac.h:210
uint16_t csma_backoff_us
CSMA sender backoff period in µs.
Definition: submac.h:199
int8_t tx_pow
Transmission power (in dBm)
Definition: submac.h:207
network_uint16_t short_addr
IEEE 802.15.4 short address.
Definition: submac.h:194
uint8_t retrans
current number of retransmissions
Definition: submac.h:203
bool wait_for_ack
SubMAC is waiting for an ACK frame.
Definition: submac.h:197
uint8_t channel_page
IEEE 802.15.4 channel page.
Definition: submac.h:202
uint16_t channel_num
IEEE 802.15.4 channel number.
Definition: submac.h:201
uint16_t ack_timeout_us
ACK timeout in µs.
Definition: submac.h:198
ieee802154_phy_mode_t phy_mode
IEEE 802.15.4 PHY mode.
Definition: submac.h:209
ieee802154_csma_be_t be
CSMA-CA backoff exponent params.
Definition: submac.h:196
ieee802154_fsm_state_t fsm_state
State of the SubMAC.
Definition: submac.h:208
ieee802154_dev_t dev
802.15.4 HAL descriptor
Definition: submac.h:192
uint8_t csma_retries
maximum number of CSMA-CA retries
Definition: submac.h:206
uint16_t panid
IEEE 802.15.4 PAN ID.
Definition: submac.h:200
TX information of the last transmitted frame.
Definition: radio.h:393
iolist structure definition
Definition: iolist.h:39
IEEE 802.15.4 header definitions.
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:74
Data type to represent an EUI-64.
Definition: eui64.h:55