A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("LrWpanCsmaCa");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (LrWpanCsmaCa);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::LrWpanCsmaCa")
39  .SetParent<Object> ()
40  .AddConstructor<LrWpanCsmaCa> ()
41  ;
42  return tid;
43 }
44 
46 {
47  // TODO-- make these into ns-3 attributes
48 
49  m_isSlotted = false;
50  m_NB = 0;
51  m_CW = 2;
52  m_BLE = false;
53  m_macMinBE = 3;
54  m_macMaxBE = 5;
56  m_aUnitBackoffPeriod = 20; //20 symbols
57  m_random = CreateObject<UniformRandomVariable> ();
58  m_BE = m_macMinBE;
59  m_ccaRequestRunning = false;
60 }
61 
63 {
64  m_mac = 0;
65 }
66 
67 void
69 {
70  m_lrWpanMacStateCallback = MakeNullCallback< void, LrWpanMacState> ();
71  Cancel ();
72  m_mac = 0;
73 }
74 
75 void
77 {
78  m_mac = mac;
79 }
80 
83 {
84  return m_mac;
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION (this);
91  m_isSlotted = true;
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION (this);
98  m_isSlotted = false;
99 }
100 
101 bool
103 {
104  NS_LOG_FUNCTION (this);
105  return (m_isSlotted);
106 }
107 
108 bool
110 {
111  NS_LOG_FUNCTION (this);
112  return (!m_isSlotted);
113 }
114 
115 void
116 LrWpanCsmaCa::SetMacMinBE (uint8_t macMinBE)
117 {
118  NS_LOG_FUNCTION (this << macMinBE);
119  m_macMinBE = macMinBE;
120 }
121 
122 uint8_t
124 {
125  NS_LOG_FUNCTION (this);
126  return m_macMinBE;
127 }
128 
129 void
130 LrWpanCsmaCa::SetMacMaxBE (uint8_t macMaxBE)
131 {
132  NS_LOG_FUNCTION (this << macMaxBE);
133  m_macMinBE = macMaxBE;
134 }
135 
136 uint8_t
138 {
139  NS_LOG_FUNCTION (this);
140  return m_macMaxBE;
141 }
142 
143 void
144 LrWpanCsmaCa::SetMacMaxCSMABackoffs (uint8_t macMaxCSMABackoffs)
145 {
146  NS_LOG_FUNCTION (this << macMaxCSMABackoffs);
147  m_macMaxCSMABackoffs = macMaxCSMABackoffs;
148 }
149 
150 uint8_t
152 {
153  NS_LOG_FUNCTION (this);
154  return m_macMaxCSMABackoffs;
155 }
156 
157 void
158 LrWpanCsmaCa::SetUnitBackoffPeriod (uint64_t unitBackoffPeriod)
159 {
160  NS_LOG_FUNCTION (this << unitBackoffPeriod);
161  m_aUnitBackoffPeriod = unitBackoffPeriod;
162 }
163 
164 uint64_t
166 {
167  NS_LOG_FUNCTION (this);
168  return m_aUnitBackoffPeriod;
169 }
170 
171 Time
173 {
174  NS_LOG_FUNCTION (this);
175 
176  // TODO: Calculate the offset to the next slot.
177 
178  return Seconds (0);
179 
180 }
181 void
183 
184 {
185  NS_LOG_FUNCTION (this);
186  m_NB = 0;
187  if (IsSlottedCsmaCa ())
188  {
189  m_CW = 2;
190  if (m_BLE)
191  {
192  m_BE = std::min (static_cast<uint8_t> (2), m_macMinBE);
193  }
194  else
195  {
196  m_BE = m_macMinBE;
197  }
198  //TODO: for slotted, locate backoff period boundary. i.e. delay to the next slot boundary
199  Time backoffBoundary = GetTimeToNextSlot ();
201  }
202  else
203  {
204  m_BE = m_macMinBE;
206  }
207  /*
208  * TODO: If using Backoff.cc (will need to modify Backoff::GetBackoffTime)
209  * Backoff.m_minSlots = 0;
210  * Backoff.m_ceiling = m_BE;
211  * Backoff.ResetBackoffTime(); //m_NB is same as m_numBackoffRetries in Backoff.h
212  * Backoff.m_maxRetries = macMaxCSMABackoffs;
213  * Backoff.m_slotTime = m_backoffPeriod;
214  */
215 }
216 
217 void
219 {
223 }
224 
225 /*
226  * Delay for backoff period in the range 0 to 2^BE -1 units
227  * TODO: If using Backoff.cc (Backoff::GetBackoffTime) will need to be slightly modified
228  */
229 void
231 {
232  NS_LOG_FUNCTION (this);
233 
234  uint64_t upperBound = (uint64_t) pow (2, m_BE) - 1;
235  uint64_t backoffPeriod;
236  Time randomBackoff;
237  uint64_t symbolRate;
238  bool isData = false;
239 
240 
241  symbolRate = (uint64_t) m_mac->GetPhy ()->GetDataOrSymbolRate (isData); //symbols per second
242  backoffPeriod = (uint64_t)m_random->GetValue (0, upperBound); //num backoff periods
243  randomBackoff = MicroSeconds (backoffPeriod * GetUnitBackoffPeriod () * 1000 * 1000 / symbolRate);
244 
245  if (IsUnSlottedCsmaCa ())
246  {
247  NS_LOG_LOGIC ("Unslotted: requesting CCA after backoff of " << randomBackoff.GetMicroSeconds () << " us");
249  }
250  else
251  {
252  NS_LOG_LOGIC ("Slotted: proceeding after backoff of " << randomBackoff.GetMicroSeconds () << " us");
254  }
255 }
256 
257 // TODO : Determine if transmission can be completed before end of CAP for the slotted csmaca
258 // If not delay to the next CAP
259 void
261 {
262  NS_LOG_FUNCTION (this);
263 
264  bool canProceed = true;
265 
266  if (m_BLE)
267  {
268  }
269  else
270  {
271  }
272 
273  if (canProceed)
274  {
275  // TODO: For slotted, Perform CCA on backoff period boundary i.e. delay to next slot boundary
276  Time backoffBoundary = GetTimeToNextSlot ();
278  }
279  else
280  {
281  Time nextCap = Seconds (0);
283  }
284 }
285 
286 void
288 {
289  NS_LOG_FUNCTION (this);
290  m_ccaRequestRunning = true;
291  m_mac->GetPhy ()->PlmeCcaRequest ();
292 }
293 
294 /*
295  * This function is called when the phy calls back after completing a PlmeCcaRequest
296  */
297 void
299 {
300  NS_LOG_FUNCTION (this << status);
301 
302  // Only react on this event, if we are actually waiting for a CCA.
303  // If the CSMA algorithm was canceled, we could still receive this event from
304  // the PHY. In this case we ignore the event.
306  {
307  m_ccaRequestRunning = false;
308  if (status == IEEE_802_15_4_PHY_IDLE)
309  {
310  if (IsSlottedCsmaCa ())
311  {
312  m_CW--;
313  if (m_CW == 0)
314  {
315  // inform MAC channel is idle
317  {
318  NS_LOG_LOGIC ("Notifying MAC of idle channel");
320  }
321  }
322  else
323  {
324  NS_LOG_LOGIC ("Perform CCA again, m_CW = " << m_CW);
325  m_requestCcaEvent = Simulator::ScheduleNow (&LrWpanCsmaCa::RequestCCA, this); // Perform CCA again
326  }
327  }
328  else
329  {
330  // inform MAC, channel is idle
332  {
333  NS_LOG_LOGIC ("Notifying MAC of idle channel");
335  }
336  }
337  }
338  else
339  {
340  if (IsSlottedCsmaCa ())
341  {
342  m_CW = 2;
343  }
344  m_BE = std::min (static_cast<uint16_t> (m_BE + 1), static_cast<uint16_t> (m_macMaxBE));
345  m_NB++;
347  {
348  // no channel found so cannot send pkt
349  NS_LOG_DEBUG ("Channel access failure");
351  {
352  NS_LOG_LOGIC ("Notifying MAC of Channel access failure");
354  }
355  return;
356  }
357  else
358  {
359  NS_LOG_DEBUG ("Perform another backoff; m_NB = " << static_cast<uint16_t> (m_NB));
360  m_randomBackoffEvent = Simulator::ScheduleNow (&LrWpanCsmaCa::RandomBackoffDelay, this); //Perform another backoff (step 2)
361  }
362  }
363  }
364 }
365 
366 void
368 {
369  NS_LOG_FUNCTION (this);
371 }
372 
373 int64_t
375 {
376  NS_LOG_FUNCTION (this);
377  m_random->SetStream (stream);
378  return 1;
379 }
380 
381 uint8_t
383 {
384  return m_NB;
385 }
386 
387 } //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:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#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 this RNG stream.
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 the class in the ns-3 factory.
Definition: object-base.h:38
void SetMacMinBE(uint8_t macMinBE)
Set the minimum backoff exponent value.
uint8_t GetMacMaxBE(void) const
Get the maximum backoff exponent value.
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:1018
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint8_t m_NB
Number of backoffs for the current transmission.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
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.
int64_t GetMicroSeconds(void) const
Definition: nstime.h:289
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
bool m_ccaRequestRunning
Flag indicating that the PHY is currently running a CCA.
void SetLrWpanMacStateCallback(LrWpanMacStateCallback macState)
Set the callback function to the MAC.
Ptr< UniformRandomVariable > m_random
Uniform random variable stream.
double GetValue(double min, double max)
Returns a random double from the uniform distribution with 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:986
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.
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:213
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...
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:47
a base class which provides memory management and object aggregation
Definition: object.h:64
EventId m_requestCcaEvent
Scheduler event when to start the CCA after a random backoff.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:93
uint8_t m_macMaxCSMABackoffs
Maximum number of backoffs.