Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Im/record callback in command sender #2

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/app/ClusterObjectCommandSenderCallback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <ClusterObjectCommandSenderCallback.h>

#include <app/CommandSender.h>
#include <app/common/StatusElement.h>
#include <app/data-model/Decode.h>

#include <functional>

namespace chip {
namespace app {
template <>
void ClusterObjectCommandSenderCallback<void>::OnResponse(const CommandSender * apCommandSender,
const CommandPath::Type & aCommandPath, TLV::TLVReader & aReader)
{
mOnResponse(apCommandSender, aCommandPath);
}

} // namespace app
} // namespace chip
85 changes: 85 additions & 0 deletions src/app/ClusterObjectCommandSenderCallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/CommandSender.h>
#include <app/common/StatusElement.h>
#include <app/data-model/Decode.h>

#include <functional>

namespace chip {
namespace app {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief description of what this class does.

template <typename ClusterObjectT>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
template <typename ClusterObjectT>
template <typename ResponseObjectType>

class ClusterObjectCommandSenderCallback final : public CommandSender::Delegate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still seems like a mouthful...any other name suggestions that are less wordy?

{
public:
template <typename ClusterObjectT2>
using OnResponseCallbackType<ClusterObjectT2> =
std::function<void(const CommandSender *, const CommandPath::Type &, const ClusterObjectT2 &)>;

template <>
using OnResponseCallbackType<void> = std::function<void(const CommandSender *, const CommandPath::Type &)>;

using OnErrorCallbackTyoe =
std::function<void(const CommandSender *, Protocols::InteractionModel::Status aIMStatus, CHIP_ERROR aError)>;

using OnFinalCallbackType = std::function<void(CommandSender *, ClusterObjectCommandSenderCallback<ClusterObjectT> *)>;

ClusterObjectCommandSenderCallback(OnResponseCallbackType<ClusterObjectT> aOnResponse, OnErrorCallbackTyoe aOnError,
OnFinalCallbackType aOnFinal) :
mOnResponse(aOnResponse),
mOnError(aOnError), mOnFinal(aOnFinal)
{}

void OnResponse(const CommandSender * apCommandSender, const CommandPath::Type & aCommandPath, TLV::TLVReader & aReader);
void OnError(const CommandSender * apCommandSender, Protocols::InteractionModel::Status aIMStatus, CHIP_ERROR aError)
{
mOnError(apCommandSender, aIMStatus, aError);
}
void OnFinal(CommandSender * apCommandSender) { mOnFinal(apCommandSender, this); }

private:
OnResponseCallbackType<ClusterObjectT> mOnResponse;
OnErrorCallbackTyoe mOnError;
OnFinalCallbackType mOnFinal;
};

template <typename ClusterObjectT>
void ClusterObjectCommandSenderCallback<ClusterObjectT>::OnResponse(const CommandSender * apCommandSender,
const CommandPath::Type & aCommandPath,
TLV::TLVReader & aReader)
{
ClusterObjectT response;
CHIP_ERROR err = DataModel::Decode(aReader, response);
if (err != CHIP_NO_ERROR)
{
mOnError(err);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other two arguments?

return;
}
mOnResponse(apCommandSender, aCommandPath, response);
}

template <>
void ClusterObjectCommandSenderCallback<void>::OnResponse(const CommandSender * apCommandSender,
const CommandPath::Type & aCommandPath, TLV::TLVReader & aReader);

} // namespace app
} // namespace chip
22 changes: 1 addition & 21 deletions src/app/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@
namespace chip {
namespace app {

CHIP_ERROR Command::Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate)
CHIP_ERROR Command::Init(Messaging::ExchangeManager * apExchangeMgr)
{
CHIP_ERROR err = CHIP_NO_ERROR;
// Error if already initialized.
VerifyOrExit(apExchangeMgr != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mpExchangeMgr == nullptr, err = CHIP_ERROR_INCORRECT_STATE);

mpExchangeMgr = apExchangeMgr;
mpDelegate = apDelegate;
err = Reset();
SuccessOrExit(err);

Expand Down Expand Up @@ -123,25 +122,6 @@ CHIP_ERROR Command::ProcessCommandMessage(System::PacketBufferHandle && payload,
return err;
}

void Command::Shutdown()
{
VerifyOrReturn(mState != CommandState::Uninitialized);
AbortExistingExchangeContext();
ShutdownInternal();
}

void Command::ShutdownInternal()
{
mCommandMessageWriter.Reset();

mpExchangeMgr = nullptr;
mpExchangeCtx = nullptr;
mpDelegate = nullptr;
ClearState();

mCommandIndex = 0;
}

CHIP_ERROR Command::PrepareCommand(const CommandPathParams & aCommandPathParams, bool aIsStatus)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
43 changes: 29 additions & 14 deletions src/app/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <app/MessageDef/CommandDataElement.h>
#include <app/MessageDef/CommandList.h>
#include <app/MessageDef/InvokeCommand.h>
#include <app/data-model/Encode.h>
#include <lib/core/CHIPCore.h>
#include <lib/support/BitFlags.h>
#include <lib/support/CodeUtils.h>
Expand All @@ -41,6 +42,8 @@
#include <system/SystemPacketBuffer.h>
#include <system/TLVPacketBufferBackingStore.h>

#include <functional>

namespace chip {
namespace app {

Expand Down Expand Up @@ -75,13 +78,10 @@ class Command
* @retval #CHIP_NO_ERROR On success.
*
*/
CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate);
CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr);

/**
* Shutdown the Command. This terminates this instance
* of the object and releases all held resources.
*/
void Shutdown();
template <typename ClusterObjectT>
CHIP_ERROR EncodeFullCommand(const CommandPathParams & aCommandPathParams, const ClusterObjectT & aPayload);

/**
* Finalize Command Message TLV Builder and finalize command message
Expand Down Expand Up @@ -125,23 +125,38 @@ class Command
void ClearState();
const char * GetStateStr() const;

/**
* Internal shutdown method that we use when we know what's going on with
* our exchange and don't need to manually close it.
*/
void ShutdownInternal();

InvokeCommand::Builder mInvokeCommandBuilder;
Messaging::ExchangeManager * mpExchangeMgr = nullptr;
Messaging::ExchangeContext * mpExchangeCtx = nullptr;
InteractionModelDelegate * mpDelegate = nullptr;
uint8_t mCommandIndex = 0;
CommandState mState = CommandState::Uninitialized;
chip::System::PacketBufferTLVWriter mCommandMessageWriter;

private:
friend class TestCommandInteraction;
TLV::TLVType mDataElementContainerType = TLV::kTLVType_NotSpecified;
chip::System::PacketBufferTLVWriter mCommandMessageWriter;
};

template <typename ClusterObjectT>
CHIP_ERROR Command::EncodeFullCommand(const CommandPathParams & aCommandPathParams, const ClusterObjectT & aPayload)
{
CHIP_ERROR err = CHIP_NO_ERROR;
CommandDataElement::Builder commandDataElement;

VerifyOrReturnError(mState == CommandState::Initialized || mState == CommandState::AddCommand,
err = CHIP_ERROR_INCORRECT_STATE);

commandDataElement = mInvokeCommandBuilder.GetCommandListBuilder().CreateCommandDataElementBuilder();
ReturnErrorOnFailure(commandDataElement.GetError());
ReturnErrorOnFailure(ConstructCommandPath(aCommandPathParams, commandDataElement));
ReturnErrorOnFailure(
DataModel::Encode(*commandDataElement.GetWriter(), TLV::ContextTag(CommandDataElement::kCsTag_Data), aPayload));

commandDataElement.EndOfCommandDataElement();
ReturnErrorOnFailure(commandDataElement.GetError());
MoveToState(CommandState::AddCommand);
return CHIP_NO_ERROR;
}

} // namespace app
} // namespace chip
18 changes: 18 additions & 0 deletions src/app/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,23 @@ CHIP_ERROR CommandHandler::AddStatusCode(const CommandPathParams & aCommandPathP
return err;
}

void CommandHandler::ShutdownInternal()
{
mCommandMessageWriter.Reset();

mpExchangeMgr = nullptr;
mpExchangeCtx = nullptr;
ClearState();

mCommandIndex = 0;
}

void CommandHandler::Shutdown()
{
VerifyOrReturn(mState != CommandState::Uninitialized);
AbortExistingExchangeContext();
ShutdownInternal();
}

} // namespace app
} // namespace chip
2 changes: 2 additions & 0 deletions src/app/CommandHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class CommandHandler : public Command
CHIP_ERROR AddStatusCode(const CommandPathParams & aCommandPathParams,
const Protocols::SecureChannel::GeneralStatusCode aGeneralCode, const Protocols::Id aProtocolId,
const Protocols::InteractionModel::Status aStatus) override;
void Shutdown();

private:
friend class TestCommandInteraction;
void ShutdownInternal();
CHIP_ERROR SendCommandResponse();
CHIP_ERROR ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement) override;
};
Expand Down
Loading