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 
196 void
198 {
199  if (m_macRxOnWhenIdle)
200  {
201  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
202  }
203  else
204  {
205  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
206  }
207 
209 }
210 
211 void
213 {
214  if (m_csmaCa != 0)
215  {
216  m_csmaCa->Dispose ();
217  m_csmaCa = 0;
218  }
219  m_txPkt = 0;
220  for (uint32_t i = 0; i < m_txQueue.size (); i++)
221  {
222  m_txQueue[i]->txQPkt = 0;
223  delete m_txQueue[i];
224  }
225  m_txQueue.clear ();
226  m_phy = 0;
227  m_mcpsDataIndicationCallback = MakeNullCallback< void, McpsDataIndicationParams, Ptr<Packet> > ();
228  m_mcpsDataConfirmCallback = MakeNullCallback< void, McpsDataConfirmParams > ();
229 
231 
233 }
234 
235 bool
237 {
238  return m_macRxOnWhenIdle;
239 }
240 
241 void
242 LrWpanMac::SetRxOnWhenIdle (bool rxOnWhenIdle)
243 {
244  NS_LOG_FUNCTION (this << rxOnWhenIdle);
245  m_macRxOnWhenIdle = rxOnWhenIdle;
246 
247  if (m_lrWpanMacState == MAC_IDLE)
248  {
249  if (m_macRxOnWhenIdle)
250  {
251  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
252  }
253  else
254  {
255  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
256  }
257  }
258 }
259 
260 void
262 {
263  //NS_LOG_FUNCTION (this << address);
265 }
266 
267 void
269 {
270  //NS_LOG_FUNCTION (this << address);
271  m_selfExt = address;
272 }
273 
274 
277 {
278  NS_LOG_FUNCTION (this);
279  return m_shortAddress;
280 }
281 
284 {
285  NS_LOG_FUNCTION (this);
286  return m_selfExt;
287 }
288 void
290 {
291  NS_LOG_FUNCTION (this << p);
292 
293  McpsDataConfirmParams confirmParams;
294  confirmParams.m_msduHandle = params.m_msduHandle;
295 
296  // TODO: We need a drop trace for the case that the packet is too large or the request parameters are maleformed.
297  // The current tx drop trace is not suitable, because packets dropped using this trace carry the mac header
298  // and footer, while packets being dropped here do not have them.
299 
301  m_macDsn++;
302 
304  {
305  // Note, this is just testing maximum theoretical frame size per the spec
306  // The frame could still be too large once headers are put on
307  // in which case the phy will reject it instead
308  NS_LOG_ERROR (this << " packet too big: " << p->GetSize ());
309  confirmParams.m_status = IEEE_802_15_4_FRAME_TOO_LONG;
311  {
312  m_mcpsDataConfirmCallback (confirmParams);
313  }
314  return;
315  }
316 
317  if ((params.m_srcAddrMode == NO_PANID_ADDR)
318  && (params.m_dstAddrMode == NO_PANID_ADDR))
319  {
320  NS_LOG_ERROR (this << " Can not send packet with no Address field" );
321  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
323  {
324  m_mcpsDataConfirmCallback (confirmParams);
325  }
326  return;
327  }
328  switch (params.m_srcAddrMode)
329  {
330  case NO_PANID_ADDR:
331  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
332  macHdr.SetNoPanIdComp ();
333  break;
334  case ADDR_MODE_RESERVED:
335  NS_ABORT_MSG ("Can not set source address type to ADDR_MODE_RESERVED. Aborting.");
336  break;
337  case SHORT_ADDR:
338  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
339  macHdr.SetSrcAddrFields (GetPanId (), GetShortAddress ());
340  break;
341  case EXT_ADDR:
342  macHdr.SetSrcAddrMode (params.m_srcAddrMode);
343  macHdr.SetSrcAddrFields (GetPanId (), GetExtendedAddress ());
344  break;
345  default:
346  NS_LOG_ERROR (this << " Can not send packet with incorrect Source Address mode = " << params.m_srcAddrMode);
347  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
349  {
350  m_mcpsDataConfirmCallback (confirmParams);
351  }
352  return;
353  }
354  switch (params.m_dstAddrMode)
355  {
356  case NO_PANID_ADDR:
357  macHdr.SetDstAddrMode (params.m_dstAddrMode);
358  macHdr.SetNoPanIdComp ();
359  break;
360  case ADDR_MODE_RESERVED:
361  NS_ABORT_MSG ("Can not set destination address type to ADDR_MODE_RESERVED. Aborting.");
362  break;
363  case SHORT_ADDR:
364  macHdr.SetDstAddrMode (params.m_dstAddrMode);
365  macHdr.SetDstAddrFields (params.m_dstPanId, params.m_dstAddr);
366  break;
367  case EXT_ADDR:
368  macHdr.SetDstAddrMode (params.m_dstAddrMode);
369  macHdr.SetDstAddrFields (params.m_dstPanId, params.m_dstExtAddr);
370  break;
371  default:
372  NS_LOG_ERROR (this << " Can not send packet with incorrect Destination Address mode = " << params.m_dstAddrMode);
373  confirmParams.m_status = IEEE_802_15_4_INVALID_ADDRESS;
375  {
376  m_mcpsDataConfirmCallback (confirmParams);
377  }
378  return;
379  }
380 
381  macHdr.SetSecDisable ();
382  //extract the first 3 bits in TxOptions
383  int b0 = params.m_txOptions & TX_OPTION_ACK;
384  int b1 = params.m_txOptions & TX_OPTION_GTS;
385  int b2 = params.m_txOptions & TX_OPTION_INDIRECT;
386 
387  if (b0 == TX_OPTION_ACK)
388  {
389  // Set AckReq bit only if the destination is not the broadcast address.
390  if (!(macHdr.GetDstAddrMode () == SHORT_ADDR && macHdr.GetShortDstAddr () == "ff:ff"))
391  {
392  macHdr.SetAckReq ();
393  }
394  }
395  else
396  {
397  macHdr.SetNoAckReq ();
398  }
399 
400 
401  if (b1 == TX_OPTION_GTS)
402  {
403  //TODO:GTS Transmission
404  }
405  else if (b2 == TX_OPTION_INDIRECT)
406  {
407  // Indirect Tx
408  // A COORDINATOR will save the packet in the pending queue and await for data
409  // requests from its associated devices. The devices are aware of pending data,
410  // from the pending bit information extracted from the received beacon.
411  // A DEVICE must be tracking beacons (MLME-SYNC.request is running) before attempting
412  // request data from the coordinator.
413 
414 
415  //TODO: Check if the current device is coordinator or PAN coordinator
416  p->AddHeader (macHdr);
417 
418  LrWpanMacTrailer macTrailer;
419  // Calculate FCS if the global attribute ChecksumEnable is set.
420  if (Node::ChecksumEnabled ())
421  {
422  macTrailer.EnableFcs (true);
423  macTrailer.SetFcs (p);
424  }
425  p->AddTrailer (macTrailer);
426 
427  if (m_txQueue.size () == m_txQueue.max_size ())
428  {
431  {
432  m_mcpsDataConfirmCallback (confirmParams);
433  }
434  }
435  else
436  {
437  IndTxQueueElement *indTxQElement = new IndTxQueueElement;
438  uint64_t unitPeriodSymbols;
439  Time expireTime;
440 
441  if (m_macBeaconOrder == 15)
442  {
443  unitPeriodSymbols = aBaseSuperframeDuration;
444  }
445  else
446  {
447  unitPeriodSymbols = ((uint64_t) 1 << m_macBeaconOrder) * aBaseSuperframeDuration;
448  }
449 
450  //TODO: check possible incorrect expire time here.
451 
453  unitPeriodSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
454  indTxQElement->expireTime = expireTime;
455  indTxQElement->txQMsduHandle = params.m_msduHandle;
456  indTxQElement->txQPkt = p;
457 
458  m_indTxQueue.push_back (indTxQElement);
459 
460  std::cout << "Indirect Transmission Pushed | Elements in the queue: " << m_indTxQueue.size ()
461  << " " << "Element to expire in: " << expireTime.GetSeconds () << "secs\n";
462  }
463  }
464  else
465  {
466  // Direct Tx
467  // From this point the packet will be pushed to a Tx queue and immediately
468  // use a slotted (beacon-enabled) or unslotted (nonbeacon-enabled) version of CSMA/CA
469  // before sending the packet, depending on whether it has previously
470  // received a valid beacon or not.
471 
472  p->AddHeader (macHdr);
473 
474  LrWpanMacTrailer macTrailer;
475  // Calculate FCS if the global attribute ChecksumEnable is set.
476  if (Node::ChecksumEnabled ())
477  {
478  macTrailer.EnableFcs (true);
479  macTrailer.SetFcs (p);
480  }
481  p->AddTrailer (macTrailer);
482 
484 
485  TxQueueElement *txQElement = new TxQueueElement;
486  txQElement->txQMsduHandle = params.m_msduHandle;
487  txQElement->txQPkt = p;
488  m_txQueue.push_back (txQElement);
489  CheckQueue ();
490  }
491 
492 }
493 
494 
495 void
497 {
498  NS_LOG_FUNCTION (this);
500 
501  MlmeStartConfirmParams confirmParams;
502 
503 
504  if (GetShortAddress () == Mac16Address ("ff:ff"))
505  {
506  NS_LOG_ERROR (this << " Invalid MAC short address" );
507  confirmParams.m_status = MLMESTART_NO_SHORT_ADDRESS;
509  {
510  m_mlmeStartConfirmCallback (confirmParams);
511  }
512  return;
513  }
514 
515 
516  if ( (params.m_logCh > 26)
517  || (params.m_bcnOrd > 15)
518  || (params.m_sfrmOrd > params.m_bcnOrd))
519  {
520  NS_LOG_ERROR (this << " One or more parameters are invalid" );
521  confirmParams.m_status = MLMESTART_INVALID_PARAMETER;
523  {
524  m_mlmeStartConfirmCallback (confirmParams);
525  }
526  return;
527  }
528 
529 
530  if (params.m_coorRealgn) //Coordinator Realignment
531  {
532  // TODO:: Send realignment request command frame in CSMA/CA
533  return;
534  }
535  else
536  {
537  if (params.m_panCoor)
538  {
539  m_panCoor = true;
540  m_macPanId = params.m_PanId;
541 
542  // Setting Channel and Page in the LrWpanPhy
543  LrWpanPhyPibAttributes pibAttr;
544  pibAttr.phyCurrentChannel = params.m_logCh;
545  pibAttr.phyCurrentPage = params.m_logChPage;
546  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentChannel,&pibAttr);
547  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentPage,&pibAttr);
548  }
549 
550  NS_ASSERT (params.m_PanId != 0xffff);
551 
552 
553  m_macBeaconOrder = params.m_bcnOrd;
554  if (m_macBeaconOrder == 15)
555  {
556  //Non-beacon enabled PAN
558  m_beaconInterval = 0;
559  m_csmaCa->SetUnSlottedCsmaCa ();
560 
561  confirmParams.m_status = MLMESTART_SUCCESS;
563  {
564  m_mlmeStartConfirmCallback (confirmParams);
565  }
566  }
567  else
568  {
570  m_csmaCa->SetBatteryLifeExtension (params.m_battLifeExt);
571 
572  m_csmaCa->SetSlottedCsmaCa ();
573 
574  //TODO: Calculate the real Final CAP slot (requires GTS implementation)
575  // FinalCapSlot = Superframe duration slots - CFP slots.
576  // In the current implementation the value of the final cap slot is equal to
577  // the total number of possible slots in the superframe (15).
578  m_fnlCapSlot = 15;
579 
582 
583  //TODO: change the beacon sending according to the startTime parameter (if not PAN coordinator)
584 
586  }
587  }
588 }
589 
590 
591 void
593 {
594  NS_LOG_FUNCTION (this);
595  NS_ASSERT (params.m_logCh <= 26 && m_macPanId != 0xffff);
596 
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 = MicroSeconds (searchSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
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 
635 
637  m_macBsn++;
638 
640 
641  Ptr<Packet> beaconPacket = Create <Packet> ();
642 
643 
644 }
645 
646 
647 void
649 {
650  NS_LOG_FUNCTION (this);
652 
654  m_macBsn++;
655  BeaconPayloadHeader macPayload;
656  Ptr<Packet> beaconPacket = Create <Packet> ();
657  LrWpanMacTrailer macTrailer;
658 
659  macHdr.SetDstAddrMode (LrWpanMacHeader::SHORTADDR);
660  macHdr.SetDstAddrFields (GetPanId (),Mac16Address ("ff:ff"));
661 
662  //see IEEE 802.15.4-2011 Section 5.1.2.4
663  if (GetShortAddress () == Mac16Address ("ff:fe"))
664  {
665  macHdr.SetSrcAddrMode (LrWpanMacHeader::EXTADDR);
666  macHdr.SetSrcAddrFields (GetPanId (),GetExtendedAddress ());
667  }
668  else
669  {
670  macHdr.SetSrcAddrMode (LrWpanMacHeader::SHORTADDR);
671  macHdr.SetSrcAddrFields (GetPanId (), GetShortAddress ());
672  }
673 
674 
675  macHdr.SetSecDisable ();
676  macHdr.SetNoAckReq ();
677 
678 
680  macPayload.SetGtsFields (GetGtsFields ());
681  macPayload.SetPndAddrFields (GetPendingAddrFields ());
682 
683 
684 
685  beaconPacket->AddHeader (macPayload);
686  beaconPacket->AddHeader (macHdr);
687 
688 
689  // Calculate FCS if the global attribute ChecksumEnable is set.
690  if (Node::ChecksumEnabled ())
691  {
692  macTrailer.EnableFcs (true);
693  macTrailer.SetFcs (beaconPacket);
694  }
695 
696  beaconPacket->AddTrailer (macTrailer);
697 
698  //Set the Beacon packet to be transmitted
699  m_txPkt = beaconPacket;
700 
701  //The beacon transmission time in symbols.
702  m_macBeaconTxTime = Simulator::Now ().GetSeconds () * m_phy->GetDataOrSymbolRate (false);
703 
704 
706  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
707 
708 }
709 
710 
711 void
713 {
714  uint32_t activeSlot;
715  uint32_t capDuration;
716  Time capTime;
717 
718  if (superframeType == OUTGOING)
719  {
721  activeSlot = m_superframeDuration / 16;
722  capDuration = activeSlot * (m_fnlCapSlot + 1);
723  capTime = Seconds (capDuration / m_phy->GetDataOrSymbolRate (false));
724  NS_LOG_DEBUG ("Outgoing superframe CAP duration " << capDuration << " symbols (" << capTime.GetSeconds () << " s)");
725  NS_LOG_DEBUG ("Active Slots duration " << activeSlot << " symbols");
726 
727  m_capEvent = Simulator::Schedule (MicroSeconds (capTime.GetMicroSeconds ()),
729 
731  }
732  else
733  {
735  activeSlot = m_incomingSuperframeDuration / 16;
736  capDuration = activeSlot * (m_incomingFnlCapSlot + 1);
737  capTime = Seconds (capDuration / m_phy->GetDataOrSymbolRate (false));
738 
739  NS_LOG_DEBUG ("Incoming superframe CAP duration " << capDuration << " symbols (" << capTime.GetSeconds () << " s)");
740  NS_LOG_DEBUG ("Active Slots duration " << activeSlot << " symbols");
741 
742  m_capEvent = Simulator::Schedule (MicroSeconds (capTime.GetMicroSeconds ()),
744  }
745 
746  CheckQueue ();
747 
748 }
749 
750 
751 void
753 {
754  uint32_t activeSlot;
755  uint32_t cfpDuration;
756  Time cfpTime;
757 
758  if (superframeType == INCOMING)
759  {
760  activeSlot = m_incomingSuperframeDuration / 16;
761  cfpDuration = activeSlot * (15 - m_incomingFnlCapSlot);
762  cfpTime = Seconds (cfpDuration / m_phy->GetDataOrSymbolRate (false));
763  if (cfpDuration > 0)
764  {
766  }
767 
768  NS_LOG_DEBUG ("Incoming superframe CFP duration " << cfpDuration << " symbols (" << cfpTime.GetSeconds () << " s)");
769 
770  m_incCfpEvent = Simulator::Schedule (MicroSeconds (cfpTime.GetMicroSeconds ()),
772  }
773  else
774  {
775  activeSlot = m_superframeDuration / 16;
776  cfpDuration = activeSlot * (15 - m_fnlCapSlot);
777  cfpTime = Seconds (cfpDuration / m_phy->GetDataOrSymbolRate (false));
778 
779  if (cfpDuration > 0)
780  {
782  }
783 
784  NS_LOG_DEBUG ("Outgoing superframe CFP duration " << cfpDuration << " symbols (" << cfpTime.GetSeconds () << " s)");
785 
786  m_cfpEvent = Simulator::Schedule (MicroSeconds (cfpTime.GetMicroSeconds ()),
788 
789  }
790  //TODO: Start transmit or receive GTS here.
791 }
792 
793 
794 void
796 {
797  uint32_t inactiveDuration;
798  Time inactiveTime;
799 
800  if (superframeType == INCOMING)
801  {
803  inactiveTime = Seconds (inactiveDuration / m_phy->GetDataOrSymbolRate (false));
804 
805  if (inactiveDuration > 0)
806  {
808  }
809 
810  NS_LOG_DEBUG ("Incoming superframe Inactive duration " << inactiveDuration << " symbols (" << inactiveTime.GetSeconds () << " s)");
811  }
812  else
813  {
814  inactiveDuration = m_beaconInterval - m_superframeDuration;
815  inactiveTime = Seconds (inactiveDuration / m_phy->GetDataOrSymbolRate (false));
816 
817  if (inactiveDuration > 0)
818  {
820  }
821 
822  NS_LOG_DEBUG ("Outgoing superframe Inactive duration " << inactiveDuration << " symbols (" << inactiveTime.GetSeconds () << " s)");
825  }
826 }
827 
828 
829 
830 void
832 {
834  {
835  MlmeSyncLossIndicationParams syncLossParams;
836  //syncLossParams.m_logCh =
837  syncLossParams.m_lossReason = MLMESYNCLOSS_BEACON_LOST;
838  syncLossParams.m_panId = m_macPanId;
839  m_mlmeSyncLossIndicationCallback (syncLossParams);
840 
841  m_beaconTrackingOn = false;
842  m_numLostBeacons = 0;
843  }
844  else
845  {
847 
848  //Search for one more beacon
849  uint64_t searchSymbols;
850  Time searchBeaconTime;
851  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
852  searchBeaconTime = MicroSeconds (searchSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
854 
855  }
856 }
857 
858 
859 void
861 {
862  NS_LOG_FUNCTION (this);
863  // Pull a packet from the queue and start sending if we are not already sending.
864  if (m_lrWpanMacState == MAC_IDLE && !m_txQueue.empty () && m_txPkt == 0 && !m_setMacState.IsRunning ())
865  {
866  TxQueueElement *txQElement = m_txQueue.front ();
867  m_txPkt = txQElement->txQPkt;
868  //TODO: this should check if the node is a coordinator and using the outcoming superframe not just the PAN coordinator
869  if (m_csmaCa->IsUnSlottedCsmaCa () || (m_outSuperframeStatus == CAP && m_panCoor) || m_incSuperframeStatus == CAP)
870  {
872  }
873  }
874 }
875 
876 
879 {
880  SuperframeField sfrmSpec;
881 
882  sfrmSpec.SetBeaconOrder (m_macBeaconOrder);
884  sfrmSpec.SetFinalCapSlot (m_fnlCapSlot);
885 
886  if (m_csmaCa->GetBatteryLifeExtension ())
887  {
888  sfrmSpec.SetBattLifeExt (true);
889  }
890 
891  if (m_panCoor)
892  {
893  sfrmSpec.SetPanCoor (true);
894  }
895  //TODO: It is possible to do association using Beacons,
896  // however, the current implementation only support manual association.
897  // The association permit should be set here.
898 
899  return sfrmSpec;
900 }
901 
902 GtsFields
904 {
905  GtsFields gtsFields;
906 
907  // TODO: Logic to populate the GTS Fields from local information here
908 
909  return gtsFields;
910 }
911 
914 {
915  PendingAddrFields pndAddrFields;
916 
917  // TODO: Logic to populate the Pending Address Fields from local information here
918  return pndAddrFields;
919 }
920 
921 
922 void
924 {
925  m_csmaCa = csmaCa;
926 }
927 
928 void
930 {
931  m_phy = phy;
932 }
933 
936 {
937  return m_phy;
938 }
939 
940 void
942 {
944 }
945 
946 void
948 {
950 }
951 
952 void
954 {
956 }
957 
958 void
960 {
962 }
963 
964 void
966 {
968 }
969 
970 void
972 {
974 }
975 
976 void
977 LrWpanMac::PdDataIndication (uint32_t psduLength, Ptr<Packet> p, uint8_t lqi)
978 {
980  NS_LOG_FUNCTION (this << psduLength << p << (uint16_t)lqi);
981 
982  bool acceptFrame;
983 
984  // from sec 7.5.6.2 Reception and rejection, Std802.15.4-2006
985  // level 1 filtering, test FCS field and reject if frame fails
986  // level 2 filtering if promiscuous mode pass frame to higher layer otherwise perform level 3 filtering
987  // level 3 filtering accept frame
988  // if Frame type and version is not reserved, and
989  // if there is a dstPanId then dstPanId=m_macPanId or broadcastPanI, and
990  // if there is a shortDstAddr then shortDstAddr =shortMacAddr or broadcastAddr, and
991  // if beacon frame then srcPanId = m_macPanId
992  // if only srcAddr field in Data or Command frame,accept frame if srcPanId=m_macPanId
993 
994  Ptr<Packet> originalPkt = p->Copy (); // because we will strip headers
995 
996  m_promiscSnifferTrace (originalPkt);
997 
998  m_macPromiscRxTrace (originalPkt);
999  // XXX no rejection tracing (to macRxDropTrace) being performed below
1000 
1001  LrWpanMacTrailer receivedMacTrailer;
1002  p->RemoveTrailer (receivedMacTrailer);
1003  if (Node::ChecksumEnabled ())
1004  {
1005  receivedMacTrailer.EnableFcs (true);
1006  }
1007 
1008  // level 1 filtering
1009  if (!receivedMacTrailer.CheckFcs (p))
1010  {
1011  m_macRxDropTrace (originalPkt);
1012  }
1013  else
1014  {
1015  LrWpanMacHeader receivedMacHdr;
1016  p->RemoveHeader (receivedMacHdr);
1017 
1018  McpsDataIndicationParams params;
1019  params.m_dsn = receivedMacHdr.GetSeqNum ();
1020  params.m_mpduLinkQuality = lqi;
1021  params.m_srcPanId = receivedMacHdr.GetSrcPanId ();
1022  params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode ();
1023  switch (params.m_srcAddrMode)
1024  {
1025  case SHORT_ADDR:
1026  params.m_srcAddr = receivedMacHdr.GetShortSrcAddr ();
1027  NS_LOG_DEBUG ("Packet from " << params.m_srcAddr);
1028  break;
1029  case EXT_ADDR:
1030  params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr ();
1031  NS_LOG_DEBUG ("Packet from " << params.m_srcExtAddr);
1032  break;
1033  default:
1034  break;
1035  }
1036  params.m_dstPanId = receivedMacHdr.GetDstPanId ();
1037  params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode ();
1038  switch (params.m_dstAddrMode)
1039  {
1040  case SHORT_ADDR:
1041  params.m_dstAddr = receivedMacHdr.GetShortDstAddr ();
1042  NS_LOG_DEBUG ("Packet to " << params.m_dstAddr);
1043  break;
1044  case EXT_ADDR:
1045  params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr ();
1046  NS_LOG_DEBUG ("Packet to " << params.m_dstExtAddr);
1047  break;
1048  default:
1049  break;
1050  }
1051 
1053  {
1054  //level 2 filtering
1055  //TODO: Fix here, this should trigger different Indication Callbacks
1056  //depending the type of frame received (data,command, beacon)
1058  {
1059  NS_LOG_DEBUG ("promiscuous mode, forwarding up");
1060  m_mcpsDataIndicationCallback (params, p);
1061  }
1062  else
1063  {
1064  NS_LOG_ERROR (this << " Data Indication Callback not initialized");
1065  }
1066  }
1067  else
1068  {
1069  //level 3 frame filtering
1070  acceptFrame = (receivedMacHdr.GetType () != LrWpanMacHeader::LRWPAN_MAC_RESERVED);
1071 
1072  if (acceptFrame)
1073  {
1074  acceptFrame = (receivedMacHdr.GetFrameVer () <= 1);
1075  }
1076 
1077  if (acceptFrame
1078  && (receivedMacHdr.GetDstAddrMode () > 1))
1079  {
1080  acceptFrame = receivedMacHdr.GetDstPanId () == m_macPanId
1081  || receivedMacHdr.GetDstPanId () == 0xffff;
1082  }
1083 
1084  if (acceptFrame
1085  && (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR))
1086  {
1087  if (receivedMacHdr.GetShortDstAddr () == m_shortAddress)
1088  {
1089  // unicast, for me
1090  acceptFrame = true;
1091  }
1092  else if (receivedMacHdr.GetShortDstAddr () == Mac16Address ("ff:ff"))
1093  {
1094  // broadcast
1095  acceptFrame = true;
1096  }
1097  else
1098  {
1099  // multicast
1100  uint8_t buf[2];
1101  receivedMacHdr.GetShortDstAddr ().CopyTo (buf);
1102  if (buf[0] & 0x80)
1103  {
1104  acceptFrame = true;
1105  }
1106  }
1107  }
1108 
1109  if (acceptFrame
1110  && (receivedMacHdr.GetDstAddrMode () == EXT_ADDR))
1111  {
1112  acceptFrame = (receivedMacHdr.GetExtDstAddr () == m_selfExt);
1113  }
1114 
1115  if (acceptFrame
1116  && (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_BEACON))
1117  {
1118  if (m_macPanId == 0xffff)
1119  {
1120  // TODO: Accept only if the frame version field is valid
1121  acceptFrame = true;
1122  }
1123  else
1124  {
1125  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId;
1126  }
1127  }
1128 
1129  if (acceptFrame
1130  && ((receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_DATA)
1131  || (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_COMMAND))
1132  && (receivedMacHdr.GetSrcAddrMode () > 1))
1133  {
1134  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId; // TODO: need to check if PAN coord
1135  }
1136 
1137  if (acceptFrame)
1138  {
1139  m_macRxTrace (originalPkt);
1140  // \todo: What should we do if we receive a frame while waiting for an ACK?
1141  // Especially if this frame has the ACK request bit set, should we reply with an ACK, possibly missing the pending ACK?
1142 
1143  // If the received frame is a frame with the ACK request bit set, we immediately send back an ACK.
1144  // If we are currently waiting for a pending ACK, we assume the ACK was lost and trigger a retransmission after sending the ACK.
1145  if ((receivedMacHdr.IsData () || receivedMacHdr.IsCommand ()) && receivedMacHdr.IsAckReq ()
1146  && !(receivedMacHdr.GetDstAddrMode () == SHORT_ADDR && receivedMacHdr.GetShortDstAddr () == "ff:ff"))
1147  {
1148  // If this is a data or mac command frame, which is not a broadcast,
1149  // with ack req set, generate and send an ack frame.
1150  // If there is a CSMA medium access in progress we cancel the medium access
1151  // for sending the ACK frame. A new transmission attempt will be started
1152  // after the ACK was send.
1154  {
1157  }
1158  else if (m_lrWpanMacState == MAC_CSMA)
1159  {
1160  // \todo: If we receive a packet while doing CSMA/CA, should we drop the packet because of channel busy,
1161  // or should we restart CSMA/CA for the packet after sending the ACK?
1162  // Currently we simply restart CSMA/CA after sending the ACK.
1163  m_csmaCa->Cancel ();
1164  }
1165  // Cancel any pending MAC state change, ACKs have higher priority.
1166  m_setMacState.Cancel ();
1168 
1169  m_setMacState = Simulator::ScheduleNow (&LrWpanMac::SendAck, this, receivedMacHdr.GetSeqNum ());
1170  }
1171 
1172  if (receivedMacHdr.IsBeacon ())
1173  {
1174  NS_LOG_DEBUG ("Beacon received");
1175 
1176  //TODO: Handle mlme-scan.request here
1177 
1178  // The Rx time of the beacon in symbols
1179  m_beaconRxTime = Simulator::Now ().GetSeconds () * m_phy->GetDataOrSymbolRate (false);
1180 
1181 
1182  // Device not associated.
1183  if (m_macPanId == 0xffff)
1184  {
1185  //TODO: mlme-associate.request here.
1186  NS_LOG_ERROR (this << "The current device is not associated to any coordinator");
1187  return;
1188  }
1189 
1190 
1191  if (m_macPanId != receivedMacHdr.GetDstPanId ())
1192  {
1193  return;
1194  }
1195 
1196  BeaconPayloadHeader receivedMacPayload;
1197  p->RemoveHeader (receivedMacPayload);
1198 
1199  SuperframeField incomingSuperframe;
1200  incomingSuperframe = receivedMacPayload.GetSuperframeSpecField ();
1201 
1202  m_incomingBeaconOrder = incomingSuperframe.GetBeaconOrder ();
1203  m_incomingSuperframeOrder = incomingSuperframe.GetFrameOrder ();
1204  m_incomingFnlCapSlot = incomingSuperframe.GetFinalCapSlot ();
1205 
1208 
1209  if (incomingSuperframe.IsBattLifeExt ())
1210  {
1211  m_csmaCa->SetBatteryLifeExtension (true);
1212  }
1213  else
1214  {
1215  m_csmaCa->SetBatteryLifeExtension (false);
1216  }
1217 
1218 
1219 
1220  if (m_incomingBeaconOrder < 15 && !m_csmaCa->IsSlottedCsmaCa ())
1221  {
1222  m_csmaCa->SetSlottedCsmaCa ();
1223  }
1224 
1225 
1226 
1227  //TODO: get Incoming frame GTS Fields here
1228 
1229 
1230  //Begin CAP on the current device using info from the Incoming superframe
1232 
1233 
1234  // Send a Beacon notification only if we are not
1235  // automatically sending data command requests or the beacon contains
1236  // a beacon payload in its MAC payload.
1237  // see IEEE 802.15.4-2011 Section 6.2.4.1
1238  if ((m_macAutoRequest == false) || p->GetSize () > 0)
1239  {
1240  //TODO: Add the rest of the MlmeBeaconNotifyIndication params
1242  {
1243  MlmeBeaconNotifyIndicationParams beaconParams;
1244  beaconParams.m_bsn = receivedMacHdr.GetSeqNum ();
1245  m_mlmeBeaconNotifyIndicationCallback (beaconParams,originalPkt);
1246  }
1247  }
1248 
1249  if (m_macAutoRequest)
1250  {
1251  // check if MLME-SYNC.request was previously issued and running
1252  // Sync. is necessary to handle pending messages (indirect transmissions)
1253  if (m_trackingEvent.IsRunning ())
1254  {
1256  m_numLostBeacons = 0;
1257 
1258  if (m_beaconTrackingOn)
1259  {
1260  //if tracking option is on keep tracking the next beacon
1261  uint64_t searchSymbols;
1262  Time searchBeaconTime;
1263 
1264  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
1265  searchBeaconTime = MicroSeconds (searchSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
1267  }
1268 
1269  PendingAddrFields pndAddrFields;
1270  pndAddrFields = receivedMacPayload.GetPndAddrFields ();
1271 
1272  //TODO: Ignore pending request if the address is in the GTS list.
1273  // If the address is not in the GTS list, then check if the address
1274  // is in the short address pending list or in the extended address
1275  // pending list.
1276 
1277 
1278 
1279  }
1280  }
1281  }
1282  else if (receivedMacHdr.IsData () && !m_mcpsDataIndicationCallback.IsNull ())
1283  {
1284  // If it is a data frame, push it up the stack.
1285  NS_LOG_DEBUG ("PdDataIndication(): Packet is for me; forwarding up");
1286  m_mcpsDataIndicationCallback (params, p);
1287  }
1288  else if (receivedMacHdr.IsAcknowledgment () && m_txPkt && m_lrWpanMacState == MAC_ACK_PENDING)
1289  {
1290  LrWpanMacHeader macHdr;
1291  Time ifsWaitTime = MicroSeconds (GetIfsSize () * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
1292  m_txPkt->PeekHeader (macHdr);
1293  if (receivedMacHdr.GetSeqNum () == macHdr.GetSeqNum ())
1294  {
1296  // If it is an ACK with the expected sequence number, finish the transmission
1297  // and notify the upper layer.
1300  {
1301  TxQueueElement *txQElement = m_txQueue.front ();
1302  McpsDataConfirmParams confirmParams;
1303  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1304  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1305  m_mcpsDataConfirmCallback (confirmParams);
1306  }
1308 
1309  // Ack was succesfully received, wait for the Interframe Space (IFS) and then proceed
1311  }
1312  else
1313  {
1314  // 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)
1316  if (!PrepareRetransmission ())
1317  {
1318  m_setMacState.Cancel ();
1320  }
1321  else
1322  {
1323  m_setMacState.Cancel ();
1325  }
1326  }
1327  }
1328  }
1329  else
1330  {
1331  m_macRxDropTrace (originalPkt);
1332  }
1333  }
1334  }
1335 }
1336 
1337 
1338 
1339 void
1340 LrWpanMac::SendAck (uint8_t seqno)
1341 {
1342  NS_LOG_FUNCTION (this << static_cast<uint32_t> (seqno));
1343 
1345 
1346  // Generate a corresponding ACK Frame.
1348  LrWpanMacTrailer macTrailer;
1349  Ptr<Packet> ackPacket = Create<Packet> (0);
1350  ackPacket->AddHeader (macHdr);
1351  // Calculate FCS if the global attribute ChecksumEnable is set.
1352  if (Node::ChecksumEnabled ())
1353  {
1354  macTrailer.EnableFcs (true);
1355  macTrailer.SetFcs (ackPacket);
1356  }
1357  ackPacket->AddTrailer (macTrailer);
1358 
1359  // Enqueue the ACK packet for further processing
1360  // when the transmitter is activated.
1361  m_txPkt = ackPacket;
1362 
1363  // Switch transceiver to TX mode. Proceed sending the Ack on confirm.
1365  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1366 
1367 }
1368 
1369 void
1371 {
1372  TxQueueElement *txQElement = m_txQueue.front ();
1373  Ptr<const Packet> p = txQElement->txQPkt;
1374  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1375 
1376  Ptr<Packet> pkt = p->Copy ();
1377  LrWpanMacHeader hdr;
1378  pkt->RemoveHeader (hdr);
1379  if (hdr.GetShortDstAddr () != Mac16Address ("ff:ff"))
1380  {
1382  }
1383 
1384  txQElement->txQPkt = 0;
1385  delete txQElement;
1386  m_txQueue.pop_front ();
1387  m_txPkt = 0;
1388  m_retransmission = 0;
1389  m_numCsmacaRetry = 0;
1390  m_macTxDequeueTrace (p);
1391 }
1392 
1393 void
1395 {
1396  NS_LOG_FUNCTION (this);
1397 
1398  // TODO: If we are a PAN coordinator and this was an indirect transmission,
1399  // we will not initiate a retransmission. Instead we wait for the data
1400  // being extracted after a new data request command.
1401  if (!PrepareRetransmission ())
1402  {
1404  }
1405  else
1406  {
1408  }
1409 }
1410 
1411 void
1413 {
1414  NS_LOG_DEBUG ("MESSAGE SENT and IFS APPLIED");
1415  m_setMacState.Cancel ();
1417 }
1418 
1419 
1420 bool
1422 {
1423  NS_LOG_FUNCTION (this);
1424 
1426  {
1427  // Maximum number of retransmissions has been reached.
1428  // remove the copy of the packet that was just sent
1429  TxQueueElement *txQElement = m_txQueue.front ();
1430  m_macTxDropTrace (txQElement->txQPkt);
1432  {
1433  McpsDataConfirmParams confirmParams;
1434  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1435  confirmParams.m_status = IEEE_802_15_4_NO_ACK;
1436  m_mcpsDataConfirmCallback (confirmParams);
1437  }
1439  return false;
1440  }
1441  else
1442  {
1443  m_retransmission++;
1444  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1445  // Start next CCA process for this packet.
1446  return true;
1447  }
1448 }
1449 
1450 void
1452 {
1454  NS_LOG_FUNCTION (this << status << m_txQueue.size ());
1455 
1456  LrWpanMacHeader macHdr;
1457  Time ifsWaitTime;
1458 
1459  m_txPkt->PeekHeader (macHdr);
1460 
1461  if (status == IEEE_802_15_4_PHY_SUCCESS)
1462  {
1463  if (!macHdr.IsAcknowledgment ())
1464  {
1465  if (macHdr.IsBeacon ())
1466  {
1467  Time ifsWaitTime = MicroSeconds (GetIfsSize () * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
1468  m_txPkt = 0;
1469  NS_LOG_DEBUG ("Beacon Sent");
1470  MlmeStartConfirmParams mlmeConfirmParams;
1471  mlmeConfirmParams.m_status = MLMESTART_SUCCESS;
1473  {
1474  m_mlmeStartConfirmCallback (mlmeConfirmParams);
1475  }
1476  }
1477  else if (macHdr.IsAckReq ()) // We have sent a regular data packet, check if we have to wait for an ACK.
1478  {
1479  // wait for the ack or the next retransmission timeout
1480  // start retransmission timer
1481  Time waitTime = MicroSeconds (GetMacAckWaitDuration () * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
1484  m_setMacState.Cancel ();
1486  return;
1487  }
1488  else
1489  {
1491  // remove the copy of the packet that was just sent
1493  {
1494  McpsDataConfirmParams confirmParams;
1495  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1496  TxQueueElement *txQElement = m_txQueue.front ();
1497  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1498  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1499  m_mcpsDataConfirmCallback (confirmParams);
1500  }
1501  ifsWaitTime = MicroSeconds (GetIfsSize () * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
1503  }
1504  }
1505  else
1506  {
1507  // We have send an ACK. Clear the packet buffer.
1508  m_txPkt = 0;
1509  }
1510  }
1511  else if (status == IEEE_802_15_4_PHY_UNSPECIFIED)
1512  {
1513 
1514  if (!macHdr.IsAcknowledgment ())
1515  {
1516  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1517  TxQueueElement *txQElement = m_txQueue.front ();
1518  m_macTxDropTrace (txQElement->txQPkt);
1520  {
1521  McpsDataConfirmParams confirmParams;
1522  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1523  confirmParams.m_status = IEEE_802_15_4_FRAME_TOO_LONG;
1524  m_mcpsDataConfirmCallback (confirmParams);
1525  }
1527  }
1528  else
1529  {
1530  NS_LOG_ERROR ("Unable to send ACK");
1531  }
1532  }
1533  else
1534  {
1535  // Something went really wrong. The PHY is not in the correct state for
1536  // data transmission.
1537  NS_FATAL_ERROR ("Transmission attempt failed with PHY status " << status);
1538  }
1539 
1540 
1541  if (!ifsWaitTime.IsZero ())
1542  {
1544  }
1545  else
1546  {
1547  m_setMacState.Cancel ();
1549  }
1550 
1551 }
1552 
1553 void
1555 {
1556  NS_LOG_FUNCTION (this << status);
1557  // Direct this call through the csmaCa object
1558  m_csmaCa->PlmeCcaConfirm (status);
1559 }
1560 
1561 void
1562 LrWpanMac::PlmeEdConfirm (LrWpanPhyEnumeration status, uint8_t energyLevel)
1563 {
1564  NS_LOG_FUNCTION (this << status << energyLevel);
1565 
1566 }
1567 
1568 void
1571  LrWpanPhyPibAttributes* attribute)
1572 {
1573  NS_LOG_FUNCTION (this << status << id << attribute);
1574 }
1575 
1576 void
1578 {
1579  NS_LOG_FUNCTION (this << status);
1580 
1582  {
1583  NS_ASSERT (m_txPkt);
1584 
1585  // Start sending if we are in state SENDING and the PHY transmitter was enabled.
1589  m_phy->PdDataRequest (m_txPkt->GetSize (), m_txPkt);
1590  }
1591  else if (m_lrWpanMacState == MAC_CSMA && (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
1592  {
1593  // Start the CSMA algorithm as soon as the receiver is enabled.
1594  m_csmaCa->Start ();
1595  }
1596  else if (m_lrWpanMacState == MAC_IDLE)
1597  {
1599  // Do nothing special when going idle.
1600  }
1601  else if (m_lrWpanMacState == MAC_ACK_PENDING)
1602  {
1604  }
1605  else
1606  {
1607  // TODO: What to do when we receive an error?
1608  // If we want to transmit a packet, but switching the transceiver on results
1609  // in an error, we have to recover somehow (and start sending again).
1610  NS_FATAL_ERROR ("Error changing transceiver state");
1611  }
1612 }
1613 
1614 void
1617 {
1618  NS_LOG_FUNCTION (this << status << id);
1619 }
1620 
1621 void
1623 {
1624  NS_LOG_FUNCTION (this << "mac state = " << macState);
1625 
1626  McpsDataConfirmParams confirmParams;
1627 
1628  if (macState == MAC_IDLE)
1629  {
1631 
1632  if (m_macRxOnWhenIdle)
1633  {
1634  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1635  }
1636  else
1637  {
1638  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
1639  }
1640 
1641  CheckQueue ();
1642  }
1643  else if (macState == MAC_ACK_PENDING)
1644  {
1646  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1647  }
1648  else if (macState == MAC_CSMA)
1649  {
1651 
1653  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1654  }
1655  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_IDLE)
1656  {
1657  // Channel is idle, set transmitter to TX_ON
1659  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1660  }
1661  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE)
1662  {
1663  NS_ASSERT (m_txPkt);
1664 
1665  // cannot find a clear channel, drop the current packet.
1666  NS_LOG_DEBUG ( this << " cannot find clear channel");
1667  confirmParams.m_msduHandle = m_txQueue.front ()->txQMsduHandle;
1671  {
1672  m_mcpsDataConfirmCallback (confirmParams);
1673  }
1674  // remove the copy of the packet that was just sent
1677  }
1678  else if (m_lrWpanMacState == MAC_CSMA && macState == MAC_CSMA_DEFERRED)
1679  {
1681  m_txPkt = 0;
1682  std::cout << "THIS PACKET WAS DEFERRED! macstate " << m_lrWpanMacState << "\n";
1683  }
1684 }
1685 
1688 {
1689  return m_associationStatus;
1690 }
1691 
1692 void
1694 {
1695  m_associationStatus = status;
1696 }
1697 
1698 uint16_t
1700 {
1701  return m_macPanId;
1702 }
1703 
1704 void
1705 LrWpanMac::SetPanId (uint16_t panId)
1706 {
1707  m_macPanId = panId;
1708 }
1709 
1710 void
1712 {
1713  NS_LOG_LOGIC (this << " change lrwpan mac state from "
1714  << m_lrWpanMacState << " to "
1715  << newState);
1716  m_macStateLogger (m_lrWpanMacState, newState);
1717  m_lrWpanMacState = newState;
1718 }
1719 
1720 uint64_t
1722 {
1723  return m_csmaCa->GetUnitBackoffPeriod () + m_phy->aTurnaroundTime + m_phy->GetPhySHRDuration ()
1724  + ceil (6 * m_phy->GetPhySymbolsPerOctet ());
1725 }
1726 
1727 uint8_t
1729 {
1730  return m_macMaxFrameRetries;
1731 }
1732 
1733 void
1735 {
1736  m_macMaxFrameRetries = retries;
1737 }
1738 
1739 bool
1741 {
1742  NS_ASSERT (m_txPkt);
1743  LrWpanMacHeader macHdr;
1744  m_txPkt->PeekHeader (macHdr);
1745 
1746  if (m_macCoordShortAddress == macHdr.GetShortDstAddr ()
1747  || m_macCoordExtendedAddress == macHdr.GetExtDstAddr ())
1748  {
1749  return true;
1750  }
1751  else
1752  {
1753  std::cout << "ERROR: Packet not for the coordinator!\n";
1754  return false;
1755 
1756  }
1757 
1758 }
1759 
1760 uint32_t
1762 {
1763  NS_ASSERT (m_txPkt);
1764 
1765  if (m_txPkt->GetSize () <= aMaxSIFSFrameSize)
1766  {
1767  return m_macSIFSPeriod;
1768  }
1769  else
1770  {
1771  return m_macLIFSPeriod;
1772  }
1773 }
1774 
1775 void
1777 {
1779 }
1780 
1781 void
1783 {
1785 }
1786 
1787 
1788 uint64_t
1790 {
1791  NS_ASSERT (m_txPkt);
1792  // Sync Header (SHR) + 8 bits PHY header (PHR) + PSDU
1793  return (m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1794  (m_txPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ()));
1795 }
1796 
1797 bool
1799 {
1800  NS_ASSERT (m_txPkt);
1801  LrWpanMacHeader macHdr;
1802  m_txPkt->PeekHeader (macHdr);
1803 
1804  return macHdr.IsAckReq ();
1805 }
1806 
1807 } // namespace ns3
1808 
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:1215
TracedValue< LrWpanMacState > m_lrWpanMacState
The current state of the MAC layer.
Definition: lr-wpan-mac.h:1242
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.
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:102
void CheckQueue(void)
Check the transmission queue.
Definition: lr-wpan-mac.cc:860
LrWpanSyncLossReason m_lossReason
The reason for the lost of synchronization.
Definition: lr-wpan-mac.h:379
#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:495
bool m_beaconTrackingOn
Indication of whether the current device is tracking incoming beacons.
Definition: lr-wpan-mac.h:890
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:489
bool m_trackBcn
True if the mlme sync with the next beacon and attempts to track future beacons.
Definition: lr-wpan-mac.h:325
#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:451
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.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
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:1118
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:276
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:361
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:1238
std::deque< TxQueueElement * > m_txQueue
The transmit queue used by the MAC.
Definition: lr-wpan-mac.h:1273
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:283
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
uint8_t m_logCh
The channel number on which to attempt coordinator synchronization.
Definition: lr-wpan-mac.h:324
void SetMlmeBeaconNotifyIndicationCallback(MlmeBeaconNotifyIndicationCallback c)
Set the callback for the indication of an incoming beacon packet.
Definition: lr-wpan-mac.cc:959
static const uint32_t aNumSuperframeSlots
Number of a superframe slots per superframe.
Definition: lr-wpan-mac.h:476
void SetMlmeStartConfirmCallback(MlmeStartConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:953
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:309
LrWpanAssociationStatus m_associationStatus
The current association status of the MAC layer.
Definition: lr-wpan-mac.h:1254
uint8_t m_incomingFnlCapSlot
Indication of the Slot where the CAP portion of the INCOMING Superframe ends.
Definition: lr-wpan-mac.h:803
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:757
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
Definition: lr-wpan-mac.cc:212
TracedCallback< LrWpanMacState, LrWpanMacState > m_macStateLogger
A trace source that fires when the LrWpanMac changes states.
Definition: lr-wpan-mac.h:1197
void SetShortAddress(Mac16Address address)
Set the short address of this MAC.
Definition: lr-wpan-mac.cc:261
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:792
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:799
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:872
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:1232
void SetMcpsDataConfirmCallback(McpsDataConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:947
Incoming Superframe.
Definition: lr-wpan-mac.h:103
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:1227
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:289
uint8_t m_numCsmacaRetry
The number of CSMA/CA retries used for sending the current packet.
Definition: lr-wpan-mac.h:1286
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:59
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:877
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:1315
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:91
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:1110
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:1258
bool GetRxOnWhenIdle(void)
Check if the receiver will be enabled when the MAC is idle.
Definition: lr-wpan-mac.cc:236
MLME-BEACON-NOTIFY.indication params.
Definition: lr-wpan-mac.h:362
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:769
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:1188
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:1097
uint8_t m_fnlCapSlot
Indication of the Slot where the CAP portion of the OUTGOING Superframe ends.
Definition: lr-wpan-mac.h:786
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:1141
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:496
bool m_panCoor
Indication of whether the current device is the PAN coordinator.
Definition: lr-wpan-mac.h:862
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...
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1390
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:307
bool isTxAckReq(void)
Check if the packet to transmit requires acknowledgment.
bool IsZero(void) const
Definition: nstime.h:294
MlmeBeaconNotifyIndicationCallback m_mlmeBeaconNotifyIndicationCallback
This callback is used to notify incoming beacon packets to the upper layers.
Definition: lr-wpan-mac.h:1210
SuperframeType
Superframe type.
Definition: lr-wpan-mac.h:100
Ptr< Packet > txQPkt
Queued packet.
Definition: lr-wpan-mac.h:982
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:303
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:1291
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:1125
EventId m_ifsEvent
Scheduler event for Interframe spacing wait time.
Definition: lr-wpan-mac.h:1299
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:965
mac
Definition: third.py:99
EventId m_setMacState
Scheduler event for a deferred MAC state change.
Definition: lr-wpan-mac.h:1295
SuperframeField GetSuperframeField(void)
Constructs a Superframe specification field from the local information, the superframe Specification ...
Definition: lr-wpan-mac.cc:878
PendingAddrFields GetPendingAddrFields(void)
Constructs Pending Address Fields from the local information, the Pending Address Fields are part of ...
Definition: lr-wpan-mac.cc:913
Helper structure for managing transmission queue elements.
Definition: lr-wpan-mac.h:979
bool m_macRxOnWhenIdle
Indication of whether the MAC sublayer is to enable its receiver during idle periods.
Definition: lr-wpan-mac.h:836
Outgoing Superframe.
Definition: lr-wpan-mac.h:102
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:592
uint64_t m_macBeaconTxTime
The time that the device transmitted its last beacon frame, in symbol periods.
Definition: lr-wpan-mac.h:738
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.
uint64_t m_beaconRxTime
The time that the device received its last beacon frame, in symbol periods.
Definition: lr-wpan-mac.h:743
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:252
Ptr< Packet > txQPkt
Queued packet.
Definition: lr-wpan-mac.h:991
void StartInactivePeriod(SuperframeType superframeType)
Start the Inactive Period in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:795
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:648
uint32_t m_macLIFSPeriod
The minimum time forming a Long InterFrame Spacing (LIFS) period.
Definition: lr-wpan-mac.h:841
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:306
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:929
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:903
Helper structure for managing indirect transmission queue elements.
Definition: lr-wpan-mac.h:988
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:1090
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:977
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:302
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:1282
EventId m_trackingEvent
Scheduler event to track the incoming beacons.
Definition: lr-wpan-mac.h:1323
int64_t GetMicroSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:369
static const uint32_t aBaseSlotDuration
Length of a superframe slot in symbols.
Definition: lr-wpan-mac.h:471
Mac64Address m_selfExt
The extended address used by this MAC.
Definition: lr-wpan-mac.h:1269
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:1311
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:782
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:1149
void StartCAP(SuperframeType superframeType)
Called to begin the Contention Access Period (CAP) in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:712
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:242
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:867
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:1133
bool m_macPromiscuousMode
Indicates if MAC sublayer is in receive all mode.
Definition: lr-wpan-mac.h:809
Mac16Address m_srcAddr
Source address.
Definition: lr-wpan-mac.h:274
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1579
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:1221
SequenceNumber8 m_macBsn
Sequence number added to transmitted beacon frame, 00-ff.
Definition: lr-wpan-mac.h:825
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:193
uint8_t m_macSuperframeOrder
Used by a PAN coordinator or coordinator.
Definition: lr-wpan-mac.h:776
EventId m_capEvent
Scheduler event for the end of the outgoing superframe CAP.
Definition: lr-wpan-mac.h:1307
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:1277
Mac16Address m_macCoordShortAddress
The short address of the coordinator through which the device is associated.
Definition: lr-wpan-mac.h:751
LrWpanMac(void)
Default constructor.
Definition: lr-wpan-mac.cc:147
uint8_t m_bsn
The beacon sequence number.
Definition: lr-wpan-mac.h:364
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
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:1168
void SetExtendedAddress(Mac64Address address)
Set the extended address of this MAC.
Definition: lr-wpan-mac.cc:268
uint8_t m_macMaxFrameRetries
The maximum number of retries allowed after a transmission failure.
Definition: lr-wpan-mac.h:830
uint16_t m_panId
The PAN identifier with which the device lost synchronization or to which it was realigned.
Definition: lr-wpan-mac.h:380
uint8_t txQMsduHandle
MSDU Handle.
Definition: lr-wpan-mac.h:981
uint16_t GetSrcPanId(void) const
Get the Source PAN ID.
Contention Access Period.
Definition: lr-wpan-mac.h:90
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:304
uint32_t m_incomingSuperframeDuration
Indication of the superframe duration in symbols (e.g.
Definition: lr-wpan-mac.h:882
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:1201
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:1303
bool IsBeacon(void) const
Returns true if the header is a beacon.
MLME-POLL.request params.
Definition: lr-wpan-mac.h:332
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:858
MLME-SYNC.request params.
Definition: lr-wpan-mac.h:317
Time expireTime
The expiration time of the packet in the indirect transmission queue.
Definition: lr-wpan-mac.h:992
MAC_CSMA.
Definition: lr-wpan-mac.h:72
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:820
void SetBattLifeExt(bool battLifeExt)
Set the Superframe Specification Battery Life Extension (BLE).
MAC_ACK_PENDING.
Definition: lr-wpan-mac.h:74
static const uint32_t aMinMPDUOverhead
The minimum number of octets added by the MAC sublayer to the PSDU.
Definition: lr-wpan-mac.h:465
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:854
uint32_t m_macSIFSPeriod
The minimum time forming a Short InterFrame Spacing (SIFS) period.
Definition: lr-wpan-mac.h:846
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:1070
MLME-SYNC-LOSS.indication params.
Definition: lr-wpan-mac.h:377
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
uint8_t m_numLostBeacons
The number of consecutive loss beacons in a beacon tracking operation.
Definition: lr-wpan-mac.h:894
Ptr< LrWpanCsmaCa > m_csmaCa
The CSMA/CA implementation used by this MAC.
Definition: lr-wpan-mac.h:1205
LrWpanMacState
MAC states.
Definition: lr-wpan-mac.h:69
LrWpanAssociationStatus
table 83 of 802.15.4
Definition: lr-wpan-mac.h:149
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:1104
uint8_t m_deviceCapability
Indication of current device capability (FFD or RFD)
Definition: lr-wpan-mac.h:886
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:815
TracedValue< SuperframeStatus > m_incSuperframeStatus
The current period of the incoming superframe.
Definition: lr-wpan-mac.h:1246
void StartCFP(SuperframeType superframeType)
Called to begin the Contention Free Period (CFP) in a beacon-enabled mode.
Definition: lr-wpan-mac.cc:752
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1086
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:308
SequenceNumber< uint8_t, int8_t > SequenceNumber8
8 bit Sequence number.
#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
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:235
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:355
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:310
Represent the Pending Address Specification field.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1372
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:935
Mac16Address m_shortAddress
The short address used by this MAC.
Definition: lr-wpan-mac.h:1264
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:187
Mac16Address GetShortAddress(void) const
Get the short address of this MAC.
Definition: lr-wpan-mac.cc:276
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:923
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:92
virtual void DoInitialize(void)
Initialize() implementation.
Definition: lr-wpan-mac.cc:197
static Mac64Address Allocate(void)
Allocate a new Mac64Address.
uint8_t txQMsduHandle
MSDU Handle.
Definition: lr-wpan-mac.h:990
static const uint32_t aBaseSuperframeDuration
Length of a superframe in symbols.
Definition: lr-wpan-mac.h:482
TracedValue< SuperframeStatus > m_outSuperframeStatus
The current period of the outgoing superframe.
Definition: lr-wpan-mac.h:1250
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:353
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:831
void SetMlmePollConfirmCallback(MlmePollConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
Definition: lr-wpan-mac.cc:971
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:1319
void SetMcpsDataIndicationCallback(McpsDataIndicationCallback c)
Set the callback for the indication of an incoming data packet.
Definition: lr-wpan-mac.cc:941