Skip to content

Commit

Permalink
[IM] Move ReadClient callbacks to separate callback class (#10963)
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing authored Nov 1, 2021
1 parent 5da5e79 commit 68e4b18
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 295 deletions.
64 changes: 0 additions & 64 deletions src/app/InteractionModelDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,62 +50,6 @@ class ReadHandler;
class InteractionModelDelegate
{
public:
/**
* Notification that the interaction model has received a list of events in response to a Read request and that list
* of events needs to be processed.
* @param[in] apExchangeContext An exchange context that represents the exchange the Report Data came in on.
* This can be used to recover the NodeId of the node that sent the Report Data.
* It is managed externally and should not be closed by the SDK consumer.
* @param[in] apEventListReader TLV reader positioned at the list that contains the events. The
* implementation of EventStreamReceived is expected to call Next() on the reader to
* advance it to the first element of the list, then process the elements from beginning to the
* end. The callee is expected to consume all events.
*
* @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented
*/
virtual CHIP_ERROR EventStreamReceived(const Messaging::ExchangeContext * apExchangeContext, TLV::TLVReader * apEventListReader)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

/**
* Notification that the interaction model has received a list of attribute data in response to a Read request. apData might be
* nullptr if status is not Status::Success.
*
* @param[in] apReadClient The read client object, the application can use GetAppIdentifier() for the read client to
* distinguish different read requests.
* @param[in] aPath The path of the attribute, contains node id, endpoint id, cluster id, field id etc.
* @param[in] apData The attribute data TLV
* @param[in] status Interaction model status code
*
*/
virtual void OnReportData(const ReadClient * apReadClient, const ClusterInfo & aPath, TLV::TLVReader * apData,
Protocols::InteractionModel::Status status)
{}

/**
* Notification that the last message for a Report Data action for the given ReadClient has been received and processed.
* @param[in] apReadClient A current readClient which can identify the read to the consumer, particularly during
* multiple read interactions
* @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented
*/
virtual CHIP_ERROR ReportProcessed(const ReadClient * apReadClient) { return CHIP_ERROR_NOT_IMPLEMENTED; }

/**
* Notification that a read attempt encountered an asynchronous failure.
* @param[in] apReadClient A current readClient which can identify the read to the consumer, particularly during
* multiple read interactions
* @param[in] aError A error that could be CHIP_ERROR_TIMEOUT when read client fails to receive, or other error when
* fail to process report data.
* @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented
*/
virtual CHIP_ERROR ReadError(ReadClient * apReadClient, CHIP_ERROR aError) { return CHIP_ERROR_NOT_IMPLEMENTED; }

/**
* Notification that a Subscribe Response has been processed and application can do further work .
*/
virtual CHIP_ERROR SubscribeResponseProcessed(const ReadClient * apReadClient) { return CHIP_ERROR_NOT_IMPLEMENTED; }

/**
* Notification that Subscription has been established successfully and application can do further work in handler.
*/
Expand All @@ -116,14 +60,6 @@ class InteractionModelDelegate
*/
virtual CHIP_ERROR SubscriptionTerminated(const ReadHandler * apReadHandler) { return CHIP_ERROR_NOT_IMPLEMENTED; }

/**
* Notification that a read interaction was completed on the client successfully.
* @param[in] apReadClient A current read client which can identify the read client to the consumer, particularly
* during multiple read interactions
* @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented
*/
virtual CHIP_ERROR ReadDone(ReadClient * apReadClient) { return CHIP_ERROR_NOT_IMPLEMENTED; }

virtual ~InteractionModelDelegate() = default;
};

Expand Down
18 changes: 6 additions & 12 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,8 @@ void InteractionModelEngine::Shutdown()
}

CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClient, ReadClient::InteractionType aInteractionType,
uint64_t aAppIdentifier, InteractionModelDelegate * apDelegateOverride)
ReadClient::Callback * aCallback)
{

if (apDelegateOverride == nullptr)
{
apDelegateOverride = mpDelegate;
}

*apReadClient = nullptr;

for (auto & readClient : mReadClients)
Expand All @@ -138,7 +132,7 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien
CHIP_ERROR err;

*apReadClient = &readClient;
err = readClient.Init(mpExchangeMgr, apDelegateOverride, aInteractionType, aAppIdentifier);
err = readClient.Init(mpExchangeMgr, aCallback, aInteractionType);
if (CHIP_NO_ERROR != err)
{
*apReadClient = nullptr;
Expand Down Expand Up @@ -428,11 +422,11 @@ void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec)
ChipLogValueExchange(ec));
}

