From a3777c82f3f1806bc4d89e110e641ea2a1bfb441 Mon Sep 17 00:00:00 2001 From: Alberto Gallegos Date: Thu, 30 Mar 2023 13:25:03 +0900 Subject: [PATCH] network: Add Mac16 and Mac64 functions --- CHANGES.md | 2 ++ RELEASE_NOTES.md | 1 + src/network/utils/mac16-address.cc | 10 ++++++++++ src/network/utils/mac16-address.h | 11 +++++++++-- src/network/utils/mac64-address.cc | 17 ++++++++++++++--- src/network/utils/mac64-address.h | 13 ++++++++++--- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 05df6c866..4edc15746 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,8 @@ Changes from ns-3.38 to ns-3-dev * (lr-wpan) Added support for orphan scans. Orphan scans can now be performed using the existing `LrWpanMac::MlmeScanRequest`; This orphan scan use the added orphan notification commands and coordinator realigment commands. Usage is shown in added `lr-wpan-orphan-scan.cc` example and in the `TestOrphanScan` included in `lr-wpan-mac-test.cc`. * (network) Added `Mac64Address::ConvertToInt`. Converts a Mac64Address object to a uint64_t. +* (network) Added `Mac16Address::ConvertToInt`. Converts a Mac16Address object to a uint16_t. +* (network) Added `Mac16Address::Mac16Address(uint16t addr)` and `Mac16Address::Mac64Address(uint64t addr)` constructors. * (lr-wpan) Added `LrwpanMac::MlmeGetRequest` function and the corresponding confirm callbacks as well as `LrwpanMac::SetMlmeGetConfirm` function. ### Changes to existing API diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 673a914d2..c4a42664f 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -23,6 +23,7 @@ Release 3-dev - (lr-wpan) !1399 - Add orphan scan support. - (network) !1405 - Add ConvertToInt to Mac64Address - (lr-wpan) !1402 - Add attributes to MLME-SET and MLME-GET +- (lr-wpan) 1410 - Add Mac16 and Mac64 functions ### Bugs fixed diff --git a/src/network/utils/mac16-address.cc b/src/network/utils/mac16-address.cc index 76f373711..f27074ae5 100644 --- a/src/network/utils/mac16-address.cc +++ b/src/network/utils/mac16-address.cc @@ -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() { diff --git a/src/network/utils/mac16-address.h b/src/network/utils/mac16-address.h index 0d497727e..6cecc71e9 100644 --- a/src/network/utils/mac16-address.h +++ b/src/network/utils/mac16-address.h @@ -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. diff --git a/src/network/utils/mac64-address.cc b/src/network/utils/mac64-address.cc index 0818523bf..fa7863f18 100644 --- a/src/network/utils/mac64-address.cc +++ b/src/network/utils/mac64-address.cc @@ -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(m_address[7]) & (shift); + uint64_t addr = static_cast(m_address[7]) & (shift); addr |= (static_cast(m_address[6]) << 8) & (shift << 8); addr |= (static_cast(m_address[5]) << 16) & (shift << 16); addr |= (static_cast(m_address[4]) << 24) & (shift << 24); diff --git a/src/network/utils/mac64-address.h b/src/network/utils/mac64-address.h index 172e6a375..e1489188d 100644 --- a/src/network/utils/mac64-address.h +++ b/src/network/utils/mac64-address.h @@ -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;