1 /*
2  * Copyright (c) 2016 Intel Corporation.
3  * Copyright (c) 2023 F. Grandel, Zephyr Project
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief Public IEEE 802.15.4 Driver API
11  *
12  * @note All references to the standard in this file cite IEEE 802.15.4-2020.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_NET_IEEE802154_RADIO_H_
16 #define ZEPHYR_INCLUDE_NET_IEEE802154_RADIO_H_
17 
18 #include <zephyr/device.h>
19 #include <zephyr/net/net_if.h>
20 #include <zephyr/net/net_pkt.h>
21 #include <zephyr/net/net_time.h>
22 #include <zephyr/net/ieee802154.h>
23 #include <zephyr/net/ieee802154_ie.h>
24 #include <zephyr/sys/util.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @defgroup ieee802154_driver IEEE 802.15.4 Drivers
32  * @ingroup ieee802154
33  *
34  * @brief IEEE 802.15.4 driver API
35  *
36  * @details This API provides a common representation of vendor-specific
37  * hardware and firmware to the native IEEE 802.15.4 L2 and OpenThread stacks.
38  * **Application developers should never interface directly with this API.** It
39  * is of interest to driver maintainers only.
40  *
41  * The IEEE 802.15.4 driver API consists of two separate parts:
42  *    - a basic, mostly PHY-level driver API to be implemented by all drivers,
43  *    - several optional MAC-level extension points to offload performance
44  *      critical or timing sensitive aspects at MAC level to the driver hardware
45  *      or firmware ("hard" MAC).
46  *
47  * Implementing the basic driver API will ensure integration with the native L2
48  * stack as well as basic support for OpenThread. Depending on the hardware,
49  * offloading to vendor-specific hardware or firmware features may be required
50  * to achieve full compliance with the Thread protocol or IEEE 802.15.4
51  * subprotocols (e.g. fast enough ACK packages, precise timing of timed TX/RX in
52  * the TSCH or CSL subprotocols).
53  *
54  * Whether or not MAC-level offloading extension points need to be implemented
55  * is to be decided by individual driver maintainers. Upper layers SHOULD
56  * provide a "soft" MAC fallback whenever possible.
57  *
58  * @note All section, table and figure references are to the IEEE 802.15.4-2020
59  * standard.
60  *
61  * @{
62  */
63 
64 /**
65  * @name IEEE 802.15.4-2020, Section 6: MAC functional description
66  * @{
67  */
68 
69 /**
70  * The symbol period (and therefore symbol rate) is defined in section 6.1: "Some
71  * of the timing parameters in definition of the MAC are in units of PHY symbols.
72  * For PHYs that have multiple symbol periods, the duration to be used for the
73  * MAC parameters is defined in that PHY clause."
74  *
75  * This is not necessarily the true physical symbol period, so take care to use
76  * this macro only when either the symbol period used for MAC timing is the same
77  * as the physical symbol period or if you actually mean the MAC timing symbol
78  * period.
79  *
80  * PHY specific symbol periods are defined in PHY specific sections below.
81  */
82 #define IEEE802154_PHY_SYMBOLS_PER_SECOND(symbol_period_ns) (NSEC_PER_SEC / symbol_period_ns)
83 
84 /** @} */
85 
86 
87 /**
88  * @name IEEE 802.15.4-2020, Section 8: MAC services
89  * @{
90  */
91 
92 /**
93  * The number of PHY symbols forming a superframe slot when the superframe order
94  * is equal to zero, see sections 8.4.2, table 8-93, aBaseSlotDuration and
95  * section 6.2.1.
96  */
97 #define IEEE802154_MAC_A_BASE_SLOT_DURATION 60U
98 
99 /**
100  * The number of slots contained in any superframe, see section 8.4.2,
101  * table 8-93, aNumSuperframeSlots.
102  */
103 #define IEEE802154_MAC_A_NUM_SUPERFRAME_SLOTS 16U
104 
105 /**
106  * The number of PHY symbols forming a superframe when the superframe order is
107  * equal to zero, see section 8.4.2, table 8-93, aBaseSuperframeDuration.
108  */
109 #define IEEE802154_MAC_A_BASE_SUPERFRAME_DURATION                                                  \
110 	(IEEE802154_MAC_A_BASE_SLOT_DURATION * IEEE802154_MAC_A_NUM_SUPERFRAME_SLOTS)
111 
112 /**
113  * MAC PIB attribute aUnitBackoffPeriod, see section 8.4.2, table 8-93, in symbol
114  * periods, valid for all PHYs except SUN PHY in the 920 MHz band.
115  */
116 #define IEEE802154_MAC_A_UNIT_BACKOFF_PERIOD(turnaround_time)                                      \
117 	(turnaround_time + IEEE802154_PHY_A_CCA_TIME)
118 
119 /**
120  * Default macResponseWaitTime in multiples of aBaseSuperframeDuration as
121  * defined in section 8.4.3.1, table 8-94.
122  */
123 #define IEEE802154_MAC_RESPONSE_WAIT_TIME_DEFAULT 32U
124 
125 /** @} */
126 
127 
128 /**
129  * @name IEEE 802.15.4-2020, Section 10: General PHY requirements
130  * @{
131  */
132 
133 /**
134  * @brief PHY channel pages, see section 10.1.3
135  *
136  * @details A device driver must support the mandatory channel pages, frequency
137  * bands and channels of at least one IEEE 802.15.4 PHY.
138  *
139  * Channel page and number assignments have developed over several versions of
140  * the standard and are not particularly well documented. Therefore some notes
141  * about peculiarities of channel pages and channel numbering:
142  * - The 2006 version of the standard had a read-only phyChannelsSupported PHY
143  *   PIB attribute that represented channel page/number combinations as a
144  *   bitmap. This attribute was removed in later versions of the standard as the
145  *   number of channels increased beyond what could be represented by a bit map.
146  *   That's the reason why it was decided to represent supported channels as a
147  *   combination of channel pages and ranges instead.
148  * - In the 2020 version of the standard, 13 channel pages are explicitly
149  *   defined, but up to 32 pages could in principle be supported. This was a
150  *   hard requirement in the 2006 standard. In later standards it is implicit
151  *   from field specifications, e.g. the MAC PIB attribute macChannelPage
152  *   (section 8.4.3.4, table 8-100) or channel page fields used in the SRM
153  *   protocol (see section 8.2.26.5).
154  * - ASK PHY (channel page one) was deprecated in the 2015 version of the
155  *   standard. The 2020 version of the standard is a bit ambivalent whether
156  *   channel page one disappeared as well or should be interpreted as O-QPSK now
157  *   (see section 10.1.3.3). In Zephyr this ambivalence is resolved by
158  *   deprecating channel page one.
159  * - For some PHYs the standard doesn't clearly specify a channel page, namely
160  *   the GFSK, RS-GFSK, CMB and TASK PHYs. These are all rather new and left out
161  *   in our list as long as no driver wants to implement them.
162  *
163  * @warning The bit numbers are not arbitrary but represent the channel
164  * page numbers as defined by the standard. Therefore do not change the
165  * bit numbering.
166  */
167 enum ieee802154_phy_channel_page {
168 	/**
169 	 * Channel page zero supports the 2.4G channels of the O-QPSK PHY and
170 	 * all channels from the BPSK PHYs initially defined in the 2003
171 	 * editions of the standard. For channel page zero, 16 channels are
172 	 * available in the 2450 MHz band (channels 11-26, O-QPSK), 10 in the
173 	 * 915 MHz band (channels 1-10, BPSK), and 1 in the 868 MHz band
174 	 * (channel 0, BPSK).
175 	 *
176 	 * You can retrieve the channels supported by a specific driver on this
177 	 * page via @ref IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES attribute.
178 	 *
179 	 * see section 10.1.3.3
180 	 */
181 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_ZERO_OQPSK_2450_BPSK_868_915 = BIT(0),
182 
183 	/** Formerly ASK PHY - deprecated in IEEE 802.15.4-2015 */
184 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_ONE_DEPRECATED = BIT(1),
185 
186 	/** O-QPSK PHY - 868 MHz and 915 MHz bands, see section 10.1.3.3 */
187 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWO_OQPSK_868_915 = BIT(2),
188 
189 	/** CSS PHY - 2450 MHz band, see section 10.1.3.4 */
190 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_THREE_CSS = BIT(3),
191 
192 	/** UWB PHY - SubG, low and high bands, see section 10.1.3.5 */
193 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_FOUR_HRP_UWB = BIT(4),
194 
195 	/** O-QPSK PHY - 780 MHz band, see section 10.1.3.2 */
196 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_FIVE_OQPSK_780 = BIT(5),
197 
198 	/** reserved - not currently assigned */
199 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_SIX_RESERVED = BIT(6),
200 
201 	/** MSK PHY - 780 MHz and 2450 MHz bands, see sections 10.1.3.6, 10.1.3.7 */
202 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_SEVEN_MSK = BIT(7),
203 
204 	/** LRP UWB PHY, see sections 10.1.3.8 */
205 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_EIGHT_LRP_UWB = BIT(8),
206 
207 	/**
208 	 * SUN FSK/OFDM/O-QPSK PHYs - predefined bands, operating modes and
209 	 * channels, see sections 10.1.3.9
210 	 */
211 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_NINE_SUN_PREDEFINED = BIT(9),
212 
213 	/**
214 	 * SUN FSK/OFDM/O-QPSK PHYs - generic modulation and channel
215 	 * description, see sections 10.1.3.9, 7.4.4.11
216 	 */
217 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_TEN_SUN_FSK_GENERIC = BIT(10),
218 
219 	/** O-QPSK PHY - 2380 MHz band, see section 10.1.3.10 */
220 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_ELEVEN_OQPSK_2380 = BIT(11),
221 
222 	/** LECIM DSSS/FSK PHYs, see section 10.1.3.11 */
223 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_TWELVE_LECIM = BIT(12),
224 
225 	/** RCC PHY, see section 10.1.3.12 */
226 	IEEE802154_ATTR_PHY_CHANNEL_PAGE_THIRTEEN_RCC = BIT(13),
227 };
228 
229 /**
230  * Represents a supported channel range, see @ref
231  * ieee802154_phy_supported_channels.
232  */
233 struct ieee802154_phy_channel_range {
234 	uint16_t from_channel;
235 	uint16_t to_channel;
236 };
237 
238 /**
239  * Represents a list channels supported by a driver for a given interface, see
240  * @ref IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES.
241  */
242 struct ieee802154_phy_supported_channels {
243 	/**
244 	 * @brief Pointer to an array of channel range structures.
245 	 *
246 	 * @warning The pointer must be valid and constant throughout the life
247 	 * of the interface.
248 	 */
249 	const struct ieee802154_phy_channel_range *const ranges;
250 
251 	/** @brief The number of currently available channel ranges. */
252 	const uint8_t num_ranges;
253 };
254 
255 /**
256  * @brief Allocate memory for the supported channels driver attribute with a
257  * single channel range constant across all driver instances. This is what most
258  * IEEE 802.15.4 drivers need.
259  *
260  * @details Example usage:
261  *
262  * @code{.c}
263  *   IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, 11, 26);
264  * @endcode
265  *
266  * The attribute may then be referenced like this:
267  *
268  * @code{.c}
269  *   ... &drv_attr.phy_supported_channels ...
270  * @endcode
271  *
272  * See @ref ieee802154_attr_get_channel_page_and_range() for a further shortcut
273  * that can be combined with this macro.
274  *
275  * @param drv_attr name of the local static variable to be declared for the
276  * local attributes structure
277  * @param from the first channel to be supported
278  * @param to the last channel to be supported
279  */
280 #define IEEE802154_DEFINE_PHY_SUPPORTED_CHANNELS(drv_attr, from, to)                               \
281 	static const struct {                                                                      \
282 		const struct ieee802154_phy_channel_range phy_channel_range;                       \
283 		const struct ieee802154_phy_supported_channels phy_supported_channels;             \
284 	} drv_attr = {                                                                             \
285 		.phy_channel_range = {.from_channel = (from), .to_channel = (to)},                 \
286 		.phy_supported_channels =                                                          \
287 			{                                                                          \
288 				.ranges = &drv_attr.phy_channel_range,                             \
289 				.num_ranges = 1U,                                                  \
290 			},                                                                         \
291 	}
292 
293 /** @} */
294 
295 
296 /**
297  * @name IEEE 802.15.4-2020, Section 11: PHY services
298  * @{
299  */
300 
301 /**
302  * Default PHY PIB attribute aTurnaroundTime, in PHY symbols, see section 11.3,
303  * table 11-1.
304  */
305 #define IEEE802154_PHY_A_TURNAROUND_TIME_DEFAULT 12U
306 
307 /**
308  * PHY PIB attribute aTurnaroundTime for SUN, RS-GFSK, TVWS, and LECIM FSK PHY,
309  * in PHY symbols, see section 11.3, table 11-1.
310  */
311 #define IEEE802154_PHY_A_TURNAROUND_TIME_1MS(symbol_period_ns)                                     \
312 	DIV_ROUND_UP(NSEC_PER_MSEC, symbol_period_ns)
313 
314 /**
315  * PHY PIB attribute aCcaTime, in PHY symbols, all PHYs except for SUN O-QPSK,
316  * see section 11.3, table 11-1.
317  */
318 #define IEEE802154_PHY_A_CCA_TIME 8U
319 
320 /** @} */
321 
322 
323 
324 /**
325  * @name IEEE 802.15.4-2020, Section 12: O-QPSK PHY
326  * @{
327  */
328 
329 /** O-QPSK 868Mhz band symbol period, see section 12.3.3 */
330 #define IEEE802154_PHY_OQPSK_868MHZ_SYMBOL_PERIOD_NS         40000LL
331 
332 /**
333  * O-QPSK 780MHz, 915MHz, 2380MHz and 2450MHz bands symbol period,
334  * see section 12.3.3
335  */
336 #define IEEE802154_PHY_OQPSK_780_TO_2450MHZ_SYMBOL_PERIOD_NS 16000LL
337 
338 /** @} */
339 
340 
341 /**
342  * @name IEEE 802.15.4-2020, Section 13: BPSK PHY
343  * @{
344  */
345 
346 /** BPSK 868MHz band symbol period, see section 13.3.3 */
347 #define IEEE802154_PHY_BPSK_868MHZ_SYMBOL_PERIOD_NS 50000LL
348 
349 /** BPSK 915MHz band symbol period, see section 13.3.3 */
350 #define IEEE802154_PHY_BPSK_915MHZ_SYMBOL_PERIOD_NS 25000LL
351 
352 /** @} */
353 
354 
355 /**
356  * @name IEEE 802.15.4-2020, Section 15: HRP UWB PHY
357  *
358  * @details For HRP UWB the symbol period is derived from the preamble symbol period
359  * (T_psym), see section 11.3, table 11-1 and section 15.2.5, table 15-4
360  * (confirmed in IEEE 802.15.4z, section 15.1). Choosing among those periods
361  * cannot be done based on channel page and channel alone. The mean pulse
362  * repetition frequency must also be known, see the 'UwbPrf' parameter of the
363  * MCPS-DATA.request primitive (section 8.3.2, table 8-88) and the preamble
364  * parameters for HRP-ERDEV length 91 codes (IEEE 802.15.4z, section 15.2.6.2,
365  * table 15-7b).
366  * @{
367  */
368 
369 /** Nominal PRF 4MHz symbol period */
370 #define IEEE802154_PHY_HRP_UWB_PRF4_TPSYM_SYMBOL_PERIOD_NS  3974.36F
371 /** Nominal PRF 16MHz symbol period */
372 #define IEEE802154_PHY_HRP_UWB_PRF16_TPSYM_SYMBOL_PERIOD_NS 993.59F
373 /** Nominal PRF 64MHz symbol period */
374 #define IEEE802154_PHY_HRP_UWB_PRF64_TPSYM_SYMBOL_PERIOD_NS 1017.63F
375 /** ERDEV symbol period */
376 #define IEEE802154_PHY_HRP_UWB_ERDEV_TPSYM_SYMBOL_PERIOD_NS 729.17F
377 
378 /** @brief represents the nominal pulse rate frequency of an HRP UWB PHY */
379 enum ieee802154_phy_hrp_uwb_nominal_prf {
380 	/** standard modes, see section 8.3.2, table 8-88. */
381 	IEEE802154_PHY_HRP_UWB_PRF_OFF = 0,
382 	IEEE802154_PHY_HRP_UWB_NOMINAL_4_M = BIT(0),
383 	IEEE802154_PHY_HRP_UWB_NOMINAL_16_M = BIT(1),
384 	IEEE802154_PHY_HRP_UWB_NOMINAL_64_M = BIT(2),
385 
386 	/**
387 	 * enhanced ranging device (ERDEV) modes not specified in table 8-88,
388 	 * see IEEE 802.15.4z, section 15.1, section 15.2.6.2, table 15-7b,
389 	 * section 15.3.4.2 and section 15.3.4.3.
390 	 */
391 	IEEE802154_PHY_HRP_UWB_NOMINAL_64_M_BPRF = BIT(3),
392 	IEEE802154_PHY_HRP_UWB_NOMINAL_128_M_HPRF = BIT(4),
393 	IEEE802154_PHY_HRP_UWB_NOMINAL_256_M_HPRF = BIT(5),
394 };
395 
396 /** RDEV device mask */
397 #define IEEE802154_PHY_HRP_UWB_RDEV                                                                \
398 	(IEEE802154_PHY_HRP_UWB_NOMINAL_4_M | IEEE802154_PHY_HRP_UWB_NOMINAL_16_M |                \
399 	 IEEE802154_PHY_HRP_UWB_NOMINAL_64_M)
400 
401 /** ERDEV device mask */
402 #define IEEE802154_PHY_HRP_UWB_ERDEV                                                               \
403 	(IEEE802154_PHY_HRP_UWB_NOMINAL_64_M_BPRF | IEEE802154_PHY_HRP_UWB_NOMINAL_128_M_HPRF |    \
404 	 IEEE802154_PHY_HRP_UWB_NOMINAL_256_M_HPRF)
405 
406 /** @} */
407 
408 
409 /**
410  * @name IEEE 802.15.4-2020, Section 19: SUN FSK PHY
411  * @{
412  */
413 
414 /** SUN FSK 863Mhz and 915MHz band symbol periods, see section 19.1, table 19-1 */
415 #define IEEE802154_PHY_SUN_FSK_863MHZ_915MHZ_SYMBOL_PERIOD_NS 20000LL
416 
417 /** SUN FSK PHY header length, in bytes, see section 19.2.4 */
418 #define IEEE802154_PHY_SUN_FSK_PHR_LEN 2
419 
420 /** @} */
421 
422 /**
423  * @name IEEE 802.15.4 Driver API
424  * @{
425  */
426 
427 /**
428  * IEEE 802.15.4 driver capabilities
429  *
430  * Any driver properties that can be represented in binary form should be
431  * modeled as capabilities. These are called "hardware" capabilities for
432  * historical reasons but may also represent driver firmware capabilities (e.g.
433  * MAC offloading features).
434  */
435 enum ieee802154_hw_caps {
436 
437 	/*
438 	 * PHY capabilities
439 	 *
440 	 * The following capabilities describe features of the underlying radio
441 	 * hardware (PHY/L1).
442 	 */
443 
444 	/** Energy detection (ED) supported (optional) */
445 	IEEE802154_HW_ENERGY_SCAN = BIT(0),
446 
447 	/*
448 	 * MAC offloading capabilities (optional)
449 	 *
450 	 * The following MAC/L2 features may optionally be offloaded to
451 	 * specialized hardware or proprietary driver firmware ("hard MAC").
452 	 *
453 	 * L2 implementations will have to provide a "soft MAC" fallback for
454 	 * these features in case the driver does not support them natively.
455 	 *
456 	 * Note: Some of these offloading capabilities may be mandatory in
457 	 * practice to stay within timing requirements of certain IEEE 802.15.4
458 	 * protocols, e.g. CPUs may not be fast enough to send ACKs within the
459 	 * required delays in the 2.4 GHz band without hard MAC support.
460 	 */
461 
462 	/** Frame checksum verification supported */
463 	IEEE802154_HW_FCS = BIT(1),
464 
465 	/** Filtering of PAN ID, extended and short address supported */
466 	IEEE802154_HW_FILTER = BIT(2),
467 
468 	/** Promiscuous mode supported */
469 	IEEE802154_HW_PROMISC = BIT(3),
470 
471 	/** CSMA-CA procedure supported on TX */
472 	IEEE802154_HW_CSMA = BIT(4),
473 
474 	/** Waits for ACK on TX if AR bit is set in TX pkt */
475 	IEEE802154_HW_TX_RX_ACK = BIT(5),
476 
477 	/** Supports retransmission on TX ACK timeout */
478 	IEEE802154_HW_RETRANSMISSION = BIT(6),
479 
480 	/** Sends ACK on RX if AR bit is set in RX pkt */
481 	IEEE802154_HW_RX_TX_ACK = BIT(7),
482 
483 	/** TX at specified time supported */
484 	IEEE802154_HW_TXTIME = BIT(8),
485 
486 	/** TX directly from sleep supported
487 	 *
488 	 *  @note This HW capability does not conform to the requirements
489 	 *  specified in #61227 as it closely couples the driver to OpenThread's
490 	 *  capability and device model which is different from Zephyr's:
491 	 *   - "Sleeping" is a well defined term in Zephyr related to internal
492 	 *     power and thread management and different from "RX off" as
493 	 *     defined in OT.
494 	 *   - Currently all OT-capable drivers have the "sleep to TX"
495 	 *     capability anyway plus we expect future drivers to implement it
496 	 *     ootb as well, so no information is actually conveyed by this
497 	 *     capability.
498 	 *   - The `start()`/`stop()` API of a net device controls the
499 	 *     interface's operational state. Drivers MUST respond with
500 	 *     -ENETDOWN when calling `tx()` while their operational state is
501 	 *     "DOWN", only devices in the "UP" state MAY transmit packets (RFC
502 	 *     2863).
503 	 *   - A migration path has been defined in #63670 for actual removal of
504 	 *     this capability in favor of a standard compliant
505 	 *     `configure(rx_on/rx_off)` call, see there for details.
506 	 *
507 	 * @deprecated Drivers and L2 SHALL not introduce additional references
508 	 * to this capability and remove existing ones as outlined in #63670.
509 	 */
510 	IEEE802154_HW_SLEEP_TO_TX = BIT(9),
511 
512 	/** Timed RX window scheduling supported */
513 	IEEE802154_HW_RXTIME = BIT(10),
514 
515 	/** TX security supported (key management, encryption and authentication) */
516 	IEEE802154_HW_TX_SEC = BIT(11),
517 
518 	/** RxOnWhenIdle handling supported */
519 	IEEE802154_RX_ON_WHEN_IDLE = BIT(12),
520 
521 	/* Note: Update also IEEE802154_HW_CAPS_BITS_COMMON_COUNT when changing
522 	 * the ieee802154_hw_caps type.
523 	 */
524 };
525 
526 /** @brief Number of bits used by ieee802154_hw_caps type. */
527 #define IEEE802154_HW_CAPS_BITS_COMMON_COUNT (13)
528 
529 /** @brief This and higher values are specific to the protocol- or driver-specific extensions. */
530 #define IEEE802154_HW_CAPS_BITS_PRIV_START IEEE802154_HW_CAPS_BITS_COMMON_COUNT
531 
532 /** Filter type, see @ref ieee802154_radio_api::filter */
533 enum ieee802154_filter_type {
534 	IEEE802154_FILTER_TYPE_IEEE_ADDR,
535 	IEEE802154_FILTER_TYPE_SHORT_ADDR,
536 	IEEE802154_FILTER_TYPE_PAN_ID,
537 	IEEE802154_FILTER_TYPE_SRC_IEEE_ADDR,
538 	IEEE802154_FILTER_TYPE_SRC_SHORT_ADDR,
539 };
540 
541 /** Driver events, see @ref IEEE802154_CONFIG_EVENT_HANDLER */
542 enum ieee802154_event {
543 	/** Data transmission started */
544 	IEEE802154_EVENT_TX_STARTED,
545 	/** Data reception failed */
546 	IEEE802154_EVENT_RX_FAILED,
547 	/**
548 	 * An RX slot ended, requires @ref IEEE802154_HW_RXTIME.
549 	 *
550 	 * @note This event SHALL not be triggered by drivers when RX is
551 	 * synchronously switched of due to a call to `stop()` or an RX slot
552 	 * being configured.
553 	 */
554 	IEEE802154_EVENT_RX_OFF,
555 };
556 
557 /** RX failed event reasons, see @ref IEEE802154_EVENT_RX_FAILED */
558 enum ieee802154_rx_fail_reason {
559 	/** Nothing received */
560 	IEEE802154_RX_FAIL_NOT_RECEIVED,
561 	/** Frame had invalid checksum */
562 	IEEE802154_RX_FAIL_INVALID_FCS,
563 	/** Address did not match */
564 	IEEE802154_RX_FAIL_ADDR_FILTERED,
565 	/** General reason */
566 	IEEE802154_RX_FAIL_OTHER
567 };
568 
569 /** Energy scan callback */
570 typedef void (*energy_scan_done_cb_t)(const struct device *dev,
571 				      int16_t max_ed);
572 
573 /** Driver event callback */
574 typedef void (*ieee802154_event_cb_t)(const struct device *dev,
575 				      enum ieee802154_event evt,
576 				      void *event_params);
577 
578 /** Filter value, see @ref ieee802154_radio_api::filter */
579 struct ieee802154_filter {
580 	union {
581 		/** Extended address, in little endian */
582 		uint8_t *ieee_addr;
583 		/** Short address, in CPU byte order */
584 		uint16_t short_addr;
585 		/** PAN ID, in CPU byte order */
586 		uint16_t pan_id;
587 	};
588 };
589 
590 /**
591  * Key configuration for transmit security offloading, see @ref
592  * IEEE802154_CONFIG_MAC_KEYS.
593  */
594 struct ieee802154_key {
595 	/** Key material */
596 	uint8_t *key_value;
597 	/** Initial value of frame counter associated with the key, see section 9.4.3 */
598 	uint32_t key_frame_counter;
599 	/** Indicates if per-key frame counter should be used, see section 9.4.3 */
600 	bool frame_counter_per_key;
601 	/** Key Identifier Mode, see section 9.4.2.3, Table 9-7 */
602 	uint8_t key_id_mode;
603 	/** Key Identifier, see section 9.4.4 */
604 	uint8_t *key_id;
605 };
606 
607 /** IEEE 802.15.4 Transmission mode. */
608 enum ieee802154_tx_mode {
609 	/** Transmit packet immediately, no CCA. */
610 	IEEE802154_TX_MODE_DIRECT,
611 
612 	/** Perform CCA before packet transmission. */
613 	IEEE802154_TX_MODE_CCA,
614 
615 	/**
616 	 * Perform full CSMA/CA procedure before packet transmission.
617 	 *
618 	 * @note requires IEEE802154_HW_CSMA capability.
619 	 */
620 	IEEE802154_TX_MODE_CSMA_CA,
621 
622 	/**
623 	 * Transmit packet in the future, at the specified time, no CCA.
624 	 *
625 	 * @note requires IEEE802154_HW_TXTIME capability.
626 	 */
627 	IEEE802154_TX_MODE_TXTIME,
628 
629 	/**
630 	 * Transmit packet in the future, perform CCA before transmission.
631 	 *
632 	 * @note requires IEEE802154_HW_TXTIME capability.
633 	 *
634 	 * @note Required for Thread 1.2 Coordinated Sampled Listening feature
635 	 * (see Thread specification 1.2.0, ch. 3.2.6.3).
636 	 */
637 	IEEE802154_TX_MODE_TXTIME_CCA,
638 
639 	/** Number of modes defined in ieee802154_tx_mode. */
640 	IEEE802154_TX_MODE_COMMON_COUNT,
641 
642 	/** This and higher values are specific to the protocol- or driver-specific extensions. */
643 	IEEE802154_TX_MODE_PRIV_START = IEEE802154_TX_MODE_COMMON_COUNT,
644 };
645 
646 /** IEEE 802.15.4 Frame Pending Bit table address matching mode. */
647 enum ieee802154_fpb_mode {
648 	/** The pending bit shall be set only for addresses found in the list. */
649 	IEEE802154_FPB_ADDR_MATCH_THREAD,
650 
651 	/** The pending bit shall be cleared for short addresses found in the
652 	 *  list.
653 	 */
654 	IEEE802154_FPB_ADDR_MATCH_ZIGBEE,
655 };
656 
657 /** IEEE 802.15.4 driver configuration types. */
658 enum ieee802154_config_type {
659 	/**
660 	 * Indicates how the driver should set the Frame Pending bit in ACK
661 	 * responses for Data Requests. If enabled, the driver should determine
662 	 * whether to set the bit or not based on the information provided with
663 	 * @ref IEEE802154_CONFIG_ACK_FPB config and FPB address matching mode
664 	 * specified. Otherwise, Frame Pending bit should be set to ``1`` (see
665 	 * section 6.7.3).
666 	 *
667 	 * @note requires @ref IEEE802154_HW_TX_RX_ACK capability and is
668 	 * available in any interface operational state.
669 	 */
670 	IEEE802154_CONFIG_AUTO_ACK_FPB,
671 
672 	/**
673 	 * Indicates whether to set ACK Frame Pending bit for specific address
674 	 * or not. Disabling the Frame Pending bit with no address provided
675 	 * (NULL pointer) should disable it for all enabled addresses.
676 	 *
677 	 * @note requires @ref IEEE802154_HW_TX_RX_ACK capability and is
678 	 * available in any interface operational state.
679 	 */
680 	IEEE802154_CONFIG_ACK_FPB,
681 
682 	/**
683 	 * Indicates whether the device is a PAN coordinator. This influences
684 	 * packet filtering.
685 	 *
686 	 * @note Available in any interface operational state.
687 	 */
688 	IEEE802154_CONFIG_PAN_COORDINATOR,
689 
690 	/**
691 	 * Enable/disable promiscuous mode.
692 	 *
693 	 * @note Available in any interface operational state.
694 	 */
695 	IEEE802154_CONFIG_PROMISCUOUS,
696 
697 	/**
698 	 * Specifies new IEEE 802.15.4 driver event handler. Specifying NULL as
699 	 * a handler will disable events notification.
700 	 *
701 	 * @note Available in any interface operational state.
702 	 */
703 	IEEE802154_CONFIG_EVENT_HANDLER,
704 
705 	/**
706 	 * Updates MAC keys, key index and the per-key frame counter for drivers
707 	 * supporting transmit security offloading, see section 9.5, tables 9-9
708 	 * and 9-10. The key configuration SHALL NOT be accepted if the frame
709 	 * counter (in case frame counter per key is true) is not strictly
710 	 * larger than the current frame counter associated with the same key,
711 	 * see sections 8.2.2, 9.2.4 g/h) and 9.4.3.
712 	 *
713 	 * @note Requires @ref IEEE802154_HW_TX_SEC capability and is available
714 	 * in any interface operational state.
715 	 */
716 	IEEE802154_CONFIG_MAC_KEYS,
717 
718 	/**
719 	 * Sets the current MAC frame counter value associated with the
720 	 * interface for drivers supporting transmit security offloading, see
721 	 * section 9.5, table 9-8, secFrameCounter.
722 	 *
723 	 * @warning The frame counter MUST NOT be accepted if it is not
724 	 * strictly greater than the current frame counter associated with the
725 	 * interface, see sections 8.2.2, 9.2.4 g/h) and 9.4.3. Otherwise the
726 	 * replay protection provided by the frame counter may be compromised.
727 	 * Drivers SHALL return -EINVAL in case the configured frame counter
728 	 * does not conform to this requirement.
729 	 *
730 	 * @note Requires @ref IEEE802154_HW_TX_SEC capability and is available
731 	 * in any interface operational state.
732 	 */
733 	IEEE802154_CONFIG_FRAME_COUNTER,
734 
735 	/**
736 	 * Sets the current MAC frame counter value if the provided value is greater than
737 	 * the current one.
738 	 *
739 	 * @note Requires @ref IEEE802154_HW_TX_SEC capability and is available
740 	 * in any interface operational state.
741 	 *
742 	 * @warning This configuration option does not conform to the
743 	 * requirements specified in #61227 as it is redundant with @ref
744 	 * IEEE802154_CONFIG_FRAME_COUNTER, and will therefore be deprecated in
745 	 * the future.
746 	 */
747 	IEEE802154_CONFIG_FRAME_COUNTER_IF_LARGER,
748 
749 	/**
750 	 * Set or unset a radio reception window (RX slot). This can be used for
751 	 * any scheduled reception, e.g.: Zigbee GP device, CSL, TSCH, etc.
752 	 *
753 	 * @details The start and duration parameters of the RX slot are
754 	 * relative to the network subsystem's local clock. If the start
755 	 * parameter of the RX slot is -1 then any previously configured RX
756 	 * slot SHALL be canceled immediately. If the start parameter is any
757 	 * value in the past (including 0) or the duration parameter is zero
758 	 * then the receiver SHALL remain off forever until the RX slot has
759 	 * either been removed or re-configured to point to a future start
760 	 * time. If an RX slot is configured while the previous RX slot is
761 	 * still scheduled, then the previous slot SHALL be cancelled and the
762 	 * new slot scheduled instead.
763 	 *
764 	 * RX slots MAY be programmed while the driver is "DOWN". If any past
765 	 * or future RX slot is configured when calling `start()` then the
766 	 * interface SHALL be placed in "UP" state but the receiver SHALL not
767 	 * be started.
768 	 *
769 	 * The driver SHALL take care to start/stop the receiver autonomously,
770 	 * asynchronously and automatically around the RX slot. The driver
771 	 * SHALL resume power just before the RX slot and suspend it again
772 	 * after the slot unless another programmed event forces the driver not
773 	 * to suspend. The driver SHALL switch to the programmed channel
774 	 * before the RX slot and back to the channel set with set_channel()
775 	 * after the RX slot. If the driver interface is "DOWN" when the start
776 	 * time of an RX slot arrives, then the RX slot SHALL not be observed
777 	 * and the receiver SHALL remain off.
778 	 *
779 	 * If the driver is "UP" while configuring an RX slot, the driver SHALL
780 	 * turn off the receiver immediately and (possibly asynchronously) put
781 	 * the driver into the lowest possible power saving mode until the
782 	 * start of the RX slot. If the driver is "UP" while the RX slot is
783 	 * deleted, then the driver SHALL enable the receiver immediately. The
784 	 * receiver MUST be ready to receive packets before returning from the
785 	 * `configure()` operation in this case.
786 	 *
787 	 * This behavior means that setting an RX slot implicitly sets the MAC
788 	 * PIB attribute macRxOnWhenIdle (see section 8.4.3.1, table 8-94) to
789 	 * "false" while deleting the RX slot implicitly sets macRxOnWhenIdle to
790 	 * "true".
791 	 *
792 	 * @note requires @ref IEEE802154_HW_RXTIME capability and is available
793 	 * in any interface operational state.
794 	 *
795 	 * @note Required for Thread 1.2 Coordinated Sampled Listening feature
796 	 * (see Thread specification 1.2.0, ch. 3.2.6.3).
797 	 */
798 	IEEE802154_CONFIG_RX_SLOT,
799 
800 	/**
801 	 * Enables or disables a device as a CSL receiver and configures its CSL
802 	 * period.
803 	 *
804 	 * @details Configures the CSL period in units of 10 symbol periods.
805 	 * Values greater than zero enable CSL if the driver supports it and the
806 	 * device starts to operate as a CSL receiver. Setting this to zero
807 	 * disables CSL on the device. If the driver does not support CSL, the
808 	 * configuration call SHALL return -ENOTSUP.
809 	 *
810 	 * See section 7.4.2.3 and section 8.4.3.6, table 8-104, macCslPeriod.
811 	 *
812 	 * @note Confusingly the standard calls the CSL receiver "CSL
813 	 * coordinator" (i.e. "coordinating the CSL protocol timing", see
814 	 * section 6.12.2.2), although, typically, a CSL coordinator is NOT also
815 	 * an IEEE 802.15.4 FFD coordinator or PAN coordintor but a simple RFD
816 	 * end device (compare the device roles outlined in sections 5.1, 5.3,
817 	 * 5.5 and 6.1). To avoid confusion we therefore prefer calling CSL
818 	 * coordinators (typically an RFD end device) "CSL receivers" and CSL
819 	 * peer devices (typically FFD coordinators or PAN coordinators) "CSL
820 	 * transmitters". Also note that at this time, we do NOT support
821 	 * unsynchronized transmission with CSL wake up frames as specified in
822 	 * section 6.12.2.4.4.
823 	 *
824 	 * To offload CSL receiver timing to the driver the upper layer SHALL
825 	 * combine several configuration options in the following way:
826 	 *
827 	 * 1. Use @ref IEEE802154_CONFIG_ENH_ACK_HEADER_IE once with an
828 	 *    appropriate pre-filled CSL IE and the CSL phase set to an
829 	 *    arbitrary value or left uninitialized. The CSL phase SHALL be
830 	 *    injected on-the-fly by the driver at runtime as outlined in 2.
831 	 *    below. Adding a short and extended address will inform the driver
832 	 *    of the specific CSL receiver to which it SHALL inject CSL IEs. If
833 	 *    no addresses are given then the CSL IE will be injected into all
834 	 *    enhanced ACK frames as soon as CSL is enabled.  This configuration
835 	 *    SHALL be done before enabling CSL by setting a CSL period greater
836 	 *    than zero.
837 	 *
838 	 * 2. Configure @ref IEEE802154_CONFIG_EXPECTED_RX_TIME immediately
839 	 *    followed by @ref IEEE802154_CONFIG_CSL_PERIOD. To prevent race
840 	 *    conditions, the upper layer SHALL ensure that the receiver is not
841 	 *    enabled during or between the two calls (e.g. by a previously
842 	 *    configured RX slot) nor SHALL a frame be transmitted concurrently.
843 	 *
844 	 *    The expected RX time SHALL point to the end of SFD of an ideally
845 	 *    timed RX frame in an arbitrary past or future CSL channel sample,
846 	 *    i.e.  whose "end of SFD" arrives exactly at the locally predicted
847 	 *    time inside the CSL channel sample.
848 	 *
849 	 *    The driver SHALL derive CSL anchor points and the CSL phase from
850 	 *    the given expected RX time as follows:
851 	 *
852 	 *        cslAnchorPointNs = last expected RX time
853 	 *                           + PHY-specific PHR duration in ns
854 	 *
855 	 *        startOfMhrNs = start of MHR of the frame containing the
856 	 *                       CSL IE relative to the local network clock
857 	 *
858 	 *        cslPhase = (startOfMhrNs - cslAnchorPointNs)
859 	 *                   / (10 * PHY specific symbol period in ns)
860 	 *                   % cslPeriod
861 	 *
862 	 *    The driver SHALL set the CSL phase in the IE configured in 1.  and
863 	 *    inject that IE on-the-fly into outgoing enhanced ACK frames if the
864 	 *    destination address conforms to the IE's address filter.
865 	 *
866 	 * 3. Use @ref IEEE802154_CONFIG_RX_SLOT periodically to schedule
867 	 *    each CSL channel sample early enough before its start time. The
868 	 *    size of the CSL channel sample SHALL take relative clock drift and
869 	 *    scheduling uncertainties with respect to CSL transmitters into
870 	 *    account as specified by the standard such that at least the full
871 	 *    SHR of a legitimate RX frame is guaranteed to land inside the
872 	 *    channel sample.
873 	 *
874 	 *    To this avail, the last configured expected RX time plus an
875 	 *    integer number of CSL periods SHALL point to a fixed offset of the
876 	 *    RX slot (not necessarily its center):
877 	 *
878 	 *        expectedRxTimeNs_N = last expected RX time
879 	 *            + N * (cslPeriod * 10 * PHY-specific symbol period in ns)
880 	 *
881 	 *        expectedRxTimeNs_N - rxSlot_N.start == const for all N
882 	 *
883 	 *    While the configured CSL period is greater than zero, drivers
884 	 *    SHOULD validate the offset of the expected RX time inside each RX
885 	 *    slot accordingly. If the driver finds that the offset varies from
886 	 *    slot to slot, drivers SHOULD log the difference but SHALL
887 	 *    nevertheless accept and schedule the RX slot with a zero success
888 	 *    value to work around minor implementation or rounding errors in
889 	 *    upper layers.
890 	 *
891 	 * Configure and start a CSL receiver:
892 	 *
893 	 *     ENH_ACK_HEADER_IE
894 	 *        |
895 	 *        | EXPECTED_RX_TIME (end of SFD of a perfectly timed RX frame
896 	 *        |    |              in any past or future channel sample)
897 	 *        |    |
898 	 *        |    | CSL_PERIOD (>0)            RX_SLOT
899 	 *        |    |    |                          |
900 	 *        v    v    v                          v
901 	 *     -----------------------------------------------[-CSL channel sample ]----+
902 	 *                                         ^                                    |
903 	 *                                         |                                    |
904 	 *                                         +--------------------- loop ---------+
905 	 *
906 	 * Disable CSL on the receiver:
907 	 *
908 	 *     CSL_PERIOD (=0)
909 	 *        |
910 	 *        v
911 	 *     ---------------------
912 	 *
913 	 * Update the CSL period to a new value:
914 	 *
915 	 *     EXPECTED_RX_TIME (based on updated period)
916 	 *        |
917 	 *        |  CSL_PERIOD (>0, updated)       RX_SLOT
918 	 *        |     |                              |
919 	 *        v     v                              v
920 	 *     -----------------------------------------------[-CSL channel sample ]----+
921 	 *                                         ^                                    |
922 	 *                                         |                                    |
923 	 *                                         +--------------------- loop ---------+
924 	 *
925 	 * @note Available in any interface operational state.
926 	 *
927 	 * @note Required for Thread 1.2 Coordinated Sampled Listening feature
928 	 * (see Thread specification 1.2.0, ch. 3.2.6.3).
929 	 */
930 	IEEE802154_CONFIG_CSL_PERIOD,
931 
932 	/**
933 	 * Configure a timepoint at which an RX frame is expected to arrive.
934 	 *
935 	 * @details Configure the nanosecond resolution timepoint relative to
936 	 * the network subsystem's local clock at which an RX frame's end of SFD
937 	 * (i.e. equivalently its end of SHR, start of PHR, or in the case of
938 	 * PHYs with RDEV or ERDEV capability the RMARKER) is expected to arrive
939 	 * at the local antenna assuming perfectly synchronized local and remote
940 	 * network clocks and zero distance between antennas.
941 	 *
942 	 * This parameter MAY be used to offload parts of timing sensitive TDMA
943 	 * (e.g.  TSCH, beacon-enabled PAN including DSME), low-energy (e.g.
944 	 * CSL, RIT) or ranging (TDoA) protocols to the driver. In these
945 	 * protocols, medium access is tightly controlled such that the expected
946 	 * arrival time of a frame can be predicted within a well-defined time
947 	 * window. This feature will typically be combined with @ref
948 	 * IEEE802154_CONFIG_RX_SLOT although this is not a hard requirement.
949 	 *
950 	 * The "expected RX time" MAY be interpreted slightly differently
951 	 * depending on the protocol context:
952 	 * - CSL phase (i.e. time to the next expected CSL transmission) or anchor
953 	 *   time (i.e. any arbitrary timepoint with "zero CSL phase") SHALL be
954 	 *   derived by adding the PHY header duration to the expected RX time
955 	 *   to calculate the "start of MHR" ("first symbol of MAC", see section
956 	 *   6.12.2.1) required by the CSL protocol, compare @ref
957 	 *   IEEE802154_CONFIG_CSL_PERIOD.
958 	 * - In TSCH the expected RX time MAY be set to macTsRxOffset +
959 	 *   macTsRxWait / 2. Then the time correction SHALL be calculated as
960 	 *   the expected RX time minus actual arrival timestamp, see section
961 	 *   6.5.4.3.
962 	 * - In ranging applications, time difference of arrival (TDOA) MAY be
963 	 *   calculated inside the driver comparing actual RMARKER timestamps
964 	 *   against the assumed synchronized time at which the ranging frame
965 	 *   was sent, see IEEE 802.15.4z.
966 	 *
967 	 * In case of periodic protocols (e.g. CSL channel samples, periodic
968 	 * beacons of a single PAN, periodic ranging "blinks"), a single
969 	 * timestamp at any time in the past or in the future may be given from
970 	 * which other expected timestamps can be derived by adding or
971 	 * substracting multiples of the RX period. See e.g. the CSL
972 	 * documentation in this API.
973 	 *
974 	 * Additionally this parameter MAY be used by drivers to discipline
975 	 * their local representation of a distributed network clock by deriving
976 	 * synchronization instants related to a remote representation of the
977 	 * same clock (as in PTP).
978 	 *
979 	 * @note Available in any interface operational state.
980 	 *
981 	 * @note Required for Thread 1.2 Coordinated Sampled Listening feature
982 	 * (see Thread specification 1.2.0, ch. 3.2.6.3).
983 	 */
984 	IEEE802154_CONFIG_EXPECTED_RX_TIME,
985 
986 	/**
987 	 * Adds a header information element (IE) to be injected into enhanced
988 	 * ACK frames generated by the driver if the given destination address
989 	 * filter matches.
990 	 *
991 	 * @details Drivers implementing the @ref IEEE802154_HW_RX_TX_ACK
992 	 * capability generate ACK frames autonomously. Setting this
993 	 * configuration will ask the driver to inject the given preconfigured
994 	 * header IE when generating enhanced ACK frames where appropriate by
995 	 * the standard. IEs for all other frame types SHALL be provided by L2.
996 	 *
997 	 * The driver shall return -ENOTSUP in the following cases:
998 	 * - It does not support the @ref IEEE802154_HW_RX_TX_ACK,
999 	 * - It does not support header IE injection,
1000 	 * - It cannot inject the runtime fields on-the-fly required for the
1001 	 *   given IE element ID (see list below).
1002 	 *
1003 	 * Enhanced ACK header IEs (element IDs in parentheses) that either
1004 	 * need to be rejected or explicitly supported and parsed by the driver
1005 	 * because they require on-the-fly timing information injection are:
1006 	 * - CSL IE (0x1a)
1007 	 * - Rendezvous Time IE (0x1d)
1008 	 * - Time Correction IE (0x1e)
1009 	 *
1010 	 * Drivers accepting this configuration option SHALL check the list of
1011 	 * configured IEs for each outgoing enhanced ACK frame, select the ones
1012 	 * appropriate for the received frame based on their element ID, inject
1013 	 * any required runtime information on-the-fly and include the selected
1014 	 * IEs into the enhanced ACK frame's MAC header.
1015 	 *
1016 	 * Drivers supporting enhanced ACK header IE injection SHALL
1017 	 * autonomously inject header termination IEs as required by the
1018 	 * standard.
1019 	 *
1020 	 * A destination short address and extended address MAY be given by L2
1021 	 * to filter the devices to which the given IE is included. Setting the
1022 	 * short address to the broadcast address and the extended address to
1023 	 * NULL will inject the given IE into all ACK frames unless a more
1024 	 * specific filter is also present for any given destination device
1025 	 * (fallback configuration). L2 SHALL take care to either set both
1026 	 * address fields to valid device addresses or none.
1027 	 *
1028 	 * This configuration type may be called several times with distinct
1029 	 * element IDs and/or addresses. The driver SHALL either store all
1030 	 * configured IE/address combinations or return -ENOMEM if no
1031 	 * additional configuration can be stored.
1032 	 *
1033 	 * Configuring a header IE with a previously configured element ID and
1034 	 * address filter SHALL override the previous configuration. This
1035 	 * implies that repetition of the same header IE/address combination is
1036 	 * NOT supported.
1037 	 *
1038 	 * Configuring an existing element ID/address filter combination with
1039 	 * the header IE's length field set to zero SHALL remove that
1040 	 * configuration. SHALL remove the fallback configuration if no address
1041 	 * is given.
1042 	 *
1043 	 * Configuring a header IE for an address filter with the header IE
1044 	 * pointer set to NULL SHALL remove all header IE's for that address
1045 	 * filter. SHALL remove ALL header IE configuration (including but not
1046 	 * limited to fallbacks) if no address is given.
1047 	 *
1048 	 * If any of the deleted configurations didn't previously exist, then
1049 	 * the call SHALL be ignored. Whenever the length field is set to zero,
1050 	 * the content fields MUST NOT be accessed by the driver.
1051 	 *
1052 	 * L2 SHALL minimize the space required to keep IE configuration inside
1053 	 * the driver by consolidating address filters and by removing
1054 	 * configuation that is no longer required.
1055 	 *
1056 	 * @note requires @ref IEEE802154_HW_RX_TX_ACK capability and is
1057 	 * available in any interface operational state. Currently we only
1058 	 * support header IEs but that may change in the future.
1059 	 *
1060 	 * @note Required for Thread 1.2 Coordinated Sampled Listening feature
1061 	 * (see Thread specification 1.2.0, ch. 3.2.6.3).
1062 	 *
1063 	 * @note Required for Thread 1.2 Link Metrics feature (see Thread
1064 	 * specification 1.2.0, ch. 4.11.3.3).
1065 	 */
1066 	IEEE802154_CONFIG_ENH_ACK_HEADER_IE,
1067 
1068 	/**
1069 	 * Enable/disable RxOnWhenIdle MAC PIB attribute (Table 8-94).
1070 	 *
1071 	 * Since there is no clear guidance in IEEE 802.15.4 specification about the definition of
1072 	 * an "idle period", this implementation expects that drivers use the RxOnWhenIdle attribute
1073 	 * to determine next radio state (false --> off, true --> receive) in the following
1074 	 * scenarios:
1075 	 * - Finalization of a regular frame reception task, provided that:
1076 	 *   - The frame is received without errors and passes the filtering and it's not an
1077 	 *     spurious ACK.
1078 	 *   - ACK is not requested or transmission of ACK is not possible due to internal
1079 	 *     conditions.
1080 	 * - Finalization of a frame transmission or transmission of an ACK frame, when ACK is not
1081 	 *     requested in the transmitted frame.
1082 	 * - Finalization of the reception operation of a requested ACK due to:
1083 	 *   - ACK timeout expiration.
1084 	 *   - Reception of an invalid ACK or not an ACK frame.
1085 	 *   - Reception of the proper ACK, unless the transmitted frame was a Data Request Command
1086 	 *     and the frame pending bit on the received ACK is set to true. In this case the radio
1087 	 *     platform implementation SHOULD keep the receiver on until a determined timeout which
1088 	 *     triggers an idle period start.
1089 	 * - Finalization of a stand alone CCA task.
1090 	 * - Finalization of a CCA operation with busy result during CSMA/CA procedure.
1091 	 * - Finalization of an Energy Detection task.
1092 	 * - Finalization of a scheduled radio reception window
1093 	 *     (see @ref IEEE802154_CONFIG_RX_SLOT).
1094 	 */
1095 	IEEE802154_CONFIG_RX_ON_WHEN_IDLE,
1096 
1097 	/** Number of types defined in ieee802154_config_type. */
1098 	IEEE802154_CONFIG_COMMON_COUNT,
1099 
1100 	/** This and higher values are specific to the protocol- or driver-specific extensions. */
1101 	IEEE802154_CONFIG_PRIV_START = IEEE802154_CONFIG_COMMON_COUNT,
1102 };
1103 
1104 /**
1105  * Configuring an RX slot with the start parameter set to this value will cancel
1106  * and delete any previously configured RX slot.
1107  */
1108 #define IEEE802154_CONFIG_RX_SLOT_NONE -1LL
1109 
1110 /**
1111  * Configuring an RX slot with this start parameter while the driver is "down",
1112  * will keep RX off when the driver is being started. Configuring an RX slot
1113  * with this start value while the driver is "up" will immediately switch RX off
1114  * until either the slot is deleted, see @ref IEEE802154_CONFIG_RX_SLOT_NONE or
1115  * a slot with a future start parameter is configured and that start time
1116  * arrives.
1117  */
1118 #define IEEE802154_CONFIG_RX_SLOT_OFF  0LL
1119 
1120 /** IEEE 802.15.4 driver configuration data. */
1121 struct ieee802154_config {
1122 	/** Configuration data. */
1123 	union {
1124 		/** see @ref IEEE802154_CONFIG_AUTO_ACK_FPB */
1125 		struct {
1126 			bool enabled;
1127 			enum ieee802154_fpb_mode mode;
1128 		} auto_ack_fpb;
1129 
1130 		/** see @ref IEEE802154_CONFIG_ACK_FPB */
1131 		struct {
1132 			uint8_t *addr; /* in little endian for both, short and extended address */
1133 			bool extended;
1134 			bool enabled;
1135 		} ack_fpb;
1136 
1137 		/** see @ref IEEE802154_CONFIG_PAN_COORDINATOR */
1138 		bool pan_coordinator;
1139 
1140 		/** see @ref IEEE802154_CONFIG_PROMISCUOUS */
1141 		bool promiscuous;
1142 
1143 		/** see @ref IEEE802154_CONFIG_RX_ON_WHEN_IDLE */
1144 		bool rx_on_when_idle;
1145 
1146 		/** see @ref IEEE802154_CONFIG_EVENT_HANDLER */
1147 		ieee802154_event_cb_t event_handler;
1148 
1149 		/**
1150 		 * @brief see @ref IEEE802154_CONFIG_MAC_KEYS
1151 		 *
1152 		 * @details Pointer to an array containing a list of keys used
1153 		 * for MAC encryption. Refer to secKeyIdLookupDescriptor and
1154 		 * secKeyDescriptor in IEEE 802.15.4
1155 		 *
1156 		 * The key_value field points to a buffer containing the 16 byte
1157 		 * key. The buffer SHALL be copied by the driver before
1158 		 * returning from the call.
1159 		 *
1160 		 * The variable length array is terminated by key_value field
1161 		 * set to NULL.
1162 		 */
1163 		struct ieee802154_key *mac_keys;
1164 
1165 		/** see @ref IEEE802154_CONFIG_FRAME_COUNTER */
1166 		uint32_t frame_counter;
1167 
1168 		/** see @ref IEEE802154_CONFIG_RX_SLOT */
1169 		struct {
1170 			/**
1171 			 * Nanosecond resolution timestamp relative to the
1172 			 * network subsystem's local clock defining the start of
1173 			 * the RX window during which the receiver is expected
1174 			 * to be listening (i.e. not including any driver
1175 			 * startup times).
1176 			 *
1177 			 * Configuring an rx_slot with the start attribute set
1178 			 * to -1 will cancel and delete any previously active rx
1179 			 * slot.
1180 			 */
1181 			net_time_t start;
1182 
1183 			/**
1184 			 * Nanosecond resolution duration of the RX window
1185 			 * relative to the above RX window start time during
1186 			 * which the receiver is expected to be listening (i.e.
1187 			 * not including any shutdown times). Only positive
1188 			 * values larger than or equal zero are allowed.
1189 			 *
1190 			 * Setting the duration to zero will disable the
1191 			 * receiver, no matter what the start parameter.
1192 			 */
1193 			net_time_t duration;
1194 
1195 			uint8_t channel;
1196 		} rx_slot;
1197 
1198 		/**
1199 		 * see @ref IEEE802154_CONFIG_CSL_PERIOD
1200 		 *
1201 		 * in CPU byte order
1202 		 */
1203 		uint32_t csl_period;
1204 
1205 		/**
1206 		 * see @ref IEEE802154_CONFIG_EXPECTED_RX_TIME
1207 		 */
1208 		net_time_t expected_rx_time;
1209 
1210 		/** see @ref IEEE802154_CONFIG_ENH_ACK_HEADER_IE */
1211 		struct {
1212 			/**
1213 			 * Pointer to the header IE, see section 7.4.2.1,
1214 			 * figure 7-21
1215 			 *
1216 			 * Certain header IEs may be incomplete if they require
1217 			 * timing information to be injected at runtime
1218 			 * on-the-fly, see the list in @ref
1219 			 * IEEE802154_CONFIG_ENH_ACK_HEADER_IE.
1220 			 */
1221 			struct ieee802154_header_ie *header_ie;
1222 
1223 			/**
1224 			 * Filters the devices that will receive this IE by
1225 			 * extended address. MAY be set to NULL to configure a
1226 			 * fallback for all devices (implies that short_addr
1227 			 * MUST also be set to @ref
1228 			 * IEEE802154_BROADCAST_ADDRESS).
1229 			 *
1230 			 * in big endian
1231 			 */
1232 			const uint8_t *ext_addr;
1233 
1234 			/**
1235 			 * Filters the devices that will receive this IE by
1236 			 * short address. MAY be set to @ref
1237 			 * IEEE802154_BROADCAST_ADDRESS to configure a fallback
1238 			 * for all devices (implies that ext_addr MUST also set
1239 			 * to NULL in this case).
1240 			 *
1241 			 * in CPU byte order
1242 			 */
1243 			uint16_t short_addr;
1244 
1245 			/**
1246 			 * Flag for purging enh ACK header IEs.
1247 			 * When flag is set to true, driver should remove all existing
1248 			 * header IEs, and all other entries in config should be ignored.
1249 			 * This means that purging current header IEs and
1250 			 * configuring a new one in the same call is not allowed.
1251 			 */
1252 			bool purge_ie;
1253 		} ack_ie;
1254 	};
1255 };
1256 
1257 /**
1258  * @brief IEEE 802.15.4 driver attributes.
1259  *
1260  * See @ref ieee802154_attr_value and @ref ieee802154_radio_api for usage
1261  * details.
1262  */
1263 enum ieee802154_attr {
1264 	/**
1265 	 * Retrieves a bit field with supported channel pages. This attribute
1266 	 * SHALL be implemented by all drivers.
1267 	 */
1268 	IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES,
1269 
1270 	/**
1271 	 * Retrieves a pointer to the array of supported channel ranges within
1272 	 * the currently configured channel page. This attribute SHALL be
1273 	 * implemented by all drivers.
1274 	 */
1275 	IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES,
1276 
1277 	/**
1278 	 * Retrieves a bit field with supported HRP UWB nominal pulse repetition
1279 	 * frequencies. This attribute SHALL be implemented by all devices that
1280 	 * support channel page four (HRP UWB).
1281 	 */
1282 	IEEE802154_ATTR_PHY_HRP_UWB_SUPPORTED_PRFS,
1283 
1284 	/** Number of attributes defined in ieee802154_attr. */
1285 	IEEE802154_ATTR_COMMON_COUNT,
1286 
1287 	/** This and higher values are specific to the protocol- or
1288 	 * driver-specific extensions.
1289 	 */
1290 	IEEE802154_ATTR_PRIV_START = IEEE802154_ATTR_COMMON_COUNT,
1291 };
1292 
1293 /**
1294  * @brief IEEE 802.15.4 driver attribute values.
1295  *
1296  * @details This structure is reserved to scalar and structured attributes that
1297  * originate in the driver implementation and can neither be implemented as
1298  * boolean @ref ieee802154_hw_caps nor be derived directly or indirectly by the
1299  * MAC (L2) layer. In particular this structure MUST NOT be used to return
1300  * configuration data that originate from L2.
1301  *
1302  * @note To keep this union reasonably small, any attribute requiring a large
1303  * memory area, SHALL be provided pointing to static memory allocated by the
1304  * driver and valid throughout the lifetime of the driver instance.
1305  */
1306 struct ieee802154_attr_value {
1307 	union {
1308 		/* TODO: Implement configuration of phyCurrentPage once drivers
1309 		 * need to support channel page switching at runtime.
1310 		 */
1311 		/**
1312 		 * @brief A bit field that represents the supported channel
1313 		 * pages, see @ref ieee802154_phy_channel_page.
1314 		 *
1315 		 * @note To keep the API extensible as required by the standard,
1316 		 * supported pages are modeled as a bitmap to support drivers
1317 		 * that implement runtime switching between multiple channel
1318 		 * pages.
1319 		 *
1320 		 * @note Currently none of the Zephyr drivers implements more
1321 		 * than one channel page at runtime, therefore only one bit will
1322 		 * be set and the current channel page (see the PHY PIB
1323 		 * attribute phyCurrentPage, section 11.3, table 11-2) is
1324 		 * considered to be read-only, fixed and "well known" via the
1325 		 * supported channel pages attribute.
1326 		 */
1327 		uint32_t phy_supported_channel_pages;
1328 
1329 		/**
1330 		 * @brief Pointer to a structure representing channel ranges
1331 		 * currently available on the selected channel page.
1332 		 *
1333 		 * @warning The pointer must be valid and constant throughout
1334 		 * the life of the interface.
1335 		 *
1336 		 * @details The selected channel page corresponds to the
1337 		 * phyCurrentPage PHY PIB attribute, see the description of
1338 		 * phy_supported_channel_pages above. Currently it can be
1339 		 * retrieved via the @ref
1340 		 * IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES attribute.
1341 		 *
1342 		 * Most drivers will expose a single channel page with a single,
1343 		 * often zero-based, fixed channel range.
1344 		 *
1345 		 * Some notable exceptions:
1346 		 * * The legacy channel page (zero) exposes ranges in different
1347 		 *   bands and even PHYs that are usually not implemented by a
1348 		 *   single driver.
1349 		 * * SUN and LECIM PHYs specify a large number of bands and
1350 		 *   operating modes on a single page with overlapping channel
1351 		 *   ranges each. Some of these ranges are not zero-based or
1352 		 *   contain "holes". This explains why several ranges may be
1353 		 *   necessary to represent all available channels.
1354 		 * * UWB PHYs often support partial channel ranges on the same
1355 		 *   channel page depending on the supported bands.
1356 		 *
1357 		 * In these cases, drivers may expose custom configuration
1358 		 * attributes (Kconfig, devicetree, runtime, ...) that allow
1359 		 * switching between sub-ranges within the same channel page
1360 		 * (e.g. switching between SubG and 2.4G bands on channel page
1361 		 * zero or switching between multiple operating modes in the SUN
1362 		 * or LECIM PHYs.
1363 		 */
1364 		const struct ieee802154_phy_supported_channels *phy_supported_channels;
1365 
1366 		/* TODO: Allow the PRF to be configured for each TX call once
1367 		 * drivers need to support PRF switching at runtime.
1368 		 */
1369 		/**
1370 		 * @brief A bit field representing supported HRP UWB pulse
1371 		 * repetition frequencies (PRF), see enum
1372 		 * ieee802154_phy_hrp_uwb_nominal_prf.
1373 		 *
1374 		 * @note Currently none of the Zephyr HRP UWB drivers implements
1375 		 * more than one nominal PRF at runtime, therefore only one bit
1376 		 * will be set and the current PRF (UwbPrf, MCPS-DATA.request,
1377 		 * section 8.3.2, table 8-88) is considered to be read-only,
1378 		 * fixed and "well known" via the supported PRF attribute.
1379 		 */
1380 		uint32_t phy_hrp_uwb_supported_nominal_prfs;
1381 	};
1382 };
1383 
1384 /**
1385  * @brief Helper function to handle channel page and range to be called from
1386  * drivers' attr_get() implementation. This only applies to drivers with a
1387  * single channel page.
1388  *
1389  * @param attr The attribute to be retrieved.
1390  * @param phy_supported_channel_page The driver's unique channel page.
1391  * @param phy_supported_channels Pointer to the structure that contains the
1392  * driver's channel range or ranges.
1393  * @param value The pointer to the value struct provided by the user.
1394  *
1395  * @retval 0 if the attribute could be resolved
1396  * @retval -ENOENT if the attribute could not be resolved
1397  */
ieee802154_attr_get_channel_page_and_range(enum ieee802154_attr attr,const enum ieee802154_phy_channel_page phy_supported_channel_page,const struct ieee802154_phy_supported_channels * phy_supported_channels,struct ieee802154_attr_value * value)1398 static inline int ieee802154_attr_get_channel_page_and_range(
1399 	enum ieee802154_attr attr,
1400 	const enum ieee802154_phy_channel_page phy_supported_channel_page,
1401 	const struct ieee802154_phy_supported_channels *phy_supported_channels,
1402 	struct ieee802154_attr_value *value)
1403 {
1404 	switch (attr) {
1405 	case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_PAGES:
1406 		value->phy_supported_channel_pages = phy_supported_channel_page;
1407 		return 0;
1408 
1409 	case IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES:
1410 		value->phy_supported_channels = phy_supported_channels;
1411 		return 0;
1412 
1413 	default:
1414 		return -ENOENT;
1415 	}
1416 }
1417 
1418 /**
1419  * @brief IEEE 802.15.4 driver interface API.
1420  *
1421  * @note This structure is called "radio" API for backwards compatibility. A
1422  * better name would be "IEEE 802.15.4 driver API" as typical drivers will not
1423  * only implement L1/radio (PHY) features but also L2 (MAC) features if the
1424  * vendor-specific driver hardware or firmware offers offloading opportunities.
1425  *
1426  * @details While L1-level driver features are exclusively implemented by
1427  * drivers and MAY be mandatory to support certain application requirements, L2
1428  * features SHOULD be optional by default and only need to be implemented for
1429  * performance optimization or precise timing as deemed necessary by driver
1430  * maintainers. Fallback implementations ("Soft MAC") SHOULD be provided in the
1431  * driver-independent L2 layer for all L2/MAC features especially if these
1432  * features are not implemented in vendor hardware/firmware by a majority of
1433  * existing in-tree drivers. If, however, a driver offers offloading
1434  * opportunities then L2 implementations SHALL delegate performance critical or
1435  * resource intensive tasks to the driver.
1436  *
1437  * All drivers SHALL support two externally observable interface operational
1438  * states: "UP" and "DOWN". Drivers MAY additionally support a "TESTING"
1439  * interface state (see `continuous_carrier()`).
1440  *
1441  * The following rules apply:
1442  * * An interface is considered "UP" when it is able to transmit and receive
1443  *   packets, "DOWN" otherwise (see precise definitions of the corresponding
1444  *   ifOperStatus values in RFC 2863, section 3.1.14, @ref net_if_oper_state and
1445  *   the `continuous_carrier()` exception below). A device that has its receiver
1446  *   temporarily disabled during "UP" state due to an active receive window
1447  *   configuration is still considered "UP".
1448  * * Upper layers will assume that the interface managed by the driver is "UP"
1449  *   after a call to `start()` returned zero or `-EALREADY`. Upper layers assume
1450  *   that the interface is "DOWN" after calling `stop()` returned zero or
1451  *   `-EALREADY`.
1452  * * The driver SHALL block `start()`/`stop()` calls until the interface fully
1453  *   transitioned to the new state (e.g. the receiver is operational, ongoing
1454  *   transmissions were finished, etc.). Drivers SHOULD yield the calling thread
1455  *   (i.e. "sleep") if waiting for the new state without CPU interaction is
1456  *   possible.
1457  * * Drivers are responsible of guaranteeing atomicity of state changes.
1458  *   Appropriate means of synchronization SHALL be implemented (locking, atomic
1459  *   flags, ...).
1460  * * While the interface is "DOWN", the driver SHALL be placed in the lowest
1461  *   possible power state. The driver MAY return from a call to `stop()` before
1462  *   it reaches the lowest possible power state, i.e. manage power
1463  *   asynchronously.  While the interface is "UP", the driver SHOULD
1464  *   autonomously and asynchronously transition to lower power states whenever
1465  *   possible. If the driver claims to support timed RX/TX capabilities and the
1466  *   upper layers configure an RX slot, then the driver SHALL immediately
1467  *   transition (asynchronously) to the lowest possible power state until the
1468  *   start of the RX slot or until a scheduled packet needs to be transmitted.
1469  * * The driver SHALL NOT change the interface's "UP"/"DOWN" state on its own.
1470  *   Initially, the interface SHALL be in the "DOWN" state.
1471  * * Drivers that implement the optional `continuous_carrier()` operation will
1472  *   be considered to be in the RFC 2863 "testing" ifOperStatus state if that
1473  *   operation returns zero. This state is active until either `start()` or
1474  *   `stop()` is called. If `continuous_carrier()` returns a non-zero value then
1475  *   the previous state is assumed by upper layers.
1476  * * If calls to `start()`/`stop()` return any other value than zero or
1477  *   `-EALREADY`, upper layers will consider the interface to be in a
1478  *   "lowerLayerDown" state as defined in RFC 2863.
1479  * * The RFC 2863 "dormant", "unknown" and "notPresent" ifOperStatus states are
1480  *   currently not supported. The "lowerLevelUp" state.
1481  * * The `ed_scan()`, `cca()` and `tx()` operations SHALL only be supported in
1482  *   the "UP" state and return `-ENETDOWN` in any other state. See the
1483  *   function-level API documentation below for further details.
1484  *
1485  * @note In case of devices that support timed RX/TX, the "UP" state is not
1486  * equal to "receiver enabled". If a receive window (i.e. RX slot, see @ref
1487  * IEEE802154_CONFIG_RX_SLOT) is configured before calling `start()` then the
1488  * receiver will not be enabled when transitioning to the "UP" state.
1489  * Configuring a receive window while the interface is "UP" will cause the
1490  * receiver to be disabled immediately until the configured reception time has
1491  * arrived.
1492  */
1493 struct ieee802154_radio_api {
1494 	/**
1495 	 * @brief network interface API
1496 	 *
1497 	 * @note Network devices must extend the network interface API. It is
1498 	 * therefore mandatory to place it at the top of the driver API struct so
1499 	 * that it can be cast to a network interface.
1500 	 */
1501 	struct net_if_api iface_api;
1502 
1503 	/**
1504 	 * @brief Get the device driver capabilities.
1505 	 *
1506 	 * @note Implementations SHALL be **isr-ok** and MUST NOT **sleep**. MAY
1507 	 * be called in any interface state once the driver is fully initialized
1508 	 * ("ready").
1509 	 *
1510 	 * @param dev pointer to IEEE 802.15.4 driver device
1511 	 *
1512 	 * @return Bit field with all supported device driver capabilities.
1513 	 */
1514 	enum ieee802154_hw_caps (*get_capabilities)(const struct device *dev);
1515 
1516 	/**
1517 	 * @brief Clear Channel Assessment - Check channel's activity
1518 	 *
1519 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. SHALL
1520 	 * return -ENETDOWN unless the interface is "UP".
1521 	 *
1522 	 * @param dev pointer to IEEE 802.15.4 driver device
1523 	 *
1524 	 * @retval 0 the channel is available
1525 	 * @retval -EBUSY The channel is busy.
1526 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1527 	 * temporarily cannot be executed without blocking.
1528 	 * @retval -ENETDOWN The interface is not "UP".
1529 	 * @retval -ENOTSUP CCA is not supported by this driver.
1530 	 * @retval -EIO The CCA procedure could not be executed.
1531 	 */
1532 	int (*cca)(const struct device *dev);
1533 
1534 	/**
1535 	 * @brief Set current channel
1536 	 *
1537 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. SHALL
1538 	 * return -EIO unless the interface is either "UP" or "DOWN".
1539 	 *
1540 	 * @param dev pointer to IEEE 802.15.4 driver device
1541 	 * @param channel the number of the channel to be set in CPU byte order
1542 	 *
1543 	 * @retval 0 channel was successfully set
1544 	 * @retval -EALREADY The previous channel is the same as the requested
1545 	 * channel.
1546 	 * @retval -EINVAL The given channel is not within the range of valid
1547 	 * channels of the driver's current channel page, see the
1548 	 * IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES driver attribute.
1549 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1550 	 * temporarily cannot be executed without blocking.
1551 	 * @retval -ENOTSUP The given channel is within the range of valid
1552 	 * channels of the driver's current channel page but unsupported by the
1553 	 * current driver.
1554 	 * @retval -EIO The channel could not be set.
1555 	 */
1556 	int (*set_channel)(const struct device *dev, uint16_t channel);
1557 
1558 	/**
1559 	 * @brief Set/Unset PAN ID, extended or short address filters.
1560 	 *
1561 	 * @note requires IEEE802154_HW_FILTER capability.
1562 	 *
1563 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. SHALL
1564 	 * return -EIO unless the interface is either "UP" or "DOWN".
1565 	 *
1566 	 * @param dev pointer to IEEE 802.15.4 driver device
1567 	 * @param set true to set the filter, false to remove it
1568 	 * @param type the type of entity to be added/removed from the filter
1569 	 * list (a PAN ID or a source/destination address)
1570 	 * @param filter the entity to be added/removed from the filter list
1571 	 *
1572 	 * @retval 0 The filter was successfully added/removed.
1573 	 * @retval -EINVAL The given filter entity or filter entity type
1574 	 * was not valid.
1575 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1576 	 * temporarily cannot be executed without blocking.
1577 	 * @retval -ENOTSUP Setting/removing this filter or filter type
1578 	 * is not supported by this driver.
1579 	 * @retval -EIO Error while setting/removing the filter.
1580 	 */
1581 	int (*filter)(const struct device *dev,
1582 		      bool set,
1583 		      enum ieee802154_filter_type type,
1584 		      const struct ieee802154_filter *filter);
1585 
1586 	/**
1587 	 * @brief Set TX power level in dbm
1588 	 *
1589 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. SHALL
1590 	 * return -EIO unless the interface is either "UP" or "DOWN".
1591 	 *
1592 	 * @param dev pointer to IEEE 802.15.4 driver device
1593 	 * @param dbm TX power in dbm
1594 	 *
1595 	 * @retval 0 The TX power was successfully set.
1596 	 * @retval -EINVAL The given dbm value is invalid or not supported by
1597 	 * the driver.
1598 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1599 	 * temporarily cannot be executed without blocking.
1600 	 * @retval -EIO The TX power could not be set.
1601 	 */
1602 	int (*set_txpower)(const struct device *dev, int16_t dbm);
1603 
1604 	/**
1605 	 * @brief Transmit a packet fragment as a single frame
1606 	 *
1607 	 * @details Depending on the level of offloading features supported by
1608 	 * the driver, the frame MAY not be fully encrypted/authenticated or it
1609 	 * MAY not contain an FCS. It is the responsibility of L2
1610 	 * implementations to prepare the frame according to the offloading
1611 	 * capabilities announced by the driver and to decide whether CCA,
1612 	 * CSMA/CA, ACK or retransmission procedures need to be executed outside
1613 	 * ("soft MAC") or inside ("hard MAC") the driver .
1614 	 *
1615 	 * All frames originating from L2 SHALL have all required IEs
1616 	 * pre-allocated and pre-filled such that the driver does not have to
1617 	 * parse and manipulate IEs at all. This includes ACK packets if the
1618 	 * driver does not have the @ref IEEE802154_HW_RX_TX_ACK capability.
1619 	 * Also see @ref IEEE802154_CONFIG_ENH_ACK_HEADER_IE for drivers that
1620 	 * have the @ref IEEE802154_HW_RX_TX_ACK capability.
1621 	 *
1622 	 * IEs that cannot be prepared by L2 unless the TX time is known (e.g.
1623 	 * CSL IE, Rendezvous Time IE, Time Correction IE, ...) SHALL be sent in
1624 	 * any of the timed TX modes with appropriate timing information
1625 	 * pre-filled in the IE such that drivers do not have to parse and
1626 	 * manipulate IEs at all unless the frame is generated by the driver
1627 	 * itself.
1628 	 *
1629 	 * In case any of the timed TX modes is supported and used (see @ref
1630 	 * ieee802154_hw_caps and @ref ieee802154_tx_mode), the driver SHALL
1631 	 * take responsibility of scheduling and sending the packet at the
1632 	 * precise programmed time autonomously without further interaction by
1633 	 * upper layers. The call to `tx()` will block until the package has
1634 	 * either been sent successfully (possibly including channel acquisition
1635 	 * and packet acknowledgment) or a terminal transmission error occurred.
1636 	 * The driver SHALL sleep and keep power consumption to the lowest
1637 	 * possible level until the scheduled transmission time arrives or
1638 	 * during any other idle waiting time.
1639 	 *
1640 	 * @warning The driver SHALL NOT take ownership of the given network
1641 	 * packet and frame (fragment) buffer. Any data required by the driver
1642 	 * including the actual frame content must be read synchronously and
1643 	 * copied internally if needed at a later time (e.g. the contents of IEs
1644 	 * required for protocol configuration, states of frame counters,
1645 	 * sequence numbers, etc). Both, the packet and the buffer MAY be
1646 	 * re-used or released by upper layers immediately after the function
1647 	 * returns.
1648 	 *
1649 	 * @note Implementations MAY **sleep** and will usually NOT be
1650 	 * **isr-ok** - especially when timed TX, CSMA/CA, retransmissions,
1651 	 * auto-ACK or any other offloading feature is supported that implies
1652 	 * considerable idle waiting time. SHALL return `-ENETDOWN` unless the
1653 	 * interface is "UP".
1654 	 *
1655 	 * @param dev pointer to IEEE 802.15.4 driver device
1656 	 * @param mode the transmission mode, some of which require specific
1657 	 * offloading capabilities.
1658 	 * @param pkt pointer to the network packet to be transmitted.
1659 	 * @param frag pointer to a network buffer containing a single fragment
1660 	 * with the frame data to be transmitted
1661 	 *
1662 	 * @retval 0 The frame was successfully sent or scheduled. If the driver
1663 	 * supports ACK offloading and the frame requested acknowlegment (AR bit
1664 	 * set), this means that the packet was successfully acknowledged by its
1665 	 * peer.
1666 	 * @retval -EINVAL Invalid packet (e.g. an expected IE is missing or the
1667 	 * encryption/authentication state is not as expected).
1668 	 * @retval -EBUSY The frame could not be sent because the medium was
1669 	 * busy (CSMA/CA or CCA offloading feature only).
1670 	 * @retval -ENOMSG The frame was not confirmed by an ACK packet (TX ACK
1671 	 * offloading feature only).
1672 	 * @retval -ENOBUFS The frame could not be scheduled due to missing
1673 	 * internal resources (timed TX offloading feature only).
1674 	 * @retval -ENETDOWN The interface is not "UP".
1675 	 * @retval -ENOTSUP The given TX mode is not supported.
1676 	 * @retval -EIO The frame could not be sent due to some unspecified
1677 	 * driver error (e.g. the driver being busy).
1678 	 */
1679 	int (*tx)(const struct device *dev, enum ieee802154_tx_mode mode,
1680 		  struct net_pkt *pkt, struct net_buf *frag);
1681 
1682 	/**
1683 	 * @brief Start the device.
1684 	 *
1685 	 * @details Upper layers will assume the interface is "UP" if this
1686 	 * operation returns with zero or `-EALREADY`. The interface is placed
1687 	 * in receive mode before returning from this operation unless an RX
1688 	 * slot has been configured (even if it lies in the past, see @ref
1689 	 * IEEE802154_CONFIG_RX_SLOT).
1690 	 *
1691 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. MAY be
1692 	 * called in any interface state once the driver is fully initialized
1693 	 * ("ready").
1694 	 *
1695 	 * @param dev pointer to IEEE 802.15.4 driver device
1696 	 *
1697 	 * @retval 0 The driver was successfully started.
1698 	 * @retval -EALREADY The driver was already "UP".
1699 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1700 	 * temporarily cannot be executed without blocking.
1701 	 * @retval -EIO The driver could not be started.
1702 	 */
1703 	int (*start)(const struct device *dev);
1704 
1705 	/**
1706 	 * @brief Stop the device.
1707 	 *
1708 	 * @details Upper layers will assume the interface is "DOWN" if this
1709 	 * operation returns with zero or `-EALREADY`. The driver switches off
1710 	 * the receiver before returning if it was previously on. The driver
1711 	 * enters the lowest possible power mode after this operation is called.
1712 	 * This MAY happen asynchronously (i.e. after the operation already
1713 	 * returned control).
1714 	 *
1715 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. MAY be
1716 	 * called in any interface state once the driver is fully initialized
1717 	 * ("ready").
1718 	 *
1719 	 * @param dev pointer to IEEE 802.15.4 driver device
1720 	 *
1721 	 * @retval 0 The driver was successfully stopped.
1722 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1723 	 * temporarily cannot be executed without blocking.
1724 	 * @retval -EALREADY The driver was already "DOWN".
1725 	 * @retval -EIO The driver could not be stopped.
1726 	 */
1727 	int (*stop)(const struct device *dev);
1728 
1729 	/**
1730 	 * @brief Start continuous carrier wave transmission.
1731 	 *
1732 	 * @details The method blocks until the interface has started to emit a
1733 	 * continuous carrier. To leave this mode, `start()` or `stop()` should
1734 	 * be called, which will put the driver back into the "UP" or "DOWN"
1735 	 * states, respectively.
1736 	 *
1737 	 * @note Implementations MAY **sleep** and will usually NOT be
1738 	 * **isr-ok**. MAY be called in any interface state once the driver is
1739 	 * fully initialized ("ready").
1740 	 *
1741 	 * @param dev pointer to IEEE 802.15.4 driver device
1742 	 *
1743 	 * @retval 0 continuous carrier wave transmission started
1744 	 * @retval -EALREADY The driver was already in "TESTING" state and
1745 	 * emitting a continuous carrier.
1746 	 * @retval -EIO not started
1747 	 */
1748 	int (*continuous_carrier)(const struct device *dev);
1749 
1750 	/**
1751 	 * @brief Set or update driver configuration.
1752 	 *
1753 	 * @details The method blocks until the interface has been reconfigured
1754 	 * atomically with respect to ongoing package reception, transmission or
1755 	 * any other ongoing driver operation.
1756 	 *
1757 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. MAY be
1758 	 * called in any interface state once the driver is fully initialized
1759 	 * ("ready"). Some configuration options may not be supported in all
1760 	 * interface operational states, see the detailed specifications in @ref
1761 	 * ieee802154_config_type. In this case the operation returns `-EACCES`.
1762 	 *
1763 	 * @param dev pointer to IEEE 802.15.4 driver device
1764 	 * @param type the configuration type to be set
1765 	 * @param config the configuration parameters to be set for the given
1766 	 * configuration type
1767 	 *
1768 	 * @retval 0 configuration successful
1769 	 * @retval -EINVAL The configuration parameters are invalid for the
1770 	 * given configuration type.
1771 	 * @retval -ENOTSUP The given configuration type is not supported by
1772 	 * this driver.
1773 	 * @retval -EACCES The given configuration type is supported by this
1774 	 * driver but cannot be configured in the current interface operational
1775 	 * state.
1776 	 * @retval -ENOMEM The configuration cannot be saved due to missing
1777 	 * memory resources.
1778 	 * @retval -ENOENT The resource referenced in the configuration
1779 	 * parameters cannot be found in the configuration.
1780 	 * @retval -EWOULDBLOCK The operation is called from ISR context but
1781 	 * temporarily cannot be executed without blocking.
1782 	 * @retval -EIO An internal error occurred while trying to configure the
1783 	 * given configuration parameter.
1784 	 */
1785 	int (*configure)(const struct device *dev,
1786 			 enum ieee802154_config_type type,
1787 			 const struct ieee802154_config *config);
1788 
1789 	/**
1790 	 * @brief Run an energy detection scan.
1791 	 *
1792 	 * @note requires IEEE802154_HW_ENERGY_SCAN capability
1793 	 *
1794 	 * @note The radio channel must be set prior to calling this function.
1795 	 *
1796 	 * @note Implementations SHALL be **isr-ok** and MAY **sleep**. SHALL
1797 	 * return `-ENETDOWN` unless the interface is "UP".
1798 	 *
1799 	 * @param dev pointer to IEEE 802.15.4 driver device
1800 	 * @param duration duration of energy scan in ms
1801 	 * @param done_cb function called when the energy scan has finished
1802 	 *
1803 	 * @retval 0 the energy detection scan was successfully scheduled
1804 	 *
1805 	 * @retval -EBUSY the energy detection scan could not be scheduled at
1806 	 * this time
1807 	 * @retval -EALREADY a previous energy detection scan has not finished
1808 	 * yet.
1809 	 * @retval -ENETDOWN The interface is not "UP".
1810 	 * @retval -ENOTSUP This driver does not support energy scans.
1811 	 * @retval -EIO The energy detection procedure could not be executed.
1812 	 */
1813 	int (*ed_scan)(const struct device *dev,
1814 		       uint16_t duration,
1815 		       energy_scan_done_cb_t done_cb);
1816 
1817 	/**
1818 	 * @brief Get the current time in nanoseconds relative to the network
1819 	 * subsystem's local uptime clock as represented by this network
1820 	 * interface.
1821 	 *
1822 	 * See @ref net_time_t for semantic details.
1823 	 *
1824 	 * @note requires IEEE802154_HW_TXTIME and/or IEEE802154_HW_RXTIME
1825 	 * capabilities. Implementations SHALL be **isr-ok** and MUST NOT
1826 	 * **sleep**. MAY be called in any interface state once the driver is
1827 	 * fully initialized ("ready").
1828 	 *
1829 	 * @param dev pointer to IEEE 802.15.4 driver device
1830 	 *
1831 	 * @return nanoseconds relative to the network subsystem's local clock,
1832 	 * -1 if an error occurred or the operation is not supported
1833 	 */
1834 	net_time_t (*get_time)(const struct device *dev);
1835 
1836 	/**
1837 	 * @brief Get the current estimated worst case accuracy (maximum ±
1838 	 * deviation from the nominal frequency) of the network subsystem's
1839 	 * local clock used to calculate tolerances and guard times when
1840 	 * scheduling delayed receive or transmit radio operations.
1841 	 *
1842 	 * The deviation is given in units of PPM (parts per million).
1843 	 *
1844 	 * @note requires IEEE802154_HW_TXTIME and/or IEEE802154_HW_RXTIME
1845 	 * capabilities.
1846 	 *
1847 	 * @note Implementations may estimate this value based on current
1848 	 * operating conditions (e.g. temperature). Implementations SHALL be
1849 	 * **isr-ok** and MUST NOT **sleep**. MAY be called in any interface
1850 	 * state once the driver is fully initialized ("ready").
1851 	 *
1852 	 * @param dev pointer to IEEE 802.15.4 driver device
1853 	 *
1854 	 * @return current estimated clock accuracy in PPM
1855 	 */
1856 	uint8_t (*get_sch_acc)(const struct device *dev);
1857 
1858 	/**
1859 	 * @brief Get the value of a driver specific attribute.
1860 	 *
1861 	 * @note This function SHALL NOT return any values configurable by the
1862 	 * MAC (L2) layer. It is reserved to non-boolean (i.e. scalar or
1863 	 * structured) attributes that originate from the driver implementation
1864 	 * and cannot be directly or indirectly derived by L2. Boolean
1865 	 * attributes SHALL be implemented as @ref ieee802154_hw_caps.
1866 	 *
1867 	 * @note Implementations SHALL be **isr-ok** and MUST NOT **sleep**. MAY
1868 	 * be called in any interface state once the driver is fully initialized
1869 	 * ("ready").
1870 	 *
1871 	 * @retval 0 The requested attribute is supported by the driver and the
1872 	 * value can be retrieved from the corresponding @ref ieee802154_attr_value
1873 	 * member.
1874 	 *
1875 	 * @retval -ENOENT The driver does not provide the requested attribute.
1876 	 * The value structure has not been updated with attribute data. The
1877 	 * content of the value attribute is undefined.
1878 	 */
1879 	int (*attr_get)(const struct device *dev,
1880 			enum ieee802154_attr attr,
1881 			struct ieee802154_attr_value *value);
1882 };
1883 
1884 /* Make sure that the network interface API is properly setup inside
1885  * IEEE 802.15.4 driver API struct (it is the first one).
1886  */
1887 BUILD_ASSERT(offsetof(struct ieee802154_radio_api, iface_api) == 0);
1888 
1889 /** @} */
1890 
1891 /**
1892  * @name IEEE 802.15.4 driver utils
1893  * @{
1894  */
1895 
1896 /** @cond INTERNAL_HIDDEN */
1897 #define IEEE802154_AR_FLAG_SET (0x20)
1898 /** INTERNAL_HIDDEN @endcond */
1899 
1900 /**
1901  * @brief Check if the AR flag is set on the frame inside the given @ref
1902  * net_pkt.
1903  *
1904  * @param frag A valid pointer on a net_buf structure, must not be NULL,
1905  *        and its length should be at least 1 byte (ImmAck frames are the
1906  *        shortest supported frames with 3 bytes excluding FCS).
1907  *
1908  * @return true if AR flag is set, false otherwise
1909  */
ieee802154_is_ar_flag_set(struct net_buf * frag)1910 static inline bool ieee802154_is_ar_flag_set(struct net_buf *frag)
1911 {
1912 	return (*frag->data & IEEE802154_AR_FLAG_SET);
1913 }
1914 
1915 /** @} */
1916 
1917 /**
1918  * @name IEEE 802.15.4 driver callbacks
1919  * @{
1920  */
1921 
1922 /* TODO: Fix drivers to either unref the packet before they return NET_OK or to
1923  * return NET_CONTINUE instead. See note below.
1924  */
1925 /**
1926  * @brief IEEE 802.15.4 driver ACK handling callback into L2 that drivers must
1927  *        call when receiving an ACK package.
1928  *
1929  * @details The IEEE 802.15.4 standard prescribes generic procedures for ACK
1930  *          handling on L2 (MAC) level. L2 stacks therefore have to provides a
1931  *          fast and re-usable generic implementation of this callback for
1932  *          drivers to call when receiving an ACK packet.
1933  *
1934  *          Note: This function is part of Zephyr's 802.15.4 stack driver -> L2
1935  *          "inversion-of-control" adaptation API and must be implemented by all
1936  *          IEEE 802.15.4 L2 stacks.
1937  *
1938  * @param iface A valid pointer on a network interface that received the packet
1939  * @param pkt A valid pointer on a packet to check
1940  *
1941  * @return NET_OK if L2 handles the ACK package, NET_CONTINUE or NET_DROP otherwise.
1942  *
1943  * @warning Deviating from other functions in the net stack returning
1944  * net_verdict, this function will not unref the package even if it returns
1945  * NET_OK.
1946  */
1947 extern enum net_verdict ieee802154_handle_ack(struct net_if *iface, struct net_pkt *pkt);
1948 
1949 /**
1950  * @brief IEEE 802.15.4 driver initialization callback into L2 called by drivers
1951  *        to initialize the active L2 stack for a given interface.
1952  *
1953  * @details Drivers must call this function as part of their own initialization
1954  *          routine.
1955  *
1956  *          Note: This function is part of Zephyr's 802.15.4 stack driver -> L2
1957  *          "inversion-of-control" adaptation API and must be implemented by all
1958  *          IEEE 802.15.4 L2 stacks.
1959  *
1960  * @param iface A valid pointer on a network interface
1961  */
1962 #ifndef CONFIG_IEEE802154_RAW_MODE
1963 extern void ieee802154_init(struct net_if *iface);
1964 #else
1965 #define ieee802154_init(_iface_)
1966 #endif /* CONFIG_IEEE802154_RAW_MODE */
1967 
1968 /** @} */
1969 
1970 #ifdef __cplusplus
1971 }
1972 #endif
1973 
1974 /**
1975  * @}
1976  */
1977 
1978 #endif /* ZEPHYR_INCLUDE_NET_IEEE802154_RADIO_H_ */
1979