try to simplify duration calculations
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 24 Oct 2007 16:02:10 +0200
changeset 2022 33579d27ce2a
parent 2021 99a028507709
child 2023 df3bd08f1173
try to simplify duration calculations
src/devices/wifi/mac-low.cc
src/devices/wifi/mac-low.h
src/devices/wifi/wifi-mac-header.cc
src/devices/wifi/wifi-mac-header.h
--- a/src/devices/wifi/mac-low.cc	Wed Oct 24 15:05:17 2007 +0200
+++ b/src/devices/wifi/mac-low.cc	Wed Oct 24 16:02:10 2007 +0200
@@ -419,8 +419,8 @@
           m_sendCtsEvent = Simulator::Schedule (GetSifs (),
                                                 &MacLow::SendCtsAfterRts, this,
                                                 hdr.GetAddr2 (), 
-                                                MicroSeconds (hdr.GetDurationUs ()),
-                                                GetCtsTxModeForRts (hdr.GetAddr2 (), txMode),
+                                                hdr.GetDuration (),
+                                                txMode,
                                                 rxSnr);
         } 
       else 
@@ -446,7 +446,7 @@
       m_sendDataEvent = Simulator::Schedule (GetSifs (), 
                                              &MacLow::SendDataAfterCts, this, 
                                              hdr.GetAddr1 (),
-                                             MicroSeconds (hdr.GetDurationUs ()),
+                                             hdr.GetDuration (),
                                              txMode);
     } 
   else if (hdr.IsAck () &&
@@ -505,8 +505,8 @@
           m_sendAckEvent = Simulator::Schedule (GetSifs (),
                                                 &MacLow::SendAckAfterData, this,
                                                 hdr.GetAddr2 (), 
-                                                MicroSeconds (hdr.GetDurationUs ()),
-                                                GetAckTxModeForData (hdr.GetAddr2 (), txMode),
+                                                hdr.GetDuration (),
+                                                txMode,
                                                 rxSnr);
         }
       goto rxPacket;
@@ -549,6 +549,18 @@
   rts.SetType (WIFI_MAC_CTL_RTS);
   return rts.GetSize () + 4;
 }
+Time
+MacLow::GetAckDuration (Mac48Address to, WifiMode dataTxMode) const
+{
+  WifiMode ackMode = GetAckTxModeForData (to, dataTxMode);
+  return m_phy->CalculateTxDuration (GetAckSize (), ackMode, WIFI_PREAMBLE_LONG);
+}
+Time
+MacLow::GetCtsDuration (Mac48Address to, WifiMode rtsTxMode) const
+{
+  WifiMode ctsMode = GetCtsTxModeForRts (to, rtsTxMode);
+  return m_phy->CalculateTxDuration (GetCtsSize (), ctsMode, WIFI_PREAMBLE_LONG);
+}
 uint32_t 
 MacLow::GetCtsSize (void) const
 {
@@ -616,16 +628,14 @@
   if (params.MustSendRts ()) 
     {
       txTime += m_phy->CalculateTxDuration (GetRtsSize (), rtsMode, WIFI_PREAMBLE_LONG);
-      WifiMode ctsMode = GetCtsTxModeForRts (m_currentHdr.GetAddr1 (), rtsMode);
-      txTime += m_phy->CalculateTxDuration (GetCtsSize (), ctsMode, WIFI_PREAMBLE_LONG);
+      txTime += GetCtsDuration (m_currentHdr.GetAddr1 (), rtsMode);
       txTime += GetSifs () * Scalar (2);
     }
   txTime += m_phy->CalculateTxDuration (dataSize, dataMode, WIFI_PREAMBLE_LONG);
   if (params.MustWaitAck ())
     {
-      WifiMode ackMode = GetAckTxModeForData (m_currentHdr.GetAddr1 (), dataMode);
       txTime += GetSifs ();
-      txTime += m_phy->CalculateTxDuration (GetAckSize (), ackMode, WIFI_PREAMBLE_LONG);
+      txTime += GetAckDuration (m_currentHdr.GetAddr1 (), dataMode);
     }
   return txTime;
 }
@@ -658,7 +668,7 @@
   Time oldNavStart = m_lastNavStart;
   Time oldNavEnd = oldNavStart + m_lastNavDuration;
   Time newNavStart = at;
-  Time duration = MicroSeconds (hdr->GetDurationUs ());
+  Time duration = hdr->GetDuration ();
 
   if (hdr->IsCfpoll () &&
       hdr->GetAddr2 () == m_device->GetBssid ()) 
@@ -775,16 +785,14 @@
   else 
     {
       WifiMode dataTxMode = GetDataTxMode (m_currentHdr.GetAddr1 (), GetCurrentSize ());
-      WifiMode ackTxMode = GetAckTxModeForData (m_currentHdr.GetAddr1 (), dataTxMode);
-      WifiMode ctsTxMode = GetCtsTxModeForRts (m_currentHdr.GetAddr1 (), rtsTxMode);
       duration += GetSifs ();
-      duration += m_phy->CalculateTxDuration (GetCtsSize (), ctsTxMode, WIFI_PREAMBLE_LONG);
+      duration += GetCtsDuration (m_currentHdr.GetAddr1 (), rtsTxMode);
       duration += GetSifs ();
       duration += m_phy->CalculateTxDuration (GetCurrentSize (), dataTxMode, WIFI_PREAMBLE_LONG);
       duration += GetSifs ();
-      duration += m_phy->CalculateTxDuration (GetAckSize (), ackTxMode, WIFI_PREAMBLE_LONG);
+      duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxMode);
     }
