Skip to content

Commit

Permalink
Send status report in IM server side when server encounter the error
Browse files Browse the repository at this point in the history
When server don't have readhandler/subscribe handler, server would send
status report with RESOURCE_EXHAUSTED.
When server don't have writehandler/commandhandler, server would send
status report with BUSY.
For other error geneared when recieving incoming request in server,
server would send status report with FAILURE
  • Loading branch information
yunhanw-google committed Nov 11, 2021
1 parent d45cfaa commit 631efe9
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 153 deletions.
2 changes: 2 additions & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ static_library("app") {
"OperationalDeviceProxy.h",
"ReadClient.cpp",
"ReadHandler.cpp",
"StatusResponse.cpp",
"StatusResponse.h",
"WriteClient.cpp",
"WriteHandler.cpp",
"decoder.cpp",
Expand Down
30 changes: 25 additions & 5 deletions src/app/CommandSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "Command.h"
#include "CommandHandler.h"
#include "InteractionModelEngine.h"
#include "StatusResponse.h"
#include <protocols/Protocols.h>
#include <protocols/interaction_model/Constants.h>

Expand Down Expand Up @@ -91,12 +92,31 @@ CHIP_ERROR CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExcha
System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrExit(apExchangeContext == mpExchangeCtx, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse),
err = CHIP_ERROR_INVALID_MESSAGE_TYPE);

err = ProcessInvokeResponse(std::move(aPayload));
if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandResponse))
{
err = ProcessInvokeResponse(std::move(aPayload));
SuccessOrExit(err);
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::StatusResponse))
{
Protocols::InteractionModel::Status status;
err = StatusResponse::ProcessStatusResponse(apExchangeContext, std::move(aPayload), status);
mpExchangeCtx = nullptr;
SuccessOrExit(err);
if (status == Protocols::InteractionModel::Status::ResourceExhausted)
{
err = CHIP_ERROR_NO_MEMORY;
}
else
{
err = CHIP_ERROR_INCORRECT_STATE;
}
}
else
{
err = CHIP_ERROR_INVALID_MESSAGE_TYPE;
}

exit:
if (mpCallback != nullptr)
Expand Down
118 changes: 52 additions & 66 deletions src/app/InteractionModelEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien
return err;
}
}

return CHIP_ERROR_NO_MEMORY;
}

Expand Down Expand Up @@ -262,7 +261,6 @@ CHIP_ERROR InteractionModelEngine::NewWriteClient(WriteClientHandle & apWriteCli
{
continue;
}

ReturnLogErrorOnFailure(writeClient.Init(mpExchangeMgr, apCallback));
apWriteClient.SetWriteClient(&writeClient);
return CHIP_NO_ERROR;
Expand All @@ -271,51 +269,34 @@ CHIP_ERROR InteractionModelEngine::NewWriteClient(WriteClientHandle & apWriteCli
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogDetail(InteractionModel, "Msg type %d not supported", aPayloadHeader.GetMessageType());

// Todo: Add status report
// err = SendStatusReport(ec, kChipProfile_Common, kStatus_UnsupportedMessage);
// SuccessOrExit(err);

apExchangeContext = nullptr;

// Todo: Fix the below check after the above status report is implemented.
if (nullptr != apExchangeContext)
{
apExchangeContext->Abort();
}
return err;
}

void InteractionModelEngine::OnDone(CommandHandler & apCommandObj)
{
mCommandHandlerObjs.ReleaseObject(&apCommandObj);
}

CHIP_ERROR InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload)
System::PacketBufferHandle && aPayload,
Protocols::InteractionModel::Status & aStatus)
{
CommandHandler * commandHandler = mCommandHandlerObjs.CreateObject(this);
if (commandHandler == nullptr)
{
ChipLogProgress(InteractionModel, "no resource for Invoke interaction");
aStatus = Protocols::InteractionModel::Status::Busy;
return CHIP_ERROR_NO_MEMORY;
}
return commandHandler->OnInvokeCommandRequest(apExchangeContext, aPayloadHeader, std::move(aPayload));
ReturnErrorOnFailure(commandHandler->OnInvokeCommandRequest(apExchangeContext, aPayloadHeader, std::move(aPayload)));
aStatus = Protocols::InteractionModel::Status::Success;
return CHIP_NO_ERROR;
}

CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload,
ReadHandler::InteractionType aInteractionType)
ReadHandler::InteractionType aInteractionType,
Protocols::InteractionModel::Status & aStatus)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogDetail(InteractionModel, "Received %s request",
aInteractionType == ReadHandler::InteractionType::Subscribe ? "Subscribe" : "Read");

Expand All @@ -328,10 +309,10 @@ CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeConte
bool keepSubscriptions = true;
System::PacketBufferTLVReader reader;
reader.Init(aPayload.Retain());
SuccessOrExit(err = reader.Next());
ReturnErrorOnFailure(reader.Next());
SubscribeRequestMessage::Parser subscribeRequestParser;
SuccessOrExit(err = subscribeRequestParser.Init(reader));
err = subscribeRequestParser.GetKeepSubscriptions(&keepSubscriptions);
ReturnErrorOnFailure(subscribeRequestParser.Init(reader));
CHIP_ERROR err = subscribeRequestParser.GetKeepSubscriptions(&keepSubscriptions);
if (err == CHIP_NO_ERROR && !keepSubscriptions)
{
readHandler.Shutdown(ReadHandler::ShutdownOptions::AbortCurrentExchange);
Expand All @@ -343,54 +324,45 @@ CHIP_ERROR InteractionModelEngine::OnReadInitialRequest(Messaging::ExchangeConte
{
if (readHandler.IsFree())
{
err = readHandler.Init(mpExchangeMgr, mpDelegate, apExchangeContext, aInteractionType);
SuccessOrExit(err);
err = readHandler.OnReadInitialRequest(std::move(aPayload));
apExchangeContext = nullptr;
break;
ReturnErrorOnFailure(readHandler.Init(mpExchangeMgr, mpDelegate, apExchangeContext, aInteractionType));
ReturnErrorOnFailure(readHandler.OnReadInitialRequest(std::move(aPayload)));
aStatus = Protocols::InteractionModel::Status::Success;
return CHIP_NO_ERROR;
}
}

exit:
ChipLogProgress(InteractionModel, "no resource for %s interaction",
aInteractionType == ReadHandler::InteractionType::Subscribe ? "Subscribe" : "Read");
aStatus = Protocols::InteractionModel::Status::ResourceExhausted;

if (nullptr != apExchangeContext)
{
apExchangeContext->Abort();
}
return err;
return CHIP_NO_ERROR;
}

CHIP_ERROR InteractionModelEngine::OnWriteRequest(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload,
Protocols::InteractionModel::Status & aStatus)
{
CHIP_ERROR err = CHIP_NO_ERROR;

ChipLogDetail(InteractionModel, "Received Write request");

for (auto & writeHandler : mWriteHandlers)
{
if (writeHandler.IsFree())
{
err = writeHandler.Init(mpDelegate);
SuccessOrExit(err);
err = writeHandler.OnWriteRequest(apExchangeContext, std::move(aPayload));
apExchangeContext = nullptr;
break;
ReturnErrorOnFailure(writeHandler.Init(mpDelegate));
ReturnErrorOnFailure(writeHandler.OnWriteRequest(apExchangeContext, std::move(aPayload)));
aStatus = Protocols::InteractionModel::Status::Success;
return CHIP_NO_ERROR;
}
}

exit:

if (nullptr != apExchangeContext)
{
apExchangeContext->Abort();
}
return err;
ChipLogProgress(InteractionModel, "no resource for write interaction");
aStatus = Protocols::InteractionModel::Status::Busy;
return CHIP_NO_ERROR;
}

CHIP_ERROR InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload)
System::PacketBufferHandle && aPayload,
Protocols::InteractionModel::Status & aStatus)
{
System::PacketBufferTLVReader reader;
reader.Init(aPayload.Retain());
Expand Down Expand Up @@ -421,31 +393,45 @@ CHIP_ERROR InteractionModelEngine::OnUnsolicitedReportData(Messaging::ExchangeCo
CHIP_ERROR InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext,
const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Protocols::InteractionModel::Status status = Protocols::InteractionModel::Status::Failure;

if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandRequest))
{
return OnInvokeCommandRequest(apExchangeContext, aPayloadHeader, std::move(aPayload));
ReturnErrorOnFailure(OnInvokeCommandRequest(apExchangeContext, aPayloadHeader, std::move(aPayload), status));
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReadRequest))
{
return OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(aPayload), ReadHandler::InteractionType::Read);
ReturnErrorOnFailure(OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(aPayload),
ReadHandler::InteractionType::Read, status));
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::WriteRequest))
{
return OnWriteRequest(apExchangeContext, aPayloadHeader, std::move(aPayload));
ReturnErrorOnFailure(OnWriteRequest(apExchangeContext, aPayloadHeader, std::move(aPayload), status));
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::SubscribeRequest))
{
return OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(aPayload),
ReadHandler::InteractionType::Subscribe);
ReturnErrorOnFailure(OnReadInitialRequest(apExchangeContext, aPayloadHeader, std::move(aPayload),
ReadHandler::InteractionType::Subscribe, status));
}
else if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::ReportData))
{
return OnUnsolicitedReportData(apExchangeContext, aPayloadHeader, std::move(aPayload));
ReturnErrorOnFailure(OnUnsolicitedReportData(apExchangeContext, aPayloadHeader, std::move(aPayload), status));
}
else
{
return OnUnknownMsgType(apExchangeContext, aPayloadHeader, std::move(aPayload));
ChipLogProgress(InteractionModel, "Msg type %d not supported", aPayloadHeader.GetMessageType());
}

