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 
27 #ifndef NET_IEEE802154_SUBMAC_H
28 #define NET_IEEE802154_SUBMAC_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <string.h>
35 
36 #include "net/ieee802154.h"
37 #include "net/ieee802154/radio.h"
38 
39 #define IEEE802154_SUBMAC_MAX_RETRANSMISSIONS (4U)
44 typedef struct ieee802154_submac ieee802154_submac_t;
45 
49 typedef enum {
57 
62 
68 
72 typedef struct {
85  void (*rx_done)(ieee802154_submac_t *submac);
99  void (*tx_done)(ieee802154_submac_t *submac, int status,
100  ieee802154_tx_info_t *info);
102 
113  bool tx;
114  uint16_t panid;
115  uint16_t channel_num;
116  uint8_t channel_page;
117  uint8_t retrans;
118  uint8_t csma_retries_nb;
119  uint8_t backoff_mask;
120  uint8_t csma_retries;
121  int8_t tx_pow;
123 };
124 
133 {
134  return submac->state;
135 }
136 
147 
162 
173  const network_uint16_t *short_addr)
174 {
175  int res = ieee802154_radio_set_hw_addr_filter(submac->dev, short_addr, NULL,
176  NULL);
177 
178  if (res >= 0) {
179  memcpy(&submac->short_addr, short_addr, IEEE802154_SHORT_ADDRESS_LEN);
180  }
181 
182  return res;
183 }
184 
195  const eui64_t *ext_addr)
196 {
197  int res = ieee802154_radio_set_hw_addr_filter(submac->dev, NULL, ext_addr,
198  NULL);
199 
200  if (res >= 0) {
201  memcpy(&submac->ext_addr, ext_addr, IEEE802154_LONG_ADDRESS_LEN);
202  }
203  return res;
204 }
205 
215 static inline int ieee802154_set_panid(ieee802154_submac_t *submac,
216  const uint16_t *panid)
217 {
218  int res = ieee802154_radio_set_hw_addr_filter(submac->dev, NULL, NULL,
219  panid);
220 
221  if (res >= 0) {
222  submac->panid = *panid;
223  }
224 
225  return res;
226 }
227 
240 int ieee802154_set_phy_conf(ieee802154_submac_t *submac, uint16_t channel_num,
241  uint8_t channel_page, int8_t tx_pow);
242 
256  uint16_t channel_num)
257 {
258  return ieee802154_set_phy_conf(submac, channel_num, submac->channel_page,
259  submac->tx_pow);
260 }
261 
275  uint16_t channel_page)
276 {
277  return ieee802154_set_phy_conf(submac, submac->channel_num, channel_page,
278  submac->tx_pow);
279 }
280 
294  int8_t tx_pow)
295 {
296  return ieee802154_set_phy_conf(submac, submac->channel_num,
297  submac->channel_page, tx_pow);
298 }
299 
308 {
309  return ieee802154_radio_len(submac->dev);
310 }
311 
325 static inline int ieee802154_read_frame(ieee802154_submac_t *submac, void *buf,
326  size_t len, ieee802154_rx_info_t *info)
327 {
328  return ieee802154_radio_read(submac->dev, buf, len, info);
329 }
330 
341 int ieee802154_submac_init(ieee802154_submac_t *submac, const network_uint16_t *short_addr,
342  const eui64_t *ext_addr);
343 
353  uint16_t us);
354 
363 
374 
381 
388 
389 #ifdef __cplusplus
390 }
391 #endif
392 
393 #endif /* NET_IEEE802154_SUBMAC_H */
394 
ieee802154_submac::panid
uint16_t panid
IEEE 802.15.4 PAN ID.
Definition: submac.h:114
ieee802154.h
IEEE 802.15.4 header definitions.
ieee802154_submac::ext_addr
eui64_t ext_addr
IEEE 802.15.4 extended address.
Definition: submac.h:107
ieee802154_set_channel_page
static int ieee802154_set_channel_page(ieee802154_submac_t *submac, uint16_t channel_page)
Set IEEE 802.15.4 channel page.
Definition: submac.h:274
be_uint16_t
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:77
ieee802154_set_panid
static int ieee802154_set_panid(ieee802154_submac_t *submac, const uint16_t *panid)
Set the IEEE 802.15.4 PAN ID.
Definition: submac.h:215
ieee802154_submac::retrans
uint8_t retrans
current number of retransmissions
Definition: submac.h:117
ieee802154_radio_len
static int ieee802154_radio_len(ieee802154_dev_t *dev)
Shortcut to ieee802154_radio_ops::len.
Definition: radio.h:765
ieee802154_rx_info_t
RX information associated to a frame.
Definition: radio.h:249
ieee802154_submac_tx_done_cb
void ieee802154_submac_tx_done_cb(ieee802154_submac_t *submac)
Indicate the SubMAC that the device finished the transmission procedure.
ieee802154_set_state
int ieee802154_set_state(ieee802154_submac_t *submac, ieee802154_submac_state_t state)
Set the internal state of the SubMAC.
ieee802154_set_ext_addr
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:194
ieee802154_set_channel_number
static int ieee802154_set_channel_number(ieee802154_submac_t *submac, uint16_t channel_num)
Set IEEE 802.15.4 channel number.
Definition: submac.h:255
ieee802154_submac::channel_page
uint8_t channel_page
IEEE 802.15.4 channel page.
Definition: submac.h:116
ieee802154_submac_init
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.
ieee802154_radio_set_hw_addr_filter
static int ieee802154_radio_set_hw_addr_filter(ieee802154_dev_t *dev, const network_uint16_t *short_addr, const eui64_t *ext_addr, const uint16_t *pan_id)
Shortcut to ieee802154_radio_ops::set_hw_addr_filter.
Definition: radio.h:861
ieee802154_submac_state_t
ieee802154_submac_state_t
SubMAC states.
Definition: submac.h:49
ieee802154_submac::tx_pow
int8_t tx_pow
Transmission power (in dBm)
Definition: submac.h:121
ieee802154_get_frame_length
static int ieee802154_get_frame_length(ieee802154_submac_t *submac)
Get the received frame length.
Definition: submac.h:307
ieee802154_submac::dev
ieee802154_dev_t * dev
pointer to the 802.15.4 HAL descriptor
Definition: submac.h:109
ieee802154_submac::cb
const ieee802154_submac_cb_t * cb
pointer to the SubMAC callbacks
Definition: submac.h:110
ieee802154_csma_be_t
CSMA-CA exponential backoff parameters.
Definition: radio.h:241
ieee802154_submac_rx_done_cb
void ieee802154_submac_rx_done_cb(ieee802154_submac_t *submac)
Indicate the SubMAC that the device received a frame.
ieee802154_tx_info_t
TX information of the last transmitted frame.
Definition: radio.h:263
ieee802154_submac::short_addr
network_uint16_t short_addr
IEEE 802.15.4 short address.
Definition: submac.h:108
ieee802154_submac::csma_retries
uint8_t csma_retries
maximum number of CSMA-CA retries
Definition: submac.h:120
ieee802154_submac::csma_retries_nb
uint8_t csma_retries_nb
current number of CSMA-CA retries
Definition: submac.h:118
IEEE802154_SHORT_ADDRESS_LEN
#define IEEE802154_SHORT_ADDRESS_LEN
IEEE 802.15.4 address lengths.
Definition: ieee802154.h:43
iolist
iolist structure definition
Definition: iolist.h:39
ieee802154_dev
the IEEE802.15.4 device descriptor
Definition: radio.h:285
ieee802154_set_short_addr
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:172
IEEE802154_STATE_LISTEN
@ IEEE802154_STATE_LISTEN
SubMAC is ready to be used and listening to incoming frames.
Definition: submac.h:66
ieee802154_submac::tx
bool tx
SubMAC is currently transmitting a frame.
Definition: submac.h:113
IEEE802154_STATE_OFF
@ IEEE802154_STATE_OFF
SubMAC and network devices are off.
Definition: submac.h:56
ieee802154_radio_read
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:781
ieee802154_submac_ack_timer_cancel
void ieee802154_submac_ack_timer_cancel(ieee802154_submac_t *submac)
Cancel the ACK timeout timer.
ieee802154_read_frame
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:325
IEEE802154_LONG_ADDRESS_LEN
#define IEEE802154_LONG_ADDRESS_LEN
long address (EUI-64)
Definition: ieee802154.h:44
ieee802154_submac
IEEE 802.15.4 SubMAC descriptor.
Definition: submac.h:106
ieee802154_submac::wait_for_ack
bool wait_for_ack
SubMAC is waiting for an ACK frame.
Definition: submac.h:112
ieee802154_set_phy_conf
int ieee802154_set_phy_conf(ieee802154_submac_t *submac, uint16_t channel_num, uint8_t channel_page, int8_t tx_pow)
Set IEEE 802.15.4 PHY configuration (channel, TX power)
ieee802154_set_tx_power
static int ieee802154_set_tx_power(ieee802154_submac_t *submac, int8_t tx_pow)
Set IEEE 802.15.4 transmission power.
Definition: submac.h:293
eui64_t
Data type to represent an EUI-64.
Definition: eui64.h:55
ieee802154_send
int ieee802154_send(ieee802154_submac_t *submac, const iolist_t *iolist)
Transmit an IEEE 802.15.4 PSDU.
ieee802154_submac::be
ieee802154_csma_be_t be
CSMA-CA backoff exponent params.
Definition: submac.h:111
IEEE802154_STATE_IDLE
@ IEEE802154_STATE_IDLE
SubMAC is ready to be used.
Definition: submac.h:61
ieee802154_get_state
static ieee802154_submac_state_t ieee802154_get_state(ieee802154_submac_t *submac)
Get the internal state of the SubMAC.
Definition: submac.h:132
ieee802154_submac::channel_num
uint16_t channel_num
IEEE 802.15.4 channel number.
Definition: submac.h:115
ieee802154_submac::backoff_mask
uint8_t backoff_mask
internal value used for random backoff calculation
Definition: submac.h:119
ieee802154_submac_cb_t
IEEE 802.15.4 SubMAC callbacks.
Definition: submac.h:72
ieee802154_submac_ack_timeout_fired
void ieee802154_submac_ack_timeout_fired(ieee802154_submac_t *submac)
Indicate the SubMAC that the ACK timeout fired.
ieee802154_submac_ack_timer_set
void ieee802154_submac_ack_timer_set(ieee802154_submac_t *submac, uint16_t us)
Set the ACK timeout timer.
ieee802154_submac::state
ieee802154_submac_state_t state
State of the SubMAC.
Definition: submac.h:122