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)
390  {
391  // short address and ACK requested.
392  Mac16Address shortAddr = macHdr.GetShortDstAddr ();
393  if (shortAddr.IsBroadcast() || shortAddr.IsMulticast())
394  {
395  NS_LOG_LOGIC ("LrWpanMac::McpsDataRequest: requested an ACK on broadcast or multicast destination (" << shortAddr << ") - forcefully removing it.");
396  macHdr.SetNoAckReq ();
397  params.m_txOptions &= ~uint8_t (TX_OPTION_ACK);
398  }
399  else
400  {
401  macHdr.SetAckReq ();
402  }
403  }
404  else
405  {
406  // other address (not short) and ACK requested
407  macHdr.SetAckReq ();
408  }
409  }
410  else
411  {
412  macHdr.SetNoAckReq ();
413  }
414 
415 
416  if (b1 == TX_OPTION_GTS)
417  {
418  //TODO:GTS Transmission
419  }
420  else if (b2 == TX_OPTION_INDIRECT)
421  {
422  // Indirect Tx
423  // A COORDINATOR will save the packet in the pending queue and await for data
424  // requests from its associated devices. The devices are aware of pending data,
425  // from the pending bit information extracted from the received beacon.
426  // A DEVICE must be tracking beacons (MLME-SYNC.request is running) before attempting
427  // request data from the coordinator.
428 
429 
430  //TODO: Check if the current device is coordinator or PAN coordinator
431  p->AddHeader (macHdr);
432 
433  LrWpanMacTrailer macTrailer;
434  // Calculate FCS if the global attribute ChecksumEnable is set.
435  if (Node::ChecksumEnabled ())
436  {
437  macTrailer.EnableFcs (true);
438  macTrailer.SetFcs (p);
439  }
440  p->AddTrailer (macTrailer);
441 
442  if (m_txQueue.size () == m_txQueue.max_size ())
443  {
446  {
447  m_mcpsDataConfirmCallback (confirmParams);
448  }
449  }
450  else
451  {
452  IndTxQueueElement *indTxQElement = new IndTxQueueElement;
453  uint64_t unitPeriodSymbols;
454  Time expireTime;
455 
456  if (m_macBeaconOrder == 15)
457  {
458  unitPeriodSymbols = aBaseSuperframeDuration;
459  }
460  else
461  {
462  unitPeriodSymbols = ((uint64_t) 1 << m_macBeaconOrder) * aBaseSuperframeDuration;
463  }
464 
465  //TODO: check possible incorrect expire time here.
466 
468  * MicroSeconds (unitPeriodSymbols * 1000 * 1000 / m_phy->GetDataOrSymbolRate (false));
469  indTxQElement->expireTime = expireTime;
470  indTxQElement->txQMsduHandle = params.m_msduHandle;
471  indTxQElement->txQPkt = p;
472 
473  m_indTxQueue.push_back (indTxQElement);
474 
475  std::cout << "Indirect Transmission Pushed | Elements in the queue: " << m_indTxQueue.size ()
476  << " " << "Element to expire in: " << expireTime.GetSeconds () << "secs\n";
477  }
478  }
479  else
480  {
481  // Direct Tx
482  // From this point the packet will be pushed to a Tx queue and immediately
483  // use a slotted (beacon-enabled) or unslotted (nonbeacon-enabled) version of CSMA/CA
484  // before sending the packet, depending on whether it has previously
485  // received a valid beacon or not.
486 
487  p->AddHeader (macHdr);
488 
489  LrWpanMacTrailer macTrailer;
490  // Calculate FCS if the global attribute ChecksumEnable is set.
491  if (Node::ChecksumEnabled ())
492  {
493  macTrailer.EnableFcs (true);
494  macTrailer.SetFcs (p);
495  }
496  p->AddTrailer (macTrailer);
497 
499 
500  TxQueueElement *txQElement = new TxQueueElement;
501  txQElement->txQMsduHandle = params.m_msduHandle;
502  txQElement->txQPkt = p;
503  m_txQueue.push_back (txQElement);
504  CheckQueue ();
505  }
506 
507 }
508 
509 
510 void
512 {
513  NS_LOG_FUNCTION (this);
515 
516  MlmeStartConfirmParams confirmParams;
517 
518 
519  if (GetShortAddress () == Mac16Address ("ff:ff"))
520  {
521  NS_LOG_ERROR (this << " Invalid MAC short address" );
522  confirmParams.m_status = MLMESTART_NO_SHORT_ADDRESS;
524  {
525  m_mlmeStartConfirmCallback (confirmParams);
526  }
527  return;
528  }
529 
530 
531  if ( (params.m_logCh > 26)
532  || (params.m_bcnOrd > 15)
533  || (params.m_sfrmOrd > params.m_bcnOrd))
534  {
535  NS_LOG_ERROR (this << " One or more parameters are invalid" );
536  confirmParams.m_status = MLMESTART_INVALID_PARAMETER;
538  {
539  m_mlmeStartConfirmCallback (confirmParams);
540  }
541  return;
542  }
543 
544 
545  if (params.m_coorRealgn) //Coordinator Realignment
546  {
547  // TODO:: Send realignment request command frame in CSMA/CA
548  return;
549  }
550  else
551  {
552  if (params.m_panCoor)
553  {
554  m_panCoor = true;
555  m_macPanId = params.m_PanId;
556 
557  // Setting Channel and Page in the LrWpanPhy
558  LrWpanPhyPibAttributes pibAttr;
559  pibAttr.phyCurrentChannel = params.m_logCh;
560  pibAttr.phyCurrentPage = params.m_logChPage;
561  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentChannel,&pibAttr);
562  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentPage,&pibAttr);
563  }
564 
565  NS_ASSERT (params.m_PanId != 0xffff);
566 
567 
568  m_macBeaconOrder = params.m_bcnOrd;
569  if (m_macBeaconOrder == 15)
570  {
571  //Non-beacon enabled PAN
573  m_beaconInterval = 0;
574  m_csmaCa->SetUnSlottedCsmaCa ();
575 
576  confirmParams.m_status = MLMESTART_SUCCESS;
578  {
579  m_mlmeStartConfirmCallback (confirmParams);
580  }
581  }
582  else
583  {
585  m_csmaCa->SetBatteryLifeExtension (params.m_battLifeExt);
586 
587  m_csmaCa->SetSlottedCsmaCa ();
588 
589  //TODO: Calculate the real Final CAP slot (requires GTS implementation)
590  // FinalCapSlot = Superframe duration slots - CFP slots.
591  // In the current implementation the value of the final cap slot is equal to
592  // the total number of possible slots in the superframe (15).
593  m_fnlCapSlot = 15;
594 
597 
598  //TODO: change the beacon sending according to the startTime parameter (if not PAN coordinator)
599 
601  }
602  }
603 }
604 
605 
606 void
608 {
609  NS_LOG_FUNCTION (this);
610  NS_ASSERT (params.m_logCh <= 26 && m_macPanId != 0xffff);
611 
612  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
613  //change phy current logical channel
614  LrWpanPhyPibAttributes pibAttr;
615  pibAttr.phyCurrentChannel = params.m_logCh;
616  m_phy->PlmeSetAttributeRequest (LrWpanPibAttributeIdentifier::phyCurrentChannel,&pibAttr);
617 
618  //Enable Phy receiver
619  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
620 
621  uint64_t searchSymbols;
622  Time searchBeaconTime;
623 
624  if (m_trackingEvent.IsRunning ())
625  {
627  }
628 
629  if (params.m_trackBcn)
630  {
631  m_numLostBeacons = 0;
632  //search for a beacon for a time = incomingSuperframe symbols + 960 symbols
633  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
634  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
635  m_beaconTrackingOn = true;
637  }
638  else
639  {
640  m_beaconTrackingOn = false;
641  }
642 
643 }
644 
645 
646 void
648 {
649  NS_LOG_FUNCTION (this);
650 
652  m_macBsn++;
653 
655 
656  Ptr<Packet> beaconPacket = Create <Packet> ();
657 }
658 
659 
660 void
662 {
663  NS_LOG_FUNCTION (this);
665 
667  m_macBsn++;
668  BeaconPayloadHeader macPayload;
669  Ptr<Packet> beaconPacket = Create <Packet> ();
670  LrWpanMacTrailer macTrailer;
671 
672  macHdr.SetDstAddrMode (LrWpanMacHeader::SHORTADDR);
673  macHdr.SetDstAddrFields (GetPanId (),Mac16Address ("ff:ff"));
674 
675  //see IEEE 802.15.4-2011 Section 5.1.2.4
676  if (GetShortAddress () == Mac16Address ("ff:fe"))
677  {
678  macHdr.SetSrcAddrMode (LrWpanMacHeader::EXTADDR);
679  macHdr.SetSrcAddrFields (GetPanId (),GetExtendedAddress ());
680  }
681  else
682  {
683  macHdr.SetSrcAddrMode (LrWpanMacHeader::SHORTADDR);
684  macHdr.SetSrcAddrFields (GetPanId (), GetShortAddress ());
685  }
686 
687  macHdr.SetSecDisable ();
688  macHdr.SetNoAckReq ();
689 
691  macPayload.SetGtsFields (GetGtsFields ());
692  macPayload.SetPndAddrFields (GetPendingAddrFields ());
693 
694  beaconPacket->AddHeader (macPayload);
695  beaconPacket->AddHeader (macHdr);
696 
697  // Calculate FCS if the global attribute ChecksumEnable is set.
698  if (Node::ChecksumEnabled ())
699  {
700  macTrailer.EnableFcs (true);
701  macTrailer.SetFcs (beaconPacket);
702  }
703 
704  beaconPacket->AddTrailer (macTrailer);
705 
706  //Set the Beacon packet to be transmitted
707  m_txPkt = beaconPacket;
708 
710 
711  NS_LOG_DEBUG ("Outgoing superframe Active Portion (Beacon + CAP + CFP): " << m_superframeDuration << " symbols");
712 
714  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
715 }
716 
717 
718 void
720 {
721  uint32_t activeSlot;
722  uint64_t capDuration;
723  Time endCapTime;
724  uint64_t symbolRate;
725 
726  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
727 
728  if (superframeType == OUTGOING)
729  {
731  activeSlot = m_superframeDuration / 16;
732  capDuration = activeSlot * (m_fnlCapSlot + 1);
733  endCapTime = Seconds ((double) capDuration / symbolRate);
734  // Obtain the end of the CAP by adjust the time it took to send the beacon
735  endCapTime -= (Simulator::Now () - m_macBeaconTxTime);
736 
737  NS_LOG_DEBUG ("Outgoing 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  else
745  {
747  activeSlot = m_incomingSuperframeDuration / 16;
748  capDuration = activeSlot * (m_incomingFnlCapSlot + 1);
749  endCapTime = Seconds ((double) capDuration / symbolRate);
750  // Obtain the end of the CAP by adjust the time it took to receive the beacon
751  endCapTime -= (Simulator::Now () - m_macBeaconRxTime);
752 
753  NS_LOG_DEBUG ("Incoming superframe CAP duration " << (endCapTime.GetSeconds () * symbolRate) << " symbols (" << endCapTime.As (Time::S) << ")");
754  NS_LOG_DEBUG ("Active Slots duration " << activeSlot << " symbols");
755 
756  m_capEvent = Simulator::Schedule (endCapTime,
758  }
759 
760  CheckQueue ();
761 
762 }
763 
764 
765 void
767 {
768  uint32_t activeSlot;
769  uint64_t cfpDuration;
770  Time endCfpTime;
771  uint64_t symbolRate;
772 
773  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
774 
775  if (superframeType == INCOMING)
776  {
777  activeSlot = m_incomingSuperframeDuration / 16;
778  cfpDuration = activeSlot * (15 - m_incomingFnlCapSlot);
779  endCfpTime = Seconds ((double) cfpDuration / symbolRate);
780  if (cfpDuration > 0)
781  {
783  }
784 
785  NS_LOG_DEBUG ("Incoming superframe CFP duration " << cfpDuration << " symbols (" << endCfpTime.As (Time::S) << ")");
786 
787  m_incCfpEvent = Simulator::Schedule (endCfpTime,
789  }
790  else
791  {
792  activeSlot = m_superframeDuration / 16;
793  cfpDuration = activeSlot * (15 - m_fnlCapSlot);
794  endCfpTime = Seconds ((double) cfpDuration / symbolRate);
795 
796  if (cfpDuration > 0)
797  {
799  }
800 
801  NS_LOG_DEBUG ("Outgoing superframe CFP duration " << cfpDuration << " symbols (" << endCfpTime.As (Time::S) << ")");
802 
803  m_cfpEvent = Simulator::Schedule (endCfpTime,
805 
806  }
807  //TODO: Start transmit or receive GTS here.
808 }
809 
810 
811 void
813 {
814  uint64_t inactiveDuration;
815  Time endInactiveTime;
816  uint64_t symbolRate;
817 
818  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
819 
820  if (superframeType == INCOMING)
821  {
823  endInactiveTime = Seconds ((double) inactiveDuration / symbolRate);
824 
825  if (inactiveDuration > 0)
826  {
828  }
829 
830  NS_LOG_DEBUG ("Incoming superframe Inactive Portion duration " << inactiveDuration << " symbols (" << endInactiveTime.As (Time::S) << ")");
831  m_beaconEvent = Simulator::Schedule (endInactiveTime, &LrWpanMac::AwaitBeacon, this);
832  }
833  else
834  {
835  inactiveDuration = m_beaconInterval - m_superframeDuration;
836  endInactiveTime = Seconds ((double) inactiveDuration / symbolRate);
837 
838  if (inactiveDuration > 0)
839  {
841  }
842 
843  NS_LOG_DEBUG ("Outgoing superframe Inactive Portion duration " << inactiveDuration << " symbols (" << endInactiveTime.As (Time::S) << ")");
844  m_beaconEvent = Simulator::Schedule (endInactiveTime, &LrWpanMac::SendOneBeacon, this);
845  }
846 }
847 
848 void
850 {
852 
853  //TODO: If the device waits more than the expected time to receive the beacon (wait = 46 symbols for default beacon size)
854  // it should continue with the start of the incoming CAP even if it did not receive the beacon.
855  // At the moment, the start of the incoming CAP is only triggered if the beacon is received.
856  // See MLME-SyncLoss for details.
857 
858 
859 }
860 
861 void
863 {
864  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
865 
867  {
868  MlmeSyncLossIndicationParams syncLossParams;
869  //syncLossParams.m_logCh =
870  syncLossParams.m_lossReason = MLMESYNCLOSS_BEACON_LOST;
871  syncLossParams.m_panId = m_macPanId;
872  m_mlmeSyncLossIndicationCallback (syncLossParams);
873 
874  m_beaconTrackingOn = false;
875  m_numLostBeacons = 0;
876  }
877  else
878  {
880 
881  //Search for one more beacon
882  uint64_t searchSymbols;
883  Time searchBeaconTime;
884  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
885  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
887 
888  }
889 }
890 
891 
892 void
894 {
895  NS_LOG_FUNCTION (this);
896  // Pull a packet from the queue and start sending if we are not already sending.
897  if (m_lrWpanMacState == MAC_IDLE && !m_txQueue.empty () && !m_setMacState.IsRunning ())
898  {
899  //TODO: this should check if the node is a coordinator and using the outcoming superframe not just the PAN coordinator
900  if (m_csmaCa->IsUnSlottedCsmaCa () || (m_outSuperframeStatus == CAP && m_panCoor) || m_incSuperframeStatus == CAP)
901  {
902  TxQueueElement *txQElement = m_txQueue.front ();
903  m_txPkt = txQElement->txQPkt;
905  }
906  }
907 }
908 
909 
912 {
913  SuperframeField sfrmSpec;
914 
915  sfrmSpec.SetBeaconOrder (m_macBeaconOrder);
917  sfrmSpec.SetFinalCapSlot (m_fnlCapSlot);
918 
919  if (m_csmaCa->GetBatteryLifeExtension ())
920  {
921  sfrmSpec.SetBattLifeExt (true);
922  }
923 
924  if (m_panCoor)
925  {
926  sfrmSpec.SetPanCoor (true);
927  }
928  //TODO: It is possible to do association using Beacons,
929  // however, the current implementation only support manual association.
930  // The association permit should be set here.
931 
932  return sfrmSpec;
933 }
934 
935 GtsFields
937 {
938  GtsFields gtsFields;
939 
940  // TODO: Logic to populate the GTS Fields from local information here
941 
942  return gtsFields;
943 }
944 
947 {
948  PendingAddrFields pndAddrFields;
949 
950  // TODO: Logic to populate the Pending Address Fields from local information here
951  return pndAddrFields;
952 }
953 
954 
955 void
957 {
958  m_csmaCa = csmaCa;
959 }
960 
961 void
963 {
964  m_phy = phy;
965 }
966 
969 {
970  return m_phy;
971 }
972 
973 void
975 {
977 }
978 
979 void
981 {
983 }
984 
985 void
987 {
989 }
990 
991 void
993 {
995 }
996 
997 void
999 {
1001 }
1002 
1003 void
1005 {
1007 }
1008 
1009 void
1010 LrWpanMac::PdDataIndication (uint32_t psduLength, Ptr<Packet> p, uint8_t lqi)
1011 {
1013  NS_LOG_FUNCTION (this << psduLength << p << (uint16_t)lqi);
1014 
1015  bool acceptFrame;
1016 
1017  // from sec 7.5.6.2 Reception and rejection, Std802.15.4-2006
1018  // level 1 filtering, test FCS field and reject if frame fails
1019  // level 2 filtering if promiscuous mode pass frame to higher layer otherwise perform level 3 filtering
1020  // level 3 filtering accept frame
1021  // if Frame type and version is not reserved, and
1022  // if there is a dstPanId then dstPanId=m_macPanId or broadcastPanI, and
1023  // if there is a shortDstAddr then shortDstAddr =shortMacAddr or broadcastAddr, and
1024  // if beacon frame then srcPanId = m_macPanId
1025  // if only srcAddr field in Data or Command frame,accept frame if srcPanId=m_macPanId
1026 
1027  Ptr<Packet> originalPkt = p->Copy (); // because we will strip headers
1028  uint64_t symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
1029  m_promiscSnifferTrace (originalPkt);
1030 
1031  m_macPromiscRxTrace (originalPkt);
1032  // XXX no rejection tracing (to macRxDropTrace) being performed below
1033 
1034  LrWpanMacTrailer receivedMacTrailer;
1035  p->RemoveTrailer (receivedMacTrailer);
1036  if (Node::ChecksumEnabled ())
1037  {
1038  receivedMacTrailer.EnableFcs (true);
1039  }
1040 
1041  // level 1 filtering
1042  if (!receivedMacTrailer.CheckFcs (p))
1043  {
1044  m_macRxDropTrace (originalPkt);
1045  }
1046  else
1047  {
1048  LrWpanMacHeader receivedMacHdr;
1049  p->RemoveHeader (receivedMacHdr);
1050 
1051  McpsDataIndicationParams params;
1052  params.m_dsn = receivedMacHdr.GetSeqNum ();
1053  params.m_mpduLinkQuality = lqi;
1054  params.m_srcPanId = receivedMacHdr.GetSrcPanId ();
1055  params.m_srcAddrMode = receivedMacHdr.GetSrcAddrMode ();
1056  switch (params.m_srcAddrMode)
1057  {
1058  case SHORT_ADDR:
1059  params.m_srcAddr = receivedMacHdr.GetShortSrcAddr ();
1060  break;
1061  case EXT_ADDR:
1062  params.m_srcExtAddr = receivedMacHdr.GetExtSrcAddr ();
1063  break;
1064  default:
1065  break;
1066  }
1067  params.m_dstPanId = receivedMacHdr.GetDstPanId ();
1068  params.m_dstAddrMode = receivedMacHdr.GetDstAddrMode ();
1069  switch (params.m_dstAddrMode)
1070  {
1071  case SHORT_ADDR:
1072  params.m_dstAddr = receivedMacHdr.GetShortDstAddr ();
1073  break;
1074  case EXT_ADDR:
1075  params.m_dstExtAddr = receivedMacHdr.GetExtDstAddr ();
1076  break;
1077  default:
1078  break;
1079  }
1080 
1082  {
1083  //level 2 filtering
1084  if (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR)
1085  {
1086  NS_LOG_DEBUG ("Packet from " << params.m_srcAddr);
1087  NS_LOG_DEBUG ("Packet to " << params.m_dstAddr);
1088  }
1089  else if (receivedMacHdr.GetDstAddrMode () == EXT_ADDR)
1090  {
1091  NS_LOG_DEBUG ("Packet from " << params.m_srcExtAddr);
1092  NS_LOG_DEBUG ("Packet to " << params.m_dstExtAddr);
1093  }
1094 
1095  //TODO: Fix here, this should trigger different Indication Callbacks
1096  //depending the type of frame received (data,command, beacon)
1098  {
1099  NS_LOG_DEBUG ("promiscuous mode, forwarding up");
1100  m_mcpsDataIndicationCallback (params, p);
1101  }
1102  else
1103  {
1104  NS_LOG_ERROR (this << " Data Indication Callback not initialized");
1105  }
1106  }
1107  else
1108  {
1109  //level 3 frame filtering
1110  acceptFrame = (receivedMacHdr.GetType () != LrWpanMacHeader::LRWPAN_MAC_RESERVED);
1111 
1112  if (acceptFrame)
1113  {
1114  acceptFrame = (receivedMacHdr.GetFrameVer () <= 1);
1115  }
1116 
1117  if (acceptFrame
1118  && (receivedMacHdr.GetDstAddrMode () > 1))
1119  {
1120  acceptFrame = receivedMacHdr.GetDstPanId () == m_macPanId
1121  || receivedMacHdr.GetDstPanId () == 0xffff;
1122  }
1123 
1124  if (acceptFrame
1125  && (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR))
1126  {
1127  if (receivedMacHdr.GetShortDstAddr () == m_shortAddress)
1128  {
1129  // unicast, for me
1130  acceptFrame = true;
1131  }
1132  else if (receivedMacHdr.GetShortDstAddr ().IsBroadcast () || receivedMacHdr.GetShortDstAddr ().IsMulticast ())
1133  {
1134  // broadcast or multicast
1135  if (receivedMacHdr.IsAckReq ())
1136  {
1137  // discard broadcast/multicast with the ACK bit set
1138  acceptFrame = false;
1139  }
1140  else
1141  {
1142  acceptFrame = true;
1143  }
1144  }
1145  else
1146  {
1147  acceptFrame = false;
1148  }
1149  }
1150 
1151  if (acceptFrame
1152  && (receivedMacHdr.GetDstAddrMode () == EXT_ADDR))
1153  {
1154  acceptFrame = (receivedMacHdr.GetExtDstAddr () == m_selfExt);
1155  }
1156 
1157  if (acceptFrame
1158  && (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_BEACON))
1159  {
1160  if (m_macPanId == 0xffff)
1161  {
1162  // TODO: Accept only if the frame version field is valid
1163  acceptFrame = true;
1164  }
1165  else
1166  {
1167  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId;
1168  }
1169  }
1170 
1171  if (acceptFrame
1172  && ((receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_DATA)
1173  || (receivedMacHdr.GetType () == LrWpanMacHeader::LRWPAN_MAC_COMMAND))
1174  && (receivedMacHdr.GetSrcAddrMode () > 1))
1175  {
1176  acceptFrame = receivedMacHdr.GetSrcPanId () == m_macPanId; // TODO: need to check if PAN coord
1177  }
1178 
1179  if (acceptFrame)
1180  {
1181  m_macRxTrace (originalPkt);
1182  // \todo: What should we do if we receive a frame while waiting for an ACK?
1183  // Especially if this frame has the ACK request bit set, should we reply with an ACK, possibly missing the pending ACK?
1184 
1185  // If the received frame is a frame with the ACK request bit set, we immediately send back an ACK.
1186  // If we are currently waiting for a pending ACK, we assume the ACK was lost and trigger a retransmission after sending the ACK.
1187  if ((receivedMacHdr.IsData () || receivedMacHdr.IsCommand ()) && receivedMacHdr.IsAckReq ()
1188  && !(receivedMacHdr.GetDstAddrMode () == SHORT_ADDR && (receivedMacHdr.GetShortDstAddr ().IsBroadcast () || receivedMacHdr.GetShortDstAddr ().IsMulticast ())))
1189  {
1190  // If this is a data or mac command frame, which is not a broadcast or multicast,
1191  // with ack req set, generate and send an ack frame.
1192  // If there is a CSMA medium access in progress we cancel the medium access
1193  // for sending the ACK frame. A new transmission attempt will be started
1194  // after the ACK was send.
1196  {
1199  }
1200  else if (m_lrWpanMacState == MAC_CSMA)
1201  {
1202  // \todo: If we receive a packet while doing CSMA/CA, should we drop the packet because of channel busy,
1203  // or should we restart CSMA/CA for the packet after sending the ACK?
1204  // Currently we simply restart CSMA/CA after sending the ACK.
1205  m_csmaCa->Cancel ();
1206  }
1207  // Cancel any pending MAC state change, ACKs have higher priority.
1208  m_setMacState.Cancel ();
1210 
1211  m_setMacState = Simulator::ScheduleNow (&LrWpanMac::SendAck, this, receivedMacHdr.GetSeqNum ());
1212  }
1213 
1214 
1215  if (receivedMacHdr.GetDstAddrMode () == SHORT_ADDR)
1216  {
1217  NS_LOG_DEBUG ("Packet from " << params.m_srcAddr);
1218  NS_LOG_DEBUG ("Packet to " << params.m_dstAddr);
1219  }
1220  else if (receivedMacHdr.GetDstAddrMode () == EXT_ADDR)
1221  {
1222  NS_LOG_DEBUG ("Packet from " << params.m_srcExtAddr);
1223  NS_LOG_DEBUG ("Packet to " << params.m_dstExtAddr);
1224  }
1225 
1226 
1227  if (receivedMacHdr.IsBeacon ())
1228  {
1229 
1230 
1231  // The received beacon size in symbols
1232  // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default 17 bytes)
1233  m_rxBeaconSymbols = m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1234  (originalPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ());
1235 
1236  // The start of Rx beacon time and start of the Incoming superframe Active Period
1237  m_macBeaconRxTime = Simulator::Now () - Seconds (double(m_rxBeaconSymbols) / symbolRate);
1238 
1239  NS_LOG_DEBUG ("Beacon Received; forwarding up (m_macBeaconRxTime: " << m_macBeaconRxTime.As (Time::S) << ")");
1240 
1241 
1242  //TODO: Handle mlme-scan.request here
1243 
1244  // Device not associated.
1245  if (m_macPanId == 0xffff)
1246  {
1247  //TODO: mlme-associate.request here.
1248  NS_LOG_ERROR (this << "The current device is not associated to any coordinator");
1249  return;
1250  }
1251 
1252 
1253  if (m_macPanId != receivedMacHdr.GetDstPanId ())
1254  {
1255  return;
1256  }
1257 
1258  BeaconPayloadHeader receivedMacPayload;
1259  p->RemoveHeader (receivedMacPayload);
1260 
1261  SuperframeField incomingSuperframe;
1262  incomingSuperframe = receivedMacPayload.GetSuperframeSpecField ();
1263 
1264  m_incomingBeaconOrder = incomingSuperframe.GetBeaconOrder ();
1265  m_incomingSuperframeOrder = incomingSuperframe.GetFrameOrder ();
1266  m_incomingFnlCapSlot = incomingSuperframe.GetFinalCapSlot ();
1267 
1270 
1271  if (incomingSuperframe.IsBattLifeExt ())
1272  {
1273  m_csmaCa->SetBatteryLifeExtension (true);
1274  }
1275  else
1276  {
1277  m_csmaCa->SetBatteryLifeExtension (false);
1278  }
1279 
1280  if (m_incomingBeaconOrder < 15 && !m_csmaCa->IsSlottedCsmaCa ())
1281  {
1282  m_csmaCa->SetSlottedCsmaCa ();
1283  }
1284 
1285  //TODO: get Incoming frame GTS Fields here
1286 
1287  NS_LOG_DEBUG ("Incoming superframe Active Portion (Beacon + CAP + CFP): " << m_incomingSuperframeDuration << " symbols");
1288 
1289  //Begin CAP on the current device using info from the Incoming superframe
1291 
1292 
1293  // Send a Beacon notification only if we are not
1294  // automatically sending data command requests or the beacon contains
1295  // a beacon payload in its MAC payload.
1296  // see IEEE 802.15.4-2011 Section 6.2.4.1
1297  if ((m_macAutoRequest == false) || p->GetSize () > 0)
1298  {
1299  //TODO: Add the rest of the MlmeBeaconNotifyIndication params
1301  {
1302  MlmeBeaconNotifyIndicationParams beaconParams;
1303  beaconParams.m_bsn = receivedMacHdr.GetSeqNum ();
1304  m_mlmeBeaconNotifyIndicationCallback (beaconParams,originalPkt);
1305  }
1306  }
1307 
1308  if (m_macAutoRequest)
1309  {
1310  // check if MLME-SYNC.request was previously issued and running
1311  // Sync. is necessary to handle pending messages (indirect transmissions)
1312  if (m_trackingEvent.IsRunning ())
1313  {
1315  m_numLostBeacons = 0;
1316 
1317  if (m_beaconTrackingOn)
1318  {
1319  //if tracking option is on keep tracking the next beacon
1320  uint64_t searchSymbols;
1321  Time searchBeaconTime;
1322 
1323  searchSymbols = ((uint64_t) 1 << m_incomingBeaconOrder) + 1 * aBaseSuperframeDuration;
1324  searchBeaconTime = Seconds ((double) searchSymbols / symbolRate);
1326  }
1327 
1328  PendingAddrFields pndAddrFields;
1329  pndAddrFields = receivedMacPayload.GetPndAddrFields ();
1330 
1331  //TODO: Ignore pending request if the address is in the GTS list.
1332  // If the address is not in the GTS list, then check if the address
1333  // is in the short address pending list or in the extended address
1334  // pending list.
1335 
1336  }
1337  }
1338  }
1339  else if (receivedMacHdr.IsData () && !m_mcpsDataIndicationCallback.IsNull ())
1340  {
1341  // If it is a data frame, push it up the stack.
1342  NS_LOG_DEBUG ("Data Packet is for me; forwarding up");
1343  m_mcpsDataIndicationCallback (params, p);
1344  }
1345  else if (receivedMacHdr.IsAcknowledgment () && m_txPkt && m_lrWpanMacState == MAC_ACK_PENDING)
1346  {
1347  LrWpanMacHeader macHdr;
1348  Time ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1349  m_txPkt->PeekHeader (macHdr);
1350  if (receivedMacHdr.GetSeqNum () == macHdr.GetSeqNum ())
1351  {
1353  // If it is an ACK with the expected sequence number, finish the transmission
1354  // and notify the upper layer.
1357  {
1358  TxQueueElement *txQElement = m_txQueue.front ();
1359  McpsDataConfirmParams confirmParams;
1360  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1361  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1362  m_mcpsDataConfirmCallback (confirmParams);
1363  }
1365 
1366  // Ack was succesfully received, wait for the Interframe Space (IFS) and then proceed
1368  }
1369  else
1370  {
1371  // 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)
1373  if (!PrepareRetransmission ())
1374  {
1375  m_setMacState.Cancel ();
1377  }
1378  else
1379  {
1380  m_setMacState.Cancel ();
1382  }
1383  }
1384  }
1385  }
1386  else
1387  {
1388  m_macRxDropTrace (originalPkt);
1389  }
1390  }
1391  }
1392 }
1393 
1394 
1395 
1396 void
1397 LrWpanMac::SendAck (uint8_t seqno)
1398 {
1399  NS_LOG_FUNCTION (this << static_cast<uint32_t> (seqno));
1400 
1402 
1403  // Generate a corresponding ACK Frame.
1405  LrWpanMacTrailer macTrailer;
1406  Ptr<Packet> ackPacket = Create<Packet> (0);
1407  ackPacket->AddHeader (macHdr);
1408  // Calculate FCS if the global attribute ChecksumEnable is set.
1409  if (Node::ChecksumEnabled ())
1410  {
1411  macTrailer.EnableFcs (true);
1412  macTrailer.SetFcs (ackPacket);
1413  }
1414  ackPacket->AddTrailer (macTrailer);
1415 
1416  // Enqueue the ACK packet for further processing
1417  // when the transmitter is activated.
1418  m_txPkt = ackPacket;
1419 
1420  // Switch transceiver to TX mode. Proceed sending the Ack on confirm.
1422  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1423 
1424 }
1425 
1426 void
1428 {
1429  TxQueueElement *txQElement = m_txQueue.front ();
1430  Ptr<const Packet> p = txQElement->txQPkt;
1431  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1432 
1433  Ptr<Packet> pkt = p->Copy ();
1434  LrWpanMacHeader hdr;
1435  pkt->RemoveHeader (hdr);
1436  if (!hdr.GetShortDstAddr ().IsBroadcast () && !hdr.GetShortDstAddr ().IsMulticast())
1437  {
1439  }
1440 
1441  txQElement->txQPkt = 0;
1442  delete txQElement;
1443  m_txQueue.pop_front ();
1444  m_txPkt = 0;
1445  m_retransmission = 0;
1446  m_numCsmacaRetry = 0;
1447  m_macTxDequeueTrace (p);
1448 }
1449 
1450 void
1452 {
1453  NS_LOG_FUNCTION (this);
1454 
1455  // TODO: If we are a PAN coordinator and this was an indirect transmission,
1456  // we will not initiate a retransmission. Instead we wait for the data
1457  // being extracted after a new data request command.
1458  if (!PrepareRetransmission ())
1459  {
1461  }
1462  else
1463  {
1465  }
1466 }
1467 
1468 void
1470 {
1471  NS_LOG_DEBUG ("IFS Completed");
1472  m_setMacState.Cancel ();
1474 }
1475 
1476 
1477 bool
1479 {
1480  NS_LOG_FUNCTION (this);
1481 
1483  {
1484  // Maximum number of retransmissions has been reached.
1485  // remove the copy of the packet that was just sent
1486  TxQueueElement *txQElement = m_txQueue.front ();
1487  m_macTxDropTrace (txQElement->txQPkt);
1489  {
1490  McpsDataConfirmParams confirmParams;
1491  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1492  confirmParams.m_status = IEEE_802_15_4_NO_ACK;
1493  m_mcpsDataConfirmCallback (confirmParams);
1494  }
1496  return false;
1497  }
1498  else
1499  {
1500  m_retransmission++;
1501  m_numCsmacaRetry += m_csmaCa->GetNB () + 1;
1502  // Start next CCA process for this packet.
1503  return true;
1504  }
1505 }
1506 
1507 void
1509 {
1511  NS_LOG_FUNCTION (this << status << m_txQueue.size ());
1512 
1513  LrWpanMacHeader macHdr;
1514  Time ifsWaitTime;
1515  uint64_t symbolRate;
1516 
1517  symbolRate = (uint64_t) m_phy->GetDataOrSymbolRate (false); //symbols per second
1518 
1519  m_txPkt->PeekHeader (macHdr);
1520 
1521  if (status == IEEE_802_15_4_PHY_SUCCESS)
1522  {
1523  if (!macHdr.IsAcknowledgment ())
1524  {
1525  if (macHdr.IsBeacon ())
1526  {
1527  ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1528 
1529  // The Tx Beacon in symbols
1530  // Beacon = 5 bytes Sync Header (SHR) + 1 byte PHY header (PHR) + PSDU (default 17 bytes)
1531  uint64_t beaconSymbols = m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1532  (m_txPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ());
1533 
1534  // The beacon Tx time and start of the Outgoing superframe Active Period
1535  m_macBeaconTxTime = Simulator::Now () - Seconds (double(beaconSymbols) / symbolRate);
1536 
1537 
1538  m_txPkt = 0;
1540  NS_LOG_DEBUG ("Beacon Sent (m_macBeaconTxTime: " << m_macBeaconTxTime.As (Time::S) << ")");
1541 
1542  MlmeStartConfirmParams mlmeConfirmParams;
1543  mlmeConfirmParams.m_status = MLMESTART_SUCCESS;
1545  {
1546  m_mlmeStartConfirmCallback (mlmeConfirmParams);
1547  }
1548  }
1549  else if (macHdr.IsAckReq ()) // We have sent a regular data packet, check if we have to wait for an ACK.
1550  {
1551  // wait for the ack or the next retransmission timeout
1552  // start retransmission timer
1553  Time waitTime = Seconds ((double) GetMacAckWaitDuration () / symbolRate);
1556  m_setMacState.Cancel ();
1558  return;
1559  }
1560  else
1561  {
1563  // remove the copy of the packet that was just sent
1565  {
1566  McpsDataConfirmParams confirmParams;
1567  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1568  TxQueueElement *txQElement = m_txQueue.front ();
1569  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1570  confirmParams.m_status = IEEE_802_15_4_SUCCESS;
1571  m_mcpsDataConfirmCallback (confirmParams);
1572  }
1573  ifsWaitTime = Seconds ((double) GetIfsSize () / symbolRate);
1575  }
1576  }
1577  else
1578  {
1579  // We have send an ACK. Clear the packet buffer.
1580  m_txPkt = 0;
1581  }
1582  }
1583  else if (status == IEEE_802_15_4_PHY_UNSPECIFIED)
1584  {
1585 
1586  if (!macHdr.IsAcknowledgment ())
1587  {
1588  NS_ASSERT_MSG (m_txQueue.size () > 0, "TxQsize = 0");
1589  TxQueueElement *txQElement = m_txQueue.front ();
1590  m_macTxDropTrace (txQElement->txQPkt);
1592  {
1593  McpsDataConfirmParams confirmParams;
1594  confirmParams.m_msduHandle = txQElement->txQMsduHandle;
1595  confirmParams.m_status = IEEE_802_15_4_FRAME_TOO_LONG;
1596  m_mcpsDataConfirmCallback (confirmParams);
1597  }
1599  }
1600  else
1601  {
1602  NS_LOG_ERROR ("Unable to send ACK");
1603  }
1604  }
1605  else
1606  {
1607  // Something went really wrong. The PHY is not in the correct state for
1608  // data transmission.
1609  NS_FATAL_ERROR ("Transmission attempt failed with PHY status " << status);
1610  }
1611 
1612 
1613  if (!ifsWaitTime.IsZero ())
1614  {
1616  }
1617  else
1618  {
1619  m_setMacState.Cancel ();
1621  }
1622 
1623 }
1624 
1625 void
1627 {
1628  NS_LOG_FUNCTION (this << status);
1629  // Direct this call through the csmaCa object
1630  m_csmaCa->PlmeCcaConfirm (status);
1631 }
1632 
1633 void
1634 LrWpanMac::PlmeEdConfirm (LrWpanPhyEnumeration status, uint8_t energyLevel)
1635 {
1636  NS_LOG_FUNCTION (this << status << energyLevel);
1637 
1638 }
1639 
1640 void
1643  LrWpanPhyPibAttributes* attribute)
1644 {
1645  NS_LOG_FUNCTION (this << status << id << attribute);
1646 }
1647 
1648 void
1650 {
1651  NS_LOG_FUNCTION (this << status);
1652 
1654  {
1655  NS_ASSERT (m_txPkt);
1656 
1657  // Start sending if we are in state SENDING and the PHY transmitter was enabled.
1661  m_phy->PdDataRequest (m_txPkt->GetSize (), m_txPkt);
1662  }
1663  else if (m_lrWpanMacState == MAC_CSMA && (status == IEEE_802_15_4_PHY_RX_ON || status == IEEE_802_15_4_PHY_SUCCESS))
1664  {
1665  // Start the CSMA algorithm as soon as the receiver is enabled.
1666  m_csmaCa->Start ();
1667  }
1668  else if (m_lrWpanMacState == MAC_IDLE)
1669  {
1671  // Do nothing special when going idle.
1672  }
1673  else if (m_lrWpanMacState == MAC_ACK_PENDING)
1674  {
1676  }
1677  else
1678  {
1679  // TODO: What to do when we receive an error?
1680  // If we want to transmit a packet, but switching the transceiver on results
1681  // in an error, we have to recover somehow (and start sending again).
1682  NS_FATAL_ERROR ("Error changing transceiver state");
1683  }
1684 }
1685 
1686 void
1689 {
1690  NS_LOG_FUNCTION (this << status << id);
1691 }
1692 
1693 void
1695 {
1696  NS_LOG_FUNCTION (this << "mac state = " << macState);
1697 
1698  McpsDataConfirmParams confirmParams;
1699 
1700  if (macState == MAC_IDLE)
1701  {
1703 
1704  if (m_macRxOnWhenIdle)
1705  {
1706  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1707  }
1708  else
1709  {
1710  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TRX_OFF);
1711  }
1712 
1713  CheckQueue ();
1714  }
1715  else if (macState == MAC_ACK_PENDING)
1716  {
1718  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1719  }
1720  else if (macState == MAC_CSMA)
1721  {
1723 
1725  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_RX_ON);
1726  }
1727  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_IDLE)
1728  {
1729  // Channel is idle, set transmitter to TX_ON
1731  m_phy->PlmeSetTRXStateRequest (IEEE_802_15_4_PHY_TX_ON);
1732  }
1733  else if (m_lrWpanMacState == MAC_CSMA && macState == CHANNEL_ACCESS_FAILURE)
1734  {
1735  NS_ASSERT (m_txPkt);
1736 
1737  // cannot find a clear channel, drop the current packet.
1738  NS_LOG_DEBUG ( this << " cannot find clear channel");
1739  confirmParams.m_msduHandle = m_txQueue.front ()->txQMsduHandle;
1743  {
1744  m_mcpsDataConfirmCallback (confirmParams);
1745  }
1746  // remove the copy of the packet that was just sent
1749  }
1750  else if (m_lrWpanMacState == MAC_CSMA && macState == MAC_CSMA_DEFERRED)
1751  {
1753  m_txPkt = 0;
1754  NS_LOG_DEBUG ("****** PACKET DEFERRED to the next superframe *****");
1755  }
1756 }
1757 
1760 {
1761  return m_associationStatus;
1762 }
1763 
1764 void
1766 {
1767  m_associationStatus = status;
1768 }
1769 
1770 uint16_t
1772 {
1773  return m_macPanId;
1774 }
1775 
1776 void
1777 LrWpanMac::SetPanId (uint16_t panId)
1778 {
1779  m_macPanId = panId;
1780 }
1781 
1782 void
1784 {
1785  NS_LOG_LOGIC (this << " change lrwpan mac state from "
1786  << m_lrWpanMacState << " to "
1787  << newState);
1788  m_macStateLogger (m_lrWpanMacState, newState);
1789  m_lrWpanMacState = newState;
1790 }
1791 
1792 uint64_t
1794 {
1795  return m_csmaCa->GetUnitBackoffPeriod () + m_phy->aTurnaroundTime + m_phy->GetPhySHRDuration ()
1796  + ceil (6 * m_phy->GetPhySymbolsPerOctet ());
1797 }
1798 
1799 uint8_t
1801 {
1802  return m_macMaxFrameRetries;
1803 }
1804 
1805 void
1807 {
1808  NS_LOG_DEBUG("Transit Queue Size: "<<m_txQueue.size());
1809 }
1810 
1811 void
1813 {
1814  m_macMaxFrameRetries = retries;
1815 }
1816 
1817 bool
1819 {
1820  NS_ASSERT (m_txPkt);
1821  LrWpanMacHeader macHdr;
1822  m_txPkt->PeekHeader (macHdr);
1823 
1824  if (m_macCoordShortAddress == macHdr.GetShortDstAddr ()
1825  || m_macCoordExtendedAddress == macHdr.GetExtDstAddr ())
1826  {
1827  return true;
1828  }
1829  else
1830  {
1831  std::cout << "ERROR: Packet not for the coordinator!\n";
1832  return false;
1833 
1834  }
1835 
1836 }
1837 
1838 uint32_t
1840 {
1841  NS_ASSERT (m_txPkt);
1842 
1843  if (m_txPkt->GetSize () <= aMaxSIFSFrameSize)
1844  {
1845  return m_macSIFSPeriod;
1846  }
1847  else
1848  {
1849  return m_macLIFSPeriod;
1850  }
1851 }
1852 
1853 void
1855 {
1857 }
1858 
1859 void
1861 {
1863 }
1864 
1865 
1866 uint64_t
1868 {
1869  NS_ASSERT (m_txPkt);
1870  // Sync Header (SHR) + 8 bits PHY header (PHR) + PSDU
1871  return (m_phy->GetPhySHRDuration () + 1 * m_phy->GetPhySymbolsPerOctet () +
1872  (m_txPkt->GetSize () * m_phy->GetPhySymbolsPerOctet ()));
1873 }
1874 
1875 bool
1877 {
1878  NS_ASSERT (m_txPkt);
1879  LrWpanMacHeader macHdr;
1880  m_txPkt->PeekHeader (macHdr);
1881 
1882  return macHdr.IsAckReq ();
1883 }
1884 
1885 } // namespace ns3
1886 
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:647
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
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:429
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
bool IsBroadcast(void) const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
void CheckQueue(void)
Check the transmission queue.
Definition: lr-wpan-mac.cc:893
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:380
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:992
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:986
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
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
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:980
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:849
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:511
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:301
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:998
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:911
PendingAddrFields GetPendingAddrFields(void)
Constructs Pending Address Fields from the local information, the Pending Address Fields are part of ...
Definition: lr-wpan-mac.cc:946
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:607
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:812
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:661
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:962
#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:936
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...
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:719
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.
bool IsMulticast(void) const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e., if its first 3 bits are 100).
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:1289
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:766
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1305
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:968
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:956
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:862
void SetMlmePollConfirmCallback(MlmePollConfirmCallback c)
Set the callback for the confirmation of a data transmission request.
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:974