A Discrete-Event Network Simulator
API
wave-simple-device.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Junling Bu <linlinjavaer@gmail.com>
16 */
17#include "ns3/command-line.h"
18#include "ns3/mobility-helper.h"
19#include "ns3/net-device-container.h"
20#include "ns3/node-container.h"
21#include "ns3/node.h"
22#include "ns3/packet.h"
23#include "ns3/seq-ts-header.h"
24#include "ns3/simulator.h"
25#include "ns3/wave-helper.h"
26#include "ns3/wave-mac-helper.h"
27#include "ns3/wave-net-device.h"
28#include "ns3/yans-wifi-helper.h"
29
30using namespace ns3;
31
41{
42 public:
44 void SendWsmpExample();
45
47 void SendIpExample();
48
50 void SendWsaExample();
51
52 private:
64 void SendIpPacket(uint32_t seq, bool ipv6);
73 bool Receive(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
82 void CreateWaveNodes();
83
86};
87
88void
90{
92 nodes.Create(2);
93
95 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
96 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
97 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
98 mobility.SetPositionAllocator(positionAlloc);
99 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
100 mobility.Install(nodes);
101
102 YansWifiChannelHelper waveChannel = YansWifiChannelHelper::Default();
103 YansWavePhyHelper wavePhy = YansWavePhyHelper::Default();
104 wavePhy.SetChannel(waveChannel.Create());
105 wavePhy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11);
106 QosWaveMacHelper waveMac = QosWaveMacHelper::Default();
107 WaveHelper waveHelper = WaveHelper::Default();
108 devices = waveHelper.Install(wavePhy, waveMac, nodes);
109
110 for (uint32_t i = 0; i != devices.GetN(); ++i)
111 {
112 Ptr<WaveNetDevice> device = DynamicCast<WaveNetDevice>(devices.Get(i));
115 }
116
117 // Tracing
118 wavePhy.EnablePcap("wave-simple-device", devices);
119}
120
121bool
124 uint16_t mode,
125 const Address& sender)
126{
127 SeqTsHeader seqTs;
128 pkt->PeekHeader(seqTs);
129 std::cout << "receive a packet: " << std::endl
130 << " sequence = " << seqTs.GetSeq() << "," << std::endl
131 << " sendTime = " << seqTs.GetTs().As(Time::S) << "," << std::endl
132 << " recvTime = " << Now().As(Time::S) << "," << std::endl
133 << " protocol = 0x" << std::hex << mode << std::dec << std::endl;
134 return true;
135}
136
137void
139{
140 Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice>(devices.Get(0));
141 Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice>(devices.Get(1));
142 const static uint16_t WSMP_PROT_NUMBER = 0x88DC;
143 Mac48Address bssWildcard = Mac48Address::GetBroadcast();
144
145 const TxInfo txInfo = TxInfo(channel);
146 Ptr<Packet> p = Create<Packet>(100);
147 SeqTsHeader seqTs;
148 seqTs.SetSeq(seq);
149 p->AddHeader(seqTs);
150 sender->SendX(p, bssWildcard, WSMP_PROT_NUMBER, txInfo);
151}
152
153void
155{
157 Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice>(devices.Get(0));
158 Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice>(devices.Get(1));
159
160 // Alternating access without immediate channel switch
161 const SchInfo schInfo = SchInfo(SCH1, false, EXTENDED_ALTERNATING);
162 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, sender, schInfo);
163 // An important point is that the receiver should also be assigned channel
164 // access for the same channel to receive packets.
165 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, receiver, schInfo);
166
167 // send WSMP packets
168 // the first packet will be queued currently and be transmitted in next SCH interval
169 Simulator::Schedule(Seconds(1.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH1, 1);
170 // the second packet will be queued currently and then be transmitted , because of in the CCH
171 // interval.
172 Simulator::Schedule(Seconds(1.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, CCH, 2);
173 // the third packet will be dropped because of no channel access for SCH2.
174 Simulator::Schedule(Seconds(1.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH2, 3);
175
176 // release SCH access
177 Simulator::Schedule(Seconds(2.0), &WaveNetDevice::StopSch, sender, SCH1);
178 Simulator::Schedule(Seconds(2.0), &WaveNetDevice::StopSch, receiver, SCH1);
179 // the fourth packet will be queued and be transmitted because of default CCH access assigned
180 // automatically.
181 Simulator::Schedule(Seconds(3.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, CCH, 4);
182 // the fifth packet will be dropped because of no SCH1 access assigned
183 Simulator::Schedule(Seconds(3.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH1, 5);
184
185 Simulator::Stop(Seconds(5.0));
186 Simulator::Run();
187 Simulator::Destroy();
188}
189
190void
192{
193 Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice>(devices.Get(0));
194 Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice>(devices.Get(1));
195 const Address dest = receiver->GetAddress();
196 // send IPv4 packet or IPv6 packet
197 const static uint16_t IPv4_PROT_NUMBER = 0x0800;
198 const static uint16_t IPv6_PROT_NUMBER = 0x86DD;
199 uint16_t protocol = ipv6 ? IPv6_PROT_NUMBER : IPv4_PROT_NUMBER;
200 Ptr<Packet> p = Create<Packet>(100);
201 SeqTsHeader seqTs;
202 seqTs.SetSeq(seq);
203 p->AddHeader(seqTs);
204 sender->Send(p, dest, protocol);
205}
206
207void
209{
211 Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice>(devices.Get(0));
212 Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice>(devices.Get(1));
213
214 // Alternating access without immediate channel switch
215 const SchInfo schInfo = SchInfo(SCH1, false, EXTENDED_ALTERNATING);
216 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, sender, schInfo);
217 // An important point is that the receiver should also be assigned channel
218 // access for the same channel to receive packets.
219 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, receiver, schInfo);
220
221 // both IPv4 and IPv6 packets below will not be inserted to internal queue because of no tx
222 // profile registered
223 Simulator::Schedule(Seconds(1.0), &WaveNetDeviceExample::SendIpPacket, this, 1, true);
224 Simulator::Schedule(Seconds(1.050), &WaveNetDeviceExample::SendIpPacket, this, 2, false);
225 // register txprofile
226 // IP packets will automatically be sent with txprofile parameter
227 const TxProfile txProfile = TxProfile(SCH1);
228 Simulator::Schedule(Seconds(2.0), &WaveNetDevice::RegisterTxProfile, sender, txProfile);
229 // both IPv4 and IPv6 packet are transmitted successfully
230 Simulator::Schedule(Seconds(3.0), &WaveNetDeviceExample::SendIpPacket, this, 3, true);
231 Simulator::Schedule(Seconds(3.050), &WaveNetDeviceExample::SendIpPacket, this, 4, false);
232 // unregister TxProfile or release channel access
233 Simulator::Schedule(Seconds(4.0), &WaveNetDevice::DeleteTxProfile, sender, SCH1);
234 Simulator::Schedule(Seconds(4.0), &WaveNetDevice::StopSch, sender, SCH1);
235 Simulator::Schedule(Seconds(4.0), &WaveNetDevice::StopSch, receiver, SCH1);
236 // these packets will be dropped again because of no channel access assigned and no tx profile
237 // registered
238 Simulator::Schedule(Seconds(5.0), &WaveNetDeviceExample::SendIpPacket, this, 5, true);
239 Simulator::Schedule(Seconds(5.050), &WaveNetDeviceExample::SendIpPacket, this, 6, false);
240
241 Simulator::Stop(Seconds(6.0));
242 Simulator::Run();
243 Simulator::Destroy();
244}
245
246bool
248{
249 std::cout << "receive a VSA management frame: recvTime = " << Now().As(Time::S) << "."
250 << std::endl;
251 return true;
252}
253
254void
256{
258 Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice>(devices.Get(0));
259 Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice>(devices.Get(1));
260
261 // Alternating access without immediate channel switch for sender and receiver
262 const SchInfo schInfo = SchInfo(SCH1, false, EXTENDED_ALTERNATING);
263 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, sender, schInfo);
264 Simulator::Schedule(Seconds(0.0), &WaveNetDevice::StartSch, receiver, schInfo);
265
266 // the peer address of VSA is broadcast address, and the repeat rate
267 // of VsaInfo is 100 per 5s, the VSA frame will be sent repeatedly.
268 Ptr<Packet> wsaPacket = Create<Packet>(100);
269 Mac48Address dest = Mac48Address::GetBroadcast();
270 const VsaInfo vsaInfo =
271 VsaInfo(dest, OrganizationIdentifier(), 0, wsaPacket, SCH1, 100, VSA_TRANSMIT_IN_BOTHI);
272 Simulator::Schedule(Seconds(1.0), &WaveNetDevice::StartVsa, sender, vsaInfo);
273 Simulator::Schedule(Seconds(3.0), &WaveNetDevice::StopVsa, sender, SCH1);
274
275 // release alternating access
276 Simulator::Schedule(Seconds(4.0), &WaveNetDevice::StopSch, sender, SCH1);
277 Simulator::Schedule(Seconds(4.0), &WaveNetDevice::StopSch, receiver, SCH1);
278
279 // these WSA packets cannot be transmitted because of no channel access assigned
280 Simulator::Schedule(Seconds(5.0), &WaveNetDevice::StartVsa, sender, vsaInfo);
281 Simulator::Schedule(Seconds(6.0), &WaveNetDevice::StopVsa, sender, SCH1);
282
283 Simulator::Stop(Seconds(6.0));
284 Simulator::Run();
285 Simulator::Destroy();
286}
287
288int
289main(int argc, char* argv[])
290{
291 CommandLine cmd(__FILE__);
292 cmd.Parse(argc, argv);
293
294 WaveNetDeviceExample example;
295 std::cout << "run WAVE WSMP routing service case:" << std::endl;
296 example.SendWsmpExample();
297 std::cout << "run WAVE IP routing service case:" << std::endl;
298 // example.SendIpExample ();
299 std::cout << "run WAVE WSA routing service case:" << std::endl;
300 // example.SendWsaExample ();
301 return 0;
302}
#define SCH2
#define SCH1
#define CCH
#define EXTENDED_ALTERNATING
This simulation is to show the routing service of WaveNetDevice described in IEEE 09....
void CreateWaveNodes()
Create WAVE nodes function.
void SendWsmpExample()
Send WSMP example function.
void SendWsaExample()
Send WSA example.
bool Receive(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive function.
void SendOneWsmpPacket(uint32_t channel, uint32_t seq)
Send one WSMP packet function.
NetDeviceContainer devices
the devices
void SendIpPacket(uint32_t seq, bool ipv6)
Send IP packet function.
NodeContainer nodes
the nodes
void SendIpExample()
Send IP example function.
bool ReceiveVsa(Ptr< const Packet > pkt, const Address &address, uint32_t, uint32_t)
Receive VSA function.
a polymophic address class
Definition: address.h:92
Parse command-line arguments.
Definition: command-line.h:232
an EUI-48 address
Definition: mac48-address.h:46
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
uint32_t GetN() const
Get the number of Ptr<NetDevice> stored in this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
the organization identifier is a public organizationally unique identifier assigned by the IEEE.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
Qos Wave Mac Helper class.
Packet header to carry sequence number and timestamp.
Definition: seq-ts-header.h:45
void SetSeq(uint32_t seq)
Time GetTs() const
uint32_t GetSeq() const
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
helps to create WaveNetDevice objects
Definition: wave-helper.h:116
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wave-helper.cc:336
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Address GetAddress() const override
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
bool SendX(Ptr< Packet > packet, const Address &dest, uint32_t protocol, const TxInfo &txInfo)
void SetWaveVsaCallback(WaveVsaCallback vsaCallback)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
To trace WaveNetDevice, we have to overwrite the trace functions of class YansWifiPhyHelper.
Definition: wave-helper.h:42
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() const
void SetChannel(Ptr< YansWifiChannel > channel)
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
@ VSA_TRANSMIT_IN_BOTHI
Definition: vsa-manager.h:39
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:691
cmd
Definition: second.py:33
channel
Definition: third.py:81
mobility
Definition: third.py:96