if (status != Protocols::InteractionModel::Status::Success)
{
err = StatusResponse::SendStatusResponse(status, apExchangeContext, false /*aExpectResponse*/);
if (err != CHIP_NO_ERROR)
{
apExchangeContext->Abort();
}
}
return err;
}

void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec)
Expand Down
15 changes: 6 additions & 9 deletions src/app/InteractionModelEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@
#include <app/InteractionModelDelegate.h>
#include <app/ReadClient.h>
#include <app/ReadHandler.h>
#include <app/StatusResponse.h>
#include <app/WriteClient.h>
#include <app/WriteHandler.h>
#include <app/reporting/Engine.h>
#include <app/util/basic-types.h>

namespace chip {
namespace app {

static constexpr size_t kMaxSecureSduLengthBytes = 1024;

/**
* @class InteractionModelEngine
*
Expand Down Expand Up @@ -199,10 +197,8 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman

void OnDone(CommandHandler & apCommandObj) override;

CHIP_ERROR OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);
CHIP_ERROR OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);
System::PacketBufferHandle && aPayload, Protocols::InteractionModel::Status & aStatus);
CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload) override;
void OnResponseTimeout(Messaging::ExchangeContext * ec) override;
Expand All @@ -213,20 +209,21 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate, public Comman
*/

CHIP_ERROR OnReadInitialRequest(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload, ReadHandler::InteractionType aInteractionType);
System::PacketBufferHandle && aPayload, ReadHandler::InteractionType aInteractionType,
Protocols::InteractionModel::Status & aStatus);

/**
* Called when Interaction Model receives a Write Request message. Errors processing
* the Write Request are handled entirely within this function.
*/
CHIP_ERROR OnWriteRequest(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);
System::PacketBufferHandle && aPayload, Protocols::InteractionModel::Status & aStatus);

/**This function handles processing of un-solicited ReportData messages on the client, which can
* only occur post subscription establishment
*/
CHIP_ERROR OnUnsolicitedReportData(Messaging::ExchangeContext * apExchangeContext, const PayloadHeader & aPayloadHeader,
System::PacketBufferHandle && aPayload);
System::PacketBufferHandle && aPayload, Protocols::InteractionModel::Status & aStatus);

void DispatchCommand(CommandHandler & apCommandObj, const ConcreteCommandPath & aCommandPath,
TLV::TLVReader & apPayload) override;
Expand Down
Loading

0 comments on commit 631efe9

Please sign in to comment.