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