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
27 #include <app/InteractionModelDelegate.h>
28 #include <core/CHIPCore.h>
29 #include <core/CHIPTLVDebug.hpp>
30 #include <messaging/ExchangeContext.h>
31 #include <messaging/ExchangeMgr.h>
32 #include <messaging/Flags.h>
33 #include <protocols/Protocols.h>
34 #include <support/CodeUtils.h>
35 #include <support/DLLUtil.h>
36 #include <support/logging/CHIPLogging.h>
37 #include <system/SystemPacketBuffer.h>
44 * @brief The read handler is responsible for processing a read request, asking the attribute/event store
45 * for the relevant data, and sending a reply.
52 * Initialize the ReadHandler. Within the lifetime
53 * of this instance, this method is invoked once after object
54 * construction until a call to Shutdown is made to terminate the
57 * @param[in] apDelegate InteractionModelDelegate set by application.
59 * @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
60 * kState_NotInitialized.
61 * @retval #CHIP_NO_ERROR On success.
64 CHIP_ERROR Init(InteractionModelDelegate * apDelegate);
67 * Shut down the ReadHandler. This terminates this instance
68 * of the object and releases all held resources.
73 * Process a read request. Parts of the processing may end up being asynchronous, but the ReadHandler
74 * guarantees that it will call Shutdown on itself when processing is done (including if OnReadRequest
77 * @param[in] apExchangeContext A pointer to the ExchangeContext.
78 * @param[in] aPayload A payload that has read request data
80 * @retval #Others If fails to process read request
81 * @retval #CHIP_NO_ERROR On success.
84 CHIP_ERROR OnReadRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle aPayload);
87 * Send ReportData to initiator
89 * @param[in] aPayload A payload that has read request data
91 * @retval #Others If fails to send report data
92 * @retval #CHIP_NO_ERROR On success.
95 CHIP_ERROR SendReportData(System::PacketBufferHandle aPayload);
97 bool IsFree() const { return mState == HandlerState::Uninitialized; }
99 virtual ~ReadHandler() = default;
102 enum class HandlerState
104 Uninitialized = 0, //< The handler has not been initialized
105 Initialized, //< The handler has been initialized and is ready
106 Reportable, //< The handler has received read request and is waiting for the data to send to be available
109 CHIP_ERROR ProcessReadRequest(System::PacketBufferHandle aPayload);
111 void MoveToState(const HandlerState aTargetState);
113 const char * GetStateStr() const;
114 CHIP_ERROR ClearExistingExchangeContext();
116 Messaging::ExchangeContext * mpExchangeCtx = nullptr;
117 InteractionModelDelegate * mpDelegate = nullptr;
119 // Don't need the response for report data if true
120 bool mSuppressResponse;
122 // Retrieve all events
123 bool mGetToAllEvents;
125 // Current Handler state