Matter SDK Coverage Report
Current view: top level - lib/dnssd - Advertiser_ImplMinimalMdnsAllocator.h (source / functions) Coverage Total Hit
Test: SHA:c5aeaea5470ddad6464d5b62caddeb9f1efd6a41 Lines: 95.2 % 62 59
Test Date: 2025-07-25 07:10:31 Functions: 100.0 % 54 54

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : #pragma once
      19              : 
      20              : #include <inttypes.h>
      21              : 
      22              : #include <lib/core/CHIPError.h>
      23              : #include <lib/dnssd/minimal_mdns/core/FlatAllocatedQName.h>
      24              : #include <lib/dnssd/minimal_mdns/core/QName.h>
      25              : #include <lib/dnssd/minimal_mdns/responders/IP.h>
      26              : #include <lib/dnssd/minimal_mdns/responders/Ptr.h>
      27              : #include <lib/dnssd/minimal_mdns/responders/QueryResponder.h>
      28              : #include <lib/dnssd/minimal_mdns/responders/RecordResponder.h>
      29              : #include <lib/dnssd/minimal_mdns/responders/Srv.h>
      30              : #include <lib/dnssd/minimal_mdns/responders/Txt.h>
      31              : #include <lib/support/CHIPMem.h>
      32              : #include <lib/support/logging/CHIPLogging.h>
      33              : 
      34              : namespace chip {
      35              : namespace Dnssd {
      36              : 
      37              : template <size_t kMaxRecords>
      38              : class QueryResponderAllocator
      39              : {
      40              : public:
      41           34 :     QueryResponderAllocator()
      42           34 :     {
      43          368 :         for (auto & responder : mAllocatedResponders)
      44              :         {
      45          334 :             responder = nullptr;
      46              :         }
      47         1122 :         for (auto & name : mAllocatedQNameParts)
      48              :         {
      49         1088 :             name = nullptr;
      50              :         }
      51           34 :     }
      52           34 :     ~QueryResponderAllocator() { Clear(); }
      53              : 
      54              :     /// Appends another responder to the internal replies.
      55              :     template <typename ResponderType, typename... Args>
      56          162 :     mdns::Minimal::QueryResponderSettings AddResponder(Args &&... args)
      57              :     {
      58          162 :         return AddAllocatedResponder(chip::Platform::New<ResponderType>(std::forward<Args>(args)...));
      59              :     }
      60              : 
      61              :     template <typename... Args>
      62          154 :     mdns::Minimal::FullQName AllocateQName(Args &&... names)
      63              :     {
      64          154 :         void * storage = AllocateQNameSpace(mdns::Minimal::FlatAllocatedQName::RequiredStorageSize(std::forward<Args>(names)...));
      65          154 :         if (storage == nullptr)
      66              :         {
      67            1 :             return mdns::Minimal::FullQName();
      68              :         }
      69          153 :         return mdns::Minimal::FlatAllocatedQName::Build(storage, std::forward<Args>(names)...);
      70              :     }
      71              : 
      72           52 :     mdns::Minimal::FullQName AllocateQNameFromArray(char const * const * names, size_t num)
      73              :     {
      74           52 :         void * storage = AllocateQNameSpace(mdns::Minimal::FlatAllocatedQName::RequiredStorageSizeFromArray(names, num));
      75           52 :         if (storage == nullptr)
      76              :         {
      77            1 :             return mdns::Minimal::FullQName();
      78              :         }
      79           51 :         return mdns::Minimal::FlatAllocatedQName::BuildFromArray(storage, names, num);
      80              :     }
      81              : 
      82              :     /// Sets the query responder to a blank state and frees up any
      83              :     /// allocated memory.
      84          112 :     void Clear()
      85              :     {
      86              :         // Init clears all responders, so that data can be freed
      87          112 :         mQueryResponder.Init();
      88              : 
      89              :         // Free all allocated data
      90         1259 :         for (auto & responder : mAllocatedResponders)
      91              :         {
      92         1147 :             if (responder != nullptr)
      93              :             {
      94          161 :                 chip::Platform::Delete(responder);
      95          161 :                 responder = nullptr;
      96              :             }
      97              :         }
      98              : 
      99         3696 :         for (auto & name : mAllocatedQNameParts)
     100              :         {
     101         3584 :             if (name != nullptr)
     102              :             {
     103          204 :                 chip::Platform::MemoryFree(name);
     104          204 :                 name = nullptr;
     105              :             }
     106              :         }
     107          112 :     }
     108          420 :     mdns::Minimal::QueryResponder<kMaxRecords + 1> * GetQueryResponder() { return &mQueryResponder; }
     109           27 :     const mdns::Minimal::RecordResponder * GetResponder(const mdns::Minimal::QType & qtype,
     110              :                                                         const mdns::Minimal::FullQName & qname) const
     111              :     {
     112          204 :         for (auto & responder : mAllocatedResponders)
     113              :         {
     114          183 :             if (responder != nullptr && responder->GetQType() == qtype && responder->GetQName() == qname)
     115              :             {
     116            6 :                 return responder;
     117              :             }
     118              :         }
     119           21 :         return nullptr;
     120              :     }
     121              :     bool IsEmpty() const
     122              :     {
     123              :         for (auto & responder : mAllocatedResponders)
     124              :         {
     125              :             if (responder != nullptr)
     126              :             {
     127              :                 return false;
     128              :             }
     129              :         }
     130              :         for (auto & name : mAllocatedQNameParts)
     131              :         {
     132              :             if (name != nullptr)
     133              :             {
     134              :                 return false;
     135              :             }
     136              :         }
     137              :         return true;
     138              :     }
     139              : 
     140              : protected:
     141              :     // For testing.
     142          363 :     size_t GetMaxAllocatedQNames() { return kMaxAllocatedQNameData; }
     143          288 :     void * GetQNamePart(size_t idx) { return mAllocatedQNameParts[idx]; }
     144          750 :     mdns::Minimal::RecordResponder * GetRecordResponder(size_t idx) { return mAllocatedResponders[idx]; }
     145              : 
     146              : private:
     147              :     static constexpr size_t kMaxAllocatedQNameData = 32;
     148              :     // dynamically allocated items
     149              :     mdns::Minimal::RecordResponder * mAllocatedResponders[kMaxRecords];
     150              :     void * mAllocatedQNameParts[kMaxAllocatedQNameData];
     151              :     // The QueryResponder needs 1 extra space to hold the record for itself.
     152              :     mdns::Minimal::QueryResponder<kMaxRecords + 1> mQueryResponder;
     153              : 
     154          162 :     mdns::Minimal::QueryResponderSettings AddAllocatedResponder(mdns::Minimal::RecordResponder * newResponder)
     155              :     {
     156          162 :         if (newResponder == nullptr)
     157              :         {
     158            0 :             ChipLogError(Discovery, "Responder memory allocation failed");
     159            0 :             return mdns::Minimal::QueryResponderSettings(); // failed
     160              :         }
     161              : 
     162          709 :         for (auto & responder : mAllocatedResponders)
     163              :         {
     164          708 :             if (responder != nullptr)
     165              :             {
     166          547 :                 continue;
     167              :             }
     168              : 
     169          161 :             responder = newResponder;
     170          161 :             return mQueryResponder.AddResponder(responder);
     171              :         }
     172              : 
     173            1 :         Platform::Delete(newResponder);
     174            1 :         ChipLogError(Discovery, "Failed to find free slot for adding a responder");
     175            1 :         return mdns::Minimal::QueryResponderSettings();
     176              :     }
     177              : 
     178          206 :     void * AllocateQNameSpace(size_t size)
     179              :     {
     180         1678 :         for (auto & name : mAllocatedQNameParts)
     181              :         {
     182         1676 :             if (name != nullptr)
     183              :             {
     184         1472 :                 continue;
     185              :             }
     186              : 
     187          204 :             name = chip::Platform::MemoryAlloc(size);
     188          204 :             if (name == nullptr)
     189              :             {
     190            0 :                 ChipLogError(Discovery, "QName memory allocation failed");
     191              :             }
     192          204 :             return name;
     193              :         }
     194            2 :         ChipLogError(Discovery, "Failed to find free slot for adding a qname");
     195            2 :         return nullptr;
     196              :     }
     197              : };
     198              : 
     199              : } // namespace Dnssd
     200              : } // namespace chip
        

Generated by: LCOV version 2.0-1