Files
unison/examples/wireless/wifi-ap.cc

232 lines
5.5 KiB
C++
Raw Normal View History

2007-10-19 12:10:01 +02:00
/*
* Copyright (c) 2005,2006,2007 INRIA
*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
2007-10-19 12:10:01 +02:00
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2022-10-07 20:08:35 +00:00
#include "ns3/athstats-helper.h"
#include "ns3/boolean.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-model.h"
2022-10-07 20:08:35 +00:00
#include "ns3/on-off-helper.h"
#include "ns3/packet-socket-address.h"
2022-10-07 20:08:35 +00:00
#include "ns3/packet-socket-helper.h"
#include "ns3/ssid.h"
#include "ns3/string.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/yans-wifi-helper.h"
2007-10-19 12:10:01 +02:00
using namespace ns3;
2022-08-28 13:24:25 -05:00
/// True for verbose output.
static bool g_verbose = true;
2022-08-28 13:24:25 -05:00
/**
* MAC-level TX trace.
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param p The packet.
2022-08-28 13:24:25 -05:00
*/
2007-11-08 15:23:06 +01:00
void
2022-10-07 20:08:35 +00:00
DevTxTrace(std::string context, Ptr<const Packet> p)
2007-10-31 11:45:04 +01:00
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << " TX p: " << *p << std::endl;
}
2007-10-31 11:45:04 +01:00
}
2022-08-28 13:24:25 -05:00
/**
* MAC-level RX trace.
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param p The packet.
2022-08-28 13:24:25 -05:00
*/
2007-11-08 15:23:06 +01:00
void
2022-10-07 20:08:35 +00:00
DevRxTrace(std::string context, Ptr<const Packet> p)
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << " RX p: " << *p << std::endl;
}
}
2022-08-28 13:24:25 -05:00
/**
* PHY-level RX OK trace
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param packet The packet.
* @param snr The SNR.
* @param mode The wifi mode.
* @param preamble The preamble.
2022-08-28 13:24:25 -05:00
*/
void
2022-10-07 20:08:35 +00:00
PhyRxOkTrace(std::string context,
Ptr<const Packet> packet,
double snr,
WifiMode mode,
WifiPreamble preamble)
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
}
}
2022-08-28 13:24:25 -05:00
/**
* PHY-level RX error trace
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param packet The packet.
* @param snr The SNR.
2022-08-28 13:24:25 -05:00
*/
void
2022-10-07 20:08:35 +00:00
PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
}
}
2022-08-28 13:24:25 -05:00
/**
* PHY-level TX trace.
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param packet The packet.
* @param mode The wifi mode.
* @param preamble The preamble.
* @param txPower The TX power.
2022-08-28 13:24:25 -05:00
*/
void
2022-10-07 20:08:35 +00:00
PhyTxTrace(std::string context,
Ptr<const Packet> packet,
WifiMode mode,
WifiPreamble preamble,
uint8_t txPower)
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
}
}
2022-08-28 13:24:25 -05:00
/**
* PHY state trace.
*
2024-11-08 18:05:46 +00:00
* @param context The context.
* @param start Start time of the state.
* @param duration Duration of the state.
* @param state The state.
2022-08-28 13:24:25 -05:00
*/
void
2022-10-07 20:08:35 +00:00
PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
2007-10-31 12:27:20 +01:00
{
2022-10-07 20:08:35 +00:00
if (g_verbose)
{
2022-10-07 20:08:35 +00:00
std::cout << " state=" << state << " start=" << start << " duration=" << duration
<< std::endl;
}
2007-10-31 12:27:20 +01:00
}
2007-10-31 11:45:04 +01:00
2022-08-28 13:24:25 -05:00
/**
* Move a node position by 5m on the x axis every second, up to 210m.
*
2024-11-08 18:05:46 +00:00
* @param node The node.
2022-08-28 13:24:25 -05:00
*/
2017-02-06 20:31:02 +01:00
static void
2022-10-07 20:08:35 +00:00
AdvancePosition(Ptr<Node> node)
2007-10-19 12:10:01 +02:00
{
2022-10-07 20:08:35 +00:00
Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
Vector pos = mobility->GetPosition();
pos.x += 5.0;
if (pos.x >= 210.0)
2007-11-21 11:12:02 +01:00
{
2022-10-07 20:08:35 +00:00
return;
2007-11-21 11:12:02 +01:00
}
2022-10-07 20:08:35 +00:00
mobility->SetPosition(pos);
Simulator::Schedule(Seconds(1), &AdvancePosition, node);
2007-10-19 12:10:01 +02:00
}
2022-10-07 20:08:35 +00:00
int
main(int argc, char* argv[])
2007-10-19 12:10:01 +02:00
{
2022-10-07 20:08:35 +00:00
CommandLine cmd(__FILE__);
cmd.AddValue("verbose", "Print trace information if true", g_verbose);
cmd.Parse(argc, argv);
Packet::EnablePrinting();
WifiHelper wifi;
MobilityHelper mobility;
NodeContainer stas;
NodeContainer ap;
NetDeviceContainer staDevs;
PacketSocketHelper packetSocket;
stas.Create(2);
ap.Create(1);
// give packet socket powers to nodes.
packetSocket.Install(stas);
packetSocket.Install(ap);
WifiMacHelper wifiMac;
YansWifiPhyHelper wifiPhy;
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
wifiPhy.SetChannel(wifiChannel.Create());
Ssid ssid = Ssid("wifi-default");
// setup stas.
wifiMac.SetType("ns3::StaWifiMac",
"ActiveProbing",
BooleanValue(true),
"Ssid",
SsidValue(ssid));
staDevs = wifi.Install(wifiPhy, wifiMac, stas);
// setup ap.
wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
wifi.Install(wifiPhy, wifiMac, ap);
// mobility.
mobility.Install(stas);
mobility.Install(ap);
Simulator::Schedule(Seconds(1), &AdvancePosition, ap.Get(0));
2022-10-07 20:08:35 +00:00
PacketSocketAddress socket;
socket.SetSingleDevice(staDevs.Get(0)->GetIfIndex());
socket.SetPhysicalAddress(staDevs.Get(1)->GetAddress());
socket.SetProtocol(1);
OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
onoff.SetConstantRate(DataRate("500kb/s"));
ApplicationContainer apps = onoff.Install(stas.Get(0));
apps.Start(Seconds(0.5));
apps.Stop(Seconds(43));
2022-10-07 20:08:35 +00:00
Simulator::Stop(Seconds(44));
2022-10-07 20:08:35 +00:00
Config::Connect("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback(&DevTxTrace));
Config::Connect("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback(&DevRxTrace));
Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback(&PhyRxOkTrace));
Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback(&PhyRxErrorTrace));
Config::Connect("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback(&PhyTxTrace));
Config::Connect("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace));
AthstatsHelper athstats;
athstats.EnableAthstats("athstats-sta", stas);
athstats.EnableAthstats("athstats-ap", ap);
Simulator::Run();
Simulator::Destroy();
return 0;
2007-10-19 12:10:01 +02:00
}