Matter SDK Coverage Report
Current view: top level - controller - AbstractDnssdDiscoveryController.cpp (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 93.0 % 43 40
Test Date: 2025-01-17 19:00:11 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 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              : // module header, comes first
      20              : #include <controller/AbstractDnssdDiscoveryController.h>
      21              : 
      22              : #include <lib/core/CHIPEncoding.h>
      23              : #include <lib/support/logging/CHIPLogging.h>
      24              : 
      25              : #include <bitset>
      26              : 
      27              : namespace chip {
      28              : namespace Controller {
      29              : 
      30            4 : static bool SameExceptOrder(const chip::Span<const Inet::IPAddress> & v1, const chip::Span<const Inet::IPAddress> & v2)
      31              : {
      32            4 :     std::bitset<chip::Dnssd::CommonResolutionData::kMaxIPAddresses> addressUsed;
      33              : 
      34            4 :     VerifyOrDie(v1.size() <= Dnssd::CommissionNodeData::kMaxIPAddresses && v2.size() <= Dnssd::CommissionNodeData::kMaxIPAddresses);
      35            4 :     if (v1.size() != v2.size())
      36              :     {
      37            2 :         return false;
      38              :     }
      39              : 
      40            6 :     for (size_t s = 0; s < v1.size(); s++)
      41              :     {
      42            7 :         for (size_t d = 0; d < v2.size(); d++)
      43              :         {
      44            6 :             if (!addressUsed[d] && v1[s] == v2[d])
      45              :             {
      46              :                 // Change the used flag so that the compared target is no longer used
      47            3 :                 addressUsed.set(d, true);
      48            3 :                 break;
      49              :             }
      50              :         }
      51              :     }
      52            2 :     return addressUsed.count() == v2.size();
      53              : }
      54              : 
      55            9 : void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & discNodeData)
      56              : {
      57           18 :     VerifyOrReturn(discNodeData.Is<chip::Dnssd::CommissionNodeData>());
      58              : 
      59            9 :     auto discoveredNodes = GetDiscoveredNodes();
      60            9 :     auto & nodeData      = discNodeData.Get<chip::Dnssd::CommissionNodeData>();
      61           89 :     for (auto & discoveredNode : discoveredNodes)
      62              :     {
      63              : 
      64           81 :         if (!discoveredNode.IsValid())
      65              :         {
      66           75 :             continue;
      67              :         }
      68            6 :         chip::Span<const Inet::IPAddress> discoveredNodeIPAddressSpan(&discoveredNode.ipAddress[0], discoveredNode.numIPs);
      69            6 :         chip::Span<const Inet::IPAddress> nodeDataIPAddressSpan(&nodeData.ipAddress[0], nodeData.numIPs);
      70           10 :         if (strcmp(discoveredNode.hostName, nodeData.hostName) == 0 && discoveredNode.port == nodeData.port &&
      71            4 :             SameExceptOrder(discoveredNodeIPAddressSpan, nodeDataIPAddressSpan))
      72              :         {
      73            1 :             discoveredNode = nodeData;
      74            1 :             if (mDeviceDiscoveryDelegate != nullptr)
      75              :             {
      76            0 :                 mDeviceDiscoveryDelegate->OnDiscoveredDevice(nodeData);
      77              :             }
      78            1 :             return;
      79              :         }
      80              :     }
      81              :     // Node not yet in the list
      82           13 :     for (auto & discoveredNode : discoveredNodes)
      83              :     {
      84           13 :         if (!discoveredNode.IsValid())
      85              :         {
      86            8 :             discoveredNode = nodeData;
      87            8 :             if (mDeviceDiscoveryDelegate != nullptr)
      88              :             {
      89            0 :                 mDeviceDiscoveryDelegate->OnDiscoveredDevice(nodeData);
      90              :             }
      91            8 :             return;
      92              :         }
      93              :     }
      94            0 :     ChipLogError(Discovery, "Failed to add discovered node with hostname %s- Insufficient space", nodeData.hostName);
      95              : }
      96              : 
      97            4 : CHIP_ERROR AbstractDnssdDiscoveryController::SetUpNodeDiscovery()
      98              : {
      99            4 :     auto discoveredNodes = GetDiscoveredNodes();
     100           44 :     for (auto & discoveredNode : discoveredNodes)
     101              :     {
     102           40 :         discoveredNode.Reset();
     103              :     }
     104            4 :     return CHIP_NO_ERROR;
     105              : }
     106              : 
     107           51 : const Dnssd::CommissionNodeData * AbstractDnssdDiscoveryController::GetDiscoveredNode(int idx)
     108              : {
     109              :     // TODO(cecille): Add assertion about main loop.
     110           51 :     auto discoveredNodes = GetDiscoveredNodes();
     111           51 :     if (0 <= idx && idx < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES && discoveredNodes.data()[idx].IsValid())
     112              :     {
     113           29 :         return discoveredNodes.data() + idx;
     114              :     }
     115           22 :     return nullptr;
     116              : }
     117              : 
     118              : } // namespace Controller
     119              : } // namespace chip
        

Generated by: LCOV version 2.0-1