Matter SDK Coverage Report
Current view: top level - inet - InetLayer.h (source / functions) Coverage Total Hit
Test: SHA:2a48c1efeab1c0f76f3adb3a0940b0f7de706453 Lines: 88.9 % 36 32
Test Date: 2026-01-31 08:14:20 Functions: 80.0 % 30 24

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020-2025 Project CHIP Authors
       4              :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       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              :  * Provides access to UDP (and optionally TCP) EndPointManager.
      21              :  */
      22              : 
      23              : #pragma once
      24              : 
      25              : #include <inet/EndPointBasis.h>
      26              : #include <inet/InetError.h>
      27              : #include <lib/support/CodeUtils.h>
      28              : #include <lib/support/ObjectLifeCycle.h>
      29              : #include <lib/support/Pool.h>
      30              : #include <lib/support/ReferenceCountedPtr.h>
      31              : #include <platform/LockTracker.h>
      32              : #include <system/SystemLayer.h>
      33              : #include <system/SystemStats.h>
      34              : 
      35              : #include <stdint.h>
      36              : 
      37              : namespace chip {
      38              : namespace Inet {
      39              : 
      40              : template <typename EndPointType>
      41              : class EndPointDeletor;
      42              : 
      43              : /**
      44              :  * Template providing traits for EndPoint types used by EndPointManager.
      45              :  *
      46              :  * Instances must define:
      47              :  *      static constexpr const char * kName;
      48              :  *      static constexpr int kSystemStatsKey;
      49              :  */
      50              : template <class EndPointType>
      51              : struct EndPointProperties;
      52              : 
      53              : template <class EndPointType>
      54              : class EndPointHandle : public ReferenceCountedPtr<EndPointType>
      55              : {
      56              : public:
      57              :     using ReferenceCountedPtr<EndPointType>::ReferenceCountedPtr;
      58              : 
      59              :     // For printing
      60              :     inline operator const void *() const { return this->mRefCounted; }
      61              : };
      62              : 
      63              : /**
      64              :  * Manage creating, deletion, and iteration of Inet::EndPoint types.
      65              :  */
      66              : template <class EndPointType>
      67              : class EndPointManager
      68              : {
      69              : public:
      70              :     using EndPoint            = EndPointType;
      71              :     using TypedEndPointHandle = EndPointHandle<EndPoint>;
      72              :     using EndPointVisitor     = Loop (*)(const TypedEndPointHandle &);
      73              : 
      74          197 :     EndPointManager() {}
      75          197 :     virtual ~EndPointManager() { VerifyOrDie(mLayerState.Destroy()); }
      76              : 
      77          232 :     CHIP_ERROR Init(System::Layer & systemLayer)
      78              :     {
      79          232 :         RegisterLayerErrorFormatter();
      80          232 :         VerifyOrReturnError(mLayerState.SetInitializing(), CHIP_ERROR_INCORRECT_STATE);
      81          232 :         VerifyOrReturnError(systemLayer.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
      82          232 :         mSystemLayer = &systemLayer;
      83          232 :         mLayerState.SetInitialized();
      84          232 :         return CHIP_NO_ERROR;
      85              :     }
      86              : 
      87          234 :     void Shutdown()
      88              :     {
      89              :         // Return to uninitialized state to permit re-initialization.
      90          234 :         mLayerState.ResetFromInitialized();
      91          234 :         mSystemLayer = nullptr;
      92          234 :     }
      93              : 
      94          770 :     System::Layer & SystemLayer() const { return *mSystemLayer; }
      95              : 
      96          163 :     CHIP_ERROR NewEndPoint(TypedEndPointHandle & retEndPoint)
      97              :     {
      98          163 :         assertChipStackLockedByCurrentThread();
      99          163 :         VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
     100              : 
     101          161 :         retEndPoint = std::move(CreateEndPoint());
     102          161 :         if (retEndPoint.IsNull())
     103              :         {
     104            0 :             ChipLogError(Inet, "%s endpoint pool FULL", EndPointProperties<EndPointType>::kName);
     105            0 :             return CHIP_ERROR_ENDPOINT_POOL_FULL;
     106              :         }
     107              : 
     108          161 :         SYSTEM_STATS_INCREMENT(EndPointProperties<EndPointType>::kSystemStatsKey);
     109          161 :         return CHIP_NO_ERROR;
     110              :     }
     111              : 
     112              :     virtual Loop ForEachEndPoint(const EndPointVisitor visitor) = 0;
     113              : 
     114              : protected:
     115              :     friend class EndPointDeletor<EndPointType>;
     116              :     friend class EndPointHandle<EndPointType>;
     117              :     friend class EndPointBasis<EndPointType>;
     118              :     friend EndPointType;
     119              : 
     120              :     virtual TypedEndPointHandle CreateEndPoint() = 0;
     121              : 
     122              :     virtual void ReleaseEndPoint(EndPoint * endPoint) = 0;
     123              : 
     124          161 :     void DeleteEndPoint(EndPoint * endPoint)
     125              :     {
     126          161 :         SYSTEM_STATS_DECREMENT(EndPointProperties<EndPointType>::kSystemStatsKey);
     127          161 :         ReleaseEndPoint(endPoint);
     128          161 :     }
     129              : 
     130              : private:
     131              :     ObjectLifeCycle mLayerState;
     132              :     System::Layer * mSystemLayer;
     133              : };
     134              : 
     135              : template <typename EndPointImpl>
     136              : class EndPointManagerImplPool : public EndPointManager<typename EndPointImpl::EndPoint>
     137              : {
     138              : public:
     139              :     using Manager             = EndPointManager<typename EndPointImpl::EndPoint>;
     140              :     using EndPoint            = typename EndPointImpl::EndPoint;
     141              :     using TypedEndPointHandle = typename Manager::TypedEndPointHandle;
     142              : 
     143          197 :     EndPointManagerImplPool()           = default;
     144          197 :     ~EndPointManagerImplPool() override = default;
     145              : 
     146          161 :     TypedEndPointHandle CreateEndPoint() override { return sEndPointPool.CreateObject(*this); }
     147          124 :     Loop ForEachEndPoint(const typename Manager::EndPointVisitor visitor) override
     148              :     {
     149          248 :         return sEndPointPool.ForEachActiveObject([&](EndPoint * endPoint) -> Loop {
     150            0 :             TypedEndPointHandle handle(endPoint);
     151            0 :             return visitor(handle);
     152          124 :         });
     153              :     }
     154              : 
     155              : private:
     156          161 :     void ReleaseEndPoint(EndPoint * endPoint) override { sEndPointPool.ReleaseObject(static_cast<EndPointImpl *>(endPoint)); }
     157              : 
     158              :     ObjectPool<EndPointImpl, EndPointProperties<EndPoint>::kNumEndPoints> sEndPointPool;
     159              : };
     160              : 
     161              : class TCPEndPoint;
     162              : class UDPEndPoint;
     163              : 
     164              : } // namespace Inet
     165              : } // namespace chip
        

Generated by: LCOV version 2.0-1