[num-limits] add kBitsPerByte, BitSizeOf(), and BytesForBitSize() (#9618)

This commit removes the use of `CHAR_BIT` in the code and instead
defines a constant `kBitsPerByte` for this in `numeric_limits.hpp`.
`CHAR_BIT` is generally defined as `8` but there are some platforms
were this may not be the case. The way we use `CHAR_BIT` is to
determine number of bits in a byte, so introducing our own constant
`kBitsPerByte` helps improve code readability and safety.

This commit also adds `BitSizeOf()` macro which is similar to `sizeof()`
but returns the number of bits of a given type or variable. Macro
`BytesForBitSize()` is also added which returns the number of bytes
needed to represent a given bit size. This replaces the
`BitVectorBytes()` macro previously defined in `encoding.hpp`.
This commit is contained in:
Abtin Keshavarzian
2023-11-19 20:10:40 -08:00
committed by GitHub
parent 7936f8bcf9
commit ce9edaf8b5
32 changed files with 104 additions and 77 deletions
+1 -1
View File
@@ -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
+1
View File
@@ -58,6 +58,7 @@
#define OPENTHREAD_PLATFORM_TOOLCHAIN_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
+4 -3
View File
@@ -90,6 +90,7 @@
#include <openthread/radio_stats.h>
#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<Cmd("channel")>(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<Cmd("discover")>(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<Cmd("scan")>(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;
}
+1 -1
View File
@@ -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];
}
+2 -1
View File
@@ -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 @@ public:
}
private:
uint8_t mMask[BitVectorBytes(N)];
uint8_t mMask[BytesForBitSize(N)];
};
/**
-3
View File
@@ -45,7 +45,6 @@
#endif
#endif
#include <limits.h>
#include <stdint.h>
namespace ot {
@@ -82,8 +81,6 @@ inline uint32_t Reverse32(uint32_t v)
return v;
}
#define BitVectorBytes(x) static_cast<uint8_t>(((x) + (CHAR_BIT - 1)) / CHAR_BIT)
namespace BigEndian {
#if BYTE_ORDER_BIG_ENDIAN
+1 -1
View File
@@ -210,7 +210,7 @@ void Notifier::LogEvents(Events aEvents) const
bool didLog = false;
String<kFlagsStringBufferSize> 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);
+22
View File
@@ -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<uint8_t>(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte)
/**
* Provides a way to query properties of arithmetic types.
*
+2 -3
View File
@@ -36,10 +36,9 @@
#include "openthread-core-config.h"
#include <limits.h>
#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 @@ private:
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
-2
View File
@@ -33,8 +33,6 @@
#include "aes_ccm.hpp"
#include <limits.h>
#include "common/code_utils.hpp"
#include "common/debug.hpp"
#include "common/encoding.hpp"
+1 -1
View File
@@ -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<mbedtls_aes_context *>(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:
+4 -4
View File
@@ -36,10 +36,10 @@
#include "openthread-core-config.h"
#include <limits.h>
#include <openthread/platform/radio.h>
#include "common/equatable.hpp"
#include "common/numeric_limits.hpp"
#include "common/string.hpp"
#include "radio/radio.hpp"
@@ -147,7 +147,7 @@ public:
*/
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 @@ public:
*/
void AddChannel(uint8_t aChannel)
{
if (aChannel < sizeof(mMask) * CHAR_BIT)
if (aChannel < BitSizeOf(mMask))
{
mMask |= (1UL << aChannel);
}
@@ -172,7 +172,7 @@ public:
*/
void RemoveChannel(uint8_t aChannel)
{
if (aChannel < sizeof(mMask) * CHAR_BIT)
if (aChannel < BitSizeOf(mMask))
{
mMask &= ~(1UL << aChannel);
}
+4 -6
View File
@@ -36,12 +36,10 @@
#include "openthread-core-config.h"
#include <limits.h>
#include <stdint.h>
#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 @@ protected:
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;
+5 -5
View File
@@ -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<uint8_t>(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<uint16_t>(mValue));
}
else if (mLength <= sizeof(uint32_t) * CHAR_BIT)
else if (mLength <= BitSizeOf(uint32_t))
{
string.Append("0x%08lx", ToUlong(static_cast<uint32_t>(mValue)));
}
+4 -5
View File
@@ -37,8 +37,6 @@
#include "openthread-core-config.h"
#include <limits.h>
#include <openthread/commissioner.h>
#include <openthread/instance.h>
#include <openthread/joiner.h>
@@ -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 @@ public:
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<uint8_t>(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<uint8_t>(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); }
+2 -1
View File
@@ -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;
+4 -2
View File
@@ -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)
{
+5 -5
View File
@@ -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++)
{
+7 -7
View File
@@ -36,14 +36,14 @@
#include "openthread-core-config.h"
#include <stdint.h>
#include <openthread/ip6.h>
#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<NetworkPrefix>, public Clearable<NetworkPrefix>
{
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<Prefix>, public Unequatable<Prefix>
{
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 @@ public:
* @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.
+2 -1
View File
@@ -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));
+2 -1
View File
@@ -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;
+2 -2
View File
@@ -36,7 +36,6 @@
#include "openthread-core-config.h"
#include <limits.h>
#include <stdint.h>
#include <string.h>
@@ -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 @@ public:
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;
+2 -1
View File
@@ -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<uint8_t>(sizeof(Radio::kSupportedChannelPages) * CHAR_BIT); page++)
for (uint8_t page = 0; page < static_cast<uint8_t>(BitSizeOf(Radio::kSupportedChannelPages)); page++)
{
if (Radio::kSupportedChannelPages & (1 << page))
{
+2 -1
View File
@@ -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);
+1 -2
View File
@@ -46,7 +46,6 @@
#include "spinel.h"
#include <errno.h>
#include <limits.h>
#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;
+2
View File
@@ -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)
+3 -3
View File
@@ -27,13 +27,13 @@
#include "changed_props_set.hpp"
#include <limits.h>
#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);
+2
View File
@@ -151,6 +151,8 @@ public:
bool ShouldDeferHostSend(void);
protected:
static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte.
typedef otError (NcpBase::*PropertyHandler)(void);
/**
+1 -1
View File
@@ -2432,7 +2432,7 @@ template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_CHANNEL_MONITOR_CHANN
{
otError error = OT_ERROR_NONE;
uint32_t channelMask = otLinkGetSupportedChannelMask(mInstance);
uint8_t channelNum = sizeof(channelMask) * CHAR_BIT;
uint8_t channelNum = sizeof(channelMask) * kBitsPerByte;
for (uint8_t channel = 0; channel < channelNum; channel++)
{
+5 -3
View File
@@ -295,16 +295,18 @@ static bool sIsSyncingState = false;
static const uint8_t allOnes[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
#define BITS_PER_BYTE 8
#define MAX_PREFIX_LENGTH (OT_IP6_ADDRESS_SIZE * BITS_PER_BYTE)
static void InitNetaskWithPrefixLength(struct in6_addr *address, uint8_t prefixLen)
{
#define MAX_PREFIX_LENGTH (OT_IP6_ADDRESS_SIZE * CHAR_BIT)
ot::Ip6::Address addr;
if (prefixLen > 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));
+2 -1
View File
@@ -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);
+8 -10
View File
@@ -26,8 +26,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <limits.h>
#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);