A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-ap.py
Go to the documentation of this file.
1# -*- Mode: Python; -*-
2# /*
3# * Copyright (c) 2005,2006,2007 INRIA
4# * Copyright (c) 2009 INESC Porto
5# *
6# * SPDX-License-Identifier: GPL-2.0-only
7# *
8# * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
9# * Gustavo Carneiro <gjc@inescporto.pt>
10# */
11
12import sys
13
14try:
15 from ns import ns
16except ModuleNotFoundError:
17 raise SystemExit(
18 "Error: ns3 Python module not found;"
19 " Python bindings may not be enabled"
20 " or your PYTHONPATH might not be properly configured"
21 )
22
23# void
24# DevTxTrace (std::string context, Ptr<const Packet> p, Mac48Address address)
25# {
26# std::cout << " TX to=" << address << " p: " << *p << std::endl;
27# }
28# void
29# DevRxTrace(std::string context, Ptr<const Packet> p, Mac48Address address)
30# {
31# std::cout << " RX from=" << address << " p: " << *p << std::endl;
32# }
33# void
34# PhyRxOkTrace(std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
35# {
36# std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
37# }
38# void
39# PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
40# {
41# std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
42# }
43# void
44# PhyTxTrace(std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
45# {
46# std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
47# }
48# void
49# PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
50# {
51# std::cout << " state=";
52# switch(state) {
53# case WifiPhy::TX:
54# std::cout << "tx ";
55# break;
56# case WifiPhy::SYNC:
57# std::cout << "sync ";
58# break;
59# case WifiPhy::CCA_BUSY:
60# std::cout << "cca-busy";
61# break;
62# case WifiPhy::IDLE:
63# std::cout << "idle ";
64# break;
65# }
66# std::cout << " start="<<start<<" duration="<<duration<<std::endl;
67# }
68
69ns.cppyy.cppdef(
70 """
71 using namespace ns3;
72 void AdvancePosition(Ptr<Node> node){
73 Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
74 Vector pos = mob->GetPosition();
75 pos.x += 5.0;
76 if (pos.x >= 210.0)
77 return;
78 mob->SetPosition(pos);
79 Simulator::Schedule(Seconds(1.0), AdvancePosition, node);
80 }"""
81)
82
83
84def main(argv):
85 ns.CommandLine().Parse(argv)
86
87 ns.Packet.EnablePrinting()
88
89 wifi = ns.WifiHelper()
90 mobility = ns.MobilityHelper()
91 stas = ns.NodeContainer()
92 ap = ns.NodeContainer()
93 # NetDeviceContainer staDevs;
94 packetSocket = ns.PacketSocketHelper()
95
96 stas.Create(2)
97 ap.Create(1)
98
99 # give packet socket powers to nodes.
100 packetSocket.Install(stas)
101 packetSocket.Install(ap)
102
103 wifiPhy = ns.YansWifiPhyHelper()
104 wifiChannel = ns.YansWifiChannelHelper.Default()
105 wifiPhy.SetChannel(wifiChannel.Create())
106
107 ssid = ns.Ssid("wifi-default")
108 wifiMac = ns.WifiMacHelper()
109
110 # setup stas.
111 wifiMac.SetType(
112 "ns3::StaWifiMac",
113 "ActiveProbing",
114 ns.BooleanValue(True),
115 "Ssid",
116 ns.SsidValue(ssid),
117 )
118 staDevs = wifi.Install(wifiPhy, wifiMac, stas)
119 # setup ap.
120 wifiMac.SetType("ns3::ApWifiMac", "Ssid", ns.SsidValue(ssid))
121 wifi.Install(wifiPhy, wifiMac, ap)
122
123 # mobility.
124 mobility.Install(stas)
125 mobility.Install(ap)
126
127 ns.Simulator.Schedule(ns.Seconds(1.0), ns.cppyy.gbl.AdvancePosition, ap.Get(0))
128
129 socket = ns.PacketSocketAddress()
130 socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
131 socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
132 socket.SetProtocol(1)
133
134 onoff = ns.OnOffHelper("ns3::PacketSocketFactory", socket.ConvertTo())
135 onoff.SetConstantRate(ns.DataRate("500kb/s"))
136
137 apps = onoff.Install(ns.NodeContainer(stas.Get(0)))
138 apps.Start(ns.Seconds(0.5))
139 apps.Stop(ns.Seconds(43.0))
140
141 ns.Simulator.Stop(ns.Seconds(44.0))
142
143 # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
144 # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
145 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
146 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
147 # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
148 # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
149
150 ns.Simulator.Run()
151 ns.Simulator.Destroy()
152
153 return 0
154
155
156if __name__ == "__main__":
157 sys.exit(main(sys.argv))