Skip to content

Commit

Permalink
Integrated SRP client API with the existing mDNS API. (#5343)
Browse files Browse the repository at this point in the history
The SRP and mDNS protocols should have some common layer
and API. For now it seems to be reasonable to make SRP client
to behave as mDNS implementation for Thread devices, so some
integration is necessary in order to reuse existing mDNS code.

* Replaced chip_mdns_advertiser and chip_enable_mdns arguments,
with one - chip_mdns acting as both of them.
* Added implementation of SetupHostname() method for Thread devices.
* Added MdnsImpl for the OpenThread platform.
* Expanded AddSrpService method by possibility to pass text entries.
* Added ifdef to not call AdvertiseCommissioning for Thread devices,
as they are not able to do that in unprovisioned state.
* Added ThreadStackManager method for getting device EUI64.
* Added starting mdns server to the Server.cpp InitServer() method,
as it was removed in the #5154 for platforms other than ESP32.
  • Loading branch information
kkasperczyk-no authored Mar 12, 2021
1 parent 571f843 commit 4401402
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 37 deletions.
4 changes: 4 additions & 0 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ chip_gn_arg_bool ("chip_inet_config_enable_dns_resolver" CONFIG_CHIP_BUILD_TE
chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL)
chip_gn_arg_bool ("chip_build_pw_rpc_lib" CONFIG_CHIP_PW_RPC)

if (CONFIG_CHIP_ENABLE_DNSSD_SRP)
chip_gn_arg_string("chip_mdns" "platform")
endif()

if (CHIP_PROJECT_CONFIG)
chip_gn_arg_string("chip_project_config_include" ${CHIP_PROJECT_CONFIG})
chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG})
Expand Down
5 changes: 5 additions & 0 deletions src/app/server/Mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ void StartServer()
}
else
{
// TODO: Thread devices are not able to advertise using mDNS before being provisioned,
// so configuraton should be added to enable commissioning advertising based on supported
// Rendezvous methods.
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD
err = app::Mdns::AdvertiseCommisioning();
#endif
}

if (err != CHIP_NO_ERROR)
Expand Down
5 changes: 5 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ void InitServer(AppDelegate * delegate)
#endif
}

// Starting mDNS server only for Thread devices due to problem reported in issue #5076.
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
app::Mdns::StartServer();
#endif

exit:
if (err != CHIP_NO_ERROR)
{
Expand Down
19 changes: 16 additions & 3 deletions src/include/platform/ThreadStackManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#pragma once

namespace chip {

namespace Mdns {
struct TextEntry;
}

namespace DeviceLayer {

class PlatformManagerImpl;
Expand Down Expand Up @@ -74,6 +79,7 @@ class ThreadStackManager
CHIP_ERROR GetAndLogThreadTopologyMinimal();
CHIP_ERROR GetAndLogThreadTopologyFull();
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf);
CHIP_ERROR GetFactoryAssignedEUI64(uint8_t (&buf)[8]);
CHIP_ERROR GetExternalIPv6Address(chip::Inet::IPAddress & addr);

CHIP_ERROR JoinerStart();
Expand All @@ -82,8 +88,8 @@ class ThreadStackManager
CHIP_ERROR SetThreadEnabled(bool val);

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
CHIP_ERROR AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort, uint32_t aLeaseInterval,
uint32_t aKeyLeaseInterval);
CHIP_ERROR AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort, chip::Mdns::TextEntry * aTxtEntries,
size_t aTxtEntiresSize, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval);
CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName);
CHIP_ERROR SetupSrpHost(const char * aHostName);
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
Expand Down Expand Up @@ -227,9 +233,11 @@ inline CHIP_ERROR ThreadStackManager::SetThreadEnabled(bool val)

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
inline CHIP_ERROR ThreadStackManager::AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
chip::Mdns::TextEntry * aTxtEntries, size_t aTxtEntiresSize,
uint32_t aLeaseInterval = 0, uint32_t aKeyLeaseInterval = 0)
{
return static_cast<ImplClass *>(this)->_AddSrpService(aInstanceName, aName, aPort, aLeaseInterval, aKeyLeaseInterval);
return static_cast<ImplClass *>(this)->_AddSrpService(aInstanceName, aName, aPort, aTxtEntries, aTxtEntiresSize, aLeaseInterval,
aKeyLeaseInterval);
}