-  rts.SetDurationUs (duration.GetMicroSeconds ());
+  rts.SetDuration (duration);
 
   Time txDuration = m_phy->CalculateTxDuration (GetRtsSize (), rtsTxMode, WIFI_PREAMBLE_LONG);
   Time timerDelay = txDuration + GetCtsTimeout ();
@@ -856,12 +864,10 @@
     } 
   else 
     {
-      WifiMode ackTxMode = GetAckTxModeForData (m_currentHdr.GetAddr1 (), 
-                                                dataTxMode);
       if (m_txParams.MustWaitAck ()) 
         {
           duration += GetSifs ();
-          duration += m_phy->CalculateTxDuration (GetAckSize (), ackTxMode, WIFI_PREAMBLE_LONG);
+          duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxMode);
         }
       if (m_txParams.HasNextPacket ()) 
         {
@@ -871,11 +877,11 @@
           if (m_txParams.MustWaitAck ()) 
             {
               duration += GetSifs ();
-              duration += m_phy->CalculateTxDuration (GetAckSize (), ackTxMode, WIFI_PREAMBLE_LONG);
+              duration += GetAckDuration (m_currentHdr.GetAddr1 (), dataTxMode);
             }
         }
     }
-  m_currentHdr.SetDurationUs (duration.GetMicroSeconds ());
+  m_currentHdr.SetDuration (duration);
 
   m_currentPacket.AddHeader (m_currentHdr);
   WifiMacTrailer fcs;
@@ -905,20 +911,22 @@
 }
 
 void
-MacLow::SendCtsAfterRts (Mac48Address source, Time duration, WifiMode txMode, double rtsSnr)
+MacLow::SendCtsAfterRts (Mac48Address source, Time duration, WifiMode rtsTxMode, double rtsSnr)
 {
   /* send a CTS when you receive a RTS 
    * right after SIFS.
    */
-  MY_DEBUG ("tx CTS to=" << source << ", mode=" << txMode);
+  WifiMode ctsTxMode = GetCtsTxModeForRts (source, rtsTxMode);
+  MY_DEBUG ("tx CTS to=" << source << ", mode=" << ctsTxMode);
   WifiMacHeader cts;
   cts.SetType (WIFI_MAC_CTL_CTS);
   cts.SetDsNotFrom ();
   cts.SetDsNotTo ();
   cts.SetAddr1 (source);
-  duration -= m_phy->CalculateTxDuration (GetCtsSize (), txMode, WIFI_PREAMBLE_LONG);
+  duration -= GetCtsDuration (source, rtsTxMode);
   duration -= GetSifs ();
-  cts.SetDurationUs (duration.GetMicroSeconds ());
+  NS_ASSERT (duration >= MicroSeconds (0));
+  cts.SetDuration (duration);
 
   Packet packet;
   packet.AddHeader (cts);
@@ -929,7 +937,7 @@
   tag.Set (rtsSnr);
   packet.AddTag (tag);
 
-  ForwardDown (packet, &cts, txMode);
+  ForwardDown (packet, &cts, ctsTxMode);
 }
 
 void
@@ -948,7 +956,8 @@
   Time txDuration = m_phy->CalculateTxDuration (GetCurrentSize (), dataTxMode, WIFI_PREAMBLE_LONG);
   duration -= txDuration;
   duration -= GetSifs ();
-  m_currentHdr.SetDurationUs (duration.GetMicroSeconds ());
+  NS_ASSERT (duration >= MicroSeconds (0));
+  m_currentHdr.SetDuration (duration);
 
   m_currentPacket.AddHeader (m_currentHdr);
   WifiMacTrailer fcs;
@@ -972,20 +981,22 @@
 }
 
 void
-MacLow::SendAckAfterData (Mac48Address source, Time duration, WifiMode txMode, double dataSnr)
+MacLow::SendAckAfterData (Mac48Address source, Time duration, WifiMode dataTxMode, double dataSnr)
 {
   /* send an ACK when you receive 
    * a packet after SIFS. 
    */
-  MY_DEBUG ("tx ACK to=" << source << ", mode=" << txMode);
+  WifiMode ackTxMode = GetAckTxModeForData (source, dataTxMode);
+  MY_DEBUG ("tx ACK to=" << source << ", mode=" << ackTxMode);
   WifiMacHeader ack;
   ack.SetType (WIFI_MAC_CTL_ACK);
   ack.SetDsNotFrom ();
   ack.SetDsNotTo ();
   ack.SetAddr1 (source);
-  duration -= m_phy->CalculateTxDuration (GetAckSize (), txMode, WIFI_PREAMBLE_LONG);
+  duration -= GetAckDuration (source, dataTxMode);
   duration -= GetSifs ();
-  ack.SetDurationUs (duration.GetMicroSeconds ());
+  NS_ASSERT (duration >= MicroSeconds (0));
+  ack.SetDuration (duration);
 
   Packet packet;
   packet.AddHeader (ack);
@@ -996,7 +1007,7 @@
   tag.Set (dataSnr);
   packet.AddTag (tag);
 
-  ForwardDown (packet, &ack, txMode);
+  ForwardDown (packet, &ack, ackTxMode);
 }
 
 } // namespace ns3
