A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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:
58 void SendOneWsmpPacket(uint32_t channel, uint32_t seq);
64 void SendIpPacket(uint32_t seq, bool ipv6);
73 bool Receive(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
80 bool ReceiveVsa(Ptr<const Packet> pkt, const Address& address, uint32_t, uint32_t);
82 void CreateWaveNodes();
83
86};
87
88void
90{
92 nodes.Create(2);
93
94 MobilityHelper mobility;
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
104 wavePhy.SetChannel(waveChannel.Create());
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));
113 device->SetReceiveCallback(MakeCallback(&WaveNetDeviceExample::Receive, this));
114 device->SetWaveVsaCallback(MakeCallback(&WaveNetDeviceExample::ReceiveVsa, this));
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;
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
170 // the second packet will be queued currently and then be transmitted , because of in the CCH
171 // interval.
173 // the third packet will be dropped because of no channel access for SCH2.
175
176 // release SCH access
179 // the fourth packet will be queued and be transmitted because of default CCH access assigned
180 // automatically.
182 // the fifth packet will be dropped because of no SCH1 access assigned
184
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
225 // register txprofile
226 // IP packets will automatically be sent with txprofile parameter
227 const TxProfile txProfile = TxProfile(SCH1);
229 // both IPv4 and IPv6 packet are transmitted successfully
232 // unregister TxProfile or release channel access
236 // these packets will be dropped again because of no channel access assigned and no tx profile
237 // registered
240
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);
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);
274
275 // release alternating access
278
279 // these WSA packets cannot be transmitted because of no channel access assigned
280 Simulator::Schedule(Seconds(5.0), &WaveNetDevice::StartVsa, sender, vsaInfo);
282
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:100
Parse command-line arguments.
Definition: command-line.h:232
an EUI-48 address
Definition: mac48-address.h:46
static Mac48Address GetBroadcast()
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 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.
static QosWaveMacHelper Default()
Create a mac helper in a default working state.
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
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
@ S
second
Definition: nstime.h:116
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:335
static WaveHelper Default()
Definition: wave-helper.cc:285
bool StartSch(const SchInfo &schInfo)
bool DeleteTxProfile(uint32_t channelNumber)
bool StopVsa(uint32_t channelNumber)
bool RegisterTxProfile(const TxProfile &txprofile)
bool StartVsa(const VsaInfo &vsaInfo)
bool StopSch(uint32_t channelNumber)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
@ DLT_IEEE802_11
IEEE 802.11 Wireless LAN headers on packets.
Definition: wifi-helper.h:175
To trace WaveNetDevice, we have to overwrite the trace functions of class YansWifiPhyHelper.
Definition: wave-helper.h:42
static YansWavePhyHelper Default()
Create a phy helper in a default working state.
Definition: wave-helper.cc:122
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
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:1336
@ VSA_TRANSMIT_IN_BOTHI
Definition: vsa-manager.h:39
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:707