Skip to content

Commit

Permalink
[encoding] avoid the use of using for Big/LittleEndian functions (o…
Browse files Browse the repository at this point in the history
…penthread#9621)

This commit refactors the code to eliminate the use of `using` for
`BigEndian` or `LittleEndian` functions like `HostSwap` and `ReadUint`.
As these functions are frequently used in header files, using the
direct namespace enhances code safety by mitigating potential conflicts
arising from the order of included headers in the absence of explicit
`using` declarations. Generally, avoiding the `using` keyword in headers
is considered a recommended practice.

Additionally, this commit removes the `Encoding` namespace to shorten
the full function names.
  • Loading branch information
abtink authored Nov 20, 2023
1 parent ce9edaf commit 6e7ed5c
Show file tree
Hide file tree
Showing 62 changed files with 559 additions and 607 deletions.
6 changes: 3 additions & 3 deletions src/core/coap/coap_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ uint8_t Message::WriteExtendedOptionField(uint16_t aValue, uint8_t *&aBuffer)
else
{
rval = kOption2ByteExtension;
Encoding::BigEndian::WriteUint16(aValue - kOption2ByteExtensionOffset, aBuffer);
BigEndian::WriteUint16(aValue - kOption2ByteExtensionOffset, aBuffer);
aBuffer += sizeof(uint16_t);
}

Expand Down Expand Up @@ -183,7 +183,7 @@ Error Message::AppendUintOption(uint16_t aNumber, uint32_t aValue)
const uint8_t *value = &buffer[0];
uint16_t length = sizeof(uint32_t);

Encoding::BigEndian::WriteUint32(aValue, buffer);
BigEndian::WriteUint32(aValue, buffer);

while ((length > 0) && (value[0] == 0))
{
Expand Down Expand Up @@ -592,7 +592,7 @@ Error Option::Iterator::ReadExtendedOptionField(uint16_t &aValue)
uint16_t value16;

SuccessOrExit(error = Read(sizeof(uint16_t), &value16));
value16 = Encoding::BigEndian::HostSwap16(value16);
value16 = BigEndian::HostSwap16(value16);
aValue = value16 + Message::kOption2ByteExtensionOffset;
}
else
Expand Down
6 changes: 2 additions & 4 deletions src/core/coap/coap_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace ot {
*/
namespace Coap {

using ot::Encoding::BigEndian::HostSwap16;

/**
* @addtogroup core-coap
*
Expand Down Expand Up @@ -315,15 +313,15 @@ class Message : public ot::Message
* @returns The Message ID value.
*
*/
uint16_t GetMessageId(void) const { return HostSwap16(GetHelpData().mHeader.mMessageId); }
uint16_t GetMessageId(void) const { return BigEndian::HostSwap16(GetHelpData().mHeader.mMessageId); }

/**
* Sets the Message ID value.
*
* @param[in] aMessageId The Message ID value.
*
*/
void SetMessageId(uint16_t aMessageId) { GetHelpData().mHeader.mMessageId = HostSwap16(aMessageId); }
void SetMessageId(uint16_t aMessageId) { GetHelpData().mHeader.mMessageId = BigEndian::HostSwap16(aMessageId); }

/**
* Returns the Token length.
Expand Down
3 changes: 1 addition & 2 deletions src/core/common/encoding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include <stdint.h>

namespace ot {
namespace Encoding {

inline uint16_t Swap16(uint16_t v) { return (((v & 0x00ffU) << 8) & 0xff00) | (((v & 0xff00U) >> 8) & 0x00ff); }

Expand Down Expand Up @@ -380,7 +379,7 @@ inline void WriteUint64(uint64_t aValue, uint8_t *aBuffer)
}

} // namespace LittleEndian
} // namespace Encoding

} // namespace ot

#endif // ENCODING_HPP_
14 changes: 4 additions & 10 deletions src/core/common/frame_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,18 @@ void FrameBuilder::Init(void *aBuffer, uint16_t aLength)

Error FrameBuilder::AppendUint8(uint8_t aUint8) { return Append<uint8_t>(aUint8); }

Error FrameBuilder::AppendBigEndianUint16(uint16_t aUint16)
{
return Append<uint16_t>(Encoding::BigEndian::HostSwap16(aUint16));
}
Error FrameBuilder::AppendBigEndianUint16(uint16_t aUint16) { return Append<uint16_t>(BigEndian::HostSwap16(aUint16)); }

Error FrameBuilder::AppendBigEndianUint32(uint32_t aUint32)
{
return Append<uint32_t>(Encoding::BigEndian::HostSwap32(aUint32));
}
Error FrameBuilder::AppendBigEndianUint32(uint32_t aUint32) { return Append<uint32_t>(BigEndian::HostSwap32(aUint32)); }

Error FrameBuilder::AppendLittleEndianUint16(uint16_t aUint16)
{
return Append<uint16_t>(Encoding::LittleEndian::HostSwap16(aUint16));
return Append<uint16_t>(LittleEndian::HostSwap16(aUint16));
}

Error FrameBuilder::AppendLittleEndianUint32(uint32_t aUint32)
{
return Append<uint32_t>(Encoding::LittleEndian::HostSwap32(aUint32));
return Append<uint32_t>(LittleEndian::HostSwap32(aUint32));
}

Error FrameBuilder::AppendBytes(const void *aBuffer, uint16_t aLength)
Expand Down
8 changes: 4 additions & 4 deletions src/core/common/frame_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Error FrameData::ReadBigEndianUint16(uint16_t &aUint16)
Error error;

SuccessOrExit(error = ReadBytes(&aUint16, sizeof(uint16_t)));
aUint16 = Encoding::BigEndian::HostSwap16(aUint16);
aUint16 = BigEndian::HostSwap16(aUint16);

exit:
return error;
Expand All @@ -56,7 +56,7 @@ Error FrameData::ReadBigEndianUint32(uint32_t &aUint32)
Error error;

SuccessOrExit(error = ReadBytes(&aUint32, sizeof(uint32_t)));
aUint32 = Encoding::BigEndian::HostSwap32(aUint32);
aUint32 = BigEndian::HostSwap32(aUint32);

exit:
return error;
Expand All @@ -67,7 +67,7 @@ Error FrameData::ReadLittleEndianUint16(uint16_t &aUint16)
Error error;

SuccessOrExit(error = ReadBytes(&aUint16, sizeof(uint16_t)));
aUint16 = Encoding::LittleEndian::HostSwap16(aUint16);
aUint16 = LittleEndian::HostSwap16(aUint16);

exit:
return error;
Expand All @@ -78,7 +78,7 @@ Error FrameData::ReadLittleEndianUint32(uint32_t &aUint32)
Error error;

SuccessOrExit(error = ReadBytes(&aUint32, sizeof(uint32_t)));
aUint32 = Encoding::LittleEndian::HostSwap32(aUint32);
aUint32 = LittleEndian::HostSwap32(aUint32);

exit:
return error;
Expand Down
48 changes: 24 additions & 24 deletions src/core/common/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,39 +190,39 @@ class SettingsBase : public InstanceLocator
* @returns The RLOC16.
*
*/
uint16_t GetRloc16(void) const { return Encoding::LittleEndian::HostSwap16(mRloc16); }
uint16_t GetRloc16(void) const { return LittleEndian::HostSwap16(mRloc16); }

/**
* Sets the RLOC16.
*
* @param[in] aRloc16 The RLOC16.
*
*/
void SetRloc16(uint16_t aRloc16) { mRloc16 = Encoding::LittleEndian::HostSwap16(aRloc16); }
void SetRloc16(uint16_t aRloc16) { mRloc16 = LittleEndian::HostSwap16(aRloc16); }

/**
* Returns the key sequence.
*
* @returns The key sequence.
*
*/
uint32_t GetKeySequence(void) const { return Encoding::LittleEndian::HostSwap32(mKeySequence); }
uint32_t GetKeySequence(void) const { return LittleEndian::HostSwap32(mKeySequence); }

/**
* Sets the key sequence.
*
* @param[in] aKeySequence The key sequence.
*
*/
void SetKeySequence(uint32_t aKeySequence) { mKeySequence = Encoding::LittleEndian::HostSwap32(aKeySequence); }
void SetKeySequence(uint32_t aKeySequence) { mKeySequence = LittleEndian::HostSwap32(aKeySequence); }

/**
* Returns the MLE frame counter.
*
* @returns The MLE frame counter.
*
*/
uint32_t GetMleFrameCounter(void) const { return Encoding::LittleEndian::HostSwap32(mMleFrameCounter); }
uint32_t GetMleFrameCounter(void) const { return LittleEndian::HostSwap32(mMleFrameCounter); }

/**
* Sets the MLE frame counter.
Expand All @@ -232,7 +232,7 @@ class SettingsBase : public InstanceLocator
*/
void SetMleFrameCounter(uint32_t aMleFrameCounter)
{
mMleFrameCounter = Encoding::LittleEndian::HostSwap32(aMleFrameCounter);
mMleFrameCounter = LittleEndian::HostSwap32(aMleFrameCounter);
}

/**
Expand All @@ -241,7 +241,7 @@ class SettingsBase : public InstanceLocator
* @returns The MAC frame counter.
*
*/
uint32_t GetMacFrameCounter(void) const { return Encoding::LittleEndian::HostSwap32(mMacFrameCounter); }
uint32_t GetMacFrameCounter(void) const { return LittleEndian::HostSwap32(mMacFrameCounter); }

/**
* Sets the MAC frame counter.
Expand All @@ -251,7 +251,7 @@ class SettingsBase : public InstanceLocator
*/
void SetMacFrameCounter(uint32_t aMacFrameCounter)
{
mMacFrameCounter = Encoding::LittleEndian::HostSwap32(aMacFrameCounter);
mMacFrameCounter = LittleEndian::HostSwap32(aMacFrameCounter);
}

/**
Expand All @@ -260,7 +260,7 @@ class SettingsBase : public InstanceLocator
* @returns The previous partition ID.
*
*/
uint32_t GetPreviousPartitionId(void) const { return Encoding::LittleEndian::HostSwap32(mPreviousPartitionId); }
uint32_t GetPreviousPartitionId(void) const { return LittleEndian::HostSwap32(mPreviousPartitionId); }

/**
* Sets the previous partition id.
Expand All @@ -270,7 +270,7 @@ class SettingsBase : public InstanceLocator
*/
void SetPreviousPartitionId(uint32_t aPreviousPartitionId)
{
mPreviousPartitionId = Encoding::LittleEndian::HostSwap32(aPreviousPartitionId);
mPreviousPartitionId = LittleEndian::HostSwap32(aPreviousPartitionId);
}

/**
Expand Down Expand Up @@ -311,15 +311,15 @@ class SettingsBase : public InstanceLocator
* @returns The Thread version.
*
*/
uint16_t GetVersion(void) const { return Encoding::LittleEndian::HostSwap16(mVersion); }
uint16_t GetVersion(void) const { return LittleEndian::HostSwap16(mVersion); }

/**
* Sets the Thread version.
*
* @param[in] aVersion The Thread version.
*
*/
void SetVersion(uint16_t aVersion) { mVersion = Encoding::LittleEndian::HostSwap16(aVersion); }
void SetVersion(uint16_t aVersion) { mVersion = LittleEndian::HostSwap16(aVersion); }

private:
void Log(Action aAction) const;
Expand Down Expand Up @@ -381,15 +381,15 @@ class SettingsBase : public InstanceLocator
* @returns The Thread version.
*
*/
uint16_t GetVersion(void) const { return Encoding::LittleEndian::HostSwap16(mVersion); }
uint16_t GetVersion(void) const { return LittleEndian::HostSwap16(mVersion); }

/**
* Sets the Thread version.
*
* @param[in] aVersion The Thread version.
*
*/
void SetVersion(uint16_t aVersion) { mVersion = Encoding::LittleEndian::HostSwap16(aVersion); }
void SetVersion(uint16_t aVersion) { mVersion = LittleEndian::HostSwap16(aVersion); }

private:
void Log(Action aAction) const;
Expand Down Expand Up @@ -443,31 +443,31 @@ class SettingsBase : public InstanceLocator
* @returns The child timeout.
*
*/
uint32_t GetTimeout(void) const { return Encoding::LittleEndian::HostSwap32(mTimeout); }
uint32_t GetTimeout(void) const { return LittleEndian::HostSwap32(mTimeout); }

/**
* Sets the child timeout.
*
* @param[in] aTimeout The child timeout.
*
*/
void SetTimeout(uint32_t aTimeout) { mTimeout = Encoding::LittleEndian::HostSwap32(aTimeout); }
void SetTimeout(uint32_t aTimeout) { mTimeout = LittleEndian::HostSwap32(aTimeout); }

/**
* Returns the RLOC16.
*
* @returns The RLOC16.
*
*/
uint16_t GetRloc16(void) const { return Encoding::LittleEndian::HostSwap16(mRloc16); }
uint16_t GetRloc16(void) const { return LittleEndian::HostSwap16(mRloc16); }

/**
* Sets the RLOC16.
*
* @param[in] aRloc16 The RLOC16.
*
*/
void SetRloc16(uint16_t aRloc16) { mRloc16 = Encoding::LittleEndian::HostSwap16(aRloc16); }
void SetRloc16(uint16_t aRloc16) { mRloc16 = LittleEndian::HostSwap16(aRloc16); }

/**
* Returns the Thread device mode.
Expand All @@ -491,15 +491,15 @@ class SettingsBase : public InstanceLocator
* @returns The Thread version.
*
*/
uint16_t GetVersion(void) const { return Encoding::LittleEndian::HostSwap16(mVersion); }
uint16_t GetVersion(void) const { return LittleEndian::HostSwap16(mVersion); }

/**
* Sets the Thread version.
*
* @param[in] aVersion The Thread version.
*
*/
void SetVersion(uint16_t aVersion) { mVersion = Encoding::LittleEndian::HostSwap16(aVersion); }
void SetVersion(uint16_t aVersion) { mVersion = LittleEndian::HostSwap16(aVersion); }

private:
void Log(Action aAction) const;
Expand Down Expand Up @@ -705,15 +705,15 @@ class SettingsBase : public InstanceLocator
* @returns The server port number.
*
*/
uint16_t GetServerPort(void) const { return Encoding::LittleEndian::HostSwap16(mServerPort); }
uint16_t GetServerPort(void) const { return LittleEndian::HostSwap16(mServerPort); }

/**
* Sets the server port number.
*
* @param[in] aPort The server port number.
*
*/
void SetServerPort(uint16_t aPort) { mServerPort = Encoding::LittleEndian::HostSwap16(aPort); }
void SetServerPort(uint16_t aPort) { mServerPort = LittleEndian::HostSwap16(aPort); }

private:
void Log(Action aAction) const;
Expand Down Expand Up @@ -750,15 +750,15 @@ class SettingsBase : public InstanceLocator
* @returns The server port number.
*
*/
uint16_t GetPort(void) const { return Encoding::LittleEndian::HostSwap16(mPort); }
uint16_t GetPort(void) const { return LittleEndian::HostSwap16(mPort); }

/**
* Sets the server port number.
*
* @param[in] aPort The server port number.
*
*/
void SetPort(uint16_t aPort) { mPort = Encoding::LittleEndian::HostSwap16(aPort); }
void SetPort(uint16_t aPort) { mPort = LittleEndian::HostSwap16(aPort); }

private:
void Log(Action aAction) const;
Expand Down
4 changes: 2 additions & 2 deletions src/core/common/tlvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ template <typename UintType> Error Tlv::ReadUintTlv(const Message &aMessage, uin
Error error;

SuccessOrExit(error = ReadTlvValue(aMessage, aOffset, &aValue, sizeof(aValue)));
aValue = Encoding::BigEndian::HostSwap<UintType>(aValue);
aValue = BigEndian::HostSwap<UintType>(aValue);

exit:
return error;
Expand Down Expand Up @@ -294,7 +294,7 @@ Error Tlv::AppendStringTlv(Message &aMessage, uint8_t aType, uint8_t aMaxStringL

template <typename UintType> Error Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, UintType aValue)
{
UintType value = Encoding::BigEndian::HostSwap<UintType>(aValue);
UintType value = BigEndian::HostSwap<UintType>(aValue);

return AppendTlv(aMessage, aType, &value, sizeof(UintType));
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/common/tlvs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@

namespace ot {

using ot::Encoding::BigEndian::HostSwap16;

class Message;

/**
Expand Down Expand Up @@ -641,7 +639,7 @@ class ExtendedTlv : public Tlv
* Returns the Length value.
*
*/
uint16_t GetLength(void) const { return HostSwap16(mLength); }
uint16_t GetLength(void) const { return BigEndian::HostSwap16(mLength); }

/**
* Sets the Length value.
Expand All @@ -652,7 +650,7 @@ class ExtendedTlv : public Tlv
void SetLength(uint16_t aLength)
{
Tlv::SetLength(kExtendedLength);
mLength = HostSwap16(aLength);
mLength = BigEndian::HostSwap16(aLength);
}

private:
Expand Down
Loading

0 comments on commit 6e7ed5c

Please sign in to comment.