Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / InteractionModelEngine.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 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 objects for a CHIP Interaction Data model Engine which handle unsolicitied IM message, and
22  *      manage different kinds of IM client and handlers.
23  *
24  */
25
26 #pragma once
27
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>
39
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>
46
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
52
53 namespace chip {
54 namespace app {
55
56 constexpr size_t kMaxSecureSduLengthBytes = 1024;
57 constexpr uint32_t kImMessageTimeoutMsec  = 3000;
58
59 /**
60  * @class InteractionModelEngine
61  *
62  * @brief This is a singleton hosting all CHIP unsolicited message processing and managing interaction model related clients and
63  * handlers
64  *
65  */
66 class InteractionModelEngine : public Messaging::ExchangeDelegate
67 {
68 public:
69     /**
70      * @brief Retrieve the singleton Interaction Model Engine.
71      *
72      *  @return  A pointer to the shared InteractionModel Engine
73      *
74      */
75     static InteractionModelEngine * GetInstance(void);
76
77     InteractionModelEngine(void);
78
79     /**
80      *  Initialize the InteractionModel Engine.
81      *
82      *  @param[in]    apExchangeMgr    A pointer to the ExchangeManager object.
83      *  @param[in]    apDelegate       InteractionModelDelegate set by application.
84      *
85      *  @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
86      *          kState_NotInitialized.
87      *  @retval #CHIP_NO_ERROR On success.
88      *
89      */
90     CHIP_ERROR Init(Messaging::ExchangeManager * apExchangeMgr, InteractionModelDelegate * apDelegate);
91
92     void Shutdown();
93
94     Messaging::ExchangeManager * GetExchangeManager(void) const { return mpExchangeMgr; };
95
96     /**
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.
99      *
100      *  @param[out]    apCommandSender    A pointer to the CommandSender object.
101      *
102      *  @retval #CHIP_ERROR_INCORRECT_STATE If there is no CommandSender available
103      *  @retval #CHIP_NO_ERROR On success.
104      */
105     CHIP_ERROR NewCommandSender(CommandSender ** const apCommandSender);
106
107     /**
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.
110      *
111      *  @param[out]    apReadClient    A pointer to the ReadClient object.
112      *
113      *  @retval #CHIP_ERROR_INCORRECT_STATE If there is no ReadClient available
114      *  @retval #CHIP_NO_ERROR On success.
115      */
116     CHIP_ERROR NewReadClient(ReadClient ** const apReadClient);
117
118     /**
119      *  Get read client index in mReadClients
120      *
121      *  @param[in]    apReadClient    A pointer to a read client object.
122      *
123      *  @retval  the index in mReadClients array
124      */
125     uint16_t GetReadClientArrayIndex(const ReadClient * const apReadClient) const;
126
127 private:
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);
135
136     /**
137      * Called when Interaction Model receives a Read Request message.  Errors processing
138      * the Read Request are handled entirely within this function.
139      */
140     void OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader,
141                        const PayloadHeader & aPayloadHeader, System::PacketBufferHandle aPayload);
142
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];
149 };
150
151 void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId,
152                                   chip::TLV::TLVReader & aReader, Command * apCommandObj);
153
154 } // namespace app
155 } // namespace chip