CHIP_ERROR InteractionModelEngine::SendReadRequest(ReadPrepareParams & aReadPrepareParams, uint64_t aAppIdentifier)
CHIP_ERROR InteractionModelEngine::SendReadRequest(ReadPrepareParams & aReadPrepareParams, ReadClient::Callback * aCallback)
{
ReadClient * client = nullptr;
CHIP_ERROR err = CHIP_NO_ERROR;
ReturnErrorOnFailure(NewReadClient(&client, ReadClient::InteractionType::Read, aAppIdentifier));
ReturnErrorOnFailure(NewReadClient(&client, ReadClient::InteractionType::Read, aCallback));
err = client->SendReadRequest(aReadPrepareParams);
if (err != CHIP_NO_ERROR)
{
Expand All @@ -441,10 +435,10 @@ CHIP_ERROR InteractionModelEngine::SendReadRequest(ReadPrepareParams & aReadPrep
return err;
}

CHIP_ERROR InteractionModelEngine::SendSubscribeRequest(ReadPrepareParams & aReadPrepareParams, uint64_t aAppIdentifier)
CHIP_ERROR InteractionModelEngine::SendSubscribeRequest(ReadPrepareParams & aReadPrepareParams, ReadClient::Callback * aCallback)
{
ReadClient * client = nullptr;
ReturnErrorOnFailure(NewReadClient(&client, ReadClient::InteractionType::Subscribe, aAppIdentifier));
ReturnErrorOnFailure(NewReadClient(&client, ReadClient::InteractionType::Subscribe, aCallback));
ReturnErrorOnFailure(client->SendSubscribeRequest(aReadPrepareParams));
return CHIP_NO_ERROR;
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman
* @retval #CHIP_ERROR_NO_MEMORY If there is no ReadClient available
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR SendReadRequest(ReadPrepareParams & aReadPrepareParams, uint64_t aAppIdentifier = 0);
CHIP_ERROR SendReadRequest(ReadPrepareParams & aReadPrepareParams, ReadClient::Callback * aCallback);

/**
* Creates a new read client and sends SubscribeRequest message to the node using the read client.
Expand All @@ -110,7 +110,7 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman
* @retval #CHIP_ERROR_NO_MEMORY If there is no ReadClient available
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR SendSubscribeRequest(ReadPrepareParams & aReadPrepareParams, uint64_t aAppIdentifier = 0);
CHIP_ERROR SendSubscribeRequest(ReadPrepareParams & aReadPrepareParams, ReadClient::Callback * aCallback);

/**
* Tears down an active subscription.
Expand Down Expand Up @@ -151,7 +151,7 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR NewReadClient(ReadClient ** const apReadClient, ReadClient::InteractionType aInteractionType,
uint64_t aAppIdentifier, InteractionModelDelegate * apDelegateOverride = nullptr);
ReadClient::Callback * aCallback);

uint32_t GetNumActiveReadHandlers() const;
uint32_t GetNumActiveReadClients() const;
Expand Down
44 changes: 20 additions & 24 deletions src/app/ReadClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
namespace chip {
namespace app {

CHIP_ERROR ReadClient::Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate,
InteractionType aInteractionType, uint64_t aAppIdentifier)
CHIP_ERROR ReadClient::Init(Messaging::ExchangeManager * apExchangeMgr, Callback * apCallback, InteractionType aInteractionType)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Error if already initialized.
VerifyOrExit(IsFree(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(apExchangeMgr != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeMgr == nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
mpExchangeMgr = apExchangeMgr;
mpDelegate = apDelegate;
mpCallback = apCallback;
mState = ClientState::Initialized;
mAppIdentifier = aAppIdentifier;
mMinIntervalFloorSeconds = 0;
mMaxIntervalCeilingSeconds = 0;
mSubscriptionId = 0;
Expand All @@ -60,17 +58,14 @@ void ReadClient::Shutdown()

void ReadClient::ShutdownInternal(CHIP_ERROR aError)
{
if (mpDelegate != nullptr)
if (mpCallback != nullptr)
{
if (aError != CHIP_NO_ERROR)
{
mpDelegate->ReadError(this, aError);
mpCallback->OnError(this, aError);
}
else
{
mpDelegate->ReadDone(this);
}
mpDelegate = nullptr;
mpCallback->OnDone(this);
mpCallback = nullptr;
}
if (IsSubscriptionType())
{
Expand Down Expand Up @@ -122,7 +117,7 @@ CHIP_ERROR ReadClient::SendReadRequest(ReadPrepareParams & aReadPrepareParams)
ChipLogDetail(DataManagement, "%s: Client[%u] [%5.5s]", __func__,
InteractionModelEngine::GetInstance()->GetReadClientArrayIndex(this), GetStateStr());
VerifyOrExit(ClientState::Initialized == mState, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpCallback != nullptr, err = CHIP_ERROR_INCORRECT_STATE);

// Discard any existing exchange context. Effectively we can only have one exchange per ReadClient
// at any one time.
Expand Down Expand Up @@ -290,7 +285,7 @@ CHIP_ERROR ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchange
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit(!IsFree(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpCallback != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData))
{
err = ProcessReportData(std::move(aPayload));
Expand Down Expand Up @@ -411,12 +406,11 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
}
SuccessOrExit(err);

if (isEventListPresent && nullptr != mpDelegate)
if (isEventListPresent && nullptr != mpCallback)
{
chip::TLV::TLVReader eventListReader;
eventList.GetReader(&eventListReader);
err = mpDelegate->EventStreamReceived(mpExchangeCtx, &eventListReader);
SuccessOrExit(err);
mpCallback->OnEventData(this, eventListReader);
}

err = report.GetAttributeDataList(&attributeDataList);
Expand All @@ -426,7 +420,7 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
err = CHIP_NO_ERROR;
}
SuccessOrExit(err);
if (isAttributeDataListPresent && nullptr != mpDelegate && !moreChunkedMessages)
if (isAttributeDataListPresent && nullptr != mpCallback && !moreChunkedMessages)
{
chip::TLV::TLVReader attributeDataListReader;
attributeDataList.GetReader(&attributeDataListReader);
Expand All @@ -440,10 +434,6 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload)
// are multiple reports
}

if (err == CHIP_NO_ERROR)
{
mpDelegate->ReportProcessed(this);
}
exit:
SendStatusResponse(err);
if (!mInitialReport)
Expand Down Expand Up @@ -524,7 +514,9 @@ CHIP_ERROR ReadClient::ProcessAttributeDataList(TLV::TLVReader & aAttributeDataL
{
ExitNow();
}
mpDelegate->OnReportData(this, clusterInfo, &dataReader, status);
mpCallback->OnAttributeData(
this, ConcreteAttributePath(clusterInfo.mEndpointId, clusterInfo.mClusterId, clusterInfo.mFieldId),
status == Protocols::InteractionModel::Status::Success ? &dataReader : nullptr, StatusIB(status));
}

if (CHIP_END_OF_TLV == err)
Expand Down Expand Up @@ -591,7 +583,11 @@ CHIP_ERROR ReadClient::ProcessSubscribeResponse(System::PacketBufferHandle && aP
VerifyOrReturnLogError(IsMatchingClient(subscriptionId), CHIP_ERROR_INVALID_ARGUMENT);
ReturnLogErrorOnFailure(subscribeResponse.GetMinIntervalFloorSeconds(&mMinIntervalFloorSeconds));
ReturnLogErrorOnFailure(subscribeResponse.GetMaxIntervalCeilingSeconds(&mMaxIntervalCeilingSeconds));
mpDelegate->SubscribeResponseProcessed(this);

if (mpCallback != nullptr)
{
mpCallback->OnSubscriptionEstablished(this);
}

MoveToState(ClientState::SubscriptionActive);

Expand All @@ -606,7 +602,7 @@ CHIP_ERROR ReadClient::SendSubscribeRequest(ReadPrepareParams & aReadPreparePara
SubscribeRequest::Builder request;
VerifyOrExit(ClientState::Initialized == mState, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpCallback != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
msgBuf = System::PacketBufferHandle::New(kMaxSecureSduLengthBytes);
VerifyOrExit(!msgBuf.IsNull(), err = CHIP_ERROR_NO_MEMORY);

Expand Down
Loading

0 comments on commit 68e4b18

Please sign in to comment.