Skip to content

Commit

Permalink
Refresh TimedRequest (#11172)
Browse files Browse the repository at this point in the history
-- Change enum to enum class for tag inside TimedRequest
-- Rename TimedRequest to TimedRequestMessage
  • Loading branch information
yunhanw-google authored and pull[bot] committed Dec 21, 2023
1 parent a12ebd3 commit 64a8275
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static_library("app") {
"MessageDef/StatusResponse.cpp",
"MessageDef/SubscribeRequest.cpp",
"MessageDef/SubscribeResponse.cpp",
"MessageDef/TimedRequest.cpp",
"MessageDef/TimedRequestMessage.cpp",
"MessageDef/WriteRequest.cpp",
"MessageDef/WriteResponse.cpp",
"ReadClient.cpp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,59 @@
* limitations under the License.
*/

#include "TimedRequest.h"
#include "TimedRequestMessage.h"
#include "MessageDefHelper.h"

namespace chip {
namespace app {
CHIP_ERROR TimedRequest::Parser::Init(const chip::TLV::TLVReader & aReader)
CHIP_ERROR TimedRequestMessage::Parser::Init(const chip::TLV::TLVReader & aReader)
{
// make a copy of the reader here
mReader.Init(aReader);
VerifyOrReturnLogError(chip::TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnLogErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
VerifyOrReturnError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
return CHIP_NO_ERROR;
}

#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
CHIP_ERROR TimedRequest::Parser::CheckSchemaValidity() const
CHIP_ERROR TimedRequestMessage::Parser::CheckSchemaValidity() const
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t TagPresenceMask = 0;
chip::TLV::TLVReader reader;
PRETTY_PRINT("TimedRequest =");
CHIP_ERROR err = CHIP_NO_ERROR;
int TagPresenceMask = 0;
TLV::TLVReader reader;
PRETTY_PRINT("TimedRequestMessage =");
PRETTY_PRINT("{");

// make a copy of the reader
reader.Init(mReader);

while (CHIP_NO_ERROR == (err = reader.Next()))
{
VerifyOrReturnLogError(chip::TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
switch (chip::TLV::TagNumFromTag(reader.GetTag()))
VerifyOrReturnError(TLV::IsContextTag(reader.GetTag()), CHIP_ERROR_INVALID_TLV_TAG);
switch (TLV::TagNumFromTag(reader.GetTag()))
{
case kCsTag_TimeoutMs:
VerifyOrReturnLogError(!(TagPresenceMask & (1 << kCsTag_TimeoutMs)), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << kCsTag_TimeoutMs);
VerifyOrReturnLogError(chip::TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
case to_underlying(Tag::kTimeoutMs):
VerifyOrReturnError(!(TagPresenceMask & (1 << to_underlying(Tag::kTimeoutMs))), CHIP_ERROR_INVALID_TLV_TAG);
TagPresenceMask |= (1 << to_underlying(Tag::kTimeoutMs));
VerifyOrReturnError(TLV::kTLVType_UnsignedInteger == reader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
#if CHIP_DETAIL_LOGGING
{
uint16_t timeout;
ReturnLogErrorOnFailure(reader.Get(timeout));
ReturnErrorOnFailure(reader.Get(timeout));
PRETTY_PRINT("\tTimeoutMs = 0x%" PRIx8 ",", timeout);
}
#endif // CHIP_DETAIL_LOGGING
break;
default:
ReturnLogErrorOnFailure(CHIP_ERROR_INVALID_TLV_TAG);
ReturnErrorOnFailure(CHIP_ERROR_INVALID_TLV_TAG);
}
}
PRETTY_PRINT("}");
PRETTY_PRINT("");

if (CHIP_END_OF_TLV == err)
{
const uint16_t RequiredFields = (1 << kCsTag_TimeoutMs);
const int RequiredFields = (1 << to_underlying(Tag::kTimeoutMs));

if ((TagPresenceMask & RequiredFields) == RequiredFields)
{
Expand All @@ -78,22 +78,22 @@ CHIP_ERROR TimedRequest::Parser::CheckSchemaValidity() const
}
#endif // CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK

CHIP_ERROR TimedRequest::Parser::GetTimeoutMs(uint16_t * const apTimeoutMs) const
CHIP_ERROR TimedRequestMessage::Parser::GetTimeoutMs(uint16_t * const apTimeoutMs) const
{
return GetUnsignedInteger(kCsTag_TimeoutMs, apTimeoutMs);
return GetUnsignedInteger(to_underlying(Tag::kTimeoutMs), apTimeoutMs);
}

CHIP_ERROR TimedRequest::Builder::Init(chip::TLV::TLVWriter * const apWriter)
CHIP_ERROR TimedRequestMessage::Builder::Init(TLV::TLVWriter * const apWriter)
{
return InitAnonymousStructure(apWriter);
}

TimedRequest::Builder & TimedRequest::Builder::TimeoutMs(const uint16_t aTimeoutMs)
TimedRequestMessage::Builder & TimedRequestMessage::Builder::TimeoutMs(const uint16_t aTimeoutMs)
{
// skip if error has already been set
if (mError == CHIP_NO_ERROR)
{
mError = mpWriter->Put(chip::TLV::ContextTag(kCsTag_TimeoutMs), aTimeoutMs);
mError = mpWriter->Put(TLV::ContextTag(to_underlying(Tag::kTimeoutMs)), aTimeoutMs);
}
EndOfContainer();
return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@

namespace chip {
namespace app {
namespace TimedRequest {
enum
namespace TimedRequestMessage {
enum class Tag : uint8_t
{
kCsTag_TimeoutMs = 0,
kTimeoutMs = 0,
};

class Parser : public chip::app::Parser
{
public:
/**
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this response
* @param [in] aReader A pointer to a TLVReader, which should point to the beginning of this request
*/
CHIP_ERROR Init(const chip::TLV::TLVReader & aReader);
CHIP_ERROR Init(const TLV::TLVReader & aReader);
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
/**
* @brief Roughly verify the message is correctly formed
Expand Down Expand Up @@ -69,14 +69,14 @@ class Parser : public chip::app::Parser
class Builder : public chip::app::Builder
{
public:
CHIP_ERROR Init(chip::TLV::TLVWriter * const apWriter);
CHIP_ERROR Init(TLV::TLVWriter * const apWriter);

/**
* @brief Timeout value, sent by a client to a server, if there is a preceding successful Timed Request action,
* the following action SHALL be received before the end of the Timeout interval.
*/
TimedRequest::Builder & TimeoutMs(const uint16_t aTimeoutMs);
TimedRequestMessage::Builder & TimeoutMs(const uint16_t aTimeoutMs);
};
} // namespace TimedRequest
} // namespace TimedRequestMessage
} // namespace app
} // namespace chip
30 changes: 15 additions & 15 deletions src/app/tests/TestMessageDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <app/MessageDef/ReportData.h>
#include <app/MessageDef/SubscribeRequest.h>
#include <app/MessageDef/SubscribeResponse.h>
#include <app/MessageDef/TimedRequest.h>
#include <app/MessageDef/TimedRequestMessage.h>
#include <app/MessageDef/WriteRequest.h>
#include <app/MessageDef/WriteResponse.h>
#include <lib/core/CHIPError.h>
Expand Down Expand Up @@ -1010,32 +1010,32 @@ void ParseSubscribeResponse(nlTestSuite * apSuite, chip::TLV::TLVReader & aReade
NL_TEST_ASSERT(apSuite, maxIntervalCeilingSeconds == 2 && err == CHIP_NO_ERROR);
}

void BuildTimedRequest(nlTestSuite * apSuite, chip::TLV::TLVWriter & aWriter)
void BuildTimedRequestMessage(nlTestSuite * apSuite, chip::TLV::TLVWriter & aWriter)
{
CHIP_ERROR err = CHIP_NO_ERROR;
TimedRequest::Builder timedRequestBuilder;
TimedRequestMessage::Builder TimedRequestMessageBuilder;

err = timedRequestBuilder.Init(&aWriter);
err = TimedRequestMessageBuilder.Init(&aWriter);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);

timedRequestBuilder.TimeoutMs(1);
NL_TEST_ASSERT(apSuite, timedRequestBuilder.GetError() == CHIP_NO_ERROR);
TimedRequestMessageBuilder.TimeoutMs(1);
NL_TEST_ASSERT(apSuite, TimedRequestMessageBuilder.GetError() == CHIP_NO_ERROR);
}

void ParseTimedRequest(nlTestSuite * apSuite, chip::TLV::TLVReader & aReader)
void ParseTimedRequestMessage(nlTestSuite * apSuite, chip::TLV::TLVReader & aReader)
{
CHIP_ERROR err = CHIP_NO_ERROR;

TimedRequest::Parser timedRequestarser;
TimedRequestMessage::Parser TimedRequestMessagearser;
uint16_t timeout = 0;

err = timedRequestarser.Init(aReader);
err = TimedRequestMessagearser.Init(aReader);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
#if CHIP_CONFIG_IM_ENABLE_SCHEMA_CHECK
err = timedRequestarser.CheckSchemaValidity();
err = TimedRequestMessagearser.CheckSchemaValidity();
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
#endif
err = timedRequestarser.GetTimeoutMs(&timeout);
err = TimedRequestMessagearser.GetTimeoutMs(&timeout);
NL_TEST_ASSERT(apSuite, timeout == 1 && err == CHIP_NO_ERROR);
}

Expand Down Expand Up @@ -1543,13 +1543,13 @@ void SubscribeResponseTest(nlTestSuite * apSuite, void * apContext)
ParseSubscribeResponse(apSuite, reader);
}

void TimedRequestTest(nlTestSuite * apSuite, void * apContext)
void TimedRequestMessageTest(nlTestSuite * apSuite, void * apContext)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::System::PacketBufferTLVWriter writer;
chip::System::PacketBufferTLVReader reader;
writer.Init(chip::System::PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSize));
BuildTimedRequest(apSuite, writer);
BuildTimedRequestMessage(apSuite, writer);
chip::System::PacketBufferHandle buf;
err = writer.Finalize(&buf);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
Expand All @@ -1559,7 +1559,7 @@ void TimedRequestTest(nlTestSuite * apSuite, void * apContext)
reader.Init(std::move(buf));
err = reader.Next();
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
ParseTimedRequest(apSuite, reader);
ParseTimedRequestMessage(apSuite, reader);
}

void CheckPointRollbackTest(nlTestSuite * apSuite, void * apContext)
Expand Down Expand Up @@ -1645,7 +1645,7 @@ const nlTest sTests[] =
NL_TEST_DEF("WriteResponseTest", WriteResponseTest),
NL_TEST_DEF("SubscribeRequestTest", SubscribeRequestTest),
NL_TEST_DEF("SubscribeResponseTest", SubscribeResponseTest),
NL_TEST_DEF("TimedRequestTest", TimedRequestTest),
NL_TEST_DEF("TimedRequestMessageTest", TimedRequestMessageTest),
NL_TEST_DEF("CheckPointRollbackTest", CheckPointRollbackTest),
NL_TEST_SENTINEL()
};
Expand Down

0 comments on commit 64a8275

Please sign in to comment.