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 SuccessOrExit(frame.GetDstAddr(dst));
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::kFcfFrameAck;
72 }
73
otMacFrameIsData(const otRadioFrame * aFrame)74 bool otMacFrameIsData(const otRadioFrame *aFrame)
75 {
76 return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kFcfFrameData;
77 }
78
otMacFrameIsCommand(const otRadioFrame * aFrame)79 bool otMacFrameIsCommand(const otRadioFrame *aFrame)
80 {
81 return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kFcfFrameMacCmd;
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)142 uint8_t otMacFrameGetSequence(const otRadioFrame *aFrame)
143 {
144 return static_cast<const Mac::Frame *>(aFrame)->GetSequence();
145 }
146
otMacFrameProcessTransmitAesCcm(otRadioFrame * aFrame,const otExtAddress * aExtAddress)147 void otMacFrameProcessTransmitAesCcm(otRadioFrame *aFrame, const otExtAddress *aExtAddress)
148 {
149 static_cast<Mac::TxFrame *>(aFrame)->ProcessTransmitAesCcm(*static_cast<const Mac::ExtAddress *>(aExtAddress));
150 }
151
otMacFrameIsVersion2015(const otRadioFrame * aFrame)152 bool otMacFrameIsVersion2015(const otRadioFrame *aFrame)
153 {
154 return static_cast<const Mac::Frame *>(aFrame)->IsVersion2015();
155 }
156
otMacFrameGenerateImmAck(const otRadioFrame * aFrame,bool aIsFramePending,otRadioFrame * aAckFrame)157 void otMacFrameGenerateImmAck(const otRadioFrame *aFrame, bool aIsFramePending, otRadioFrame *aAckFrame)
158 {
159 assert(aFrame != nullptr && aAckFrame != nullptr);
160
161 static_cast<Mac::TxFrame *>(aAckFrame)->GenerateImmAck(*static_cast<const Mac::RxFrame *>(aFrame), aIsFramePending);
162 }
163
164 #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)165 otError otMacFrameGenerateEnhAck(const otRadioFrame *aFrame,
166 bool aIsFramePending,
167 const uint8_t * aIeData,
168 uint8_t aIeLength,
169 otRadioFrame * aAckFrame)
170 {
171 assert(aFrame != nullptr && aAckFrame != nullptr);
172
173 return static_cast<Mac::TxFrame *>(aAckFrame)->GenerateEnhAck(*static_cast<const Mac::RxFrame *>(aFrame),
174 aIsFramePending, aIeData, aIeLength);
175 }
176 #endif
177
178 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
otMacFrameSetCslIe(otRadioFrame * aFrame,uint16_t aCslPeriod,uint16_t aCslPhase)179 void otMacFrameSetCslIe(otRadioFrame *aFrame, uint16_t aCslPeriod, uint16_t aCslPhase)
180 {
181 static_cast<Mac::Frame *>(aFrame)->SetCslIe(aCslPeriod, aCslPhase);
182 }
183 #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
184
otMacFrameIsSecurityEnabled(otRadioFrame * aFrame)185 bool otMacFrameIsSecurityEnabled(otRadioFrame *aFrame)
186 {
187 return static_cast<const Mac::Frame *>(aFrame)->GetSecurityEnabled();
188 }
189
otMacFrameIsKeyIdMode1(otRadioFrame * aFrame)190 bool otMacFrameIsKeyIdMode1(otRadioFrame *aFrame)
191 {
192 uint8_t keyIdMode;
193 otError error;
194
195 error = static_cast<const Mac::Frame *>(aFrame)->GetKeyIdMode(keyIdMode);
196
197 return (error == OT_ERROR_NONE) ? (keyIdMode == Mac::Frame::kKeyIdMode1) : false;
198 }
199
otMacFrameGetKeyId(otRadioFrame * aFrame)200 uint8_t otMacFrameGetKeyId(otRadioFrame *aFrame)
201 {
202 uint8_t keyId = 0;
203
204 IgnoreError(static_cast<const Mac::Frame *>(aFrame)->GetKeyId(keyId));
205
206 return keyId;
207 }
208
otMacFrameSetKeyId(otRadioFrame * aFrame,uint8_t aKeyId)209 void otMacFrameSetKeyId(otRadioFrame *aFrame, uint8_t aKeyId)
210 {
211 static_cast<Mac::Frame *>(aFrame)->SetKeyId(aKeyId);
212 }
213
otMacFrameGetFrameCounter(otRadioFrame * aFrame)214 uint32_t otMacFrameGetFrameCounter(otRadioFrame *aFrame)
215 {
216 uint32_t frameCounter = UINT32_MAX;
217
218 IgnoreError(static_cast<Mac::Frame *>(aFrame)->GetFrameCounter(frameCounter));
219
220 return frameCounter;
221 }
222
otMacFrameSetFrameCounter(otRadioFrame * aFrame,uint32_t aFrameCounter)223 void otMacFrameSetFrameCounter(otRadioFrame *aFrame, uint32_t aFrameCounter)
224 {
225 static_cast<Mac::Frame *>(aFrame)->SetFrameCounter(aFrameCounter);
226 }
227
228 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
otMacFrameGenerateCslIeTemplate(uint8_t * aDest)229 uint8_t otMacFrameGenerateCslIeTemplate(uint8_t *aDest)
230 {
231 assert(aDest != nullptr);
232
233 reinterpret_cast<Mac::HeaderIe *>(aDest)->SetId(Mac::CslIe::kHeaderIeId);
234 reinterpret_cast<Mac::HeaderIe *>(aDest)->SetLength(sizeof(Mac::CslIe));
235
236 return sizeof(Mac::HeaderIe) + sizeof(Mac::CslIe);
237 }
238 #endif
239
240 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
otMacFrameGenerateEnhAckProbingIe(uint8_t * aDest,const uint8_t * aIeData,uint8_t aIeDataLength)241 uint8_t otMacFrameGenerateEnhAckProbingIe(uint8_t *aDest, const uint8_t *aIeData, uint8_t aIeDataLength)
242 {
243 uint8_t len = sizeof(Mac::VendorIeHeader) + aIeDataLength;
244
245 assert(aDest != nullptr);
246
247 reinterpret_cast<Mac::HeaderIe *>(aDest)->SetId(Mac::ThreadIe::kHeaderIeId);
248 reinterpret_cast<Mac::HeaderIe *>(aDest)->SetLength(len);
249
250 aDest += sizeof(Mac::HeaderIe);
251
252 reinterpret_cast<Mac::VendorIeHeader *>(aDest)->SetVendorOui(Mac::ThreadIe::kVendorOuiThreadCompanyId);
253 reinterpret_cast<Mac::VendorIeHeader *>(aDest)->SetSubType(Mac::ThreadIe::kEnhAckProbingIe);
254
255 if (aIeData != nullptr)
256 {
257 aDest += sizeof(Mac::VendorIeHeader);
258 memcpy(aDest, aIeData, aIeDataLength);
259 }
260
261 return sizeof(Mac::HeaderIe) + len;
262 }
263
otMacFrameSetEnhAckProbingIe(otRadioFrame * aFrame,const uint8_t * aData,uint8_t aDataLen)264 void otMacFrameSetEnhAckProbingIe(otRadioFrame *aFrame, const uint8_t *aData, uint8_t aDataLen)
265 {
266 assert(aFrame != nullptr && aData != nullptr);
267
268 reinterpret_cast<Mac::Frame *>(aFrame)->SetEnhAckProbingIe(aData, aDataLen);
269 }
270 #endif // OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
271