Skip to content

Commit

Permalink
[DNS-SD] Clean up common constants (#10553)
Browse files Browse the repository at this point in the history
* [DNS-SD] Clean up common constants

DNS-SD constants are currently defined in many places, some
constants are duplicated, and there are constants that have
similar names, but mean different things (like the service
instance name for the commissionable and operational node,
respectively).

Try to clean them up by putting most of them in a single
lib/dnssd/Constants.h header file. Also, remove unnecessary
kDnssd prefixes since the constants are already in the
Dnssd namespace.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Nov 30, 2021
1 parent 2a00e11 commit 7440755
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAdd
ChipLogDetail(AppServer, "SendUserDirectedCommissioningRequest2");

CHIP_ERROR err;
char nameBuffer[chip::Dnssd::kMaxInstanceNameSize + 1];
char nameBuffer[chip::Dnssd::Commissionable::kInstanceNameMaxLength + 1];
err = app::DnssdServer::Instance().GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer));
if (err != CHIP_NO_ERROR)
{
Expand Down
14 changes: 0 additions & 14 deletions src/lib/dnssd/Advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ static constexpr uint16_t kMdnsPort = 5353;
// Need 8 bytes to fit a thread mac.
static constexpr size_t kMaxMacSize = 8;

// Commissionable/commissioner node subtypes
static constexpr size_t kSubTypeShortDiscriminatorMaxLength = 4; // _S<dd>
static constexpr size_t kSubTypeLongDiscriminatorMaxLength = 6; // _L<dddd>
static constexpr size_t kSubTypeVendorMaxLength = 7; // _V<ddddd>
static constexpr size_t kSubTypeDeviceTypeMaxLength = 5; // _T<ddd>
static constexpr size_t kSubTypeCommissioningModeMaxLength = 3; // _C<d>
static constexpr size_t kSubTypeAdditionalCommissioningMaxLength = 3; // _A<d>
static constexpr size_t kSubTypeCompressedFabricIdMaxLength = 18; //_I<16-hex-digits>
// These are the max vals for comissioning adverts
static constexpr size_t kSubTypeMaxNumber = 6;
static constexpr size_t kSubTypeTotalLength = kSubTypeShortDiscriminatorMaxLength + kSubTypeLongDiscriminatorMaxLength +
kSubTypeVendorMaxLength + kSubTypeDeviceTypeMaxLength + kSubTypeCommissioningModeMaxLength +
kSubTypeAdditionalCommissioningMaxLength;

enum class CommssionAdvertiseMode : uint8_t
{
kCommissionableNode,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ QueryResponderAllocator<AdvertiserMinMdns::kMaxOperationalRecords> * AdvertiserM

CHIP_ERROR AdvertiserMinMdns::Advertise(const OperationalAdvertisingParameters & params)
{
char nameBuffer[kOperationalServiceNamePrefix + 1] = "";
char nameBuffer[Operational::kInstanceNameMaxLength + 1] = "";

/// need to set server name
ReturnErrorOnFailure(MakeInstanceName(nameBuffer, sizeof(nameBuffer), params.GetPeerId()));
Expand Down Expand Up @@ -439,7 +439,7 @@ CHIP_ERROR AdvertiserMinMdns::Advertise(const OperationalAdvertisingParameters &

CHIP_ERROR AdvertiserMinMdns::GetCommissionableInstanceName(char * instanceName, size_t maxLength)
{
if (maxLength < (kMaxInstanceNameSize + 1))
if (maxLength < (Commissionable::kInstanceNameMaxLength + 1))
{
return CHIP_ERROR_NO_MEMORY;
}
Expand Down
97 changes: 97 additions & 0 deletions src/lib/dnssd/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <lib/support/Fold.h>

#include <algorithm>
#include <initializer_list>

namespace chip {
namespace Dnssd {

/*
* Matter DNS host settings
*/

constexpr size_t kHostNameMaxLength = 16; // MAC or 802.15.4 Extended Address in hex

/*
* Matter DNS service subtypes
*/

constexpr size_t kSubTypeShortDiscriminatorMaxLength = 4; // _S<dd>
constexpr size_t kSubTypeLongDiscriminatorMaxLength = 6; // _L<dddd>
constexpr size_t kSubTypeVendorMaxLength = 7; // _V<ddddd>
constexpr size_t kSubTypeDeviceTypeMaxLength = 5; // _T<ddd>
constexpr size_t kSubTypeCommissioningModeMaxLength = 3; // _C<d>
constexpr size_t kSubTypeAdditionalCommissioningMaxLength = 3; // _A<d>
constexpr size_t kSubTypeCompressedFabricIdMaxLength = 18; // _I<16-hex-digits>

/*
* Matter operational node service settings
*/

namespace Operational {

#define SUBTYPES (std::initializer_list<size_t>{ kSubTypeCompressedFabricIdMaxLength })

constexpr size_t kInstanceNameMaxLength = 33; // <NodeId>-<FabricId> in hex (16 + 1 + 16)
constexpr size_t kSubTypeMaxNumber = SUBTYPES.size();
constexpr size_t kSubTypeMaxLength = std::max(SUBTYPES);
constexpr size_t kSubTypeTotalLength = chip::Sum(SUBTYPES);

#undef SUBTYPES

} // namespace Operational

/*
* Matter commissionable/commissioner node service constants.
*/

namespace Commissionable {

#define SUBTYPES \
(std::initializer_list<size_t>{ kSubTypeShortDiscriminatorMaxLength, kSubTypeLongDiscriminatorMaxLength, \
kSubTypeVendorMaxLength, kSubTypeDeviceTypeMaxLength, kSubTypeCommissioningModeMaxLength, \
kSubTypeAdditionalCommissioningMaxLength })

constexpr size_t kInstanceNameMaxLength = 16; // 64-bit random number in hex
constexpr size_t kSubTypeMaxNumber = SUBTYPES.size();
constexpr size_t kSubTypeMaxLength = std::max(SUBTYPES);
constexpr size_t kSubTypeTotalLength = chip::Sum(SUBTYPES);

#undef SUBTYPES

} // namespace Commissionable

/*
* Constants for any Matter service.
*/

namespace Common {

constexpr size_t kInstanceNameMaxLength = std::max(Operational::kInstanceNameMaxLength, Commissionable::kInstanceNameMaxLength);
constexpr size_t kSubTypeMaxNumber = std::max(Operational::kSubTypeMaxNumber, Commissionable::kSubTypeMaxNumber);
constexpr size_t kSubTypeMaxLength = std::max(Operational::kSubTypeMaxLength, Commissionable::kSubTypeMaxLength);
constexpr size_t kSubTypeTotalLength = std::max(Operational::kSubTypeTotalLength, Commissionable::kSubTypeTotalLength);

} // namespace Common

} // namespace Dnssd
} // namespace chip
6 changes: 3 additions & 3 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void DiscoveryImplPlatform::HandleDnssdError(void * context, CHIP_ERROR error)

CHIP_ERROR DiscoveryImplPlatform::GetCommissionableInstanceName(char * instanceName, size_t maxLength)
{
if (maxLength < (chip::Dnssd::kMaxInstanceNameSize + 1))
if (maxLength < (chip::Dnssd::Commissionable::kInstanceNameMaxLength + 1))
{
return CHIP_ERROR_NO_MEMORY;
}
Expand Down Expand Up @@ -217,7 +217,7 @@ CHIP_ERROR DiscoveryImplPlatform::Advertise(const CommissionAdvertisingParameter
char commissioningModeSubType[kSubTypeCommissioningModeMaxLength + 1];
char deviceTypeSubType[kSubTypeDeviceTypeMaxLength + 1];
// size of subTypes array should be count of SubTypes above
const char * subTypes[kSubTypeMaxNumber];
const char * subTypes[Commissionable::kSubTypeMaxNumber];
size_t subTypeSize = 0;

if (!mDnssdInitialized)
Expand Down Expand Up @@ -394,7 +394,7 @@ CHIP_ERROR DiscoveryImplPlatform::Advertise(const OperationalAdvertisingParamete
CHIP_ERROR error = CHIP_NO_ERROR;

char compressedFabricIdSub[kSubTypeCompressedFabricIdMaxLength + 1];
const char * subTypes[1];
const char * subTypes[Operational::kSubTypeMaxNumber];
size_t subTypeSize = 0;

mOperationalAdvertisingParams = params;
Expand Down
22 changes: 10 additions & 12 deletions src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
#include <lib/core/CHIPError.h>
#include <lib/core/Optional.h>
#include <lib/core/PeerId.h>
#include <lib/dnssd/Constants.h>
#include <lib/support/BytesToHex.h>

namespace chip {
namespace Dnssd {

// Largest host name is 64-bits in hex.
static constexpr int kMaxHostNameSize = 16;
constexpr uint32_t kUndefinedRetryInterval = std::numeric_limits<uint32_t>::max();

struct ResolvedNodeData
Expand Down Expand Up @@ -63,26 +62,25 @@ struct ResolvedNodeData
}

PeerId mPeerId;
Inet::IPAddress mAddress = Inet::IPAddress::Any;
Inet::InterfaceId mInterfaceId = INET_NULL_INTERFACEID;
uint16_t mPort = 0;
char mHostName[kMaxHostNameSize + 1] = {};
bool mSupportsTcp = false;
uint32_t mMrpRetryIntervalIdle = kUndefinedRetryInterval;
uint32_t mMrpRetryIntervalActive = kUndefinedRetryInterval;
Inet::IPAddress mAddress = Inet::IPAddress::Any;
Inet::InterfaceId mInterfaceId = INET_NULL_INTERFACEID;
uint16_t mPort = 0;
char mHostName[kHostNameMaxLength + 1] = {};
bool mSupportsTcp = false;
uint32_t mMrpRetryIntervalIdle = kUndefinedRetryInterval;
uint32_t mMrpRetryIntervalActive = kUndefinedRetryInterval;
};

constexpr size_t kMaxDeviceNameLen = 32;
constexpr size_t kMaxRotatingIdLen = 50;
constexpr size_t kMaxPairingInstructionLen = 128;

static constexpr int kMaxInstanceNameSize = 16;
struct DiscoveredNodeData
{
// TODO(cecille): is 4 OK? IPv6 LL, GUA, ULA, IPv4?
static constexpr int kMaxIPAddresses = 5;
char hostName[kMaxHostNameSize + 1];
char instanceName[kMaxInstanceNameSize + 1];
char hostName[kHostNameMaxLength + 1];
char instanceName[Commissionable::kInstanceNameMaxLength + 1];
uint16_t longDiscriminator;
uint16_t vendorId;
uint16_t productId;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dnssd/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ CHIP_ERROR MinMdnsResolver::BrowseNodes(DiscoveryType type, DiscoveryFilter filt
}
else
{
char subtypeStr[kMaxSubtypeDescSize];
char subtypeStr[Common::kSubTypeMaxLength + 1];
ReturnErrorOnFailure(MakeServiceSubtype(subtypeStr, sizeof(subtypeStr), filter));
qname = CheckAndAllocateQName(subtypeStr, kSubtypeServiceNamePart, kCommissionableServiceName, kCommissionProtocol,
kLocalDomain);
Expand All @@ -483,7 +483,7 @@ CHIP_ERROR MinMdnsResolver::BrowseNodes(DiscoveryType type, DiscoveryFilter filt
}
else
{
char subtypeStr[kMaxSubtypeDescSize];
char subtypeStr[Common::kSubTypeMaxLength + 1];
ReturnErrorOnFailure(MakeServiceSubtype(subtypeStr, sizeof(subtypeStr), filter));
qname = CheckAndAllocateQName(subtypeStr, kSubtypeServiceNamePart, kCommissionerServiceName, kCommissionProtocol,
kLocalDomain);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/ServiceNaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Dnssd {

CHIP_ERROR MakeInstanceName(char * buffer, size_t bufferLen, const PeerId & peerId)
{
ReturnErrorCodeIf(bufferLen <= kOperationalServiceNamePrefix, CHIP_ERROR_BUFFER_TOO_SMALL);
ReturnErrorCodeIf(bufferLen <= Operational::kInstanceNameMaxLength, CHIP_ERROR_BUFFER_TOO_SMALL);

NodeId nodeId = peerId.GetNodeId();
CompressedFabricId fabricId = peerId.GetCompressedFabricId();
Expand Down
24 changes: 11 additions & 13 deletions src/lib/dnssd/ServiceNaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <lib/core/CHIPError.h>
#include <lib/core/PeerId.h>
#include <lib/dnssd/Constants.h>
#include <lib/dnssd/Resolver.h>
#include <lib/support/Span.h>

Expand All @@ -27,28 +28,25 @@

namespace chip {
namespace Dnssd {
constexpr size_t kMaxSubtypeDescSize = 19; // _I service subtype = 16 chars for 64-bit id, + 2 for "_I" + nullchar
constexpr char kSubtypeServiceNamePart[] = "_sub";
constexpr char kCommissionableServiceName[] = "_matterc";
constexpr char kOperationalServiceName[] = "_matter";
constexpr char kCommissionerServiceName[] = "_matterd";
constexpr char kOperationalProtocol[] = "_tcp";
constexpr char kCommissionProtocol[] = "_udp";
constexpr char kLocalDomain[] = "local";
constexpr size_t kOperationalServiceNamePrefix = 16 + 1 + 16; // 2 * 64-bit value in HEX + hyphen
constexpr size_t kCommissionServiceNamePrefix = 16;
constexpr char kSubtypeServiceNamePart[] = "_sub";
constexpr char kCommissionableServiceName[] = "_matterc";
constexpr char kOperationalServiceName[] = "_matter";
constexpr char kCommissionerServiceName[] = "_matterd";
constexpr char kOperationalProtocol[] = "_tcp";
constexpr char kCommissionProtocol[] = "_udp";
constexpr char kLocalDomain[] = "local";

// each includes space for a null terminator, which becomes a . when the names are appended.
constexpr size_t kMaxCommisisonableServiceNameSize =
kMaxSubtypeDescSize + sizeof(kSubtypeServiceNamePart) + sizeof(kCommissionableServiceName);
Common::kSubTypeMaxLength + 1 + sizeof(kSubtypeServiceNamePart) + sizeof(kCommissionableServiceName);

// each includes space for a null terminator, which becomes a . when the names are appended.
constexpr size_t kMaxCommisisonerServiceNameSize =
kMaxSubtypeDescSize + sizeof(kSubtypeServiceNamePart) + sizeof(kCommissionerServiceName);
Common::kSubTypeMaxLength + 1 + sizeof(kSubtypeServiceNamePart) + sizeof(kCommissionerServiceName);

// + 1 for nullchar on prefix.
constexpr size_t kMaxOperationalServiceNameSize =
kOperationalServiceNamePrefix + 1 + sizeof(kOperationalServiceName) + sizeof(kOperationalProtocol) + sizeof(kLocalDomain);
Operational::kInstanceNameMaxLength + 1 + sizeof(kOperationalServiceName) + sizeof(kOperationalProtocol) + sizeof(kLocalDomain);

/// builds the MDNS advertising name for a given fabric + nodeid pair
CHIP_ERROR MakeInstanceName(char * buffer, size_t bufferLen, const PeerId & peerId);
Expand Down
11 changes: 5 additions & 6 deletions src/lib/dnssd/platform/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@
#include <inet/InetInterface.h>
#include <lib/core/CHIPError.h>
#include <lib/core/Optional.h>
#include <lib/dnssd/Constants.h>
#include <lib/dnssd/ServiceNaming.h>

namespace chip {
namespace Dnssd {

// None of these sizes include an null character at the end.
static constexpr uint8_t kDnssdInstanceNameMaxSize = 33; // [Node]-[Fabric] ID in hex - 16+1+16
static constexpr uint8_t kDnssdHostNameMaxSize = 16; // 64-bits in hex.
static constexpr size_t kDnssdProtocolTextMaxSize = std::max(sizeof(kOperationalProtocol), sizeof(kCommissionProtocol)) - 1;
static constexpr size_t kDnssdProtocolTextMaxSize = std::max(sizeof(kOperationalProtocol), sizeof(kCommissionProtocol)) - 1;
static constexpr size_t kDnssdTypeMaxSize =
std::max({ sizeof(kCommissionableServiceName), sizeof(kOperationalServiceName), sizeof(kCommissionerServiceName) }) - 1;
static constexpr uint8_t kDnssdTypeAndProtocolMaxSize = kDnssdTypeMaxSize + kDnssdProtocolTextMaxSize + 1; // <type>.<protocol>
static constexpr uint16_t kDnssdTextMaxSize = 64;
static constexpr uint8_t kDnssdFullTypeAndProtocolMaxSize = kMaxSubtypeDescSize + /* '.' */ 1 + kDnssdTypeAndProtocolMaxSize;
static constexpr uint8_t kDnssdFullTypeAndProtocolMaxSize = Common::kSubTypeMaxLength + /* '.' */ 1 + kDnssdTypeAndProtocolMaxSize;

enum class DnssdServiceProtocol : uint8_t
{
Expand All @@ -63,8 +62,8 @@ struct TextEntry

struct DnssdService
{
char mName[kDnssdInstanceNameMaxSize + 1];
char mHostName[kDnssdHostNameMaxSize + 1] = "";
char mName[Common::kInstanceNameMaxLength + 1];
char mHostName[kHostNameMaxLength + 1] = "";
char mType[kDnssdTypeMaxSize + 1];
DnssdServiceProtocol mProtocol;
Inet::IPAddressType mAddressType;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/dnssd/tests/TestTxtFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ void TestGetRotatingDeviceId(nlTestSuite * inSuite, void * inContext)

sprintf(ri, "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F3031");
GetRotatingDeviceId(GetSpan(ri), id, &len);
NL_TEST_ASSERT(inSuite, len == kMaxRotatingIdLen);
for (uint8_t i = 0; i < kMaxRotatingIdLen; ++i)
NL_TEST_ASSERT(inSuite, len == sizeof(id));
for (uint8_t i = 0; i < sizeof(id); ++i)
{
NL_TEST_ASSERT(inSuite, id[i] == i);
}
Expand Down
49 changes: 49 additions & 0 deletions src/lib/support/Fold.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <functional>

namespace chip {

/**
* Apply the functor sequentially to the previous value and a subsequent element of the container.
*
* This provides a similar functionality to std::acumulate, but can be used in constexpr contexts.
*/
template <class Container, class Functor, class ValueType = typename Container::value_type>
constexpr ValueType Fold(const Container & container, ValueType initial, Functor functor)
{
for (const auto & element : container)
{
initial = functor(initial, element);
}

return initial;
}

/**
* Sum all elements of the container using "+" operator.
*/
template <class Container, class ValueType = typename Container::value_type>
constexpr ValueType Sum(const Container & container)
{
return Fold(container, ValueType{}, std::plus<ValueType>{});
}

} // namespace chip
Loading

0 comments on commit 7440755

Please sign in to comment.