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