A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-mac-cw.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Leonard Tracy <lentracy@gmail.com>
7 */
8
9#include "uan-mac-cw.h"
10
11#include "uan-header-common.h"
12
13#include "ns3/attribute.h"
14#include "ns3/double.h"
15#include "ns3/log.h"
16#include "ns3/nstime.h"
17#include "ns3/trace-source-accessor.h"
18#include "ns3/uinteger.h"
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("UanMacCw");
24
26
28 : UanMac(),
29 m_phy(nullptr),
30 m_pktTx(nullptr),
31 m_txOngoing(false),
32 m_state(IDLE),
33 m_cleared(false)
34
35{
37}
38
42
43void
45{
46 if (m_cleared)
47 {
48 return;
49 }
50 m_cleared = true;
51 m_pktTx = nullptr;
52 if (m_phy)
53 {
54 m_phy->Clear();
55 m_phy = nullptr;
56 }
58 m_txOngoing = false;
59}
60
61void
67
70{
71 static TypeId tid = TypeId("ns3::UanMacCw")
73 .SetGroupName("Uan")
74 .AddConstructor<UanMacCw>()
75 .AddAttribute("CW",
76 "The MAC parameter CW.",
77 UintegerValue(10),
80 .AddAttribute("SlotTime",
81 "Time slot duration for MAC backoff.",
85 .AddTraceSource("Enqueue",
86 "A packet arrived at the MAC for transmission.",
88 "ns3::UanMacCw::QueueTracedCallback")
89 .AddTraceSource("Dequeue",
90 "A was passed down to the PHY from the MAC.",
92 "ns3::UanMacCw::QueueTracedCallback")
93 .AddTraceSource("RX",
94 "A packet was destined for this MAC and was received.",
96 "ns3::UanMac::PacketModeTracedCallback")
97
98 ;
99 return tid;
100}
101
102bool
103UanMacCw::Enqueue(Ptr<Packet> packet, uint16_t protocolNumber, const Address& dest)
104{
105 switch (m_state)
106 {
107 case CCABUSY:
108 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " MAC " << GetAddress()
109 << " Starting enqueue CCABUSY");
110 if (m_txOngoing)
111 {
112 NS_LOG_DEBUG("State is TX");
113 }
114 else
115 {
116 NS_LOG_DEBUG("State is not TX");
117 }
118
119 NS_ASSERT(!m_phy->GetTransducer()->GetArrivalList().empty() || m_phy->IsStateTx());
120 return false;
121 case RUNNING:
122 NS_LOG_DEBUG("MAC " << GetAddress() << " Starting enqueue RUNNING");
123 NS_ASSERT(m_phy->GetTransducer()->GetArrivalList().empty() && !m_phy->IsStateTx());
124 return false;
125 case TX:
126 case IDLE: {
128
129 UanHeaderCommon header;
130 header.SetDest(Mac8Address::ConvertFrom(dest));
132 header.SetType(0);
133 header.SetProtocolNumber(0);
134 packet->AddHeader(header);
135
137
138 if (m_phy->IsStateBusy())
139 {
140 m_pktTx = packet;
143 auto cw = (uint32_t)m_rv->GetValue(0, m_cw);
146 NS_LOG_DEBUG("Time " << Now().As(Time::S) << ": Addr " << GetAddress()
147 << ": Enqueuing new packet while busy: (Chose CW " << cw
148 << ", Sending at " << m_sendTime.As(Time::S)
149 << " Packet size: " << packet->GetSize());
150 NS_ASSERT(!m_phy->GetTransducer()->GetArrivalList().empty() || m_phy->IsStateTx());
151 }
152 else
153 {
154 NS_ASSERT(m_state != TX);
155 NS_LOG_DEBUG("Time " << Now().As(Time::S) << ": Addr " << GetAddress()
156 << ": Enqueuing new packet while idle (sending)");
157 NS_ASSERT(m_phy->GetTransducer()->GetArrivalList().empty() && !m_phy->IsStateTx());
158 m_state = TX;
159 m_phy->SendPacket(packet, GetTxModeIndex());
160 }
161 break;
162 }
163 default:
164 NS_LOG_DEBUG("MAC " << GetAddress() << " Starting enqueue SOMETHING ELSE");
165 return false;
166 }
167
168 return true;
169}
170
171void
173{
174 m_forwardUpCb = cb;
175}
176
177void
179{
180 m_phy = phy;
181 m_phy->SetReceiveOkCallback(MakeCallback(&UanMacCw::PhyRxPacketGood, this));
182 m_phy->SetReceiveErrorCallback(MakeCallback(&UanMacCw::PhyRxPacketError, this));
183 m_phy->RegisterListener(this);
184}
185
186void
188{
189 if (m_state == RUNNING)
190 {
191 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
192 << ": Switching to channel busy");
193 SaveTimer();
195 }
196}
197
198void
200{
201 if (m_state == CCABUSY && !m_phy->IsStateCcaBusy())
202 {
203 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
204 << ": Switching to channel idle");
206 StartTimer();
207 }
208}
209
210void
212{
213 if (m_state == CCABUSY && !m_phy->IsStateCcaBusy())
214 {
215 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
216 << ": Switching to channel idle");
218 StartTimer();
219 }
220}
221
222void
224{
225 if (m_state == RUNNING)
226 {
227 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
228 << ": Switching to channel busy");
230 SaveTimer();
231 }
232}
233
234void
236{
237 if (m_state == CCABUSY)
238 {
239 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
240 << ": Switching to channel idle");
242 StartTimer();
243 }
244}
245
246void
248{
249 m_txOngoing = true;
250
251 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Tx Start Notified");
252
253 if (m_state == RUNNING)
254 {
255 NS_ASSERT(0);
257 SaveTimer();
258 }
259}
260
261void
263{
264 m_txOngoing = false;
265
266 EndTx();
267}
268
269int64_t
271{
272 NS_LOG_FUNCTION(this << stream);
273 m_rv->SetStream(stream);
274 return 1;
275}
276
277void
279{
281 if (m_state == TX)
282 {
283 m_state = IDLE;
284 }
285 else if (m_state == CCABUSY)
286 {
287 if (m_phy->IsStateIdle())
288 {
289 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
290 << ": Switching to channel idle (After TX!)");
292 StartTimer();
293 }
294 }
295 else
296 {
297 NS_FATAL_ERROR("In strange state at UanMacCw EndTx");
298 }
299}
300
301void
303{
304 m_cw = cw;
305}
306
307void
309{
310 m_slotTime = duration;
311}
312
315{
316 return m_cw;
317}
318
319Time
321{
322 return m_slotTime;
323}
324
325void
326UanMacCw::PhyRxPacketGood(Ptr<Packet> packet, double /* sinr */, UanTxMode /* mode */)
327{
328 UanHeaderCommon header;
329 packet->RemoveHeader(header);
330
331 if (header.GetDest() == Mac8Address::ConvertFrom(GetAddress()) ||
333 {
334 m_forwardUpCb(packet, header.GetProtocolNumber(), header.GetSrc());
335 }
336}
337
338void
339UanMacCw::PhyRxPacketError(Ptr<Packet> /* packet */, double /* sinr */)
340{
341}
342
343void
345{
346 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
347 << " Saving timer (Delay = "
348 << (m_savedDelayS = m_sendTime - Now()).As(Time::S) << ")");
353}
354
355void
357{
359 if (m_sendTime == Simulator::Now())
360 {
361 SendPacket();
362 }
363 else
364 {
366 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress()
367 << " Starting timer (New send time = " << this->m_sendTime.As(Time::S)
368 << ")");
369 }
370}
371
372void
374{
375 NS_LOG_DEBUG("Time " << Now().As(Time::S) << " Addr " << GetAddress() << " Transmitting ");
377 m_state = TX;
378 m_phy->SendPacket(m_pktTx, m_pktTxProt);
379 m_pktTx = nullptr;
380 m_sendTime = Seconds(0);
382}
383
384} // namespace ns3
a polymophic address class
Definition address.h:90
Callback template class.
Definition callback.h:422
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
A class used for addressing MAC8 MAC's.
static Mac8Address GetBroadcast()
Get the broadcast address (255).
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
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.
Definition simulator.h:560
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Common packet header fields.
void SetSrc(Mac8Address src)
Set the source address.
Mac8Address GetDest() const
Get the destination address.
void SetProtocolNumber(uint16_t protocolNumber)
Set the packet type.
Mac8Address GetSrc() const
Get the source address.
void SetDest(Mac8Address dest)
Set the destination address.
void SetType(uint8_t type)
Set the header type.
uint16_t GetProtocolNumber() const
Get the packet type value.
CW-MAC protocol, similar in idea to the 802.11 DCF with constant backoff window.
Definition uan-mac-cw.h:37
bool m_txOngoing
Tx is ongoing.
Definition uan-mac-cw.h:135
Time m_sendTime
Time to send next packet.
Definition uan-mac-cw.h:125
TracedCallback< Ptr< const Packet >, UanTxMode > m_rxLogger
A packet destined for this MAC was received.
Definition uan-mac-cw.h:113
void NotifyCcaStart() override
Called when UanPhy begins sensing channel is busy.
void Clear() override
Clears all pointer references.
Definition uan-mac-cw.cc:44
Ptr< Packet > m_pktTx
Next packet to send.
Definition uan-mac-cw.h:129
void SaveTimer()
Cancel SendEvent and save remaining delay.
TracedCallback< Ptr< const Packet >, uint16_t > m_dequeueLogger
A packet was passed down to the PHY from the MAC.
Definition uan-mac-cw.h:117
void NotifyRxEndError() override
Called when UanPhy finishes receiving packet in error.
bool m_cleared
Flag when we've been cleared.
Definition uan-mac-cw.h:140
void EndTx()
End TX state.
void SendPacket()
Send packet on PHY.
void NotifyCcaEnd() override
Called when UanPhy stops sensing channel is busy.
virtual void SetSlotTime(Time duration)
Set the slot time duration.
virtual uint32_t GetCw()
Get the contention window size.
static TypeId GetTypeId()
Register this type.
Definition uan-mac-cw.cc:69
UanMacCw()
Default constructor.
Definition uan-mac-cw.cc:27
State m_state
Current state.
Definition uan-mac-cw.h:137
Ptr< UniformRandomVariable > m_rv
Provides uniform random variable for contention window.
Definition uan-mac-cw.h:143
bool Enqueue(Ptr< Packet > pkt, uint16_t protocolNumber, const Address &dest) override
Enqueue packet to be transmitted.
void NotifyRxEndOk() override
Called when UanPhy finishes receiving packet without error.
void DoDispose() override
Destructor implementation.
Definition uan-mac-cw.cc:62
virtual void SetCw(uint32_t cw)
Set the contention window size.
Time m_slotTime
Slot time duration.
Definition uan-mac-cw.h:121
~UanMacCw() override
Dummy destructor, DoDispose.
Definition uan-mac-cw.cc:39
void NotifyTxStart(Time duration) override
Called when transmission starts from Phy object.
EventId m_sendEvent
Scheduled SendPacket event.
Definition uan-mac-cw.h:133
void PhyRxPacketError(Ptr< Packet > packet, double sinr)
Packet received at lower layer in error.
Callback< void, Ptr< Packet >, uint16_t, const Mac8Address & > m_forwardUpCb
Forwarding up callback.
Definition uan-mac-cw.h:109
void NotifyRxStart() override
Called when UanPhy begins receiving packet.
void PhyRxPacketGood(Ptr< Packet > packet, double sinr, UanTxMode mode)
Receive packet from lower layer (passed to PHY as callback).
@ IDLE
Idle state.
Definition uan-mac-cw.h:102
@ RUNNING
Delay timer running.
Definition uan-mac-cw.h:104
@ TX
Transmitting.
Definition uan-mac-cw.h:105
@ CCABUSY
Channel busy.
Definition uan-mac-cw.h:103
TracedCallback< Ptr< const Packet >, uint16_t > m_enqueueLogger
A packet arrived at the MAC for transmission.
Definition uan-mac-cw.h:115
void NotifyTxEnd() override
Function called when Phy object finishes transmitting packet.
Time m_savedDelayS
Remaining delay until next send.
Definition uan-mac-cw.h:127
virtual Time GetSlotTime()
Get the slot time duration.
void StartTimer()
Schedule SendPacket after delay.
Ptr< UanPhy > m_phy
PHY layer attached to this MAC.
Definition uan-mac-cw.h:111
uint32_t m_cw
Contention window size.
Definition uan-mac-cw.h:120
void AttachPhy(Ptr< UanPhy > phy) override
Attach PHY layer to this MAC.
void SetForwardUpCb(Callback< void, Ptr< Packet >, uint16_t, const Mac8Address & > cb) override
Set the callback to forward packets up to higher layers.
uint16_t m_pktTxProt
Next packet protocol number (usage varies by MAC).
Definition uan-mac-cw.h:131
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
Virtual base class for all UAN MAC protocols.
Definition uan-mac.h:35
uint32_t GetTxModeIndex() const
Get the Tx mode index (Modulation type).
Definition uan-mac.cc:35
virtual Address GetAddress()
Get the MAC Address.
Definition uan-mac.cc:41
Abstraction of packet modulation information.
Definition uan-tx-mode.h:32
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:294
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416
@ IDLE
Channel is IDLE, no packet is being transmitted.