A Discrete-Event Network Simulator
API
adhoc-wifi-mac.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, 2009 INRIA
3  * Copyright (c) 2009 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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  * Mirko Banchi <mk.banchi@gmail.com>
20  */
21 
22 #include "adhoc-wifi-mac.h"
23 
24 #include "qos-txop.h"
25 
26 #include "ns3/eht-capabilities.h"
27 #include "ns3/he-capabilities.h"
28 #include "ns3/ht-capabilities.h"
29 #include "ns3/log.h"
30 #include "ns3/packet.h"
31 #include "ns3/vht-capabilities.h"
32 
33 namespace ns3
34 {
35 
36 NS_LOG_COMPONENT_DEFINE("AdhocWifiMac");
37 
38 NS_OBJECT_ENSURE_REGISTERED(AdhocWifiMac);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId("ns3::AdhocWifiMac")
44  .SetParent<WifiMac>()
45  .SetGroupName("Wifi")
46  .AddConstructor<AdhocWifiMac>();
47  return tid;
48 }
49 
51 {
52  NS_LOG_FUNCTION(this);
53  // Let the lower layers know that we are acting in an IBSS
55 }
56 
58 {
59  NS_LOG_FUNCTION(this);
60 }
61 
62 bool
64 {
65  return true;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION(this << packet << to);
72  if (GetWifiRemoteStationManager()->IsBrandNew(to))
73  {
74  // In ad hoc mode, we assume that every destination supports all the rates we support.
75  if (GetHtSupported())
76  {
77  GetWifiRemoteStationManager()->AddAllSupportedMcs(to);
78  GetWifiRemoteStationManager()->AddStationHtCapabilities(
79  to,
81  }
83  {
84  GetWifiRemoteStationManager()->AddStationVhtCapabilities(
85  to,
87  }
88  if (GetHeSupported())
89  {
90  GetWifiRemoteStationManager()->AddStationHeCapabilities(
91  to,
93  }
94  if (GetEhtSupported())
95  {
96  GetWifiRemoteStationManager()->AddStationEhtCapabilities(
97  to,
99  }
100  GetWifiRemoteStationManager()->AddAllSupportedModes(to);
101  GetWifiRemoteStationManager()->RecordDisassociated(to);
102  }
103 
104  WifiMacHeader hdr;
105 
106  // If we are not a QoS STA then we definitely want to use AC_BE to
107  // transmit the packet. A TID of zero will map to AC_BE (through \c
108  // QosUtilsMapTidToAc()), so we use that as our default here.
109  uint8_t tid = 0;
110 
111  // For now, a STA that supports QoS does not support non-QoS
112  // associations, and vice versa. In future the STA model should fall
113  // back to non-QoS if talking to a peer that is also non-QoS. At
114  // that point there will need to be per-station QoS state maintained
115  // by the association state machine, and consulted here.
116  if (GetQosSupported())
117  {
120  hdr.SetQosNoEosp();
121  hdr.SetQosNoAmsdu();
122  // Transmission of multiple frames in the same TXOP is not
123  // supported for now
124  hdr.SetQosTxopLimit(0);
125 
126  // Fill in the QoS control field in the MAC header
127  tid = QosUtilsGetTidForPacket(packet);
128  // Any value greater than 7 is invalid and likely indicates that
129  // the packet had no QoS tag, so we revert to zero, which will
130  // mean that AC_BE is used.
131  if (tid > 7)
132  {
133  tid = 0;
134  }
135  hdr.SetQosTid(tid);
136  }
137  else
138  {
139  hdr.SetType(WIFI_MAC_DATA);
140  }
141 
142  if (GetHtSupported())
143  {
144  hdr.SetNoOrder(); // explicitly set to 0 for the time being since HT control field is not
145  // yet implemented (set it to 1 when implemented)
146  }
147  hdr.SetAddr1(to);
148  hdr.SetAddr2(GetAddress());
149  hdr.SetAddr3(GetBssid(0));
150  hdr.SetDsNotFrom();
151  hdr.SetDsNotTo();
152 
153  if (GetQosSupported())
154  {
155  // Sanity check that the TID is valid
156  NS_ASSERT(tid < 8);
157  GetQosTxop(tid)->Queue(packet, hdr);
158  }
159  else
160  {
161  GetTxop()->Queue(packet, hdr);
162  }
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION(this << &linkUp);
170 
171  // The approach taken here is that, from the point of view of a STA
172  // in IBSS mode, the link is always up, so we immediately invoke the
173  // callback if one is set
174  linkUp();
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION(this << *mpdu << +linkId);
181  const WifiMacHeader* hdr = &mpdu->GetHeader();
182  NS_ASSERT(!hdr->IsCtl());
183  Mac48Address from = hdr->GetAddr2();
184  Mac48Address to = hdr->GetAddr1();
185  if (GetWifiRemoteStationManager()->IsBrandNew(from))
186  {
187  // In ad hoc mode, we assume that every destination supports all the rates we support.
188  if (GetHtSupported())
189  {
190  GetWifiRemoteStationManager()->AddAllSupportedMcs(from);
191  GetWifiRemoteStationManager()->AddStationHtCapabilities(
192  from,
194  }
196  {
197  GetWifiRemoteStationManager()->AddStationVhtCapabilities(
198  from,
200  }
201  if (GetHeSupported())
202  {
203  GetWifiRemoteStationManager()->AddStationHeCapabilities(
204  from,
206  }
207  if (GetEhtSupported())
208  {
209  GetWifiRemoteStationManager()->AddStationEhtCapabilities(
210  from,
212  }
213  GetWifiRemoteStationManager()->AddAllSupportedModes(from);
214  GetWifiRemoteStationManager()->RecordDisassociated(from);
215  }
216  if (hdr->IsData())
217  {
218  if (hdr->IsQosData() && hdr->IsQosAmsdu())
219  {
220  NS_LOG_DEBUG("Received A-MSDU from" << from);
222  }
223  else
224  {
225  ForwardUp(mpdu->GetPacket()->Copy(), from, to);
226  }
227  return;
228  }
229 
230  // Invoke the receive handler of our parent class to deal with any
231  // other frames. Specifically, this will handle Block Ack-related
232  // Management Action frames.
233  WifiMac::Receive(mpdu, linkId);
234 }
235 
236 } // namespace ns3
Wifi MAC high model for an ad-hoc Wifi MAC.
~AdhocWifiMac() override
void Receive(Ptr< const WifiMpdu > mpdu, uint8_t linkId) override
This method acts as the MacRxMiddle receive callback and is invoked to notify us that a frame has bee...
static TypeId GetTypeId()
Get the type ID.
void SetLinkUpCallback(Callback< void > linkUp) override
bool CanForwardPacketsTo(Mac48Address to) const override
Return true if packets can be forwarded to the given destination, false otherwise.
void Enqueue(Ptr< Packet > packet, Mac48Address to) override
an EUI-48 address
Definition: mac48-address.h:46
virtual void Queue(Ptr< Packet > packet, const WifiMacHeader &hdr)
Definition: txop.cc:523
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Implements the IEEE 802.11 MAC header.
void SetQosAckPolicy(QosAckPolicy policy)
Set the QoS Ack policy in the QoS control field.
bool IsQosAmsdu() const
Check if the A-MSDU present bit is set in the QoS control field.
Mac48Address GetAddr1() const
Return the address in the Address 1 field.
void SetQosTxopLimit(uint8_t txop)
Set TXOP limit in the QoS control field.
bool IsCtl() const
Return true if the Type is Control.
void SetNoOrder()
Unset order bit in the frame control field.
void SetDsNotFrom()
Un-set the From DS bit in the Frame Control field.
void SetAddr1(Mac48Address address)
Fill the Address 1 field with the given address.
void SetQosNoAmsdu()
Set that A-MSDU is not present.
virtual void SetType(WifiMacType type, bool resetToDsFromDs=true)
Set Type/Subtype values with the correct values depending on the given type.
Mac48Address GetAddr2() const
Return the address in the Address 2 field.
void SetQosTid(uint8_t tid)
Set the TID for the QoS header.
bool IsData() const
Return true if the Type is DATA.
void SetQosNoEosp()
Un-set the end of service period (EOSP) bit in the QoS control field.
void SetAddr2(Mac48Address address)
Fill the Address 2 field with the given address.
bool IsQosData() const
Return true if the Type is DATA and Subtype is one of the possible values for QoS Data.
void SetAddr3(Mac48Address address)
Fill the Address 3 field with the given address.
void SetDsNotTo()
Un-set the To DS bit in the Frame Control field.
base class for all MAC-level wifi objects.
Definition: wifi-mac.h:96
Mac48Address GetBssid(uint8_t linkId) const
Definition: wifi-mac.cc:478
Ptr< Txop > GetTxop() const
Accessor for the Txop object.
Definition: wifi-mac.cc:493
VhtCapabilities GetVhtCapabilities(uint8_t linkId) const
Return the VHT capabilities of the device for the given link.
Definition: wifi-mac.cc:2018
bool GetQosSupported() const
Return whether the device supports QoS.
Definition: wifi-mac.cc:1222
bool GetHtSupported() const
Return whether the device supports HT.
Definition: wifi-mac.cc:1761
void SetTypeOfStation(TypeOfStation type)
This method is invoked by a subclass to specify what type of station it is implementing.
Definition: wifi-mac.cc:415
bool GetEhtSupported() const
Return whether the device supports EHT.
Definition: wifi-mac.cc:1780
bool GetHeSupported() const
Return whether the device supports HE.
Definition: wifi-mac.cc:1774
HtCapabilities GetHtCapabilities(uint8_t linkId) const
Return the HT capabilities of the device for the given link.
Definition: wifi-mac.cc:1959
bool GetVhtSupported(uint8_t linkId) const
Return whether the device supports VHT on the given link.
Definition: wifi-mac.cc:1767
virtual void DeaggregateAmsduAndForward(Ptr< const WifiMpdu > mpdu)
This method can be called to de-aggregate an A-MSDU and forward the constituent packets up the stack.
Definition: wifi-mac.cc:1620
virtual void SetLinkUpCallback(Callback< void > linkUp)
Definition: wifi-mac.cc:1291
Ptr< WifiRemoteStationManager > GetWifiRemoteStationManager(uint8_t linkId=0) const
Definition: wifi-mac.cc:906
void ForwardUp(Ptr< const Packet > packet, Mac48Address from, Mac48Address to)
Forward the packet up to the device.
Definition: wifi-mac.cc:1490
virtual void Receive(Ptr< const WifiMpdu > mpdu, uint8_t linkId)
This method acts as the MacRxMiddle receive callback and is invoked to notify us that a frame has bee...
Definition: wifi-mac.cc:1497
Mac48Address GetAddress() const
Definition: wifi-mac.cc:452
EhtCapabilities GetEhtCapabilities(uint8_t linkId) const
Return the EHT capabilities of the device for the given link.
Definition: wifi-mac.cc:2158
HeCapabilities GetHeCapabilities(uint8_t linkId) const
Return the HE capabilities of the device for the given link.
Definition: wifi-mac.cc:2100
Ptr< QosTxop > GetQosTxop(AcIndex ac) const
Accessor for a specified EDCA object.
Definition: wifi-mac.cc:499
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
uint8_t QosUtilsGetTidForPacket(Ptr< const Packet > packet)
If a QoS tag is attached to the packet, returns a value < 8.
Definition: qos-utils.cc:156
@ ADHOC_STA
Definition: wifi-mac.h:67
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static constexpr uint8_t SINGLE_LINK_OP_ID
Link ID for single link operations (helps tracking places where correct link ID is to be used to supp...
Definition: wifi-utils.h:192
@ WIFI_MAC_DATA
@ WIFI_MAC_QOSDATA