From 80b712eb23cbd4a874440ae0727d83b7057e6273 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 9 Sep 2022 17:30:13 +0200 Subject: [PATCH 1/4] [Tizen] Implement mDNS service remove for Thread --- src/platform/Tizen/ThreadStackManagerImpl.cpp | 63 ++++++++++++++----- src/platform/Tizen/ThreadStackManagerImpl.h | 18 ++++++ 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 233f28b87b0293..3681a421966da8 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -525,40 +526,68 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co const Span & aTxtEntries, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval) { - ChipLogDetail(DeviceLayer, "%s +", __func__); - CHIP_ERROR error = CHIP_NO_ERROR; - int threadErr = THREAD_ERROR_NONE; - VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); - VerifyOrExit(aInstanceName, error = CHIP_ERROR_INVALID_ARGUMENT); - VerifyOrExit(aName, error = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(aInstanceName != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(aName != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + + int threadErr; threadErr = thread_srp_client_register_service(mThreadInstance, aInstanceName, aName, aPort); - VerifyOrExit(threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, error = CHIP_ERROR_INTERNAL); + VerifyOrReturnError( + threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, + (ChipLogError(DeviceLayer, "thread_srp_client_register_service() failed. ret: %d", threadErr), CHIP_ERROR_INTERNAL)); - return CHIP_NO_ERROR; + SrpClientService service; + Platform::CopyString(service.mInstanceName, aInstanceName); + Platform::CopyString(service.mName, aName); + service.mPort = aPort; + mSrpClientServices.push_back(service); -exit: - ChipLogError(DeviceLayer, "FAIL: thread_srp_client_register_service"); - return error; + return CHIP_NO_ERROR; } CHIP_ERROR ThreadStackManagerImpl::_RemoveSrpService(const char * aInstanceName, const char * aName) { - ChipLogError(DeviceLayer, "Not implemented"); - return CHIP_ERROR_NOT_IMPLEMENTED; + VerifyOrReturnError(mIsInitialized, CHIP_ERROR_WELL_UNINITIALIZED); + VerifyOrReturnError(aInstanceName != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(aName != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + + int threadErr; + + threadErr = thread_srp_client_remove_service(mThreadInstance, aInstanceName, aName); + VerifyOrReturnError( + threadErr == THREAD_ERROR_NONE, + (ChipLogError(DeviceLayer, "thread_srp_client_remove_service() failed. ret: %d", threadErr), CHIP_ERROR_INTERNAL)); + + return CHIP_NO_ERROR; } CHIP_ERROR ThreadStackManagerImpl::_InvalidateAllSrpServices() { - ChipLogError(DeviceLayer, "Not implemented"); - return CHIP_ERROR_NOT_IMPLEMENTED; + for (auto & service : mSrpClientServices) + { + service.mValid = false; + } + return CHIP_NO_ERROR; } CHIP_ERROR ThreadStackManagerImpl::_RemoveInvalidSrpServices() { - ChipLogError(DeviceLayer, "Not implemented"); - return CHIP_ERROR_NOT_IMPLEMENTED; + for (auto it = mSrpClientServices.begin(); it != mSrpClientServices.end();) + { + if (!it->mValid) + { + auto err = _RemoveSrpService(it->mInstanceName, it->mName); + VerifyOrReturnError(err == CHIP_NO_ERROR, err); + it = mSrpClientServices.erase(it); + } + else + { + ++it; + } + } + + return CHIP_NO_ERROR; } void ThreadStackManagerImpl::_ThreadIpAddressCb(int index, char * ipAddr, thread_ipaddr_type_e ipAddrType, void * userData) diff --git a/src/platform/Tizen/ThreadStackManagerImpl.h b/src/platform/Tizen/ThreadStackManagerImpl.h index 96cc7c715036ef..018e8a5358f199 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.h +++ b/src/platform/Tizen/ThreadStackManagerImpl.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include @@ -31,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -127,6 +129,19 @@ class ThreadStackManagerImpl : public ThreadStackManager #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT private: + static constexpr size_t kSrpServiceInstanceNameSize = Dnssd::Common::kInstanceNameMaxLength + 1; // add null-terminator + static constexpr size_t kSrpServiceNameSize = Dnssd::Common::kSubTypeTotalLength + 1; // add null-terminator + +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + struct SrpClientService + { + char mInstanceName[kSrpServiceInstanceNameSize]; + char mName[kSrpServiceNameSize]; + uint16_t mPort; + bool mValid = true; + }; +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + static constexpr char kOpenthreadDeviceRoleDisabled[] = "disabled"; static constexpr char kOpenthreadDeviceRoleDetached[] = "detached"; static constexpr char kOpenthreadDeviceRoleChild[] = "child"; @@ -153,6 +168,9 @@ class ThreadStackManagerImpl : public ThreadStackManager bool mIsAttached; bool mIsInitialized; thread_instance_h mThreadInstance; +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT + std::vector mSrpClientServices; +#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT }; } // namespace DeviceLayer From ae6ea599904ab6c5a3ff0e615395f46642d46cd4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Fri, 9 Sep 2022 17:31:27 +0200 Subject: [PATCH 2/4] [Tizen] Register Thread mDNS service with TXT data --- src/platform/Tizen/ThreadStackManagerImpl.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 3681a421966da8..8120d866beff47 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -532,7 +532,20 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co int threadErr; - threadErr = thread_srp_client_register_service(mThreadInstance, aInstanceName, aName, aPort); + std::vector entries; + entries.reserve(aTxtEntries.size()); + + thread_dns_txt_entry_s * ee = entries.data(); + for (auto & entry : aTxtEntries) + { + ee->key = entry.mKey; + ee->value = entry.mData; + ee->value_len = entry.mDataSize; + ee++; + } + + threadErr = + thread_srp_client_register_service_full(mThreadInstance, aInstanceName, aName, aPort, 0, 0, entries.data(), entries.size()); VerifyOrReturnError( threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, (ChipLogError(DeviceLayer, "thread_srp_client_register_service() failed. ret: %d", threadErr), CHIP_ERROR_INTERNAL)); From af02a31edc0905c9f171044f2c7d246708d4bd0a Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 28 Feb 2023 14:12:22 +0100 Subject: [PATCH 3/4] Fix VerifyOrReturnError usage after rebase on master --- src/platform/Tizen/ThreadStackManagerImpl.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 8120d866beff47..4a1fac5373ec6d 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -546,9 +546,8 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co threadErr = thread_srp_client_register_service_full(mThreadInstance, aInstanceName, aName, aPort, 0, 0, entries.data(), entries.size()); - VerifyOrReturnError( - threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, - (ChipLogError(DeviceLayer, "thread_srp_client_register_service() failed. ret: %d", threadErr), CHIP_ERROR_INTERNAL)); + VerifyOrReturnError(threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, CHIP_ERROR_INTERNAL, + ChipLogError(DeviceLayer, "thread_srp_client_register_service() failed. ret: %d", threadErr)); SrpClientService service; Platform::CopyString(service.mInstanceName, aInstanceName); @@ -568,9 +567,8 @@ CHIP_ERROR ThreadStackManagerImpl::_RemoveSrpService(const char * aInstanceName, int threadErr; threadErr = thread_srp_client_remove_service(mThreadInstance, aInstanceName, aName); - VerifyOrReturnError( - threadErr == THREAD_ERROR_NONE, - (ChipLogError(DeviceLayer, "thread_srp_client_remove_service() failed. ret: %d", threadErr), CHIP_ERROR_INTERNAL)); + VerifyOrReturnError(threadErr == THREAD_ERROR_NONE, CHIP_ERROR_INTERNAL, + ChipLogError(DeviceLayer, "thread_srp_client_remove_service() failed. ret: %d", threadErr)); return CHIP_NO_ERROR; } From 8ca866e07e12dea126c1357068ec98e1c1a15e39 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Tue, 14 Mar 2023 09:04:28 +0100 Subject: [PATCH 4/4] Safely cast size_t to uint8_t --- src/platform/Tizen/ThreadStackManagerImpl.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/platform/Tizen/ThreadStackManagerImpl.cpp b/src/platform/Tizen/ThreadStackManagerImpl.cpp index 4a1fac5373ec6d..137497afb76526 100644 --- a/src/platform/Tizen/ThreadStackManagerImpl.cpp +++ b/src/platform/Tizen/ThreadStackManagerImpl.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -538,14 +539,16 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co thread_dns_txt_entry_s * ee = entries.data(); for (auto & entry : aTxtEntries) { - ee->key = entry.mKey; - ee->value = entry.mData; - ee->value_len = entry.mDataSize; + ee->key = entry.mKey; + ee->value = entry.mData; + VerifyOrReturnError(chip::CanCastTo(entry.mDataSize), CHIP_ERROR_INVALID_ARGUMENT); + ee->value_len = static_cast(entry.mDataSize); ee++; } - threadErr = - thread_srp_client_register_service_full(mThreadInstance, aInstanceName, aName, aPort, 0, 0, entries.data(), entries.size()); + VerifyOrReturnError(chip::CanCastTo(entries.size()), CHIP_ERROR_INVALID_ARGUMENT); + threadErr = thread_srp_client_register_service_full(mThreadInstance, aInstanceName, aName, aPort, 0, 0, entries.data(), + static_cast(entries.size())); VerifyOrReturnError(threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, CHIP_ERROR_INTERNAL, ChipLogError(DeviceLayer, "thread_srp_client_register_service() failed. ret: %d", threadErr));