Skip to content

Commit 000edb4

Browse files
Prioritize DNS-SD results from the network on tvOS. (project-chip#32513)
* 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. * Address review comments, fix CI to use right SDK name.
1 parent 68c66e3 commit 000edb4

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-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 appletvos -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

+66-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,40 @@ 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, 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
522536

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)))
525540
{
526-
continue;
541+
if (needDelete)
542+
{
543+
MdnsContexts::GetInstance().Delete(this);
544+
}
545+
return;
527546
}
547+
}
528548

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))
541552
{
542-
callback(context, &service, addresses, CHIP_NO_ERROR);
553+
break;
543554
}
544-
break;
545555
}
546556

547557
if (needDelete)
@@ -550,6 +560,42 @@ void ResolveContext::DispatchSuccess()
550560
}
551561
}
552562

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+
553599
CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address)
554600
{
555601
// 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)