diff --git a/include/openthread/instance.h b/include/openthread/instance.h index fdcdf6b55e7..e87e43bccf8 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (373) +#define OPENTHREAD_API_VERSION (374) /** * @addtogroup api-instance diff --git a/include/openthread/platform/toolchain.h b/include/openthread/platform/toolchain.h index 383aefbb327..a35e3ecdec5 100644 --- a/include/openthread/platform/toolchain.h +++ b/include/openthread/platform/toolchain.h @@ -58,6 +58,7 @@ #define OPENTHREAD_PLATFORM_TOOLCHAIN_H_ #include +#include #ifdef __cplusplus extern "C" { diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index e4e62620165..c25410b4fd9 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -90,6 +90,7 @@ #include #endif #include "common/new.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "mac/channel_mask.hpp" @@ -1291,7 +1292,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) if (otChannelMonitorIsEnabled(GetInstancePtr())) { uint32_t channelMask = otLinkGetSupportedChannelMask(GetInstancePtr()); - uint8_t channelNum = sizeof(channelMask) * CHAR_BIT; + uint8_t channelNum = BitSizeOf(channelMask); OutputLine("interval: %lu", ToUlong(otChannelMonitorGetSampleInterval(GetInstancePtr()))); OutputLine("threshold: %d", otChannelMonitorGetRssiThreshold(GetInstancePtr())); @@ -2611,7 +2612,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) uint8_t channel; SuccessOrExit(error = aArgs[0].ParseAsUint8(channel)); - VerifyOrExit(channel < sizeof(scanChannels) * CHAR_BIT, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(channel < BitSizeOf(scanChannels), error = OT_ERROR_INVALID_ARGS); scanChannels = 1 << channel; } @@ -7113,7 +7114,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) uint8_t channel; SuccessOrExit(error = aArgs->ParseAsUint8(channel)); - VerifyOrExit(channel < sizeof(scanChannels) * CHAR_BIT, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(channel < BitSizeOf(scanChannels), error = OT_ERROR_INVALID_ARGS); scanChannels = 1 << channel; } diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index 61c2ae0ab81..18d2ad30f44 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -551,7 +551,7 @@ Error Option::Iterator::ReadOptionValue(uint64_t &aUintValue) const for (uint16_t pos = 0; pos < mOption.mLength; pos++) { - aUintValue <<= CHAR_BIT; + aUintValue <<= kBitsPerByte; aUintValue |= buffer[pos]; } diff --git a/src/core/common/bit_vector.hpp b/src/core/common/bit_vector.hpp index 8c6962c133f..9b24043d028 100644 --- a/src/core/common/bit_vector.hpp +++ b/src/core/common/bit_vector.hpp @@ -40,6 +40,7 @@ #include "common/debug.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" +#include "common/numeric_limits.hpp" namespace ot { @@ -122,7 +123,7 @@ template class BitVector : public Equatable>, public C } private: - uint8_t mMask[BitVectorBytes(N)]; + uint8_t mMask[BytesForBitSize(N)]; }; /** diff --git a/src/core/common/encoding.hpp b/src/core/common/encoding.hpp index 3427a7c8951..f8ee910ca4d 100644 --- a/src/core/common/encoding.hpp +++ b/src/core/common/encoding.hpp @@ -45,7 +45,6 @@ #endif #endif -#include #include namespace ot { @@ -82,8 +81,6 @@ inline uint32_t Reverse32(uint32_t v) return v; } -#define BitVectorBytes(x) static_cast(((x) + (CHAR_BIT - 1)) / CHAR_BIT) - namespace BigEndian { #if BYTE_ORDER_BIG_ENDIAN diff --git a/src/core/common/notifier.cpp b/src/core/common/notifier.cpp index f25e71eb515..f1f9863a6e9 100644 --- a/src/core/common/notifier.cpp +++ b/src/core/common/notifier.cpp @@ -210,7 +210,7 @@ void Notifier::LogEvents(Events aEvents) const bool didLog = false; String string; - for (uint8_t bit = 0; bit < sizeof(Events::Flags) * CHAR_BIT; bit++) + for (uint8_t bit = 0; bit < BitSizeOf(Events::Flags); bit++) { VerifyOrExit(flags != 0); diff --git a/src/core/common/numeric_limits.hpp b/src/core/common/numeric_limits.hpp index 35e984394cb..911df238097 100644 --- a/src/core/common/numeric_limits.hpp +++ b/src/core/common/numeric_limits.hpp @@ -38,6 +38,28 @@ namespace ot { +static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte. + +/** + * Returns the bit-size (number of bits) of a given type or variable. + * + * @param[in] aItem The item (type or variable or expression) to get the bit-size of. + * + * @returns Number of bits of @p aItem. + * + */ +#define BitSizeOf(aItem) (sizeof(aItem) * kBitsPerByte) + +/** + * Determines number of byes to represent a given number of bits. + * + * @param[in] aBitSize The bit-size (number of bits). + * + * @returns Number of bytes to represent @p aBitSize. + * + */ +#define BytesForBitSize(aBitSize) static_cast(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte) + /** * Provides a way to query properties of arithmetic types. * diff --git a/src/core/common/time_ticker.hpp b/src/core/common/time_ticker.hpp index 4f7bc587701..e8b37d07baa 100644 --- a/src/core/common/time_ticker.hpp +++ b/src/core/common/time_ticker.hpp @@ -36,10 +36,9 @@ #include "openthread-core-config.h" -#include - #include "common/locator.hpp" #include "common/non_copyable.hpp" +#include "common/numeric_limits.hpp" #include "common/time.hpp" #include "common/timer.hpp" @@ -124,7 +123,7 @@ class TimeTicker : public InstanceLocator, private NonCopyable uint32_t mReceivers; TickerTimer mTimer; - static_assert(kNumReceivers < sizeof(mReceivers) * CHAR_BIT, "Too many `Receiver`s - does not fit in a bit mask"); + static_assert(kNumReceivers < BitSizeOf(mReceivers), "Too many `Receiver`s - does not fit in a bit mask"); }; } // namespace ot diff --git a/src/core/crypto/aes_ccm.cpp b/src/core/crypto/aes_ccm.cpp index bb9bec12510..8ca66aa4e19 100644 --- a/src/core/crypto/aes_ccm.cpp +++ b/src/core/crypto/aes_ccm.cpp @@ -33,8 +33,6 @@ #include "aes_ccm.hpp" -#include - #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/encoding.hpp" diff --git a/src/core/crypto/crypto_platform.cpp b/src/core/crypto/crypto_platform.cpp index 2d71d0eebe4..6c7e54cee6f 100644 --- a/src/core/crypto/crypto_platform.cpp +++ b/src/core/crypto/crypto_platform.cpp @@ -110,7 +110,7 @@ OT_TOOL_WEAK otError otPlatCryptoAesSetKey(otCryptoContext *aContext, const otCr VerifyOrExit(aContext->mContextSize >= sizeof(mbedtls_aes_context), error = kErrorFailed); context = static_cast(aContext->mContext); - VerifyOrExit((mbedtls_aes_setkey_enc(context, key.GetBytes(), (key.GetLength() * CHAR_BIT)) == 0), + VerifyOrExit((mbedtls_aes_setkey_enc(context, key.GetBytes(), (key.GetLength() * kBitsPerByte)) == 0), error = kErrorFailed); exit: diff --git a/src/core/mac/channel_mask.hpp b/src/core/mac/channel_mask.hpp index 36ad78d458a..7549760f785 100644 --- a/src/core/mac/channel_mask.hpp +++ b/src/core/mac/channel_mask.hpp @@ -36,10 +36,10 @@ #include "openthread-core-config.h" -#include #include #include "common/equatable.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "radio/radio.hpp" @@ -147,7 +147,7 @@ class ChannelMask : public Unequatable */ bool ContainsChannel(uint8_t aChannel) const { - return (aChannel < sizeof(mMask) * CHAR_BIT) ? ((1UL << aChannel) & mMask) != 0 : false; + return (aChannel < BitSizeOf(mMask)) ? ((1UL << aChannel) & mMask) != 0 : false; } /** @@ -158,7 +158,7 @@ class ChannelMask : public Unequatable */ void AddChannel(uint8_t aChannel) { - if (aChannel < sizeof(mMask) * CHAR_BIT) + if (aChannel < BitSizeOf(mMask)) { mMask |= (1UL << aChannel); } @@ -172,7 +172,7 @@ class ChannelMask : public Unequatable */ void RemoveChannel(uint8_t aChannel) { - if (aChannel < sizeof(mMask) * CHAR_BIT) + if (aChannel < BitSizeOf(mMask)) { mMask &= ~(1UL << aChannel); } diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 267e2f73557..d1ce98a485f 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -36,12 +36,10 @@ #include "openthread-core-config.h" -#include -#include - #include "common/as_core_type.hpp" #include "common/const_cast.hpp" #include "common/encoding.hpp" +#include "common/numeric_limits.hpp" #include "mac/mac_types.hpp" #include "meshcop/network_name.hpp" @@ -1115,9 +1113,9 @@ class Frame : public otRadioFrame static constexpr uint8_t kKeyIdModeMask = 3 << 3; static constexpr uint8_t kMic0Size = 0; - static constexpr uint8_t kMic32Size = 32 / CHAR_BIT; - static constexpr uint8_t kMic64Size = 64 / CHAR_BIT; - static constexpr uint8_t kMic128Size = 128 / CHAR_BIT; + static constexpr uint8_t kMic32Size = 32 / kBitsPerByte; + static constexpr uint8_t kMic64Size = 64 / kBitsPerByte; + static constexpr uint8_t kMic128Size = 128 / kBitsPerByte; static constexpr uint8_t kMaxMicSize = kMic128Size; static constexpr uint8_t kKeySourceSizeMode0 = 0; diff --git a/src/core/meshcop/meshcop.cpp b/src/core/meshcop/meshcop.cpp index da000d5639c..82f7a139f60 100644 --- a/src/core/meshcop/meshcop.cpp +++ b/src/core/meshcop/meshcop.cpp @@ -136,12 +136,12 @@ void JoinerDiscerner::CopyTo(Mac::ExtAddress &aExtAddress) const OT_ASSERT(IsValid()); // Write full bytes - while (remaining >= CHAR_BIT) + while (remaining >= kBitsPerByte) { *cur = static_cast(value & 0xff); - value >>= CHAR_BIT; + value >>= kBitsPerByte; cur--; - remaining -= CHAR_BIT; + remaining -= kBitsPerByte; } // Write any remaining bits (not a full byte) @@ -168,11 +168,11 @@ JoinerDiscerner::InfoString JoinerDiscerner::ToString(void) const { InfoString string; - if (mLength <= sizeof(uint16_t) * CHAR_BIT) + if (mLength <= BitSizeOf(uint16_t)) { string.Append("0x%04x", static_cast(mValue)); } - else if (mLength <= sizeof(uint32_t) * CHAR_BIT) + else if (mLength <= BitSizeOf(uint32_t)) { string.Append("0x%08lx", ToUlong(static_cast(mValue))); } diff --git a/src/core/meshcop/meshcop.hpp b/src/core/meshcop/meshcop.hpp index 47a8c387e28..339872bb38c 100644 --- a/src/core/meshcop/meshcop.hpp +++ b/src/core/meshcop/meshcop.hpp @@ -37,8 +37,6 @@ #include "openthread-core-config.h" -#include - #include #include #include @@ -49,6 +47,7 @@ #include "common/equatable.hpp" #include "common/log.hpp" #include "common/message.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "mac/mac_types.hpp" #include "meshcop/meshcop_tlvs.hpp" @@ -393,10 +392,10 @@ class SteeringData : public otSteeringData private: static constexpr uint8_t kPermitAll = 0xff; - uint8_t GetNumBits(void) const { return (mLength * CHAR_BIT); } + uint8_t GetNumBits(void) const { return (mLength * kBitsPerByte); } - uint8_t BitIndex(uint8_t aBit) const { return (mLength - 1 - (aBit / CHAR_BIT)); } - uint8_t BitFlag(uint8_t aBit) const { return static_cast(1U << (aBit % CHAR_BIT)); } + uint8_t BitIndex(uint8_t aBit) const { return (mLength - 1 - (aBit / kBitsPerByte)); } + uint8_t BitFlag(uint8_t aBit) const { return static_cast(1U << (aBit % kBitsPerByte)); } bool GetBit(uint8_t aBit) const { return (m8[BitIndex(aBit)] & BitFlag(aBit)) != 0; } void SetBit(uint8_t aBit) { m8[BitIndex(aBit)] |= BitFlag(aBit); } diff --git a/src/core/meshcop/meshcop_tlvs.cpp b/src/core/meshcop/meshcop_tlvs.cpp index 4bf5abc08dc..4ad9a4c6eca 100644 --- a/src/core/meshcop/meshcop_tlvs.cpp +++ b/src/core/meshcop/meshcop_tlvs.cpp @@ -36,6 +36,7 @@ #include "common/const_cast.hpp" #include "common/debug.hpp" #include "common/num_utils.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "meshcop/meshcop.hpp" @@ -146,7 +147,7 @@ bool ChannelTlv::IsValid(void) const bool ret = false; VerifyOrExit(GetLength() == sizeof(*this) - sizeof(Tlv)); - VerifyOrExit(mChannelPage < sizeof(uint32_t) * CHAR_BIT); + VerifyOrExit(mChannelPage < BitSizeOf(uint32_t)); VerifyOrExit((1U << mChannelPage) & Radio::kSupportedChannelPages); VerifyOrExit(Radio::kChannelMin <= GetChannel() && GetChannel() <= Radio::kChannelMax); ret = true; diff --git a/src/core/net/ip4_types.cpp b/src/core/net/ip4_types.cpp index bbe80f1982b..a6bc1c2fb83 100644 --- a/src/core/net/ip4_types.cpp +++ b/src/core/net/ip4_types.cpp @@ -32,7 +32,9 @@ */ #include "ip4_types.hpp" -#include "ip6_address.hpp" + +#include "common/numeric_limits.hpp" +#include "net/ip6_address.hpp" namespace ot { namespace Ip4 { @@ -92,7 +94,7 @@ void Address::ExtractFromIp6Address(uint8_t aPrefixLength, const Ip6::Address &a OT_ASSERT(Ip6::Prefix::IsValidNat64PrefixLength(aPrefixLength)); - ip6Index = aPrefixLength / CHAR_BIT; + ip6Index = aPrefixLength / kBitsPerByte; for (uint8_t &i : mFields.m8) { diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index 3686c19552e..68db559d368 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -132,7 +132,7 @@ bool Prefix::operator<(const Prefix &aOther) const ExitNow(); } - isSmaller = GetBytes()[matchedLength / CHAR_BIT] < aOther.GetBytes()[matchedLength / CHAR_BIT]; + isSmaller = GetBytes()[matchedLength / kBitsPerByte] < aOther.GetBytes()[matchedLength / kBitsPerByte]; exit: return isSmaller; @@ -150,7 +150,7 @@ uint8_t Prefix::MatchLength(const uint8_t *aPrefixA, const uint8_t *aPrefixB, ui if (diff == 0) { - matchedLength += CHAR_BIT; + matchedLength += kBitsPerByte; } else { @@ -405,8 +405,8 @@ void Address::CopyBits(uint8_t *aDst, const uint8_t *aSrc, uint8_t aNumBits) // the case where `aNumBits` may not be a multiple of 8. It leaves the // remaining bits beyond `aNumBits` in `aDst` unchanged. - uint8_t numBytes = aNumBits / CHAR_BIT; - uint8_t extraBits = aNumBits % CHAR_BIT; + uint8_t numBytes = aNumBits / kBitsPerByte; + uint8_t extraBits = aNumBits % kBitsPerByte; memcpy(aDst, aSrc, numBytes); @@ -520,7 +520,7 @@ void Address::SynthesizeFromIp4Address(const Prefix &aPrefix, const Ip4::Address Clear(); SetPrefix(aPrefix); - ip6Index = aPrefix.GetLength() / CHAR_BIT; + ip6Index = aPrefix.GetLength() / kBitsPerByte; for (uint8_t i = 0; i < Ip4::Address::kSize; i++) { diff --git a/src/core/net/ip6_address.hpp b/src/core/net/ip6_address.hpp index 3e91551a5b1..f836f272174 100644 --- a/src/core/net/ip6_address.hpp +++ b/src/core/net/ip6_address.hpp @@ -36,14 +36,14 @@ #include "openthread-core-config.h" -#include - #include #include "common/as_core_type.hpp" #include "common/clearable.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" +#include "common/num_utils.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "mac/mac_types.hpp" @@ -73,8 +73,8 @@ OT_TOOL_PACKED_BEGIN class NetworkPrefix : public otIp6NetworkPrefix, public Equatable, public Clearable { public: - static constexpr uint8_t kSize = OT_IP6_PREFIX_SIZE; ///< Size in bytes. - static constexpr uint8_t kLength = OT_IP6_PREFIX_SIZE * CHAR_BIT; ///< Length of Network Prefix in bits. + static constexpr uint8_t kSize = OT_IP6_PREFIX_SIZE; ///< Size in bytes. + static constexpr uint8_t kLength = kSize * kBitsPerByte; ///< Length of Network Prefix in bits. /** * Generates and sets the Network Prefix to a crypto-secure random Unique Local Address (ULA) based @@ -96,8 +96,8 @@ OT_TOOL_PACKED_BEGIN class Prefix : public otIp6Prefix, public Clearable, public Unequatable { public: - static constexpr uint8_t kMaxLength = OT_IP6_ADDRESS_SIZE * CHAR_BIT; ///< Max length of a prefix in bits. - static constexpr uint8_t kMaxSize = OT_IP6_ADDRESS_SIZE; ///< Max (byte) size of a prefix. + static constexpr uint8_t kMaxSize = OT_IP6_ADDRESS_SIZE; ///< Max (byte) size of a prefix. + static constexpr uint8_t kMaxLength = kMaxSize * kBitsPerByte; ///< Max length of a prefix in bits. static constexpr uint16_t kInfoStringSize = OT_IP6_PREFIX_STRING_SIZE; ///< Info string size (`ToString()`). @@ -283,7 +283,7 @@ class Prefix : public otIp6Prefix, public Clearable, public Unequatable< * @returns The size (in bytes) of the prefix. * */ - static uint8_t SizeForLength(uint8_t aLength) { return BitVectorBytes(aLength); } + static uint8_t SizeForLength(uint8_t aLength) { return BytesForBitSize(aLength); } /** * Returns the number of IPv6 prefix bits that match. diff --git a/src/core/thread/lowpan.cpp b/src/core/thread/lowpan.cpp index c00506c3178..cb84634552f 100644 --- a/src/core/thread/lowpan.cpp +++ b/src/core/thread/lowpan.cpp @@ -37,6 +37,7 @@ #include "common/debug.hpp" #include "common/encoding.hpp" #include "common/locator_getters.hpp" +#include "common/numeric_limits.hpp" #include "instance/instance.hpp" #include "net/ip6.hpp" #include "net/udp6.hpp" @@ -858,7 +859,7 @@ Error Lowpan::DecompressExtensionHeader(Message &aMessage, FrameData &aFrameData } // length - hdr[1] = BitVectorBytes(sizeof(hdr) + len) - 1; + hdr[1] = BytesForBitSize(sizeof(hdr) + len) - 1; SuccessOrExit(aMessage.AppendBytes(hdr, sizeof(hdr))); aMessage.MoveOffset(sizeof(hdr)); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index f82042ea7ab..ca957855de0 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -43,6 +43,7 @@ #include "common/encoding.hpp" #include "common/locator_getters.hpp" #include "common/num_utils.hpp" +#include "common/numeric_limits.hpp" #include "common/random.hpp" #include "common/serial_number.hpp" #include "common/settings.hpp" @@ -654,7 +655,7 @@ uint32_t Mle::GetAttachStartDelay(void) const uint16_t counter = mAttachCounter - 1; const uint32_t ratio = kAttachBackoffMaxInterval / kAttachBackoffMinInterval; - if ((counter < sizeof(ratio) * CHAR_BIT) && ((1UL << counter) <= ratio)) + if ((counter < BitSizeOf(ratio)) && ((1UL << counter) <= ratio)) { delay = kAttachBackoffMinInterval; delay <<= counter; diff --git a/src/core/thread/mle_types.hpp b/src/core/thread/mle_types.hpp index ab2a7b02d25..ffc6a428722 100644 --- a/src/core/thread/mle_types.hpp +++ b/src/core/thread/mle_types.hpp @@ -36,7 +36,6 @@ #include "openthread-core-config.h" -#include #include #include @@ -51,6 +50,7 @@ #include "common/code_utils.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" +#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "mac/mac_types.hpp" #include "meshcop/extended_panid.hpp" @@ -481,7 +481,7 @@ class RouterIdSet : public Equatable private: static uint8_t MaskFor(uint8_t aRouterId) { return (0x80 >> (aRouterId % 8)); } - uint8_t mRouterIdSet[BitVectorBytes(Mle::kMaxRouterId + 1)]; + uint8_t mRouterIdSet[BytesForBitSize(Mle::kMaxRouterId + 1)]; } OT_TOOL_PACKED_END; class TxChallenge; diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 235f03c6fb2..634574bc2a9 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -41,6 +41,7 @@ #include "common/encoding.hpp" #include "common/locator_getters.hpp" #include "common/log.hpp" +#include "common/numeric_limits.hpp" #include "common/random.hpp" #include "instance/instance.hpp" #include "mac/mac.hpp" @@ -321,7 +322,7 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage) tlv.Init(); - for (uint8_t page = 0; page < static_cast(sizeof(Radio::kSupportedChannelPages) * CHAR_BIT); page++) + for (uint8_t page = 0; page < static_cast(BitSizeOf(Radio::kSupportedChannelPages)); page++) { if (Radio::kSupportedChannelPages & (1 << page)) { diff --git a/src/core/utils/slaac_address.cpp b/src/core/utils/slaac_address.cpp index 76a53ba26cc..7420bdf1a62 100644 --- a/src/core/utils/slaac_address.cpp +++ b/src/core/utils/slaac_address.cpp @@ -39,6 +39,7 @@ #include "common/code_utils.hpp" #include "common/locator_getters.hpp" #include "common/log.hpp" +#include "common/numeric_limits.hpp" #include "common/random.hpp" #include "common/settings.hpp" #include "crypto/sha256.hpp" @@ -303,7 +304,7 @@ Error Slaac::GenerateIid(Ip6::Netif::UnicastAddress &aAddress, uint8_t &aDadCoun for (uint16_t count = 0; count < kMaxIidCreationAttempts; count++, aDadCounter++) { sha256.Start(); - sha256.Update(aAddress.mAddress.mFields.m8, BitVectorBytes(aAddress.mPrefixLength)); + sha256.Update(aAddress.mAddress.mFields.m8, BytesForBitSize(aAddress.mPrefixLength)); sha256.Update(netIface); sha256.Update(aDadCounter); diff --git a/src/lib/spinel/spinel.c b/src/lib/spinel/spinel.c index ab82faceddc..963f2762636 100644 --- a/src/lib/spinel/spinel.c +++ b/src/lib/spinel/spinel.c @@ -46,7 +46,6 @@ #include "spinel.h" #include -#include #ifndef SPINEL_PLATFORM_HEADER /* These are all already included in the spinel platform header @@ -241,7 +240,7 @@ spinel_ssize_t spinel_packed_uint_decode(const uint8_t *bytes, spinel_size_t len do { - if ((len < sizeof(uint8_t)) || (i >= sizeof(unsigned int) * CHAR_BIT)) + if ((len < sizeof(uint8_t)) || (i >= sizeof(unsigned int) * SPINEL_BITS_PER_BYTE)) { ret = -1; break; diff --git a/src/lib/spinel/spinel.h b/src/lib/spinel/spinel.h index 3d6ceda660d..a4802af9b41 100644 --- a/src/lib/spinel/spinel.h +++ b/src/lib/spinel/spinel.h @@ -479,6 +479,8 @@ /// Macro for generating bit masks using bit index from the spec #define SPINEL_BIT_MASK(bit_index, field_bit_count) ((1 << ((field_bit_count)-1)) >> (bit_index)) +#define SPINEL_BITS_PER_BYTE 8 // Number of bits in a byte + // ---------------------------------------------------------------------------- #if defined(__cplusplus) diff --git a/src/ncp/changed_props_set.cpp b/src/ncp/changed_props_set.cpp index ceac025d994..9e847f05ce9 100644 --- a/src/ncp/changed_props_set.cpp +++ b/src/ncp/changed_props_set.cpp @@ -27,13 +27,13 @@ #include "changed_props_set.hpp" -#include - #include "common/code_utils.hpp" namespace ot { namespace Ncp { +static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte. + // ---------------------------------------------------------------------------- // MARK: ChangedPropsSet class // ---------------------------------------------------------------------------- @@ -95,7 +95,7 @@ const ChangedPropsSet::Entry ChangedPropsSet::mSupportedProps[] = { uint8_t ChangedPropsSet::GetNumEntries(void) const { - static_assert(OT_ARRAY_LENGTH(mSupportedProps) <= sizeof(mChangedSet) * CHAR_BIT, + static_assert(OT_ARRAY_LENGTH(mSupportedProps) <= sizeof(mChangedSet) * kBitsPerByte, "Changed set size is smaller than number of entries in `mSupportedProps[]` array"); return OT_ARRAY_LENGTH(mSupportedProps); diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index fa4c856ddbb..b618101c358 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -151,6 +151,8 @@ class NcpBase bool ShouldDeferHostSend(void); protected: + static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte. + typedef otError (NcpBase::*PropertyHandler)(void); /** diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index 7d699c7b435..f5b06dd55ce 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -2432,7 +2432,7 @@ template <> otError NcpBase::HandlePropertyGet MAX_PREFIX_LENGTH) { prefixLen = MAX_PREFIX_LENGTH; } - ot::Ip6::Address addr; - addr.Clear(); addr.SetPrefix(allOnes, prefixLen); memcpy(address, addr.mFields.m8, sizeof(addr.mFields.m8)); diff --git a/tests/unit/test_checksum.cpp b/tests/unit/test_checksum.cpp index c37f5ba62c8..0949f5b9124 100644 --- a/tests/unit/test_checksum.cpp +++ b/tests/unit/test_checksum.cpp @@ -28,6 +28,7 @@ #include "common/encoding.hpp" #include "common/message.hpp" +#include "common/numeric_limits.hpp" #include "common/random.hpp" #include "instance/instance.hpp" #include "net/checksum.hpp" @@ -160,7 +161,7 @@ void CorruptMessage(Message &aMessage) SuccessOrQuit(aMessage.Read(byteOffset, byte)); - bitOffset = Random::NonCrypto::GetUint8InRange(0, CHAR_BIT); + bitOffset = Random::NonCrypto::GetUint8InRange(0, kBitsPerByte); byte ^= (1 << bitOffset); diff --git a/tests/unit/test_ip_address.cpp b/tests/unit/test_ip_address.cpp index 9fac84e0291..31c52217e69 100644 --- a/tests/unit/test_ip_address.cpp +++ b/tests/unit/test_ip_address.cpp @@ -26,8 +26,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include - #include "common/array.hpp" #include "common/encoding.hpp" #include "common/string.hpp" @@ -328,8 +326,8 @@ bool CheckPrefix(const Ip6::Address &aAddress, const uint8_t *aPrefix, uint8_t a for (uint8_t bit = 0; bit < aPrefixLength; bit++) { - uint8_t index = bit / CHAR_BIT; - uint8_t mask = (0x80 >> (bit % CHAR_BIT)); + uint8_t index = bit / kBitsPerByte; + uint8_t mask = (0x80 >> (bit % kBitsPerByte)); if ((aAddress.mFields.m8[index] & mask) != (aPrefix[index] & mask)) { @@ -349,8 +347,8 @@ bool CheckPrefixInIid(const Ip6::InterfaceIdentifier &aIid, const uint8_t *aPref for (uint8_t bit = 64; bit < aPrefixLength; bit++) { - uint8_t index = bit / CHAR_BIT; - uint8_t mask = (0x80 >> (bit % CHAR_BIT)); + uint8_t index = bit / kBitsPerByte; + uint8_t mask = (0x80 >> (bit % kBitsPerByte)); if ((aIid.mFields.m8[index - 8] & mask) != (aPrefix[index] & mask)) { @@ -368,10 +366,10 @@ bool CheckInterfaceId(const Ip6::Address &aAddress1, const Ip6::Address &aAddres bool matches = true; - for (size_t bit = aPrefixLength; bit < sizeof(Ip6::Address) * CHAR_BIT; bit++) + for (size_t bit = aPrefixLength; bit < sizeof(Ip6::Address) * kBitsPerByte; bit++) { - uint8_t index = bit / CHAR_BIT; - uint8_t mask = (0x80 >> (bit % CHAR_BIT)); + uint8_t index = bit / kBitsPerByte; + uint8_t mask = (0x80 >> (bit % kBitsPerByte)); if ((aAddress1.mFields.m8[index] & mask) != (aAddress2.mFields.m8[index] & mask)) { @@ -405,7 +403,7 @@ void TestIp6AddressSetPrefix(void) memcpy(address.mFields.m8, prefix, sizeof(address)); printf("Prefix is %s\n", address.ToString().AsCString()); - for (size_t prefixLength = 0; prefixLength <= sizeof(Ip6::Address) * CHAR_BIT; prefixLength++) + for (size_t prefixLength = 0; prefixLength <= sizeof(Ip6::Address) * kBitsPerByte; prefixLength++) { ip6Prefix.Clear(); ip6Prefix.Set(prefix, prefixLength);