mirror of
https://github.com/espressif/openthread.git
synced 2026-09-07 09:40:12 +00:00
[message] add new flavors of Read/Write/Append/Prepend methods (#5604)
This commit adds new helper methods in `Message` class. It adds new template methods to allow an object (of any type) to be read from or written/appended/prepended to the message. The `Read<ObjectType>()` method returns a parse `otError` if there are not enough bytes remaining in the message to read the entire object. The template methods are used in other core modules which help simplify the code (there is no need to specify the template type since it can be deduced by the complier from the passed-in argument). Also since the template methods directly use the `sizeof` the object type, they help reduce the chance of incorrect use (using incorrect read/write length when calling the method). This commit also updates and renames the methods which read/write raw bytes from/to the message to `ReadBytes()`/`WriteBytes()`. The order of parameters in these methods are changed to follow the common pattern used by other modules and the OT public APIs (i.e., the pointer to the buffer is given first, followed by its length).
This commit is contained in:
@@ -79,27 +79,24 @@ void TestIphcVector::GetUncompressedStream(uint8_t *aIp6, uint16_t &aIp6Length)
|
||||
|
||||
void TestIphcVector::GetUncompressedStream(Message &aMessage)
|
||||
{
|
||||
SuccessOrQuit(aMessage.Append(reinterpret_cast<uint8_t *>(&mIpHeader), sizeof(mIpHeader)),
|
||||
"6lo: Message::Append failed");
|
||||
SuccessOrQuit(aMessage.Append(mIpHeader), "6lo: Message::Append failed");
|
||||
|
||||
if (mExtHeader.mLength)
|
||||
{
|
||||
SuccessOrQuit(aMessage.Append(mExtHeader.mData, mExtHeader.mLength), "6lo: Message::Append failed");
|
||||
SuccessOrQuit(aMessage.AppendBytes(mExtHeader.mData, mExtHeader.mLength), "6lo: Message::Append failed");
|
||||
}
|
||||
|
||||
if (mIpTunneledHeader.GetPayloadLength())
|
||||
{
|
||||
SuccessOrQuit(aMessage.Append(reinterpret_cast<uint8_t *>(&mIpTunneledHeader), sizeof(mIpTunneledHeader)),
|
||||
"6lo: Message::Append failed");
|
||||
SuccessOrQuit(aMessage.Append(mIpTunneledHeader), "6lo: Message::Append failed");
|
||||
}
|
||||
|
||||
if (mUdpHeader.GetLength())
|
||||
{
|
||||
SuccessOrQuit(aMessage.Append(reinterpret_cast<uint8_t *>(&mUdpHeader), sizeof(mUdpHeader)),
|
||||
"6lo: Message::Append failed");
|
||||
SuccessOrQuit(aMessage.Append(mUdpHeader), "6lo: Message::Append failed");
|
||||
}
|
||||
|
||||
SuccessOrQuit(aMessage.Append(mPayload.mData, mPayload.mLength), "6lo: Message::Append failed");
|
||||
SuccessOrQuit(aMessage.AppendBytes(mPayload.mData, mPayload.mLength), "6lo: Message::Append failed");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +128,7 @@ static void Init(void)
|
||||
Message *message = sInstance->Get<MessagePool>().New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message != nullptr, "6lo: Ip6::NewMessage failed");
|
||||
|
||||
SuccessOrQuit(message->Append(mockNetworkData, sizeof(mockNetworkData)), "6lo: Message::Append failed");
|
||||
SuccessOrQuit(message->AppendBytes(mockNetworkData, sizeof(mockNetworkData)), "6lo: Message::Append failed");
|
||||
|
||||
IgnoreError(sInstance->Get<NetworkData::Leader>().SetNetworkData(0, 0, true, *message, 0));
|
||||
}
|
||||
@@ -188,7 +185,8 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress)
|
||||
uint8_t compressBytes = static_cast<uint8_t>(buffer.GetWritePointer() - result);
|
||||
|
||||
// Append payload to the LOWPAN_IPHC.
|
||||
message->Read(message->GetOffset(), message->GetLength() - message->GetOffset(), result + compressBytes);
|
||||
message->ReadBytes(message->GetOffset(), result + compressBytes,
|
||||
message->GetLength() - message->GetOffset());
|
||||
|
||||
DumpBuffer("Resulted LOWPAN_IPHC compressed frame", result,
|
||||
compressBytes + message->GetLength() - message->GetOffset());
|
||||
@@ -210,7 +208,7 @@ static void Test(TestIphcVector &aVector, bool aCompress, bool aDecompress)
|
||||
int decompressedBytes =
|
||||
sLowpan->Decompress(*message, aVector.mMacSource, aVector.mMacDestination, iphc, iphcLength, 0);
|
||||
|
||||
message->Read(0, message->GetLength(), result);
|
||||
message->ReadBytes(0, result, message->GetLength());
|
||||
|
||||
if (aVector.mError == OT_ERROR_NONE)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user