diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp index 969c1c52075894..950bb04f01574c 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp @@ -2140,12 +2140,35 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::FromOtDnsRespons return CHIP_NO_ERROR; } +template +void GenericThreadStackManagerImpl_OpenThread::DispatchResolve(intptr_t context) +{ + auto * dnsResult = reinterpret_cast(context); + ThreadStackMgrImpl().mDnsResolveCallback(dnsResult->context, &(dnsResult->mMdnsService), dnsResult->error); + Platform::Delete(dnsResult); +} + +template +void GenericThreadStackManagerImpl_OpenThread::DispatchBrowseEmpty(intptr_t context) +{ + auto * dnsResult = reinterpret_cast(context); + ThreadStackMgrImpl().mDnsBrowseCallback(dnsResult->context, nullptr, 0, dnsResult->error); + Platform::Delete(dnsResult); +} + +template +void GenericThreadStackManagerImpl_OpenThread::DispatchBrowse(intptr_t context) +{ + auto * dnsResult = reinterpret_cast(context); + ThreadStackMgrImpl().mDnsBrowseCallback(dnsResult->context, &dnsResult->mMdnsService, 1, dnsResult->error); + Platform::Delete(dnsResult); +} + template void GenericThreadStackManagerImpl_OpenThread::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 @@ -2169,27 +2192,33 @@ void GenericThreadStackManagerImpl_OpenThread::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(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(dnsResult)); wasAnythingBrowsed = true; } + else + { + Platform::Delete(dnsResult); + } index++; } @@ -2197,7 +2226,10 @@ void GenericThreadStackManagerImpl_OpenThread::OnDnsBrowseResult(otEr // 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(aContext, MapOpenThreadError(aError)); + DeviceLayer::PlatformMgr().ScheduleWork(DispatchBrowseEmpty, reinterpret_cast(dnsResult)); + } } template @@ -2231,7 +2263,7 @@ void GenericThreadStackManagerImpl_OpenThread::OnDnsResolveResult(otE void * aContext) { CHIP_ERROR error; - DnsResult resolveResult; + DnsResult * dnsResult = Platform::New(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 @@ -2249,8 +2281,8 @@ void GenericThreadStackManagerImpl_OpenThread::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, ); @@ -2263,11 +2295,12 @@ void GenericThreadStackManagerImpl_OpenThread::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(dnsResult)); } template diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h index 985b115264338c..ca10b5d31cd3fe 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h @@ -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 @@ -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);