src/devices/wifi/mac-low.cc
changeset 2985 fa0747c4ad5e
parent 2793 10a571d9a9f7
child 3038 5962e2962fa9
equal deleted inserted replaced
2984:27b183b18d4d 2985:fa0747c4ad5e
    31 #include "wifi-mac.h"
    31 #include "wifi-mac.h"
    32 
    32 
    33 NS_LOG_COMPONENT_DEFINE ("MacLow");
    33 NS_LOG_COMPONENT_DEFINE ("MacLow");
    34 
    34 
    35 #define MY_DEBUG(x) \
    35 #define MY_DEBUG(x) \
    36   NS_LOG_DEBUG (Simulator::Now () << " " << m_mac->GetAddress () << " " << x)
    36   NS_LOG_DEBUG (m_mac->GetAddress () << " " << x)
    37 
    37 
    38 namespace ns3 {
    38 namespace ns3 {
    39 
    39 
    40 class SnrTag : public Tag
    40 class SnrTag : public Tag
    41 {
    41 {
   213 {
   213 {
   214   NS_ASSERT (HasNextPacket ());
   214   NS_ASSERT (HasNextPacket ());
   215   return m_nextSize;
   215   return m_nextSize;
   216 }
   216 }
   217 
   217 
   218 
   218 std::ostream &operator << (std::ostream &os, const MacLowTransmissionParameters &params)
       
   219 {
       
   220   os << "[" 
       
   221      << "send rts=" << params.m_sendRts << ", "
       
   222      << "next size=" << params.m_nextSize << ", "
       
   223      << "dur=" << params.m_overrideDurationId << ", "
       
   224      << "ack=";
       
   225   switch (params.m_waitAck) {
       
   226   case MacLowTransmissionParameters::ACK_NONE:
       
   227     os << "none";
       
   228     break;
       
   229   case MacLowTransmissionParameters::ACK_NORMAL:
       
   230     os << "normal";
       
   231     break;
       
   232   case MacLowTransmissionParameters::ACK_FAST:
       
   233     os << "fast";
       
   234     break;
       
   235   case MacLowTransmissionParameters::ACK_SUPER_FAST:
       
   236     os << "super-fast";
       
   237     break;
       
   238   }
       
   239   os << "]";
       
   240   return os;
       
   241 }
   219 
   242 
   220 MacLow::MacLow ()
   243 MacLow::MacLow ()
   221   : m_normalAckTimeoutEvent (),
   244   : m_normalAckTimeoutEvent (),
   222     m_fastAckTimeoutEvent (),
   245     m_fastAckTimeoutEvent (),
   223     m_superFastAckTimeoutEvent (),
   246     m_superFastAckTimeoutEvent (),
   228     m_sendDataEvent (),
   251     m_sendDataEvent (),
   229     m_waitSifsEvent (),
   252     m_waitSifsEvent (),
   230     m_currentPacket (0),
   253     m_currentPacket (0),
   231     m_listener (0)
   254     m_listener (0)
   232 {
   255 {
       
   256   NS_LOG_FUNCTION (this);
   233   m_lastNavDuration = Seconds (0);
   257   m_lastNavDuration = Seconds (0);
   234   m_lastNavStart = Seconds (0);
   258   m_lastNavStart = Seconds (0);
   235 }
   259 }
   236 
   260 
   237 MacLow::~MacLow ()
   261 MacLow::~MacLow ()
   238 {}
   262 {
       
   263   NS_LOG_FUNCTION (this);
       
   264 }
   239 
   265 
   240 void 
   266 void 
   241 MacLow::DoDispose (void)
   267 MacLow::DoDispose (void)
   242 {
   268 {
       
   269   NS_LOG_FUNCTION (this);
   243   CancelAllEvents ();
   270   CancelAllEvents ();
   244   m_phy = 0;
   271   m_phy = 0;
   245   m_mac = 0;
   272   m_mac = 0;
   246   m_stationManager = 0;
   273   m_stationManager = 0;
   247 }
   274 }
   248 
   275 
   249 void
   276 void
   250 MacLow::CancelAllEvents (void)
   277 MacLow::CancelAllEvents (void)
   251 {
   278 {
       
   279   NS_LOG_FUNCTION (this);
   252   bool oneRunning = false;
   280   bool oneRunning = false;
   253   if (m_normalAckTimeoutEvent.IsRunning ()) 
   281   if (m_normalAckTimeoutEvent.IsRunning ()) 
   254     {
   282     {
   255       m_normalAckTimeoutEvent.Cancel ();
   283       m_normalAckTimeoutEvent.Cancel ();
   256       oneRunning = true;
   284       oneRunning = true;
   337 
   365 
   338 
   366 
   339 void 
   367 void 
   340 MacLow::StartTransmission (Ptr<const Packet> packet, 
   368 MacLow::StartTransmission (Ptr<const Packet> packet, 
   341                            WifiMacHeader const*hdr, 
   369                            WifiMacHeader const*hdr, 
   342                            MacLowTransmissionParameters parameters,
   370                            MacLowTransmissionParameters params,
   343                            MacLowTransmissionListener *listener)
   371                            MacLowTransmissionListener *listener)
   344 {
   372 {
       
   373   NS_LOG_FUNCTION (this << packet << hdr << params << listener);
   345   /* m_currentPacket is not NULL because someone started
   374   /* m_currentPacket is not NULL because someone started
   346    * a transmission and was interrupted before one of:
   375    * a transmission and was interrupted before one of:
   347    *   - ctsTimeout
   376    *   - ctsTimeout
   348    *   - sendDataAfterCTS
   377    *   - sendDataAfterCTS
   349    * expired. This means that one of these timers is still
   378    * expired. This means that one of these timers is still
   358    */
   387    */
   359   m_currentPacket = packet->Copy ();
   388   m_currentPacket = packet->Copy ();
   360   m_currentHdr = *hdr;
   389   m_currentHdr = *hdr;
   361   CancelAllEvents ();
   390   CancelAllEvents ();
   362   m_listener = listener;
   391   m_listener = listener;
   363   m_txParams = parameters;
   392   m_txParams = params;
   364 
   393 
   365   //NS_ASSERT (m_phy->IsStateIdle ());
   394   //NS_ASSERT (m_phy->IsStateIdle ());
   366 
   395 
   367   MY_DEBUG ("startTx size="<< GetSize (m_currentPacket, &m_currentHdr) << 
   396   MY_DEBUG ("startTx size="<< GetSize (m_currentPacket, &m_currentHdr) << 
   368             ", to=" << m_currentHdr.GetAddr1()<<", listener="<<m_listener);
   397             ", to=" << m_currentHdr.GetAddr1()<<", listener="<<m_listener);
   381 }
   410 }
   382 
   411 
   383 void
   412 void
   384 MacLow::ReceiveError (Ptr<Packet> packet, double rxSnr)
   413 MacLow::ReceiveError (Ptr<Packet> packet, double rxSnr)
   385 {
   414 {
       
   415   NS_LOG_FUNCTION (this << packet << rxSnr);
   386   MY_DEBUG ("rx failed ");
   416   MY_DEBUG ("rx failed ");
   387   if (m_txParams.MustWaitFastAck ()) 
   417   if (m_txParams.MustWaitFastAck ()) 
   388     {
   418     {
   389       NS_ASSERT (m_fastAckFailedTimeoutEvent.IsExpired ());
   419       NS_ASSERT (m_fastAckFailedTimeoutEvent.IsExpired ());
   390       m_fastAckFailedTimeoutEvent = Simulator::Schedule (GetSifs (), 
   420       m_fastAckFailedTimeoutEvent = Simulator::Schedule (GetSifs (), 
   394 }
   424 }
   395 
   425 
   396 void 
   426 void 
   397 MacLow::ReceiveOk (Ptr<Packet> packet, double rxSnr, WifiMode txMode, WifiPreamble preamble)
   427 MacLow::ReceiveOk (Ptr<Packet> packet, double rxSnr, WifiMode txMode, WifiPreamble preamble)
   398 {
   428 {
       
   429   NS_LOG_FUNCTION (this << packet << rxSnr << txMode << preamble);
   399   /* A packet is received from the PHY.
   430   /* A packet is received from the PHY.
   400    * When we have handled this packet,
   431    * When we have handled this packet,
   401    * we handle any packet present in the
   432    * we handle any packet present in the
   402    * packet queue.
   433    * packet queue.
   403    */
   434    */
   752 
   783 
   753 void
   784 void
   754 MacLow::ForwardDown (Ptr<const Packet> packet, WifiMacHeader const* hdr, 
   785 MacLow::ForwardDown (Ptr<const Packet> packet, WifiMacHeader const* hdr, 
   755                      WifiMode txMode)
   786                      WifiMode txMode)
   756 {
   787 {
       
   788   NS_LOG_FUNCTION (this << packet << hdr << txMode);
   757   MY_DEBUG ("send " << hdr->GetTypeString () <<
   789   MY_DEBUG ("send " << hdr->GetTypeString () <<
   758             ", to=" << hdr->GetAddr1 () <<
   790             ", to=" << hdr->GetAddr1 () <<
   759             ", size=" << packet->GetSize () <<
   791             ", size=" << packet->GetSize () <<
   760             ", mode=" << txMode <<
   792             ", mode=" << txMode <<
   761             ", duration=" << hdr->GetDuration () <<
   793             ", duration=" << hdr->GetDuration () <<
   771 }
   803 }
   772 
   804 
   773 void
   805 void
   774 MacLow::CtsTimeout (void)
   806 MacLow::CtsTimeout (void)
   775 {
   807 {
       
   808   NS_LOG_FUNCTION (this);
   776   MY_DEBUG ("cts timeout");
   809   MY_DEBUG ("cts timeout");
   777   // XXX: should check that there was no rx start before now.
   810   // XXX: should check that there was no rx start before now.
   778   // we should restart a new cts timeout now until the expected
   811   // we should restart a new cts timeout now until the expected
   779   // end of rx if there was a rx start before now.
   812   // end of rx if there was a rx start before now.
   780   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   813   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   785   listener->MissedCts ();
   818   listener->MissedCts ();
   786 }
   819 }
   787 void
   820 void
   788 MacLow::NormalAckTimeout (void)
   821 MacLow::NormalAckTimeout (void)
   789 {
   822 {
       
   823   NS_LOG_FUNCTION (this);
   790   MY_DEBUG ("normal ack timeout");
   824   MY_DEBUG ("normal ack timeout");
   791   // XXX: should check that there was no rx start before now.
   825   // XXX: should check that there was no rx start before now.
   792   // we should restart a new ack timeout now until the expected
   826   // we should restart a new ack timeout now until the expected
   793   // end of rx if there was a rx start before now.
   827   // end of rx if there was a rx start before now.
   794   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   828   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   798   listener->MissedAck ();
   832   listener->MissedAck ();
   799 }
   833 }
   800 void
   834 void
   801 MacLow::FastAckTimeout (void)
   835 MacLow::FastAckTimeout (void)
   802 {
   836 {
       
   837   NS_LOG_FUNCTION (this);
   803   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   838   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   804   station->ReportDataFailed ();
   839   station->ReportDataFailed ();
   805   MacLowTransmissionListener *listener = m_listener;
   840   MacLowTransmissionListener *listener = m_listener;
   806   m_listener = 0;
   841   m_listener = 0;
   807   if (m_phy->IsStateIdle ()) 
   842   if (m_phy->IsStateIdle ()) 
   815     }
   850     }
   816 }
   851 }
   817 void
   852 void
   818 MacLow::SuperFastAckTimeout ()
   853 MacLow::SuperFastAckTimeout ()
   819 {
   854 {
       
   855   NS_LOG_FUNCTION (this);
   820   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   856   WifiRemoteStation *station = GetStation (m_currentHdr.GetAddr1 ());
   821   station->ReportDataFailed ();
   857   station->ReportDataFailed ();
   822   MacLowTransmissionListener *listener = m_listener;
   858   MacLowTransmissionListener *listener = m_listener;
   823   m_listener = 0;
   859   m_listener = 0;
   824   if (m_phy->IsStateIdle ()) 
   860   if (m_phy->IsStateIdle ()) 
   834 }
   870 }
   835 
   871 
   836 void
   872 void
   837 MacLow::SendRtsForPacket (void)
   873 MacLow::SendRtsForPacket (void)
   838 {
   874 {
       
   875   NS_LOG_FUNCTION (this);
   839   /* send an RTS for this packet. */
   876   /* send an RTS for this packet. */
   840   WifiMacHeader rts;
   877   WifiMacHeader rts;
   841   rts.SetType (WIFI_MAC_CTL_RTS);
   878   rts.SetType (WIFI_MAC_CTL_RTS);
   842   rts.SetDsNotFrom ();
   879   rts.SetDsNotFrom ();
   843   rts.SetDsNotTo ();
   880   rts.SetDsNotTo ();
   915 }
   952 }
   916 
   953 
   917 void
   954 void
   918 MacLow::SendDataPacket (void)
   955 MacLow::SendDataPacket (void)
   919 {
   956 {
       
   957   NS_LOG_FUNCTION (this);
   920   /* send this packet directly. No RTS is needed. */
   958   /* send this packet directly. No RTS is needed. */
   921   StartDataTxTimers ();
   959   StartDataTxTimers ();
   922 
   960 
   923   WifiMode dataTxMode = GetDataTxMode (m_currentPacket, &m_currentHdr);
   961   WifiMode dataTxMode = GetDataTxMode (m_currentPacket, &m_currentHdr);
   924   Time duration = Seconds (0.0);
   962   Time duration = Seconds (0.0);
   975 }
  1013 }
   976 
  1014 
   977 void
  1015 void
   978 MacLow::SendCtsAfterRts (Mac48Address source, Time duration, WifiMode rtsTxMode, double rtsSnr)
  1016 MacLow::SendCtsAfterRts (Mac48Address source, Time duration, WifiMode rtsTxMode, double rtsSnr)
   979 {
  1017 {
       
  1018   NS_LOG_FUNCTION (this);
   980   /* send a CTS when you receive a RTS 
  1019   /* send a CTS when you receive a RTS 
   981    * right after SIFS.
  1020    * right after SIFS.
   982    */
  1021    */
   983   WifiMode ctsTxMode = GetCtsTxModeForRts (source, rtsTxMode);
  1022   WifiMode ctsTxMode = GetCtsTxModeForRts (source, rtsTxMode);
   984   WifiMacHeader cts;
  1023   WifiMacHeader cts;
  1005 }
  1044 }
  1006 
  1045 
  1007 void
  1046 void
  1008 MacLow::SendDataAfterCts (Mac48Address source, Time duration, WifiMode txMode)
  1047 MacLow::SendDataAfterCts (Mac48Address source, Time duration, WifiMode txMode)
  1009 {
  1048 {
       
  1049   NS_LOG_FUNCTION (this);
  1010   /* send the third step in a 
  1050   /* send the third step in a 
  1011    * RTS/CTS/DATA/ACK hanshake 
  1051    * RTS/CTS/DATA/ACK hanshake 
  1012    */
  1052    */
  1013   NS_ASSERT (m_currentPacket != 0);
  1053   NS_ASSERT (m_currentPacket != 0);
  1014   StartDataTxTimers ();
  1054   StartDataTxTimers ();
  1041 }
  1081 }
  1042 
  1082 
  1043 void
  1083 void
  1044 MacLow::FastAckFailedTimeout (void)
  1084 MacLow::FastAckFailedTimeout (void)
  1045 {
  1085 {
       
  1086   NS_LOG_FUNCTION (this);
  1046   MacLowTransmissionListener *listener = m_listener;
  1087   MacLowTransmissionListener *listener = m_listener;
  1047   m_listener = 0;
  1088   m_listener = 0;
  1048   listener->MissedAck ();
  1089   listener->MissedAck ();
  1049   MY_DEBUG ("fast Ack busy but missed");
  1090   MY_DEBUG ("fast Ack busy but missed");
  1050 }
  1091 }
  1051 
  1092 
  1052 void
  1093 void
  1053 MacLow::SendAckAfterData (Mac48Address source, Time duration, WifiMode dataTxMode, double dataSnr)
  1094 MacLow::SendAckAfterData (Mac48Address source, Time duration, WifiMode dataTxMode, double dataSnr)
  1054 {
  1095 {
       
  1096   NS_LOG_FUNCTION (this);
  1055   /* send an ACK when you receive 
  1097   /* send an ACK when you receive 
  1056    * a packet after SIFS. 
  1098    * a packet after SIFS. 
  1057    */
  1099    */
  1058   WifiMode ackTxMode = GetAckTxModeForData (source, dataTxMode);
  1100   WifiMode ackTxMode = GetAckTxModeForData (source, dataTxMode);
  1059   WifiMacHeader ack;
  1101   WifiMacHeader ack;