3 * Copyright (c) 2020-2021 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 objects for a CHIP Interaction Data model Engine which handle unsolicitied IM message, and
22 * manage different kinds of IM client and handlers.
28 #include <app/MessageDef/ReportData.h>
29 #include <core/CHIPCore.h>
30 #include <messaging/ExchangeContext.h>
31 #include <messaging/ExchangeMgr.h>
32 #include <messaging/Flags.h>
33 #include <protocols/Protocols.h>
34 #include <protocols/interaction_model/Constants.h>
35 #include <support/CodeUtils.h>
36 #include <support/DLLUtil.h>
37 #include <support/logging/CHIPLogging.h>
38 #include <system/SystemPacketBuffer.h>
40 #include <app/Command.h>
41 #include <app/CommandHandler.h>
42 #include <app/CommandSender.h>
43 #include <app/InteractionModelDelegate.h>
44 #include <app/ReadClient.h>
45 #include <app/ReadHandler.h>
47 #define CHIP_MAX_NUM_COMMAND_HANDLER 1
48 #define CHIP_MAX_NUM_COMMAND_SENDER 1
49 #define CHIP_MAX_NUM_READ_CLIENT 1
50 #define CHIP_MAX_NUM_READ_HANDLER 1
51 #define CHIP_MAX_REPORTS_IN_FLIGHT 1
56 constexpr size_t kMaxSecureSduLengthBytes = 1024;
57 constexpr uint32_t kImMessageTimeoutMsec = 3000;
60 * @class InteractionModelEngine
62 * @brief This is a singleton hosting all CHIP unsolicited message processing and managing interaction model related clients and
66 class InteractionModelEngine : public Messaging::ExchangeDelegate
70 * @brief Retrieve the singleton Interaction Model Engine.
72 * @return A pointer to the shared InteractionModel Engine
75 static InteractionModelEngine * GetInstance(void);
77 InteractionModelEngine(void);
80 * Initialize the InteractionModel Engine.
82 * @param[in] apExchangeMgr A pointer to the ExchangeManager object.
83 * @param[in] apDelegate InteractionModelDelegate set by application.
85 * @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
86 * kState_NotInitialized.
87 * @retval #CHIP_NO_ERROR On success.
90 CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate);
94 Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeMgr; };
97 * Retrieve a CommandSender that the SDK consumer can use to send a set of commands. If the call succeeds,
98 * the consumer is responsible for calling Shutdown() on the CommandSender once it's done using it.
100 * @param[out] apCommandSender A pointer to the CommandSender object.
102 * @retval #CHIP_ERROR_INCORRECT_STATE If there is no CommandSender available
103 * @retval #CHIP_NO_ERROR On success.
105 CHIP_ERROR NewCommandSender(CommandSender ** const apCommandSender);
108 * Retrieve a ReadClient that the SDK consumer can use to send do a read. If the call succeeds, the consumer
109 * is responsible for calling Shutdown() on the ReadClient once it's done using it.
111 * @param[out] apReadClient A pointer to the ReadClient object.
113 * @retval #CHIP_ERROR_INCORRECT_STATE If there is no ReadClient available
114 * @retval #CHIP_NO_ERROR On success.
116 CHIP_ERROR NewReadClient(ReadClient ** const apReadClient);
119 * Get read client index in mReadClients
121 * @param[in] apReadClient A pointer to a read client object.
123 * @retval the index in mReadClients array
125 uint16_t GetReadClientArrayIndex(const ReadClient * const apReadClient) const;
128 void OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
129 const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload);
130 void OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
131 const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload);
132 void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
133 const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload);
134 void OnResponseTimeout(Messaging::ExchangeContext * ec);
137 * Called when Interaction Model receives a Read Request message. Errors processing
138 * the Read Request are handled entirely within this function.
140 void OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
141 const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload);
143 Messaging::ExchangeManager * mpExchangeMgr = nullptr;
144 InteractionModelDelegate * mpDelegate = nullptr;
145 CommandHandler mCommandHandlerObjs[CHIP_MAX_NUM_COMMAND_HANDLER];
146 CommandSender mCommandSenderObjs[CHIP_MAX_NUM_COMMAND_SENDER];
147 ReadClient mReadClients[CHIP_MAX_NUM_READ_CLIENT];
148 ReadHandler mReadHandlers[CHIP_MAX_NUM_READ_HANDLER];
151 void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId,
152 chip::TLV::TLVReader & aReader, Command * apCommandObj);