3 * Copyright (c) 2020 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 object for a CHIP IM Invoke Command Handler
25 #include "CommandHandler.h"
27 #include "CommandSender.h"
28 #include "InteractionModelEngine.h"
30 #include <protocols/secure_channel/Constants.h>
32 using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;
36 void CommandHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader,
37 const PayloadHeader & payloadHeader, System::PacketBufferHandle payload)
39 CHIP_ERROR err = CHIP_NO_ERROR;
40 System::PacketBufferHandle response;
42 // NOTE: we already know this is an InvokeCommand Request message because we explicitly registered with the
43 // Exchange Manager for unsolicited InvokeCommand Requests.
47 err = ProcessCommandMessage(std::move(payload), CommandRoleId::HandlerId);
50 SendCommandResponse();
53 ChipLogFunctError(err);
56 CHIP_ERROR CommandHandler::SendCommandResponse()
58 CHIP_ERROR err = CHIP_NO_ERROR;
60 err = FinalizeCommandsMessage();
63 VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
64 err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::InvokeCommandResponse, std::move(mCommandMessageBuf),
65 Messaging::SendFlags(Messaging::SendMessageFlags::kNone));
68 MoveToState(CommandState::Sending);
72 ChipLogFunctError(err);
76 CHIP_ERROR CommandHandler::ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement)
78 CHIP_ERROR err = CHIP_NO_ERROR;
79 CommandPath::Parser commandPath;
80 chip::TLV::TLVReader commandDataReader;
81 chip::ClusterId clusterId;
82 chip::CommandId commandId;
83 chip::EndpointId endpointId;
85 ReturnErrorOnFailure(aCommandElement.GetCommandPath(&commandPath));
86 ReturnErrorOnFailure(commandPath.GetClusterId(&clusterId));
87 ReturnErrorOnFailure(commandPath.GetCommandId(&commandId));
88 ReturnErrorOnFailure(commandPath.GetEndpointId(&endpointId));
90 err = aCommandElement.GetData(&commandDataReader);
91 if (CHIP_END_OF_TLV == err)
93 // Empty Command, Add status code in invoke command response, notify cluster handler to hand it further.
95 ChipLogDetail(DataManagement, "Add Status code for empty command, cluster Id is %d", clusterId);
96 AddStatusCode(GeneralStatusCode::kSuccess, Protocols::SecureChannel::Id, Protocols::SecureChannel::kProtocolCodeSuccess);
98 else if (CHIP_NO_ERROR == err)
100 DispatchSingleClusterCommand(clusterId, commandId, endpointId, commandDataReader, this);