[mac-frame] enhance utility functions (#4826)

This commit enhances some utility functions in mac frame for frame
parsing and adds some basic testing.

- Currently the mac frame parsing only doesn't include the rule for
  version 2015. This commit adds the parsing of dstPanId for 2015
  frames.

- This commit adds some helper function dealing with Security Header
  so that later commit of CSL and setting security header in enhanced
  ACK would be easier.
This commit is contained in:
Li Cao
2020-04-16 23:11:45 -07:00
committed by GitHub
parent c34f3ef817
commit ab029ec0ac
3 changed files with 252 additions and 83 deletions
+65
View File
@@ -405,6 +405,70 @@ void TestMacChannelMask(void)
VerifyOrQuit(mask1 != mask2, "ChannelMask.operator== failed");
}
void TestMacFrameApi(void)
{
uint8_t ack_psdu1[] = {0x02, 0x10, 0x5e, 0xd2, 0x9b};
Mac::Frame frame;
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
uint8_t data_psdu1[] = {0x29, 0xee, 0x53, 0xce, 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x6e, 0x16, 0x05,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x6e, 0x16, 0x0d, 0x01, 0x00, 0x00, 0x00, 0x01};
otError error;
uint8_t scf; // SecurityControlField
#endif
// Imm-Ack, Sequence Number: 94
// Frame Control Field: 0x1002
// .... .... .... .010 = Frame Type: Ack (0x2)
// .... .... .... 0... = Security Enabled: False
// .... .... ...0 .... = Frame Pending: False
// .... .... ..0. .... = Acknowledge Request: False
// .... .... .0.. .... = PAN ID Compression: False
// .... ...0 .... .... = Sequence Number Suppression: False
// .... ..0. .... .... = Information Elements Present: False
// .... 00.. .... .... = Destination Addressing Mode: None (0x0)
// ..01 .... .... .... = Frame Version: IEEE Std 802.15.4-2006 (1)
// 00.. .... .... .... = Source Addressing Mode: None (0x0)
// Sequence Number: 94
// FCS: 0x9bd2 (Correct)
frame.mPsdu = ack_psdu1;
frame.mLength = sizeof(ack_psdu1);
VerifyOrQuit(frame.GetType() == Mac::Frame::kFcfFrameAck, "Mac::Frame::GetType() failed\n");
VerifyOrQuit(frame.GetSecurityEnabled() == false, "Mac::Frame::GetSecurityEnabled() failed\n");
VerifyOrQuit(frame.GetFramePending() == false, "Mac::Frame::GetFramePendIng() failed\n");
VerifyOrQuit(frame.GetAckRequest() == false, "Mac::Frame::GetAckRequest failed\n");
VerifyOrQuit(frame.IsIePresent() == false, "Mac::Frame::IsIePresent failed\n");
VerifyOrQuit(frame.IsDstPanIdPresent() == false, "Mac::Frame::IsDstPanIdPresent failed\n");
VerifyOrQuit(frame.IsDstAddrPresent() == false, "Mac::Frame::IsDstAddrPresent failed\n");
VerifyOrQuit(frame.GetVersion() == Mac::Frame::kFcfFrameVersion2006, "Mac::Frame::GetVersion failed\n");
VerifyOrQuit(frame.IsSrcAddrPresent() == false, "Mac::Frame::IsSrcAddrPresent failed\n");
VerifyOrQuit(frame.GetSequence() == 94, "Mac::Frame::GetSequence failed\n");
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
// IEEE 802.15.4-2015 Data
// Sequence Number: 83
// Destination PAN: 0xface
// Destination: 16:6e:0a:00:00:00:00:01
// Extended Source: 16:6e:0a:00:00:00:00:05
// Auxiliary Security Header
// Security Control Field: 0x0d
frame.mPsdu = data_psdu1;
frame.mLength = sizeof(data_psdu1);
VerifyOrQuit(frame.IsVersion2015() == true, "Mac::Frame::IsVersion2015 failed\n");
VerifyOrQuit(frame.IsDstPanIdPresent() == true, "Mac::Frame::IsDstPanIdPresent failed\n");
VerifyOrQuit(frame.IsDstAddrPresent() == true, "Mac::Frame::IsDstAddrPresent failed\n");
VerifyOrQuit(frame.IsSrcAddrPresent() == true, "Mac::Frame::IsSrcAddrPresent failed\n");
VerifyOrQuit((error = frame.GetSecurityControlField(scf)) == OT_ERROR_NONE,
"Mac::Frame::GetSecurityControlField failed\n");
VerifyOrQuit(scf == 0x0d, "Mac::Frame::GetSecurityControlField value failed\n");
frame.SetSecurityControlField(0xff);
VerifyOrQuit((error = frame.GetSecurityControlField(scf)) == OT_ERROR_NONE,
"Mac::Frame::GetSecurityControlField failed\n");
VerifyOrQuit(scf == 0xff, "Mac::Frame::SetSecurityControlField value failed\n");
#endif // OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2
}
} // namespace ot
int main(void)
@@ -413,6 +477,7 @@ int main(void)
ot::TestMacNetworkName();
ot::TestMacHeader();
ot::TestMacChannelMask();
ot::TestMacFrameApi();
printf("All tests passed\n");
return 0;
}