mirror of
https://github.com/espressif/openthread.git
synced 2026-09-02 07:10:08 +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:
@@ -179,7 +179,7 @@ void WriteTestFrame1(Spinel::Buffer &aNcpBuffer, Spinel::Buffer::Priority aPrior
|
||||
message = sMessagePool->New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message != nullptr, "Null Message");
|
||||
SuccessOrQuit(message->SetLength(sizeof(sMottoText)), "Could not set the length of message.");
|
||||
message->Write(0, sizeof(sMottoText), sMottoText);
|
||||
message->Write(0, sMottoText);
|
||||
|
||||
oldContext = sContext;
|
||||
aNcpBuffer.InFrameBegin(aPriority);
|
||||
@@ -223,12 +223,12 @@ void WriteTestFrame2(Spinel::Buffer &aNcpBuffer, Spinel::Buffer::Priority aPrior
|
||||
message1 = sMessagePool->New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message1 != nullptr, "Null Message");
|
||||
SuccessOrQuit(message1->SetLength(sizeof(sMysteryText)), "Could not set the length of message.");
|
||||
message1->Write(0, sizeof(sMysteryText), sMysteryText);
|
||||
message1->Write(0, sMysteryText);
|
||||
|
||||
message2 = sMessagePool->New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message2 != nullptr, "Null Message");
|
||||
SuccessOrQuit(message2->SetLength(sizeof(sHelloText)), "Could not set the length of message.");
|
||||
message2->Write(0, sizeof(sHelloText), sHelloText);
|
||||
message2->Write(0, sHelloText);
|
||||
|
||||
aNcpBuffer.InFrameBegin(aPriority);
|
||||
SuccessOrQuit(aNcpBuffer.InFrameFeedMessage(message1), "InFrameFeedMessage() failed.");
|
||||
@@ -529,7 +529,7 @@ void TestBuffer(void)
|
||||
message = sMessagePool->New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message != nullptr, "Null Message");
|
||||
SuccessOrQuit(message->SetLength(sizeof(sMysteryText)), "Could not set the length of message.");
|
||||
message->Write(0, sizeof(sMysteryText), sMysteryText);
|
||||
message->Write(0, sMysteryText);
|
||||
|
||||
SuccessOrQuit(ncpBuffer.InFrameFeedMessage(message), "InFrameFeedMessage() failed.");
|
||||
|
||||
@@ -734,7 +734,7 @@ void TestBuffer(void)
|
||||
message = sMessagePool->New(Message::kTypeIp6, 0);
|
||||
VerifyOrQuit(message != nullptr, "Null Message");
|
||||
SuccessOrQuit(message->SetLength(sizeof(sMysteryText)), "Could not set the length of message.");
|
||||
message->Write(0, sizeof(sMysteryText), sMysteryText);
|
||||
message->Write(0, sMysteryText);
|
||||
VerifyOrQuit(ncpBuffer.InFrameFeedMessage(message) == OT_ERROR_INVALID_STATE, "Incorrect error status");
|
||||
message->Free();
|
||||
VerifyOrQuit(ncpBuffer.InFrameEnd() == OT_ERROR_INVALID_STATE, "Incorrect error status");
|
||||
|
||||
Reference in New Issue
Block a user