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 using namespace ns3;
71 void AdvancePosition(Ptr<Node> node){
72 Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
73 Vector pos = mob->GetPosition();
74 pos.x += 5.0;
75 if (pos.x >= 210.0)
76 return;
77 mob->SetPosition(pos);
78 Simulator::Schedule(Seconds(1), AdvancePosition, node);
79 }""")
80
81
82def main(argv):
83 ns.CommandLine().Parse(argv)
84
85 ns.Packet.EnablePrinting()
86
87 wifi = ns.WifiHelper()
88 mobility = ns.MobilityHelper()
89 stas = ns.NodeContainer()
90 ap = ns.NodeContainer()
91 # NetDeviceContainer staDevs;
92 packetSocket = ns.PacketSocketHelper()
93
94 stas.Create(2)
95 ap.Create(1)
96
97 # give packet socket powers to nodes.
98 packetSocket.Install(stas)
99 packetSocket.Install(ap)
100
101 wifiPhy = ns.YansWifiPhyHelper()
102 wifiChannel = ns.YansWifiChannelHelper.Default()
103 wifiPhy.SetChannel(wifiChannel.Create())
104
105 ssid = ns.Ssid("wifi-default")
106 wifiMac = ns.WifiMacHelper()
107
108 # setup stas.
109 wifiMac.SetType(
110 "ns3::StaWifiMac",
111 "ActiveProbing",
112 ns.BooleanValue(True),
113 "Ssid",
114 ns.SsidValue(ssid),
115 )
116 staDevs = wifi.Install(wifiPhy, wifiMac, stas)
117 # setup ap.
118 wifiMac.SetType("ns3::ApWifiMac", "Ssid", ns.SsidValue(ssid))
119 wifi.Install(wifiPhy, wifiMac, ap)
120
121 # mobility.
122 mobility.Install(stas)
123 mobility.Install(ap)
124
125 ns.Simulator.Schedule(ns.Seconds(1), ns.cppyy.gbl.AdvancePosition, ap.Get(0))
126
127 socket = ns.PacketSocketAddress()
128 socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
129 socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
130 socket.SetProtocol(1)
131
132 onoff = ns.OnOffHelper("ns3::PacketSocketFactory", socket.ConvertTo())
133 onoff.SetConstantRate(ns.DataRate("500kb/s"))
134
135 apps = onoff.Install(ns.NodeContainer(stas.Get(0)))
136 apps.Start(ns.Seconds(0.5))
137 apps.Stop(ns.Seconds(43))
138
139 ns.Simulator.Stop(ns.Seconds(44))
140
141 # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
142 # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
143 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
144 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
145 # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
146 # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
147
148 ns.Simulator.Run()
149 ns.Simulator.Destroy()
150
151 return 0
152
153
154if __name__ == "__main__":
155 sys.exit(main(sys.argv))