inline CHIP_ERROR ThreadStackManager::RemoveSrpService(const char * aInstanceName, const char * aName)
Expand Down Expand Up @@ -333,6 +341,11 @@ inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
}

inline CHIP_ERROR ThreadStackManager::GetFactoryAssignedEUI64(uint8_t (&buf)[8])
{
return static_cast<ImplClass *>(this)->_GetFactoryAssignedEUI64(buf);
}

inline CHIP_ERROR ThreadStackManager::GetExternalIPv6Address(chip::Inet::IPAddress & addr)
{
return static_cast<ImplClass *>(this)->_GetExternalIPv6Address(addr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GenericConfigurationManagerImpl
CHIP_ERROR _GetPrimaryWiFiMACAddress(uint8_t * buf);
CHIP_ERROR _StorePrimaryWiFiMACAddress(const uint8_t * buf);
CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);
CHIP_ERROR _GetFactoryAssignedEUI64(uint8_t (&buf)[8]);
CHIP_ERROR _StorePrimary802154MACAddress(const uint8_t * buf);
CHIP_ERROR _GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth);
CHIP_ERROR _StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen);
Expand Down
17 changes: 3 additions & 14 deletions src/lib/mdns/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
import("//build_overrides/chip.gni")
import("${chip_root}/src/platform/device.gni")

declare_args() {
# Set up what advertiser to use for mDNS advertisement
chip_mdns_advertiser = "minimal"
}

