LCOV - code coverage report
Current view: top level - lib/dnssd/minimal_mdns/records - Txt.h (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 10 11 90.9 %
Date: 2024-02-15 08:20:41 Functions: 2 2 100.0 %

          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 <string.h>
      21             : 
      22             : #include <lib/dnssd/minimal_mdns/records/ResourceRecord.h>
      23             : 
      24             : namespace mdns {
      25             : namespace Minimal {
      26             : 
      27             : class TxtResourceRecord : public ResourceRecord
      28             : {
      29             : public:
      30             :     static constexpr uint64_t kDefaultTtl = 4500; // 75 minutes
      31             : 
      32             :     TxtResourceRecord(const FullQName & qName, const char ** entries, size_t entryCount) :
      33             :         ResourceRecord(QType::TXT, qName), mEntries(entries), mEntryCount(entryCount)
      34             :     {
      35             :         SetTtl(kDefaultTtl);
      36             :     }
      37             : 
      38             :     template <size_t N>
      39             :     TxtResourceRecord(const FullQName & qName, const char * (&entries)[N]) :
      40             :         ResourceRecord(QType::TXT, qName), mEntries(entries), mEntryCount(N)
      41             :     {
      42             :         SetTtl(kDefaultTtl);
      43             :     }
      44             : 
      45             :     // A FullQName is a holder of a string array.
      46           8 :     TxtResourceRecord(const FullQName & qName, FullQName entries) :
      47           8 :         ResourceRecord(QType::TXT, qName), mEntries(entries.names), mEntryCount(entries.nameCount)
      48             :     {
      49           8 :         SetTtl(kDefaultTtl);
      50           8 :     }
      51             :     size_t GetNumEntries() const { return mEntryCount; }
      52             :     const char * const * GetEntries() const { return mEntries; }
      53             : 
      54             : protected:
      55          48 :     bool WriteData(RecordWriter & out) const override
      56             :     {
      57         288 :         for (size_t i = 0; i < mEntryCount; i++)
      58             :         {
      59         240 :             size_t len = strlen(mEntries[i]);
      60         240 :             if (len > kMaxTxtRecordLength)
      61             :             {
      62           0 :                 return false;
      63             :             }
      64             : 
      65         240 :             out.Put8(static_cast<uint8_t>(len)).PutString(mEntries[i]);
      66             :         }
      67          48 :         return out.Fit();
      68             :     }
      69             : 
      70             : private:
      71             :     const char * const * mEntries;
      72             :     const size_t mEntryCount;
      73             : 
      74             :     static constexpr size_t kMaxTxtRecordLength = 63;
      75             : };
      76             : 
      77             : } // namespace Minimal
      78             : } // namespace mdns

Generated by: LCOV version 1.14