Skip to content

Commit

Permalink
Move Init to private
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Jan 20, 2022
1 parent 2b63d89 commit 67dda87
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/app/WriteClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
namespace chip {
namespace app {

CHIP_ERROR WriteClient::Init(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback,
const Optional<uint16_t> & 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);
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 12 additions & 8 deletions src/app/WriteClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t> & aTimedWriteTimeoutMs);
WriteClient(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback,
const Optional<uint16_t> & aTimedWriteTimeoutMs) :
mpExchangeMgr(apExchangeMgr),
mpCallback(apCallback), mTimedWriteTimeoutMs(aTimedWriteTimeoutMs)
{}

/**
* Encode an attribute value that can be directly encoded using TLVWriter::Put
Expand Down Expand Up @@ -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
*/
Expand Down
22 changes: 7 additions & 15 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand Down Expand Up @@ -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<uint16_t>::Missing());
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
app::WriteClient writeClient(engine->GetExchangeManager(), &callback, Optional<uint16_t>::Missing());

System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);

Expand Down Expand Up @@ -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<uint16_t>::Missing());
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
app::WriteClient writeClient(engine->GetExchangeManager(), &callback, Optional<uint16_t>::Missing());

System::PacketBufferHandle buf = System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize);
AddAttributeDataIB(apSuite, apContext, writeClient);
Expand Down
5 changes: 2 additions & 3 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>::Missing());
chip::app::WriteClient writeClient(chip::app::InteractionModelEngine::GetInstance()->GetExchangeManager(), &gMockDelegate,
chip::Optional<uint16_t>::Missing());
SuccessOrExit(err);

err = SendWriteRequest(writeClient);
Expand Down
7 changes: 3 additions & 4 deletions src/controller/WriteInteraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ CHIP_ERROR WriteAttribute(const SessionHandle & sessionHandle, chip::EndpointId
WriteCallback::OnErrorCallbackType onErrorCb, const Optional<uint16_t> & aTimedWriteTimeoutMs,
WriteCallback::OnDoneCallbackType onDoneCb = nullptr)
{
auto client = Platform::MakeUnique<app::WriteClient>();
auto callback = Platform::MakeUnique<WriteCallback>(onSuccessCb, onErrorCb, onDoneCb);
auto client = Platform::MakeUnique<app::WriteClient>(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(
Expand Down
6 changes: 3 additions & 3 deletions src/controller/python/chip/clusters/attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ chip::ChipError::StorageType pychip_WriteClient_WriteAttributes(void * appContex
CHIP_ERROR err = CHIP_NO_ERROR;

std::unique_ptr<WriteClientCallback> callback = std::make_unique<WriteClientCallback>(appContext);
std::unique_ptr<WriteClient> client = std::make_unique<WriteClient>();
std::unique_ptr<WriteClient> client = std::make_unique<WriteClient>(
app::InteractionModelEngine::GetInstance()->GetExchangeManager(), callback.get(),
timedWriteTimeoutMs != 0 ? Optional<uint16_t>(timedWriteTimeoutMs) : Optional<uint16_t>::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<uint16_t>(timedWriteTimeoutMs) : Optional<uint16_t>::Missing()));

{
for (size_t i = 0; i < n; i++)
Expand Down

0 comments on commit 67dda87

Please sign in to comment.