Skip to content

Commit

Permalink
[Tizen] Implement Thread SRP service remove (#25371)
Browse files Browse the repository at this point in the history
* [Tizen] Implement mDNS service remove for Thread

* [Tizen] Register Thread mDNS service with TXT data

* Fix VerifyOrReturnError usage after rebase on master

* Safely cast size_t to uint8_t
  • Loading branch information
arkq authored and pull[bot] committed May 22, 2024
1 parent f0bb4c9 commit 1478460
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
79 changes: 61 additions & 18 deletions src/platform/Tizen/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
#include <lib/core/DataModelTypes.h>
#include <lib/dnssd/Constants.h>
#include <lib/dnssd/platform/Dnssd.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <lib/support/Span.h>
#include <lib/support/ThreadOperationalDataset.h>
#include <platform/CHIPDeviceConfig.h>
Expand Down Expand Up @@ -525,40 +527,81 @@ CHIP_ERROR ThreadStackManagerImpl::_AddSrpService(const char * aInstanceName, co
const Span<const Dnssd::TextEntry> & 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);

threadErr = thread_srp_client_register_service(mThreadInstance, aInstanceName, aName, aPort);
VerifyOrExit(threadErr == THREAD_ERROR_NONE || threadErr == THREAD_ERROR_ALREADY_DONE, error = CHIP_ERROR_INTERNAL);
int threadErr;

return CHIP_NO_ERROR;
std::vector<thread_dns_txt_entry_s> entries;
entries.reserve(aTxtEntries.size());

exit:
ChipLogError(DeviceLayer, "FAIL: thread_srp_client_register_service");
return error;
thread_dns_txt_entry_s * ee = entries.data();
for (auto & entry : aTxtEntries)
{
ee->key = entry.mKey;
ee->value = entry.mData;
VerifyOrReturnError(chip::CanCastTo<uint8_t>(entry.mDataSize), CHIP_ERROR_INVALID_ARGUMENT);
ee->value_len = static_cast<uint8_t>(entry.mDataSize);
ee++;
}

VerifyOrReturnError(chip::CanCastTo<uint8_t>(entries.size()), CHIP_ERROR_INVALID_ARGUMENT);
threadErr = thread_srp_client_register_service_full(mThreadInstance, aInstanceName, aName, aPort, 0, 0, entries.data(),
static_cast<uint8_t>(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));

SrpClientService service;
Platform::CopyString(service.mInstanceName, aInstanceName);
Platform::CopyString(service.mName, aName);
service.mPort = aPort;
mSrpClientServices.push_back(service);

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, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "thread_srp_client_remove_service() failed. ret: %d", threadErr));

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)
Expand Down
18 changes: 18 additions & 0 deletions src/platform/Tizen/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
#pragma once

#include <cstdint>
#include <vector>

#include <thread.h>

#include <app/AttributeAccessInterface.h>
#include <inet/IPAddress.h>
#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/dnssd/platform/Dnssd.h>
#include <lib/support/Span.h>
#include <lib/support/ThreadOperationalDataset.h>
#include <platform/CHIPDeviceConfig.h>
Expand Down Expand Up @@ -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";
Expand All @@ -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<SrpClientService> mSrpClientServices;
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
};

} // namespace DeviceLayer
Expand Down

0 comments on commit 1478460

Please sign in to comment.