-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DNS-SD] Clean up common constants (#10553)
* [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
Showing
25 changed files
with
307 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.