From 7f05fb624fa3168c614a540db45a94e9b8b731a4 Mon Sep 17 00:00:00 2001 From: yunhanw-google Date: Fri, 29 Oct 2021 06:55:58 -0700 Subject: [PATCH] Refresh TimedRequest (#11172) -- Change enum to enum class for tag inside TimedRequest -- Rename TimedRequest to TimedRequestMessage --- src/app/BUILD.gn | 2 +- ...medRequest.cpp => TimedRequestMessage.cpp} | 46 +++++++++---------- .../{TimedRequest.h => TimedRequestMessage.h} | 16 +++---- src/app/tests/TestMessageDef.cpp | 30 ++++++------ 4 files changed, 47 insertions(+), 47 deletions(-) rename src/app/MessageDef/{TimedRequest.cpp => TimedRequestMessage.cpp} (52%) rename src/app/MessageDef/{TimedRequest.h => TimedRequestMessage.h} (87%) diff --git a/src/app/BUILD.gn b/src/app/BUILD.gn index 5fa15d41afd031..27a18093ad83d4 100644 --- a/src/app/BUILD.gn +++ b/src/app/BUILD.gn @@ -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", diff --git a/src/app/MessageDef/TimedRequest.cpp b/src/app/MessageDef/TimedRequestMessage.cpp similarity index 52% rename from src/app/MessageDef/TimedRequest.cpp rename to src/app/MessageDef/TimedRequestMessage.cpp index 520b4efca45af1..ef4e69248ea3da 100644 --- a/src/app/MessageDef/TimedRequest.cpp +++ b/src/app/MessageDef/TimedRequestMessage.cpp @@ -14,27 +14,27 @@ * 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 @@ -42,23 +42,23 @@ CHIP_ERROR TimedRequest::Parser::CheckSchemaValidity() const 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("}"); @@ -66,7 +66,7 @@ CHIP_ERROR TimedRequest::Parser::CheckSchemaValidity() const 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) { @@ -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; diff --git a/src/app/MessageDef/TimedRequest.h b/src/app/MessageDef/TimedRequestMessage.h similarity index 87% rename from src/app/MessageDef/TimedRequest.h rename to src/app/MessageDef/TimedRequestMessage.h index 9fd8cfaa4516fb..6bdd6c30220ff8 100644 --- a/src/app/MessageDef/TimedRequest.h +++ b/src/app/MessageDef/TimedRequestMessage.h @@ -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 @@ -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 diff --git a/src/app/tests/TestMessageDef.cpp b/src/app/tests/TestMessageDef.cpp index 891b6e29578d24..7b147e877aab6b 100644 --- a/src/app/tests/TestMessageDef.cpp +++ b/src/app/tests/TestMessageDef.cpp @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include @@ -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); } @@ -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); @@ -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) @@ -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() };