Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[genio] Correct dnssd api usage #23848

Merged
merged 6 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/platform/mt793x/link_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ void __assert_func(const char * file, int line, const char * func, const char *
#include <assert.h>
#include <stdlib.h>

void * __wrap__calloc_r(size_t nmemb, size_t size) {
void * p = pvPortCalloc(nmemb, size);
while(!p)
;
return p;
}

void * __wrap__malloc_r(void * REENT, size_t size)
{
void * p = pvPortMalloc(size);
Expand Down
80 changes: 77 additions & 3 deletions src/platform/mt793x/DnssdContexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace {
constexpr uint8_t kDnssdKeyMaxSize = 32;
constexpr uint8_t kDnssdTxtRecordMaxEntries = 20;


std::string GetHostNameWithoutDomain(const char * hostnameWithDomain)
{
std::string hostname(hostnameWithDomain);
Expand All @@ -45,6 +46,7 @@ std::string GetHostNameWithoutDomain(const char * hostnameWithDomain)
return hostname;
}


std::string GetFullTypeWithoutSubTypes(std::string fullType)
{
size_t position = fullType.find(",");
Expand All @@ -56,6 +58,7 @@ std::string GetFullTypeWithoutSubTypes(std::string fullType)
return fullType;
}


void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t len)
{
uint16_t recordCount = TXTRecordGetCount(len, data);
Expand Down Expand Up @@ -93,6 +96,7 @@ void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t
}
}


DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType)
{
#if INET_CONFIG_ENABLE_IPV4
Expand All @@ -112,12 +116,11 @@ DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType)
return kDNSServiceProtocol_IPv6;
#endif
}

} // namespace


namespace chip {
namespace Dnssd {

CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err)
{
if (MdnsContexts::GetInstance().Has(this) == CHIP_NO_ERROR)
Expand All @@ -139,6 +142,56 @@ CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err)
return Error::ToChipError(err);
}


RegisterContext::RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext)
{
type = ContextType::Register;
context = cbContext;
callback = cb;

mType = sType;
mInstanceName = instanceName;
}


void RegisterContext::DispatchFailure(DNSServiceErrorType err)
{
ChipLogError(Discovery, "Mdns: Register failure (%s)", Error::ToString(err));
callback(context, nullptr, nullptr, Error::ToChipError(err));
MdnsContexts::GetInstance().Remove(this);
}


void RegisterContext::DispatchSuccess()
{
std::string typeWithoutSubTypes = GetFullTypeWithoutSubTypes(mType);
callback(context, typeWithoutSubTypes.c_str(), mInstanceName.c_str(), CHIP_NO_ERROR);

// Once a service has been properly published it is normally unreachable because the hostname has not yet been
// registered against the dns daemon. Register the records mapping the hostname to our IP.
// mHostNameRegistrar.Register();
}


CHIP_ERROR MdnsContexts::GetRegisterContextOfType(const char * type, RegisterContext ** context)
{
bool found = false;
std::vector<GenericContext *>::iterator iter;

for (iter = mContexts.begin(); iter != mContexts.end(); iter++)
{
if ((*iter)->type == ContextType::Register && (static_cast<RegisterContext *>(*iter))->matches(type))
{
*context = static_cast<RegisterContext *>(*iter);
found = true;
break;
}
}

return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND;
}


MdnsContexts::~MdnsContexts()
{
std::vector<GenericContext *>::const_iterator iter = mContexts.cbegin();
Expand All @@ -149,6 +202,7 @@ MdnsContexts::~MdnsContexts()
}
}


CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef)
{
VerifyOrReturnError(context != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
Expand All @@ -165,6 +219,7 @@ CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef)
return CHIP_NO_ERROR;
}


CHIP_ERROR MdnsContexts::Remove(GenericContext * context)
{
bool found = false;
Expand All @@ -187,6 +242,7 @@ CHIP_ERROR MdnsContexts::Remove(GenericContext * context)
return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND;
}


CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type)
{
bool found = false;
Expand All @@ -208,6 +264,7 @@ CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type)
return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND;
}


