From 67dda879bc50c537e2be5c2a5fa133dfa69d02f2 Mon Sep 17 00:00:00 2001 From: Song Guo Date: Thu, 20 Jan 2022 10:49:19 +0800 Subject: [PATCH] Move Init to private --- src/app/WriteClient.cpp | 16 +++++++------- src/app/WriteClient.h | 20 ++++++++++------- src/app/tests/TestWriteInteraction.cpp | 22 ++++++------------- .../tests/integration/chip_im_initiator.cpp | 5 ++--- src/controller/WriteInteraction.h | 7 +++--- .../python/chip/clusters/attribute.cpp | 6 ++--- 6 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/app/WriteClient.cpp b/src/app/WriteClient.cpp index 6abda7065e0c13..fcb647adb8922b 100644 --- a/src/app/WriteClient.cpp +++ b/src/app/WriteClient.cpp @@ -31,11 +31,10 @@ namespace chip { namespace app { -CHIP_ERROR WriteClient::Init(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback, - const Optional & aTimedWriteTimeoutMs) +CHIP_ERROR WriteClient::Init() { - VerifyOrReturnError(apExchangeMgr != nullptr, CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrReturnError(mpExchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mState == State::Uninitialized, CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(mpExchangeMgr != nullptr, CHIP_ERROR_INCORRECT_STATE); VerifyOrReturnError(mpExchangeCtx == nullptr, CHIP_ERROR_INCORRECT_STATE); System::PacketBufferHandle packet = System::PacketBufferHandle::New(chip::app::kMaxSecureSduLengthBytes); @@ -44,14 +43,11 @@ CHIP_ERROR WriteClient::Init(Messaging::ExchangeManager * apExchangeMgr, Callbac mMessageWriter.Init(std::move(packet)); ReturnErrorOnFailure(mWriteRequestBuilder.Init(&mMessageWriter)); - mWriteRequestBuilder.TimedRequest(aTimedWriteTimeoutMs.HasValue()); + mWriteRequestBuilder.TimedRequest(mTimedWriteTimeoutMs.HasValue()); ReturnErrorOnFailure(mWriteRequestBuilder.GetError()); mWriteRequestBuilder.CreateWriteRequests(); ReturnErrorOnFailure(mWriteRequestBuilder.GetError()); - mpExchangeMgr = apExchangeMgr; - mpCallback = apCallback; - mTimedWriteTimeoutMs = aTimedWriteTimeoutMs; MoveToState(State::Initialized); return CHIP_NO_ERROR; @@ -150,6 +146,10 @@ CHIP_ERROR WriteClient::ProcessWriteResponseMessage(System::PacketBufferHandle & CHIP_ERROR WriteClient::PrepareAttribute(const AttributePathParams & attributePathParams) { + if (mState == State::Uninitialized) + { + ReturnErrorOnFailure(Init()); + } VerifyOrReturnError(attributePathParams.IsValidAttributePath(), CHIP_ERROR_INVALID_PATH_LIST); AttributeDataIBs::Builder & writeRequests = mWriteRequestBuilder.GetWriteRequests(); AttributeDataIB::Builder & attributeDataIB = writeRequests.CreateAttributeDataIBBuilder(); diff --git a/src/app/WriteClient.h b/src/app/WriteClient.h index 39fed9bbb1e665..43ab35df860148 100644 --- a/src/app/WriteClient.h +++ b/src/app/WriteClient.h @@ -106,19 +106,18 @@ class WriteClient : public Messaging::ExchangeDelegate }; /** - * Initialize the client object. Within the lifetime - * of this instance, this method is invoked once after object - * construction until a call to Shutdown is made to terminate the - * instance. + * Construct the client object. Within the lifetime + * of this instance. * * @param[in] apExchangeMgr A pointer to the ExchangeManager object. * @param[in] apDelegate InteractionModelDelegate set by application. * @param[in] aTimedWriteTimeoutMs If provided, do a timed write using this timeout. - * @retval #CHIP_ERROR_INCORRECT_STATE incorrect state if it is already initialized - * @retval #CHIP_NO_ERROR On success. */ - CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, Callback * apDelegate, - const Optional & aTimedWriteTimeoutMs); + WriteClient(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback, + const Optional & aTimedWriteTimeoutMs) : + mpExchangeMgr(apExchangeMgr), + mpCallback(apCallback), mTimedWriteTimeoutMs(aTimedWriteTimeoutMs) + {} /** * Encode an attribute value that can be directly encoded using TLVWriter::Put @@ -182,6 +181,11 @@ class WriteClient : public Messaging::ExchangeDelegate AwaitingDestruction, // The object has completed its work and is awaiting destruction by the application. }; + /** + * The actual init function, called during encoding first attribute data. + */ + CHIP_ERROR Init(); + /** * Finalize Write Request Message TLV Builder and retrieve final data from tlv builder for later sending */ diff --git a/src/app/tests/TestWriteInteraction.cpp b/src/app/tests/TestWriteInteraction.cpp index 9b953704e46c59..489d8899d6b37c 100644 --- a/src/app/tests/TestWriteInteraction.cpp +++ b/src/app/tests/TestWriteInteraction.cpp @@ -146,7 +146,7 @@ void TestWriteInteraction::GenerateWriteRequest(nlTestSuite * apSuite, void * ap chip::TLV::TLVWriter * pWriter = attributeDataIBBuilder.GetWriter(); chip::TLV::TLVType dummyType = chip::TLV::kTLVType_NotSpecified; err = pWriter->StartContainer(chip::TLV::ContextTag(to_underlying(AttributeDataIB::Tag::kData)), - chip::TLV::kTLVType_Structure, dummyType); + chip::TLV::kTLVType_Structure, dummyType); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); err = pWriter->PutBoolean(chip::TLV::ContextTag(1), true); @@ -214,12 +214,10 @@ void TestWriteInteraction::TestWriteClient(nlTestSuite * apSuite, void * apConte CHIP_ERROR err = CHIP_NO_ERROR; - app::WriteClient writeClient; + TestWriteClientCallback callback; + app::WriteClient writeClient(&ctx.GetExchangeManager(), &callback, /* aTimedWriteTimeoutMs = */ NullOptional); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); - TestWriteClientCallback callback; - err = writeClient.Init(&ctx.GetExchangeManager(), &callback, /* aTimedWriteTimeoutMs = */ NullOptional); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); AddAttributeDataIB(apSuite, apContext, writeClient); err = writeClient.SendWriteRequest(ctx.GetSessionBobToAlice()); @@ -242,12 +240,10 @@ void TestWriteInteraction::TestWriteClientGroup(nlTestSuite * apSuite, void * ap CHIP_ERROR err = CHIP_NO_ERROR; - app::WriteClient writeClient; + TestWriteClientCallback callback; + app::WriteClient writeClient(&ctx.GetExchangeManager(), &callback, /* aTimedWriteTimeoutMs = */ NullOptional); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); - TestWriteClientCallback callback; - err = writeClient.Init(&ctx.GetExchangeManager(), &callback, /* aTimedWriteTimeoutMs = */ NullOptional); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); AddAttributeDataIB(apSuite, apContext, writeClient); SessionHandle groupSession = ctx.GetSessionBobToFriends(); @@ -333,9 +329,7 @@ void TestWriteInteraction::TestWriteRoundtripWithClusterObjects(nlTestSuite * ap err = engine->Init(&ctx.GetExchangeManager(), nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - app::WriteClient writeClient; - err = writeClient.Init(engine->GetExchangeManager(), &callback, Optional::Missing()); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + app::WriteClient writeClient(engine->GetExchangeManager(), &callback, Optional::Missing()); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); @@ -401,9 +395,7 @@ void TestWriteInteraction::TestWriteRoundtrip(nlTestSuite * apSuite, void * apCo err = engine->Init(&ctx.GetExchangeManager(), nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - app::WriteClient writeClient; - err = writeClient.Init(engine->GetExchangeManager(), &callback, Optional::Missing()); - NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + app::WriteClient writeClient(engine->GetExchangeManager(), &callback, Optional::Missing()); System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize); AddAttributeDataIB(apSuite, apContext, writeClient); diff --git a/src/app/tests/integration/chip_im_initiator.cpp b/src/app/tests/integration/chip_im_initiator.cpp index a7df44fa0b46bc..7e84ac02f8e64d 100644 --- a/src/app/tests/integration/chip_im_initiator.cpp +++ b/src/app/tests/integration/chip_im_initiator.cpp @@ -556,9 +556,8 @@ void WriteRequestTimerHandler(chip::System::Layer * systemLayer, void * appState if (gWriteRespCount < kMaxWriteMessageCount) { - chip::app::WriteClient writeClient; - err = writeClient.Init(chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager(), &gMockDelegate, - chip::Optional::Missing()); + chip::app::WriteClient writeClient(chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager(), &gMockDelegate, + chip::Optional::Missing()); SuccessOrExit(err); err = SendWriteRequest(writeClient); diff --git a/src/controller/WriteInteraction.h b/src/controller/WriteInteraction.h index 3651c1338d5784..09cc527f462515 100644 --- a/src/controller/WriteInteraction.h +++ b/src/controller/WriteInteraction.h @@ -101,14 +101,13 @@ CHIP_ERROR WriteAttribute(const SessionHandle & sessionHandle, chip::EndpointId WriteCallback::OnErrorCallbackType onErrorCb, const Optional & aTimedWriteTimeoutMs, WriteCallback::OnDoneCallbackType onDoneCb = nullptr) { - auto client = Platform::MakeUnique(); auto callback = Platform::MakeUnique(onSuccessCb, onErrorCb, onDoneCb); + auto client = Platform::MakeUnique(app::InteractionModelEngine::GetInstance()->GetExchangeManager(), + callback.get(), aTimedWriteTimeoutMs); + VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY); VerifyOrReturnError(client != nullptr, CHIP_ERROR_NO_MEMORY); - ReturnErrorOnFailure( - client->Init(app::InteractionModelEngine::GetInstance()->GetExchangeManager(), callback.get(), aTimedWriteTimeoutMs)); - if (sessionHandle->IsGroupSession()) { ReturnErrorOnFailure( diff --git a/src/controller/python/chip/clusters/attribute.cpp b/src/controller/python/chip/clusters/attribute.cpp index 55428d23991121..465448437aa7ee 100644 --- a/src/controller/python/chip/clusters/attribute.cpp +++ b/src/controller/python/chip/clusters/attribute.cpp @@ -256,14 +256,14 @@ chip::ChipError::StorageType pychip_WriteClient_WriteAttributes(void * appContex CHIP_ERROR err = CHIP_NO_ERROR; std::unique_ptr callback = std::make_unique(appContext); - std::unique_ptr client = std::make_unique(); + std::unique_ptr client = std::make_unique( + app::InteractionModelEngine::GetInstance()->GetExchangeManager(), callback.get(), + timedWriteTimeoutMs != 0 ? Optional(timedWriteTimeoutMs) : Optional::Missing()); va_list args; va_start(args, n); VerifyOrExit(device != nullptr && device->GetSecureSession().HasValue(), err = CHIP_ERROR_INCORRECT_STATE); - SuccessOrExit(client->Init(app::InteractionModelEngine::GetInstance()->GetExchangeManager(), callback.get(), - timedWriteTimeoutMs != 0 ? Optional(timedWriteTimeoutMs) : Optional::Missing())); { for (size_t i = 0; i < n; i++)