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 objects for a CHIP Echo unsolicitied
22 * initaitor (client) and responder (server).
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>
42 * Echo Protocol Message Types
44 enum class MsgType : uint8_t
50 using EchoFunct = void (*)(Messaging::ExchangeContext * ec, System::PacketBufferHandle payload);
52 class DLL_EXPORT EchoClient : public Messaging::ExchangeDelegate
55 // TODO: Init function will take a Channel instead a SecureSessionHandle, when Channel API is ready
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
62 * @param[in] exchangeMgr A pointer to the ExchangeManager object.
63 * @param[in] sessoin A handle to the session.
65 * @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
66 * kState_NotInitialized.
67 * @retval #CHIP_NO_ERROR On success.
70 CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr, SecureSessionHandle session);
73 * Shutdown the EchoClient. This terminates this instance
74 * of the object and releases all held resources.
80 * Set the application callback to be invoked when an echo response is received.
82 * @param[in] callback The callback function to receive echo response message.
85 void SetEchoResponseReceived(EchoFunct callback) { OnEchoResponseReceived = callback; }
88 * Send an echo request to a CHIP node.
90 * @param payload A PacketBufferHandle with the payload.
91 * @param sendFlags Flags set by the application for the CHIP message being sent.
93 * @return CHIP_ERROR_NO_MEMORY if no ExchangeContext is available.
94 * Other CHIP_ERROR codes as returned by the lower layers.
97 CHIP_ERROR SendEchoRequest(System::PacketBufferHandle && payload, const Messaging::SendFlags & sendFlags);
100 Messaging::ExchangeManager * mExchangeMgr = nullptr;
101 Messaging::ExchangeContext * mExchangeCtx = nullptr;
102 EchoFunct OnEchoResponseReceived = nullptr;
103 SecureSessionHandle mSecureSession;
105 void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
106 System::PacketBufferHandle payload) override;
107 void OnResponseTimeout(Messaging::ExchangeContext * ec) override;
110 class DLL_EXPORT EchoServer : public Messaging::ExchangeDelegate
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
119 * @param[in] exchangeMgr A pointer to the ExchangeManager object.
121 * @retval #CHIP_ERROR_INCORRECT_STATE If the state is not equal to
122 * kState_NotInitialized.
123 * @retval #CHIP_NO_ERROR On success.
126 CHIP_ERROR Init(Messaging::ExchangeManager * exchangeMgr);
129 * Shutdown the EchoServer. This terminates this instance
130 * of the object and releases all held resources.
136 * Set the application callback to be invoked when an echo request is received.
138 * @param[in] callback The callback function to receive echo request message.
141 void SetEchoRequestReceived(EchoFunct callback) { OnEchoRequestReceived = callback; }
144 Messaging::ExchangeManager * mExchangeMgr = nullptr;
145 EchoFunct OnEchoRequestReceived = nullptr;
147 void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader,
148 System::PacketBufferHandle payload) override;
149 void OnResponseTimeout(Messaging::ExchangeContext * ec) override {}
155 struct MessageTypeTraits<Echo::MsgType>
157 static constexpr const Protocols::Id & ProtocolId() { return Echo::Id; }
160 } // namespace Protocols