Skip to content

Commit

Permalink
[mac-frame] add DetermineFcfAddrType() helper (openthread#10780)
Browse files Browse the repository at this point in the history
This commit adds the `DetermineFcfAddrType()` helper function, which
determines the Frame Control Field (FCF) address type for a given
address. The result is bit-shifted based on whether the address is
the source or destination and whether the frame uses the general
format or is a multipurpose frame. This helper simplifies methods
that prepare MAC headers.
  • Loading branch information
abtink authored Oct 4, 2024
1 parent 678d137 commit b8d2e5d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
84 changes: 34 additions & 50 deletions src/core/mac/mac_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,12 @@ void TxFrame::Info::PrepareHeadersIn(TxFrame &aTxFrame) const

fcf = static_cast<uint16_t>(mType) | static_cast<uint16_t>(mVersion);

switch (mAddrs.mSource.GetType())
{
case Address::kTypeNone:
fcf |= kFcfSrcAddrNone;
break;
case Address::kTypeShort:
fcf |= kFcfSrcAddrShort;
break;
case Address::kTypeExtended:
fcf |= kFcfSrcAddrExt;
break;
}
fcf |= DetermineFcfAddrType(mAddrs.mSource, kFcfSrcAddrShift);
fcf |= DetermineFcfAddrType(mAddrs.mDestination, kFcfDstAddrShift);

switch (mAddrs.mDestination.GetType())
if (!mAddrs.mDestination.IsNone() && !mAddrs.mDestination.IsBroadcast() && (mType != kTypeAck))
{
case Address::kTypeNone:
fcf |= kFcfDstAddrNone;
break;
case Address::kTypeShort:
fcf |= kFcfDstAddrShort;
fcf |= ((mAddrs.mDestination.GetShort() == kShortAddrBroadcast) ? 0 : kFcfAckRequest);
break;
case Address::kTypeExtended:
fcf |= (kFcfDstAddrExt | kFcfAckRequest);
break;
}

if (mType == kTypeAck)
{
fcf &= ~kFcfAckRequest;
fcf |= kFcfAckRequest;
}

fcf |= (mSecurityLevel != kSecurityNone) ? kFcfSecurityEnabled : 0;
Expand Down Expand Up @@ -917,6 +893,33 @@ uint8_t Frame::SkipSecurityHeaderIndex(void) const
return index;
}

uint16_t Frame::DetermineFcfAddrType(const Address &aAddress, uint16_t aBitShift)
{
// Determines the FCF address type for a given `aAddress`. The
// result will be bit-shifted using `aBitShift` value which
// correspond to whether address is the source or destination
// and whether the frame uses the general format or is a
// multipurpose frame

uint16_t fcfAddrType = kFcfAddrNone;

switch (aAddress.GetType())
{
case Address::kTypeNone:
break;
case Address::kTypeShort:
fcfAddrType = kFcfAddrShort;
break;
case Address::kTypeExtended:
fcfAddrType = kFcfAddrExt;
break;
}

fcfAddrType <<= aBitShift;

return fcfAddrType;
}

uint8_t Frame::CalculateSecurityHeaderSize(uint8_t aSecurityControl)
{
uint8_t size = kSecurityControlSize + kFrameCounterSize;
Expand Down Expand Up @@ -1535,29 +1538,10 @@ Error TxFrame::GenerateWakeupFrame(PanId aPanId, const Address &aDest, const Add
fcf = kTypeMultipurpose | kMpFcfLongFrame | kMpFcfPanidPresent | kMpFcfSecurityEnabled | kMpFcfSequenceSuppression |
kMpFcfIePresent;

switch (aDest.GetType())
{
case Address::Type::kTypeShort:
fcf |= kMpFcfDstAddrShort;
break;
case Address::Type::kTypeExtended:
fcf |= kMpFcfDstAddrExt;
break;
default:
ExitNow(error = kErrorInvalidArgs);
}
VerifyOrExit(!aDest.IsNone() && !aSource.IsNone(), error = kErrorInvalidArgs);

switch (aSource.GetType())
{
case Address::Type::kTypeShort:
fcf |= kMpFcfSrcAddrShort;
break;
case Address::Type::kTypeExtended:
fcf |= kMpFcfSrcAddrExt;
break;
default:
ExitNow(error = kErrorInvalidArgs);
}
fcf |= DetermineFcfAddrType(aDest, kMpFcfDstAddrShift);
fcf |= DetermineFcfAddrType(aSource, kMpFcfSrcAddrShift);

builder.Init(mPsdu, GetMtu());

Expand Down
2 changes: 2 additions & 0 deletions src/core/mac/mac_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,8 @@ class Frame : public otRadioFrame
static bool IsAckRequest(uint16_t aFcf) { return MaskFcf<kFcfAckRequest, kMpFcfAckRequest>(aFcf); }
static bool IsVersion2015(uint16_t aFcf) { return (aFcf & kFcfFrameVersionMask) == kVersion2015; }

static uint16_t DetermineFcfAddrType(const Address &aAddress, uint16_t aBitShift);

static uint8_t CalculateAddrFieldSize(uint16_t aFcf);
static uint8_t CalculateSecurityHeaderSize(uint8_t aSecurityControl);
static uint8_t CalculateMicSize(uint8_t aSecurityControl);
Expand Down

0 comments on commit b8d2e5d

Please sign in to comment.