21
21
#include < lib/support/CHIPMemString.h>
22
22
#include < platform/CHIPDeviceLayer.h>
23
23
24
+ #include < net/if.h>
25
+
24
26
using namespace chip ::Dnssd;
25
27
using namespace chip ::Dnssd::Internal;
26
28
@@ -516,32 +518,40 @@ void ResolveContext::DispatchSuccess()
516
518
// ChipDnssdResolveNoLongerNeeded don't find us and try to also remove us.
517
519
bool needDelete = MdnsContexts::GetInstance ().RemoveWithoutDeleting (this );
518
520
519
- for (auto & interface : interfaces)
520
- {
521
- auto & ips = interface.second .addresses ;
521
+ #if TARGET_OS_TV
522
+ // On tvOS, prioritize results from en0, en1, ir0 in that order, if those
523
+ // interfaces are present, since those will generally have more up-to-date
524
+ // information.
525
+ static const unsigned int priorityInterfaceIndices[] = {
526
+ if_nametoindex (" en0" ),
527
+ if_nametoindex (" en1" ),
528
+ if_nametoindex (" ir0" ),
529
+ };
530
+ #else
531
+ // Elsewhere prioritize "lo0" over other interfaces.
532
+ static const unsigned int priorityInterfaceIndices[] = {
533
+ if_nametoindex (" lo0" ),
534
+ };
535
+ #endif // TARGET_OS_TV
522
536
523
- // Some interface may not have any ips, just ignore them.
524
- if (ips.size () == 0 )
537
+ for (auto interfaceIndex : priorityInterfaceIndices)
538
+ {
539
+ if (TryReportingResultsForInterfaceIndex (static_cast <uint32_t >(interfaceIndex)))
525
540
{
526
- continue ;
541
+ if (needDelete)
542
+ {
543
+ MdnsContexts::GetInstance ().Delete (this );
544
+ }
545
+ return ;
527
546
}
547
+ }
528
548
529
- ChipLogProgress (Discovery, " Mdns: Resolve success on interface %" PRIu32, interface.first );
530
-
531
- auto & service = interface.second .service ;
532
- auto addresses = Span<Inet::IPAddress>(ips.data (), ips.size ());
533
- if (nullptr == callback)
534
- {
535
- auto delegate = static_cast <CommissioningResolveDelegate *>(context);
536
- DiscoveredNodeData nodeData;
537
- service.ToDiscoveredNodeData (addresses, nodeData);
538
- delegate->OnNodeDiscovered (nodeData);
539
- }
540
- else
549
+ for (auto & interface : interfaces)
550
+ {
551
+ if (TryReportingResultsForInterfaceIndex (interface.first ))
541
552
{
542
- callback (context, &service, addresses, CHIP_NO_ERROR) ;
553
+ break ;
543
554
}
544
- break ;
545
555
}
546
556
547
557
if (needDelete)
@@ -550,6 +560,42 @@ void ResolveContext::DispatchSuccess()
550
560
}
551
561
}
552
562
563
+ bool ResolveContext::TryReportingResultsForInterfaceIndex (uint32_t interfaceIndex)
564
+ {
565
+ if (interfaceIndex == 0 )
566
+ {
567
+ // Not actually an interface we have.
568
+ return false ;
569
+ }
570
+
571
+ auto & interface = interfaces[interfaceIndex];
572
+ auto & ips = interface.addresses ;
573
+
574
+ // Some interface may not have any ips, just ignore them.
575
+ if (ips.size () == 0 )
576
+ {
577
+ return false ;
578
+ }
579
+
580
+ ChipLogProgress (Discovery, " Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
581
+
582
+ auto & service = interface.service ;
583
+ auto addresses = Span<Inet::IPAddress>(ips.data (), ips.size ());
584
+ if (nullptr == callback)
585
+ {
586
+ auto delegate = static_cast <CommissioningResolveDelegate *>(context);
587
+ DiscoveredNodeData nodeData;
588
+ service.ToDiscoveredNodeData (addresses, nodeData);
589
+ delegate->OnNodeDiscovered (nodeData);
590
+ }
591
+ else
592
+ {
593
+ callback (context, &service, addresses, CHIP_NO_ERROR);
594
+ }
595
+
596
+ return true ;
597
+ }
598
+
553
599
CHIP_ERROR ResolveContext::OnNewAddress (uint32_t interfaceId, const struct sockaddr * address)
554
600
{
555
601
// If we don't have any information about this interfaceId, just ignore the
0 commit comments