28#include <ns3/random-variable-stream.h>
29#include <ns3/simulator.h>
33#undef NS_LOG_APPEND_CONTEXT
34#define NS_LOG_APPEND_CONTEXT \
35 std::clog << "[address " << m_mac->GetShortAddress() << " | " << m_mac->GetExtendedAddress() \
51 .SetGroupName(
"LrWpan")
67 m_random = CreateObject<UniformRandomVariable>();
150 "MacMaxBE (" << macMaxBE <<
") should be >= 3 and <= 8");
165 NS_ASSERT_MSG(macMaxCSMABackoffs <= 5,
"MacMaxCSMABackoffs should be <= 5");
185 Time elapsedSuperframe;
187 double symbolsToBoundary;
189 uint64_t elapsedSuperframeSymbols;
190 uint64_t symbolRate =
191 (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
197 elapsedSuperframe = currentTime -
m_mac->m_macBeaconRxTime;
199 Time beaconTime [[maybe_unused]] =
Seconds((
double)
m_mac->m_rxBeaconSymbols / symbolRate);
200 Time elapsedCap [[maybe_unused]] = elapsedSuperframe - beaconTime;
202 <<
" (" << elapsedCap.
As(
Time::S) <<
")");
207 elapsedSuperframe = currentTime -
m_mac->m_macBeaconTxTime;
211 elapsedSuperframeSymbols = elapsedSuperframe.
GetSeconds() * symbolRate;
215 timeAtBoundary =
Seconds((
double)(elapsedSuperframeSymbols + symbolsToBoundary) / symbolRate);
218 nextBoundary = timeAtBoundary - elapsedSuperframe;
220 NS_LOG_DEBUG(
"Elapsed Superframe symbols: " << elapsedSuperframeSymbols <<
" ("
221 << elapsedSuperframe.
As(
Time::S) <<
")");
224 << nextBoundary.
GetSeconds() * symbolRate <<
" symbols ("
278 m_mac->GetPhy()->CcaCancel();
286 uint64_t upperBound = (uint64_t)pow(2,
m_BE) - 1;
291 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
305 NS_LOG_DEBUG(
"Unslotted CSMA-CA: requesting CCA after backoff of "
317 NS_LOG_DEBUG(
"Slotted CSMA-CA: proceeding after random backoff of "
319 << (randomBackoff.
GetSeconds() * symbolRate) <<
" symbols or "
324 <<
" (" << (timeLeftInCap.
GetSeconds() * symbolRate) <<
" symbols or "
327 if (randomBackoff >= timeLeftInCap)
329 uint64_t usedBackoffs =
332 NS_LOG_DEBUG(
"No time in CAP to complete backoff delay, deferring to the next CAP");
355 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
359 activeSlot =
m_mac->m_incomingSuperframeDuration / 16;
360 capSymbols = activeSlot * (
m_mac->m_incomingFnlCapSlot + 1);
361 endCapTime =
m_mac->m_macBeaconRxTime +
Seconds((
double)capSymbols / symbolRate);
365 activeSlot =
m_mac->m_superframeDuration / 16;
366 capSymbols = activeSlot * (
m_mac->m_fnlCapSlot + 1);
367 endCapTime =
m_mac->m_macBeaconTxTime +
Seconds((
double)capSymbols / symbolRate);
370 return (endCapTime - currentTime);
381 Time transactionTime;
386 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
399 ccaSymbols += 8 *
m_CW;
407 transactionSymbols = ccaSymbols +
m_mac->GetTxPacketSymbols();
409 if (
m_mac->IsTxAckReq())
412 transactionSymbols +=
m_mac->GetMacAckWaitDuration();
419 transactionSymbols +=
m_mac->GetIfsSize();
427 transactionTime =
Seconds((
double)transactionSymbols / symbolRate);
428 NS_LOG_DEBUG(
"Total required transaction: " << transactionSymbols <<
" symbols ("
431 if (transactionTime > timeLeftInCap)
434 << transactionSymbols <<
" symbols "
435 <<
"cannot be completed in CAP, deferring transmission to the next CAP");
453 m_mac->GetPhy()->PlmeCcaRequest();
519 NS_LOG_LOGIC(
"Notifying MAC of Channel access failure");
526 NS_LOG_DEBUG(
"Perform another backoff; m_NB = " <<
static_cast<uint16_t
>(
m_NB));
bool IsNull() const
Check for null implementation.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Introspection did not find any typical Config paths.
A base class which provides memory management and object aggregation.
Smart pointer class similar to boost::intrusive_ptr.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static Time Now()
Return the current simulation virtual time.
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
This class is a helper for the LrWpanMac to manage the Csma/CA state machine according to IEEE 802....
uint8_t m_macMaxCSMABackoffs
Maximum number of backoffs.
bool m_coorDest
Indicates whether the CSMA procedure is targeted for a message to be sent to the coordinator.
EventId m_randomBackoffEvent
Scheduler event for the start of the next random backoff/slot.
LrWpanMacStateCallback m_lrWpanMacStateCallback
The callback to inform the configured MAC of the CSMA/CA result.
LrWpanMacTransCostCallback m_lrWpanMacTransCostCallback
The callback to inform the cost of a transaction in slotted CSMA-CA.
uint8_t m_CW
Contention window length (used in slotted ver only).
void SetUnSlottedCsmaCa()
Configure for the use of the unslotted CSMA/CA version.
void SetMacMaxCSMABackoffs(uint8_t macMaxCSMABackoffs)
Set the maximum number of backoffs.
void Cancel()
Cancel CSMA-CA algorithm.
void Start()
Start CSMA-CA algorithm (step 1), initialize NB, BE for both slotted and unslotted CSMA-CA.
static TypeId GetTypeId()
Get the type ID.
EventId m_canProceedEvent
Scheduler event for checking if we can complete the transmission before the end of the CAP.
EventId m_requestCcaEvent
Scheduler event when to start the CCA after a random backoff.
Ptr< LrWpanMac > GetMac() const
Get the MAC to which this CSMA/CA implementation is attached to.
void RandomBackoffDelay()
In step 2 of the CSMA-CA, perform a random backoff in the range of 0 to 2^BE -1.
void CanProceed()
In the slotted CSMA-CA, after random backoff, determine if the remaining CSMA-CA operation can procee...
uint8_t GetNB() const
Get the number of CSMA retries.
bool IsUnSlottedCsmaCa() const
Check if the unslotted CSMA/CA version is being used.
uint8_t m_BE
Backoff exponent.
Time GetTimeToNextSlot() const
Locates the time to the next backoff period boundary in the SUPERFRAME and returns the amount of time...
void PlmeCcaConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
bool GetBatteryLifeExtension() const
Get the value of the Battery Life Extension.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
uint64_t m_randomBackoffPeriodsLeft
Count the number of remaining random backoff periods left to delay.
uint8_t GetMacMaxBE() const
Get the maximum backoff exponent value.
uint8_t GetMacMaxCSMABackoffs() const
Get the maximum number of backoffs.
uint8_t GetMacMinBE() const
Get the minimum backoff exponent value.
void SetLrWpanMacTransCostCallback(LrWpanMacTransCostCallback trans)
Set the callback function to report a transaction cost in slotted CSMA-CA.
uint8_t m_macMaxBE
Maximum backoff exponent.
void SetSlottedCsmaCa()
Configure for the use of the slotted CSMA/CA version.
bool m_ccaRequestRunning
Flag indicating that the PHY is currently running a CCA.
bool m_macBattLifeExt
Battery Life Extension.
bool IsSlottedCsmaCa() const
Check if the slotted CSMA/CA version is being used.
Ptr< UniformRandomVariable > m_random
Uniform random variable stream.
bool m_isSlotted
Beacon-enabled slotted or nonbeacon-enabled unslotted CSMA-CA.
void SetMacMinBE(uint8_t macMinBE)
Set the minimum backoff exponent value.
uint8_t m_NB
Number of backoffs for the current transmission.
EventId m_endCapEvent
Scheduler event for the end of the current CAP.
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to which this CSMA/CA implementation is attached to.
Time GetTimeLeftInCap()
Get the time left in the CAP portion of the Outgoing or Incoming superframe.
void SetBatteryLifeExtension(bool batteryLifeExtension)
Set the value of the Battery Life Extension.
void SetMacMaxBE(uint8_t macMaxBE)
Set the maximum backoff exponent value.
void DoDispose() override
Destructor implementation.
void RequestCCA()
Request the Phy to perform CCA (Step 3)
void DeferCsmaTimeout()
The CSMA algorithm call this function at the end of the CAP to return the MAC state back to to IDLE a...
uint8_t m_macMinBE
Minimum backoff exponent.
Ptr< LrWpanMac > m_mac
The MAC instance for which this CSMA/CA implementation is configured.
LrWpanCsmaCa()
Default constructor.
void SetLrWpanMacStateCallback(LrWpanMacStateCallback macState)
Set the callback function to the MAC.
constexpr uint32_t aUnitBackoffPeriod
Number of symbols per CSMA/CA time unit, default 20 symbols.
constexpr uint32_t aTurnaroundTime
The turnaround time in symbol periods for switching the transceiver from RX to TX or vice-versa.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
@ macMinBE
The minimum value of the backoff exponent (BE) in the CSMA-CA algorithm.
@ CHANNEL_ACCESS_FAILURE
CHANNEL_ACCESS_FAILURE.
@ CHANNEL_IDLE
CHANNEL_IDLE.
@ MAC_CSMA_DEFERRED
MAC_CSMA_DEFERRED.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.