From 1980674b441ae48b499db683a0340e0fd1d39dd6 Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk <66371704+kkasperczyk-no@users.noreply.github.com> Date: Fri, 7 May 2021 23:03:30 +0200 Subject: [PATCH] Added removing specific SRP service before adding new one. (#6539) SRP doesn't allow to update services, so in case of registering multiple times the same service (e.g. because of updating its record content) only first attempt will succeed and the following will end with OT_DUPLICATED result. * In MdnsImpl added removing specific service before adding it, to make sure that it won't exist in the moment of adding its the most present version. --- src/platform/OpenThread/MdnsImpl.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/platform/OpenThread/MdnsImpl.cpp b/src/platform/OpenThread/MdnsImpl.cpp index dc1d6fa3b41d96..8f920f3924c890 100644 --- a/src/platform/OpenThread/MdnsImpl.cpp +++ b/src/platform/OpenThread/MdnsImpl.cpp @@ -18,6 +18,8 @@ #include "lib/mdns/platform/Mdns.h" #include +#include +#include using namespace ::chip::DeviceLayer; @@ -42,14 +44,24 @@ const char * GetProtocolString(MdnsServiceProtocol protocol) CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) { - if (service == nullptr) - return CHIP_ERROR_INVALID_ARGUMENT; + CHIP_ERROR result = CHIP_NO_ERROR; + + VerifyOrExit(service, result = CHIP_ERROR_INVALID_ARGUMENT); char serviceType[kMdnsTypeMaxSize + kMdnsProtocolTextMaxSize + 1]; snprintf(serviceType, sizeof(serviceType), "%s.%s", service->mType, GetProtocolString(service->mProtocol)); - return ThreadStackMgr().AddSrpService(service->mName, serviceType, service->mPort, service->mTextEntries, - service->mTextEntrySize); + // Try to remove service before adding it, as SRP doesn't allow to update existing services. + result = ThreadStackMgr().RemoveSrpService(service->mName, serviceType); + + // Service should be successfully removed or not found (not exists). + VerifyOrExit((result == CHIP_NO_ERROR) || (result == Internal::MapOpenThreadError(OT_ERROR_NOT_FOUND)), ); + + result = + ThreadStackMgr().AddSrpService(service->mName, serviceType, service->mPort, service->mTextEntries, service->mTextEntrySize); + +exit: + return result; } CHIP_ERROR ChipMdnsStopPublish()