Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / protocols / echo / Echo.h
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 objects for a CHIP Echo unsolicitied
22  *      initaitor (client) and responder (server).
23  *
24  */
25
26 #pragma once
27
28 #include <core/CHIPCore.h>
29 #include <messaging/ExchangeContext.h>
30 #include <messaging/ExchangeMgr.h>
31 #include <messaging/Flags.h>
32 #include <protocols/Protocols.h>
33 #include <support/CodeUtils.h>
34 #include <support/DLLUtil.h>
35 #include <support/logging/CHIPLogging.h>
36
37 namespace chip {
38 namespace Protocols {
39 namespace Echo {
40
41 /**
42  * Echo Protocol Message Types
43  */
44 enum class MsgType : uint8_t
45 {
46     EchoRequest  = 0x01,
47     EchoResponse = 0x02
48 };
49
50 using EchoFunct = void (*)(Messaging::ExchangeContext * ec, System::PacketBufferHandle payload);
51
52 class DLL_EXPORT EchoClient : public Messaging::ExchangeDelegate
53 {
54 public:
55     // TODO: Init function will take a Channel instead a SecureSessionHandle, when Channel API is ready
56     /**
57      *  Initialize the EchoClient object. Within the lifetime
58      *  of this instance, this method is invoked once after object
59      *  construction until a call to Shutdown is made to terminate the
60      *  instance.
61      *
62      *  @param[in]    exchangeMgr    A pointer to the ExchangeManager object.
63      *  @param[in]    sessoin        A handle to the session.
64      *
65      *  @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
66      *          kState_NotInitialized.
67      *  @retval #CHIP_NO_ERROR On success.
68      *
69      */
70     CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr, SecureSessionHandle session);
71
72     /**
73      *  Shutdown the EchoClient. This terminates this instance
74      *  of the object and releases all held resources.
75      *
76      */
77     void Shutdown();
78
79     /**
80      * Set the application callback to be invoked when an echo response is received.
81      *
82      *  @param[in]    callback    The callback function to receive echo response message.
83      *
84      */
85     void SetEchoResponseReceived(EchoFunct callback) { OnEchoResponseReceived = callback; }
86
87     /**
88      * Send an echo request to a CHIP node.
89      *
90      * @param payload       A PacketBufferHandle with the payload.
91      * @param sendFlags     Flags set by the application for the CHIP message being sent.
92      *
93      * @return CHIP_ERROR_NO_MEMORY if no ExchangeContext is available.
94      *         Other CHIP_ERROR codes as returned by the lower layers.
95      *
96      */
97     CHIP_ERROR SendEchoRequest(System::PacketBufferHandle && payload, const Messaging::SendFlags & sendFlags);
98
99 private:
100     Messaging::ExchangeManager * mExchangeMgr = nullptr;
101     Messaging::ExchangeContext * mExchangeCtx = nullptr;
102     EchoFunct OnEchoResponseReceived          = nullptr;
103     SecureSessionHandle mSecureSession;
104
105     void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
106                            System::PacketBufferHandle payload) override;
107     void OnResponseTimeout(Messaging::ExchangeContext * ec) override;
108 };
109
110 class DLL_EXPORT EchoServer : public Messaging::ExchangeDelegate
111 {
112 public:
113     /**
114      *  Initialize the EchoServer object. Within the lifetime
115      *  of this instance, this method is invoked once after object
116      *  construction until a call to Shutdown is made to terminate the
117      *  instance.
118      *
119      *  @param[in]    exchangeMgr    A pointer to the ExchangeManager object.
120      *
121      *  @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
122      *          kState_NotInitialized.
123      *  @retval #CHIP_NO_ERROR On success.
124      *
125      */
126     CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr);
127
128     /**
129      *  Shutdown the EchoServer. This terminates this instance
130      *  of the object and releases all held resources.
131      *
132      */
133     void Shutdown();
134
135     /**
136      * Set the application callback to be invoked when an echo request is received.
137      *
138      *  @param[in]    callback    The callback function to receive echo request message.
139      *
140      */
141     void SetEchoRequestReceived(EchoFunct callback) { OnEchoRequestReceived = callback; }
142
143 private:
144     Messaging::ExchangeManager * mExchangeMgr = nullptr;
145     EchoFunct OnEchoRequestReceived           = nullptr;
146
147     void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
148                            System::PacketBufferHandle payload) override;
149     void OnResponseTimeout(Messaging::ExchangeContext * ec) override {}
150 };
151
152 } // namespace Echo
153
154 template <>
155 struct MessageTypeTraits<Echo::MsgType>
156 {
157     static constexpr const Protocols::Id & ProtocolId() { return Echo::Id; }
158 };
159
160 } // namespace Protocols
161 } // namespace chip