A Discrete-Event Network Simulator
API
lr-wpan-csmaca.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 The Boeing Company
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author:
19  * kwong yin <kwong-sang.yin@boeing.com>
20  * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
21  */
22 
23 #include "lr-wpan-csmaca.h"
24 #include <ns3/random-variable-stream.h>
25 #include <ns3/simulator.h>
26 #include <ns3/log.h>
27 #include <algorithm>
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("LrWpanCsmaCa");
32 
33 NS_OBJECT_ENSURE_REGISTERED (LrWpanCsmaCa);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::LrWpanCsmaCa")
39  .SetParent<Object> ()
40  .SetGroupName ("LrWpan")
41  .AddConstructor<LrWpanCsmaCa> ()
42  ;
43  return tid;
44 }
45 
47 {
48  // TODO-- make these into ns-3 attributes
49 
50  m_isSlotted = false;
51  m_NB = 0;
52  m_CW = 2;
53  m_BLE = false;
54  m_macMinBE = 3;
55  m_macMaxBE = 5;
57  m_aUnitBackoffPeriod = 20; //20 symbols
58  m_random = CreateObject<UniformRandomVariable> ();
59  m_BE = m_macMinBE;
60  m_ccaRequestRunning = false;
61 }
62 
64 {
65  m_mac = 0;
66 }
67 
68 void
70 {
71  m_lrWpanMacStateCallback = MakeNullCallback< void, LrWpanMacState> ();
72  Cancel ();
73  m_mac = 0;
74 }
75 
76 void
78 {
79  m_mac = mac;
80 }
81 
84 {
85  return m_mac;
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this);
92  m_isSlotted = true;
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION (this);
99  m_isSlotted = false;
100 }
101 
102 bool
104 {
105  NS_LOG_FUNCTION (this);
106  return (m_isSlotted);
107 }
108 
109 bool
111 {
112  NS_LOG_FUNCTION (this);
113  return (!m_isSlotted);
114 }
115 
116 void
117 LrWpanCsmaCa::SetMacMinBE (uint8_t macMinBE)
118 {
119  NS_LOG_FUNCTION (this << macMinBE);
120  m_macMinBE = macMinBE;
121 }
122 
123 uint8_t
125 {
126  NS_LOG_FUNCTION (this);
127  return m_macMinBE;
128 }
129 
130 void
131 LrWpanCsmaCa::SetMacMaxBE (uint8_t macMaxBE)
132 {
133  NS_LOG_FUNCTION (this << macMaxBE);
134  m_macMinBE = macMaxBE;
135 }
136 
137 uint8_t
139 {
140  NS_LOG_FUNCTION (this);
141  return m_macMaxBE;
142 }
143 
144 void
145 LrWpanCsmaCa::SetMacMaxCSMABackoffs (uint8_t macMaxCSMABackoffs)
146 {
147  NS_LOG_FUNCTION (this << macMaxCSMABackoffs);
148  m_macMaxCSMABackoffs = macMaxCSMABackoffs;
149 }
150 
151 uint8_t
153 {
154  NS_LOG_FUNCTION (this);
155  return m_macMaxCSMABackoffs;
156 }
157 
158 void
159 LrWpanCsmaCa::SetUnitBackoffPeriod (uint64_t unitBackoffPeriod)
160 {
161  NS_LOG_FUNCTION (this << unitBackoffPeriod);
162  m_aUnitBackoffPeriod = unitBackoffPeriod;
163 }
164 
165 uint64_t
167 {
168  NS_LOG_FUNCTION (this);
169  return m_aUnitBackoffPeriod;
170 }
171 
172 Time
174 {
175  NS_LOG_FUNCTION (this);
176 
177  // TODO: Calculate the offset to the next slot.
178 
179  return Seconds (0);
180 
181 }
182 void
184 
185 {
186  NS_LOG_FUNCTION (this);
187  m_NB = 0;
188  if (IsSlottedCsmaCa ())
189  {
190  m_CW = 2;
191  if (m_BLE)
192  {
193  m_BE = std::min (static_cast<uint8_t> (2), m_macMinBE);
194  }
195  else
196  {
197  m_BE = m_macMinBE;
198  }
199  //TODO: for slotted, locate backoff period boundary. i.e. delay to the next slot boundary
200  Time backoffBoundary = GetTimeToNextSlot ();
202  }
203  else
204  {
205  m_BE = m_macMinBE;
207  }
208  /*
209  * TODO: If using Backoff.cc (will need to modify Backoff::GetBackoffTime)
210  * Backoff.m_minSlots = 0;
211  * Backoff.m_ceiling = m_BE;
212  * Backoff.ResetBackoffTime(); //m_NB is same as m_numBackoffRetries in Backoff.h
213  * Backoff.m_maxRetries = macMaxCSMABackoffs;
214  * Backoff.m_slotTime = m_backoffPeriod;
215  */
216 }
217 
218 void
220 {
224 }
225 
226 /*
227  * Delay for backoff period in the range 0 to 2^BE -1 units
228  * TODO: If using Backoff.cc (Backoff::GetBackoffTime) will need to be slightly modified
229  */
230 void
232 {
233  NS_LOG_FUNCTION (this);
234 
235  uint64_t upperBound = (uint64_t) pow (2, m_BE) - 1;
236  uint64_t backoffPeriod;
237  Time randomBackoff;
238  uint64_t symbolRate;
239  bool isData = false;
240 
241 
242  symbolRate = (uint64_t) m_mac->GetPhy ()->GetDataOrSymbolRate (isData); //symbols per second
243  backoffPeriod = (uint64_t)m_random->GetValue (0, upperBound+1); // num backoff periods
244  randomBackoff = MicroSeconds (backoffPeriod * GetUnitBackoffPeriod () * 1000 * 1000 / symbolRate);
245 
246  if (IsUnSlottedCsmaCa ())
247  {
248  NS_LOG_LOGIC ("Unslotted: requesting CCA after backoff of " << randomBackoff.GetMicroSeconds () << " us");
250  }
251  else
252  {
253  NS_LOG_LOGIC ("Slotted: proceeding after backoff of " << randomBackoff.GetMicroSeconds () << " us");
255  }
256 }
257 
258 // TODO : Determine if transmission can be completed before end of CAP for the slotted csmaca
259 // If not delay to the next CAP
260 void
262 {
263  NS_LOG_FUNCTION (this);
264 
265  bool canProceed = true;
266 
267  if (m_BLE)
268  {
269  }
270  else
271  {
272  }
273 
274  if (canProceed)
275  {
276  // TODO: For slotted, Perform CCA on backoff period boundary i.e. delay to next slot boundary
277  Time backoffBoundary = GetTimeToNextSlot ();
279  }
280  else
281  {
282  Time nextCap = Seconds (0);
284  }
285 }
286 
287 void
289 {
290  NS_LOG_FUNCTION (this);
291  m_ccaRequestRunning = true;
292  m_mac->GetPhy ()->PlmeCcaRequest ();
293 }
294 
295 /*
296  * This function is called when the phy calls back after completing a PlmeCcaRequest
297  */
298 void
300 {
301  NS_LOG_FUNCTION (this << status);
302 
303  // Only react on this event, if we are actually waiting for a CCA.
304  // If the CSMA algorithm was canceled, we could still receive this event from
305  // the PHY. In this case we ignore the event.
307  {
308  m_ccaRequestRunning = false;
309  if (status == IEEE_802_15_4_PHY_IDLE)
310  {
311  if (IsSlottedCsmaCa ())
312  {
313  m_CW--;
314  if (m_CW == 0)
315  {
316  // inform MAC channel is idle
318  {
319  NS_LOG_LOGIC ("Notifying MAC of idle channel");
321  }
322  }
323  else
324  {
325  NS_LOG_LOGIC ("Perform CCA again, m_CW = " << m_CW);
326  m_requestCcaEvent = Simulator::ScheduleNow (&LrWpanCsmaCa::RequestCCA, this); // Perform CCA again
327  }
328  }
329  else
330  {
331  // inform MAC, channel is idle
333  {
334  NS_LOG_LOGIC ("Notifying MAC of idle channel");
336  }
337  }
338  }
339  else
340  {
341  if (IsSlottedCsmaCa ())
342  {
343  m_CW = 2;
344  }
345  m_BE = std::min (static_cast<uint16_t> (m_BE + 1), static_cast<uint16_t> (m_macMaxBE));
346  m_NB++;
348  {
349  // no channel found so cannot send pkt
350  NS_LOG_DEBUG ("Channel access failure");
352  {
353  NS_LOG_LOGIC ("Notifying MAC of Channel access failure");
355  }
356  return;
357  }
358  else
359  {
360  NS_LOG_DEBUG ("Perform another backoff; m_NB = " << static_cast<uint16_t> (m_NB));
361  m_randomBackoffEvent = Simulator::ScheduleNow (&LrWpanCsmaCa::RandomBackoffDelay, this); //Perform another backoff (step 2)
362  }
363  }
364  }
365 }
366 
367 void
369 {
370  NS_LOG_FUNCTION (this);
372 }
373 
374 int64_t
376 {
377  NS_LOG_FUNCTION (this);
378  m_random->SetStream (stream);
379  return 1;
380 }
381 
382 uint8_t
384 {
385  return m_NB;
386 }
387 
388 } //namespace ns3
static TypeId GetTypeId(void)
Get the type ID.
Time GetTimeToNextSlot(void) const
Get the amount of time from now to the beginning of the next slot.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
uint64_t GetUnitBackoffPeriod(void) const
Get the number of symbols forming the basic time period used by the CSMA-CA algorithm.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void SetMacMinBE(uint8_t macMinBE)
Set the minimum backoff exponent value.
#define min(a, b)
Definition: 80211b.c:44
uint8_t GetMacMaxBE(void) const
Get the maximum backoff exponent value.
CHANNEL_IDLE.
Definition: lr-wpan-mac.h:74
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
uint8_t m_CW
Contention window length (used in slotted ver only).
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to which this CSMA/CA implementation is attached to.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint8_t m_NB
Number of backoffs for the current transmission.
bool IsUnSlottedCsmaCa(void) const
Check if the unslotted CSMA/CA version is being used.
uint8_t GetNB(void)
Get the number of CSMA retries.
void SetUnitBackoffPeriod(uint64_t unitBackoffPeriod)
Set the number of symbols forming the basic time period used by the CSMA-CA algorithm.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC to which this CSMA/CA implementation is attached to.
uint8_t m_macMinBE
Minimum backoff exponent.
LrWpanMacStateCallback m_lrWpanMacStateCallback
The callback to inform the configured MAC of the CSMA/CA result.
void SetMacMaxBE(uint8_t macMaxBE)
Set the maximum backoff exponent value.
void SetUnSlottedCsmaCa(void)
Configure for the use of the unslotted CSMA/CA version.
void RequestCCA(void)
Request the Phy to perform CCA (Step 3)
bool IsSlottedCsmaCa(void) const
Check if the slotted CSMA/CA version is being used.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:349
virtual void DoDispose(void)
Destructor implementation.
void RandomBackoffDelay(void)
In step 2 of the CSMA-CA, perform a random backoff in the range of 0 to 2^BE -1.
void Start(void)
Start CSMA-CA algorithm (step 1), initialize NB, BE for both slotted and unslotted CSMA-CA...
uint8_t GetMacMinBE(void) const
Get the minimum backoff exponent value.
void Cancel(void)
Cancel CSMA-CA algorithm.
uint64_t m_aUnitBackoffPeriod
Number of symbols per CSMA/CA time unit, default 20 symbols.
LrWpanCsmaCa(void)
Default constructor.
bool m_BLE
Battery Life Extension.
Ptr< LrWpanMac > m_mac
The MAC instance for which this CSMAƄ/CA implemenation is configured.
tuple mac
Definition: third.py:92
bool m_ccaRequestRunning
Flag indicating that the PHY is currently running a CCA.
void SetLrWpanMacStateCallback(LrWpanMacStateCallback macState)
Set the callback function to the MAC.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< UniformRandomVariable > m_random
Uniform random variable stream.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
bool m_isSlotted
Beacon-enabled slotted or nonbeacon-enabled unslotted CSMA-CA.
uint8_t m_macMaxBE
Maximum backoff exponent.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1564
EventId m_randomBackoffEvent
Scheduler event for the start of the next random backoff/slot.
uint8_t GetMacMaxCSMABackoffs(void) const
Get the maximum number of backoffs.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
CHANNEL_ACCESS_FAILURE.
Definition: lr-wpan-mac.h:73
void SetSlottedCsmaCa(void)
Configure for the use of the slotted CSMA/CA version.
void CanProceed(void)
In the slotted CSMA-CA, after random backoff, determine if the remaining CSMA-CA operation can procee...
virtual ~LrWpanCsmaCa(void)
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
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...
This class is a helper for the LrWpanMac to manage the Csma/CA state machine according to IEEE 802...
void SetMacMaxCSMABackoffs(uint8_t macMaxCSMABackoffs)
Set the maximum number of backoffs.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1009
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:105
A base class which provides memory management and object aggregation.
Definition: object.h:87
EventId m_requestCcaEvent
Scheduler event when to start the CCA after a random backoff.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
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.