Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / CommandHandler.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file defines object for a CHIP IM Invoke Command Handler
22  *
23  */
24
25 #include "CommandHandler.h"
26 #include "Command.h"
27 #include "CommandSender.h"
28 #include "InteractionModelEngine.h"
29
30 #include <protocols/secure_channel/Constants.h>
31
32 using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode;
33
34 namespace chip {
35 namespace app {
36 void CommandHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader,
37                                        const PayloadHeader & payloadHeader, System::PacketBufferHandle payload)
38 {
39     CHIP_ERROR err = CHIP_NO_ERROR;
40     System::PacketBufferHandle response;
41
42     // NOTE: we already know this is an InvokeCommand Request message because we explicitly registered with the
43     // Exchange Manager for unsolicited InvokeCommand Requests.
44
45     mpExchangeCtx = ec;
46
47     err = ProcessCommandMessage(std::move(payload), CommandRoleId::HandlerId);
48     SuccessOrExit(err);
49
50     SendCommandResponse();
51
52 exit:
53     ChipLogFunctError(err);
54 }
55
56 CHIP_ERROR CommandHandler::SendCommandResponse()
57 {
58     CHIP_ERROR err = CHIP_NO_ERROR;
59
60     err = FinalizeCommandsMessage();
61     SuccessOrExit(err);
62
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));
66     SuccessOrExit(err);
67
68     MoveToState(CommandState::Sending);
69
70 exit:
71     Shutdown();
72     ChipLogFunctError(err);
73     return err;
74 }
75
76 CHIP_ERROR CommandHandler::ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement)
77 {
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;
84
85     ReturnErrorOnFailure(aCommandElement.GetCommandPath(&commandPath));
86     ReturnErrorOnFailure(commandPath.GetClusterId(&clusterId));
87     ReturnErrorOnFailure(commandPath.GetCommandId(&commandId));
88     ReturnErrorOnFailure(commandPath.GetEndpointId(&endpointId));
89
90     err = aCommandElement.GetData(&commandDataReader);
91     if (CHIP_END_OF_TLV == err)
92     {
93         // Empty Command, Add status code in invoke command response, notify cluster handler to hand it further.
94         err = CHIP_NO_ERROR;
95         ChipLogDetail(DataManagement, "Add Status code for empty command, cluster Id is %d", clusterId);
96         AddStatusCode(GeneralStatusCode::kSuccess, Protocols::SecureChannel::Id, Protocols::SecureChannel::kProtocolCodeSuccess);
97     }
98     else if (CHIP_NO_ERROR == err)
99     {
100         DispatchSingleClusterCommand(clusterId, commandId, endpointId, commandDataReader, this);
101     }
102
103     return err;
104 }
105 } // namespace app
106 } // namespace chip