Line data Source code
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 implements an object for a CHIP Echo unsolicited 22 : * responder (server). 23 : * 24 : */ 25 : 26 : #include "Echo.h" 27 : 28 : namespace chip { 29 : namespace Protocols { 30 : namespace Echo { 31 : 32 2 : CHIP_ERROR EchoServer::Init(Messaging::ExchangeManager * exchangeMgr) 33 : { 34 : // Error if already initialized. 35 2 : VerifyOrReturnError(mExchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE); 36 : 37 2 : mExchangeMgr = exchangeMgr; 38 2 : OnEchoRequestReceived = nullptr; 39 : 40 : // Register to receive unsolicited Echo Request messages from the exchange manager. 41 2 : mExchangeMgr->RegisterUnsolicitedMessageHandlerForType(MsgType::EchoRequest, this); 42 : 43 2 : return CHIP_NO_ERROR; 44 : } 45 : 46 0 : void EchoServer::Shutdown() 47 : { 48 0 : if (mExchangeMgr != nullptr) 49 : { 50 0 : mExchangeMgr->UnregisterUnsolicitedMessageHandlerForType(MsgType::EchoRequest); 51 0 : mExchangeMgr = nullptr; 52 : } 53 0 : } 54 : 55 2 : CHIP_ERROR EchoServer::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) 56 : { 57 : // Handle messages by myself 58 2 : newDelegate = this; 59 2 : return CHIP_NO_ERROR; 60 : } 61 : 62 2 : CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, 63 : System::PacketBufferHandle && payload) 64 : { 65 : // NOTE: we already know this is an Echo Request message because we explicitly registered with the 66 : // Exchange Manager for unsolicited Echo Requests. 67 : 68 : // Call the registered OnEchoRequestReceived handler, if any. 69 2 : if (OnEchoRequestReceived != nullptr) 70 : { 71 0 : OnEchoRequestReceived(ec, payload.Retain()); 72 : } 73 : 74 2 : System::PacketBufferHandle response; 75 : 76 : // Since we are re-using the inbound EchoRequest buffer to send the EchoResponse, if necessary, 77 : // adjust the position of the payload within the buffer to ensure there is enough room for the 78 : // outgoing network headers. This is necessary because in some network stack configurations, 79 : // the incoming header size may be smaller than the outgoing size. 80 2 : if (payload->EnsureReservedSize(CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) && MessagePacketBuffer::HasFooterSpace(payload)) 81 : { 82 2 : response = std::move(payload); 83 : } 84 : else 85 : { 86 0 : response = MessagePacketBuffer::NewWithData(payload->Start(), payload->DataLength()); 87 0 : VerifyOrReturnError(!response.IsNull(), CHIP_ERROR_NO_MEMORY); 88 : } 89 : 90 : // Send an Echo Response back to the sender. 91 2 : return ec->SendMessage(MsgType::EchoResponse, std::move(response)); 92 2 : } 93 : 94 : } // namespace Echo 95 : } // namespace Protocols 96 : } // namespace chip