void MdnsContexts::Delete(GenericContext * context)
{
if (context->serviceRef != nullptr)
Expand All @@ -217,6 +274,7 @@ void MdnsContexts::Delete(GenericContext * context)
chip::Platform::Delete(context);
}


CHIP_ERROR MdnsContexts::Has(GenericContext * context)
{
std::vector<GenericContext *>::iterator iter;
Expand All @@ -232,6 +290,7 @@ CHIP_ERROR MdnsContexts::Has(GenericContext * context)
return CHIP_ERROR_KEY_NOT_FOUND;
}


GenericContext * MdnsContexts::GetBySockFd(int fd)
{
std::vector<GenericContext *>::iterator iter;
Expand All @@ -247,6 +306,7 @@ GenericContext * MdnsContexts::GetBySockFd(int fd)
return NULL;
}


int MdnsContexts::GetSelectFd(fd_set * pSelectFd)
{
int maxFd = 0;
Expand All @@ -266,6 +326,7 @@ int MdnsContexts::GetSelectFd(fd_set * pSelectFd)
return maxFd;
}


BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol)
{
type = ContextType::Browse;
Expand All @@ -275,19 +336,22 @@ BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServ
mSelectCount = 0;
}


void BrowseContext::DispatchFailure(DNSServiceErrorType err)
{
ChipLogError(Discovery, "Mdns: Browse failure (%s)", Error::ToString(err));
callback(context, nullptr, 0, true, Error::ToChipError(err));
MdnsContexts::GetInstance().Remove(this);
}


void BrowseContext::DispatchSuccess()
{
callback(context, services.data(), services.size(), true, CHIP_NO_ERROR);
MdnsContexts::GetInstance().Remove(this);
}


ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::Inet::IPAddressType cbAddressType)
{
type = ContextType::Resolve;
Expand All @@ -297,15 +361,18 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::
mSelectCount = 0;
}


ResolveContext::~ResolveContext() {}


void ResolveContext::DispatchFailure(DNSServiceErrorType err)
{
ChipLogError(Discovery, "Mdns: Resolve failure (%s)", Error::ToString(err));
callback(context, nullptr, Span<Inet::IPAddress>(), Error::ToChipError(err));
MdnsContexts::GetInstance().Remove(this);
}


void ResolveContext::DispatchSuccess()
{
for (auto & interface : interfaces)
Expand All @@ -326,6 +393,7 @@ void ResolveContext::DispatchSuccess()
MdnsContexts::GetInstance().Remove(this);
}


CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -367,6 +435,7 @@ CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct socka
return err;
}


CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress()
{
struct sockaddr_in sockaddr;
Expand All @@ -381,6 +450,7 @@ CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress()
return OnNewAddress(kDNSServiceInterfaceIndexLocalOnly, reinterpret_cast<struct sockaddr *>(&sockaddr));
}


bool ResolveContext::HasAddress()
{
for (auto & interface : interfaces)
Expand All @@ -394,6 +464,7 @@ bool ResolveContext::HasAddress()
return false;
}


void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, const char * hostnameWithDomain, uint16_t port,
uint16_t txtLen, const unsigned char * txtRecord)
{
Expand Down Expand Up @@ -463,17 +534,20 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname,
interfaces.insert(std::make_pair(interfaceId, std::move(interface)));
}


bool ResolveContext::HasInterface()
{
return interfaces.size();
}


InterfaceInfo::InterfaceInfo()
{
service.mTextEntrySize = 0;
service.mTextEntries = nullptr;
}


InterfaceInfo::InterfaceInfo(InterfaceInfo && other) :
service(std::move(other.service)), addresses(std::move(other.addresses)),
fullyQualifiedDomainName(std::move(other.fullyQualifiedDomainName))
Expand All @@ -484,6 +558,7 @@ InterfaceInfo::InterfaceInfo(InterfaceInfo && other) :
other.service.mTextEntries = nullptr;
}


InterfaceInfo::~InterfaceInfo()
{
if (service.mTextEntries == nullptr)
Expand All @@ -500,6 +575,5 @@ InterfaceInfo::~InterfaceInfo()
}
Platform::MemoryFree(const_cast<TextEntry *>(service.mTextEntries));
}

} // namespace Dnssd
} // namespace chip
Loading