network: Add Mac16 and Mac64 functions

This commit is contained in:
Alberto Gallegos
2023-03-30 13:25:03 +09:00
parent 043199ce9d
commit a3777c82f3
6 changed files with 46 additions and 8 deletions

View File

@@ -106,6 +106,7 @@ Mac16Address::Mac16Address(const char* str)
Mac16Address::Mac16Address(uint16_t addr)
{
NS_LOG_FUNCTION(this);
m_address[1] = addr & 0xFF;
m_address[0] = (addr >> 8) & 0xFF;
}
@@ -153,6 +154,15 @@ Mac16Address::ConvertTo() const
return Address(GetType(), m_address, 2);
}
uint16_t
Mac16Address::ConvertToInt() const
{
uint16_t addr = m_address[1] & (0xFF);
addr |= (m_address[0] << 8) & (0xFF << 8);
return addr;
}
Mac16Address
Mac16Address::Allocate()
{

View File

@@ -51,9 +51,9 @@ class Mac16Address
Mac16Address(const char* str);
/**
* \param addr The 16 bit integer used to create a Mac16Address object.
* \param addr The 16 bit unsigned integer used to create a Mac16Address object.
*
* Create a Mac16Address from an 16 bit integer.
* Create a Mac16Address from an 16 bit unsigned integer.
*/
Mac16Address(uint16_t addr);
@@ -95,6 +95,13 @@ class Mac16Address
*/
Address ConvertTo() const;
/**
* \return the mac address in a 16 bit unsigned integer
*
* Convert an instance of this class to a 16 bit unsigned integer.
*/
uint16_t ConvertToInt() const;
/**
* \param address address to test
* \returns true if the address matches, false otherwise.

View File

@@ -104,6 +104,19 @@ Mac64Address::Mac64Address(const char* str)
NS_ASSERT(i == 8);
}
Mac64Address::Mac64Address(uint64_t addr)
{
NS_LOG_FUNCTION(this);
m_address[7] = addr & 0xFF;
m_address[6] = (addr >> 8) & 0xFF;
m_address[5] = (addr >> 16) & 0xFF;
m_address[4] = (addr >> 24) & 0xFF;
m_address[3] = (addr >> 32) & 0xFF;
m_address[2] = (addr >> 40) & 0xFF;
m_address[1] = (addr >> 48) & 0xFF;
m_address[0] = (addr >> 56) & 0xFF;
}
void
Mac64Address::CopyFrom(const uint8_t buffer[8])
{
@@ -150,10 +163,8 @@ Mac64Address::ConvertTo() const
uint64_t
Mac64Address::ConvertToInt() const
{
uint64_t addr = 0;
uint64_t shift = 0xFF;
addr = static_cast<uint64_t>(m_address[7]) & (shift);
uint64_t addr = static_cast<uint64_t>(m_address[7]) & (shift);
addr |= (static_cast<uint64_t>(m_address[6]) << 8) & (shift << 8);
addr |= (static_cast<uint64_t>(m_address[5]) << 16) & (shift << 16);
addr |= (static_cast<uint64_t>(m_address[4]) << 24) & (shift << 24);

View File

@@ -49,10 +49,17 @@ class Mac64Address
/**
* \param str a string representing the new Mac64Address
*
* The format of the string is "xx:xx:xx:xx:xx:xx"
* The format of the string is "xx:xx:xx:xx:xx:xx:xx:xx"
*/
Mac64Address(const char* str);
/**
* \param addr The 64 bit unsigned integer used to create a Mac64Address object.
*
* Create a Mac64Address from an 64 bit unsigned integer.
*/
Mac64Address(uint64_t addr);
/**
* \param buffer address in network order
*
@@ -88,9 +95,9 @@ class Mac64Address
Address ConvertTo() const;
/**
* \return the mac address in a 64 bit int
* \return the mac address in a 64 bit unsigned integer.
*
* Convert an instance of this class to a 64 bit int.
* Convert an instance of this class to a 64 bit unsigned integer.
*/
uint64_t ConvertToInt() const;