|
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2005,2006 INRIA |
|
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 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
|
19 */ |
|
20 #include "mac-high-nqap.h" |
|
21 #include "mac-stations.h" |
|
22 #include "dca-txop.h" |
|
23 #include "wifi-net-device.h" |
|
24 #include "wifi-mac-header.h" |
|
25 #include "mgt-headers.h" |
|
26 #include "ns3/assert.h" |
|
27 |
|
28 #define noNQAP_DEBUG 1 |
|
29 |
|
30 #ifdef NQAP_DEBUG |
|
31 #include <iostream> |
|
32 #include "ns3/simulator.h" |
|
33 # define TRACE(x) \ |
|
34 std::cout << "NQAP now=" << Simulator::NowUs () << "us " << x << std::endl; |
|
35 #else |
|
36 # define TRACE(x) |
|
37 #endif |
|
38 |
|
39 |
|
40 namespace ns3 { |
|
41 |
|
42 MacHighNqap::MacHighNqap () |
|
43 : m_beaconIntervalUs (500000) |
|
44 {} |
|
45 MacHighNqap::~MacHighNqap () |
|
46 {} |
|
47 |
|
48 void |
|
49 MacHighNqap::SetDcaTxop (DcaTxop *dca) |
|
50 { |
|
51 m_dca = dca; |
|
52 m_dca->SetTxOkCallback (MakeCallback (&MacHighNqap::TxOk, this)); |
|
53 m_dca->SetTxFailedCallback (MakeCallback (&MacHighNqap::TxFailed, this)); |
|
54 } |
|
55 void |
|
56 MacHighNqap::SetInterface (WifiNetDevice *interface) |
|
57 { |
|
58 m_interface = interface; |
|
59 } |
|
60 void |
|
61 MacHighNqap::SetStations (MacStations *stations) |
|
62 { |
|
63 m_stations = stations; |
|
64 } |
|
65 void |
|
66 MacHighNqap::SetForwardCallback (ForwardCallback callback) |
|
67 { |
|
68 m_forwardUp = callback; |
|
69 } |
|
70 void |
|
71 MacHighNqap::SetSupportedRates (SupportedRates rates) |
|
72 { |
|
73 m_rates = rates; |
|
74 } |
|
75 void |
|
76 MacHighNqap::SetBeaconIntervalUs (uint64_t us) |
|
77 { |
|
78 m_beaconIntervalUs = us; |
|
79 } |
|
80 void |
|
81 MacHighNqap::ForwardDown (Packet packet, Mac48Address from, Mac48Address to) |
|
82 { |
|
83 WifiMacHeader hdr; |
|
84 hdr.SetTypeData (); |
|
85 hdr.SetAddr1 (to); |
|
86 hdr.SetAddr2 (m_interface->GetSelfAddress ()); |
|
87 hdr.SetAddr3 (from); |
|
88 hdr.SetDsFrom (); |
|
89 hdr.SetDsNotTo (); |
|
90 m_dca->Queue (packet, hdr); |
|
91 } |
|
92 void |
|
93 MacHighNqap::Queue (Packet packet, Mac48Address to) |
|
94 { |
|
95 ForwardDown (packet, m_interface->GetSelfAddress (), to); |
|
96 } |
|
97 SupportedRates |
|
98 MacHighNqap::GetSupportedRates (void) |
|
99 { |
|
100 return m_rates; |
|
101 } |
|
102 void |
|
103 MacHighNqap::SendProbeResp (Mac48Address to) |
|
104 { |
|
105 TRACE ("send probe response to="<<to); |
|
106 WifiMacHeader hdr; |
|
107 hdr.SetProbeResp (); |
|
108 hdr.SetAddr1 (to); |
|
109 hdr.SetAddr2 (m_interface->GetSelfAddress ()); |
|
110 hdr.SetAddr3 (m_interface->GetSelfAddress ()); |
|
111 hdr.SetDsNotFrom (); |
|
112 hdr.SetDsNotTo (); |
|
113 Packet packet; |
|
114 MgtProbeResponseHeader probe; |
|
115 probe.SetSsid (m_interface->GetSsid ()); |
|
116 SupportedRates rates = GetSupportedRates (); |
|
117 probe.SetSupportedRates (rates); |
|
118 probe.SetBeaconIntervalUs (m_beaconIntervalUs); |
|
119 packet.AddHeader (probe); |
|
120 |
|
121 m_dca->Queue (packet, hdr); |
|
122 } |
|
123 void |
|
124 MacHighNqap::SendAssocResp (Mac48Address to) |
|
125 { |
|
126 TRACE ("send assoc response to="<<to); |
|
127 WifiMacHeader hdr; |
|
128 hdr.SetAssocResp (); |
|
129 hdr.SetAddr1 (to); |
|
130 hdr.SetAddr2 (m_interface->GetSelfAddress ()); |
|
131 hdr.SetAddr3 (m_interface->GetSelfAddress ()); |
|
132 hdr.SetDsNotFrom (); |
|
133 hdr.SetDsNotTo (); |
|
134 Packet packet; |
|
135 MgtAssocResponseHeader assoc; |
|
136 StatusCode code; |
|
137 code.SetSuccess (); |
|
138 assoc.SetStatusCode (code); |
|
139 packet.AddHeader (assoc); |
|
140 |
|
141 m_dca->Queue (packet, hdr); |
|
142 } |
|
143 void |
|
144 MacHighNqap::TxOk (WifiMacHeader const &hdr) |
|
145 { |
|
146 MacStation *station = m_stations->Lookup (hdr.GetAddr1 ()); |
|
147 if (hdr.IsAssocResp () && |
|
148 station->IsWaitAssocTxOk ()) |
|
149 { |
|
150 TRACE ("associated with sta="<<hdr.GetAddr1 ()); |
|
151 station->RecordGotAssocTxOk (); |
|
152 } |
|
153 } |
|
154 void |
|
155 MacHighNqap::TxFailed (WifiMacHeader const &hdr) |
|
156 { |
|
157 MacStation *station = m_stations->Lookup (hdr.GetAddr1 ()); |
|
158 if (hdr.IsAssocResp () && |
|
159 station->IsWaitAssocTxOk ()) |
|
160 { |
|
161 TRACE ("assoc failed with sta="<<hdr.GetAddr1 ()); |
|
162 station->RecordGotAssocTxFailed (); |
|
163 } |
|
164 } |
|
165 void |
|
166 MacHighNqap::Receive (Packet packet, WifiMacHeader const *hdr) |
|
167 { |
|
168 MacStation *station = m_stations->Lookup (hdr->GetAddr2 ()); |
|
169 |
|
170 if (hdr->IsData ()) |
|
171 { |
|
172 if (!hdr->IsFromDs () && |
|
173 hdr->IsToDs () && |
|
174 hdr->GetAddr1 () == m_interface->GetSelfAddress () && |
|
175 station->IsAssociated ()) |
|
176 { |
|
177 if (hdr->GetAddr3 () == m_interface->GetSelfAddress ()) |
|
178 { |
|
179 m_forwardUp (packet); |
|
180 } |
|
181 else |
|
182 { |
|
183 ForwardDown (packet, |
|
184 hdr->GetAddr2 (), |
|
185 hdr->GetAddr3 ()); |
|
186 m_forwardUp (packet); |
|
187 } |
|
188 } |
|
189 else if (hdr->IsFromDs () && |
|
190 hdr->IsToDs ()) |
|
191 { |
|
192 // this is an AP-to-AP frame |
|
193 // we ignore for now. |
|
194 } |
|
195 else |
|
196 { |
|
197 // we can ignore these frames since |
|
198 // they are not targeted at the AP |
|
199 } |
|
200 } |
|
201 else if (hdr->IsMgt ()) |
|
202 { |
|
203 if (hdr->IsProbeReq ()) |
|
204 { |
|
205 NS_ASSERT (hdr->GetAddr1 ().IsBroadcast ()); |
|
206 SendProbeResp (hdr->GetAddr2 ()); |
|
207 } |
|
208 else if (hdr->GetAddr1 () == m_interface->GetSelfAddress ()) |
|
209 { |
|
210 if (hdr->IsAssocReq ()) |
|
211 { |
|
212 station->RecordWaitAssocTxOk (); |
|
213 SendAssocResp (hdr->GetAddr2 ()); |
|
214 } |
|
215 else if (hdr->IsDisassociation ()) |
|
216 { |
|
217 station->RecordDisassociated (); |
|
218 } |
|
219 else if (hdr->IsReassocReq ()) |
|
220 { |
|
221 /* we don't support reassoc frames for now */ |
|
222 } |
|
223 else if (hdr->IsAuthentication () || |
|
224 hdr->IsDeauthentication ()) |
|
225 { |
|
226 /* |
|
227 */ |
|
228 } |
|
229 else |
|
230 { |
|
231 /* unknown mgt frame |
|
232 */ |
|
233 } |
|
234 } |
|
235 } |
|
236 else |
|
237 { |
|
238 /* damn, what could this be ? a control frame ? |
|
239 * control frames should never reach the MacHigh so, |
|
240 * this is likely to be a bug. assert. |
|
241 */ |
|
242 NS_ASSERT (false); |
|
243 } |
|
244 } |
|
245 |
|
246 } // namespace ns3 |