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/node.h"
19 #include "ns3/packet.h"
20 #include "ns3/simulator.h"
21 #include "ns3/node-container.h"
22 #include "ns3/net-device-container.h"
23 #include "ns3/yans-wifi-helper.h"
24 #include "ns3/mobility-helper.h"
25 #include "ns3/seq-ts-header.h"
26 #include "ns3/wave-net-device.h"
27 #include "ns3/wave-mac-helper.h"
28 #include "ns3/wave-helper.h"
29 
30 using namespace ns3;
40 {
41 public:
42  void SendWsmpExample (void);
43 
44  void SendIpExample (void);
45 
46  void SendWsaExample (void);
47 
48 private:
49  void SendOneWsmpPacket (uint32_t channel, uint32_t seq);
50  void SendIpPacket (uint32_t seq, bool ipv6);
51  bool Receive (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
52  bool ReceiveVsa (Ptr<const Packet>,const Address &, uint32_t, uint32_t);
53  void CreateWaveNodes (void);
54 
57 };
58 void
60 {
61  nodes = NodeContainer ();
62  nodes.Create (2);
63 
64  MobilityHelper mobility;
65  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
66  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
67  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
68  mobility.SetPositionAllocator (positionAlloc);
69  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
70  mobility.Install (nodes);
71 
74  wavePhy.SetChannel (waveChannel.Create ());
77  WaveHelper waveHelper = WaveHelper::Default ();
78  devices = waveHelper.Install (wavePhy, waveMac, nodes);
79 
80  for (uint32_t i = 0; i != devices.GetN (); ++i)
81  {
82  Ptr<WaveNetDevice> device = DynamicCast<WaveNetDevice> (devices.Get (i));
85  }
86 
87  // Tracing
88  wavePhy.EnablePcap ("wave-simple-device", devices);
89 }
90 
91 bool
93 {
94  SeqTsHeader seqTs;
95  pkt->PeekHeader (seqTs);
96  std::cout << "receive a packet: " << std::endl
97  << " sequence = " << seqTs.GetSeq () << "," << std::endl
98  << " sendTime = " << seqTs.GetTs ().GetSeconds () << "s," << std::endl
99  << " recvTime = " << Now ().GetSeconds () << "s," << std::endl
100  << " protocol = 0x" << std::hex << mode << std::dec << std::endl;
101  return true;
102 }
103 
104 void
105 WaveNetDeviceExample::SendOneWsmpPacket (uint32_t channel, uint32_t seq)
106 {
107  Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice> (devices.Get (0));
108  Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice> (devices.Get (1));
109  const static uint16_t WSMP_PROT_NUMBER = 0x88DC;
110  Mac48Address bssWildcard = Mac48Address::GetBroadcast ();
111 
112  const TxInfo txInfo = TxInfo (channel);
113  Ptr<Packet> p = Create<Packet> (100);
114  SeqTsHeader seqTs;
115  seqTs.SetSeq (seq);
116  p->AddHeader (seqTs);
117  sender->SendX (p, bssWildcard, WSMP_PROT_NUMBER, txInfo);
118 
119  Ptr<Packet> p2 = Create<Packet> (100);
120  SeqTsHeader seqTs2;
121  seqTs2.SetSeq (seq + 1);
122  p2->AddHeader (seqTs2);
123  receiver->SendX (p2, bssWildcard, WSMP_PROT_NUMBER, txInfo);
124 
125 }
126 
127 void
129 {
130  CreateWaveNodes ();
131  Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice> (devices.Get (0));
132  Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice> (devices.Get (1));
133 
134  // Alternating access without immediate channel switch
135  const SchInfo schInfo = SchInfo (SCH1, false, EXTENDED_ALTERNATING);
136  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch,sender,schInfo);
137  // An important point is that the receiver should also be assigned channel
138  // access for the same channel to receive packets.
139  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch, receiver, schInfo);
140 
141  // send WSMP packets
142  // the first packet will be queued currently and be transmitted in next SCH interval
143  //Simulator::Schedule (Seconds (1.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH1, 1);
144  // the second packet will be queued currently and then be transmitted , because of in the CCH interval.
146  // the third packet will be dropped because of no channel access for SCH2.
147  //Simulator::Schedule (Seconds (1.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH2, 3);
148 
149  // release SCH access
150  //Simulator::Schedule (Seconds (2.0), &WaveNetDevice::StopSch, sender, SCH1);
151  //Simulator::Schedule (Seconds (2.0), &WaveNetDevice::StopSch, receiver, SCH1);
152  // the fourth packet will be queued and be transmitted because of default CCH access assigned automatically.
153  //Simulator::Schedule (Seconds (3.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, CCH, 4);
154  // the fifth packet will be dropped because of no SCH1 access assigned
155  //Simulator::Schedule (Seconds (3.0), &WaveNetDeviceExample::SendOneWsmpPacket, this, SCH1, 5);
156 
157  Simulator::Stop (Seconds (5.0));
158  Simulator::Run ();
160 }
161 
162 void
163 WaveNetDeviceExample::SendIpPacket (uint32_t seq, bool ipv6)
164 {
165  Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice> (devices.Get (0));
166  Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice> (devices.Get (1));
167  const Address dest = receiver->GetAddress ();
168  // send IPv4 packet or IPv6 packet
169  const static uint16_t IPv4_PROT_NUMBER = 0x0800;
170  const static uint16_t IPv6_PROT_NUMBER = 0x86DD;
171  uint16_t protocol = ipv6 ? IPv6_PROT_NUMBER : IPv4_PROT_NUMBER;
172  Ptr<Packet> p = Create<Packet> (100);
173  SeqTsHeader seqTs;
174  seqTs.SetSeq (seq);
175  p->AddHeader (seqTs);
176  sender->Send (p, dest, protocol);
177 }
178 
179 void
181 {
182  CreateWaveNodes ();
183  Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice> (devices.Get (0));
184  Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice> (devices.Get (1));
185 
186  // Alternating access without immediate channel switch
187  const SchInfo schInfo = SchInfo (SCH1, false, EXTENDED_ALTERNATING);
188  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch, sender, schInfo);
189  // An important point is that the receiver should also be assigned channel
190  // access for the same channel to receive packets.
191  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch, receiver, schInfo);
192 
193  // both IPv4 and IPv6 packets below will not be inserted to internal queue because of no tx profile registered
196  //register txprofile
197  // IP packets will automatically be sent with txprofile parameter
198  const TxProfile txProfile = TxProfile (SCH1);
199  Simulator::Schedule (Seconds (2.0), &WaveNetDevice::RegisterTxProfile, sender, txProfile);
200  // both IPv4 and IPv6 packet are transmitted successfully
203  // unregister TxProfile or release channel access
207  // these packets will be dropped again because of no channel access assigned and no tx profile registered
210 
211  Simulator::Stop (Seconds (6.0));
212  Simulator::Run ();
214 }
215 
216 bool
218 {
219  std::cout << "receive a VSA management frame: recvTime = " << Now ().GetSeconds () << "s." << std::endl;
220  return true;
221 }
222 
223 void
225 {
226  CreateWaveNodes ();
227  Ptr<WaveNetDevice> sender = DynamicCast<WaveNetDevice> (devices.Get (0));
228  Ptr<WaveNetDevice> receiver = DynamicCast<WaveNetDevice> (devices.Get (1));
229 
230 // Alternating access without immediate channel switch for sender and receiver
231  const SchInfo schInfo = SchInfo (SCH1, false, EXTENDED_ALTERNATING);
232  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch, sender, schInfo);
233  Simulator::Schedule (Seconds (0.0), &WaveNetDevice::StartSch, receiver, schInfo);
234 
235 // the peer address of VSA is broadcast address, and the repeat rate
236 // of VsaInfo is 100 per 5s, the VSA frame will be sent repeatedly.
237  Ptr<Packet> wsaPacket = Create<Packet> (100);
239  const VsaInfo vsaInfo = VsaInfo (dest, OrganizationIdentifier (), 0, wsaPacket, SCH1, 100, VSA_TRANSMIT_IN_BOTHI);
240  Simulator::Schedule (Seconds (1.0), &WaveNetDevice::StartVsa, sender, vsaInfo);
242 
243 // release alternating access
246 
247 // these WSA packets cannot be transmitted because of no channel access assigned
248  Simulator::Schedule (Seconds (5.0), &WaveNetDevice::StartVsa, sender, vsaInfo);
250 
251  Simulator::Stop (Seconds (6.0));
252  Simulator::Run ();
254 }
255 
256 int
257 main (int argc, char *argv[])
258 {
259  WaveNetDeviceExample example;
260  std::cout << "run WAVE WSMP routing service case:" << std::endl;
261  example.SendWsmpExample ();
262  std::cout << "run WAVE IP routing service case:" << std::endl;
263  //example.SendIpExample ();
264  std::cout << "run WAVE WSA routing service case:" << std::endl;
265  //example.SendWsaExample ();
266  return 0;
267 }
bool StopVsa(uint32_t channelNumber)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t GetSeq(void) const
tuple devices
Definition: first.py:32
bool StartSch(const SchInfo &schInfo)
void SetWaveVsaCallback(WaveVsaCallback vsaCallback)
Ptr< YansWifiChannel > Create(void) const
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
bool ReceiveVsa(Ptr< const Packet >, const Address &, uint32_t, uint32_t)
This simulation is to show the routing service of WaveNetDevice described in IEEE 09...
void SendOneWsmpPacket(uint32_t channel, uint32_t seq)
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
bool SendX(Ptr< Packet > packet, const Address &dest, uint32_t protocol, const TxInfo &txInfo)
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
static WaveHelper Default(void)
Definition: wave-helper.cc:390
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wave-helper.cc:482
#define CCH
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
a polymophic address class
Definition: address.h:90
IEEE 802.11 Wireless LAN headers on packets.
tuple nodes
Definition: first.py:25
void SetChannel(Ptr< YansWifiChannel > channel)
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:327
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
#define EXTENDED_ALTERNATING
the organization identifier is a public organizationally unique identifier assigned by the IEEE...
bool DeleteTxProfile(uint32_t channelNumber)
helps to create WaveNetDevice objects
Definition: wave-helper.h:110
holds a vector of ns3::NetDevice pointers
void SendIpPacket(uint32_t seq, bool ipv6)
static Mac48Address GetBroadcast(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
bool StopSch(uint32_t channelNumber)
Introspection did not find any typical Config paths.
Definition: seq-ts-header.h:36
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:277
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
an EUI-48 address
Definition: mac48-address.h:43
manage and create wifi channel objects for the yans model.
To trace WaveNetDevice, we have to overwrite the trace functions of class YansWifiPhyHelper.
Definition: wave-helper.h:39
NetDeviceContainer devices
Helper class used to assign positions and mobility models to nodes.
bool StartVsa(const VsaInfo &vsaInfo)
Time GetTs(void) const
static YansWavePhyHelper Default(void)
Create a phy helper in a default working state.
Definition: wave-helper.cc:244
static QosWaveMacHelper Default(void)
Create a mac helper in a default working state.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
void SetSeq(uint32_t seq)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
void Add(Vector v)
Add a position to the list of positions.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:330
tuple address
Definition: first.py:37
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
bool Receive(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
bool RegisterTxProfile(const TxProfile &txprofile)
#define SCH1
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253