src/wifi/model/mac-low.cc
changeset 11174 780a43e4980c
parent 11159 b744c32b69bc
child 11188 e010083f6592
equal deleted inserted replaced
11173:9b4c47e6c37e 11174:780a43e4980c
    32 #include "wifi-phy.h"
    32 #include "wifi-phy.h"
    33 #include "wifi-mac-trailer.h"
    33 #include "wifi-mac-trailer.h"
    34 #include "qos-utils.h"
    34 #include "qos-utils.h"
    35 #include "edca-txop-n.h"
    35 #include "edca-txop-n.h"
    36 #include "snr-tag.h"
    36 #include "snr-tag.h"
       
    37 #include "yans-wifi-phy.h"
       
    38 #include "ampdu-tag.h"
       
    39 #include "wifi-mac-queue.h"
       
    40 #include "mpdu-aggregator.h"
    37 
    41 
    38 #undef NS_LOG_APPEND_CONTEXT
    42 #undef NS_LOG_APPEND_CONTEXT
    39 #define NS_LOG_APPEND_CONTEXT std::clog << "[mac=" << m_self << "] "
    43 #define NS_LOG_APPEND_CONTEXT std::clog << "[mac=" << m_self << "] "
    40 
    44 
    41 
    45 
    48 }
    52 }
    49 MacLowTransmissionListener::~MacLowTransmissionListener ()
    53 MacLowTransmissionListener::~MacLowTransmissionListener ()
    50 {
    54 {
    51 }
    55 }
    52 void
    56 void
    53 MacLowTransmissionListener::GotBlockAck (const CtrlBAckResponseHeader *blockAck,
    57 MacLowTransmissionListener::GotBlockAck (const CtrlBAckResponseHeader *blockAck, Mac48Address source, WifiMode mode)
    54                                          Mac48Address source)
       
    55 {
    58 {
    56 }
    59 }
    57 void
    60 void
    58 MacLowTransmissionListener::MissedBlockAck (void)
    61 MacLowTransmissionListener::MissedBlockAck (void)
    59 {
    62 {
    68 MacLowBlockAckEventListener::MacLowBlockAckEventListener ()
    71 MacLowBlockAckEventListener::MacLowBlockAckEventListener ()
    69 {
    72 {
    70 }
    73 }
    71 MacLowBlockAckEventListener::~MacLowBlockAckEventListener ()
    74 MacLowBlockAckEventListener::~MacLowBlockAckEventListener ()
    72 {
    75 {
       
    76 }
       
    77 
       
    78 void MacLowBlockAckEventListener::SetAmpdu (bool ampdu)
       
    79 {
       
    80 }
       
    81 void MacLowBlockAckEventListener::CompleteTransfer(Mac48Address address, uint8_t tid)
       
    82 {
       
    83 }
       
    84 void
       
    85 MacLowBlockAckEventListener::CompleteMpduTx (Ptr<const Packet> packet, WifiMacHeader hdr, Time tstamp)
       
    86 {
       
    87 }
       
    88 uint16_t 
       
    89 MacLowBlockAckEventListener::GetNextSequenceNumberfor (WifiMacHeader *hdr)
       
    90 {
       
    91  return 0;
       
    92 }
       
    93 uint16_t 
       
    94 MacLowBlockAckEventListener::PeekNextSequenceNumberfor (WifiMacHeader *hdr)
       
    95 {
       
    96  return 0;
       
    97 }
       
    98 Ptr<const Packet> 
       
    99 MacLowBlockAckEventListener::PeekNextPacketInBaQueue (WifiMacHeader &header, Mac48Address recipient, uint8_t tid, Time *timestamp)
       
   100 {
       
   101  return 0;
       
   102 }
       
   103 void 
       
   104 MacLowBlockAckEventListener::RemoveFromBaQueue (uint8_t tid, Mac48Address recipient, uint16_t seqnumber)
       
   105 {
       
   106 }
       
   107 uint32_t 
       
   108 MacLowBlockAckEventListener::GetNOutstandingPackets (Mac48Address recipient, uint8_t tid)
       
   109 {
       
   110   return 0;
       
   111 }
       
   112 uint32_t 
       
   113 MacLowBlockAckEventListener::GetNRetryNeededPackets (Mac48Address recipient, uint8_t tid) const
       
   114 {
       
   115   return 0;
    73 }
   116 }
    74 
   117 
    75 MacLowTransmissionParameters::MacLowTransmissionParameters ()
   118 MacLowTransmissionParameters::MacLowTransmissionParameters ()
    76   : m_nextSize (0),
   119   : m_nextSize (0),
    77     m_waitAck (ACK_NONE),
   120     m_waitAck (ACK_NONE),
   302     m_sendCtsEvent (),
   345     m_sendCtsEvent (),
   303     m_sendAckEvent (),
   346     m_sendAckEvent (),
   304     m_sendDataEvent (),
   347     m_sendDataEvent (),
   305     m_waitSifsEvent (),
   348     m_waitSifsEvent (),
   306     m_endTxNoAckEvent (),
   349     m_endTxNoAckEvent (),
       
   350     m_mpduAggregator (0),
   307     m_currentPacket (0),
   351     m_currentPacket (0),
   308     m_listener (0),
   352     m_listener (0),
   309     m_phyMacLowListener (0),
   353     m_phyMacLowListener (0),
   310     m_ctsToSelfSupported (false)
   354     m_ctsToSelfSupported (false),
       
   355     m_receivedAtLeastOneMpdu (false)
   311 {
   356 {
   312   NS_LOG_FUNCTION (this);
   357   NS_LOG_FUNCTION (this);
   313   m_lastNavDuration = Seconds (0);
   358   m_lastNavDuration = Seconds (0);
   314   m_lastNavStart = Seconds (0);
   359   m_lastNavStart = Seconds (0);
   315   m_promisc = false;
   360   m_promisc = false;
       
   361   m_ampdu = false;
       
   362   m_sentMpdus = 0;
       
   363   m_aggregateQueue = CreateObject<WifiMacQueue> ();
   316 }
   364 }
   317 
   365 
   318 MacLow::~MacLow ()
   366 MacLow::~MacLow ()
   319 {
   367 {
   320   NS_LOG_FUNCTION (this);
   368   NS_LOG_FUNCTION (this);
   359   if (m_phyMacLowListener != 0)
   407   if (m_phyMacLowListener != 0)
   360     {
   408     {
   361 	  delete m_phyMacLowListener;
   409 	  delete m_phyMacLowListener;
   362 	  m_phyMacLowListener = 0;
   410 	  m_phyMacLowListener = 0;
   363     }
   411     }
       
   412   m_mpduAggregator = 0;
       
   413   m_sentMpdus = 0;
       
   414   m_aggregateQueue = 0;
       
   415   m_ampdu = false;
   364 }
   416 }
   365 
   417 
   366 void
   418 void
   367 MacLow::CancelAllEvents (void)
   419 MacLow::CancelAllEvents (void)
   368 {
   420 {
   437 
   489 
   438 void
   490 void
   439 MacLow::SetPhy (Ptr<WifiPhy> phy)
   491 MacLow::SetPhy (Ptr<WifiPhy> phy)
   440 {
   492 {
   441   m_phy = phy;
   493   m_phy = phy;
   442   m_phy->SetReceiveOkCallback (MakeCallback (&MacLow::ReceiveOk, this));
   494   m_phy->SetReceiveOkCallback (MakeCallback (&MacLow::DeaggregateAmpduAndReceive, this));
   443   m_phy->SetReceiveErrorCallback (MakeCallback (&MacLow::ReceiveError, this));
   495   m_phy->SetReceiveErrorCallback (MakeCallback (&MacLow::ReceiveError, this));
   444   SetupPhyMacLowListener (phy);
   496   SetupPhyMacLowListener (phy);
   445 }
   497 }
   446 Ptr<WifiPhy>
   498 Ptr<WifiPhy>
   447 MacLow::GetPhy (void) const
   499 MacLow::GetPhy (void) const
   592 MacLow::RegisterDcfListener (MacLowDcfListener *listener)
   644 MacLow::RegisterDcfListener (MacLowDcfListener *listener)
   593 {
   645 {
   594   m_dcfListeners.push_back (listener);
   646   m_dcfListeners.push_back (listener);
   595 }
   647 }
   596 
   648 
       
   649 bool
       
   650 MacLow::IsAmpdu (Ptr<const Packet> packet, const WifiMacHeader hdr)
       
   651 {
       
   652   uint32_t size, actualSize;
       
   653   WifiMacTrailer fcs;
       
   654   size = packet->GetSize () + hdr.GetSize () + fcs.GetSerializedSize ();
       
   655   Ptr<Packet> p = AggregateToAmpdu (packet, hdr);
       
   656   actualSize = p->GetSize();
       
   657   if (actualSize > size)
       
   658     {
       
   659       m_currentPacket = p;
       
   660       return true;
       
   661     }
       
   662   else
       
   663       return false;
       
   664 }
   597 
   665 
   598 void
   666 void
   599 MacLow::StartTransmission (Ptr<const Packet> packet,
   667 MacLow::StartTransmission (Ptr<const Packet> packet,
   600                            const WifiMacHeader* hdr,
   668                            const WifiMacHeader* hdr,
   601                            MacLowTransmissionParameters params,
   669                            MacLowTransmissionParameters params,
   614    *
   682    *
   615    * This typically happens because the high-priority
   683    * This typically happens because the high-priority
   616    * QapScheduler has taken access to the channel from
   684    * QapScheduler has taken access to the channel from
   617    * one of the Edca of the QAP.
   685    * one of the Edca of the QAP.
   618    */
   686    */
   619   m_currentPacket = packet->Copy ();
       
   620   m_currentHdr = *hdr;
   687   m_currentHdr = *hdr;
   621   CancelAllEvents ();
   688   CancelAllEvents ();
   622   m_listener = listener;
   689   m_listener = listener;
   623   m_txParams = params;
   690   m_txParams = params;
   624 
   691 
   625   //NS_ASSERT (m_phy->IsStateIdle ());
   692   //NS_ASSERT (m_phy->IsStateIdle ());
   626 
   693 
       
   694   if(m_aggregateQueue->GetSize () == 0)
       
   695   {
       
   696     m_currentPacket = packet->Copy ();
       
   697     m_ampdu = IsAmpdu (m_currentPacket, m_currentHdr);
       
   698   }
       
   699   else
       
   700   {
       
   701    /*m_aggregateQueue > 0 occurs when a RTS/CTS exchange failed before an A-MPDU transmission.
       
   702     *In that case, we transmit the same A-MPDU as previously.
       
   703     */
       
   704     m_sentMpdus = m_aggregateQueue->GetSize ();
       
   705     m_ampdu = true;
       
   706   }
       
   707 
   627   NS_LOG_DEBUG ("startTx size=" << GetSize (m_currentPacket, &m_currentHdr) <<
   708   NS_LOG_DEBUG ("startTx size=" << GetSize (m_currentPacket, &m_currentHdr) <<
   628                 ", to=" << m_currentHdr.GetAddr1 () << ", listener=" << m_listener);
   709                 ", to=" << m_currentHdr.GetAddr1 () << ", listener=" << m_listener);
   629 
   710 
       
   711   if (m_ampdu)
       
   712       m_txParams.EnableCompressedBlockAck ();
       
   713 
   630   if (m_txParams.MustSendRts ())
   714   if (m_txParams.MustSendRts ())
   631     {
   715     {
   632       SendRtsForPacket ();
   716       SendRtsForPacket ();
   633     }
   717     }
   634   else
   718   else
   655 void
   739 void
   656 MacLow::ReceiveError (Ptr<const Packet> packet, double rxSnr)
   740 MacLow::ReceiveError (Ptr<const Packet> packet, double rxSnr)
   657 {
   741 {
   658   NS_LOG_FUNCTION (this << packet << rxSnr);
   742   NS_LOG_FUNCTION (this << packet << rxSnr);
   659   NS_LOG_DEBUG ("rx failed ");
   743   NS_LOG_DEBUG ("rx failed ");
   660   if (m_txParams.MustWaitFastAck ())
   744   AmpduTag ampdu;
       
   745   Ptr<Packet> pkt = packet->Copy();
       
   746   bool isInAmpdu = pkt->RemovePacketTag(ampdu);
       
   747 
       
   748   if(isInAmpdu && m_receivedAtLeastOneMpdu && (ampdu.GetNoOfMpdus() == 1))
       
   749     {
       
   750       MpduAggregator::DeaggregatedMpdus packets =  MpduAggregator::Deaggregate (pkt);
       
   751       MpduAggregator::DeaggregatedMpdusCI n = packets.begin ();
       
   752       WifiMacHeader hdr;
       
   753       (*n).first->PeekHeader(hdr);
       
   754       if(hdr.IsQosData())
       
   755         {
       
   756           NS_LOG_DEBUG ("last a-mpdu subframe detected/sendImmediateBlockAck from=" << hdr.GetAddr2 ());
       
   757           m_sendAckEvent = Simulator::Schedule (GetSifs (),
       
   758                                                 &MacLow::SendBlockAckAfterAmpdu, this,
       
   759                                                 hdr.GetQosTid(),
       
   760                                                 hdr.GetAddr2 (),
       
   761                                                 hdr.GetDuration (),
       
   762                                                 m_currentMode);
       
   763         }
       
   764       else if (hdr.IsBlockAckReq())
       
   765         {
       
   766  	  NS_LOG_DEBUG("last a-mpdu subframe is BAR");
       
   767   	}
       
   768       m_receivedAtLeastOneMpdu = false;
       
   769     }
       
   770   else if (m_txParams.MustWaitFastAck ())
   661     {
   771     {
   662       NS_ASSERT (m_fastAckFailedTimeoutEvent.IsExpired ());
   772       NS_ASSERT (m_fastAckFailedTimeoutEvent.IsExpired ());
   663       m_fastAckFailedTimeoutEvent = Simulator::Schedule (GetSifs (),
   773       m_fastAckFailedTimeoutEvent = Simulator::Schedule (GetSifs (),
   664                                                          &MacLow::FastAckFailedTimeout, this);
   774                                                          &MacLow::FastAckFailedTimeout, this);
   665     }
   775     }
   696   m_currentPacket = 0;
   806   m_currentPacket = 0;
   697   m_listener = 0;
   807   m_listener = 0;
   698 }
   808 }
   699 
   809 
   700 void
   810 void
   701 MacLow::ReceiveOk (Ptr<Packet> packet, double rxSnr, WifiMode txMode, WifiPreamble preamble)
   811 MacLow::ReceiveOk (Ptr<Packet> packet, double rxSnr, WifiMode txMode, WifiPreamble preamble, bool ampduSubframe)
   702 {
   812 {
   703   NS_LOG_FUNCTION (this << packet << rxSnr << txMode << preamble);
   813   NS_LOG_FUNCTION (this << packet << rxSnr << txMode << preamble);
   704   /* A packet is received from the PHY.
   814   /* A packet is received from the PHY.
   705    * When we have handled this packet,
   815    * When we have handled this packet,
   706    * we handle any packet present in the
   816    * we handle any packet present in the
   718        * A STA that is addressed by an RTS frame shall transmit a CTS frame after a SIFS
   828        * A STA that is addressed by an RTS frame shall transmit a CTS frame after a SIFS
   719        * period if the NAV at the STA receiving the RTS frame indicates that the medium is
   829        * period if the NAV at the STA receiving the RTS frame indicates that the medium is
   720        * idle. If the NAV at the STA receiving the RTS indicates the medium is not idle,
   830        * idle. If the NAV at the STA receiving the RTS indicates the medium is not idle,
   721        * that STA shall not respond to the RTS frame.
   831        * that STA shall not respond to the RTS frame.
   722        */
   832        */
   723       if (isPrevNavZero
   833       if (ampduSubframe)
   724           && hdr.GetAddr1 () == m_self)
   834         {
   725         {
   835            NS_FATAL_ERROR ("Received RTS as part of an A-MPDU");
   726           NS_LOG_DEBUG ("rx RTS from=" << hdr.GetAddr2 () << ", schedule CTS");
       
   727           NS_ASSERT (m_sendCtsEvent.IsExpired ());
       
   728           m_stationManager->ReportRxOk (hdr.GetAddr2 (), &hdr,
       
   729                                         rxSnr, txMode);
       
   730           m_sendCtsEvent = Simulator::Schedule (GetSifs (),
       
   731                                                 &MacLow::SendCtsAfterRts, this,
       
   732                                                 hdr.GetAddr2 (),
       
   733                                                 hdr.GetDuration (),
       
   734                                                 txMode,
       
   735                                                 rxSnr);
       
   736         }
   836         }
   737       else
   837       else
   738         {
   838         {
   739           NS_LOG_DEBUG ("rx RTS from=" << hdr.GetAddr2 () << ", cannot schedule CTS");
   839           if (isPrevNavZero
       
   840               && hdr.GetAddr1 () == m_self)
       
   841             {
       
   842               NS_LOG_DEBUG ("rx RTS from=" << hdr.GetAddr2 () << ", schedule CTS");
       
   843               NS_ASSERT (m_sendCtsEvent.IsExpired ());
       
   844               m_stationManager->ReportRxOk (hdr.GetAddr2 (), &hdr,
       
   845                                             rxSnr, txMode);
       
   846               m_sendCtsEvent = Simulator::Schedule (GetSifs (),
       
   847                                                     &MacLow::SendCtsAfterRts, this,
       
   848                                                     hdr.GetAddr2 (),
       
   849                                                     hdr.GetDuration (),
       
   850                                                     txMode,
       
   851                                                     rxSnr);
       
   852             }
       
   853           else
       
   854             {
       
   855               NS_LOG_DEBUG ("rx RTS from=" << hdr.GetAddr2 () << ", cannot schedule CTS");
       
   856             }
   740         }
   857         }
   741     }
   858     }
   742   else if (hdr.IsCts ()
   859   else if (hdr.IsCts ()
   743            && hdr.GetAddr1 () == m_self
   860            && hdr.GetAddr1 () == m_self
   744            && m_ctsTimeoutEvent.IsRunning ()
   861            && m_ctsTimeoutEvent.IsRunning ()
   745            && m_currentPacket != 0)
   862            && m_currentPacket != 0)
   746     {
   863     {
       
   864       if (ampduSubframe)
       
   865         {
       
   866           NS_FATAL_ERROR ("Received CTS as part of an A-MPDU");
       
   867         }
   747       NS_LOG_DEBUG ("receive cts from=" << m_currentHdr.GetAddr1 ());
   868       NS_LOG_DEBUG ("receive cts from=" << m_currentHdr.GetAddr1 ());
   748       SnrTag tag;
   869       SnrTag tag;
   749       packet->RemovePacketTag (tag);
   870       packet->RemovePacketTag (tag);
   750       m_stationManager->ReportRxOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   871       m_stationManager->ReportRxOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   751                                     rxSnr, txMode);
   872                                     rxSnr, txMode);
   774       packet->RemovePacketTag (tag);
   895       packet->RemovePacketTag (tag);
   775       m_stationManager->ReportRxOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   896       m_stationManager->ReportRxOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   776                                     rxSnr, txMode);
   897                                     rxSnr, txMode);
   777       m_stationManager->ReportDataOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   898       m_stationManager->ReportDataOk (m_currentHdr.GetAddr1 (), &m_currentHdr,
   778                                       rxSnr, txMode, tag.Get ());
   899                                       rxSnr, txMode, tag.Get ());
       
   900         
   779       bool gotAck = false;
   901       bool gotAck = false;
   780       if (m_txParams.MustWaitNormalAck ()
   902       if (m_txParams.MustWaitNormalAck ()
   781           && m_normalAckTimeoutEvent.IsRunning ())
   903           && m_normalAckTimeoutEvent.IsRunning ())
   782         {
   904         {
   783           m_normalAckTimeoutEvent.Cancel ();
   905           m_normalAckTimeoutEvent.Cancel ();
   807     {
   929     {
   808       NS_LOG_DEBUG ("got block ack from " << hdr.GetAddr2 ());
   930       NS_LOG_DEBUG ("got block ack from " << hdr.GetAddr2 ());
   809       CtrlBAckResponseHeader blockAck;
   931       CtrlBAckResponseHeader blockAck;
   810       packet->RemoveHeader (blockAck);
   932       packet->RemoveHeader (blockAck);
   811       m_blockAckTimeoutEvent.Cancel ();
   933       m_blockAckTimeoutEvent.Cancel ();
   812       m_listener->GotBlockAck (&blockAck, hdr.GetAddr2 ());
   934       NotifyAckTimeoutResetNow ();
       
   935       m_listener->GotBlockAck (&blockAck, hdr.GetAddr2 (),txMode);
       
   936       m_sentMpdus = 0;
       
   937       m_ampdu = false;
   813     }
   938     }
   814   else if (hdr.IsBlockAckReq () && hdr.GetAddr1 () == m_self)
   939   else if (hdr.IsBlockAckReq () && hdr.GetAddr1 () == m_self)
   815     {
   940     {
   816       CtrlBAckRequestHeader blockAckReq;
   941       CtrlBAckRequestHeader blockAckReq;
   817       packet->RemoveHeader (blockAckReq);
   942       packet->RemoveHeader (blockAckReq);
   841                 }
   966                 }
   842               else
   967               else
   843                 {
   968                 {
   844                   NS_FATAL_ERROR ("Delayed block ack not supported.");
   969                   NS_FATAL_ERROR ("Delayed block ack not supported.");
   845                 }
   970                 }
       
   971               m_receivedAtLeastOneMpdu = false;
   846             }
   972             }
   847           else
   973           else
   848             {
   974             {
   849               NS_LOG_DEBUG ("There's not a valid agreement for this block ack request.");
   975               NS_LOG_DEBUG ("There's not a valid agreement for this block ack request.");
   850             }
   976             }
   855         }
   981         }
   856     }
   982     }
   857   else if (hdr.IsCtl ())
   983   else if (hdr.IsCtl ())
   858     {
   984     {
   859       NS_LOG_DEBUG ("rx drop " << hdr.GetTypeString ());
   985       NS_LOG_DEBUG ("rx drop " << hdr.GetTypeString ());
       
   986       m_receivedAtLeastOneMpdu = false;
   860     }
   987     }
   861   else if (hdr.GetAddr1 () == m_self)
   988   else if (hdr.GetAddr1 () == m_self)
   862     {
   989     {
   863       m_stationManager->ReportRxOk (hdr.GetAddr2 (), &hdr,
   990       m_stationManager->ReportRxOk (hdr.GetAddr2 (), &hdr,
   864                                     rxSnr, txMode);
   991                                     rxSnr, txMode);
   865 
   992       if (hdr.IsQosData () && ReceiveMpdu (packet, hdr))
   866       if (hdr.IsQosData () && StoreMpduIfNeeded (packet, hdr))
       
   867         {
   993         {
   868           /* From section 9.10.4 in IEEE 802.11:
   994           /* From section 9.10.4 in IEEE 802.11:
   869              Upon the receipt of a QoS data frame from the originator for which
   995              Upon the receipt of a QoS data frame from the originator for which
   870              the Block Ack agreement exists, the recipient shall buffer the MSDU
   996              the Block Ack agreement exists, the recipient shall buffer the MSDU
   871              regardless of the value of the Ack Policy subfield within the
   997              regardless of the value of the Ack Policy subfield within the
   872              QoS Control field of the QoS data frame. */
   998              QoS Control field of the QoS data frame. */;
   873           if (hdr.IsQosAck ())
   999           if (hdr.IsQosAck () && !ampduSubframe)
   874             {
  1000             {
       
  1001               NS_LOG_DEBUG ("rx QoS unicast/sendAck from=" << hdr.GetAddr2 ());
   875               AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  1002               AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
       
  1003 
   876               RxCompleteBufferedPacketsWithSmallerSequence (it->second.first.GetStartingSequence (),
  1004               RxCompleteBufferedPacketsWithSmallerSequence (it->second.first.GetStartingSequence (),
   877                                                             hdr.GetAddr2 (), hdr.GetQosTid ());
  1005                                                             hdr.GetAddr2 (), hdr.GetQosTid ());
   878               RxCompleteBufferedPacketsUntilFirstLost (hdr.GetAddr2 (), hdr.GetQosTid ());
  1006               RxCompleteBufferedPacketsUntilFirstLost (hdr.GetAddr2 (), hdr.GetQosTid ());
   879               NS_ASSERT (m_sendAckEvent.IsExpired ());
  1007               NS_ASSERT (m_sendAckEvent.IsExpired ());
   880               m_sendAckEvent = Simulator::Schedule (GetSifs (),
  1008               m_sendAckEvent = Simulator::Schedule (GetSifs (),
   881                                                     &MacLow::SendAckAfterData, this,
  1009                                                     &MacLow::SendAckAfterData, this,
   882                                                     hdr.GetAddr2 (),
  1010                                                     hdr.GetAddr2 (),
   883                                                     hdr.GetDuration (),
  1011                                                     hdr.GetDuration (),
   884                                                     txMode,
  1012                                                     txMode,
   885                                                     rxSnr);
  1013                                                     rxSnr);
       
  1014               m_receivedAtLeastOneMpdu = false;
   886             }
  1015             }
   887           else if (hdr.IsQosBlockAck ())
  1016           else if (hdr.IsQosBlockAck ())
   888             {
  1017             {
   889               AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  1018               AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
   890               /* See section 11.5.3 in IEEE 802.11 for mean of this timer */
  1019               /* See section 11.5.3 in IEEE 802.11 for mean of this timer */
   906           m_edcaListeners[ac]->BlockAckInactivityTimeout (hdr.GetAddr2 (), hdr.GetQosTid ());
  1035           m_edcaListeners[ac]->BlockAckInactivityTimeout (hdr.GetAddr2 (), hdr.GetQosTid ());
   907           return;
  1036           return;
   908         }
  1037         }
   909       else if (hdr.IsQosData () && hdr.IsQosNoAck ())
  1038       else if (hdr.IsQosData () && hdr.IsQosNoAck ())
   910         {
  1039         {
   911           NS_LOG_DEBUG ("rx unicast/noAck from=" << hdr.GetAddr2 ());
  1040            if (ampduSubframe)
       
  1041                {
       
  1042                  NS_LOG_DEBUG ("rx Ampdu with No Ack Policy from=" << hdr.GetAddr2 ());
       
  1043                }
       
  1044              else
       
  1045                {
       
  1046                  NS_LOG_DEBUG ("rx unicast/noAck from=" << hdr.GetAddr2 ());
       
  1047                }
   912         }
  1048         }
   913       else if (hdr.IsData () || hdr.IsMgt ())
  1049       else if (hdr.IsData () || hdr.IsMgt ())
   914         {
  1050         {
   915           NS_LOG_DEBUG ("rx unicast/sendAck from=" << hdr.GetAddr2 ());
  1051           if (hdr.IsMgt() && ampduSubframe)
   916           NS_ASSERT (m_sendAckEvent.IsExpired ());
  1052             {
   917           m_sendAckEvent = Simulator::Schedule (GetSifs (),
  1053               NS_FATAL_ERROR ("Received management packet as part of an A-MPDU");
   918                                                 &MacLow::SendAckAfterData, this,
  1054             }
   919                                                 hdr.GetAddr2 (),
  1055           else
   920                                                 hdr.GetDuration (),
  1056             {
   921                                                 txMode,
  1057               NS_LOG_DEBUG ("rx unicast/sendAck from=" << hdr.GetAddr2 ());
   922                                                 rxSnr);
  1058               NS_ASSERT (m_sendAckEvent.IsExpired ());
       
  1059               m_sendAckEvent = Simulator::Schedule (GetSifs (),
       
  1060                                                     &MacLow::SendAckAfterData, this,
       
  1061                                                     hdr.GetAddr2 (),
       
  1062                                                     hdr.GetDuration (),
       
  1063                                                     txMode,
       
  1064                                                     rxSnr);
       
  1065             }
   923         }
  1066         }
   924       goto rxPacket;
  1067       goto rxPacket;
   925     }
  1068     }
   926   else if (hdr.GetAddr1 ().IsGroup ())
  1069   else if (hdr.GetAddr1 ().IsGroup ())
   927     {
  1070     {
   928       if (hdr.IsData () || hdr.IsMgt ())
  1071       if (ampduSubframe)
   929         {
  1072         {
   930           NS_LOG_DEBUG ("rx group from=" << hdr.GetAddr2 ());
  1073           NS_FATAL_ERROR ("Received group addressed packet as part of an A-MPDU");
   931           goto rxPacket;
       
   932         }
  1074         }
   933       else
  1075       else
   934         {
  1076         {
   935           // DROP
  1077           if (hdr.IsData () || hdr.IsMgt ())
       
  1078             {
       
  1079               NS_LOG_DEBUG ("rx group from=" << hdr.GetAddr2 ());
       
  1080               m_receivedAtLeastOneMpdu = false;
       
  1081               goto rxPacket;
       
  1082             }
       
  1083           else
       
  1084             {
       
  1085               // DROP
       
  1086             }
   936         }
  1087         }
   937     }
  1088     }
   938   else if (m_promisc)
  1089   else if (m_promisc)
   939     {
  1090     {
   940       NS_ASSERT (hdr.GetAddr1 () != m_self);
  1091       NS_ASSERT (hdr.GetAddr1 () != m_self);
   953   packet->RemoveTrailer (fcs);
  1104   packet->RemoveTrailer (fcs);
   954   m_rxCallback (packet, &hdr);
  1105   m_rxCallback (packet, &hdr);
   955   return;
  1106   return;
   956 }
  1107 }
   957 
  1108 
       
  1109 uint8_t
       
  1110 MacLow::GetTid (Ptr<const Packet> packet, const WifiMacHeader hdr) const
       
  1111 {
       
  1112   uint8_t tid = 0;
       
  1113   if (hdr.IsQosData ()) 
       
  1114     tid = hdr.GetQosTid ();
       
  1115   else if (hdr.IsBlockAckReq ())
       
  1116     {
       
  1117       CtrlBAckRequestHeader baReqHdr;
       
  1118       packet->PeekHeader (baReqHdr);
       
  1119       tid = baReqHdr.GetTidInfo();
       
  1120      }
       
  1121   else if (hdr.IsBlockAck ())
       
  1122     {
       
  1123       CtrlBAckResponseHeader baRespHdr;
       
  1124       packet->PeekHeader (baRespHdr);
       
  1125       tid = baRespHdr.GetTidInfo ();
       
  1126     }   
       
  1127   return tid;
       
  1128 }
       
  1129 
   958 uint32_t
  1130 uint32_t
   959 MacLow::GetAckSize (void) const
  1131 MacLow::GetAckSize (void) const
   960 {
  1132 {
   961   WifiMacHeader ack;
  1133   WifiMacHeader ack;
   962   ack.SetType (WIFI_MAC_CTL_ACK);
  1134   ack.SetType (WIFI_MAC_CTL_ACK);
  1002   WifiPreamble preamble;
  1174   WifiPreamble preamble;
  1003   if (ackTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1175   if (ackTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1004     preamble= WIFI_PREAMBLE_HT_MF;
  1176     preamble= WIFI_PREAMBLE_HT_MF;
  1005   else
  1177   else
  1006     preamble=WIFI_PREAMBLE_LONG;
  1178     preamble=WIFI_PREAMBLE_LONG;
  1007   return m_phy->CalculateTxDuration (GetAckSize (), ackTxVector, preamble, m_phy->GetFrequency());
  1179   return m_phy->CalculateTxDuration (GetAckSize (), ackTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1008 }
  1180 }
  1009 Time
  1181 Time
  1010 MacLow::GetBlockAckDuration (Mac48Address to, WifiTxVector blockAckReqTxVector, enum BlockAckType type) const
  1182 MacLow::GetBlockAckDuration (Mac48Address to, WifiTxVector blockAckReqTxVector, enum BlockAckType type) const
  1011 {
  1183 {
  1012   /*
  1184   /*
  1020   WifiPreamble preamble;
  1192   WifiPreamble preamble;
  1021   if (blockAckReqTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1193   if (blockAckReqTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1022     preamble= WIFI_PREAMBLE_HT_MF;
  1194     preamble= WIFI_PREAMBLE_HT_MF;
  1023   else
  1195   else
  1024     preamble=WIFI_PREAMBLE_LONG;
  1196     preamble=WIFI_PREAMBLE_LONG;
  1025   return m_phy->CalculateTxDuration (GetBlockAckSize (type), blockAckReqTxVector, preamble, m_phy->GetFrequency());
  1197   return m_phy->CalculateTxDuration (GetBlockAckSize (type), blockAckReqTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1026 }
  1198 }
  1027 Time
  1199 Time
  1028 MacLow::GetCtsDuration (Mac48Address to, WifiTxVector rtsTxVector) const
  1200 MacLow::GetCtsDuration (Mac48Address to, WifiTxVector rtsTxVector) const
  1029 {
  1201 {
  1030   WifiTxVector ctsTxVector = GetCtsTxVectorForRts (to, rtsTxVector.GetMode());
  1202   WifiTxVector ctsTxVector = GetCtsTxVectorForRts (to, rtsTxVector.GetMode());
  1037   WifiPreamble preamble;
  1209   WifiPreamble preamble;
  1038   if (ctsTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1210   if (ctsTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1039     preamble= WIFI_PREAMBLE_HT_MF;
  1211     preamble= WIFI_PREAMBLE_HT_MF;
  1040   else
  1212   else
  1041     preamble=WIFI_PREAMBLE_LONG;
  1213     preamble=WIFI_PREAMBLE_LONG;
  1042   return m_phy->CalculateTxDuration (GetCtsSize (), ctsTxVector, preamble, m_phy->GetFrequency());
  1214   return m_phy->CalculateTxDuration (GetCtsSize (), ctsTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1043 }
  1215 }
  1044 uint32_t
  1216 uint32_t
  1045 MacLow::GetCtsSize (void) const
  1217 MacLow::GetCtsSize (void) const
  1046 {
  1218 {
  1047   WifiMacHeader cts;
  1219   WifiMacHeader cts;
  1049   return cts.GetSize () + 4;
  1221   return cts.GetSize () + 4;
  1050 }
  1222 }
  1051 uint32_t
  1223 uint32_t
  1052 MacLow::GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr) const
  1224 MacLow::GetSize (Ptr<const Packet> packet, const WifiMacHeader *hdr) const
  1053 {
  1225 {
       
  1226   uint32_t size;
  1054   WifiMacTrailer fcs;
  1227   WifiMacTrailer fcs;
  1055   return packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
  1228   if (m_ampdu)
       
  1229      size = packet->GetSize ();
       
  1230   else
       
  1231      size= packet->GetSize () + hdr->GetSize () + fcs.GetSerializedSize ();
       
  1232   return size;
  1056 }
  1233 }
  1057 
  1234 
  1058 WifiTxVector
  1235 WifiTxVector
  1059 MacLow::GetCtsToSelfTxVector (Ptr<const Packet> packet, const WifiMacHeader *hdr) const
  1236 MacLow::GetCtsToSelfTxVector (Ptr<const Packet> packet, const WifiMacHeader *hdr) const
  1060 {
  1237 {
  1125         }
  1302         }
  1126       else
  1303       else
  1127         {
  1304         {
  1128           preamble = WIFI_PREAMBLE_LONG;
  1305           preamble = WIFI_PREAMBLE_LONG;
  1129         }
  1306         }
  1130       txTime += m_phy->CalculateTxDuration (GetRtsSize (), rtsTxVector, preamble, m_phy->GetFrequency());
  1307       txTime += m_phy->CalculateTxDuration (GetRtsSize (), rtsTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1131       txTime += GetCtsDuration (hdr->GetAddr1 (), rtsTxVector);
  1308       txTime += GetCtsDuration (hdr->GetAddr1 (), rtsTxVector);
  1132       txTime += Time (GetSifs () * 2);
  1309       txTime += Time (GetSifs () * 2);
  1133     }
  1310     }
  1134   WifiTxVector dataTxVector = GetDataTxVector (packet, hdr);
  1311   WifiTxVector dataTxVector = GetDataTxVector (packet, hdr);
  1135   //standard says RTS packets can have GF format sec 9.6.0e.1 page 110 bullet b 2
  1312   //standard says RTS packets can have GF format sec 9.6.0e.1 page 110 bullet b 2
  1138   else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1315   else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1139     preamble= WIFI_PREAMBLE_HT_MF;
  1316     preamble= WIFI_PREAMBLE_HT_MF;
  1140   else
  1317   else
  1141     preamble=WIFI_PREAMBLE_LONG;
  1318     preamble=WIFI_PREAMBLE_LONG;
  1142   uint32_t dataSize = GetSize (packet, hdr);
  1319   uint32_t dataSize = GetSize (packet, hdr);
  1143   txTime += m_phy->CalculateTxDuration (dataSize, dataTxVector, preamble, m_phy->GetFrequency());
  1320   txTime += m_phy->CalculateTxDuration (dataSize, dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1144   if (params.MustWaitAck ())
  1321   if (params.MustWaitAck ())
  1145     {
  1322     {
  1146       txTime += GetSifs ();
  1323       txTime += GetSifs ();
  1147       txTime += GetAckDuration (hdr->GetAddr1 (), dataTxVector);
  1324       txTime += GetAckDuration (hdr->GetAddr1 (), dataTxVector);
  1148     }
  1325     }
  1165       else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1342       else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1166         preamble= WIFI_PREAMBLE_HT_MF;
  1343         preamble= WIFI_PREAMBLE_HT_MF;
  1167       else
  1344       else
  1168         preamble=WIFI_PREAMBLE_LONG;
  1345         preamble=WIFI_PREAMBLE_LONG;
  1169       txTime += GetSifs ();
  1346       txTime += GetSifs ();
  1170       txTime += m_phy->CalculateTxDuration (params.GetNextPacketSize (), dataTxVector, preamble, m_phy->GetFrequency());
  1347       txTime += m_phy->CalculateTxDuration (params.GetNextPacketSize (), dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1171     }
  1348     }
  1172   return txTime;
  1349   return txTime;
  1173 }
  1350 }
  1174 
  1351 
  1175 void
  1352 void
  1203            */
  1380            */
  1204           WifiMacHeader cts;
  1381           WifiMacHeader cts;
  1205           cts.SetType (WIFI_MAC_CTL_CTS);
  1382           cts.SetType (WIFI_MAC_CTL_CTS);
  1206           WifiTxVector txVector=GetRtsTxVector (packet, &hdr);
  1383           WifiTxVector txVector=GetRtsTxVector (packet, &hdr);
  1207           Time navCounterResetCtsMissedDelay =
  1384           Time navCounterResetCtsMissedDelay =
  1208             m_phy->CalculateTxDuration (cts.GetSerializedSize (), txVector, preamble, m_phy->GetFrequency()) +
  1385             m_phy->CalculateTxDuration (cts.GetSerializedSize (), txVector, preamble, m_phy->GetFrequency(), 0, 0) +
  1209             Time (2 * GetSifs ()) + Time (2 * GetSlotTime ());
  1386             Time (2 * GetSifs ()) + Time (2 * GetSlotTime ());
  1210           m_navCounterResetCtsMissed = Simulator::Schedule (navCounterResetCtsMissedDelay,
  1387           m_navCounterResetCtsMissed = Simulator::Schedule (navCounterResetCtsMissedDelay,
  1211                                                             &MacLow::NavCounterResetCtsMissed, this,
  1388                                                             &MacLow::NavCounterResetCtsMissed, this,
  1212                                                             Simulator::Now ());
  1389                                                             Simulator::Now ());
  1213         }
  1390         }
  1292                 ", to=" << hdr->GetAddr1 () <<
  1469                 ", to=" << hdr->GetAddr1 () <<
  1293                 ", size=" << packet->GetSize () <<
  1470                 ", size=" << packet->GetSize () <<
  1294                 ", mode=" << txVector.GetMode() <<
  1471                 ", mode=" << txVector.GetMode() <<
  1295                 ", duration=" << hdr->GetDuration () <<
  1472                 ", duration=" << hdr->GetDuration () <<
  1296                 ", seq=0x" << std::hex << m_currentHdr.GetSequenceControl () << std::dec);
  1473                 ", seq=0x" << std::hex << m_currentHdr.GetSequenceControl () << std::dec);
  1297   m_phy->SendPacket (packet, txVector, preamble);
  1474   if (!m_ampdu || hdr->IsRts ())
       
  1475     {
       
  1476       m_phy->SendPacket (packet, txVector, preamble, 0);
       
  1477     }
       
  1478   else
       
  1479     {
       
  1480       Ptr<Packet> newPacket;
       
  1481       Ptr <const Packet> dequeuedPacket;
       
  1482       WifiMacHeader newHdr;
       
  1483       WifiMacTrailer fcs;
       
  1484         uint32_t queueSize = m_aggregateQueue->GetSize ();
       
  1485       bool last = false;
       
  1486       uint8_t packetType = 0;
       
  1487       //Add packet tag
       
  1488       AmpduTag ampdutag;
       
  1489       ampdutag.SetAmpdu (true);
       
  1490       Time delay = Seconds (0);
       
  1491       for ( ; queueSize > 0; queueSize--)
       
  1492         {
       
  1493           dequeuedPacket = m_aggregateQueue->Dequeue (&newHdr);
       
  1494           newPacket = dequeuedPacket->Copy ();
       
  1495           newHdr.SetDuration (hdr->GetDuration ());
       
  1496           newPacket->AddHeader (newHdr);
       
  1497           newPacket->AddTrailer (fcs);
       
  1498           if (queueSize == 1)
       
  1499             {
       
  1500               last = true;
       
  1501               packetType = 2;
       
  1502             }
       
  1503           m_mpduAggregator->AddHeaderAndPad (newPacket, last);
       
  1504 
       
  1505           ampdutag.SetNoOfMpdus(queueSize);
       
  1506           newPacket->AddPacketTag(ampdutag);
       
  1507           if (delay == Seconds (0))
       
  1508             {
       
  1509               NS_LOG_DEBUG("Sending MPDU as part of A-MPDU");
       
  1510               packetType = 1;
       
  1511               m_phy->SendPacket (newPacket, txVector, preamble, packetType);
       
  1512             }
       
  1513           else
       
  1514             {
       
  1515               Simulator::Schedule (delay, &MacLow::SendPacket, this, newPacket, txVector, preamble, packetType);
       
  1516             }
       
  1517           if(queueSize > 1)
       
  1518             delay = delay + m_phy->CalculateTxDuration (GetSize (newPacket, &newHdr), txVector, preamble, m_phy->GetFrequency(), packetType, 0);
       
  1519           preamble = WIFI_PREAMBLE_NONE;
       
  1520         }
       
  1521     }
       
  1522 }
       
  1523 
       
  1524 void
       
  1525 MacLow::SendPacket (Ptr<const Packet> packet, WifiTxVector txVector, WifiPreamble preamble, uint8_t packetType)
       
  1526 {
       
  1527   NS_LOG_DEBUG("Sending MPDU as part of A-MPDU");
       
  1528   m_phy->SendPacket (packet, txVector, preamble, packetType); 
  1298 }
  1529 }
  1299 
  1530 
  1300 void
  1531 void
  1301 MacLow::CtsTimeout (void)
  1532 MacLow::CtsTimeout (void)
  1302 {
  1533 {
  1304   NS_LOG_DEBUG ("cts timeout");
  1535   NS_LOG_DEBUG ("cts timeout");
  1305   /// \todo should check that there was no rx start before now.
  1536   /// \todo should check that there was no rx start before now.
  1306   /// we should restart a new cts timeout now until the expected
  1537   /// we should restart a new cts timeout now until the expected
  1307   /// end of rx if there was a rx start before now.
  1538   /// end of rx if there was a rx start before now.
  1308   m_stationManager->ReportRtsFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1539   m_stationManager->ReportRtsFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1309   m_currentPacket = 0;
  1540   if(m_sentMpdus == 0)
       
  1541     {
       
  1542       m_currentPacket = 0;
       
  1543     }
  1310   MacLowTransmissionListener *listener = m_listener;
  1544   MacLowTransmissionListener *listener = m_listener;
  1311   m_listener = 0;
  1545   m_listener = 0;
       
  1546   m_sentMpdus = 0;
       
  1547   m_ampdu = false;
  1312   listener->MissedCts ();
  1548   listener->MissedCts ();
  1313 }
  1549 }
  1314 void
  1550 void
  1315 MacLow::NormalAckTimeout (void)
  1551 MacLow::NormalAckTimeout (void)
  1316 {
  1552 {
  1320   /// we should restart a new ack timeout now until the expected
  1556   /// we should restart a new ack timeout now until the expected
  1321   /// end of rx if there was a rx start before now.
  1557   /// end of rx if there was a rx start before now.
  1322   m_stationManager->ReportDataFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1558   m_stationManager->ReportDataFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1323   MacLowTransmissionListener *listener = m_listener;
  1559   MacLowTransmissionListener *listener = m_listener;
  1324   m_listener = 0;
  1560   m_listener = 0;
       
  1561   m_sentMpdus = 0;
       
  1562   m_ampdu = false;
  1325   listener->MissedAck ();
  1563   listener->MissedAck ();
  1326 }
  1564 }
  1327 void
  1565 void
  1328 MacLow::FastAckTimeout (void)
  1566 MacLow::FastAckTimeout (void)
  1329 {
  1567 {
  1348   NS_LOG_DEBUG ("block ack timeout");
  1586   NS_LOG_DEBUG ("block ack timeout");
  1349 
  1587 
  1350   m_stationManager->ReportDataFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1588   m_stationManager->ReportDataFailed (m_currentHdr.GetAddr1 (), &m_currentHdr);
  1351   MacLowTransmissionListener *listener = m_listener;
  1589   MacLowTransmissionListener *listener = m_listener;
  1352   m_listener = 0;
  1590   m_listener = 0;
       
  1591   m_sentMpdus = 0;
       
  1592   m_ampdu = false;
  1353   listener->MissedBlockAck ();
  1593   listener->MissedBlockAck ();
  1354 }
  1594 }
  1355 void
  1595 void
  1356 MacLow::SuperFastAckTimeout ()
  1596 MacLow::SuperFastAckTimeout ()
  1357 {
  1597 {
  1405       WifiTxVector dataTxVector = GetDataTxVector (m_currentPacket, &m_currentHdr);
  1645       WifiTxVector dataTxVector = GetDataTxVector (m_currentPacket, &m_currentHdr);
  1406       duration += GetSifs ();
  1646       duration += GetSifs ();
  1407       duration += GetCtsDuration (m_currentHdr.GetAddr1 (), rtsTxVector);
  1647       duration += GetCtsDuration (m_currentHdr.GetAddr1 (), rtsTxVector);
  1408       duration += GetSifs ();
  1648       duration += GetSifs ();
  1409       duration += m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr),
  1649       duration += m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr),
  1410                                               dataTxVector, preamble, m_phy->GetFrequency());
  1650                                               dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1411       duration += GetSifs ();
  1651       duration += GetSifs ();
  1412       duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1652       if (m_txParams.MustWaitBasicBlockAck ())
       
  1653         {
       
  1654           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2(), dataTxVector.GetMode());
       
  1655           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, BASIC_BLOCK_ACK);
       
  1656         }
       
  1657       else if (m_txParams.MustWaitCompressedBlockAck ())
       
  1658         {
       
  1659           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2(), dataTxVector.GetMode());
       
  1660           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
       
  1661         }
       
  1662       else if (m_txParams.MustWaitAck ())
       
  1663         {
       
  1664           duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
       
  1665         }
       
  1666       if (m_txParams.HasNextPacket ())
       
  1667         {
       
  1668           duration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (),
       
  1669                                                   dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
       
  1670           if (m_txParams.MustWaitAck ())
       
  1671             {
       
  1672               duration += GetSifs ();
       
  1673               duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
       
  1674             }
       
  1675         }
  1413     }
  1676     }
  1414   rts.SetDuration (duration);
  1677   rts.SetDuration (duration);
  1415 
  1678 
  1416   Time txDuration = m_phy->CalculateTxDuration (GetRtsSize (), rtsTxVector, preamble, m_phy->GetFrequency());
  1679   Time txDuration = m_phy->CalculateTxDuration (GetRtsSize (), rtsTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1417   Time timerDelay = txDuration + GetCtsTimeout ();
  1680   Time timerDelay = txDuration + GetCtsTimeout ();
  1418 
  1681 
  1419   NS_ASSERT (m_ctsTimeoutEvent.IsExpired ());
  1682   NS_ASSERT (m_ctsTimeoutEvent.IsExpired ());
  1420   NotifyCtsTimeoutStartNow (timerDelay);
  1683   NotifyCtsTimeoutStartNow (timerDelay);
  1421   m_ctsTimeoutEvent = Simulator::Schedule (timerDelay, &MacLow::CtsTimeout, this);
  1684   m_ctsTimeoutEvent = Simulator::Schedule (timerDelay, &MacLow::CtsTimeout, this);
  1439   else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1702   else if (dataTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  1440     preamble= WIFI_PREAMBLE_HT_MF;
  1703     preamble= WIFI_PREAMBLE_HT_MF;
  1441   else
  1704   else
  1442     preamble=WIFI_PREAMBLE_LONG;
  1705     preamble=WIFI_PREAMBLE_LONG;
  1443  
  1706  
  1444   Time txDuration = m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr), dataTxVector, preamble, m_phy->GetFrequency());
  1707   Time txDuration = m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr), dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1445   if (m_txParams.MustWaitNormalAck ())
  1708   if (m_txParams.MustWaitNormalAck ())
  1446     {
  1709     {
  1447       Time timerDelay = txDuration + GetAckTimeout ();
  1710       Time timerDelay = txDuration + GetAckTimeout ();
  1448       NS_ASSERT (m_normalAckTimeoutEvent.IsExpired ());
  1711       NS_ASSERT (m_normalAckTimeoutEvent.IsExpired ());
  1449       NotifyAckTimeoutStartNow (timerDelay);
  1712       NotifyAckTimeoutStartNow (timerDelay);
  1526   else
  1789   else
  1527     {
  1790     {
  1528       if (m_txParams.MustWaitBasicBlockAck ())
  1791       if (m_txParams.MustWaitBasicBlockAck ())
  1529         {
  1792         {
  1530           duration += GetSifs ();
  1793           duration += GetSifs ();
  1531           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), dataTxVector, BASIC_BLOCK_ACK);
  1794           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1795           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, BASIC_BLOCK_ACK);
  1532         }
  1796         }
  1533       else if (m_txParams.MustWaitCompressedBlockAck ())
  1797       else if (m_txParams.MustWaitCompressedBlockAck ())
  1534         {
  1798         {
  1535           duration += GetSifs ();
  1799           duration += GetSifs ();
  1536           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), dataTxVector, COMPRESSED_BLOCK_ACK);
  1800           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1801           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
  1537         }
  1802         }
  1538       else if (m_txParams.MustWaitAck ())
  1803       else if (m_txParams.MustWaitAck ())
  1539         {
  1804         {
  1540           duration += GetSifs ();
  1805           duration += GetSifs ();
  1541           duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1806           duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1542         }
  1807         }
  1543       if (m_txParams.HasNextPacket ())
  1808       if (m_txParams.HasNextPacket ())
  1544         {
  1809         {
  1545           duration += GetSifs ();
  1810           duration += GetSifs ();
  1546           duration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (),
  1811           duration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (),
  1547                                                   dataTxVector, preamble, m_phy->GetFrequency());
  1812                                                   dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1548           if (m_txParams.MustWaitAck ())
  1813           if (m_txParams.MustWaitAck ())
  1549             {
  1814             {
  1550               duration += GetSifs ();
  1815               duration += GetSifs ();
  1551               duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1816               duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1552             }
  1817             }
  1553         }
  1818         }
  1554     }
  1819     }
  1555   m_currentHdr.SetDuration (duration);
  1820   m_currentHdr.SetDuration (duration);
  1556 
  1821 
  1557   m_currentPacket->AddHeader (m_currentHdr);
  1822   if (!m_ampdu)
  1558   WifiMacTrailer fcs;
  1823     {
  1559   m_currentPacket->AddTrailer (fcs);
  1824       m_currentPacket->AddHeader (m_currentHdr);
       
  1825       WifiMacTrailer fcs;
       
  1826       m_currentPacket->AddTrailer (fcs);
       
  1827     }
  1560 
  1828 
  1561   ForwardDown (m_currentPacket, &m_currentHdr, dataTxVector,preamble);
  1829   ForwardDown (m_currentPacket, &m_currentHdr, dataTxVector,preamble);
  1562   m_currentPacket = 0;
  1830   m_currentPacket = 0;
  1563 }
  1831 }
  1564 
  1832 
  1602   else
  1870   else
  1603     {
  1871     {
  1604       WifiTxVector dataTxVector = GetDataTxVector (m_currentPacket, &m_currentHdr);
  1872       WifiTxVector dataTxVector = GetDataTxVector (m_currentPacket, &m_currentHdr);
  1605       duration += GetSifs ();
  1873       duration += GetSifs ();
  1606       duration += m_phy->CalculateTxDuration (GetSize (m_currentPacket,&m_currentHdr),
  1874       duration += m_phy->CalculateTxDuration (GetSize (m_currentPacket,&m_currentHdr),
  1607                                               dataTxVector, preamble, m_phy->GetFrequency());
  1875                                               dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1608       if (m_txParams.MustWaitBasicBlockAck ())
  1876       if (m_txParams.MustWaitBasicBlockAck ())
  1609         {
  1877         {
  1610           
  1878           
  1611           duration += GetSifs ();
  1879           duration += GetSifs ();
  1612           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), dataTxVector, BASIC_BLOCK_ACK);
  1880           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1881           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, BASIC_BLOCK_ACK);
  1613         }
  1882         }
  1614       else if (m_txParams.MustWaitCompressedBlockAck ())
  1883       else if (m_txParams.MustWaitCompressedBlockAck ())
  1615         {
  1884         {
  1616           duration += GetSifs ();
  1885           duration += GetSifs ();
  1617           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), dataTxVector, COMPRESSED_BLOCK_ACK);
  1886           WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1887           duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
  1618         }
  1888         }
  1619       else if (m_txParams.MustWaitAck ())
  1889       else if (m_txParams.MustWaitAck ())
  1620         {
  1890         {
  1621           duration += GetSifs ();
  1891           duration += GetSifs ();
  1622           duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1892           duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1623         }
  1893         }
  1624       if (m_txParams.HasNextPacket ())
  1894       if (m_txParams.HasNextPacket ())
  1625         {
  1895         {
  1626           duration += GetSifs ();
  1896           duration += GetSifs ();
  1627           duration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (),
  1897           duration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (),
  1628                                                   dataTxVector, preamble, m_phy->GetFrequency());
  1898                                                   dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1629           if (m_txParams.MustWaitCompressedBlockAck ())
  1899           if (m_txParams.MustWaitCompressedBlockAck ())
  1630             {
  1900             {
  1631               duration += GetSifs ();
  1901               duration += GetSifs ();
  1632               duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), dataTxVector, COMPRESSED_BLOCK_ACK);
  1902               WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1903               duration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
  1633             }
  1904             }
  1634           else if (m_txParams.MustWaitAck ())
  1905           else if (m_txParams.MustWaitAck ())
  1635             {
  1906             {
  1636               duration += GetSifs ();
  1907               duration += GetSifs ();
  1637               duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1908               duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1646   WifiMacTrailer fcs;
  1917   WifiMacTrailer fcs;
  1647   packet->AddTrailer (fcs);
  1918   packet->AddTrailer (fcs);
  1648 
  1919 
  1649   ForwardDown (packet, &cts, ctsTxVector,preamble);
  1920   ForwardDown (packet, &cts, ctsTxVector,preamble);
  1650 
  1921 
  1651   Time txDuration = m_phy->CalculateTxDuration (GetCtsSize (), ctsTxVector, preamble, m_phy->GetFrequency());
  1922   Time txDuration = m_phy->CalculateTxDuration (GetCtsSize (), ctsTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1652   txDuration += GetSifs ();
  1923   txDuration += GetSifs ();
  1653   NS_ASSERT (m_sendDataEvent.IsExpired ());
  1924   NS_ASSERT (m_sendDataEvent.IsExpired ());
  1654   
  1925   
  1655   m_sendDataEvent = Simulator::Schedule (txDuration,
  1926   m_sendDataEvent = Simulator::Schedule (txDuration,
  1656                                          &MacLow::SendDataAfterCts, this,
  1927                                          &MacLow::SendDataAfterCts, this,
  1714   else
  1985   else
  1715      preamble=WIFI_PREAMBLE_LONG;
  1986      preamble=WIFI_PREAMBLE_LONG;
  1716   
  1987   
  1717   StartDataTxTimers (dataTxVector);
  1988   StartDataTxTimers (dataTxVector);
  1718   Time newDuration = Seconds (0);
  1989   Time newDuration = Seconds (0);
  1719   newDuration += GetSifs ();
  1990   if (m_txParams.MustWaitBasicBlockAck ())
  1720   newDuration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
  1991     {
  1721   Time txDuration = m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr),
  1992       newDuration += GetSifs ();
  1722                                                 dataTxVector, preamble, m_phy->GetFrequency());
  1993       WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  1994       newDuration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, BASIC_BLOCK_ACK);
       
  1995     }
       
  1996   else if (m_txParams.MustWaitCompressedBlockAck ())
       
  1997     {
       
  1998       newDuration += GetSifs ();
       
  1999       WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  2000       newDuration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
       
  2001     }
       
  2002   else if (m_txParams.MustWaitAck ())
       
  2003     {
       
  2004       newDuration += GetSifs ();
       
  2005       newDuration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
       
  2006     }
       
  2007   if (m_txParams.HasNextPacket ())
       
  2008     {
       
  2009       newDuration += GetSifs ();
       
  2010       newDuration += m_phy->CalculateTxDuration (m_txParams.GetNextPacketSize (), dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
       
  2011       if (m_txParams.MustWaitCompressedBlockAck ())
       
  2012         {
       
  2013            newDuration += GetSifs ();
       
  2014            WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (m_currentHdr.GetAddr2 (), dataTxVector.GetMode());
       
  2015            newDuration += GetBlockAckDuration (m_currentHdr.GetAddr1 (), blockAckReqTxVector, COMPRESSED_BLOCK_ACK);
       
  2016         }
       
  2017       else if (m_txParams.MustWaitAck ())
       
  2018         {
       
  2019            newDuration += GetSifs ();
       
  2020            newDuration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxVector);
       
  2021         }
       
  2022     }
       
  2023 
       
  2024   Time txDuration = m_phy->CalculateTxDuration (GetSize (m_currentPacket, &m_currentHdr),dataTxVector, preamble, m_phy->GetFrequency(), 0, 0);
  1723   duration -= txDuration;
  2025   duration -= txDuration;
  1724   duration -= GetSifs ();
  2026   duration -= GetSifs ();
  1725 
  2027 
  1726   duration = std::max (duration, newDuration);
  2028   duration = std::max (duration, newDuration);
  1727   NS_ASSERT (duration >= MicroSeconds (0));
  2029   NS_ASSERT (duration >= MicroSeconds (0));
  1728   m_currentHdr.SetDuration (duration);
  2030   m_currentHdr.SetDuration (duration);
  1729 
  2031 
  1730   m_currentPacket->AddHeader (m_currentHdr);
  2032   if (!m_ampdu)
  1731   WifiMacTrailer fcs;
  2033   {
  1732   m_currentPacket->AddTrailer (fcs);
  2034     m_currentPacket->AddHeader (m_currentHdr);
       
  2035     WifiMacTrailer fcs;
       
  2036     m_currentPacket->AddTrailer (fcs);
       
  2037   }
  1733 
  2038 
  1734   ForwardDown (m_currentPacket, &m_currentHdr, dataTxVector,preamble);
  2039   ForwardDown (m_currentPacket, &m_currentHdr, dataTxVector,preamble);
  1735   m_currentPacket = 0;
  2040   m_currentPacket = 0;
  1736 }
  2041 }
  1737 
  2042 
  1796     preamble=WIFI_PREAMBLE_LONG;
  2101     preamble=WIFI_PREAMBLE_LONG;
  1797   ForwardDown (packet, &ack, ackTxVector, preamble);
  2102   ForwardDown (packet, &ack, ackTxVector, preamble);
  1798 }
  2103 }
  1799 
  2104 
  1800 bool
  2105 bool
       
  2106 MacLow::IsInWindow (uint16_t seq, uint16_t winstart, uint16_t winsize)
       
  2107 {
       
  2108   return ((seq - winstart+ 4096) % 4096) < winsize;
       
  2109 }
       
  2110 
       
  2111 bool 
       
  2112 MacLow::ReceiveMpdu (Ptr<Packet> packet, WifiMacHeader hdr)
       
  2113  {
       
  2114   if (m_stationManager->HasHtSupported ())
       
  2115     {
       
  2116       Mac48Address originator = hdr.GetAddr2 ();
       
  2117       uint8_t tid = 0;
       
  2118       if (hdr.IsQosData ())
       
  2119         tid = hdr.GetQosTid ();
       
  2120       uint16_t seqNumber = hdr.GetSequenceNumber ();
       
  2121       AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
       
  2122       if (it != m_bAckAgreements.end ())
       
  2123         {
       
  2124           //Implement HT immediate Block Ack support for HT Delayed Block Ack is not added yet
       
  2125           if (!QosUtilsIsOldPacket ((*it).second.first.GetStartingSequence (), seqNumber))
       
  2126             { 
       
  2127               StoreMpduIfNeeded (packet, hdr);
       
  2128               if (!IsInWindow(hdr.GetSequenceNumber (), (*it).second.first.GetStartingSequence (), (*it).second.first.GetBufferSize ()))
       
  2129                 {
       
  2130                   uint16_t delta = (seqNumber - (*it).second.first.GetWinEnd()+ 4096) % 4096;
       
  2131                   if (delta > 1)
       
  2132                     {
       
  2133                      (*it).second.first.SetWinEnd (seqNumber);
       
  2134                      int16_t winEnd = (*it).second.first.GetWinEnd ();
       
  2135                      int16_t bufferSize = (*it).second.first.GetBufferSize ();
       
  2136                      uint16_t sum = ((uint16_t)(std::abs(winEnd - bufferSize + 1))) % 4096;
       
  2137                      (*it).second.first.SetStartingSequence (sum);
       
  2138                      RxCompleteBufferedPacketsWithSmallerSequence ((*it).second.first.GetStartingSequence (), originator, tid);
       
  2139                    } 
       
  2140                }
       
  2141               RxCompleteBufferedPacketsUntilFirstLost (originator, tid); //forwards up packets starting from winstart and set winstart to last +1
       
  2142              (*it).second.first.SetWinEnd(((*it).second.first.GetStartingSequence()+(*it).second.first.GetBufferSize()-1)%4096);  
       
  2143            }
       
  2144           return true;
       
  2145         }  
       
  2146       return false;
       
  2147     }
       
  2148  else
       
  2149    return StoreMpduIfNeeded (packet,hdr);
       
  2150 }
       
  2151 
       
  2152 bool
  1801 MacLow::StoreMpduIfNeeded (Ptr<Packet> packet, WifiMacHeader hdr)
  2153 MacLow::StoreMpduIfNeeded (Ptr<Packet> packet, WifiMacHeader hdr)
  1802 {
  2154 {
  1803   AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  2155   AgreementsI it = m_bAckAgreements.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  1804   if (it != m_bAckAgreements.end ())
  2156   if (it != m_bAckAgreements.end ())
  1805     {
  2157     {
  1820 
  2172 
  1821       //Update block ack cache
  2173       //Update block ack cache
  1822       BlockAckCachesI j = m_bAckCaches.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  2174       BlockAckCachesI j = m_bAckCaches.find (std::make_pair (hdr.GetAddr2 (), hdr.GetQosTid ()));
  1823       NS_ASSERT (j != m_bAckCaches.end ());
  2175       NS_ASSERT (j != m_bAckCaches.end ());
  1824       (*j).second.UpdateWithMpdu (&hdr);
  2176       (*j).second.UpdateWithMpdu (&hdr);
  1825 
       
  1826       return true;
  2177       return true;
  1827     }
  2178     }
  1828   return false;
  2179   return false;
  1829 }
  2180 }
  1830 
  2181 
  1892   AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
  2243   AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
  1893   if (it != m_bAckAgreements.end ())
  2244   if (it != m_bAckAgreements.end ())
  1894     {
  2245     {
  1895       uint16_t endSequence = ((*it).second.first.GetStartingSequence () + 2047) % 4096;
  2246       uint16_t endSequence = ((*it).second.first.GetStartingSequence () + 2047) % 4096;
  1896       uint16_t mappedStart = QosUtilsMapSeqControlToUniqueInteger (seq, endSequence);
  2247       uint16_t mappedStart = QosUtilsMapSeqControlToUniqueInteger (seq, endSequence);
  1897       uint16_t guard = (*it).second.second.begin ()->second.GetSequenceControl () & 0xfff0;
       
  1898       BufferedPacketI last = (*it).second.second.begin ();
  2248       BufferedPacketI last = (*it).second.second.begin ();
       
  2249       uint16_t guard;
       
  2250       if (last != (*it).second.second.end ())
       
  2251         guard = (*it).second.second.begin ()->second.GetSequenceControl () & 0xfff0;
  1899 
  2252 
  1900       BufferedPacketI i = (*it).second.second.begin ();
  2253       BufferedPacketI i = (*it).second.second.begin ();
  1901       for (; i != (*it).second.second.end ()
  2254       for (; i != (*it).second.second.end ()
  1902            && QosUtilsMapSeqControlToUniqueInteger ((*i).second.GetSequenceNumber (), endSequence) < mappedStart;)
  2255            && QosUtilsMapSeqControlToUniqueInteger ((*i).second.GetSequenceNumber (), endSequence) < mappedStart;)
  1903         {
  2256         {
  1975       /* All packets already forwarded to WifiMac must be removed from buffer:
  2328       /* All packets already forwarded to WifiMac must be removed from buffer:
  1976       [begin (), lastComplete) */
  2329       [begin (), lastComplete) */
  1977       (*it).second.second.erase ((*it).second.second.begin (), lastComplete);
  2330       (*it).second.second.erase ((*it).second.second.begin (), lastComplete);
  1978     }
  2331     }
  1979 }
  2332 }
  1980 
       
  1981 void
  2333 void
  1982 MacLow::SendBlockAckResponse (const CtrlBAckResponseHeader* blockAck, Mac48Address originator, bool immediate,
  2334 MacLow::SendBlockAckResponse (const CtrlBAckResponseHeader* blockAck, Mac48Address originator, bool immediate,
  1983                               Time duration, WifiMode blockAckReqTxMode)
  2335                               Time duration, WifiMode blockAckReqTxMode)
  1984 {
  2336 {
  1985   Ptr<Packet> packet = Create<Packet> ();
  2337   Ptr<Packet> packet = Create<Packet> ();
  1992   hdr.SetDsNotFrom ();
  2344   hdr.SetDsNotFrom ();
  1993   hdr.SetDsNotTo ();
  2345   hdr.SetDsNotTo ();
  1994   hdr.SetNoRetry ();
  2346   hdr.SetNoRetry ();
  1995   hdr.SetNoMoreFragments ();
  2347   hdr.SetNoMoreFragments ();
  1996 
  2348 
  1997   WifiTxVector blockAckTxVector = GetBlockAckTxVector (originator, blockAckReqTxMode);
  2349   WifiTxVector blockAckReqTxVector = GetBlockAckTxVector (originator, blockAckReqTxMode);
  1998   WifiTxVector blockAckReqTxVector;
       
  1999   blockAckReqTxVector.SetMode(blockAckReqTxMode);
       
  2000   blockAckReqTxVector.SetNss(1);
       
  2001   blockAckReqTxVector.SetStbc(false);
       
  2002 
  2350 
  2003   m_currentPacket = packet;
  2351   m_currentPacket = packet;
  2004   m_currentHdr = hdr;
  2352   m_currentHdr = hdr;
  2005   if (immediate)
  2353   if (immediate)
  2006     {
  2354     {
  2027     }
  2375     }
  2028   m_txParams.DisableNextData ();
  2376   m_txParams.DisableNextData ();
  2029 
  2377 
  2030   if (!immediate)
  2378   if (!immediate)
  2031     {
  2379     {
  2032       StartDataTxTimers (blockAckTxVector);
  2380       StartDataTxTimers (blockAckReqTxVector);
  2033     }
  2381     }
  2034 
  2382 
  2035   NS_ASSERT (duration >= MicroSeconds (0));
  2383   NS_ASSERT (duration >= MicroSeconds (0));
  2036   hdr.SetDuration (duration);
  2384   hdr.SetDuration (duration);
  2037   //here should be present a control about immediate or delayed block ack
  2385   //here should be present a control about immediate or delayed block ack
  2038   //for now we assume immediate
  2386   //for now we assume immediate
  2039   packet->AddHeader (hdr);
  2387   packet->AddHeader (hdr);
  2040   WifiMacTrailer fcs;
  2388   WifiMacTrailer fcs;
  2041   packet->AddTrailer (fcs);
  2389   packet->AddTrailer (fcs);
  2042    WifiPreamble preamble;
  2390   WifiPreamble preamble;
  2043   if (blockAckTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  2391   if (blockAckReqTxVector.GetMode().GetModulationClass () == WIFI_MOD_CLASS_HT)
  2044     preamble= WIFI_PREAMBLE_HT_MF;
  2392     preamble= WIFI_PREAMBLE_HT_MF;
  2045   else
  2393   else
  2046     preamble=WIFI_PREAMBLE_LONG;
  2394     preamble=WIFI_PREAMBLE_LONG;
  2047   ForwardDown (packet, &hdr, blockAckTxVector,preamble);
  2395   ForwardDown (packet, &hdr, blockAckReqTxVector, preamble);
  2048   m_currentPacket = 0;
  2396   m_currentPacket = 0;
       
  2397 }
       
  2398 void 
       
  2399 MacLow::SendBlockAckAfterAmpdu (uint8_t tid, Mac48Address originator, Time duration, WifiMode blockAckReqTxMode)
       
  2400 {
       
  2401   NS_LOG_FUNCTION (this);
       
  2402   CtrlBAckResponseHeader blockAck;
       
  2403   uint16_t seqNumber = 0;
       
  2404   BlockAckCachesI i = m_bAckCaches.find (std::make_pair (originator, tid));
       
  2405   NS_ASSERT (i != m_bAckCaches.end ());
       
  2406   seqNumber = (*i).second.GetWinStart ();
       
  2407 
       
  2408   bool immediate = true;
       
  2409   AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
       
  2410   blockAck.SetStartingSequence (seqNumber);
       
  2411   blockAck.SetTidInfo (tid);
       
  2412   immediate = (*it).second.first.IsImmediateBlockAck ();
       
  2413   blockAck.SetType (COMPRESSED_BLOCK_ACK);
       
  2414   NS_LOG_DEBUG ("Got Implicit block Ack Req with seq " << seqNumber);
       
  2415   (*i).second.FillBlockAckBitmap (&blockAck);  
       
  2416 
       
  2417   SendBlockAckResponse (&blockAck, originator, immediate, duration, blockAckReqTxMode);
  2049 }
  2418 }
  2050 
  2419 
  2051 void
  2420 void
  2052 MacLow::SendBlockAckAfterBlockAckRequest (const CtrlBAckRequestHeader reqHdr, Mac48Address originator,
  2421 MacLow::SendBlockAckAfterBlockAckRequest (const CtrlBAckRequestHeader reqHdr, Mac48Address originator,
  2053                                           Time duration, WifiMode blockAckReqTxMode)
  2422                                           Time duration, WifiMode blockAckReqTxMode)
  2054 {
  2423 {
  2055   NS_LOG_FUNCTION (this);
  2424   NS_LOG_FUNCTION (this);
  2056   CtrlBAckResponseHeader blockAck;
  2425   CtrlBAckResponseHeader blockAck;
  2057   uint8_t tid;
  2426   uint8_t tid = 0;
  2058   bool immediate = false;
  2427   bool immediate = false;
  2059   if (!reqHdr.IsMultiTid ())
  2428   if (!reqHdr.IsMultiTid ())
  2060     {
  2429     {
  2061       tid = reqHdr.GetTidInfo ();
  2430       tid = reqHdr.GetTidInfo ();
  2062       AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
  2431       AgreementsI it = m_bAckAgreements.find (std::make_pair (originator, tid));
  2074               blockAck.SetType (COMPRESSED_BLOCK_ACK);
  2443               blockAck.SetType (COMPRESSED_BLOCK_ACK);
  2075             }
  2444             }
  2076           BlockAckCachesI i = m_bAckCaches.find (std::make_pair (originator, tid));
  2445           BlockAckCachesI i = m_bAckCaches.find (std::make_pair (originator, tid));
  2077           NS_ASSERT (i != m_bAckCaches.end ());
  2446           NS_ASSERT (i != m_bAckCaches.end ());
  2078           (*i).second.FillBlockAckBitmap (&blockAck);
  2447           (*i).second.FillBlockAckBitmap (&blockAck);
  2079 
  2448           NS_LOG_DEBUG ("Got block Ack Req with seq " << reqHdr.GetStartingSequence ());
  2080           /* All packets with smaller sequence than starting sequence control must be passed up to Wifimac
  2449 
  2081            * See 9.10.3 in IEEE 802.11e standard.
  2450           if (!m_stationManager->HasHtSupported())
  2082            */
  2451             {
  2083           RxCompleteBufferedPacketsWithSmallerSequence (reqHdr.GetStartingSequence (), originator, tid);
  2452               /* All packets with smaller sequence than starting sequence control must be passed up to Wifimac
  2084           RxCompleteBufferedPacketsUntilFirstLost (originator, tid);
  2453                * See 9.10.3 in IEEE 802.11e standard.
       
  2454                */
       
  2455               RxCompleteBufferedPacketsWithSmallerSequence (reqHdr.GetStartingSequence (), originator, tid);
       
  2456               RxCompleteBufferedPacketsUntilFirstLost (originator, tid);
       
  2457             }
       
  2458           else
       
  2459             {
       
  2460               if (!QosUtilsIsOldPacket ((*it).second.first.GetStartingSequence(), reqHdr.GetStartingSequence ()))
       
  2461                 { 
       
  2462                   (*it).second.first.SetStartingSequence(reqHdr.GetStartingSequence ());
       
  2463                   (*it).second.first.SetWinEnd(((*it).second.first.GetStartingSequence()+(*it).second.first.GetBufferSize()-1) % 4096);
       
  2464                   RxCompleteBufferedPacketsWithSmallerSequence (reqHdr.GetStartingSequence (), originator, tid);
       
  2465                   RxCompleteBufferedPacketsUntilFirstLost (originator, tid);
       
  2466                   (*it).second.first.SetWinEnd(((*it).second.first.GetStartingSequence()+(*it).second.first.GetBufferSize()-1) % 4096);
       
  2467                 }
       
  2468             }
  2085         }
  2469         }
  2086       else
  2470       else
  2087         {
  2471         {
  2088           NS_LOG_DEBUG ("there's not a valid block ack agreement with " << originator);
  2472           NS_LOG_DEBUG ("there's not a valid block ack agreement with " << originator);
  2089         }
  2473         }
  2102   if (agreement.GetTimeout () != 0)
  2486   if (agreement.GetTimeout () != 0)
  2103     {
  2487     {
  2104       NS_ASSERT (agreement.m_inactivityEvent.IsRunning ());
  2488       NS_ASSERT (agreement.m_inactivityEvent.IsRunning ());
  2105       agreement.m_inactivityEvent.Cancel ();
  2489       agreement.m_inactivityEvent.Cancel ();
  2106       Time timeout = MicroSeconds (1024 * agreement.GetTimeout ());
  2490       Time timeout = MicroSeconds (1024 * agreement.GetTimeout ());
  2107 
       
  2108       AcIndex ac = QosUtilsMapTidToAc (agreement.GetTid ());
  2491       AcIndex ac = QosUtilsMapTidToAc (agreement.GetTid ());
  2109       //std::map<AcIndex, MacLowTransmissionListener*>::iterator it = m_edcaListeners.find (ac);
  2492       //std::map<AcIndex, MacLowTransmissionListener*>::iterator it = m_edcaListeners.find (ac);
  2110       //NS_ASSERT (it != m_edcaListeners.end ());
  2493       //NS_ASSERT (it != m_edcaListeners.end ());
  2111 
  2494 
  2112       agreement.m_inactivityEvent = Simulator::Schedule (timeout,
  2495       agreement.m_inactivityEvent = Simulator::Schedule (timeout,
  2121 MacLow::RegisterBlockAckListenerForAc (enum AcIndex ac, MacLowBlockAckEventListener *listener)
  2504 MacLow::RegisterBlockAckListenerForAc (enum AcIndex ac, MacLowBlockAckEventListener *listener)
  2122 {
  2505 {
  2123   m_edcaListeners.insert (std::make_pair (ac, listener));
  2506   m_edcaListeners.insert (std::make_pair (ac, listener));
  2124 }
  2507 }
  2125 
  2508 
       
  2509 void
       
  2510 MacLow::SetMpduAggregator (Ptr<MpduAggregator> aggregator)
       
  2511 {
       
  2512   m_mpduAggregator = aggregator;
       
  2513 }
       
  2514 
       
  2515 void
       
  2516 MacLow::DeaggregateAmpduAndReceive (Ptr<Packet> aggregatedPacket, double rxSnr, WifiMode txMode, WifiPreamble preamble)
       
  2517 {
       
  2518   m_currentMode = txMode;
       
  2519   AmpduTag ampdu;
       
  2520   bool normalAck = false;
       
  2521   bool ampduSubframe = false;
       
  2522   if (aggregatedPacket->RemovePacketTag(ampdu))
       
  2523     {
       
  2524       ampduSubframe = true;
       
  2525       MpduAggregator::DeaggregatedMpdus packets =  MpduAggregator::Deaggregate (aggregatedPacket);
       
  2526       MpduAggregator::DeaggregatedMpdusCI n = packets.begin ();
       
  2527 
       
  2528       WifiMacHeader firsthdr;
       
  2529       (*n).first->PeekHeader(firsthdr);
       
  2530       NS_LOG_DEBUG ("duration/id=" << firsthdr.GetDuration ());
       
  2531       NotifyNav ((*n).first,firsthdr, txMode, preamble);
       
  2532       if (firsthdr.GetAddr1 () == m_self)
       
  2533         {
       
  2534           m_receivedAtLeastOneMpdu = true;
       
  2535           if (firsthdr.IsAck () || firsthdr.IsBlockAck () || firsthdr.IsBlockAckReq ())
       
  2536               ReceiveOk ((*n).first, rxSnr, txMode, preamble, ampduSubframe);
       
  2537           else if (firsthdr.IsData () || firsthdr.IsQosData ())
       
  2538             {
       
  2539               NS_LOG_DEBUG ("Deaagregate packet with sequence=" << firsthdr.GetSequenceNumber ());
       
  2540               ReceiveOk ((*n).first, rxSnr, txMode, preamble, ampduSubframe);
       
  2541               if (firsthdr.IsQosAck ())
       
  2542                 {
       
  2543                   NS_LOG_DEBUG ("Normal Ack");
       
  2544                   normalAck = true;
       
  2545                 }
       
  2546             } 
       
  2547           else
       
  2548               NS_FATAL_ERROR ("Received A-MPDU with invalid first MPDU type");
       
  2549         }
       
  2550 
       
  2551       if (normalAck && (ampdu.GetNoOfMpdus () == 1))
       
  2552         { 
       
  2553           //send block Ack
       
  2554           if (firsthdr.IsBlockAckReq ())
       
  2555             {
       
  2556               NS_FATAL_ERROR ("Sending a BlockAckReq with QosPolicy equal to Normal Ack");
       
  2557             }
       
  2558           uint8_t tid = firsthdr.GetQosTid ();
       
  2559           AgreementsI it = m_bAckAgreements.find (std::make_pair (firsthdr.GetAddr2 (), tid));
       
  2560           if (it != m_bAckAgreements.end ())
       
  2561             { 
       
  2562               NS_ASSERT (m_sendAckEvent.IsExpired ());
       
  2563               /* See section 11.5.3 in IEEE 802.11 for mean of this timer */
       
  2564               ResetBlockAckInactivityTimerIfNeeded (it->second.first);
       
  2565               NS_LOG_DEBUG ("rx A-MPDU/sendImmediateBlockAck from=" << firsthdr.GetAddr2 ());
       
  2566               m_sendAckEvent = Simulator::Schedule (GetSifs (),
       
  2567                                                     &MacLow::SendBlockAckAfterAmpdu, this,
       
  2568                                                     firsthdr.GetQosTid(),
       
  2569                                                     firsthdr.GetAddr2 (),
       
  2570                                                     firsthdr.GetDuration (),
       
  2571                                                     txMode);
       
  2572             } 
       
  2573           else
       
  2574             { 
       
  2575               NS_LOG_DEBUG ("There's not a valid agreement for this block ack request.");
       
  2576             }
       
  2577           m_receivedAtLeastOneMpdu = false;
       
  2578         }
       
  2579     }
       
  2580   else
       
  2581     {     
       
  2582           ReceiveOk (aggregatedPacket,rxSnr, txMode, preamble, ampduSubframe);
       
  2583     }
       
  2584 }
       
  2585 
       
  2586 bool 
       
  2587 MacLow::StopAggregation(Ptr<const Packet> peekedPacket, WifiMacHeader peekedHdr, Ptr<Packet> aggregatedPacket, uint16_t size) const
       
  2588 {
       
  2589     WifiPreamble preamble;
       
  2590     WifiTxVector dataTxVector = GetDataTxVector (m_currentPacket, &m_currentHdr);
       
  2591     if (m_phy->GetGreenfield () && m_stationManager->GetGreenfieldSupported (m_currentHdr.GetAddr1 ()))
       
  2592         preamble = WIFI_PREAMBLE_HT_GF;
       
  2593     else
       
  2594         preamble = WIFI_PREAMBLE_HT_MF;
       
  2595     
       
  2596     if (peekedPacket == 0)
       
  2597         return true;
       
  2598     
       
  2599     //An HT STA shall not transmit a PPDU that has a duration that is greater than aPPDUMaxTime (10 milliseconds)
       
  2600     if(m_phy->CalculateTxDuration (aggregatedPacket->GetSize () + peekedPacket->GetSize () + peekedHdr.GetSize () +WIFI_MAC_FCS_LENGTH,dataTxVector, preamble, m_phy->GetFrequency(), 0, 0) > MilliSeconds(10))
       
  2601         return true;
       
  2602     
       
  2603     if (!m_mpduAggregator->CanBeAggregated (peekedPacket->GetSize () + peekedHdr.GetSize () + WIFI_MAC_FCS_LENGTH, aggregatedPacket, size))
       
  2604         return true;
       
  2605     
       
  2606     return false;
       
  2607 }
       
  2608 
       
  2609 Ptr<Packet>
       
  2610 MacLow::AggregateToAmpdu (Ptr<const Packet> packet, const WifiMacHeader hdr)
       
  2611 {
       
  2612   NS_ASSERT (m_aggregateQueue->GetSize () == 0);
       
  2613   bool isAmpdu = false;
       
  2614   Ptr<Packet> newPacket;
       
  2615   WifiMacHeader peekedHdr;
       
  2616   newPacket = packet->Copy();
       
  2617   //missing hdr.IsAck() since we have no means of knowing the Tid of the Ack yet
       
  2618   if (hdr.IsQosData() || hdr.IsBlockAck()|| hdr.IsBlockAckReq())
       
  2619     {
       
  2620       Time tstamp;
       
  2621       uint8_t tid = GetTid (packet, hdr);
       
  2622       Ptr<WifiMacQueue> queue;
       
  2623       AcIndex ac = QosUtilsMapTidToAc (tid);
       
  2624       //since a blockack agreement always preceeds mpdu aggregation there should always exist blockAck listener
       
  2625       std::map<AcIndex, MacLowBlockAckEventListener*>::const_iterator listenerIt= m_edcaListeners.find(ac);
       
  2626       NS_ASSERT (listenerIt != m_edcaListeners.end ());
       
  2627       queue = listenerIt->second->GetQueue();
       
  2628       
       
  2629       if (!hdr.GetAddr1 ().IsBroadcast () && m_mpduAggregator!= 0)
       
  2630         {
       
  2631           //Have to make sure that their exist a block Ack agreement before sending an AMPDU (BlockAck Manager)
       
  2632           if (listenerIt->second->GetBlockAckAgreementExists (hdr.GetAddr1(), tid))
       
  2633             {
       
  2634               /* here is performed mpdu aggregation */
       
  2635               /* MSDU aggregation happened in edca if the user asked for it so m_currentPacket may contains a normal packet or a A-MSDU*/
       
  2636               Ptr<Packet> currentAggregatedPacket = Create<Packet> ();
       
  2637               peekedHdr = hdr;
       
  2638               uint16_t startingSequenceNumber = 0;
       
  2639               uint16_t currentSequenceNumber = 0;
       
  2640               uint8_t qosPolicy = 0;
       
  2641               uint16_t blockAckSize = 0;
       
  2642               bool aggregated = false;
       
  2643               int i = 0;
       
  2644               Ptr<Packet> aggPacket = newPacket->Copy ();
       
  2645 
       
  2646               if (!hdr.IsBlockAckReq())
       
  2647                 {
       
  2648                   if (!hdr.IsBlockAck())
       
  2649                     {
       
  2650                        startingSequenceNumber = peekedHdr.GetSequenceNumber();
       
  2651                        peekedHdr.SetQosAckPolicy (WifiMacHeader::NORMAL_ACK);
       
  2652                     }
       
  2653                   currentSequenceNumber = peekedHdr.GetSequenceNumber();
       
  2654                   newPacket->AddHeader (peekedHdr);
       
  2655                   WifiMacTrailer fcs;
       
  2656                   newPacket->AddTrailer (fcs);
       
  2657 
       
  2658                   aggregated=m_mpduAggregator->Aggregate (newPacket, currentAggregatedPacket);
       
  2659 
       
  2660                   if (aggregated)
       
  2661                     {
       
  2662                       NS_LOG_DEBUG ("Adding packet with Sequence number " << peekedHdr.GetSequenceNumber()<<" to A-MPDU");
       
  2663                       i++;
       
  2664                       m_sentMpdus++;
       
  2665                       m_aggregateQueue->Enqueue (aggPacket, peekedHdr);
       
  2666                     }
       
  2667                 } 
       
  2668               else if (hdr.IsBlockAckReq())
       
  2669                 {
       
  2670                   blockAckSize = packet->GetSize() + hdr.GetSize() + WIFI_MAC_FCS_LENGTH;
       
  2671                   qosPolicy = 3; //if the last subrame is block ack req then set ack policy of all frames to blockack
       
  2672                   CtrlBAckRequestHeader blockAckReq;
       
  2673                   packet->PeekHeader (blockAckReq);
       
  2674                   startingSequenceNumber = blockAckReq.GetStartingSequence ();
       
  2675                 }
       
  2676               aggregated = false;
       
  2677               bool retry = false;
       
  2678               //looks for other packets to the same destination with the same Tid need to extend that to include MSDUs
       
  2679               Ptr<const Packet> peekedPacket = listenerIt->second->PeekNextPacketInBaQueue (peekedHdr, peekedHdr.GetAddr1 (), tid, &tstamp);
       
  2680               if (peekedPacket == 0) 
       
  2681                 {
       
  2682                   peekedPacket = queue->PeekByTidAndAddress (&peekedHdr, tid,
       
  2683                                                              WifiMacHeader::ADDR1,
       
  2684                                                              hdr.GetAddr1 (), &tstamp);
       
  2685                   currentSequenceNumber = listenerIt->second->PeekNextSequenceNumberfor (&peekedHdr);
       
  2686                 }
       
  2687               else
       
  2688                 {
       
  2689                   retry = true;
       
  2690                   currentSequenceNumber = peekedHdr.GetSequenceNumber(); 
       
  2691                 }
       
  2692 
       
  2693                while (IsInWindow (currentSequenceNumber, startingSequenceNumber, 64) && !StopAggregation (peekedPacket, peekedHdr, currentAggregatedPacket, blockAckSize))
       
  2694 //&& listenerIt->second->GetNOutstandingPackets (hdr.GetAddr1(), tid) < 63
       
  2695                 {
       
  2696                   //for now always send AMPDU with normal ACK
       
  2697                   if (retry == false)
       
  2698                     {
       
  2699                       currentSequenceNumber = listenerIt->second->GetNextSequenceNumberfor (&peekedHdr);
       
  2700                       peekedHdr.SetSequenceNumber (currentSequenceNumber);
       
  2701                       peekedHdr.SetFragmentNumber (0);
       
  2702                       peekedHdr.SetNoMoreFragments ();
       
  2703                       peekedHdr.SetNoRetry ();
       
  2704                     }                      
       
  2705                   if (qosPolicy == 0)
       
  2706                       peekedHdr.SetQosAckPolicy (WifiMacHeader::NORMAL_ACK);
       
  2707                   else
       
  2708                       peekedHdr.SetQosAckPolicy (WifiMacHeader::BLOCK_ACK);
       
  2709 
       
  2710                   newPacket = peekedPacket->Copy ();
       
  2711                   Ptr<Packet> aggPacket = newPacket->Copy ();
       
  2712                  
       
  2713                   newPacket->AddHeader (peekedHdr);
       
  2714                   WifiMacTrailer fcs;
       
  2715                   newPacket->AddTrailer (fcs);
       
  2716                   aggregated = m_mpduAggregator->Aggregate (newPacket, currentAggregatedPacket);
       
  2717                   if (aggregated)
       
  2718                     {
       
  2719                       m_aggregateQueue->Enqueue (aggPacket, peekedHdr);
       
  2720                       if (i == 1 && hdr.IsQosData ())
       
  2721                       {
       
  2722                           listenerIt->second->CompleteMpduTx (packet, hdr, tstamp);
       
  2723                       }
       
  2724                       NS_LOG_DEBUG ("Adding packet with Sequence number " << peekedHdr.GetSequenceNumber()<<" to A-MPDU");
       
  2725                       i++;
       
  2726                       isAmpdu = true;
       
  2727                       m_sentMpdus++;
       
  2728                       listenerIt->second->CompleteMpduTx (peekedPacket, peekedHdr, tstamp);
       
  2729                       if (retry)
       
  2730                           listenerIt->second->RemoveFromBaQueue(tid, hdr.GetAddr1 (), peekedHdr.GetSequenceNumber ());
       
  2731                       else
       
  2732                           queue->Remove (peekedPacket);
       
  2733                       newPacket = 0;
       
  2734                     }
       
  2735                   else
       
  2736                       break;
       
  2737                   if (retry == true)
       
  2738                     {
       
  2739                       peekedPacket = listenerIt->second->PeekNextPacketInBaQueue(peekedHdr, hdr.GetAddr1(), tid, &tstamp);
       
  2740                       if (peekedPacket == 0)
       
  2741                         {
       
  2742                           //I reached the first packet that I added to this A-MPDU
       
  2743                           retry = false;
       
  2744                           peekedPacket = queue->PeekByTidAndAddress (&peekedHdr, tid,
       
  2745                                                                      WifiMacHeader::ADDR1, hdr.GetAddr1 (), &tstamp);
       
  2746                           if (peekedPacket != 0)
       
  2747                             {
       
  2748                               //find what will the sequence number be so that we don't send more than 64 packets apart
       
  2749                               currentSequenceNumber = listenerIt->second->PeekNextSequenceNumberfor (&peekedHdr);
       
  2750                             }   
       
  2751                         }
       
  2752                       else
       
  2753                           currentSequenceNumber = peekedHdr.GetSequenceNumber();
       
  2754                     }
       
  2755                   else
       
  2756                     {
       
  2757                       peekedPacket = queue->PeekByTidAndAddress (&peekedHdr, tid,
       
  2758                                                                  WifiMacHeader::ADDR1, hdr.GetAddr1 (), &tstamp);
       
  2759                       if (peekedPacket != 0)
       
  2760                         {
       
  2761                           //find what will the sequence number be so that we don't send more than 64 packets apart
       
  2762                           currentSequenceNumber = listenerIt->second->PeekNextSequenceNumberfor (&peekedHdr);
       
  2763                         }   
       
  2764                     }
       
  2765                 }
       
  2766               if (isAmpdu)
       
  2767                 {
       
  2768                   if (hdr.IsBlockAckReq())
       
  2769                     {
       
  2770                       newPacket = packet->Copy();
       
  2771                       peekedHdr = hdr;
       
  2772                       Ptr<Packet> aggPacket = newPacket->Copy();
       
  2773                       m_aggregateQueue->Enqueue (aggPacket, peekedHdr);
       
  2774                       newPacket->AddHeader (peekedHdr);
       
  2775                       WifiMacTrailer fcs;
       
  2776                       newPacket->AddTrailer (fcs);
       
  2777                       m_mpduAggregator->Aggregate (newPacket, currentAggregatedPacket);
       
  2778                     }
       
  2779                   if (qosPolicy==0)
       
  2780                     {
       
  2781                          listenerIt->second->CompleteTransfer(hdr.GetAddr1 (),tid);
       
  2782                     }
       
  2783                   //Add packet tag
       
  2784                   AmpduTag ampdutag;
       
  2785                   ampdutag.SetAmpdu (true);
       
  2786                   ampdutag.SetNoOfMpdus(i);
       
  2787                   newPacket = currentAggregatedPacket;
       
  2788                   newPacket->AddPacketTag(ampdutag);
       
  2789                   currentAggregatedPacket = 0;
       
  2790                   NS_LOG_DEBUG ("tx unicast A-MPDU");
       
  2791                   listenerIt->second->SetAmpdu(true);
       
  2792                 }
       
  2793               else
       
  2794                 {
       
  2795                   uint32_t queueSize = m_aggregateQueue->GetSize ();
       
  2796                   NS_ASSERT (queueSize <= 2); //since it is not an A-MPDU then only 2 packets should have been added to the queue no more
       
  2797                   if (queueSize >= 1)
       
  2798                     {
       
  2799                       //remove any packets that we added to the aggregate queue
       
  2800                       FlushAggregateQueue ();
       
  2801                     }
       
  2802                 }
       
  2803             }
       
  2804         }
       
  2805     }
       
  2806   return newPacket;
       
  2807 }
       
  2808 
       
  2809 void
       
  2810 MacLow::FlushAggregateQueue (void)
       
  2811 {
       
  2812   NS_LOG_DEBUG("Flush aggregate queue");
       
  2813   m_aggregateQueue->Flush ();
       
  2814 }
       
  2815 
  2126 } // namespace ns3
  2816 } // namespace ns3