Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / ReadHandler.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 read handler for a CHIP Interaction Data model
22  *
23  */
24
25 #include <app/InteractionModelEngine.h>
26 #include <app/MessageDef/EventPath.h>
27 #include <app/ReadHandler.h>
28
29 namespace chip {
30 namespace app {
31 CHIP_ERROR ReadHandler::Init(InteractionModelDelegate * apDelegate)
32 {
33     CHIP_ERROR err = CHIP_NO_ERROR;
34     // Error if already initialized.
35     VerifyOrExit(apDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
36     VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE);
37
38     mpExchangeCtx     = nullptr;
39     mpDelegate        = apDelegate;
40     mSuppressResponse = true;
41     mGetToAllEvents   = true;
42     MoveToState(HandlerState::Initialized);
43
44 exit:
45     ChipLogFunctError(err);
46     return err;
47 }
48
49 void ReadHandler::Shutdown()
50 {
51     ClearExistingExchangeContext();
52     MoveToState(HandlerState::Uninitialized);
53     mpDelegate = nullptr;
54 }
55
56 CHIP_ERROR ReadHandler::ClearExistingExchangeContext()
57 {
58     if (mpExchangeCtx != nullptr)
59     {
60         mpExchangeCtx->Abort();
61         mpExchangeCtx = nullptr;
62     }
63
64     return CHIP_NO_ERROR;
65 }
66
67 CHIP_ERROR ReadHandler::OnReadRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle aPayload)
68 {
69     CHIP_ERROR err = CHIP_NO_ERROR;
70     System::PacketBufferHandle response;
71
72     mpExchangeCtx = apExchangeContext;
73
74     err = ProcessReadRequest(std::move(aPayload));
75     SuccessOrExit(err);
76
77 exit:
78     if (err != CHIP_NO_ERROR)
79     {
80         ChipLogFunctError(err);
81         Shutdown();
82     }
83
84     return err;
85 }
86
87 CHIP_ERROR ReadHandler::SendReportData(System::PacketBufferHandle aPayload)
88 {
89     CHIP_ERROR err = CHIP_NO_ERROR;
90     VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
91
92     err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::ReportData, std::move(aPayload),
93                                      Messaging::SendFlags(Messaging::SendMessageFlags::kNone));
94     SuccessOrExit(err);
95
96     // Todo: Once status report support is added, we would shutdown only when there is error or when this is last chunk of report
97 exit:
98     Shutdown();
99     return err;
100 }
101
102 CHIP_ERROR ReadHandler::ProcessReadRequest(System::PacketBufferHandle aPayload)
103 {
104     CHIP_ERROR err = CHIP_NO_ERROR;
105     System::PacketBufferTLVReader reader;
106
107     ReadRequest::Parser readRequestParser;
108     EventPathList::Parser eventPathListParser;
109     TLV::TLVReader eventPathListReader;
110
111     reader.Init(std::move(aPayload));
112
113     err = reader.Next();
114     SuccessOrExit(err);
115
116     err = readRequestParser.Init(reader);
117     SuccessOrExit(err);
118
119     err = readRequestParser.CheckSchemaValidity();
120     SuccessOrExit(err);
121
122     err = readRequestParser.GetEventPathList(&eventPathListParser);
123     if (err == CHIP_END_OF_TLV)
124     {
125         err = CHIP_NO_ERROR;
126     }
127     else
128     {
129         SuccessOrExit(err);
130         eventPathListParser.GetReader(&eventPathListReader);
131
132         while (CHIP_NO_ERROR == (err = eventPathListReader.Next()))
133         {
134             VerifyOrExit(TLV::AnonymousTag == eventPathListReader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
135
136             EventPath::Parser eventPath;
137
138             err = eventPath.Init(eventPathListReader);
139             SuccessOrExit(err);
140             // TODO: Pass event path to report engine to generate report with interested events
141         }
142     }
143
144     // if we have exhausted this container
145     if (CHIP_END_OF_TLV == err)
146     {
147         err = CHIP_NO_ERROR;
148     }
149
150 exit:
151     ChipLogFunctError(err);
152     return err;
153 }
154
155 const char * ReadHandler::GetStateStr() const
156 {
157 #if CHIP_DETAIL_LOGGING
158     switch (mState)
159     {
160     case HandlerState::Uninitialized:
161         return "Uninitialized";
162
163     case HandlerState::Initialized:
164         return "Initialized";
165
166     case HandlerState::Reportable:
167         return "Reportable";
168     }
169 #endif // CHIP_DETAIL_LOGGING
170     return "N/A";
171 }
172
173 void ReadHandler::MoveToState(const HandlerState aTargetState)
174 {
175     mState = aTargetState;
176     ChipLogDetail(DataManagement, "IM RH moving to [%s]", GetStateStr());
177 }
178
179 } // namespace app
180 } // namespace chip