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# * This program is free software; you can redistribute it and/or modify
7# * it under the terms of the GNU General Public License version 2 as
8# * published by the Free Software Foundation;
9# *
10# * This program is distributed in the hope that it will be useful,
11# * but WITHOUT ANY WARRANTY; without even the implied warranty of
12# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# * GNU General Public License for more details.
14# *
15# * You should have received a copy of the GNU General Public License
16# * along with this program; if not, write to the Free Software
17# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18# *
19# * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20# * Gustavo Carneiro <gjc@inescporto.pt>
21# */
22
23import sys
24
25try:
26 from ns import ns
27except ModuleNotFoundError:
28 raise SystemExit(
29 "Error: ns3 Python module not found;"
30 " Python bindings may not be enabled"
31 " or your PYTHONPATH might not be properly configured"
32 )
33
34# void
35# DevTxTrace (std::string context, Ptr<const Packet> p, Mac48Address address)
36# {
37# std::cout << " TX to=" << address << " p: " << *p << std::endl;
38# }
39# void
40# DevRxTrace(std::string context, Ptr<const Packet> p, Mac48Address address)
41# {
42# std::cout << " RX from=" << address << " p: " << *p << std::endl;
43# }
44# void
45# PhyRxOkTrace(std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
46# {
47# std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
48# }
49# void
50# PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
51# {
52# std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
53# }
54# void
55# PhyTxTrace(std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
56# {
57# std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
58# }
59# void
60# PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
61# {
62# std::cout << " state=";
63# switch(state) {
64# case WifiPhy::TX:
65# std::cout << "tx ";
66# break;
67# case WifiPhy::SYNC:
68# std::cout << "sync ";
69# break;
70# case WifiPhy::CCA_BUSY:
71# std::cout << "cca-busy";
72# break;
73# case WifiPhy::IDLE:
74# std::cout << "idle ";
75# break;
76# }
77# std::cout << " start="<<start<<" duration="<<duration<<std::endl;
78# }
79
80ns.cppyy.cppdef(
81 """
82 using namespace ns3;
83 void AdvancePosition(Ptr<Node> node){
84 Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
85 Vector pos = mob->GetPosition();
86 pos.x += 5.0;
87 if (pos.x >= 210.0)
88 return;
89 mob->SetPosition(pos);
90 Simulator::Schedule(Seconds(1.0), AdvancePosition, node);
91 }"""
92)
93
94
95def main(argv):
96 ns.core.CommandLine().Parse(argv)
97
98 ns.network.Packet.EnablePrinting()
99
100 wifi = ns.wifi.WifiHelper()
101 mobility = ns.mobility.MobilityHelper()
102 stas = ns.network.NodeContainer()
103 ap = ns.network.NodeContainer()
104 # NetDeviceContainer staDevs;
105 packetSocket = ns.network.PacketSocketHelper()
106
107 stas.Create(2)
108 ap.Create(1)
109
110 # give packet socket powers to nodes.
111 packetSocket.Install(stas)
112 packetSocket.Install(ap)
113
114 wifiPhy = ns.wifi.YansWifiPhyHelper()
115 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
116 wifiPhy.SetChannel(wifiChannel.Create())
117
118 ssid = ns.wifi.Ssid("wifi-default")
119 wifiMac = ns.wifi.WifiMacHelper()
120
121 # setup stas.
122 wifiMac.SetType(
123 "ns3::StaWifiMac",
124 "ActiveProbing",
125 ns.core.BooleanValue(True),
126 "Ssid",
127 ns.wifi.SsidValue(ssid),
128 )
129 staDevs = wifi.Install(wifiPhy, wifiMac, stas)
130 # setup ap.
131 wifiMac.SetType("ns3::ApWifiMac", "Ssid", ns.wifi.SsidValue(ssid))
132 wifi.Install(wifiPhy, wifiMac, ap)
133
134 # mobility.
135 mobility.Install(stas)
136 mobility.Install(ap)
137
138 ns.core.Simulator.Schedule(ns.core.Seconds(1.0), ns.cppyy.gbl.AdvancePosition, ap.Get(0))
139
140 socket = ns.network.PacketSocketAddress()
141 socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
142 socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
143 socket.SetProtocol(1)
144
145 onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", socket.ConvertTo())
146 onoff.SetConstantRate(ns.network.DataRate("500kb/s"))
147
148 apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
149 apps.Start(ns.core.Seconds(0.5))
150 apps.Stop(ns.core.Seconds(43.0))
151
152 ns.core.Simulator.Stop(ns.core.Seconds(44.0))
153
154 # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
155 # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
156 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
157 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
158 # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
159 # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
160
161 ns.core.Simulator.Run()
162 ns.core.Simulator.Destroy()
163
164 return 0
165
166
167if __name__ == "__main__":
168 sys.exit(main(sys.argv))
static void AdvancePosition(Ptr< Node > node)
Move a node position by 5m on the x axis every second, up to 210m.
Definition: wifi-ap.cc:153