--- a/src/devices/wifi/mac-low.h	Wed Oct 24 15:05:17 2007 +0200
+++ b/src/devices/wifi/mac-low.h	Wed Oct 24 16:02:10 2007 +0200
@@ -194,6 +194,8 @@
   WifiMode GetDataTxMode (Mac48Address to, uint32_t size) const;
   WifiMode GetCtsTxModeForRts (Mac48Address to, WifiMode rtsTxMode) const;
   WifiMode GetAckTxModeForData (Mac48Address to, WifiMode dataTxMode) const;
+  Time GetCtsDuration (Mac48Address to, WifiMode rtsTxMode) const;
+  Time GetAckDuration (Mac48Address to, WifiMode dataTxMode) const;
   void NotifyNav (Time at, WifiMacHeader const*hdr);
   bool IsNavZero (Time at);
   void MaybeCancelPrevious (void);
--- a/src/devices/wifi/wifi-mac-header.cc	Wed Oct 24 15:05:17 2007 +0200
+++ b/src/devices/wifi/wifi-mac-header.cc	Wed Oct 24 16:02:10 2007 +0200
@@ -254,14 +254,15 @@
   m_ctrlFromDs = 0;
 }
 void 
-WifiMacHeader::SetDuration (uint16_t duration)
+WifiMacHeader::SetRawDuration (uint16_t duration)
 {
   m_duration = duration;
 }
 void 
-WifiMacHeader::SetDurationUs (uint64_t duration_us)
+WifiMacHeader::SetDuration (Time duration)
 {
-  NS_ASSERT (duration_us <= (1<<16) - 1);
+  int64_t duration_us = duration.GetMicroSeconds ();
+  NS_ASSERT (duration_us >= 0 && duration_us <= 0x7fff);
   m_duration = static_cast<uint16_t> (duration_us);
 }
 
@@ -554,14 +555,14 @@
 
 
 uint16_t 
-WifiMacHeader::GetDuration (void) const
+WifiMacHeader::GetRawDuration (void) const
 {
   return m_duration;
 }
-uint64_t 
-WifiMacHeader::GetDurationUs (void) const
+Time
+WifiMacHeader::GetDuration (void) const
 {
-  return m_duration;
+  return MicroSeconds (m_duration);
 }
 uint16_t 
 WifiMacHeader::GetSequenceControl (void) const
@@ -674,15 +675,6 @@
   m_qosStuff = (qos >> 8) & 0x00ff;
 }
 
-
-void 
-WifiMacHeader::SetDurationS (double duration)
-{
-  uint16_t us = (uint16_t)(duration * 1000000);
-  us &= 0x7fff;
-  SetDuration (us);
-}
-
 uint32_t 
 WifiMacHeader::GetSize (void) const
 {
--- a/src/devices/wifi/wifi-mac-header.h	Wed Oct 24 15:05:17 2007 +0200
+++ b/src/devices/wifi/wifi-mac-header.h	Wed Oct 24 16:02:10 2007 +0200
@@ -22,6 +22,7 @@
 
 #include "ns3/header.h"
 #include "ns3/mac48-address.h"
+#include "ns3/nstime.h"
 #include <stdint.h>
 
 namespace ns3 {
@@ -88,8 +89,8 @@
   void SetAddr3 (Mac48Address address);
   void SetAddr4 (Mac48Address address);
   void SetType (enum WifiMacType_e type);
-  void SetDuration (uint16_t duration);
-  void SetDurationUs (uint64_t duration);
+  void SetRawDuration (uint16_t duration);
+  void SetDuration (Time duration);
   void SetId (uint16_t id);
   void SetSequenceNumber (uint16_t seq);
   void SetFragmentNumber (uint8_t frag);
@@ -125,8 +126,8 @@
   bool IsDisassociation (void) const;
   bool IsAuthentication (void) const;
   bool IsDeauthentication (void) const;
-  uint16_t GetDuration (void) const;
-  uint64_t GetDurationUs (void) const;
+  uint16_t GetRawDuration (void) const;
+  Time GetDuration (void) const;
   uint16_t GetSequenceControl (void) const;
   uint16_t GetSequenceNumber (void) const;
   uint16_t GetFragmentNumber (void) const;
@@ -138,7 +139,6 @@
   uint8_t GetQosTid (void) const;
   uint8_t GetQosTxopLimit (void) const;
 
-  void SetDurationS (double duration);
   uint32_t GetSize (void) const;
   char const *GetTypeString (void) const;