1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009, 2010 MIRKO BANCHI
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Authors: Mirko Banchi <mk.banchi@gmail.com>
19  *          Tommaso Pecorella <tommaso.pecorella@unifi.it>
20  */
21 
22 #include "ns3/log.h"
23 #include "originator-block-ack-agreement.h"
24 #include "wifi-mac-queue-item.h"
25 #include "wifi-utils.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("OriginatorBlockAckAgreement");
30 
OriginatorBlockAckAgreement(Mac48Address recipient,uint8_t tid)31 OriginatorBlockAckAgreement::OriginatorBlockAckAgreement (Mac48Address recipient, uint8_t tid)
32   : BlockAckAgreement (recipient, tid),
33     m_state (PENDING)
34 {
35 }
36 
~OriginatorBlockAckAgreement()37 OriginatorBlockAckAgreement::~OriginatorBlockAckAgreement ()
38 {
39 }
40 
41 void
SetState(State state)42 OriginatorBlockAckAgreement::SetState (State state)
43 {
44   m_state = state;
45 }
46 
47 bool
IsPending(void) const48 OriginatorBlockAckAgreement::IsPending (void) const
49 {
50   return (m_state == PENDING) ? true : false;
51 }
52 
53 bool
IsEstablished(void) const54 OriginatorBlockAckAgreement::IsEstablished (void) const
55 {
56   return (m_state == ESTABLISHED) ? true : false;
57 }
58 
59 bool
IsRejected(void) const60 OriginatorBlockAckAgreement::IsRejected (void) const
61 {
62   return (m_state == REJECTED) ? true : false;
63 }
64 
65 bool
IsNoReply(void) const66 OriginatorBlockAckAgreement::IsNoReply (void) const
67 {
68   return (m_state == NO_REPLY) ? true : false;
69 }
70 
71 bool
IsReset(void) const72 OriginatorBlockAckAgreement::IsReset (void) const
73 {
74   return (m_state == RESET) ? true : false;
75 }
76 
77 uint16_t
GetStartingSequence(void) const78 OriginatorBlockAckAgreement::GetStartingSequence (void) const
79 {
80   if (m_txWindow.GetWinSize () == 0)
81     {
82       // the TX window has not been initialized yet
83       return m_startingSeq;
84     }
85   return m_txWindow.GetWinStart ();
86 }
87 
88 std::size_t
GetDistance(uint16_t seqNumber) const89 OriginatorBlockAckAgreement::GetDistance (uint16_t seqNumber) const
90 {
91   return BlockAckAgreement::GetDistance (seqNumber, m_txWindow.GetWinStart ());
92 }
93 
94 void
InitTxWindow(void)95 OriginatorBlockAckAgreement::InitTxWindow (void)
96 {
97   m_txWindow.Init (m_startingSeq, m_bufferSize);
98 }
99 
100 void
AdvanceTxWindow(void)101 OriginatorBlockAckAgreement::AdvanceTxWindow (void)
102 {
103   while (m_txWindow.At (0))
104     {
105       m_txWindow.Advance (1); // reset the current head -- ensures loop termination
106     }
107 }
108 
109 void
NotifyTransmittedMpdu(Ptr<const WifiMacQueueItem> mpdu)110 OriginatorBlockAckAgreement::NotifyTransmittedMpdu (Ptr<const WifiMacQueueItem> mpdu)
111 {
112   uint16_t mpduSeqNumber = mpdu->GetHeader ().GetSequenceNumber ();
113   uint16_t distance = GetDistance (mpduSeqNumber);
114 
115   if (distance >= SEQNO_SPACE_HALF_SIZE)
116     {
117       NS_LOG_DEBUG ("Transmitted an old MPDU, do nothing.");
118       return;
119     }
120 
121   // advance the transmit window if an MPDU beyond the current transmit window
122   // is transmitted (see Section 10.24.7.7 of 802.11-2016)
123   if (distance >= m_txWindow.GetWinSize ())
124     {
125       std::size_t count = distance - m_txWindow.GetWinSize () + 1;
126       m_txWindow.Advance (count);
127       // transmit window may advance further
128       AdvanceTxWindow ();
129       NS_LOG_DEBUG ("Transmitted MPDU beyond current transmit window. New starting sequence number: "
130                     << m_txWindow.GetWinStart ());
131     }
132 }
133 
134 void
NotifyAckedMpdu(Ptr<const WifiMacQueueItem> mpdu)135 OriginatorBlockAckAgreement::NotifyAckedMpdu (Ptr<const WifiMacQueueItem> mpdu)
136 {
137   uint16_t mpduSeqNumber = mpdu->GetHeader ().GetSequenceNumber ();
138   uint16_t distance = GetDistance (mpduSeqNumber);
139 
140   if (distance >= SEQNO_SPACE_HALF_SIZE)
141     {
142       NS_LOG_DEBUG ("Acked an old MPDU, do nothing.");
143       return;
144     }
145 
146   // when an MPDU is transmitted, the transmit window is updated such that the
147   // transmitted MPDU is in the window, hence we cannot be notified of the
148   // acknowledgment of an MPDU which is beyond the transmit window
149   m_txWindow.At (distance) = true;
150 
151   // the starting sequence number can be advanced to the sequence number of
152   // the nearest unacknowledged MPDU
153   AdvanceTxWindow ();
154   NS_LOG_DEBUG ("Starting sequence number: " << m_txWindow.GetWinStart ());
155 }
156 
157 void
NotifyDiscardedMpdu(Ptr<const WifiMacQueueItem> mpdu)158 OriginatorBlockAckAgreement::NotifyDiscardedMpdu (Ptr<const WifiMacQueueItem> mpdu)
159 {
160   uint16_t mpduSeqNumber = mpdu->GetHeader ().GetSequenceNumber ();
161   uint16_t distance = GetDistance (mpduSeqNumber);
162 
163   if (distance >= SEQNO_SPACE_HALF_SIZE)
164     {
165       NS_LOG_DEBUG ("Discarded an old MPDU, do nothing.");
166       return;
167     }
168 
169   m_txWindow.Advance (distance + 1);
170   // transmit window may advance further
171   AdvanceTxWindow ();
172   NS_LOG_DEBUG ("Discarded MPDU within current transmit window. New starting sequence number: "
173                 << m_txWindow.GetWinStart ());
174 }
175 
176 } //namespace ns3
177