Skip to content

Commit 125f6c7

Browse files
committedMar 8, 2024
Prioritize DNS-SD results from the network on tvOS.
Since we can only deliver results from one interface given the current state of Matter SDK APIs (see project-chip#32512), prioritize the interfaces that are more likely to have up-to-date results, not stale caches.
1 parent 28da08f commit 125f6c7

File tree

3 files changed

+74
-20
lines changed

3 files changed

+74
-20
lines changed
 

‎.github/workflows/darwin.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ jobs:
5454
# Disable availability annotations, since we are not building a system
5555
# Matter.framework.
5656
run: xcodebuild -target "Matter" -sdk watchos -configuration Debug GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
57+
- name: Run tvOS Build Debug
58+
working-directory: src/darwin/Framework
59+
# Disable availability annotations, since we are not building a system
60+
# Matter.framework.
61+
run: xcodebuild -target "Matter" -sdk tvos -configuration Debug GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
5762
- name: Run iOS Build Debug
5863
working-directory: src/darwin/Framework
5964
# Disable availability annotations, since we are not building a system

‎src/platform/Darwin/DnssdContexts.cpp

+61-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <lib/support/CHIPMemString.h>
2222
#include <platform/CHIPDeviceLayer.h>
2323

24+
#include <net/if.h>
25+
2426
using namespace chip::Dnssd;
2527
using namespace chip::Dnssd::Internal;
2628

@@ -516,32 +518,35 @@ void ResolveContext::DispatchSuccess()
516518
// ChipDnssdResolveNoLongerNeeded don't find us and try to also remove us.
517519
bool needDelete = MdnsContexts::GetInstance().RemoveWithoutDeleting(this);
518520

519-
for (auto & interface : interfaces)
520-
{
521-
auto & ips = interface.second.addresses;
521+
#if TARGET_OS_TV
522+
// On tvOS, priotize 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+
};
522530

523-
// Some interface may not have any ips, just ignore them.
524-
if (ips.size() == 0)
531+
for (auto interfaceIndex : priorityInterfaceIndices)
532+
{
533+
if (TryReportingResultsForInterfaceIndex(static_cast<uint32_t>(interfaceIndex)))
525534
{
526-
continue;
535+
if (needDelete)
536+
{
537+
MdnsContexts::GetInstance().Delete(this);
538+
}
539+
return;
527540
}
541+
}
542+
#endif // TARGET_OS_TV
528543

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
544+
for (auto & interface : interfaces)
545+
{
546+
if (TryReportingResultsForInterfaceIndex(interface.first))
541547
{
542-
callback(context, &service, addresses, CHIP_NO_ERROR);
548+
break;
543549
}
544-
break;
545550
}
546551

547552
if (needDelete)
@@ -550,6 +555,42 @@ void ResolveContext::DispatchSuccess()
550555
}
551556
}
552557

558+
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex)
559+
{
560+
if (interfaceIndex == 0)
561+
{
562+
// Not actually an interface we have.
563+
return false;
564+
}
565+
566+
auto & interface = interfaces[interfaceIndex];
567+
auto & ips = interface.addresses;
568+
569+
// Some interface may not have any ips, just ignore them.
570+
if (ips.size() == 0)
571+
{
572+
return false;
573+
}
574+
575+
ChipLogProgress(Discovery, "Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
576+
577+
auto & service = interface.service;
578+
auto addresses = Span<Inet::IPAddress>(ips.data(), ips.size());
579+
if (nullptr == callback)
580+
{
581+
auto delegate = static_cast<CommissioningResolveDelegate *>(context);
582+
DiscoveredNodeData nodeData;
583+
service.ToDiscoveredNodeData(addresses, nodeData);
584+
delegate->OnNodeDiscovered(nodeData);
585+
}
586+
else
587+
{
588+
callback(context, &service, addresses, CHIP_NO_ERROR);
589+
}
590+
591+
return true;
592+
}
593+
553594
CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address)
554595
{
555596
// If we don't have any information about this interfaceId, just ignore the

‎src/platform/Darwin/DnssdImpl.h

+8
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ struct ResolveContext : public GenericContext
251251
const unsigned char * txtRecord);
252252
bool HasInterface();
253253
bool Matches(const char * otherInstanceName) const { return instanceName == otherInstanceName; }
254+
255+
private:
256+
/**
257+
* Try reporting the results we got on the provided interface index.
258+
* Returns true if information was reported, false if not (e.g. if there
259+
* were no IP addresses, etc).
260+
*/
261+
bool TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex);
254262
};
255263

256264
} // namespace Dnssd

0 commit comments

Comments
 (0)