xref: /aosp_15_r20/external/openthread/examples/platforms/utils/mac_frame.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1 /*
2  *  Copyright (c) 2019, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "mac_frame.h"
30 
31 #include <assert.h>
32 #include "mac/mac_frame.hpp"
33 
34 using namespace ot;
35 
otMacFrameDoesAddrMatch(const otRadioFrame * aFrame,otPanId aPanId,otShortAddress aShortAddress,const otExtAddress * aExtAddress)36 bool otMacFrameDoesAddrMatch(const otRadioFrame *aFrame,
37                              otPanId             aPanId,
38                              otShortAddress      aShortAddress,
39                              const otExtAddress *aExtAddress)
40 {
41     const Mac::Frame &frame = *static_cast<const Mac::Frame *>(aFrame);
42     bool              rval  = true;
43     Mac::Address      dst;
44     Mac::PanId        panid;
45 
46     VerifyOrExit(frame.GetDstAddr(dst) == kErrorNone, rval = false);
47 
48     switch (dst.GetType())
49     {
50     case Mac::Address::kTypeShort:
51         VerifyOrExit(dst.GetShort() == Mac::kShortAddrBroadcast || dst.GetShort() == aShortAddress, rval = false);
52         break;
53 
54     case Mac::Address::kTypeExtended:
55         VerifyOrExit(dst.GetExtended() == *static_cast<const Mac::ExtAddress *>(aExtAddress), rval = false);
56         break;
57 
58     case Mac::Address::kTypeNone:
59         break;
60     }
61 
62     SuccessOrExit(frame.GetDstPanId(panid));
63     VerifyOrExit(panid == Mac::kPanIdBroadcast || panid == aPanId, rval = false);
64 
65 exit:
66     return rval;
67 }
68 
otMacFrameIsAck(const otRadioFrame * aFrame)69 bool otMacFrameIsAck(const otRadioFrame *aFrame)
70 {
71     return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kTypeAck;
72 }
73 
otMacFrameIsData(const otRadioFrame * aFrame)74 bool otMacFrameIsData(const otRadioFrame *aFrame)
75 {
76     return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kTypeData;
77 }
78 
otMacFrameIsCommand(const otRadioFrame * aFrame)79 bool otMacFrameIsCommand(const otRadioFrame *aFrame)
80 {
81     return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kTypeMacCmd;
82 }
83 
otMacFrameIsDataRequest(const otRadioFrame * aFrame)84 bool otMacFrameIsDataRequest(const otRadioFrame *aFrame)
85 {
86     return static_cast<const Mac::Frame *>(aFrame)->IsDataRequestCommand();
87 }
88 
otMacFrameIsAckRequested(const otRadioFrame * aFrame)89 bool otMacFrameIsAckRequested(const otRadioFrame *aFrame)
90 {
91     return static_cast<const Mac::Frame *>(aFrame)->GetAckRequest();
92 }
93 
GetOtMacAddress(const Mac::Address & aInAddress,otMacAddress * aOutAddress)94 static void GetOtMacAddress(const Mac::Address &aInAddress, otMacAddress *aOutAddress)
95 {
96     switch (aInAddress.GetType())
97     {
98     case Mac::Address::kTypeNone:
99         aOutAddress->mType = OT_MAC_ADDRESS_TYPE_NONE;
100         break;
101 
102     case Mac::Address::kTypeShort:
103         aOutAddress->mType                  = OT_MAC_ADDRESS_TYPE_SHORT;
104         aOutAddress->mAddress.mShortAddress = aInAddress.GetShort();
105         break;
106 
107     case Mac::Address::kTypeExtended:
108         aOutAddress->mType                = OT_MAC_ADDRESS_TYPE_EXTENDED;
109         aOutAddress->mAddress.mExtAddress = aInAddress.GetExtended();
110         break;
111     }
112 }
113 
otMacFrameGetSrcAddr(const otRadioFrame * aFrame,otMacAddress * aMacAddress)114 otError otMacFrameGetSrcAddr(const otRadioFrame *aFrame, otMacAddress *aMacAddress)
115 {
116     otError      error;
117     Mac::Address address;
118 
119     error = static_cast<const Mac::Frame *>(aFrame)->GetSrcAddr(address);
120     SuccessOrExit(error);
121 
122     GetOtMacAddress(address, aMacAddress);
123 
124 exit:
125     return error;
126 }
127 
otMacFrameGetDstAddr(const otRadioFrame * aFrame,otMacAddress * aMacAddress)128 otError otMacFrameGetDstAddr(const otRadioFrame *aFrame, otMacAddress *aMacAddress)
129 {
130     otError      error;
131     Mac::Address address;
132 
133     error = static_cast<const Mac::Frame *>(aFrame)->GetDstAddr(address);
134     SuccessOrExit(error);
135 
136     GetOtMacAddress(address, aMacAddress);
137 
138 exit:
139     return error;
140 }
141 
otMacFrameGetSequence(const otRadioFrame * aFrame,uint8_t * aSequence)142 otError otMacFrameGetSequence(const otRadioFrame *aFrame, uint8_t *aSequence)
143 {
144     otError error;
145 
146     if (static_cast<const Mac::Frame *>(aFrame)->IsSequencePresent())
147     {
148         *aSequence = static_cast<const Mac::Frame *>(aFrame)->GetSequence();
149         error      = kErrorNone;
150     }
151     else
152     {
153         error = kErrorParse;
154     }
155 
156     return error;
157 }
158 
otMacFrameProcessTransmitAesCcm(otRadioFrame * aFrame,const otExtAddress * aExtAddress)159 void otMacFrameProcessTransmitAesCcm(otRadioFrame *aFrame, const otExtAddress *aExtAddress)
160 {
161     static_cast<Mac::TxFrame *>(aFrame)->ProcessTransmitAesCcm(*static_cast<const Mac::ExtAddress *>(aExtAddress));
162 }
163 
otMacFrameIsVersion2015(const otRadioFrame * aFrame)164 bool otMacFrameIsVersion2015(const otRadioFrame *aFrame)
165 {
166     return static_cast<const Mac::Frame *>(aFrame)->IsVersion2015();
167 }
168 
otMacFrameGenerateImmAck(const otRadioFrame * aFrame,bool aIsFramePending,otRadioFrame * aAckFrame)169 void otMacFrameGenerateImmAck(const otRadioFrame *aFrame, bool aIsFramePending, otRadioFrame *aAckFrame)
170 {
171     assert(aFrame != nullptr && aAckFrame != nullptr);
172 
173     static_cast<Mac::TxFrame *>(aAckFrame)->GenerateImmAck(*static_cast<const Mac::RxFrame *>(aFrame), aIsFramePending);
174 }
175 
176 #if OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2
otMacFrameGenerateEnhAck(const otRadioFrame * aFrame,bool aIsFramePending,const uint8_t * aIeData,uint8_t aIeLength,otRadioFrame * aAckFrame)177 otError otMacFrameGenerateEnhAck(const otRadioFrame *aFrame,
178                                  bool                aIsFramePending,
179                                  const uint8_t      *aIeData,
180                                  uint8_t             aIeLength,
181                                  otRadioFrame       *aAckFrame)
182 {
183     assert(aFrame != nullptr && aAckFrame != nullptr);
184 
185     return static_cast<Mac::TxFrame *>(aAckFrame)->GenerateEnhAck(*static_cast<const Mac::RxFrame *>(aFrame),
186                                                                   aIsFramePending, aIeData, aIeLength);
187 }
188 #endif
189 
190 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
otMacFrameSetCslIe(otRadioFrame * aFrame,uint16_t aCslPeriod,uint16_t aCslPhase)191 void otMacFrameSetCslIe(otRadioFrame *aFrame, uint16_t aCslPeriod, uint16_t aCslPhase)
192 {
193     static_cast<Mac::Frame *>(aFrame)->SetCslIe(aCslPeriod, aCslPhase);
194 }
195 #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
196 
otMacFrameIsSecurityEnabled(otRadioFrame * aFrame)197 bool otMacFrameIsSecurityEnabled(otRadioFrame *aFrame)
198 {
199     return static_cast<const Mac::Frame *>(aFrame)->GetSecurityEnabled();
200 }
201 
otMacFrameIsKeyIdMode1(otRadioFrame * aFrame)202 bool otMacFrameIsKeyIdMode1(otRadioFrame *aFrame)
203 {
204     uint8_t keyIdMode;
205     otError error;
206 
207     error = static_cast<const Mac::Frame *>(aFrame)->GetKeyIdMode(keyIdMode);
208 
209     return (error == OT_ERROR_NONE) ? (keyIdMode == Mac::Frame::kKeyIdMode1) : false;
210 }
211 
otMacFrameGetKeyId(otRadioFrame * aFrame)212 uint8_t otMacFrameGetKeyId(otRadioFrame *aFrame)
213 {
214     uint8_t keyId = 0;
215 
216     IgnoreError(static_cast<const Mac::Frame *>(aFrame)->GetKeyId(keyId));
217 
218     return keyId;
219 }
220 
otMacFrameSetKeyId(otRadioFrame * aFrame,uint8_t aKeyId)221 void otMacFrameSetKeyId(otRadioFrame *aFrame, uint8_t aKeyId) { static_cast<Mac::Frame *>(aFrame)->SetKeyId(aKeyId); }
222 
otMacFrameGetFrameCounter(otRadioFrame * aFrame)223 uint32_t otMacFrameGetFrameCounter(otRadioFrame *aFrame)
224 {
225     uint32_t frameCounter = UINT32_MAX;
226 
227     IgnoreError(static_cast<Mac::Frame *>(aFrame)->GetFrameCounter(frameCounter));
228 
229     return frameCounter;
230 }
231 
otMacFrameSetFrameCounter(otRadioFrame * aFrame,uint32_t aFrameCounter)232 void otMacFrameSetFrameCounter(otRadioFrame *aFrame, uint32_t aFrameCounter)
233 {
234     static_cast<Mac::Frame *>(aFrame)->SetFrameCounter(aFrameCounter);
235 }
236 
237 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
otMacFrameGenerateCslIeTemplate(uint8_t * aDest)238 uint8_t otMacFrameGenerateCslIeTemplate(uint8_t *aDest)
239 {
240     assert(aDest != nullptr);
241 
242     reinterpret_cast<Mac::HeaderIe *>(aDest)->SetId(Mac::CslIe::kHeaderIeId);
243     reinterpret_cast<Mac::HeaderIe *>(aDest)->SetLength(sizeof(Mac::CslIe));
244 
245     return sizeof(Mac::HeaderIe) + sizeof(Mac::CslIe);
246 }
247 #endif
248 
249 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
otMacFrameGenerateEnhAckProbingIe(uint8_t * aDest,const uint8_t * aIeData,uint8_t aIeDataLength)250 uint8_t otMacFrameGenerateEnhAckProbingIe(uint8_t *aDest, const uint8_t *aIeData, uint8_t aIeDataLength)
251 {
252     uint8_t len = sizeof(Mac::VendorIeHeader) + aIeDataLength;
253 
254     assert(aDest != nullptr);
255 
256     reinterpret_cast<Mac::HeaderIe *>(aDest)->SetId(Mac::ThreadIe::kHeaderIeId);
257     reinterpret_cast<Mac::HeaderIe *>(aDest)->SetLength(len);
258 
259     aDest += sizeof(Mac::HeaderIe);
260 
261     reinterpret_cast<Mac::VendorIeHeader *>(aDest)->SetVendorOui(Mac::ThreadIe::kVendorOuiThreadCompanyId);
262     reinterpret_cast<Mac::VendorIeHeader *>(aDest)->SetSubType(Mac::ThreadIe::kEnhAckProbingIe);
263 
264     if (aIeData != nullptr)
265     {
266         aDest += sizeof(Mac::VendorIeHeader);
267         memcpy(aDest, aIeData, aIeDataLength);
268     }
269 
270     return sizeof(Mac::HeaderIe) + len;
271 }
272 
otMacFrameSetEnhAckProbingIe(otRadioFrame * aFrame,const uint8_t * aData,uint8_t aDataLen)273 void otMacFrameSetEnhAckProbingIe(otRadioFrame *aFrame, const uint8_t *aData, uint8_t aDataLen)
274 {
275     assert(aFrame != nullptr && aData != nullptr);
276 
277     reinterpret_cast<Mac::Frame *>(aFrame)->SetEnhAckProbingIe(aData, aDataLen);
278 }
279 #endif // OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
280