26#include <ns3/random-variable-stream.h>
27#include <ns3/simulator.h>
31#undef NS_LOG_APPEND_CONTEXT
32#define NS_LOG_APPEND_CONTEXT std::clog << "[address " << m_mac->GetShortAddress() << "] ";
46 .SetGroupName(
"LrWpan")
63 m_random = CreateObject<UniformRandomVariable>();
130 "MacMinBE (" << macMinBE <<
") should be <= MacMaxBE (" <<
m_macMaxBE <<
")");
146 "MacMaxBE (" << macMaxBE <<
") should be >= 3 and <= 8");
161 NS_ASSERT_MSG(macMaxCSMABackoffs <= 5,
"MacMaxCSMABackoffs should be <= 5");
195 Time elapsedSuperframe;
197 double symbolsToBoundary;
199 uint64_t elapsedSuperframeSymbols;
200 uint64_t symbolRate =
201 (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
207 elapsedSuperframe = currentTime -
m_mac->m_macBeaconRxTime;
209 Time beaconTime [[maybe_unused]] =
Seconds((
double)
m_mac->m_rxBeaconSymbols / symbolRate);
210 Time elapsedCap [[maybe_unused]] = elapsedSuperframe - beaconTime;
212 <<
" (" << elapsedCap.
As(
Time::S) <<
")");
217 elapsedSuperframe = currentTime -
m_mac->m_macBeaconTxTime;
221 elapsedSuperframeSymbols = elapsedSuperframe.
GetSeconds() * symbolRate;
225 timeAtBoundary =
Seconds((
double)(elapsedSuperframeSymbols + symbolsToBoundary) / symbolRate);
228 nextBoundary = timeAtBoundary - elapsedSuperframe;
230 NS_LOG_DEBUG(
"Elapsed Superframe symbols: " << elapsedSuperframeSymbols <<
" ("
231 << elapsedSuperframe.
As(
Time::S) <<
")");
234 << nextBoundary.
GetSeconds() * symbolRate <<
" symbols ("
288 m_mac->GetPhy()->CcaCancel();
296 uint64_t upperBound = (uint64_t)pow(2,
m_BE) - 1;
301 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
315 NS_LOG_DEBUG(
"Unslotted CSMA-CA: requesting CCA after backoff of "
327 NS_LOG_DEBUG(
"Slotted CSMA-CA: proceeding after random backoff of "
329 << (randomBackoff.
GetSeconds() * symbolRate) <<
" symbols or "
334 << (timeLeftInCap.
GetSeconds() * symbolRate) <<
" symbols or "
337 if (randomBackoff >= timeLeftInCap)
339 uint64_t usedBackoffs =
342 NS_LOG_DEBUG(
"No time in CAP to complete backoff delay, deferring to the next CAP");
365 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
369 activeSlot =
m_mac->m_incomingSuperframeDuration / 16;
370 capSymbols = activeSlot * (
m_mac->m_incomingFnlCapSlot + 1);
371 endCapTime =
m_mac->m_macBeaconRxTime +
Seconds((
double)capSymbols / symbolRate);
375 activeSlot =
m_mac->m_superframeDuration / 16;
376 capSymbols = activeSlot * (
m_mac->m_fnlCapSlot + 1);
377 endCapTime =
m_mac->m_macBeaconTxTime +
Seconds((
double)capSymbols / symbolRate);
380 return (endCapTime - currentTime);
391 Time transactionTime;
396 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
409 ccaSymbols += 8 *
m_CW;
417 transactionSymbols = ccaSymbols +
m_mac->GetTxPacketSymbols();
419 if (
m_mac->isTxAckReq())
422 transactionSymbols +=
m_mac->GetMacAckWaitDuration();
427 transactionSymbols += (
m_mac->GetPhy()->aTurnaroundTime * 2);
429 transactionSymbols +=
m_mac->GetIfsSize();
437 transactionTime =
Seconds((
double)transactionSymbols / symbolRate);
438 NS_LOG_DEBUG(
"Total required transaction: " << transactionSymbols <<
" symbols ("
441 if (transactionTime > timeLeftInCap)
444 << transactionSymbols <<
" symbols "
445 <<
"cannot be completed in CAP, deferring transmission to the next CAP");
463 m_mac->GetPhy()->PlmeCcaRequest();
529 NS_LOG_LOGIC(
"Notifying MAC of Channel access failure");
536 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.
This class is a helper for the LrWpanMac to manage the Csma/CA state machine according to IEEE 802....
uint64_t m_aUnitBackoffPeriod
Number of symbols per CSMA/CA time unit, default 20 symbols.
Ptr< LrWpanMac > m_mac
The MAC instance for which this CSMA/CA implemenation is configured.
void SetBatteryLifeExtension(bool batteryLifeExtension)
Set the value of the Battery Life Extension.
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
LrWpanCsmaCa()
Default constructor.
void RequestCCA()
Request the Phy to perform CCA (Step 3)
uint8_t GetMacMinBE() const
Get the minimum backoff exponent value.
bool m_isSlotted
Beacon-enabled slotted or nonbeacon-enabled unslotted CSMA-CA.
uint8_t m_BE
Backoff exponent.
EventId m_canProceedEvent
Scheduler event for checking if we can complete the transmission before the end of the CAP.
static TypeId GetTypeId()
Get the type ID.
EventId m_randomBackoffEvent
Scheduler event for the start of the next random backoff/slot.
bool GetBatteryLifeExtension()
Get the value of the Battery Life Extension.
void SetUnSlottedCsmaCa()
Configure for the use of the unslotted CSMA/CA version.
void DeferCsmaTimeout()
The CSMA algorithm call this function at the end of the CAP to return the MAC state back to to IDLE a...
bool IsUnSlottedCsmaCa() const
Check if the unslotted CSMA/CA version is being used.
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to which this CSMA/CA implementation is attached to.
LrWpanMacStateCallback m_lrWpanMacStateCallback
The callback to inform the configured MAC of the CSMA/CA result.
Ptr< LrWpanMac > GetMac() const
Get the MAC to which this CSMA/CA implementation is attached to.
LrWpanMacTransCostCallback m_lrWpanMacTransCostCallback
The callback to inform the cost of a transaction in slotted CSMA-CA.
void Cancel()
Cancel CSMA-CA algorithm.
uint8_t GetNB()
Get the number of CSMA retries.
EventId m_requestCcaEvent
Scheduler event when to start the CCA after a random backoff.
bool m_coorDest
Indicates whether the CSMA procedure is targeted for a message to be sent to the coordinator.
bool IsSlottedCsmaCa() const
Check if the slotted CSMA/CA version is being used.
void SetLrWpanMacTransCostCallback(LrWpanMacTransCostCallback trans)
Set the callback function to report a transaction cost in slotted CSMA-CA.
void SetMacMaxBE(uint8_t macMaxBE)
Set the maximum backoff exponent value.
void RandomBackoffDelay()
In step 2 of the CSMA-CA, perform a random backoff in the range of 0 to 2^BE -1.
uint8_t m_macMaxBE
Maximum backoff exponent.
void SetMacMinBE(uint8_t macMinBE)
Set the minimum backoff exponent value.
uint8_t m_CW
Contention window length (used in slotted ver only).
uint8_t m_macMinBE
Minimum backoff exponent.
uint8_t GetMacMaxCSMABackoffs() const
Get the maximum number of backoffs.
void SetUnitBackoffPeriod(uint64_t unitBackoffPeriod)
Set the number of symbols forming the basic time period used by the CSMA-CA algorithm.
bool m_macBattLifeExt
Battery Life Extension.
Time GetTimeToNextSlot() const
Locates the time to the next backoff period boundary in the SUPERFRAME and returns the amount of time...
uint8_t GetMacMaxBE() const
Get the maximum backoff exponent value.
void SetSlottedCsmaCa()
Configure for the use of the slotted CSMA/CA version.
EventId m_endCapEvent
Scheduler event for the end of the current CAP.
void CanProceed()
In the slotted CSMA-CA, after random backoff, determine if the remaining CSMA-CA operation can procee...
uint64_t m_randomBackoffPeriodsLeft
Count the number of remaining random backoff periods left to delay.
Time GetTimeLeftInCap()
Get the time left in the CAP portion of the Outgoing or Incoming superframe.
bool m_ccaRequestRunning
Flag indicating that the PHY is currently running a CCA.
void SetLrWpanMacStateCallback(LrWpanMacStateCallback macState)
Set the callback function to the MAC.
void DoDispose() override
Destructor implementation.
void Start()
Start CSMA-CA algorithm (step 1), initialize NB, BE for both slotted and unslotted CSMA-CA.
uint8_t m_NB
Number of backoffs for the current transmission.
Ptr< UniformRandomVariable > m_random
Uniform random variable stream.
void SetMacMaxCSMABackoffs(uint8_t macMaxCSMABackoffs)
Set the maximum number of backoffs.
uint64_t GetUnitBackoffPeriod() const
Get the number of symbols forming the basic time period used by the CSMA-CA algorithm.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
uint8_t m_macMaxCSMABackoffs
Maximum number of backoffs.
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.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#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 ",...
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
@ CHANNEL_ACCESS_FAILURE
CHANNEL_ACCESS_FAILURE.
@ MAC_CSMA_DEFERRED
MAC_CSMA_DEFERRED.
@ CHANNEL_IDLE
CHANNEL_IDLE.
#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.