00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "yans-wifi-helper.h"
00021 #include "ns3/error-rate-model.h"
00022 #include "ns3/propagation-loss-model.h"
00023 #include "ns3/propagation-delay-model.h"
00024 #include "ns3/yans-wifi-channel.h"
00025 #include "ns3/yans-wifi-phy.h"
00026 #include "ns3/wifi-net-device.h"
00027 #include "ns3/pcap-writer.h"
00028 #include "ns3/simulator.h"
00029 #include "ns3/config.h"
00030
00031 namespace ns3 {
00032
00033 static void PcapPhyTxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet,
00034 WifiMode mode, WifiPreamble preamble,
00035 uint8_t txLevel)
00036 {
00037 writer->WritePacket (packet);
00038 }
00039
00040 static void PcapPhyRxEvent (Ptr<PcapWriter> writer,
00041 Ptr<const Packet> packet, double snr, WifiMode mode,
00042 enum WifiPreamble preamble)
00043 {
00044 writer->WritePacket (packet);
00045 }
00046
00047 static void AsciiPhyTxEvent (std::ostream *os, std::string context,
00048 Ptr<const Packet> packet,
00049 WifiMode mode, WifiPreamble preamble,
00050 uint8_t txLevel)
00051 {
00052 *os << "+ " << Simulator::Now () << " " << context << " " << *packet << std::endl;
00053 }
00054
00055 static void AsciiPhyRxOkEvent (std::ostream *os, std::string context,
00056 Ptr<const Packet> packet, double snr, WifiMode mode,
00057 enum WifiPreamble preamble)
00058 {
00059 *os << "r " << Simulator::Now () << " " << context << " " << *packet << std::endl;
00060 }
00061
00062
00063 YansWifiChannelHelper::YansWifiChannelHelper ()
00064 {}
00065
00066 YansWifiChannelHelper
00067 YansWifiChannelHelper::Default (void)
00068 {
00069 YansWifiChannelHelper helper;
00070 helper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
00071 helper.AddPropagationLoss ("ns3::LogDistancePropagationLossModel");
00072 return helper;
00073 }
00074
00075 void
00076 YansWifiChannelHelper::AddPropagationLoss (std::string type,
00077 std::string n0, const AttributeValue &v0,
00078 std::string n1, const AttributeValue &v1,
00079 std::string n2, const AttributeValue &v2,
00080 std::string n3, const AttributeValue &v3,
00081 std::string n4, const AttributeValue &v4,
00082 std::string n5, const AttributeValue &v5,
00083 std::string n6, const AttributeValue &v6,
00084 std::string n7, const AttributeValue &v7)
00085 {
00086 ObjectFactory factory;
00087 factory.SetTypeId (type);
00088 factory.Set (n0, v0);
00089 factory.Set (n1, v1);
00090 factory.Set (n2, v2);
00091 factory.Set (n3, v3);
00092 factory.Set (n4, v4);
00093 factory.Set (n5, v5);
00094 factory.Set (n6, v6);
00095 factory.Set (n7, v7);
00096 m_propagationLoss.push_back (factory);
00097 }
00098
00099 void
00100 YansWifiChannelHelper::SetPropagationDelay (std::string type,
00101 std::string n0, const AttributeValue &v0,
00102 std::string n1, const AttributeValue &v1,
00103 std::string n2, const AttributeValue &v2,
00104 std::string n3, const AttributeValue &v3,
00105 std::string n4, const AttributeValue &v4,
00106 std::string n5, const AttributeValue &v5,
00107 std::string n6, const AttributeValue &v6,
00108 std::string n7, const AttributeValue &v7)
00109 {
00110 ObjectFactory factory;
00111 factory.SetTypeId (type);
00112 factory.Set (n0, v0);
00113 factory.Set (n1, v1);
00114 factory.Set (n2, v2);
00115 factory.Set (n3, v3);
00116 factory.Set (n4, v4);
00117 factory.Set (n5, v5);
00118 factory.Set (n6, v6);
00119 factory.Set (n7, v7);
00120 m_propagationDelay = factory;
00121 }
00122
00123 Ptr<YansWifiChannel>
00124 YansWifiChannelHelper::Create (void) const
00125 {
00126 Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel> ();
00127 Ptr<PropagationLossModel> prev = 0;
00128 for (std::vector<ObjectFactory>::const_iterator i = m_propagationLoss.begin (); i != m_propagationLoss.end (); ++i)
00129 {
00130 Ptr<PropagationLossModel> cur = (*i).Create<PropagationLossModel> ();
00131 if (prev != 0)
00132 {
00133 prev->SetNext (cur);
00134 }
00135 if (m_propagationLoss.begin () == i)
00136 {
00137 channel->SetPropagationLossModel (cur);
00138 }
00139 prev = cur;
00140 }
00141 Ptr<PropagationDelayModel> delay = m_propagationDelay.Create<PropagationDelayModel> ();
00142 channel->SetPropagationDelayModel (delay);
00143 return channel;
00144 }
00145
00146
00147 YansWifiPhyHelper::YansWifiPhyHelper ()
00148 : m_channel (0)
00149 {
00150 m_phy.SetTypeId ("ns3::YansWifiPhy");
00151 }
00152
00153 YansWifiPhyHelper
00154 YansWifiPhyHelper::Default (void)
00155 {
00156 YansWifiPhyHelper helper;
00157 helper.SetErrorRateModel ("ns3::YansErrorRateModel");
00158 return helper;
00159 }
00160
00161 void
00162 YansWifiPhyHelper::SetChannel (Ptr<YansWifiChannel> channel)
00163 {
00164 m_channel = channel;
00165 }
00166 void
00167 YansWifiPhyHelper::Set (std::string name, const AttributeValue &v)
00168 {
00169 m_phy.Set (name, v);
00170 }
00171
00172 void
00173 YansWifiPhyHelper::SetErrorRateModel (std::string name,
00174 std::string n0, const AttributeValue &v0,
00175 std::string n1, const AttributeValue &v1,
00176 std::string n2, const AttributeValue &v2,
00177 std::string n3, const AttributeValue &v3,
00178 std::string n4, const AttributeValue &v4,
00179 std::string n5, const AttributeValue &v5,
00180 std::string n6, const AttributeValue &v6,
00181 std::string n7, const AttributeValue &v7)
00182 {
00183 m_errorRateModel = ObjectFactory ();
00184 m_errorRateModel.SetTypeId (name);
00185 m_errorRateModel.Set (n0, v0);
00186 m_errorRateModel.Set (n1, v1);
00187 m_errorRateModel.Set (n2, v2);
00188 m_errorRateModel.Set (n3, v3);
00189 m_errorRateModel.Set (n4, v4);
00190 m_errorRateModel.Set (n5, v5);
00191 m_errorRateModel.Set (n6, v6);
00192 m_errorRateModel.Set (n7, v7);
00193 }
00194
00195
00196 Ptr<WifiPhy>
00197 YansWifiPhyHelper::Create (Ptr<Node> node, Ptr<WifiNetDevice> device) const
00198 {
00199 Ptr<YansWifiPhy> phy = m_phy.Create<YansWifiPhy> ();
00200 Ptr<ErrorRateModel> error = m_errorRateModel.Create<ErrorRateModel> ();
00201 phy->SetErrorRateModel (error);
00202 phy->SetChannel (m_channel);
00203 phy->SetMobility (node);
00204 phy->SetDevice (device);
00205 return phy;
00206 }
00207
00208 void
00209 YansWifiPhyHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
00210 {
00211 std::ostringstream oss;
00212 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/";
00213 Config::MatchContainer matches = Config::LookupMatches (oss.str ());
00214 if (matches.GetN () == 0)
00215 {
00216 return;
00217 }
00218 oss.str ("");
00219 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
00220
00221
00222 Ptr<PcapWriter> pcap = ::ns3::Create<PcapWriter> ();
00223 pcap->Open (oss.str ());
00224 pcap->WriteWifiHeader ();
00225 oss.str ("");
00226 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/Tx";
00227 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PcapPhyTxEvent, pcap));
00228 oss.str ("");
00229 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/RxOk";
00230 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PcapPhyRxEvent, pcap));
00231 }
00232 void
00233 YansWifiPhyHelper::EnablePcap (std::string filename, NetDeviceContainer d)
00234 {
00235 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00236 {
00237 Ptr<NetDevice> dev = *i;
00238 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00239 }
00240 }
00241 void
00242 YansWifiPhyHelper::EnablePcap (std::string filename, NodeContainer n)
00243 {
00244 NetDeviceContainer devs;
00245 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00246 {
00247 Ptr<Node> node = *i;
00248 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00249 {
00250 devs.Add (node->GetDevice (j));
00251 }
00252 }
00253 EnablePcap (filename, devs);
00254 }
00255
00256 void
00257 YansWifiPhyHelper::EnablePcapAll (std::string filename)
00258 {
00259 EnablePcap (filename, NodeContainer::GetGlobal ());
00260 }
00261
00262 void
00263 YansWifiPhyHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
00264 {
00265 Packet::EnablePrinting ();
00266 std::ostringstream oss;
00267 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/RxOk";
00268 Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyRxOkEvent, &os));
00269 oss.str ("");
00270 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::WifiNetDevice/Phy/State/Tx";
00271 Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTxEvent, &os));
00272 }
00273 void
00274 YansWifiPhyHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
00275 {
00276 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00277 {
00278 Ptr<NetDevice> dev = *i;
00279 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00280 }
00281 }
00282 void
00283 YansWifiPhyHelper::EnableAscii (std::ostream &os, NodeContainer n)
00284 {
00285 NetDeviceContainer devs;
00286 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00287 {
00288 Ptr<Node> node = *i;
00289 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00290 {
00291 devs.Add (node->GetDevice (j));
00292 }
00293 }
00294 EnableAscii (os, devs);
00295 }
00296
00297 void
00298 YansWifiPhyHelper::EnableAsciiAll (std::ostream &os)
00299 {
00300 EnableAscii (os, NodeContainer::GetGlobal ());
00301 }
00302
00303
00304
00305 }