Matter SDK Coverage Report
Current view: top level - protocols/echo - EchoServer.cpp (source / functions) Coverage Total Hit
Test: SHA:e98a48c2e59f85a25417956e1d105721433aa5d1 Lines: 65.2 % 23 15
Test Date: 2026-01-09 16:53:50 Functions: 75.0 % 4 3

            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 :     return mExchangeMgr->RegisterUnsolicitedMessageHandlerForType(MsgType::EchoRequest, this);
      42              : }
      43              : 
      44            0 : void EchoServer::Shutdown()
      45              : {
      46            0 :     if (mExchangeMgr != nullptr)
      47              :     {
      48            0 :         TEMPORARY_RETURN_IGNORED mExchangeMgr->UnregisterUnsolicitedMessageHandlerForType(MsgType::EchoRequest);
      49            0 :         mExchangeMgr = nullptr;
      50              :     }
      51            0 : }
      52              : 
      53            2 : CHIP_ERROR EchoServer::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate)
      54              : {
      55              :     // Handle messages by myself
      56            2 :     newDelegate = this;
      57            2 :     return CHIP_NO_ERROR;
      58              : }
      59              : 
      60            2 : CHIP_ERROR EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
      61              :                                          System::PacketBufferHandle && payload)
      62              : {
      63              :     // NOTE: we already know this is an Echo Request message because we explicitly registered with the
      64              :     // Exchange Manager for unsolicited Echo Requests.
      65              : 
      66              :     // Call the registered OnEchoRequestReceived handler, if any.
      67            2 :     if (OnEchoRequestReceived != nullptr)
      68              :     {
      69            0 :         OnEchoRequestReceived(ec, payload.Retain());
      70              :     }
      71              : 
      72            2 :     System::PacketBufferHandle response;
      73              : 
      74              :     // Since we are re-using the inbound EchoRequest buffer to send the EchoResponse, if necessary,
      75              :     // adjust the position of the payload within the buffer to ensure there is enough room for the
      76              :     // outgoing network headers.  This is necessary because in some network stack configurations,
      77              :     // the incoming header size may be smaller than the outgoing size.
      78            2 :     if (payload->EnsureReservedSize(CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) && MessagePacketBuffer::HasFooterSpace(payload))
      79              :     {
      80            2 :         response = std::move(payload);
      81              :     }
      82              :     else
      83              :     {
      84            0 :         response = MessagePacketBuffer::NewWithData(payload->Start(), payload->DataLength());
      85            0 :         VerifyOrReturnError(!response.IsNull(), CHIP_ERROR_NO_MEMORY);
      86              :     }
      87              : 
      88              :     // Send an Echo Response back to the sender.
      89            2 :     return ec->SendMessage(MsgType::EchoResponse, std::move(response));
      90            2 : }
      91              : 
      92              : } // namespace Echo
      93              : } // namespace Protocols
      94              : } // namespace chip
        

Generated by: LCOV version 2.0-1