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 read handler for a CHIP Interaction Data model
25 #include <app/InteractionModelEngine.h>
26 #include <app/MessageDef/EventPath.h>
27 #include <app/ReadHandler.h>
31 CHIP_ERROR ReadHandler::Init(InteractionModelDelegate * apDelegate)
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);
38 mpExchangeCtx = nullptr;
39 mpDelegate = apDelegate;
40 mSuppressResponse = true;
41 mGetToAllEvents = true;
42 MoveToState(HandlerState::Initialized);
45 ChipLogFunctError(err);
49 void ReadHandler::Shutdown()
51 ClearExistingExchangeContext();
52 MoveToState(HandlerState::Uninitialized);
56 CHIP_ERROR ReadHandler::ClearExistingExchangeContext()
58 if (mpExchangeCtx != nullptr)
60 mpExchangeCtx->Abort();
61 mpExchangeCtx = nullptr;
67 CHIP_ERROR ReadHandler::OnReadRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle aPayload)
69 CHIP_ERROR err = CHIP_NO_ERROR;
70 System::PacketBufferHandle response;
72 mpExchangeCtx = apExchangeContext;
74 err = ProcessReadRequest(std::move(aPayload));
78 if (err != CHIP_NO_ERROR)
80 ChipLogFunctError(err);
87 CHIP_ERROR ReadHandler::SendReportData(System::PacketBufferHandle aPayload)
89 CHIP_ERROR err = CHIP_NO_ERROR;
90 VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
92 err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::ReportData, std::move(aPayload),
93 Messaging::SendFlags(Messaging::SendMessageFlags::kNone));
96 // Todo: Once status report support is added, we would shutdown only when there is error or when this is last chunk of report
102 CHIP_ERROR ReadHandler::ProcessReadRequest(System::PacketBufferHandle aPayload)
104 CHIP_ERROR err = CHIP_NO_ERROR;
105 System::PacketBufferTLVReader reader;
107 ReadRequest::Parser readRequestParser;
108 EventPathList::Parser eventPathListParser;
109 TLV::TLVReader eventPathListReader;
111 reader.Init(std::move(aPayload));
116 err = readRequestParser.Init(reader);
119 err = readRequestParser.CheckSchemaValidity();
122 err = readRequestParser.GetEventPathList(&eventPathListParser);
123 if (err == CHIP_END_OF_TLV)
130 eventPathListParser.GetReader(&eventPathListReader);
132 while (CHIP_NO_ERROR == (err = eventPathListReader.Next()))
134 VerifyOrExit(TLV::AnonymousTag == eventPathListReader.GetTag(), err = CHIP_ERROR_INVALID_TLV_TAG);
136 EventPath::Parser eventPath;
138 err = eventPath.Init(eventPathListReader);
140 // TODO: Pass event path to report engine to generate report with interested events
144 // if we have exhausted this container
145 if (CHIP_END_OF_TLV == err)
151 ChipLogFunctError(err);
155 const char * ReadHandler::GetStateStr() const
157 #if CHIP_DETAIL_LOGGING
160 case HandlerState::Uninitialized:
161 return "Uninitialized";
163 case HandlerState::Initialized:
164 return "Initialized";
166 case HandlerState::Reportable:
169 #endif // CHIP_DETAIL_LOGGING
173 void ReadHandler::MoveToState(const HandlerState aTargetState)
175 mState = aTargetState;
176 ChipLogDetail(DataManagement, "IM RH moving to [%s]", GetStateStr());