Skip to content

Commit

Permalink
[OpenThread Platform] Update the dnssd code to dispatch browse and re…
Browse files Browse the repository at this point in the history
…solve results onto the chip thread (project-chip#13370)
  • Loading branch information
vivien-apple authored and step0035 committed Feb 8, 2022
1 parent a20427d commit 24daf99
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2140,12 +2140,35 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::FromOtDnsRespons
return CHIP_NO_ERROR;
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::DispatchResolve(intptr_t context)
{
auto * dnsResult = reinterpret_cast<DnsResult *>(context);
ThreadStackMgrImpl().mDnsResolveCallback(dnsResult->context, &(dnsResult->mMdnsService), dnsResult->error);
Platform::Delete<DnsResult>(dnsResult);
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::DispatchBrowseEmpty(intptr_t context)
{
auto * dnsResult = reinterpret_cast<DnsResult *>(context);
ThreadStackMgrImpl().mDnsBrowseCallback(dnsResult->context, nullptr, 0, dnsResult->error);
Platform::Delete<DnsResult>(dnsResult);
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::DispatchBrowse(intptr_t context)
{
auto * dnsResult = reinterpret_cast<DnsResult *>(context);
ThreadStackMgrImpl().mDnsBrowseCallback(dnsResult->context, &dnsResult->mMdnsService, 1, dnsResult->error);
Platform::Delete<DnsResult>(dnsResult);
}

template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnDnsBrowseResult(otError aError, const otDnsBrowseResponse * aResponse,
void * aContext)
{
CHIP_ERROR error;
DnsResult browseResult;
// type buffer size is kDnssdTypeAndProtocolMaxSize + . + kMaxDomainNameSize + . + termination character
char type[Dnssd::kDnssdTypeAndProtocolMaxSize + SrpClient::kMaxDomainNameSize + 3];
// hostname buffer size is kHostNameMaxLength + . + kMaxDomainNameSize + . + termination character
Expand All @@ -2169,35 +2192,44 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnDnsBrowseResult(otEr

VerifyOrExit(error == CHIP_NO_ERROR, );

while (otDnsBrowseResponseGetServiceInstance(aResponse, index, browseResult.mMdnsService.mName,
sizeof(browseResult.mMdnsService.mName)) == OT_ERROR_NONE)
char serviceName[Dnssd::Common::kInstanceNameMaxLength + 1];
while (otDnsBrowseResponseGetServiceInstance(aResponse, index, serviceName, sizeof(serviceName)) == OT_ERROR_NONE)
{
serviceInfo.mHostNameBuffer = hostname;
serviceInfo.mHostNameBufferSize = sizeof(hostname);
serviceInfo.mTxtData = txtBuffer;
serviceInfo.mTxtDataSize = sizeof(txtBuffer);

error = MapOpenThreadError(otDnsBrowseResponseGetServiceInfo(aResponse, browseResult.mMdnsService.mName, &serviceInfo));
error = MapOpenThreadError(otDnsBrowseResponseGetServiceInfo(aResponse, serviceName, &serviceInfo));

VerifyOrExit(error == CHIP_NO_ERROR, );

if (FromOtDnsResponseToMdnsData(serviceInfo, type, browseResult.mMdnsService, browseResult.mServiceTxtEntry) ==
CHIP_NO_ERROR)
DnsResult * dnsResult = Platform::New<DnsResult>(aContext, MapOpenThreadError(aError));
error = FromOtDnsResponseToMdnsData(serviceInfo, type, dnsResult->mMdnsService, dnsResult->mServiceTxtEntry);
if (CHIP_NO_ERROR == error)
{
// Invoke callback for every service one by one instead of for the whole list due to large memory size needed to
// allocate on
// stack.
ThreadStackMgrImpl().mDnsBrowseCallback(aContext, &browseResult.mMdnsService, 1, MapOpenThreadError(aError));
strncpy(dnsResult->mMdnsService.mName, serviceName, sizeof(serviceName));
DeviceLayer::PlatformMgr().ScheduleWork(DispatchBrowse, reinterpret_cast<intptr_t>(dnsResult));
wasAnythingBrowsed = true;
}
else
{
Platform::Delete<DnsResult>(dnsResult);
}
index++;
}

exit:

// In case no service was found invoke callback to notify about failure. In other case it was already called before.
if (!wasAnythingBrowsed)
ThreadStackMgrImpl().mDnsBrowseCallback(aContext, nullptr, 0, error);
{
DnsResult * dnsResult = Platform::New<DnsResult>(aContext, MapOpenThreadError(aError));
DeviceLayer::PlatformMgr().ScheduleWork(DispatchBrowseEmpty, reinterpret_cast<intptr_t>(dnsResult));
}
}

template <class ImplClass>
Expand Down Expand Up @@ -2231,7 +2263,7 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnDnsResolveResult(otE
void * aContext)
{
CHIP_ERROR error;
DnsResult resolveResult;
DnsResult * dnsResult = Platform::New<DnsResult>(aContext, MapOpenThreadError(aError));
// type buffer size is kDnssdTypeAndProtocolMaxSize + . + kMaxDomainNameSize + . + termination character
char type[Dnssd::kDnssdTypeAndProtocolMaxSize + SrpClient::kMaxDomainNameSize + 3];
// hostname buffer size is kHostNameMaxLength + . + kMaxDomainNameSize + . + termination character
Expand All @@ -2249,8 +2281,8 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnDnsResolveResult(otE

VerifyOrExit(aError == OT_ERROR_NONE, error = MapOpenThreadError(aError));

error = MapOpenThreadError(otDnsServiceResponseGetServiceName(aResponse, resolveResult.mMdnsService.mName,
sizeof(resolveResult.mMdnsService.mName), type, sizeof(type)));
error = MapOpenThreadError(otDnsServiceResponseGetServiceName(aResponse, dnsResult->mMdnsService.mName,
sizeof(dnsResult->mMdnsService.mName), type, sizeof(type)));

VerifyOrExit(error == CHIP_NO_ERROR, );

Expand All @@ -2263,11 +2295,12 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnDnsResolveResult(otE

VerifyOrExit(error == CHIP_NO_ERROR, );

error = FromOtDnsResponseToMdnsData(serviceInfo, type, resolveResult.mMdnsService, resolveResult.mServiceTxtEntry);
error = FromOtDnsResponseToMdnsData(serviceInfo, type, dnsResult->mMdnsService, dnsResult->mServiceTxtEntry);

exit:

ThreadStackMgrImpl().mDnsResolveCallback(aContext, &(resolveResult.mMdnsService), error);
dnsResult->error = error;
DeviceLayer::PlatformMgr().ScheduleWork(DispatchResolve, reinterpret_cast<intptr_t>(dnsResult));
}

template <class ImplClass>
Expand Down
11 changes: 11 additions & 0 deletions src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class GenericThreadStackManagerImpl_OpenThread
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
CHIP_ERROR _DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext);
CHIP_ERROR _DnsResolve(const char * aServiceName, const char * aInstanceName, DnsResolveCallback aCallback, void * aContext);
static void DispatchResolve(intptr_t context);
static void DispatchBrowseEmpty(intptr_t context);
static void DispatchBrowse(intptr_t context);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

Expand Down Expand Up @@ -226,8 +229,16 @@ class GenericThreadStackManagerImpl_OpenThread

struct DnsResult
{
void * context;
chip::Dnssd::DnssdService mMdnsService;
DnsServiceTxtEntries mServiceTxtEntry;
CHIP_ERROR error;

DnsResult(void * cbContext, CHIP_ERROR aError)
{
context = cbContext;
error = aError;
}
};

static void OnDnsBrowseResult(otError aError, const otDnsBrowseResponse * aResponse, void * aContext);
Expand Down

0 comments on commit 24daf99

Please sign in to comment.