1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008,2009 IITP RAS
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: Kirill Andreev <andreev@iitp.ru>
19  *         Pavel Boyko <boyko@iitp.ru>
20  */
21 
22 #include "mesh-helper.h"
23 #include "ns3/simulator.h"
24 #include "ns3/pointer.h"
25 #include "ns3/mesh-point-device.h"
26 #include "ns3/wifi-net-device.h"
27 #include "ns3/minstrel-wifi-manager.h"
28 #include "ns3/mesh-wifi-interface-mac.h"
29 #include "ns3/wifi-helper.h"
30 #include "ns3/frame-exchange-manager.h"
31 #include "ns3/wifi-default-protection-manager.h"
32 #include "ns3/wifi-default-ack-manager.h"
33 
34 namespace ns3
35 {
MeshHelper()36 MeshHelper::MeshHelper () :
37   m_nInterfaces (1),
38   m_spreadChannelPolicy (ZERO_CHANNEL),
39   m_stack (0),
40   m_standard (WIFI_STANDARD_80211a)
41 {
42 }
~MeshHelper()43 MeshHelper::~MeshHelper ()
44 {
45   m_stack = 0;
46 }
47 void
SetSpreadInterfaceChannels(enum ChannelPolicy policy)48 MeshHelper::SetSpreadInterfaceChannels (enum ChannelPolicy policy)
49 {
50   m_spreadChannelPolicy = policy;
51 }
52 void
SetStackInstaller(std::string type,std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)53 MeshHelper::SetStackInstaller (std::string type,
54                                std::string n0, const AttributeValue &v0,
55                                std::string n1, const AttributeValue &v1,
56                                std::string n2, const AttributeValue &v2,
57                                std::string n3, const AttributeValue &v3,
58                                std::string n4, const AttributeValue &v4,
59                                std::string n5, const AttributeValue &v5,
60                                std::string n6, const AttributeValue &v6,
61                                std::string n7, const AttributeValue &v7)
62 {
63   m_stackFactory.SetTypeId (type);
64   m_stackFactory.Set (n0, v0);
65   m_stackFactory.Set (n1, v1);
66   m_stackFactory.Set (n2, v2);
67   m_stackFactory.Set (n3, v3);
68   m_stackFactory.Set (n4, v4);
69   m_stackFactory.Set (n5, v5);
70   m_stackFactory.Set (n6, v6);
71   m_stackFactory.Set (n7, v7);
72 
73   m_stack = m_stackFactory.Create<MeshStack> ();
74   if (m_stack == 0)
75     {
76       NS_FATAL_ERROR ("Stack has not been created: " << type);
77     }
78 }
79 
80 void
SetNumberOfInterfaces(uint32_t nInterfaces)81 MeshHelper::SetNumberOfInterfaces (uint32_t nInterfaces)
82 {
83   m_nInterfaces = nInterfaces;
84 }
85 NetDeviceContainer
Install(const WifiPhyHelper & phyHelper,NodeContainer c) const86 MeshHelper::Install (const WifiPhyHelper &phyHelper, NodeContainer c) const
87 {
88   NetDeviceContainer devices;
89   NS_ASSERT (m_stack != 0);
90   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
91     {
92       Ptr<Node> node = *i;
93       // Create a mesh point device
94       Ptr<MeshPointDevice> mp = CreateObject<MeshPointDevice> ();
95       node->AddDevice (mp);
96       // Create wifi interfaces (single interface by default)
97       for (uint32_t i = 0; i < m_nInterfaces; ++i)
98         {
99           uint32_t channel = 0;
100           if (m_spreadChannelPolicy == ZERO_CHANNEL)
101             {
102               channel = 100;
103             }
104           if (m_spreadChannelPolicy == SPREAD_CHANNELS)
105             {
106               channel = 100 + i * 5;
107             }
108           Ptr<WifiNetDevice> iface = CreateInterface (phyHelper, node, channel);
109           mp->AddInterface (iface);
110         }
111       if (!m_stack->InstallStack (mp))
112         {
113           NS_FATAL_ERROR ("Stack is not installed!");
114         }
115       devices.Add (mp);
116     }
117   return devices;
118 }
119 MeshHelper
Default(void)120 MeshHelper::Default (void)
121 {
122   MeshHelper helper;
123   helper.SetMacType ();
124   helper.SetRemoteStationManager ("ns3::ArfWifiManager");
125   helper.SetSpreadInterfaceChannels (SPREAD_CHANNELS);
126   return helper;
127 }
128 
129 void
SetMacType(std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)130 MeshHelper::SetMacType (std::string n0, const AttributeValue &v0,
131                         std::string n1, const AttributeValue &v1,
132                         std::string n2, const AttributeValue &v2,
133                         std::string n3, const AttributeValue &v3,
134                         std::string n4, const AttributeValue &v4,
135                         std::string n5, const AttributeValue &v5,
136                         std::string n6, const AttributeValue &v6,
137                         std::string n7, const AttributeValue &v7)
138 {
139   m_mac.SetTypeId ("ns3::MeshWifiInterfaceMac");
140   m_mac.Set (n0, v0);
141   m_mac.Set (n1, v1);
142   m_mac.Set (n2, v2);
143   m_mac.Set (n3, v3);
144   m_mac.Set (n4, v4);
145   m_mac.Set (n5, v5);
146   m_mac.Set (n6, v6);
147   m_mac.Set (n7, v7);
148 }
149 void
SetRemoteStationManager(std::string type,std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)150 MeshHelper::SetRemoteStationManager (std::string type,
151                                      std::string n0, const AttributeValue &v0,
152                                      std::string n1, const AttributeValue &v1,
153                                      std::string n2, const AttributeValue &v2,
154                                      std::string n3, const AttributeValue &v3,
155                                      std::string n4, const AttributeValue &v4,
156                                      std::string n5, const AttributeValue &v5,
157                                      std::string n6, const AttributeValue &v6,
158                                      std::string n7, const AttributeValue &v7)
159 {
160   m_stationManager = ObjectFactory ();
161   m_stationManager.SetTypeId (type);
162   m_stationManager.Set (n0, v0);
163   m_stationManager.Set (n1, v1);
164   m_stationManager.Set (n2, v2);
165   m_stationManager.Set (n3, v3);
166   m_stationManager.Set (n4, v4);
167   m_stationManager.Set (n5, v5);
168   m_stationManager.Set (n6, v6);
169   m_stationManager.Set (n7, v7);
170 }
171 void
SetStandard(enum WifiStandard standard)172 MeshHelper::SetStandard (enum WifiStandard standard)
173 {
174   m_standard = standard;
175 }
176 
177 Ptr<WifiNetDevice>
CreateInterface(const WifiPhyHelper & phyHelper,Ptr<Node> node,uint16_t channelId) const178 MeshHelper::CreateInterface (const WifiPhyHelper &phyHelper, Ptr<Node> node, uint16_t channelId) const
179 {
180   Ptr<WifiNetDevice> device = CreateObject<WifiNetDevice> ();
181 
182   auto it = wifiStandards.find (m_standard);
183   if (it == wifiStandards.end ())
184     {
185       NS_FATAL_ERROR ("Selected standard is not defined!");
186       return device;
187     }
188 
189   Ptr<MeshWifiInterfaceMac> mac = m_mac.Create<MeshWifiInterfaceMac> ();
190   NS_ASSERT (mac != 0);
191   mac->SetSsid (Ssid ());
192   mac->SetDevice (device);
193   Ptr<WifiRemoteStationManager> manager = m_stationManager.Create<WifiRemoteStationManager> ();
194   NS_ASSERT (manager != 0);
195   Ptr<WifiPhy> phy = phyHelper.Create (node, device);
196   mac->SetAddress (Mac48Address::Allocate ());
197   mac->ConfigureStandard (m_standard);
198   Ptr<RegularWifiMac> wifiMac = DynamicCast<RegularWifiMac> (mac);
199   Ptr<FrameExchangeManager> fem;
200   if (wifiMac != 0 && (fem = wifiMac->GetFrameExchangeManager ()) != 0)
201     {
202       Ptr<WifiProtectionManager> protectionManager = CreateObject<WifiDefaultProtectionManager> ();
203       protectionManager->SetWifiMac (wifiMac);
204       fem->SetProtectionManager (protectionManager);
205 
206       Ptr<WifiAckManager> ackManager = CreateObject<WifiDefaultAckManager> ();
207       ackManager->SetWifiMac (wifiMac);
208       fem->SetAckManager (ackManager);
209     }
210   phy->ConfigureStandardAndBand (it->second.phyStandard, it->second.phyBand);
211   device->SetMac (mac);
212   device->SetPhy (phy);
213   device->SetRemoteStationManager (manager);
214   node->AddDevice (device);
215   mac->SwitchFrequencyChannel (channelId);
216 
217   return device;
218 }
219 void
Report(const ns3::Ptr<ns3::NetDevice> & device,std::ostream & os)220 MeshHelper::Report (const ns3::Ptr<ns3::NetDevice>& device, std::ostream& os)
221 {
222   NS_ASSERT (m_stack != 0);
223   Ptr<MeshPointDevice> mp = device->GetObject<MeshPointDevice> ();
224   NS_ASSERT (mp != 0);
225   std::vector<Ptr<NetDevice> > ifaces = mp->GetInterfaces ();
226   os << "<MeshPointDevice time=\"" << Simulator::Now ().GetSeconds () << "\" address=\""
227      << Mac48Address::ConvertFrom (mp->GetAddress ()) << "\">\n";
228   m_stack->Report (mp, os);
229   os << "</MeshPointDevice>\n";
230 }
231 void
ResetStats(const ns3::Ptr<ns3::NetDevice> & device)232 MeshHelper::ResetStats (const ns3::Ptr<ns3::NetDevice>& device)
233 {
234   NS_ASSERT (m_stack != 0);
235   Ptr<MeshPointDevice> mp = device->GetObject<MeshPointDevice> ();
236   NS_ASSERT (mp != 0);
237   m_stack->ResetStats (mp);
238 }
239 int64_t
AssignStreams(NetDeviceContainer c,int64_t stream)240 MeshHelper::AssignStreams (NetDeviceContainer c, int64_t stream)
241 {
242   int64_t currentStream = stream;
243   Ptr<NetDevice> netDevice;
244   for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
245     {
246       netDevice = (*i);
247       Ptr<MeshPointDevice> mpd = DynamicCast<MeshPointDevice> (netDevice);
248       Ptr<WifiNetDevice> wifi;
249       Ptr<MeshWifiInterfaceMac> mac;
250       if (mpd)
251         {
252           // To access, we need the underlying WifiNetDevices
253           std::vector<Ptr<NetDevice> > ifaces = mpd->GetInterfaces ();
254           for (std::vector<Ptr<NetDevice> >::iterator i = ifaces.begin (); i != ifaces.end (); i++)
255             {
256               wifi = DynamicCast<WifiNetDevice> (*i);
257 
258               // Handle any random numbers in the PHY objects.
259               currentStream += wifi->GetPhy ()->AssignStreams (currentStream);
260 
261               // Handle any random numbers in the station managers.
262               Ptr<WifiRemoteStationManager> manager = wifi->GetRemoteStationManager ();
263               Ptr<MinstrelWifiManager> minstrel = DynamicCast<MinstrelWifiManager> (manager);
264               if (minstrel)
265                 {
266                   currentStream += minstrel->AssignStreams (currentStream);
267                 }
268               // Handle any random numbers in the mesh mac and plugins
269               mac = DynamicCast<MeshWifiInterfaceMac> (wifi->GetMac ());
270               if (mac)
271                 {
272                   currentStream += mac->AssignStreams (currentStream);
273                 }
274               Ptr<RegularWifiMac> rmac = DynamicCast<RegularWifiMac> (mac);
275               if (rmac)
276                 {
277                   PointerValue ptr;
278                   rmac->GetAttribute ("Txop", ptr);
279                   Ptr<Txop> txop = ptr.Get<Txop> ();
280                   currentStream += txop->AssignStreams (currentStream);
281 
282                   rmac->GetAttribute ("VO_Txop", ptr);
283                   Ptr<QosTxop> vo_txop = ptr.Get<QosTxop> ();
284                   currentStream += vo_txop->AssignStreams (currentStream);
285 
286                   rmac->GetAttribute ("VI_Txop", ptr);
287                   Ptr<QosTxop> vi_txop = ptr.Get<QosTxop> ();
288                   currentStream += vi_txop->AssignStreams (currentStream);
289 
290                   rmac->GetAttribute ("BE_Txop", ptr);
291                   Ptr<QosTxop> be_txop = ptr.Get<QosTxop> ();
292                   currentStream += be_txop->AssignStreams (currentStream);
293 
294                   rmac->GetAttribute ("BK_Txop", ptr);
295                   Ptr<QosTxop> bk_txop = ptr.Get<QosTxop> ();
296                   currentStream += bk_txop->AssignStreams (currentStream);
297                }
298             }
299         }
300     }
301   return (currentStream - stream);
302 }
303 
304 } // namespace ns3
305 
306