3 * Copyright (c) 2020-2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file defines Base class for a CHIP IM Command
27 #include <core/CHIPCore.h>
28 #include <messaging/ExchangeContext.h>
29 #include <messaging/ExchangeMgr.h>
30 #include <messaging/Flags.h>
31 #include <protocols/Protocols.h>
32 #include <support/BitFlags.h>
33 #include <support/CodeUtils.h>
34 #include <support/DLLUtil.h>
35 #include <support/logging/CHIPLogging.h>
36 #include <system/SystemPacketBuffer.h>
37 #include <system/TLVPacketBufferBackingStore.h>
39 #include <app/InteractionModelDelegate.h>
40 #include <app/MessageDef/CommandDataElement.h>
41 #include <app/MessageDef/CommandList.h>
42 #include <app/MessageDef/InvokeCommand.h>
50 enum class CommandRoleId
56 enum class CommandState
58 Uninitialized = 0, //< The invoke command message has not been initialized
59 Initialized, //< The invoke command message has been initialized and is ready
60 AddCommand, //< The invoke command message has added Command
61 Sending, //< The invoke command message has sent out the invoke command
64 enum class CommandPathFlags : uint8_t
66 kEndpointIdValid = 0x01, /**< Set when the EndpointId field is valid */
67 kGroupIdValid = 0x02, /**< Set when the GroupId field is valid */
71 * Encapsulates arguments to be passed into SendCommand().
76 CommandParams(chip::EndpointId endpointId, chip::GroupId groupId, chip::ClusterId clusterId, chip::CommandId commandId,
77 const BitFlags<CommandPathFlags> & flags) :
78 EndpointId(endpointId),
79 GroupId(groupId), ClusterId(clusterId), CommandId(commandId), Flags(flags)
82 chip::EndpointId EndpointId;
83 chip::GroupId GroupId;
84 chip::ClusterId ClusterId;
85 chip::CommandId CommandId;
86 BitFlags<CommandPathFlags> Flags;
90 * Initialize the Command object. Within the lifetime
91 * of this instance, this method is invoked once after object
92 * construction until a call to Shutdown is made to terminate the
95 * @param[in] apExchangeMgr A pointer to the ExchangeManager object.
96 * @param[in] apDelegate InteractionModelDelegate set by application.
98 * @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
99 * CommandState::NotInitialized.
100 * @retval #CHIP_NO_ERROR On success.
103 CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate);
106 * Shutdown the CommandSender. This terminates this instance
107 * of the object and releases all held resources.
113 * Finalize Command Message TLV Builder and finalize command message
118 CHIP_ERROR FinalizeCommandsMessage();
120 chip::TLV::TLVWriter & CreateCommandDataElementTLVWriter();
121 CHIP_ERROR AddCommand(chip::EndpointId aEndpintId, chip::GroupId aGroupId, chip::ClusterId aClusterId,
122 chip::CommandId aCommandId, BitFlags<CommandPathFlags> Flags);
123 CHIP_ERROR AddCommand(CommandParams & aCommandParams);
124 CHIP_ERROR AddStatusCode(const Protocols::SecureChannel::GeneralStatusCode aGeneralCode, Protocols::Id aProtocolId,
125 const uint16_t aProtocolCode);
128 * Gets the inner exchange context object, without ownership.
130 * @return The inner exchange context, might be nullptr if no
131 * exchange context has been assigned or the context
134 const Messaging::ExchangeContext * GetExchangeContext() const { return mpExchangeCtx; }
138 virtual ~Command() = default;
140 bool IsFree() const { return (nullptr == mpExchangeCtx); };
141 virtual CHIP_ERROR ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement) = 0;
144 CHIP_ERROR ClearExistingExchangeContext();
145 void MoveToState(const CommandState aTargetState);
146 CHIP_ERROR ProcessCommandMessage(System::PacketBufferHandle && payload, CommandRoleId aCommandRoleId);
148 const char * GetStateStr() const;
150 Messaging::ExchangeManager * mpExchangeMgr = nullptr;
151 Messaging::ExchangeContext * mpExchangeCtx = nullptr;
152 InteractionModelDelegate * mpDelegate = nullptr;
153 chip::System::PacketBufferHandle mCommandMessageBuf;
154 uint8_t mCommandIndex = 0;
157 friend class TestCommandInteraction;
158 chip::System::PacketBufferHandle mpBufHandle;
159 InvokeCommand::Builder mInvokeCommandBuilder;
162 chip::System::PacketBufferHandle mCommandDataBuf;
163 chip::System::PacketBufferTLVWriter mCommandMessageWriter;
164 chip::System::PacketBufferTLVWriter mCommandDataWriter;