#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/applications-module.h"
#include "ns3/tap-bridge-module.h"
#include "ns3/log.h"
#include "iostream"
using namespace ns3;
static int currentChannel = 1;
NS_LOG_COMPONENT_DEFINE("FrequencyHoppingExample");
void ChangeFrequency(Ptr<NetDevice> device, double hopInterval) {
Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice>(device);
if (wifiDevice == nullptr) {
NS_LOG_UNCOND("wifiDevice is null for device ID: " << device->GetNode()->GetId());
return;
}
Ptr<WifiPhy> wifiPhy = wifiDevice->GetPhy();
Ptr<YansWifiPhy> yansWifiPhy = DynamicCast<YansWifiPhy>(wifiPhy);
// 更新当前信道
currentChannel = (currentChannel % 14) + 1; // 在1到14之间循环
// 更改信道
YansWifiChannelHelper wifiChannel;
yansWifiPhy->SetChannel(wifiChannel.Create());
//wifiPhy->SetAttribute("ChannelSettings", StringValue("{0, 0, BAND_2_4GHZ, 0}"));
// Log the change
NS_LOG_UNCOND("Time: " << Simulator::Now().GetSeconds()
<< "s, Node " << device->GetNode()->GetId()
<< " changed to channel " << currentChannel);
// Schedule next change
Simulator::Schedule(Seconds(hopInterval), &ChangeFrequency, device, hopInterval);
}
// void ChangeFrequency(Ptr<NetDevice> device, double hopInterval)
// {
// Ptr<WifiNetDevice> wifiDevice = DynamicCast<WifiNetDevice>(device);
// Ptr<WifiPhy> wifiPhy = wifiDevice->GetPhy();
// currentChannel = (currentChannel % 11) + 1; // Assuming 11 channels
// double frequency = 2407 + (currentChannel - 1) * 5;
// wifiPhy->SetOperatingChannel({currentChannel,0,frequency,0});
// LogComponentEnable("wifiPhy", LOG_LEVEL_INFO);
// NS_LOG_UNCOND("Node" << device->GetNode()->GetId() << " changed to channel " << currentChannel);
// // Schedule the next frequency change
// Simulator::Schedule(Seconds(hopInterval), &ChangeFrequency, device, hopInterval);
// }
int main(int argc, char *argv[])
{
uint32_t numNodes = 2;
double simTime = 10.0;
double hopInterval = 1.0; // Interval in seconds for frequency hopping
CommandLine cmd;
cmd.AddValue("numNodes", "Number of nodes", numNodes);
cmd.AddValue("simTime", "Simulation time (seconds)", simTime);
cmd.AddValue("hopInterval", "Frequency hopping interval (seconds)", hopInterval);
cmd.Parse(argc, argv);
LogComponentEnable("WifiPhy", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create(numNodes);
YansWifiChannelHelper wifiChannel;
YansWifiPhyHelper wifiPhy;
wifiPhy.SetChannel(wifiChannel.Create());
WifiHelper wifi;
wifi.SetStandard(WIFI_STANDARD_80211n);
WifiMacHelper wifiMac;
wifiMac.SetType("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
//配置IP
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
// Install applications
//发送端
uint16_t port = 9;
OnOffHelper onoff("ns3::UdpSocketFactory", Address(InetSocketAddress(interfaces.GetAddress(numNodes-1), port)));
onoff.SetConstantRate(DataRate("500kbps"));
ApplicationContainer apps = onoff.Install(nodes.Get(0));
apps.Start(Seconds(1.0));
apps.Stop(Seconds(simTime));
//接收端
PacketSinkHelper sink("ns3::UdpSocketFactory", Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
apps = sink.Install(nodes.Get(numNodes-1));
apps.Start(Seconds(0.0));
apps.Stop(Seconds(simTime));
LogComponentEnable("FrequencyHoppingExample", LOG_LEVEL_ALL);
// Implement frequency hopping
for (uint32_t i = 0; i < numNodes; ++i)
{
Simulator::ScheduleWithContext(nodes.Get(i)->GetId(), Seconds(hopInterval), &ChangeFrequency, devices.Get(i), hopInterval);
}
Simulator::Stop(Seconds(simTime));
Simulator::Run();
Simulator::Destroy();
return 0;
}