A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
adhoc-aloha-ideal-phy.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
9#include <ns3/adhoc-aloha-noack-ideal-phy-helper.h>
10#include <ns3/applications-module.h>
11#include <ns3/core-module.h>
12#include <ns3/friis-spectrum-propagation-loss.h>
13#include <ns3/ism-spectrum-value-helper.h>
14#include <ns3/log.h>
15#include <ns3/mobility-module.h>
16#include <ns3/network-module.h>
17#include <ns3/propagation-delay-model.h>
18#include <ns3/single-model-spectrum-channel.h>
19#include <ns3/spectrum-analyzer.h>
20#include <ns3/spectrum-helper.h>
21#include <ns3/spectrum-model-300kHz-300GHz-log.h>
22#include <ns3/spectrum-model-ism2400MHz-res1MHz.h>
23#include <ns3/waveform-generator.h>
24
25#include <iostream>
26#include <string>
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("TestAdhocOfdmAloha");
31
32/// True for verbose output.
33static bool g_verbose = false;
34
35/**
36 * PHY start TX trace.
37 *
38 * \param context The context.
39 * \param p The packet.
40 */
41void
42PhyTxStartTrace(std::string context, Ptr<const Packet> p)
43{
44 if (g_verbose)
45 {
46 std::cout << context << " PHY TX START p: " << p << std::endl;
47 }
48}
49
50/**
51 * PHY end TX trace.
52 *
53 * \param context The context.
54 * \param p The packet.
55 */
56void
57PhyTxEndTrace(std::string context, Ptr<const Packet> p)
58{
59 if (g_verbose)
60 {
61 std::cout << context << " PHY TX END p: " << p << std::endl;
62 }
63}
64
65/**
66 * PHY start RX trace.
67 *
68 * \param context The context.
69 * \param p The packet.
70 */
71void
72PhyRxStartTrace(std::string context, Ptr<const Packet> p)
73{
74 if (g_verbose)
75 {
76 std::cout << context << " PHY RX START p:" << p << std::endl;
77 }
78}
79
80/**
81 * PHY end OK RX trace.
82 *
83 * \param context The context.
84 * \param p The packet.
85 */
86void
87PhyRxEndOkTrace(std::string context, Ptr<const Packet> p)
88{
89 if (g_verbose)
90 {
91 std::cout << context << " PHY RX END OK p:" << p << std::endl;
92 }
93}
94
95/**
96 * PHY end error RX trace.
97 *
98 * \param context The context.
99 * \param p The packet.
100 */
101void
103{
104 if (g_verbose)
105 {
106 std::cout << context << " PHY RX END ERROR p:" << p << std::endl;
107 }
108}
109
110/**
111 * Receive callback.
112 *
113 * \param socket The receiving socket.
114 */
115void
117{
118 Ptr<Packet> packet;
119 uint64_t bytes = 0;
120 while ((packet = socket->Recv()))
121 {
122 bytes += packet->GetSize();
123 }
124 if (g_verbose)
125 {
126 std::cout << "SOCKET received " << bytes << " bytes" << std::endl;
127 }
128}
129
130/**
131 * Create a socket and prepare it for packet reception.
132 *
133 * \param node The node.
134 * \return a new socket
135 */
138{
139 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
141 sink->Bind();
142 sink->SetRecvCallback(MakeCallback(&ReceivePacket));
143 return sink;
144}
145
146int
147main(int argc, char** argv)
148{
149 CommandLine cmd(__FILE__);
150 cmd.AddValue("verbose", "Print trace information if true", g_verbose);
151 cmd.Parse(argc, argv);
152
154 c.Create(2);
155
158 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
159 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
160 mobility.SetPositionAllocator(positionAlloc);
161 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
162
163 mobility.Install(c);
164
166 Ptr<SpectrumChannel> channel = channelHelper.Create();
167
169
170 double txPower = 0.1; // Watts
171 uint32_t channelNumber = 1;
172 Ptr<SpectrumValue> txPsd = sf.CreateTxPowerSpectralDensity(txPower, channelNumber);
173
174 // for the noise, we use the Power Spectral Density of thermal noise
175 // at room temperature. The value of the PSD will be constant over the band of interest.
176 const double k = 1.381e-23; // Boltzmann's constant
177 const double T = 290; // temperature in Kelvin
178 double noisePsdValue = k * T; // watts per hertz
179 Ptr<SpectrumValue> noisePsd = sf.CreateConstant(noisePsdValue);
180
182 deviceHelper.SetChannel(channel);
183 deviceHelper.SetTxPowerSpectralDensity(txPsd);
184 deviceHelper.SetNoisePowerSpectralDensity(noisePsd);
185 deviceHelper.SetPhyAttribute("Rate", DataRateValue(DataRate("1Mbps")));
186 NetDeviceContainer devices = deviceHelper.Install(c);
187
188 PacketSocketHelper packetSocket;
189 packetSocket.Install(c);
190
191 PacketSocketAddress socket;
192 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
193 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
194 socket.SetProtocol(1);
195
196 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
197 onoff.SetConstantRate(DataRate("0.5Mbps"));
198 onoff.SetAttribute("PacketSize", UintegerValue(125));
199
200 ApplicationContainer apps = onoff.Install(c.Get(0));
201 apps.Start(Seconds(0.1));
202 apps.Stop(Seconds(0.104));
203
204 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
205
207
208 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxStart", MakeCallback(&PhyTxStartTrace));
209 Config::Connect("/NodeList/*/DeviceList/*/Phy/TxEnd", MakeCallback(&PhyTxEndTrace));
210 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxStart", MakeCallback(&PhyRxStartTrace));
211 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndOk", MakeCallback(&PhyRxEndOkTrace));
212 Config::Connect("/NodeList/*/DeviceList/*/Phy/RxEndError", MakeCallback(&PhyRxEndErrorTrace));
213
215
217
218 return 0;
219}
void PhyTxEndTrace(std::string context, Ptr< const Packet > p)
PHY end TX trace.
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
void PhyRxEndErrorTrace(std::string context, Ptr< const Packet > p)
PHY end error RX trace.
void PhyRxStartTrace(std::string context, Ptr< const Packet > p)
PHY start RX trace.
void ReceivePacket(Ptr< Socket > socket)
Receive callback.
static bool g_verbose
True for verbose output.
void PhyRxEndOkTrace(std::string context, Ptr< const Packet > p)
PHY end OK RX trace.
void PhyTxStartTrace(std::string context, Ptr< const Packet > p)
PHY start TX trace.
a polymophic address class
Definition address.h:90
void SetPhyAttribute(std::string name, const AttributeValue &v)
void SetTxPowerSpectralDensity(Ptr< SpectrumValue > txPsd)
void SetNoisePowerSpectralDensity(Ptr< SpectrumValue > noisePsd)
void SetChannel(Ptr< SpectrumChannel > channel)
set the SpectrumChannel that will be used by SpectrumPhy instances created by this helper
NetDeviceContainer Install(NodeContainer c) const
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Setup a SpectrumChannel.
Ptr< SpectrumChannel > Create() const
static SpectrumChannelHelper Default()
Setup a default SpectrumChannel.
Implements Wifi SpectrumValue for the 2.4 GHz ISM band only, with a 5 MHz spectrum resolution.
virtual Ptr< SpectrumValue > CreateConstant(double psd)
Creates a SpectrumValue instance with a constant value for all frequencies.
virtual Ptr< SpectrumValue > CreateTxPowerSpectralDensity(double txPower, uint8_t channel)
Creates a SpectrumValue instance that represents the TX Power Spectral Density of a wifi device corre...
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Hold an unsigned integer type.
Definition uinteger.h:34
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
devices
Definition first.py:31
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:684
channel
Definition third.py:77
mobility
Definition third.py:92
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44