Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ieee802154_pkt.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation.
3 * Copyright (c) 2022 Florian Grandel.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
15#ifndef ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_
16#define ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_
17
18#include <string.h>
19
20#include <zephyr/types.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
28#ifndef NET_PKT_HAS_CONTROL_BLOCK
29#define NET_PKT_HAS_CONTROL_BLOCK
30#endif
31
32/* See section 6.16.2.8 - Received Signal Strength Indicator (RSSI) */
33#define IEEE802154_MAC_RSSI_MIN 0U /* corresponds to -174 dBm */
34#define IEEE802154_MAC_RSSI_MAX 254U /* corresponds to 80 dBm */
35#define IEEE802154_MAC_RSSI_UNDEFINED 255U /* used by us to indicate an undefined RSSI value */
36
37#define IEEE802154_MAC_RSSI_DBM_MIN -174 /* in dBm */
38#define IEEE802154_MAC_RSSI_DBM_MAX 80 /* in dBm */
39#define IEEE802154_MAC_RSSI_DBM_UNDEFINED INT16_MIN /* represents an undefined RSSI value */
40
41struct net_pkt_cb_ieee802154 {
42#if defined(CONFIG_NET_L2_OPENTHREAD)
43 uint32_t ack_fc; /* Frame counter set in the ACK */
44 uint8_t ack_keyid; /* Key index set in the ACK */
45#endif
46 union {
47 /* RX packets */
48 struct {
49 uint8_t lqi; /* Link Quality Indicator */
50 /* See section 6.16.2.8 - Received Signal Strength Indicator (RSSI)
51 * "RSSI is represented as one octet of integer [...]; therefore,
52 * the minimum and maximum values are 0 (–174 dBm) and 254 (80 dBm),
53 * respectively. 255 is reserved." (MAC PIB attribute macRssi, see
54 * section 8.4.3.10, table 8-108)
55 *
56 * TX packets will show zero for this value. Drivers may set the
57 * field to the reserved value 255 (0xff) to indicate that an RSSI
58 * value is not available for this packet.
59 */
60 uint8_t rssi;
61 };
62 };
63
64 /* Flags */
65 uint8_t ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
66 uint8_t frame_secured : 1; /* Frame is authenticated and
67 * encrypted according to its
68 * Auxiliary Security Header
69 */
70 uint8_t mac_hdr_rdy : 1; /* Indicates if frame's MAC header
71 * is ready to be transmitted or if
72 * it requires further modifications,
73 * e.g. Frame Counter injection.
74 */
75#if defined(CONFIG_NET_L2_OPENTHREAD)
76 uint8_t ack_seb : 1; /* Security Enabled Bit was set in the ACK */
77#endif
78};
79
80struct net_pkt;
81static inline void *net_pkt_cb(struct net_pkt *pkt);
82
83static inline struct net_pkt_cb_ieee802154 *net_pkt_cb_ieee802154(struct net_pkt *pkt)
84{
85 return (struct net_pkt_cb_ieee802154 *)net_pkt_cb(pkt);
86};
87
88static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
89{
90 return net_pkt_cb_ieee802154(pkt)->lqi;
91}
92
93static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt, uint8_t lqi)
94{
95 net_pkt_cb_ieee802154(pkt)->lqi = lqi;
96}
97
110static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
111{
112 return net_pkt_cb_ieee802154(pkt)->rssi;
113}
114
127static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt, uint8_t rssi)
128{
129 net_pkt_cb_ieee802154(pkt)->rssi = rssi;
130}
131
143static inline int16_t net_pkt_ieee802154_rssi_dbm(struct net_pkt *pkt)
144{
145 int16_t rssi = net_pkt_cb_ieee802154(pkt)->rssi;
146 return rssi == IEEE802154_MAC_RSSI_UNDEFINED ? IEEE802154_MAC_RSSI_DBM_UNDEFINED
147 : rssi + IEEE802154_MAC_RSSI_DBM_MIN;
148}
149
161static inline void net_pkt_set_ieee802154_rssi_dbm(struct net_pkt *pkt, int16_t rssi)
162{
163 if (likely(rssi >= IEEE802154_MAC_RSSI_DBM_MIN && rssi <= IEEE802154_MAC_RSSI_DBM_MAX)) {
164 int16_t unsigned_rssi = rssi - IEEE802154_MAC_RSSI_DBM_MIN;
165
166 net_pkt_cb_ieee802154(pkt)->rssi = unsigned_rssi;
167 return;
168 } else if (rssi == IEEE802154_MAC_RSSI_DBM_UNDEFINED) {
169 net_pkt_cb_ieee802154(pkt)->rssi = IEEE802154_MAC_RSSI_UNDEFINED;
170 return;
171 } else if (rssi < IEEE802154_MAC_RSSI_DBM_MIN) {
172 net_pkt_cb_ieee802154(pkt)->rssi = IEEE802154_MAC_RSSI_MIN;
173 return;
174 } else if (rssi > IEEE802154_MAC_RSSI_DBM_MAX) {
175 net_pkt_cb_ieee802154(pkt)->rssi = IEEE802154_MAC_RSSI_MAX;
176 return;
177 }
178
179 CODE_UNREACHABLE;
180}
181
182static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
183{
184 return net_pkt_cb_ieee802154(pkt)->ack_fpb;
185}
186
187static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt, bool fpb)
188{
189 net_pkt_cb_ieee802154(pkt)->ack_fpb = fpb;
190}
191
192static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
193{
194 return net_pkt_cb_ieee802154(pkt)->frame_secured;
195}
196
197static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt, bool secured)
198{
199 net_pkt_cb_ieee802154(pkt)->frame_secured = secured;
200}
201
202static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
203{
204 return net_pkt_cb_ieee802154(pkt)->mac_hdr_rdy;
205}
206
207static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt, bool rdy)
208{
209 net_pkt_cb_ieee802154(pkt)->mac_hdr_rdy = rdy;
210}
211
212#if defined(CONFIG_NET_L2_OPENTHREAD)
213static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
214{
215 return net_pkt_cb_ieee802154(pkt)->ack_fc;
216}
217
218static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt, uint32_t fc)
219{
220 net_pkt_cb_ieee802154(pkt)->ack_fc = fc;
221}
222
223static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
224{
225 return net_pkt_cb_ieee802154(pkt)->ack_keyid;
226}
227
228static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt, uint8_t keyid)
229{
230 net_pkt_cb_ieee802154(pkt)->ack_keyid = keyid;
231}
232
233static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
234{
235 return net_pkt_cb_ieee802154(pkt)->ack_seb;
236}
237
238static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
239{
240 net_pkt_cb_ieee802154(pkt)->ack_seb = seb;
241}
242#endif /* CONFIG_NET_L2_OPENTHREAD */
243
246#ifdef __cplusplus
247}
248#endif
249
250#endif /* ZEPHYR_INCLUDE_NET_IEEE802154_PKT_H_ */
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__INT16_TYPE__ int16_t
Definition stdint.h:73
Network packet.
Definition net_pkt.h:67