A Discrete-Event Network Simulator
API
lr-wpan-mac.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  * Authors:
19  * Gary Pei <guangyu.pei@boeing.com>
20  * kwong yin <kwong-sang.yin@boeing.com>
21  * Tom Henderson <thomas.r.henderson@boeing.com>
22  * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
23  * Erwan Livolant <erwan.livolant@inria.fr>
24  * Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
25  */
26 #include "lr-wpan-mac.h"
27 #include "lr-wpan-csmaca.h"
28 #include "lr-wpan-mac-header.h"
29 #include "lr-wpan-mac-pl-headers.h"
30 #include "lr-wpan-mac-trailer.h"
31 #include <ns3/simulator.h>
32 #include <ns3/log.h>
33 #include <ns3/uinteger.h>
34 #include <ns3/node.h>
35 #include <ns3/packet.h>
36 #include <ns3/random-variable-stream.h>
37 #include <ns3/double.h>
38 
39 #undef NS_LOG_APPEND_CONTEXT
40 #define NS_LOG_APPEND_CONTEXT \
41  std::clog << "[address " << m_shortAddress << "] ";
42 
43 namespace ns3 {
44 
45 NS_LOG_COMPONENT_DEFINE ("LrWpanMac");
46 
47 NS_OBJECT_ENSURE_REGISTERED (LrWpanMac);
48 
49 //IEEE 802.15.4-2011 Table 51
50 const uint32_t LrWpanMac::aMinMPDUOverhead = 9;
51 const uint32_t LrWpanMac::aBaseSlotDuration = 60;
52 const uint32_t LrWpanMac::aNumSuperframeSlots = 16;
53 const uint32_t LrWpanMac::aBaseSuperframeDuration = aBaseSlotDuration * aNumSuperframeSlots;
54 const uint32_t LrWpanMac::aMaxLostBeacons = 4;
55 const uint32_t LrWpanMac::aMaxSIFSFrameSize = 18;
56 
57 TypeId
59 {
60  static TypeId tid = TypeId ("ns3::LrWpanMac")
61  .SetParent<Object> ()
62  .SetGroupName ("LrWpan")
63  .AddConstructor<LrWpanMac> ()
64  .AddAttribute ("PanId", "16-bit identifier of the associated PAN",
65  UintegerValue (),
67  MakeUintegerChecker<uint16_t> ())
68  .AddTraceSource ("MacTxEnqueue",
69  "Trace source indicating a packet has been "
70  "enqueued in the transaction queue",
72  "ns3::Packet::TracedCallback")
73  .AddTraceSource ("MacTxDequeue",
74  "Trace source indicating a packet has was "
75  "dequeued from the transaction queue",
77  "ns3::Packet::TracedCallback")
78  .AddTraceSource ("MacTx",
79  "Trace source indicating a packet has "
80  "arrived for transmission by this device",
82  "ns3::Packet::TracedCallback")
83  .AddTraceSource ("MacTxOk",
84  "Trace source indicating a packet has been "
85  "successfully sent",
87  "ns3::Packet::TracedCallback")
88  .AddTraceSource ("MacTxDrop",
89  "Trace source indicating a packet has been "
90  "dropped during transmission",
92  "ns3::Packet::TracedCallback")
93  .AddTraceSource ("MacPromiscRx",
94  "A packet has been received by this device, "
95  "has been passed up from the physical layer "
96  "and is being forwarded up the local protocol stack. "
97  "This is a promiscuous trace,",
99  "ns3::Packet::TracedCallback")
100  .AddTraceSource ("MacRx",
101  "A packet has been received by this device, "
102  "has been passed up from the physical layer "
103  "and is being forwarded up the local protocol stack. "
104  "This is a non-promiscuous trace,",
106  "ns3::Packet::TracedCallback")
107  .AddTraceSource ("MacRxDrop",
108  "Trace source indicating a packet was received, "
109  "but dropped before being forwarded up the stack",
111  "ns3::Packet::TracedCallback")
112  .AddTraceSource ("Sniffer",
113  "Trace source simulating a non-promiscuous "
114  "packet sniffer attached to the device",
116  "ns3::Packet::TracedCallback")
117  .AddTraceSource ("PromiscSniffer",
118  "Trace source simulating a promiscuous "
119  "packet sniffer attached to the device",
121  "ns3::Packet::TracedCallback")
122  .AddTraceSource ("MacStateValue",
123  "The state of LrWpan Mac",
125  "ns3::TracedValueCallback::LrWpanMacState")
126  .AddTraceSource ("MacIncSuperframeStatus",
127  "The period status of the incoming superframe",
129  "ns3::TracedValueCallback::SuperframeState")
130  .AddTraceSource ("MacOutSuperframeStatus",
131  "The period status of the outgoing superframe",
133  "ns3::TracedValueCallback::SuperframeState")
134  .AddTraceSource ("MacState",
135  "The state of LrWpan Mac",
137  "ns3::LrWpanMac::StateTracedCallback")
138  .AddTraceSource ("MacSentPkt",
139  "Trace source reporting some information about "
140  "the sent packet",
142  "ns3::LrWpanMac::SentTracedCallback")
143  ;
144  return tid;
145 }
146 
148 {
149 
150  // First set the state to a known value, call ChangeMacState to fire trace source.
152 
154 
157 
158  m_macRxOnWhenIdle = true;
159  m_macPanId = 0xffff;
163  m_macPromiscuousMode = false;
165  m_retransmission = 0;
166  m_numCsmacaRetry = 0;
167  m_txPkt = 0;
168  m_ifs = 0;
169 
170  m_macLIFSPeriod = 40;
171  m_macSIFSPeriod = 12;
172 
173  m_macBeaconOrder = 15;
175  m_macTransactionPersistanceTime = 500; //0x01F5
176  m_macAutoRequest = true;
177 
180  m_beaconTrackingOn = false;
181  m_numLostBeacons = 0;
182 
183 
184  Ptr<UniformRandomVariable> uniformVar = CreateObject<UniformRandomVariable> ();
185  uniformVar->SetAttribute ("Min", DoubleValue (0.0));
186  uniformVar->SetAttribute ("Max", DoubleValue (255.0));
187  m_macDsn = SequenceNumber8 (uniformVar->GetValue ());
188  m_macBsn = SequenceNumber8 (uniformVar->GetValue ());
189  m_shortAddress = Mac16Address ("00:00");
190 }
191 
193 {}
194 
195 void
197 {
198  if (m_macRxOnWhenIdle)
199  {
200  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
201  }
202  else
203  {
204  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
205  }
206 
208 }
209 
210 void
212 {
213  if (m_csmaCa != 0)
214  {
215  m_csmaCa->Dispose ();
216  m_csmaCa = 0;
217  }
218  m_txPkt = 0;
219  for (uint32_t i = 0; i < m_txQueue.size (); i++)
220  {
221  m_txQueue[i]->txQPkt = 0;
222  delete m_txQueue[i];
223  }
224  m_txQueue.clear ();
225  m_phy = 0;
226  m_mcpsDataIndicationCallback = MakeNullCallback< void, McpsDataIndicationParams, Ptr<Packet> > ();
227  m_mcpsDataConfirmCallback = MakeNullCallback< void, McpsDataConfirmParams > ();
228 
230 
232 }
233 
234 bool
236 {
237  return m_macRxOnWhenIdle;
238 }
239 
240 void
241 LrWpanMac::SetRxOnWhenIdle (bool rxOnWhenIdle)
242 {
243  NS_LOG_FUNCTION (this << rxOnWhenIdle);
244  m_macRxOnWhenIdle = rxOnWhenIdle;
245 
246  if (m_lrWpanMacState == MAC_IDLE)
247  {
248  if (m_macRxOnWhenIdle)
249  {
250  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
251  }
252  else
253  {
254  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
255  }
256  }
257 }
258 
259 void
261 {
262  //NS_LOG_FUNCTION (this << address);
264 }
265 
266 void
268 {
269  //NS_LOG_FUNCTION (this << address);
270  m_selfExt = address;
271 }
272 
273 
276 {
277  NS_LOG_FUNCTION (this);
278  return m_shortAddress;
279 }
280 
283 {
284  NS_LOG_FUNCTION (this);
285  return m_selfExt;
286 }
287 void
289 {
290  NS_LOG_FUNCTION (this << p);
291 
292  McpsDataConfirmParams confirmParams;
293  confirmParams.m_msduHandle = params.m_msduHandle;
294 
295  // TODO: We need a drop trace for the case that the packet is too large or the request parameters are maleformed.
296  // The current tx drop trace is not suitable, because packets dropped using this trace carry the mac header
297  // and footer, while packets being dropped here do not have them.
298 
300  m_macDsn++;
301 
303  {
304  // Note, this is just testing maximum theoretical frame size per the spec
305  // The frame could still be too large once headers are put on
306  // in which case the phy will reject it instead
307  NS_LOG_ERROR (this << " packet too big: " << p->GetSize ());
308  confirmParams.m_status = IEEE_802_15_4_FRAME_TOO_LONG;
310  {
311  m_mcpsDataConfirmCallback (confirmParams);
312  }
313  return;
314  }
315 
316  if ((params.m_srcAddrMode == NO_PANID_ADDR)
317  && (params.m_dstAddrMode == NO_PANID_ADDR))
318  {
319  NS_LOG_ERROR (this << " Can not send packet with no Address field" );
320  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
322  {
323  m_mcpsDataConfirmCallback (confirmParams);
324  }
325  return;
326  }
327  switch (params.m_srcAddrMode)
328  {
329  case NO_PANID_ADDR:
330  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
331  macHdr.SetNoPanIdComp ();
332  break;
333  case ADDR_MODE_RESERVED:
334  NS_ABORT_MSG ("Can not set source address type to ADDR_MODE_RESERVED. Aborting.");
335  break;
336  case SHORT_ADDR:
337  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
338  macHdr.SetSrcAddrFields (GetPanId (), GetShortAddress ());
339  break;
340  case EXT_ADDR:
341  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
342  macHdr.SetSrcAddrFields (GetPanId (), GetExtendedAddress ());
343  break;
344  default:
345  NS_LOG_ERROR (this << " Can not send packet with incorrect Source Address mode = " << params.m_srcAddrMode);
346  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
348  {
349  m_mcpsDataConfirmCallback (confirmParams);
350  }
351  return;
352  }
353  switch (params.m_dstAddrMode)
354  {
355  case NO_PANID_ADDR:
356  macHdr.SetDstAddrMode (params.m_dstAddrMode);
357  macHdr.SetNoPanIdComp ();
358  break;
359  case ADDR_MODE_RESERVED:
360  NS_ABORT_MSG ("Can not set destination address type to ADDR_MODE_RESERVED. Aborting.");
361  break;
362  case SHORT_ADDR:
363  macHdr.SetDstAddrMode (params.m_dstAddrMode);
364  macHdr.SetDstAddrFields (params.m_dstPanId, params.m_dstAddr);
365  break;
366  case EXT_ADDR:
367  macHdr.SetDstAddrMode (params.m_dstAddrMode);
368  macHdr.SetDstAddrFields (params.m_dstPanId, params.m_dstExtAddr);
369  break;
370  default:
371  NS_LOG_ERROR (this << " Can not send packet with incorrect Destination Address mode = " << params.m_dstAddrMode);
372  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
374  {
375  m_mcpsDataConfirmCallback (confirmParams);
376  }
377  return;
378  }
379 
380  macHdr.SetSecDisable ();
381  //extract the first 3 bits in TxOptions
382  int b0 = params.m_txOptions & TX_OPTION_ACK;
383  int b1 = params.m_txOptions & TX_OPTION_GTS;
384  int b2 = params.m_txOptions & TX_OPTION_INDIRECT;
385 
386  if (b0 == TX_OPTION_ACK)
387  {
388  // Set AckReq bit only if the destination is not the broadcast address.
389  if (!(macHdr.GetDstAddrMode () == SHORT_ADDR && macHdr.GetShortDstAddr () == "ff:ff"))
390  {
391  macHdr.SetAckReq ();
392  }
393  }
394  else
395  {
396  macHdr.SetNoAckReq ();
397  }
398 
399 
400  if (b1 == TX_OPTION_GTS)
401  {
402  //TODO:GTS Transmission
403  }
404  else if (b2 == TX_OPTION_INDIRECT)
405  {
406  // Indirect Tx
407  // A COORDINATOR will save the packet in the pending queue and await for data
408  // requests from its associated devices. The devices are aware of pending data,
409  // from the pending bit information extracted from the received beacon.
410  // A DEVICE must be tracking beacons (MLME-SYNC.request is running) before attempting
411  // request data from the coordinator.
412 
413 
414  //TODO: Check if the current device is coordinator or PAN coordinator
415  p->AddHeader (macHdr);
416 
417  LrWpanMacTrailer macTrailer;
418  // Calculate FCS if the global attribute ChecksumEnable is set.
419  if (Node::ChecksumEnabled ())
420  {
421  macTrailer.EnableFcs (true);
422  macTrailer.SetFcs (p);
423  }
424  p->AddTrailer (macTrailer);
425 
426  if (m_txQueue.size () == m_txQueue.max_size ())
427  {
430  {
431  m_mcpsDataConfirmCallback (confirmParams);
432  }
433  }
434  else
435  {
436  IndTxQueueElement *indTxQElement = new IndTxQueueElement;
437  uint64_t unitPeriodSymbols;
438  Time expireTime;
439 
440  if (m_macBeaconOrder == 15)
441  {
442  unitPeriodSymbols = aBaseSuperframeDuration;
443  }
444  else
445  {
446  unitPeriodSymbols = ((uint64_t) 1 << m_macBeaconOrder) * aBaseSuperframeDuration;
447  }
448 
449  //TODO: check possible incorrect expire time here.
450 
452  * MicroSeconds (unitPeriodSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
453  indTxQElement->expireTime = expireTime;
454  indTxQElement->txQMsduHandle = params.m_msduHandle;
455  indTxQElement->txQPkt = p;
456 
457  m_indTxQueue.push_back (indTxQElement);
458 
459  std::cout << "Indirect Transmission Pushed | Elements in the queue: " << m_indTxQueue.size ()
460  << " " << "Element to expire in: " << expireTime.GetSeconds () << "secs\n";
461  }
462  }
463  else
464  {
465  // Direct Tx
466  // From this point the packet will be pushed to a Tx queue and immediately
467  // use a slotted (beacon-enabled) or unslotted (nonbeacon-enabled) version of CSMA/CA
468  // before sending the packet, depending on whether it has previously
469  // received a valid beacon or not.
470 
471  p->AddHeader (macHdr);
472 
473  LrWpanMacTrailer macTrailer;
474  // Calculate FCS if the global attribute ChecksumEnable is set.
475  if (Node::ChecksumEnabled ())
476  {
477  macTrailer.EnableFcs (true);
478  macTrailer.SetFcs (p);
479  }
480  p->AddTrailer (macTrailer);
481 
483 
484  TxQueueElement *txQElement = new TxQueueElement;
485  txQElement->txQMsduHandle = params.m_msduHandle;
486  txQElement->txQPkt = p;
487  m_txQueue.push_back (txQElement);
488  CheckQueue ();
489  }
490 
491 }
492 
493 
494 void
496 {
497  NS_LOG_FUNCTION (this);
499 
500  MlmeStartConfirmParams confirmParams;
501 
502 
503  if (GetShortAddress () == Mac16Address ("ff:ff"))
504  {
505  NS_LOG_ERROR (this << " Invalid MAC short address" );
506  confirmParams.m_status = MLMESTART_NO_SHORT_ADDRESS;
508  {
509  m_mlmeStartConfirmCallback (confirmParams);
510  }
511  return;
512  }
513 
514 
515  if ( (params.m_logCh > 26)
516  || (params.m_bcnOrd > 15)
517  || (params.m_sfrmOrd > params.m_bcnOrd))
518  {
519  NS_LOG_ERROR (this << " One or more parameters are invalid" );
520  confirmParams.m_status = MLMESTART_INVALID_PARAMETER;
522  {
523  m_mlmeStartConfirmCallback (confirmParams);
524  }
525  return;
526  }
527 
528 
529  if (params.m_coorRealgn) //Coordinator Realignment
530  {
531  // TODO:: Send realignment request command frame in CSMA/CA
532  return;
533  }
534  else
535  {
536  if (params.m_panCoor)
537  {
538  m_panCoor = true;
539  m_macPanId = params.m_PanId;
540 
541  // Setting Channel and Page in the LrWpanPhy
542  LrWpanPhyPibAttributes pibAttr;
543  pibAttr.phyCurrentChannel = params.m_logCh;
544  pibAttr.phyCurrentPage = params.m_logChPage;
545  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentChannel,&pibAttr);
546  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentPage,&pibAttr);
547  }
548 
549  NS_ASSERT (params.m_PanId != 0xffff);
550 
551 
552  m_macBeaconOrder = params.m_bcnOrd;
553  if (m_macBeaconOrder == 15)
554  {
555  //Non-beacon enabled PAN
557  m_beaconInterval = 0;
558  m_csmaCa->SetUnSlottedCsmaCa ();
559 
560  confirmParams.m_status = MLMESTART_SUCCESS;
562  {
563  m_mlmeStartConfirmCallback (confirmParams);
564  }
565  }
566  else
567  {
569  m_csmaCa->SetBatteryLifeExtension (params.m_battLifeExt);
570 
571  m_csmaCa->SetSlottedCsmaCa ();
572 
573  //TODO: Calculate the real Final CAP slot (requires GTS implementation)
574  // FinalCapSlot = Superframe duration slots - CFP slots.
575  // In the current implementation the value of the final cap slot is equal to
576  // the total number of possible slots in the superframe (15).
577  m_fnlCapSlot = 15;
578 
581 
582  //TODO: change the beacon sending according to the startTime parameter (if not PAN coordinator)
583 
585  }
586  }
587 }
588 
589 
590 void
592 {
593  NS_LOG_FUNCTION (this);
594  NS_ASSERT (params.m_logCh <= 26 && m_macPanId != 0xffff);
595 
596  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
597  //change phy current logical channel
598  LrWpanPhyPibAttributes pibAttr;
599  pibAttr.phyCurrentChannel = params.m_logCh;
600  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentChannel,&pibAttr);
601 
602  //Enable Phy receiver
603  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
604 
605  uint64_t searchSymbols;
606  Time searchBeaconTime;
607 
608  if (m_trackingEvent.IsRunning ())
609  {
611  }
612 
613  if (params.m_trackBcn)
614  {
615  m_numLostBeacons = 0;
616  //search for a beacon for a time = incomingSuperframe symbols + 960 symbols
617  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
618  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
619  m_beaconTrackingOn = true;
621  }
622  else
623  {
624  m_beaconTrackingOn = false;
625  }
626 
627 }
628 
629 
630 void
632 {
633  NS_LOG_FUNCTION (this);
634 
636  m_macBsn++;
637 
639 
640  Ptr<Packet> beaconPacket = Create <Packet> ();
641 }
642 
643 
644 void
646 {
647  NS_LOG_FUNCTION (this);
649 
651  m_macBsn++;
652  BeaconPayloadHeader macPayload;
653  Ptr<Packet> beaconPacket = Create <Packet> ();
654  LrWpanMacTrailer macTrailer;
655 
656  macHdr.SetDstAddrMode (LrWpanMacHeader::SHORTADDR);
657  macHdr.SetDstAddrFields (GetPanId (),Mac16Address ("ff:ff"));
658 
659  //see IEEE 802.15.4-2011 Section 5.1.2.4
660  if (GetShortAddress () == Mac16Address ("ff:fe"))
661  {
662  macHdr.SetSrcAddrMode (LrWpanMacHeader::EXTADDR);
663  macHdr.SetSrcAddrFields (GetPanId (),GetExtendedAddress ());
664  }
665  else
666  {
667  macHdr.SetSrcAddrMode (LrWpanMacHeader::SHORTADDR);
668  macHdr.SetSrcAddrFields (GetPanId (), GetShortAddress ());
669  }
670 
671  macHdr.SetSecDisable ();
672  macHdr.SetNoAckReq ();
673 
675  macPayload.SetGtsFields (GetGtsFields ());
676  macPayload.SetPndAddrFields (GetPendingAddrFields ());
677 
678  beaconPacket->AddHeader (macPayload);
679  beaconPacket->AddHeader (macHdr);
680 
681  // Calculate FCS if the global attribute ChecksumEnable is set.
682  if (Node::ChecksumEnabled ())
683  {
684  macTrailer.EnableFcs (true);
685  macTrailer.SetFcs (beaconPacket);
686  }
687 
688  beaconPacket->AddTrailer (macTrailer);
689 
690  //Set the Beacon packet to be transmitted
691  m_txPkt = beaconPacket;
692 
694 
695  NS_LOG_DEBUG ("Outgoing superframe Active Portion (Beacon + CAP + CFP): " << m_superframeDuration << " symbols");
696 
698  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
699 }
700 
701 
702 void
704 {
705  uint32_t activeSlot;
706  uint64_t capDuration;
707  Time endCapTime;
708  uint64_t symbolRate;
709 
710  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
711 
712  if (superframeType == OUTGOING)
713  {
715  activeSlot = m_superframeDuration / 16;
716  capDuration = activeSlot * (m_fnlCapSlot + 1);
717  endCapTime = Seconds ((double) capDuration / symbolRate);
718  // Obtain the end of the CAP by adjust the time it took to send the beacon
719  endCapTime -= (Simulator::Now () - m_macBeaconTxTime);
720 
721  NS_LOG_DEBUG ("Outgoing superframe CAP duration " << (endCapTime.GetSeconds () * symbolRate) << " symbols (" << endCapTime.As (Time::S) << ")");
722  NS_LOG_DEBUG ("Active Slots duration " << activeSlot << " symbols");
723 
724  m_capEvent = Simulator::Schedule (endCapTime,
726 
727  }
728  else
729  {
731  activeSlot = m_incomingSuperframeDuration / 16;
732  capDuration = activeSlot * (m_incomingFnlCapSlot + 1);
733  endCapTime = Seconds ((double) capDuration / symbolRate);
734  // Obtain the end of the CAP by adjust the time it took to receive the beacon
735  endCapTime -= (Simulator::Now () - m_macBeaconRxTime);
736 
737  NS_LOG_DEBUG ("Incoming superframe CAP duration " << (endCapTime.GetSeconds () * symbolRate) << " symbols (" << endCapTime.As (Time::S) << ")");
738  NS_LOG_DEBUG ("Active Slots duration " << activeSlot << " symbols");
739 
740  m_capEvent = Simulator::Schedule (endCapTime,
742  }
743 
744  CheckQueue ();
745 
746 }
747 
748 
749 void
751 {
752  uint32_t activeSlot;
753  uint64_t cfpDuration;
754  Time endCfpTime;
755  uint64_t symbolRate;
756 
757  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
758 
759  if (superframeType == INCOMING)
760  {
761  activeSlot = m_incomingSuperframeDuration / 16;
762  cfpDuration = activeSlot * (15 - m_incomingFnlCapSlot);
763  endCfpTime = Seconds ((double) cfpDuration / symbolRate);
764  if (cfpDuration > 0)
765  {
767  }
768 
769  NS_LOG_DEBUG ("Incoming superframe CFP duration " << cfpDuration << " symbols (" << endCfpTime.As (Time::S) << ")");
770 
771  m_incCfpEvent = Simulator::Schedule (endCfpTime,
773  }
774  else
775  {
776  activeSlot = m_superframeDuration / 16;
777  cfpDuration = activeSlot * (15 - m_fnlCapSlot);
778  endCfpTime = Seconds ((double) cfpDuration / symbolRate);
779 
780  if (cfpDuration > 0)
781  {
783  }
784 
785  NS_LOG_DEBUG ("Outgoing superframe CFP duration " << cfpDuration << " symbols (" << endCfpTime.As (Time::S) << ")");
786 
787  m_cfpEvent = Simulator::Schedule (endCfpTime,
789 
790  }
791  //TODO: Start transmit or receive GTS here.
792 }
793 
794 
795 void
797 {
798  uint64_t inactiveDuration;
799  Time endInactiveTime;
800  uint64_t symbolRate;
801 
802  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
803 
804  if (superframeType == INCOMING)
805  {
807  endInactiveTime = Seconds ((double) inactiveDuration / symbolRate);
808 
809  if (inactiveDuration > 0)
810  {
812  }
813 
814  NS_LOG_DEBUG ("Incoming superframe Inactive Portion duration " << inactiveDuration << " symbols (" << endInactiveTime.As (Time::S) << ")");
815  m_beaconEvent = Simulator::Schedule (endInactiveTime, &LrWpanMac::AwaitBeacon, this);
816  }
817  else
818  {
819  inactiveDuration = m_beaconInterval - m_superframeDuration;
820  endInactiveTime = Seconds ((double) inactiveDuration / symbolRate);
821 
822  if (inactiveDuration > 0)
823  {
825  }
826 
827  NS_LOG_DEBUG ("Outgoing superframe Inactive Portion duration " << inactiveDuration << " symbols (" << endInactiveTime.As (Time::S) << ")");
828  m_beaconEvent = Simulator::Schedule (endInactiveTime, &LrWpanMac::SendOneBeacon, this);
829  }
830 }
831 
832 void
834 {
836 
837  //TODO: If the device waits more than the expected time to receive the beacon (wait = 46 symbols for default beacon size)
838  // it should continue with the start of the incoming CAP even if it did not receive the beacon.
839  // At the moment, the start of the incoming CAP is only triggered if the beacon is received.
840  // See MLME-SyncLoss for details.
841 
842 
843 }
844 
845 void
847 {
848  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
849 
851  {
852  MlmeSyncLossIndicationParams syncLossParams;
853  //syncLossParams.m_logCh =
854  syncLossParams.m_lossReason = MLMESYNCLOSS_BEACON_LOST;
855  syncLossParams.m_panId = m_macPanId;
856  m_mlmeSyncLossIndicationCallback (syncLossParams);
857 
858  m_beaconTrackingOn = false;
859  m_numLostBeacons = 0;
860  }
861  else
862  {
864 
865  //Search for one more beacon
866  uint64_t searchSymbols;
867  Time searchBeaconTime;
868  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
869  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
871 
872  }
873 }
874 
875 
876 void
878 {
879  NS_LOG_FUNCTION (this);
880  // Pull a packet from the queue and start sending if we are not already sending.
881  if (m_lrWpanMacState == MAC_IDLE && !m_txQueue.empty () && !m_setMacState.IsRunning ())
882  {
883  //TODO: this should check if the node is a coordinator and using the outcoming superframe not just the PAN coordinator
884  if (m_csmaCa->IsUnSlottedCsmaCa () || (m_outSuperframeStatus == CAP && m_panCoor) || m_incSuperframeStatus == CAP)
885  {
886  TxQueueElement *txQElement = m_txQueue.front ();
887  m_txPkt = txQElement->txQPkt;
889  }
890  }
891 }
892 
893 
896 {
897  SuperframeField sfrmSpec;
898 
899  sfrmSpec.SetBeaconOrder (m_macBeaconOrder);
901  sfrmSpec.SetFinalCapSlot (m_fnlCapSlot);
902 
903  if (m_csmaCa->GetBatteryLifeExtension ())
904  {
905  sfrmSpec.SetBattLifeExt (true);
906  }
907 
908  if (m_panCoor)
909  {
910  sfrmSpec.SetPanCoor (true);
911  }
912  //TODO: It is possible to do association using Beacons,
913  // however, the current implementation only support manual association.
914  // The association permit should be set here.
915 
916  return sfrmSpec;
917 }
918 
919 GtsFields
921 {
922  GtsFields gtsFields;
923 
924  // TODO: Logic to populate the GTS Fields from local information here
925 
926  return gtsFields;
927 }
928 
931 {
932  PendingAddrFields pndAddrFields;
933 
934  // TODO: Logic to populate the Pending Address Fields from local information here
935  return pndAddrFields;
936 }
937 
938 
939 void
941 {
942  m_csmaCa = csmaCa;
943 }
944 
945 void
947 {
948  m_phy = phy;
949 }
950 
953 {
954  return m_phy;
955 }
956 
957 void
959 {
961 }
962 
963 void
965 {
967 }
968 
969 void
971 {
973 }
974 
975 void
977 {
979 }
980 
981 void
983 {
985 }
986 
987 void
989 {
991 }
992 
993 void
994 LrWpanMac::PdDataIndication (uint32_t psduLength, Ptr<Packet> p, uint8_t lqi)
995 {
997  NS_LOG_FUNCTION (this << psduLength << p << (uint16_t)lqi);
998 
999  bool acceptFrame;
1000 
1001  // from sec 7.5.6.2 Reception and rejection, Std802.15.4-2006
1002  // level 1 filtering, test FCS field and reject if frame fails
1003  // level 2 filtering if promiscuous mode pass frame to higher layer otherwise perform level 3 filtering
1004  // level 3 filtering accept frame
1005  // if Frame type and version is not reserved, and
1006  // if there is a dstPanId then dstPanId=m_macPanId or broadcastPanI, and
1007  // if there is a shortDstAddr then shortDstAddr =shortMacAddr or broadcastAddr, and
1008  // if beacon frame then srcPanId = m_macPanId
1009  // if only srcAddr field in Data or Command frame,accept frame if srcPanId=m_macPanId
1010 
1011  Ptr<Packet> originalPkt = p->Copy (); // because we will strip headers
1012  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
1013  m_promiscSnifferTrace (originalPkt);
1014 
1015  m_macPromiscRxTrace (originalPkt);
1016  // XXX no rejection tracing (to macRxDropTrace) being performed below
1017 
1018  LrWpanMacTrailer receivedMacTrailer;
1019  p->RemoveTrailer (receivedMacTrailer);
1020  if (Node::ChecksumEnabled ())
1021  {
1022  receivedMacTrailer.EnableFcs (true);
1023  }
1024 
1025  // level 1 filtering
1026  if (!receivedMacTrailer.CheckFcs (p))
1027  {
1028  m_macRxDropTrace (originalPkt);
1029  }
1030  else
1031  {
1032  LrWpanMacHeader receivedMacHdr;
1033  p->RemoveHeader (receivedMacHdr);
1034 
1035  McpsDataIndicationParams params;
1036  params.m_dsn = receivedMacHdr.GetSeqNum ();
1037  params.m_mpduLinkQuality = lqi;
1038  params.m_srcPanId = receivedMacHdr.GetSrcPanId ();
1039  params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode ();
1040  switch (params.m_srcAddrMode)
1041  {
1042  case SHORT_ADDR:
1043  params.m_srcAddr = receivedMacHdr.GetShortSrcAddr ();
1044  break;
1045  case EXT_ADDR:
1046  params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr ();
1047  break;
1048  default:
1049  break;
1050  }
1051  params.m_dstPanId = receivedMacHdr.GetDstPanId ();
1052  params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode ();
1053  switch (params.m_dstAddrMode)
1054  {
1055  case SHORT_ADDR:
1056  params.m_dstAddr = receivedMacHdr.GetShortDstAddr ();
1057  break;
1058  case EXT_ADDR:
1059  params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr ();
1060  break;
1061  default:
1062  break;
1063  }
1064 
1066  {
1067  //level 2 filtering
1068  if (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR)
1069  {
1070  NS_LOG_DEBUG ("Packet from " << params.m_srcAddr);
1071  NS_LOG_DEBUG ("Packet to " << params.m_dstAddr);
1072  }
1073  else if (receivedMacHdr.GetDstAddrMode () == EXT_ADDR)
1074  {
1075  NS_LOG_DEBUG ("Packet from " << params.m_srcExtAddr);
1076  NS_LOG_DEBUG ("Packet to " << params.m_dstExtAddr);
1077  }
1078 
1079  //TODO: Fix here, this should trigger different Indication Callbacks
1080  //depending the type of frame received (data,command, beacon)
1082  {
1083  NS_LOG_DEBUG ("promiscuous mode, forwarding up");
1084  m_mcpsDataIndicationCallback (params, p);
1085  }
1086  else
1087  {
1088  NS_LOG_ERROR (this << " Data Indication Callback not initialized");
1089  }
1090  }
1091  else
1092  {
1093  //level 3 frame filtering
1094  acceptFrame = (receivedMacHdr.GetType () != LrWpanMacHeader::LRWPAN_MAC_RESERVED);
1095 
1096  if (acceptFrame)
1097  {
1098  acceptFrame = (receivedMacHdr.GetFrameVer () <= 1);
1099  }
1100 
1101  if (acceptFrame
1102  && (receivedMacHdr.GetDstAddrMode () > 1))
1103  {
1104  acceptFrame = receivedMacHdr.GetDstPanId () == m_macPanId
1105  || receivedMacHdr.GetDstPanId () == 0xffff;
1106  }
1107 
1108  if (acceptFrame
1109  && (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR))
1110  {
1111  if (receivedMacHdr.GetShortDstAddr () == m_shortAddress)
1112  {
1113  // unicast, for me
1114  acceptFrame = true;
1115  }
1116  else if (receivedMacHdr.GetShortDstAddr () == Mac16Address ("ff:ff"))
1117  {
1118  // broadcast
1119  acceptFrame = true;
1120  }
1121  else
1122  {
1123  // multicast
1124  // See RFC 4944, Section 12
1125  // Multicast address 16 bits: 100X XXXX XXXX XXXX
1126  uint8_t buf[2];
1127  receivedMacHdr.GetShortDstAddr ().CopyTo (buf);
1128  if (buf[0] & 0x80)
1129  {
1130  acceptFrame = true;
1131  }
1132  else
1133  {
1134  acceptFrame = false;
1135  }
1136  }
1137  }
1138 
1139  if (acceptFrame
1140  && (receivedMacHdr.GetDstAddrMode () == EXT_ADDR))
1141  {
1142  acceptFrame = (receivedMacHdr.GetExtDstAddr () == m_selfExt);
1143  }
1144 
1145  if (acceptFrame
1146  && (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_BEACON))
1147  {
1148  if (m_macPanId == 0xffff)
1149  {
1150  // TODO: Accept only if the frame version field is valid
1151  acceptFrame = true;
1152  }
1153  else
1154  {
1155  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId;
1156  }
1157  }
1158 
1159  if (acceptFrame
1160  && ((receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_DATA)
1161  || (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_COMMAND))
1162  && (receivedMacHdr.GetSrcAddrMode () > 1))
1163  {
1164  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId; // TODO: need to check if PAN coord
1165  }
1166 
1167  if (acceptFrame)
1168  {
1169  m_macRxTrace (originalPkt);
1170  // \todo: What should we do if we receive a frame while waiting for an ACK?
1171  // Especially if this frame has the ACK request bit set, should we reply with an ACK, possibly missing the pending ACK?
1172 
1173  // If the received frame is a frame with the ACK request bit set, we immediately send back an ACK.
1174  // If we are currently waiting for a pending ACK, we assume the ACK was lost and trigger a retransmission after sending the ACK.
1175  if ((receivedMacHdr.IsData () || receivedMacHdr.IsCommand ()) && receivedMacHdr.IsAckReq ()
1176  && !(receivedMacHdr.GetDstAddrMode () == SHORT_ADDR && receivedMacHdr.GetShortDstAddr () == "ff:ff"))
1177  {
1178  // If this is a data or mac command frame, which is not a broadcast,
1179  // with ack req set, generate and send an ack frame.
1180  // If there is a CSMA medium access in progress we cancel the medium access
1181  // for sending the ACK frame. A new transmission attempt will be started
1182  // after the ACK was send.
1184  {
1187  }
1188  else if (m_lrWpanMacState == MAC_CSMA)
1189  {
1190  // \todo: If we receive a packet while doing CSMA/CA, should we drop the packet because of channel busy,
1191  // or should we restart CSMA/CA for the packet after sending the ACK?
1192  // Currently we simply restart CSMA/CA after sending the ACK.
1193  m_csmaCa->Cancel ();
1194  }
1195  // Cancel any pending MAC state change, ACKs have higher priority.
1196  m_setMacState.Cancel ();
1198 
1199  m_setMacState = Simulator::ScheduleNow (&LrWpanMac::SendAck, this, receivedMacHdr.GetSeqNum ());
1200  }
1201 
1202 
1203  if (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR)
1204  {
1205  NS_LOG_DEBUG ("Packet from " << params.m_srcAddr);
1206  NS_LOG_DEBUG ("Packet to " << params.m_dstAddr);
1207  }
1208  else if (receivedMacHdr.GetDstAddrMode () == EXT_ADDR)
1209  {
1210  NS_LOG_DEBUG ("Packet from " << params.m_srcExtAddr);
1211  NS_LOG_DEBUG ("Packet to " << params.m_dstExtAddr);
1212  }
1213 
1214 
1215  if (receivedMacHdr.IsBeacon ())
1216  {
1217 
1218 
1219  // The received beacon size in symbols
1220  // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default 17 bytes)
1221  m_rxBeaconSymbols = m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1222  (originalPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ());
1223 
1224  // The start of Rx beacon time and start of the Incoming superframe Active Period
1225  m_macBeaconRxTime = Simulator::Now () - Seconds (double(m_rxBeaconSymbols) / symbolRate);
1226 
1227  NS_LOG_DEBUG ("Beacon Received; forwarding up (m_macBeaconRxTime: " << m_macBeaconRxTime.As (Time::S) << ")");
1228 
1229 
1230  //TODO: Handle mlme-scan.request here
1231 
1232  // Device not associated.
1233  if (m_macPanId == 0xffff)
1234  {
1235  //TODO: mlme-associate.request here.
1236  NS_LOG_ERROR (this << "The current device is not associated to any coordinator");
1237  return;
1238  }
1239 
1240 
1241  if (m_macPanId != receivedMacHdr.GetDstPanId ())
1242  {
1243  return;
1244  }
1245 
1246  BeaconPayloadHeader receivedMacPayload;
1247  p->RemoveHeader (receivedMacPayload);
1248 
1249  SuperframeField incomingSuperframe;
1250  incomingSuperframe = receivedMacPayload.GetSuperframeSpecField ();
1251 
1252  m_incomingBeaconOrder = incomingSuperframe.GetBeaconOrder ();
1253  m_incomingSuperframeOrder = incomingSuperframe.GetFrameOrder ();
1254  m_incomingFnlCapSlot = incomingSuperframe.GetFinalCapSlot ();
1255 
1258 
1259  if (incomingSuperframe.IsBattLifeExt ())
1260  {
1261  m_csmaCa->SetBatteryLifeExtension (true);
1262  }
1263  else
1264  {
1265  m_csmaCa->SetBatteryLifeExtension (false);
1266  }
1267 
1268  if (m_incomingBeaconOrder < 15 && !m_csmaCa->IsSlottedCsmaCa ())
1269  {
1270  m_csmaCa->SetSlottedCsmaCa ();
1271  }
1272 
1273  //TODO: get Incoming frame GTS Fields here
1274 
1275  NS_LOG_DEBUG ("Incoming superframe Active Portion (Beacon + CAP + CFP): " << m_incomingSuperframeDuration << " symbols");
1276 
1277  //Begin CAP on the current device using info from the Incoming superframe
1279 
1280 
1281  // Send a Beacon notification only if we are not
1282  // automatically sending data command requests or the beacon contains
1283  // a beacon payload in its MAC payload.
1284  // see IEEE 802.15.4-2011 Section 6.2.4.1
1285  if ((m_macAutoRequest == false) || p->GetSize () > 0)
1286  {
1287  //TODO: Add the rest of the MlmeBeaconNotifyIndication params
1289  {
1290  MlmeBeaconNotifyIndicationParams beaconParams;
1291  beaconParams.m_bsn = receivedMacHdr.GetSeqNum ();
1292  m_mlmeBeaconNotifyIndicationCallback (beaconParams,originalPkt);
1293  }
1294  }
1295 
1296  if (m_macAutoRequest)
1297  {
1298  // check if MLME-SYNC.request was previously issued and running
1299  // Sync. is necessary to handle pending messages (indirect transmissions)
1300  if (m_trackingEvent.IsRunning ())
1301  {
1303  m_numLostBeacons = 0;
1304 
1305  if (m_beaconTrackingOn)
1306  {
1307  //if tracking option is on keep tracking the next beacon
1308  uint64_t searchSymbols;
1309  Time searchBeaconTime;
1310 
1311  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
1312  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
1314  }
1315 
1316  PendingAddrFields pndAddrFields;
1317  pndAddrFields = receivedMacPayload.GetPndAddrFields ();
1318 
1319  //TODO: Ignore pending request if the address is in the GTS list.
1320  // If the address is not in the GTS list, then check if the address
1321  // is in the short address pending list or in the extended address
1322  // pending list.
1323 
1324  }
1325  }
1326  }
1327  else if (receivedMacHdr.IsData () && !m_mcpsDataIndicationCallback.IsNull ())
1328  {
1329  // If it is a data frame, push it up the stack.
1330  NS_LOG_DEBUG ("Data Packet is for me; forwarding up");
1331  m_mcpsDataIndicationCallback (params, p);
1332  }
1333  else if (receivedMacHdr.IsAcknowledgment () && m_txPkt && m_lrWpanMacState == MAC_ACK_PENDING)
1334  {
1335  LrWpanMacHeader macHdr;
1336  Time ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1337  m_txPkt->PeekHeader (macHdr);
1338  if (receivedMacHdr.GetSeqNum () == macHdr.GetSeqNum ())
1339  {
1341  // If it is an ACK with the expected sequence number, finish the transmission
1342  // and notify the upper layer.
1345  {
1346  TxQueueElement *txQElement = m_txQueue.front ();
1347  McpsDataConfirmParams confirmParams;
1348  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1349  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1350  m_mcpsDataConfirmCallback (confirmParams);
1351  }
1353 
1354  // Ack was succesfully received, wait for the Interframe Space (IFS) and then proceed
1356  }
1357  else
1358  {
1359  // If it is an ACK with an unexpected sequence number, mark the current transmission as failed and start a retransmit. (cf 7.5.6.4.3)
1361  if (!PrepareRetransmission ())
1362  {
1363  m_setMacState.Cancel ();
1365  }
1366  else
1367  {
1368  m_setMacState.Cancel ();
1370  }
1371  }
1372  }
1373  }
1374  else
1375  {
1376  m_macRxDropTrace (originalPkt);
1377  }
1378  }
1379  }
1380 }
1381 
1382 
1383 
1384 void
1385 LrWpanMac::SendAck (uint8_t seqno)
1386 {
1387  NS_LOG_FUNCTION (this << static_cast<uint32_t> (seqno));
1388 
1390 
1391  // Generate a corresponding ACK Frame.
1393  LrWpanMacTrailer macTrailer;
1394  Ptr<Packet> ackPacket = Create<Packet> (0);
1395  ackPacket->AddHeader (macHdr);
1396  // Calculate FCS if the global attribute ChecksumEnable is set.
1397  if (Node::ChecksumEnabled ())
1398  {
1399  macTrailer.EnableFcs (true);
1400  macTrailer.SetFcs (ackPacket);
1401  }
1402  ackPacket->AddTrailer (macTrailer);
1403 
1404  // Enqueue the ACK packet for further processing
1405  // when the transmitter is activated.
1406  m_txPkt = ackPacket;
1407 
1408  // Switch transceiver to TX mode. Proceed sending the Ack on confirm.
1410  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1411 
1412 }
1413 
1414 void
1416 {
1417  TxQueueElement *txQElement = m_txQueue.front ();
1418  Ptr<const Packet> p = txQElement->txQPkt;
1419  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1420 
1421  Ptr<Packet> pkt = p->Copy ();
1422  LrWpanMacHeader hdr;
1423  pkt->RemoveHeader (hdr);
1424  if (hdr.GetShortDstAddr () != Mac16Address ("ff:ff"))
1425  {
1427  }
1428 
1429  txQElement->txQPkt = 0;
1430  delete txQElement;
1431  m_txQueue.pop_front ();
1432  m_txPkt = 0;
1433  m_retransmission = 0;
1434  m_numCsmacaRetry = 0;
1435  m_macTxDequeueTrace (p);
1436 }
1437 
1438 void
1440 {
1441  NS_LOG_FUNCTION (this);
1442 
1443  // TODO: If we are a PAN coordinator and this was an indirect transmission,
1444  // we will not initiate a retransmission. Instead we wait for the data
1445  // being extracted after a new data request command.
1446  if (!PrepareRetransmission ())
1447  {
1449  }
1450  else
1451  {
1453  }
1454 }
1455 
1456 void
1458 {
1459  NS_LOG_DEBUG ("IFS Completed");
1460  m_setMacState.Cancel ();
1462 }
1463 
1464 
1465 bool
1467 {
1468  NS_LOG_FUNCTION (this);
1469 
1471  {
1472  // Maximum number of retransmissions has been reached.
1473  // remove the copy of the packet that was just sent
1474  TxQueueElement *txQElement = m_txQueue.front ();
1475  m_macTxDropTrace (txQElement->txQPkt);
1477  {
1478  McpsDataConfirmParams confirmParams;
1479  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1480  confirmParams.m_status = IEEE_802_15_4_NO_ACK;
1481  m_mcpsDataConfirmCallback (confirmParams);
1482  }
1484  return false;
1485  }
1486  else
1487  {
1488  m_retransmission++;
1489  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1490  // Start next CCA process for this packet.
1491  return true;
1492  }
1493 }
1494 
1495 void
1497 {
1499  NS_LOG_FUNCTION (this << status << m_txQueue.size ());
1500 
1501  LrWpanMacHeader macHdr;
1502  Time ifsWaitTime;
1503  uint64_t symbolRate;
1504 
1505  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
1506 
1507  m_txPkt->PeekHeader (macHdr);
1508 
1509  if (status == IEEE_802_15_4_PHY_SUCCESS)
1510  {
1511  if (!macHdr.IsAcknowledgment ())
1512  {
1513  if (macHdr.IsBeacon ())
1514  {
1515  ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1516 
1517  // The Tx Beacon in symbols
1518  // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default 17 bytes)
1519  uint64_t beaconSymbols = m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1520  (m_txPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ());
1521 
1522  // The beacon Tx time and start of the Outgoing superframe Active Period
1523  m_macBeaconTxTime = Simulator::Now () - Seconds (double(beaconSymbols) / symbolRate);
1524 
1525 
1526  m_txPkt = 0;
1528  NS_LOG_DEBUG ("Beacon Sent (m_macBeaconTxTime: " << m_macBeaconTxTime.As (Time::S) << ")");
1529 
1530  MlmeStartConfirmParams mlmeConfirmParams;
1531  mlmeConfirmParams.m_status = MLMESTART_SUCCESS;
1533  {
1534  m_mlmeStartConfirmCallback (mlmeConfirmParams);
1535  }
1536  }
1537  else if (macHdr.IsAckReq ()) // We have sent a regular data packet, check if we have to wait for an ACK.
1538  {
1539  // wait for the ack or the next retransmission timeout
1540  // start retransmission timer
1541  Time waitTime = Seconds ((double) GetMacAckWaitDuration () / symbolRate);
1544  m_setMacState.Cancel ();
1546  return;
1547  }
1548  else
1549  {
1551  // remove the copy of the packet that was just sent
1553  {
1554  McpsDataConfirmParams confirmParams;
1555  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1556  TxQueueElement *txQElement = m_txQueue.front ();
1557  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1558  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1559  m_mcpsDataConfirmCallback (confirmParams);
1560  }
1561  ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1563  }
1564  }
1565  else
1566  {
1567  // We have send an ACK. Clear the packet buffer.
1568  m_txPkt = 0;
1569  }
1570  }
1571  else if (status == IEEE_802_15_4_PHY_UNSPECIFIED)
1572  {
1573 
1574  if (!macHdr.IsAcknowledgment ())
1575  {
1576  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1577  TxQueueElement *txQElement = m_txQueue.front ();
1578  m_macTxDropTrace (txQElement->txQPkt);
1580  {
1581  McpsDataConfirmParams confirmParams;
1582  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1583  confirmParams.m_status = IEEE_802_15_4_FRAME_TOO_LONG;
1584  m_mcpsDataConfirmCallback (confirmParams);
1585  }
1587  }
1588  else
1589  {
1590  NS_LOG_ERROR ("Unable to send ACK");
1591  }
1592  }
1593  else
1594  {
1595  // Something went really wrong. The PHY is not in the correct state for
1596  // data transmission.
1597  NS_FATAL_ERROR ("Transmission attempt failed with PHY status " << status);
1598  }
1599 
1600 
1601  if (!ifsWaitTime.IsZero ())
1602  {
1604  }
1605  else
1606  {
1607  m_setMacState.Cancel ();
1609  }
1610 
1611 }
1612 
1613 void
1615 {
1616  NS_LOG_FUNCTION (this << status);
1617  // Direct this call through the csmaCa object
1618  m_csmaCa->PlmeCcaConfirm (status);
1619 }
1620 
1621 void
1622 LrWpanMac::PlmeEdConfirm (LrWpanPhyEnumeration status, uint8_t energyLevel)
1623 {
1624  NS_LOG_FUNCTION (this << status << energyLevel);
1625 
1626 }
1627 
1628 void
1631  LrWpanPhyPibAttributes* attribute)
1632 {
1633  NS_LOG_FUNCTION (this << status << id << attribute);
1634 }
1635 
1636 void
1638 {
1639  NS_LOG_FUNCTION (this << status);
1640 
1642  {
1643  NS_ASSERT (m_txPkt);
1644 
1645  // Start sending if we are in state SENDING and the PHY transmitter was enabled.
1649  m_phy->PdDataRequest (m_txPkt->GetSize (), m_txPkt);
1650  }
1651  else if (m_lrWpanMacState == MAC_CSMA && (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
1652  {
1653  // Start the CSMA algorithm as soon as the receiver is enabled.
1654  m_csmaCa->Start ();
1655  }
1656  else if (m_lrWpanMacState == MAC_IDLE)
1657  {
1659  // Do nothing special when going idle.
1660  }
1661  else if (m_lrWpanMacState == MAC_ACK_PENDING)
1662  {
1664  }
1665  else
1666  {
1667  // TODO: What to do when we receive an error?
1668  // If we want to transmit a packet, but switching the transceiver on results
1669  // in an error, we have to recover somehow (and start sending again).
1670  NS_FATAL_ERROR ("Error changing transceiver state");
1671  }
1672 }
1673 
1674 void
1677 {
1678  NS_LOG_FUNCTION (this << status << id);
1679 }
1680 
1681 void
1683 {
1684  NS_LOG_FUNCTION (this << "mac state = " << macState);
1685 
1686  McpsDataConfirmParams confirmParams;
1687 
1688  if (macState == MAC_IDLE)
1689  {
1691 
1692  if (m_macRxOnWhenIdle)
1693  {
1694  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1695  }
1696  else
1697  {
1698  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
1699  }
1700 
1701  CheckQueue ();
1702  }
1703  else if (macState == MAC_ACK_PENDING)
1704  {
1706  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1707  }
1708  else if (macState == MAC_CSMA)
1709  {
1711 
1713  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1714  }
1715  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_IDLE)
1716  {
1717  // Channel is idle, set transmitter to TX_ON
1719  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1720  }
1721  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE)
1722  {
1723  NS_ASSERT (m_txPkt);
1724 
1725  // cannot find a clear channel, drop the current packet.
1726  NS_LOG_DEBUG ( this << " cannot find clear channel");
1727  confirmParams.m_msduHandle = m_txQueue.front ()->txQMsduHandle;
1731  {
1732  m_mcpsDataConfirmCallback (confirmParams);
1733  }
1734  // remove the copy of the packet that was just sent
1737  }
1738  else if (m_lrWpanMacState == MAC_CSMA && macState == MAC_CSMA_DEFERRED)
1739  {
1741  m_txPkt = 0;
1742  NS_LOG_DEBUG ("****** PACKET DEFERRED to the next superframe *****");
1743  }
1744 }
1745 
1748 {
1749  return m_associationStatus;
1750 }
1751 
1752 void
1754 {
1755  m_associationStatus = status;
1756 }
1757 
1758 uint16_t
1760 {
1761  return m_macPanId;
1762 }
1763 
1764 void
1765 LrWpanMac::SetPanId (uint16_t panId)
1766 {
1767  m_macPanId = panId;
1768 }
1769 
1770 void
1772 {
1773  NS_LOG_LOGIC (this << " change lrwpan mac state from "
1774  << m_lrWpanMacState << " to "
1775  << newState);
1776  m_macStateLogger (m_lrWpanMacState, newState);
1777  m_lrWpanMacState = newState;
1778 }
1779 
1780 uint64_t
1782 {
1783  return m_csmaCa->GetUnitBackoffPeriod () + m_phy->aTurnaroundTime + m_phy->GetPhySHRDuration ()
1784  + ceil (6 * m_phy->GetPhySymbolsPerOctet ());
1785 }
1786 
1787 uint8_t
1789 {
1790  return m_macMaxFrameRetries;
1791 }
1792 
1793 void
1795 {
1796  NS_LOG_DEBUG("Transit Queue Size: "<<m_txQueue.size());
1797 }
1798 
1799 void
1801 {
1802  m_macMaxFrameRetries = retries;
1803 }
1804 
1805 bool
1807 {
1808  NS_ASSERT (m_txPkt);
1809  LrWpanMacHeader macHdr;
1810  m_txPkt->PeekHeader (macHdr);
1811 
1812  if (m_macCoordShortAddress == macHdr.GetShortDstAddr ()
1813  || m_macCoordExtendedAddress == macHdr.GetExtDstAddr ())
1814  {
1815  return true;
1816  }
1817  else
1818  {
1819  std::cout << "ERROR: Packet not for the coordinator!\n";
1820  return false;
1821 
1822  }
1823 
1824 }
1825 
1826 uint32_t
1828 {
1829  NS_ASSERT (m_txPkt);
1830 
1831  if (m_txPkt->GetSize () <= aMaxSIFSFrameSize)
1832  {
1833  return m_macSIFSPeriod;
1834  }
1835  else
1836  {
1837  return m_macLIFSPeriod;
1838  }
1839 }
1840 
1841 void
1843 {
1845 }
1846 
1847 void
1849 {
1851 }
1852 
1853 
1854 uint64_t
1856 {
1857  NS_ASSERT (m_txPkt);
1858  // Sync Header (SHR) + 8 bits PHY header (PHR) + PSDU
1859  return (m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1860  (m_txPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ()));
1861 }
1862 
1863 bool
1865 {
1866  NS_ASSERT (m_txPkt);
1867  LrWpanMacHeader macHdr;
1868  m_txPkt->PeekHeader (macHdr);
1869 
1870  return macHdr.IsAckReq ();
1871 }
1872 
1873 } // namespace ns3
1874 
void SetBeaconOrder(uint8_t bcnOrder)
Set the superframe specification Beacon Order field.
MlmeSyncLossIndicationCallback m_mlmeSyncLossIndicationCallback
This callback is used to indicate the loss of synchronization with a coordinator. ...
Definition: lr-wpan-mac.h:1227
TracedValue< LrWpanMacState > m_lrWpanMacState
The current state of the MAC layer.
Definition: lr-wpan-mac.h:1254
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void MlmePollRequest(MlmePollRequestParams params)
IEEE 802.15.4-2011, section 6.2.14.2 MLME-POLL.request Prompts the device to request data from the co...
Definition: lr-wpan-mac.cc:631
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
TX_OPTION_INDIRECT.
Definition: lr-wpan-mac.h:61
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
void CheckQueue(void)
Check the transmission queue.
Definition: lr-wpan-mac.cc:877
LrWpanSyncLossReason m_lossReason
The reason for the lost of synchronization.
Definition: lr-wpan-mac.h:376
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint8_t GetSeqNum(void) const
Get the frame Sequence number.
static const uint32_t aMaxSIFSFrameSize
The maximum size of an MPDU, in octets, that can be followed by a Short InterFrame Spacing (SIFS) per...
Definition: lr-wpan-mac.h:492
bool m_beaconTrackingOn
Indication of whether the current device is tracking incoming beacons.
Definition: lr-wpan-mac.h:894
static const uint32_t aMaxLostBeacons
The number of consecutive lost beacons that will cause the MAC sublayer of a receiving device to decl...
Definition: lr-wpan-mac.h:486
bool m_trackBcn
True if the mlme sync with the next beacon and attempts to track future beacons.
Definition: lr-wpan-mac.h:323
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
Class that implements the LR-WPAN MAC state machine.
Definition: lr-wpan-mac.h:448
Mac16Address GetShortDstAddr(void) const
Get the Destination Short address.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint8_t GetFinalCapSlot(void) const
Check if the Final CAP Slot bit is enabled.
void PlmeGetAttributeConfirm(LrWpanPhyEnumeration status, LrWpanPibAttributeIdentifier id, LrWpanPhyPibAttributes *attribute)
IEEE 802.15.4-2006 section 6.2.2.6 PLME-GET.confirm Get attributes per definition from Table 23 in se...
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Mac16Address GetShortSrcAddr(void) const
Get the Source Short address.
uint8_t GetBeaconOrder(void) const
Get the Superframe Specification Beacon Order field.
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:249
void RemoveFirstTxQElement()
Remove the tip of the transmission queue, including clean up related to the last packet transmission...
Mac64Address m_srcExtAddr
Source extended address.
Definition: lr-wpan-mac.h:275
virtual ~LrWpanMac(void)
Definition: lr-wpan-mac.cc:192
TracedCallback< Ptr< const Packet > > m_macTxOkTrace
The trace source fired when packets where successfully transmitted, that is an acknowledgment was rec...
Definition: lr-wpan-mac.h:1130
LrWpanPibAttributeIdentifier
IEEE802.15.4-2006 PHY PIB Attribute Identifiers Table 23 in section 6.4.2.
Definition: lr-wpan-phy.h:140
uint32_t phyCurrentPage
Current channel page.
Definition: lr-wpan-phy.h:163
PendingAddrFields GetPndAddrFields(void) const
static bool ChecksumEnabled(void)
Definition: node.cc:278
CHANNEL_IDLE.
Definition: lr-wpan-mac.h:76
bool IsData(void) const
Returns true if the header is a data.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
bool isCoordDest(void)
Check if the packet destination is its coordinator.
McpsDataConfirmCallback m_mcpsDataConfirmCallback
This callback is used to report data transmission request status to the upper layers.
Definition: lr-wpan-mac.h:1250
std::deque< TxQueueElement * > m_txQueue
The transmit queue used by the MAC.
Definition: lr-wpan-mac.h:1285
MAC_CSMA_DEFERRED.
Definition: lr-wpan-mac.h:80
Mac64Address GetExtendedAddress(void) const
Get the extended address of this MAC.
Definition: lr-wpan-mac.cc:282
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:278
uint8_t m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:276
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
Mac64Address m_dstExtAddr
Destination extended address.
Definition: lr-wpan-mac.h:279
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
uint8_t m_logCh
The channel number on which to attempt coordinator synchronization.
Definition: lr-wpan-mac.h:322
void SetMlmeBeaconNotifyIndicationCallback(MlmeBeaconNotifyIndicationCallback c)
Set the callback for the indication of an incoming beacon packet.
Definition: lr-wpan-mac.cc:976
static const uint32_t aNumSuperframeSlots
Number of a superframe slots per superframe.
Definition: lr-wpan-mac.h:473
void SetMlmeStartConfirmCallback(MlmeStartConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:970
bool IsBattLifeExt(void) const
Check if the Battery Life Extension bit is enabled.
bool m_battLifeExt
Flag indicating whether or not the Battery life extension (BLE) features are used.
Definition: lr-wpan-mac.h:308
LrWpanAssociationStatus m_associationStatus
The current association status of the MAC layer.
Definition: lr-wpan-mac.h:1266
uint8_t m_incomingFnlCapSlot
Indication of the Slot where the CAP portion of the INCOMING Superframe ends.
Definition: lr-wpan-mac.h:807
Mac64Address m_dstExtAddr
Destination extended address.
Definition: lr-wpan-mac.h:250
void CopyTo(uint8_t buffer[2]) const
Mac64Address m_macCoordExtendedAddress
The extended address of the coordinator through which the device is associated.
Definition: lr-wpan-mac.h:756
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
virtual void DoDispose(void)
Destructor implementation.
Definition: lr-wpan-mac.cc:211
TracedCallback< LrWpanMacState, LrWpanMacState > m_macStateLogger
A trace source that fires when the LrWpanMac changes states.
Definition: lr-wpan-mac.h:1209
void SetShortAddress(Mac16Address address)
Set the short address of this MAC.
Definition: lr-wpan-mac.cc:260
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
uint8_t m_incomingBeaconOrder
The beaconOrder value of the INCOMING frame.
Definition: lr-wpan-mac.h:796
Represent the Mac Header with the Frame Control and Sequence Number fields.
bool IsCommand(void) const
Returns true if the header is a command.
uint8_t m_incomingSuperframeOrder
Used by all devices that have a parent.
Definition: lr-wpan-mac.h:803
uint16_t GetDstPanId(void) const
Get the Destination PAN ID.
uint32_t m_superframeDuration
Indication of the superframe duration in symbols.
Definition: lr-wpan-mac.h:876
TimeWithUnit As(const enum Unit unit) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:389
IEEE802.15.4-2006 PHY PIB Attributes Table 23 in section 6.4.2.
Definition: lr-wpan-phy.h:157
McpsDataIndicationCallback m_mcpsDataIndicationCallback
This callback is used to notify incoming packets to the upper layers.
Definition: lr-wpan-mac.h:1244
void SetMcpsDataConfirmCallback(McpsDataConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:964
void AwaitBeacon(void)
Called after the end of an INCOMING superframe to start the moment a device waits for a new incoming ...
Definition: lr-wpan-mac.cc:833
Incoming Superframe.
Definition: lr-wpan-mac.h:104
MlmeStartConfirmCallback m_mlmeStartConfirmCallback
This callback is used to report the start of a new PAN or the begin of a new superframe configuration...
Definition: lr-wpan-mac.h:1239
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p)
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU. ...
Definition: lr-wpan-mac.cc:288
uint8_t m_numCsmacaRetry
The number of CSMA/CA retries used for sending the current packet.
Definition: lr-wpan-mac.h:1298
MLME-START.request params.
Definition: lr-wpan-mac.h:288
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:65
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:251
uint32_t m_incomingBeaconInterval
Indication of the interval a node should receive a superframe expressed in symbols.
Definition: lr-wpan-mac.h:881
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:262
uint8_t GetFrameVer(void) const
Get the Frame Version of Frame control field.
EventId m_incCapEvent
Scheduler event for the end of the incoming superframe CAP.
Definition: lr-wpan-mac.h:1327
static const uint32_t aMaxPhyPacketSize
The maximum packet size accepted by the PHY.
Definition: lr-wpan-phy.h:262
void SetSuperframeSpecField(SuperframeField sfrmField)
Set the superframe specification field to the beacon payload header.
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:277
Contention Free Period.
Definition: lr-wpan-mac.h:92
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TracedCallback< Ptr< const Packet > > m_macTxTrace
The trace source fired when packets are being sent down to L1.
Definition: lr-wpan-mac.h:1122
an EUI-64 address
Definition: mac64-address.h:43
phy
Definition: third.py:93
Ptr< Packet > m_txPkt
The packet which is currently being sent by the MAC layer.
Definition: lr-wpan-mac.h:1270
bool GetRxOnWhenIdle(void)
Check if the receiver will be enabled when the MAC is idle.
Definition: lr-wpan-mac.cc:235
The Beacon transmission or reception Period.
Definition: lr-wpan-mac.h:90
MLME-BEACON-NOTIFY.indication params.
Definition: lr-wpan-mac.h:359
LrWpanAssociationStatus GetAssociationStatus(void) const
Get the current association status.
void SetLrWpanMacState(LrWpanMacState macState)
CSMA-CA algorithm calls back the MAC after executing channel assessment.
void SetMacMaxFrameRetries(uint8_t retries)
Set the macMaxFrameRetries attribute value.
void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t energyLevel)
IEEE 802.15.4-2006 section 6.2.2.4 PLME-ED.confirm status and energy level.
uint8_t m_macBeaconOrder
Used by a PAN coordinator or coordinator.
Definition: lr-wpan-mac.h:768
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device...
Definition: lr-wpan-mac.h:1200
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:59
TracedCallback< Ptr< const Packet > > m_macTxEnqueueTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition...
Definition: lr-wpan-mac.h:1109
uint8_t m_fnlCapSlot
Indication of the Slot where the CAP portion of the OUTGOING Superframe ends.
Definition: lr-wpan-mac.h:790
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
Definition: lr-wpan-mac.h:1153
void MlmeStartRequest(MlmeStartRequestParams params)
IEEE 802.15.4-2006, section 7.1.14.1 MLME-START.request Request to allow a PAN coordinator to initiat...
Definition: lr-wpan-mac.cc:495
bool m_panCoor
Indication of whether the current device is the PAN coordinator.
Definition: lr-wpan-mac.h:866
void SetSuperframeOrder(uint8_t frmOrder)
Set the superframe specification Superframe Order field.
uint64_t GetTxPacketSymbols(void)
Obtain the number of symbols in the packet which is currently being sent by the MAC layer...
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:306
bool isTxAckReq(void)
Check if the packet to transmit requires acknowledgment.
bool IsZero(void) const
Exactly equivalent to t == 0.
Definition: nstime.h:300
MlmeBeaconNotifyIndicationCallback m_mlmeBeaconNotifyIndicationCallback
This callback is used to notify incoming beacon packets to the upper layers.
Definition: lr-wpan-mac.h:1222
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:588
SuperframeType
Superframe type.
Definition: lr-wpan-mac.h:101
Ptr< Packet > txQPkt
Queued packet.
Definition: lr-wpan-mac.h:989
uint8_t GetDstAddrMode(void) const
Get the Dest.
uint8_t m_logCh
Logical channel on which to start using the new superframe configuration.
Definition: lr-wpan-mac.h:302
bool PrepareRetransmission(void)
Check for remaining retransmissions for the packet currently being sent.
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
Hold an unsigned integer type.
Definition: uinteger.h:44
EventId m_ackWaitTimeout
Scheduler event for the ACK timeout of the currently transmitted data packet.
Definition: lr-wpan-mac.h:1303
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
The trace source fired when packets are dropped due to missing ACKs or because of transmission failur...
Definition: lr-wpan-mac.h:1137
EventId m_ifsEvent
Scheduler event for Interframe spacing wait time.
Definition: lr-wpan-mac.h:1311
uint16_t GetPanId(void) const
Get the PAN id used by this MAC.
void SetMlmeSyncLossIndicationCallback(MlmeSyncLossIndicationCallback c)
Set the callback for the loss of synchronization with a coordinator.
Definition: lr-wpan-mac.cc:982
mac
Definition: third.py:99
EventId m_setMacState
Scheduler event for a deferred MAC state change.
Definition: lr-wpan-mac.h:1307
SuperframeField GetSuperframeField(void)
Constructs a Superframe specification field from the local information, the superframe Specification ...
Definition: lr-wpan-mac.cc:895
PendingAddrFields GetPendingAddrFields(void)
Constructs Pending Address Fields from the local information, the Pending Address Fields are part of ...
Definition: lr-wpan-mac.cc:930
Helper structure for managing transmission queue elements.
Definition: lr-wpan-mac.h:986
bool m_macRxOnWhenIdle
Indication of whether the MAC sublayer is to enable its receiver during idle periods.
Definition: lr-wpan-mac.h:840
Outgoing Superframe.
Definition: lr-wpan-mac.h:103
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
void MlmeSyncRequest(MlmeSyncRequestParams params)
IEEE 802.15.4-2011, section 6.2.13.1 MLME-SYNC.request Request to synchronize with the coordinator by...
Definition: lr-wpan-mac.cc:591
enum LrWpanMacType GetType(void) const
Get the header type.
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:260
void PdDataConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.1.2 Confirm the end of transmission of an MPDU to MAC.
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:252
Ptr< Packet > txQPkt
Queued packet.
Definition: lr-wpan-mac.h:998
void StartInactivePeriod(SuperframeType superframeType)
Start the Inactive Period in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:796
void PrintTransmitQueueSize(void)
Print the number of elements in the packet transmit queue.
uint32_t GetIfsSize()
Get the size of the Interframe Space according to MPDU size (m_txPkt).
void SendOneBeacon(void)
Called to send a single beacon frame.
Definition: lr-wpan-mac.cc:645
uint32_t m_macLIFSPeriod
The minimum time forming a Long InterFrame Spacing (LIFS) period.
Definition: lr-wpan-mac.h:845
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
Definition: lr-wpan-mac.h:305
uint8_t m_mpduLinkQuality
LQI value measured during reception of the MPDU.
Definition: lr-wpan-mac.h:280
void SetPhy(Ptr< LrWpanPhy > phy)
Set the underlying PHY for the MAC.
Definition: lr-wpan-mac.cc:946
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
GtsFields GetGtsFields(void)
Constructs the Guaranteed Time Slots (GTS) Fields from local information The GTS Fields are part of t...
Definition: lr-wpan-mac.cc:920
Helper structure for managing indirect transmission queue elements.
Definition: lr-wpan-mac.h:995
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
bool IsAcknowledgment(void) const
Returns true if the header is an ack.
void SetAssociatedCoor(Mac16Address mac)
Check if the packet destination is its coordinator.
SuperframeField GetSuperframeSpecField(void) const
Get the superframe specification field from the beacon payload header.
Implements the header for the MAC payload beacon frame according to the IEEE 802.15.4-2011 Std.
TracedCallback< Ptr< const Packet >, uint8_t, uint8_t > m_sentPktTrace
The trace source fired when packets are considered as successfully sent or the transmission has been ...
Definition: lr-wpan-mac.h:1102
void PdDataIndication(uint32_t psduLength, Ptr< Packet > p, uint8_t lqi)
IEEE 802.15.4-2006 section 6.2.1.3 PD-DATA.indication Indicates the transfer of an MPDU from PHY to M...
Definition: lr-wpan-mac.cc:994
uint8_t m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:272
Mac64Address GetExtSrcAddr(void) const
Get the Source Extended address.
uint16_t m_PanId
Pan Identifier used by the device.
Definition: lr-wpan-mac.h:301
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:263
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
uint8_t m_retransmission
The number of already used retransmission for the currently transmitted packet.
Definition: lr-wpan-mac.h:1294
EventId m_trackingEvent
Scheduler event to track the incoming beacons.
Definition: lr-wpan-mac.h:1335
static const uint32_t aBaseSlotDuration
Length of a superframe slot in symbols.
Definition: lr-wpan-mac.h:468
Mac64Address m_selfExt
The extended address used by this MAC.
Definition: lr-wpan-mac.h:1281
uint64_t GetMacAckWaitDuration(void) const
Get the macAckWaitDuration attribute value.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
EventId m_cfpEvent
Scheduler event for the end of the outgoing superframe CFP.
Definition: lr-wpan-mac.h:1323
uint8_t GetSrcAddrMode(void) const
Get the Source Addressing Mode of Frame control field.
address
Definition: first.py:44
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:247
bool IsAckReq(void) const
Check if Ack.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:307
uint16_t m_macTransactionPersistanceTime
The maximum time (in superframe periods) that a transaction is stored by a coordinator and indicated ...
Definition: lr-wpan-mac.h:781
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
TracedCallback< Ptr< const Packet > > m_macRxDropTrace
The trace source fired for packets successfully received by the device but dropped before being forwa...
Definition: lr-wpan-mac.h:1161
void StartCAP(SuperframeType superframeType)
Called to begin the Contention Access Period (CAP) in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:703
uint8_t GetFrameOrder(void) const
Get the Superframe Specification Frame Order field.
TX_OPTION_GTS.
Definition: lr-wpan-mac.h:60
void SetRxOnWhenIdle(bool rxOnWhenIdle)
Set if the receiver should be enabled when the MAC is idle.
Definition: lr-wpan-mac.cc:241
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:318
uint32_t m_beaconInterval
Indication of the Interval used by the coordinator to transmit beacon frames expressed in symbols...
Definition: lr-wpan-mac.h:871
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
Definition: lr-wpan-mac.h:1145
bool m_macPromiscuousMode
Indicates if MAC sublayer is in receive all mode.
Definition: lr-wpan-mac.h:813
Mac16Address m_srcAddr
Source address.
Definition: lr-wpan-mac.h:274
MlmePollConfirmCallback m_mlmePollConfirmCallback
This callback is used to report the status after a device send data command request to the coordinato...
Definition: lr-wpan-mac.h:1233
SequenceNumber8 m_macBsn
Sequence number added to transmitted beacon frame, 00-ff.
Definition: lr-wpan-mac.h:829
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
Represent the Superframe Specification information field.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
uint8_t m_macSuperframeOrder
Used by a PAN coordinator or coordinator.
Definition: lr-wpan-mac.h:775
EventId m_capEvent
Scheduler event for the end of the outgoing superframe CAP.
Definition: lr-wpan-mac.h:1319
uint8_t m_dsn
The DSN of the received data frame.
Definition: lr-wpan-mac.h:281
std::deque< IndTxQueueElement * > m_indTxQueue
The indirect transmit queue used by the MAC pending messages.
Definition: lr-wpan-mac.h:1289
Mac16Address m_macCoordShortAddress
The short address of the coordinator through which the device is associated.
Definition: lr-wpan-mac.h:750
LrWpanMac(void)
Default constructor.
Definition: lr-wpan-mac.cc:147
uint8_t m_bsn
The beacon sequence number.
Definition: lr-wpan-mac.h:361
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
Definition: lr-wpan-mac.h:1180
void SetExtendedAddress(Mac64Address address)
Set the extended address of this MAC.
Definition: lr-wpan-mac.cc:267
uint8_t m_macMaxFrameRetries
The maximum number of retries allowed after a transmission failure.
Definition: lr-wpan-mac.h:834
uint16_t m_panId
The PAN identifier with which the device lost synchronization or to which it was realigned.
Definition: lr-wpan-mac.h:377
uint8_t txQMsduHandle
MSDU Handle.
Definition: lr-wpan-mac.h:988
uint16_t GetSrcPanId(void) const
Get the Source PAN ID.
Contention Access Period.
Definition: lr-wpan-mac.h:91
static TypeId GetTypeId(void)
Get the type ID.
Definition: lr-wpan-mac.cc:58
uint32_t m_logChPage
Logical channel page on which to start using the new superframe configuration.
Definition: lr-wpan-mac.h:303
uint32_t m_incomingSuperframeDuration
Indication of the superframe duration in symbols (e.g.
Definition: lr-wpan-mac.h:886
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
Represent the Mac Trailer with the Frame Check Sequence field.
Ptr< LrWpanPhy > m_phy
The PHY associated with this MAC.
Definition: lr-wpan-mac.h:1213
void SetGtsFields(GtsFields gtsFields)
Set the superframe Guaranteed Time Slot (GTS) fields to the beacon payload header.
EventId m_beaconEvent
Scheduler event for generation of one beacon.
Definition: lr-wpan-mac.h:1315
bool IsBeacon(void) const
Returns true if the header is a beacon.
MLME-POLL.request params.
Definition: lr-wpan-mac.h:330
void SetFinalCapSlot(uint8_t capSlot)
Set the superframe specification Final CAP slot field.
CHANNEL_ACCESS_FAILURE.
Definition: lr-wpan-mac.h:75
uint32_t m_ifs
The value of the necessary InterFrame Space after the transmission of a packet.
Definition: lr-wpan-mac.h:862
MLME-SYNC.request params.
Definition: lr-wpan-mac.h:316
Time expireTime
The expiration time of the packet in the indirect transmission queue.
Definition: lr-wpan-mac.h:999
MAC_CSMA.
Definition: lr-wpan-mac.h:72
Time m_macBeaconRxTime
The time that the device received its last bit of the beacon frame.
Definition: lr-wpan-mac.h:742
Implements the header for the MAC payload command frame according to the IEEE 802.15.4-2011 Std.
SequenceNumber8 m_macDsn
Sequence number added to transmitted data or MAC command frame, 00-ff.
Definition: lr-wpan-mac.h:824
void SetBattLifeExt(bool battLifeExt)
Set the Superframe Specification Battery Life Extension (BLE).
MAC_ACK_PENDING.
Definition: lr-wpan-mac.h:74
uint64_t m_rxBeaconSymbols
The total size of the received beacon in symbols.
Definition: lr-wpan-mac.h:786
static const uint32_t aMinMPDUOverhead
The minimum number of octets added by the MAC sublayer to the PSDU.
Definition: lr-wpan-mac.h:462
bool m_macAutoRequest
Indication of whether a device automatically sends data request command if its address is listed in t...
Definition: lr-wpan-mac.h:858
uint32_t m_macSIFSPeriod
The minimum time forming a Short InterFrame Spacing (SIFS) period.
Definition: lr-wpan-mac.h:850
void ChangeMacState(LrWpanMacState newState)
Change the current MAC state to the given new state.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
MLME-SYNC-LOSS.indication params.
Definition: lr-wpan-mac.h:374
Time m_macBeaconTxTime
The time that the device transmitted its last beacon frame.
Definition: lr-wpan-mac.h:735
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:71
uint8_t m_numLostBeacons
The number of consecutive loss beacons in a beacon tracking operation.
Definition: lr-wpan-mac.h:898
Ptr< LrWpanCsmaCa > m_csmaCa
The CSMA/CA implementation used by this MAC.
Definition: lr-wpan-mac.h:1217
LrWpanMacState
MAC states.
Definition: lr-wpan-mac.h:69
LrWpanAssociationStatus
table 83 of 802.15.4
Definition: lr-wpan-mac.h:150
void SetPndAddrFields(PendingAddrFields pndAddrFields)
Set the superframe Pending Address fields to the beacon payload header.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
uint8_t GetMacMaxFrameRetries(void) const
Get the macMaxFrameRetries attribute value.
TracedCallback< Ptr< const Packet > > m_macTxDequeueTrace
The trace source fired when packets are dequeued from the L3/l2 transmission queue.
Definition: lr-wpan-mac.h:1116
uint8_t m_deviceCapability
Indication of current device capability (FFD or RFD)
Definition: lr-wpan-mac.h:890
uint8_t phyCurrentChannel
The RF channel to use.
Definition: lr-wpan-phy.h:159
uint16_t m_macPanId
16 bits id of PAN on which this device is operating.
Definition: lr-wpan-mac.h:819
TracedValue< SuperframeStatus > m_incSuperframeStatus
The current period of the incoming superframe.
Definition: lr-wpan-mac.h:1258
void StartCFP(SuperframeType superframeType)
Called to begin the Contention Free Period (CFP) in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:750
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1294
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:307
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
void PlmeSetTRXStateConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.8 PLME-SET-TRX-STATE.confirm Set PHY state.
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:105
second
Definition: nstime.h:115
Full Functional Device (FFD)
void SetPanCoor(bool panCoor)
Set the Superframe Specification PAN coordinator field.
void SendAck(uint8_t seqno)
Send an acknowledgment packet for the given sequence number.
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:236
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
A base class which provides memory management and object aggregation.
Definition: object.h:87
LrWpanMlmeStartConfirmStatus m_status
The status of a MLME-start.request.
Definition: lr-wpan-mac.h:352
void AckWaitTimeout(void)
Handle an ACK timeout with a packet retransmission, if there are retransmission left, or a packet drop.
bool m_coorRealgn
True if a realignment request command is to be transmitted prior changing the superframe.
Definition: lr-wpan-mac.h:309
Represent the Pending Address Specification field.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Represent the GTS information fields.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
Ptr< LrWpanPhy > GetPhy(void)
Get the underlying PHY of the MAC.
Definition: lr-wpan-mac.cc:952
Mac16Address m_shortAddress
The short address used by this MAC.
Definition: lr-wpan-mac.h:1276
NUMERIC_TYPE GetValue() const
Extracts the numeric value of the sequence number.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
Mac16Address GetShortAddress(void) const
Get the short address of this MAC.
Definition: lr-wpan-mac.cc:275
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
void IfsWaitTimeout(void)
After a successful transmission of a frame (beacon, data) or an ack frame reception, the mac layer wait an Interframe Space (IFS) time and triggers this function to continue with the MAC flow.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
void SetCsmaCa(Ptr< LrWpanCsmaCa > csmaCa)
Set the CSMA/CA implementation to be used by the MAC.
Definition: lr-wpan-mac.cc:940
void SetPanId(uint16_t panId)
Set the PAN id used by this MAC.
void PlmeSetAttributeConfirm(LrWpanPhyEnumeration status, LrWpanPibAttributeIdentifier id)
IEEE 802.15.4-2006 section 6.2.2.10 PLME-SET.confirm Set attributes per definition from Table 23 in s...
Inactive Period or unslotted CSMA-CA.
Definition: lr-wpan-mac.h:93
virtual void DoInitialize(void)
Initialize() implementation.
Definition: lr-wpan-mac.cc:196
static Mac64Address Allocate(void)
Allocate a new Mac64Address.
uint8_t txQMsduHandle
MSDU Handle.
Definition: lr-wpan-mac.h:997
static const uint32_t aBaseSuperframeDuration
Length of a superframe in symbols.
Definition: lr-wpan-mac.h:479
TracedValue< SuperframeStatus > m_outSuperframeStatus
The current period of the outgoing superframe.
Definition: lr-wpan-mac.h:1262
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
MAC_SENDING.
Definition: lr-wpan-mac.h:73
MLME-START.confirm params.
Definition: lr-wpan-mac.h:350
uint16_t m_srcPanId
Source PAN identifier.
Definition: lr-wpan-mac.h:273
MAC_IDLE.
Definition: lr-wpan-mac.h:71
void BeaconSearchTimeout(void)
Called if the device is unable to locate a beacon in the time set by MLME-SYNC.request.
Definition: lr-wpan-mac.cc:846
void SetMlmePollConfirmCallback(MlmePollConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:988
void SetAssociationStatus(LrWpanAssociationStatus status)
Set the current association status.
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:270
Mac64Address GetExtDstAddr(void) const
Get the Destination Extended address.
EventId m_incCfpEvent
Scheduler event for the end of the incoming superframe CFP.
Definition: lr-wpan-mac.h:1331
void SetMcpsDataIndicationCallback(McpsDataIndicationCallback c)
Set the callback for the indication of an incoming data packet.
Definition: lr-wpan-mac.cc:958