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