From d1f44345e5e04fb6bab42fff10de3cc0e039edee Mon Sep 17 00:00:00 2001 From: Alberto Gallegos Ramonet Date: Tue, 25 Jun 2024 11:47:53 +0900 Subject: [PATCH] lr-wpan: beacon improvements and jitter addition --- CHANGES.md | 3 ++ RELEASE_NOTES.md | 1 + src/lr-wpan/model/lr-wpan-csmaca.cc | 10 ++--- src/lr-wpan/model/lr-wpan-mac.cc | 64 +++++++++++++++++++++++------ src/lr-wpan/model/lr-wpan-mac.h | 11 ++++- 5 files changed, 70 insertions(+), 19 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 387b539fc..287c924d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,10 +21,13 @@ Changes from ns-3.42 to ns-3-dev ### Changes to existing API * (lr-wpan) Attribute `macBeaconPayload` in `MacPibAttributes` is now a std::vector instead of a packet pointer. * (lr-wpan) Removes the word `address` from the MAC address prefix when `LOG_PREFIX_FUNC` is used. +* (lr-wpan) Removes the word `address` from the CSMA-CA logs prefix when `LOG_PREFIX_FUNC` is used. ### Changes to build system ### Changed behavior +* (lr-wpan) Beacons are now transmitted using CSMA-CA when requested from a beacon request command. +* (lr-wpan) Upon a beacon request command, beacons are transmitted after a jitter to reduce the probability of collisions. Changes from ns-3.41 to ns-3.42 ------------------------------- diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index d7c615bd6..5424d3754 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -23,6 +23,7 @@ Release 3-dev ### Bugs fixed - (lr-wpan) !2001 - Beacon improvements and fixes +- (lr-wpan) !2042 - Beacon improvements and jitter addition Release 3.42 ------------ diff --git a/src/lr-wpan/model/lr-wpan-csmaca.cc b/src/lr-wpan/model/lr-wpan-csmaca.cc index 5b4ee97f3..a2068d5d8 100644 --- a/src/lr-wpan/model/lr-wpan-csmaca.cc +++ b/src/lr-wpan/model/lr-wpan-csmaca.cc @@ -32,8 +32,7 @@ #undef NS_LOG_APPEND_CONTEXT #define NS_LOG_APPEND_CONTEXT \ - std::clog << "[address " << m_mac->GetShortAddress() << " | " << m_mac->GetExtendedAddress() \ - << "] "; + std::clog << "[" << m_mac->GetShortAddress() << " | " << m_mac->GetExtendedAddress() << "] "; namespace ns3 { @@ -101,28 +100,24 @@ LrWpanCsmaCa::GetMac() const void LrWpanCsmaCa::SetSlottedCsmaCa() { - NS_LOG_FUNCTION(this); m_isSlotted = true; } void LrWpanCsmaCa::SetUnSlottedCsmaCa() { - NS_LOG_FUNCTION(this); m_isSlotted = false; } bool LrWpanCsmaCa::IsSlottedCsmaCa() const { - NS_LOG_FUNCTION(this); return m_isSlotted; } bool LrWpanCsmaCa::IsUnSlottedCsmaCa() const { - NS_LOG_FUNCTION(this); return !m_isSlotted; } @@ -234,6 +229,8 @@ LrWpanCsmaCa::Start() m_NB = 0; if (IsSlottedCsmaCa()) { + NS_LOG_DEBUG("Using Slotted CSMA-CA"); + // TODO: Check if the current PHY is using the Japanese band 950 Mhz: // (IEEE_802_15_4_950MHZ_BPSK and IEEE_802_15_4_950MHZ_2GFSK) // if in use, m_CW = 1. @@ -264,6 +261,7 @@ LrWpanCsmaCa::Start() } else { + NS_LOG_DEBUG("Using Unslotted CSMA-CA"); m_BE = m_macMinBE; m_randomBackoffEvent = Simulator::ScheduleNow(&LrWpanCsmaCa::RandomBackoffDelay, this); } diff --git a/src/lr-wpan/model/lr-wpan-mac.cc b/src/lr-wpan/model/lr-wpan-mac.cc index 820039ffa..88b0e66fa 100644 --- a/src/lr-wpan/model/lr-wpan-mac.cc +++ b/src/lr-wpan/model/lr-wpan-mac.cc @@ -252,9 +252,9 @@ LrWpanMac::LrWpanMac() m_maxTxQueueSize = m_txQueue.max_size(); m_maxIndTxQueueSize = m_indTxQueue.max_size(); - uniformVar = CreateObject(); - m_macDsn = SequenceNumber8(uniformVar->GetInteger(0, 255)); - m_macBsn = SequenceNumber8(uniformVar->GetInteger(0, 255)); + m_uniformVar = CreateObject(); + m_macDsn = SequenceNumber8(m_uniformVar->GetInteger(0, 255)); + m_macBsn = SequenceNumber8(m_uniformVar->GetInteger(0, 255)); m_macBeaconPayload = {}; m_macBeaconPayloadLength = 0; m_shortAddress = Mac16Address("FF:FF"); // FF:FF = The address is not assigned. @@ -1049,18 +1049,28 @@ LrWpanMac::SendOneBeacon() beaconPacket->AddTrailer(macTrailer); - // Set the Beacon packet to be transmitted - m_txPkt = beaconPacket; - if (m_csmaCa->IsSlottedCsmaCa()) { + // Beacon in beacon-enabled mode + // Transmit beacon immediately (i.e. Without CSMA/CA) + m_txPkt = beaconPacket; m_outSuperframeStatus = BEACON; NS_LOG_DEBUG("Outgoing superframe Active Portion (Beacon + CAP + CFP): " << m_superframeDuration << " symbols"); - } - ChangeMacState(MAC_SENDING); - m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON); + ChangeMacState(MAC_SENDING); + m_phy->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON); + } + else + { + // Beacon as a result of a beacon request + // The beacon shall be transmitted using CSMA/CA + // IEEE 802.15.4-2011 (Section 5.1.2.1.2) + Ptr txQElement = Create(); + txQElement->txQPkt = beaconPacket; + EnqueueTxQElement(txQElement); + CheckQueue(); + } } void @@ -1710,7 +1720,7 @@ LrWpanMac::BeaconSearchTimeout() void LrWpanMac::ReceiveBeacon(uint8_t lqi, Ptr p) { - NS_LOG_FUNCTION(this); + NS_LOG_FUNCTION(this << lqi << p); // The received beacon size in symbols // Beacon = Sync Header (SHR)[5 bytes] + // PHY header (PHR) [1 byte] + @@ -2322,7 +2332,17 @@ LrWpanMac::PdDataIndication(uint32_t psduLength, Ptr p, uint8_t lqi) case CommandPayloadHeader::BEACON_REQ: if (m_csmaCa->IsUnSlottedCsmaCa() && m_coor) { - SendOneBeacon(); + // Jitter = Between 0 and 2 aUnitBackoffPeriods + // (0, 320us or 640us in 2.4Ghz O-QPSK) + // While this jitter is not described by the standard, + // it reduces the probability of collisions in beacons + // transmitted as a result of a beacon request + Time jitter = + Seconds(static_cast(m_uniformVar->GetInteger(0, 3) * + aUnitBackoffPeriod) / + symbolRate); + + Simulator::Schedule((jitter), &LrWpanMac::SendOneBeacon, this); } else { @@ -2981,6 +3001,15 @@ LrWpanMac::PrintTxQueue(std::ostream& os) const os << "\n"; } +int64_t +LrWpanMac::AssignStreams(int64_t stream) +{ + NS_LOG_FUNCTION(this); + m_uniformVar->SetStream(stream); + m_csmaCa->AssignStreams(stream); + return 1; +} + void LrWpanMac::RemovePendTxQElement(Ptr p) { @@ -3063,7 +3092,18 @@ LrWpanMac::PdDataConfirm(PhyEnumeration status) } ifsWaitTime = Seconds(static_cast(GetIfsSize()) / symbolRate); - m_txPkt = nullptr; + + if (m_csmaCa->IsSlottedCsmaCa()) + { + // The beacon was sent immediately in beacon-enabled mode + m_txPkt = nullptr; + } + else + { + // The beacon was sent using CSMA/CA as a result of a beacon request + // therefore, remove it from TX Queue + RemoveFirstTxQElement(); + } } else if (macHdr.IsAckReq()) // We have sent a regular data packet, check if we have to // wait for an ACK. diff --git a/src/lr-wpan/model/lr-wpan-mac.h b/src/lr-wpan/model/lr-wpan-mac.h index 6a5c1602c..4c2c0a95a 100644 --- a/src/lr-wpan/model/lr-wpan-mac.h +++ b/src/lr-wpan/model/lr-wpan-mac.h @@ -712,6 +712,15 @@ class LrWpanMac : public LrWpanMacBase */ void PrintTxQueue(std::ostream& os) const; + /** + * Assign a fixed random variable stream number to the random variables + * used by this model. Return the number of streams that have been assigned. + * + * \param stream first stream index to use + * \return the number of stream indices assigned by this model + */ + int64_t AssignStreams(int64_t stream); + /** * TracedCallback signature for sent packets. * @@ -1335,7 +1344,7 @@ class LrWpanMac : public LrWpanMacBase /** * The uniform random variable used in this mac layer */ - Ptr uniformVar; + Ptr m_uniformVar; }; } // namespace lrwpan } // namespace ns3