diff --git a/src/platform/nxp/common/DnssdImpl.cpp b/src/platform/nxp/common/DnssdImpl.cpp index 5b35693534059f..65061aa80c4266 100644 --- a/src/platform/nxp/common/DnssdImpl.cpp +++ b/src/platform/nxp/common/DnssdImpl.cpp @@ -99,7 +99,7 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, intptr_t * browseIdentifier) { - if (ConnectivityMgr().IsWiFiStationConnected()) //|| ESP32Utils::HasIPv6LinkLocalAddress(ESP32Utils::kDefaultEthernetNetifKey)) + if (ConnectivityMgr().IsWiFiStationConnected()) { ReturnErrorOnFailure(NxpChipDnssdBrowse(type, protocol, addressType, interface, callback, context, browseIdentifier)); } @@ -119,7 +119,7 @@ CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) { - if (ConnectivityMgr().IsWiFiStationConnected()) //|| ESP32Utils::HasIPv6LinkLocalAddress(ESP32Utils::kDefaultEthernetNetifKey)) + if (ConnectivityMgr().IsWiFiStationConnected()) { ReturnErrorOnFailure(NxpChipDnssdResolve(service, interface, callback, context)); } diff --git a/src/platform/nxp/common/DnssdImplBr.cpp b/src/platform/nxp/common/DnssdImplBr.cpp index 5eab86fe197963..e65ae2d5ef331b 100644 --- a/src/platform/nxp/common/DnssdImplBr.cpp +++ b/src/platform/nxp/common/DnssdImplBr.cpp @@ -28,6 +28,8 @@ #include #include +#include + using namespace ::chip::DeviceLayer; using namespace chip::DeviceLayer::Internal; @@ -363,6 +365,11 @@ CHIP_ERROR NxpChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, if (type == nullptr || callback == nullptr) return CHIP_ERROR_INVALID_ARGUMENT; + if (mBrowseContext != nullptr) + { + NxpChipDnssdStopBrowse(reinterpret_cast(mBrowseContext)); + } + mBrowseContext = Platform::New(context, callback); VerifyOrReturnError(mBrowseContext != nullptr, CHIP_ERROR_NO_MEMORY); @@ -412,7 +419,8 @@ CHIP_ERROR NxpChipDnssdStopBrowse(intptr_t browseIdentifier) // that has been freed in DispatchBrowseEmpty. if ((true == bBrowseInProgress) && (browseContext)) { - browseContext->error = MapOpenThreadError(otMdnsStopBrowser(thrInstancePtr, &browseContext->mBrowseInfo)); + error = otMdnsStopBrowser(thrInstancePtr, &browseContext->mBrowseInfo); + browseContext->error = MapOpenThreadError(error); // browse context will be freed in DispatchBrowseEmpty DispatchBrowseEmpty(reinterpret_cast(browseContext)); @@ -430,6 +438,13 @@ CHIP_ERROR NxpChipDnssdResolve(DnssdService * browseResult, Inet::InterfaceId in otInstance * thrInstancePtr = ThreadStackMgrImpl().OTInstance(); + if (mResolveContext != nullptr) + { + // In case there is an ongoing query and NxpChipDnssdResolveNoLongerNeeded has not been called yet + // free the allocated context and do a proper cleanup of the previous transaction + NxpChipDnssdResolveNoLongerNeeded(mResolveContext->mMdnsService.mName); + } + mResolveContext = Platform::New(context, callback); VerifyOrReturnError(mResolveContext != nullptr, CHIP_ERROR_NO_MEMORY); @@ -450,12 +465,16 @@ CHIP_ERROR NxpChipDnssdResolve(DnssdService * browseResult, Inet::InterfaceId in mResolveContext->mSrvInfo.mServiceInstance = mResolveContext->mMdnsService.mName; mResolveContext->mSrvInfo.mServiceType = mResolveContext->mServiceType; - return MapOpenThreadError(otMdnsStartSrvResolver(thrInstancePtr, &mResolveContext->mSrvInfo)); + error = MapOpenThreadError(otMdnsStartSrvResolver(thrInstancePtr, &mResolveContext->mSrvInfo)); } - else + + if (error != CHIP_NO_ERROR) { - return error; + Platform::Delete(mResolveContext); + mResolveContext = nullptr; } + + return error; } void NxpChipDnssdResolveNoLongerNeeded(const char * instanceName) {