17#include <ns3/random-variable-stream.h>
18#include <ns3/simulator.h>
22#undef NS_LOG_APPEND_CONTEXT
23#define NS_LOG_APPEND_CONTEXT \
24 std::clog << "[" << m_mac->GetShortAddress() << " | " << m_mac->GetExtendedAddress() << "] ";
40 .SetGroupName(
"LrWpan")
135 "MacMaxBE (" << macMaxBE <<
") should be >= 3 and <= 8");
150 NS_ASSERT_MSG(macMaxCSMABackoffs <= 5,
"MacMaxCSMABackoffs should be <= 5");
170 Time elapsedSuperframe;
172 double symbolsToBoundary;
174 uint64_t elapsedSuperframeSymbols;
175 uint64_t symbolRate =
176 (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
182 elapsedSuperframe = currentTime -
m_mac->m_macBeaconRxTime;
184 Time beaconTime [[maybe_unused]] =
Seconds((
double)
m_mac->m_rxBeaconSymbols / symbolRate);
185 Time elapsedCap [[maybe_unused]] = elapsedSuperframe - beaconTime;
187 <<
" (" << elapsedCap.
As(
Time::S) <<
")");
192 elapsedSuperframe = currentTime -
m_mac->m_macBeaconTxTime;
196 elapsedSuperframeSymbols = elapsedSuperframe.
GetSeconds() * symbolRate;
200 timeAtBoundary =
Seconds((
double)(elapsedSuperframeSymbols + symbolsToBoundary) / symbolRate);
203 nextBoundary = timeAtBoundary - elapsedSuperframe;
205 NS_LOG_DEBUG(
"Elapsed Superframe symbols: " << elapsedSuperframeSymbols <<
" ("
206 << elapsedSuperframe.
As(
Time::S) <<
")");
209 << nextBoundary.
GetSeconds() * symbolRate <<
" symbols ("
266 m_mac->GetPhy()->CcaCancel();
274 uint64_t upperBound = (uint64_t)pow(2,
m_BE) - 1;
279 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
293 NS_LOG_DEBUG(
"Unslotted CSMA-CA: requesting CCA after backoff of "
305 NS_LOG_DEBUG(
"Slotted CSMA-CA: proceeding after random backoff of "
307 << (randomBackoff.
GetSeconds() * symbolRate) <<
" symbols or "
312 <<
" (" << (timeLeftInCap.
GetSeconds() * symbolRate) <<
" symbols or "
315 if (randomBackoff >= timeLeftInCap)
317 uint64_t usedBackoffs =
320 NS_LOG_DEBUG(
"No time in CAP to complete backoff delay, deferring to the next CAP");
343 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
347 activeSlot =
m_mac->m_incomingSuperframeDuration / 16;
348 capSymbols = activeSlot * (
m_mac->m_incomingFnlCapSlot + 1);
349 endCapTime =
m_mac->m_macBeaconRxTime +
Seconds((
double)capSymbols / symbolRate);
353 activeSlot =
m_mac->m_superframeDuration / 16;
354 capSymbols = activeSlot * (
m_mac->m_fnlCapSlot + 1);
355 endCapTime =
m_mac->m_macBeaconTxTime +
Seconds((
double)capSymbols / symbolRate);
358 return (endCapTime - currentTime);
369 Time transactionTime;
374 symbolRate = (uint64_t)
m_mac->GetPhy()->GetDataOrSymbolRate(
false);
387 ccaSymbols += 8 *
m_CW;
395 transactionSymbols = ccaSymbols +
m_mac->GetTxPacketSymbols();
397 if (
m_mac->IsTxAckReq())
400 transactionSymbols +=
m_mac->GetMacAckWaitDuration();
407 transactionSymbols +=
m_mac->GetIfsSize();
415 transactionTime =
Seconds((
double)transactionSymbols / symbolRate);
416 NS_LOG_DEBUG(
"Total required transaction: " << transactionSymbols <<
" symbols ("
419 if (transactionTime > timeLeftInCap)
422 << transactionSymbols <<
" symbols "
423 <<
"cannot be completed in CAP, deferring transmission to the next CAP");
441 m_mac->GetPhy()->PlmeCcaRequest();
507 NS_LOG_LOGIC(
"Notifying MAC of Channel access failure");
514 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.
A base class which provides memory management and object aggregation.
Smart pointer class similar to boost::intrusive_ptr.
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 AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
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...
Callback< R, Args... > MakeNullCallback()
#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.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
#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.