source_set("platform_header") {
sources = [ "platform/Mdns.h" ]
}
Expand All @@ -33,24 +28,18 @@ static_library("mdns") {

sources = [ "Advertiser.h" ]

if (chip_enable_mdns) {
_chip_mdns_advertiser = chip_mdns_advertiser
} else {
_chip_mdns_advertiser = "none"
}

if (_chip_mdns_advertiser == "none") {
if (chip_mdns == "none") {
sources += [
"Advertiser_ImplNone.cpp",
"Resolver_ImplNone.cpp",
]
} else if (_chip_mdns_advertiser == "minimal") {
} else if (chip_mdns == "minimal") {
sources += [
"Advertiser_ImplMinimalMdns.cpp",
"Resolver_ImplNone.cpp",
]
public_deps += [ "${chip_root}/src/lib/mdns/minimal" ]
} else if (_chip_mdns_advertiser == "platform") {
} else if (chip_mdns == "platform") {
sources += [
"Discovery_ImplPlatform.cpp",
"Discovery_ImplPlatform.h",
Expand Down
19 changes: 18 additions & 1 deletion src/lib/mdns/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t po
{
ChipLogError(Discovery, "Failed to initialize platform mdns: %s", ErrorStr(error));
}

error = SetupHostname();
if (error != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to setup mdns hostname: %s", ErrorStr(error));
}

return error;
}

Expand Down Expand Up @@ -141,6 +148,13 @@ CHIP_ERROR DiscoveryImplPlatform::GenerateRotatingDeviceId(char rotatingDeviceId

CHIP_ERROR DiscoveryImplPlatform::SetupHostname()
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
static char hostname[17]; // Hostname is 64-bit EUI-64 expressed as a 16-character hexadecimal string.
uint8_t eui64[8];
chip::DeviceLayer::ThreadStackMgr().GetFactoryAssignedEUI64(eui64);
snprintf(hostname, sizeof(hostname), "%02X%02X%02X%02X%02X%02X%02X%02X", eui64[0], eui64[1], eui64[2], eui64[3], eui64[4],
eui64[5], eui64[6], eui64[7]);
#else
uint8_t mac[6]; // 6 byte wifi mac
char hostname[13]; // Hostname will be the hex representation of mac.

Expand All @@ -149,6 +163,8 @@ CHIP_ERROR DiscoveryImplPlatform::SetupHostname()
{
snprintf(&hostname[i * 2], sizeof(hostname) - i * 2, "%02X", mac[i]);
}
#endif

ReturnErrorOnFailure(ChipMdnsSetHostname(hostname));

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -255,7 +271,8 @@ CHIP_ERROR DiscoveryImplPlatform::Advertise(const OperationalAdvertisingParamete

mOperationalAdvertisingParams = params;
// TODO: There may be multilple device/fabrid ids after multi-admin.
snprintf(service.mName, sizeof(service.mName), "%" PRIX64 "-%" PRIX64, params.GetNodeId(), params.GetFabricId());
snprintf(service.mName, sizeof(service.mName), "%08X%08X-%08X%08X", (uint32_t)(params.GetNodeId() >> 32),
(uint32_t)(params.GetNodeId()), (uint32_t)(params.GetFabricId() >> 32), (uint32_t)(params.GetFabricId()));
strncpy(service.mType, "_chip", sizeof(service.mType));
service.mProtocol = MdnsServiceProtocol::kMdnsProtocolTcp;
service.mPort = CHIP_PORT;
Expand Down
7 changes: 4 additions & 3 deletions src/lib/mdns/platform/Mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
namespace chip {
namespace Mdns {

static constexpr uint8_t kMdnsNameMaxSize = 33;
static constexpr uint8_t kMdnsTypeMaxSize = 32;
static constexpr uint16_t kMdnsTextMaxSize = 64;
static constexpr uint8_t kMdnsNameMaxSize = 33;
static constexpr uint8_t kMdnsProtocolTextMaxSize = 8;
static constexpr uint8_t kMdnsTypeMaxSize = 32;
static constexpr uint16_t kMdnsTextMaxSize = 64;

enum class MdnsServiceProtocol : uint8_t
{
Expand Down
13 changes: 9 additions & 4 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (chip_enable_openthread) {
}
}

if (chip_device_platform == "linux" && chip_enable_mdns) {
if (chip_device_platform == "linux" && chip_mdns != "none") {
pkg_config("avahi_client_config") {
packages = [ "avahi-client" ]
}
Expand Down Expand Up @@ -75,7 +75,7 @@ if (chip_device_platform != "none") {
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE=${chip_enable_ble}" ]
}

if (chip_enable_mdns) {
if (chip_mdns != "none") {
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_MDNS=1" ]
}

Expand Down Expand Up @@ -241,7 +241,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"${chip_root}/src:includes",
]

if (chip_enable_mdns) {
if (chip_mdns != "none") {
public_deps += [ "${chip_root}/src/lib/mdns:platform_header" ]
}

Expand Down Expand Up @@ -494,7 +494,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
"Linux/bluez/Types.h",
]

if (chip_enable_mdns) {
if (chip_mdns != "none") {
sources += [
"Linux/MdnsImpl.cpp",
"Linux/MdnsImpl.h",
Expand Down Expand Up @@ -595,6 +595,11 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
}
}

if (chip_enable_openthread && chip_mdns == "platform" &&
chip_device_platform != "linux") {
sources += [ "OpenThread/MdnsImpl.cpp" ]
}

allow_circular_includes_from = [ "${chip_root}/src/lib/support" ]
}
} else if (chip_device_platform == "external") {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ CHIP_ERROR ThreadStackManagerImpl::_GetPrimary802154MACAddress(uint8_t * buf)
return OTBR_TO_CHIP_ERROR(error);
}

CHIP_ERROR ThreadStackManagerImpl::_GetFactoryAssignedEUI64(uint8_t (&buf)[8])
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR ThreadStackManagerImpl::_GetExternalIPv6Address(chip::Inet::IPAddress & addr)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/ThreadStackManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class ThreadStackManagerImpl : public ThreadStackManager

CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);

CHIP_ERROR _GetFactoryAssignedEUI64(uint8_t (&buf)[8]);

CHIP_ERROR _GetExternalIPv6Address(chip::Inet::IPAddress & addr);

CHIP_ERROR _JoinerStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnOpenThreadStateChang
event.ThreadStateChange.NetDataChanged = (flags & OT_CHANGED_THREAD_NETDATA) != 0;
event.ThreadStateChange.ChildNodesChanged = (flags & (OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0;
event.ThreadStateChange.OpenThread.Flags = flags;

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
if (event.ThreadStateChange.AddressChanged)
{
const otSrpClientHostInfo * hostInfo =
otSrpClientGetHostInfo(static_cast<GenericThreadStackManagerImpl_OpenThread *>(context)->Impl()->OTInstance());
if (hostInfo && hostInfo->mName)
{
static_cast<GenericThreadStackManagerImpl_OpenThread *>(context)->Impl()->_SetupSrpHost(hostInfo->mName);
}
}
#endif

PlatformMgr().PostEvent(&event);
}

Expand Down Expand Up @@ -817,6 +830,15 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetPrimary80215
return CHIP_NO_ERROR;
};

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetFactoryAssignedEUI64(uint8_t (&buf)[8])
{
otExtAddress extendedAddr;
otLinkGetFactoryAssignedIeeeEui64(mOTInst, &extendedAddr);
memcpy(buf, extendedAddr.m8, sizeof(extendedAddr.m8));
return CHIP_NO_ERROR;
};

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_GetExternalIPv6Address(chip::Inet::IPAddress & addr)
{
Expand Down Expand Up @@ -875,7 +897,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
// state change occurs. Note that we reference the OnOpenThreadStateChange method
// on the concrete implementation class so that that class can override the default
// method implementation if it chooses to.
otErr = otSetStateChangedCallback(otInst, ImplClass::OnOpenThreadStateChange, NULL);
otErr = otSetStateChangedCallback(otInst, ImplClass::OnOpenThreadStateChange, this);
VerifyOrExit(otErr == OT_ERROR_NONE, err = MapOpenThreadError(otErr));

// Enable automatic assignment of Thread advertised addresses.
Expand Down Expand Up @@ -1116,7 +1138,8 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnSrpClientStateChange

template <class ImplClass>
CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_AddSrpService(const char * aInstanceName, const char * aName,
uint16_t aPort, uint32_t aLeaseInterval,
uint16_t aPort, chip::Mdns::TextEntry * aTxtEntries,
size_t aTxtEntiresSize, uint32_t aLeaseInterval,
uint32_t aKeyLeaseInterval)
{
CHIP_ERROR error = CHIP_NO_ERROR;
Expand Down Expand Up @@ -1158,6 +1181,29 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_AddSrpService(c

srpService->mService.mPort = aPort;

// Check if there are some optional text entries to add.
if (aTxtEntries && aTxtEntiresSize != 0)
{
VerifyOrExit(aTxtEntiresSize <= SrpClient::kMaxTxtEntriesNumber, error = CHIP_ERROR_INVALID_LIST_LENGTH);

srpService->mService.mNumTxtEntries = static_cast<uint8_t>(aTxtEntiresSize);

for (uint8_t entryId = 0; entryId < aTxtEntiresSize; entryId++)
{
VerifyOrExit(aTxtEntries[entryId].mDataSize <= SrpClient::kMaxTxtValueSize, error = CHIP_ERROR_BUFFER_TOO_SMALL);
VerifyOrExit((strlen(aTxtEntries[entryId].mKey) + 1) <= SrpClient::kMaxTxtKeySize, error = CHIP_ERROR_BUFFER_TOO_SMALL);

srpService->mTxtEntries[entryId].mValueLength = static_cast<uint8_t>(aTxtEntries[entryId].mDataSize);
memcpy(&(srpService->mTxtValueBuffers[entryId][0]), aTxtEntries[entryId].mData, aTxtEntries[entryId].mDataSize);
srpService->mTxtEntries[entryId].mValue = &(srpService->mTxtValueBuffers[entryId][0]);

memcpy(&(srpService->mTxtKeyBuffers[entryId][0]), aTxtEntries[entryId].mKey, strlen(aTxtEntries[entryId].mKey) + 1);
srpService->mTxtEntries[entryId].mKey = &(srpService->mTxtKeyBuffers[entryId][0]);
}

srpService->mService.mTxtEntries = srpService->mTxtEntries;
}

error = MapOpenThreadError(otSrpClientAddService(mOTInst, &(srpService->mService)));

exit:
Expand Down Expand Up @@ -1214,11 +1260,13 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_SetupSrpHost(co
error = MapOpenThreadError(otSrpClientSetHostName(mOTInst, aHostName));
SuccessOrExit(error);

error = ThreadStackMgr().GetExternalIPv6Address(hostAddress);
SuccessOrExit(error);

memcpy(&mSrpClient.mHostAddress.mFields.m32, hostAddress.Addr, sizeof(hostAddress.Addr));
error = MapOpenThreadError(otSrpClientSetHostAddresses(mOTInst, &mSrpClient.mHostAddress, 1));
// Check if device has any external IPv6 assigned. If not, host will be set without IPv6 addresses
// and updated later on.
if (ThreadStackMgr().GetExternalIPv6Address(hostAddress) == CHIP_NO_ERROR)
{
memcpy(&mSrpClient.mHostAddress.mFields.m32, hostAddress.Addr, sizeof(hostAddress.Addr));
error = MapOpenThreadError(otSrpClientSetHostAddresses(mOTInst, &mSrpClient.mHostAddress, 1));
}

exit:
Impl()->UnlockThreadStack();
Expand Down
Loading

0 comments on commit 4401402

Please sign in to comment.