A Discrete-Event Network Simulator
API
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 
23 import sys
24 
25 from ns import ns
26 
27 # void
28 # DevTxTrace (std::string context, Ptr<const Packet> p, Mac48Address address)
29 # {
30 # std::cout << " TX to=" << address << " p: " << *p << std::endl;
31 # }
32 # void
33 # DevRxTrace(std::string context, Ptr<const Packet> p, Mac48Address address)
34 # {
35 # std::cout << " RX from=" << address << " p: " << *p << std::endl;
36 # }
37 # void
38 # PhyRxOkTrace(std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
39 # {
40 # std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
41 # }
42 # void
43 # PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
44 # {
45 # std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
46 # }
47 # void
48 # PhyTxTrace(std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
49 # {
50 # std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
51 # }
52 # void
53 # PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
54 # {
55 # std::cout << " state=";
56 # switch(state) {
57 # case WifiPhy::TX:
58 # std::cout << "tx ";
59 # break;
60 # case WifiPhy::SYNC:
61 # std::cout << "sync ";
62 # break;
63 # case WifiPhy::CCA_BUSY:
64 # std::cout << "cca-busy";
65 # break;
66 # case WifiPhy::IDLE:
67 # std::cout << "idle ";
68 # break;
69 # }
70 # std::cout << " start="<<start<<" duration="<<duration<<std::endl;
71 # }
72 
73 ns.cppyy.cppdef("""
74  using namespace ns3;
75  void AdvancePosition(Ptr<Node> node){
76  Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
77  Vector pos = mob->GetPosition();
78  pos.x += 5.0;
79  if (pos.x >= 210.0)
80  return;
81  mob->SetPosition(pos);
82  Simulator::Schedule(Seconds(1.0), AdvancePosition, node);
83  }""")
84 
85 def main(argv):
86  ns.core.CommandLine().Parse(argv)
87 
88  ns.network.Packet.EnablePrinting();
89 
90  wifi = ns.wifi.WifiHelper()
91  mobility = ns.mobility.MobilityHelper()
92  stas = ns.network.NodeContainer()
93  ap = ns.network.NodeContainer()
94  #NetDeviceContainer staDevs;
95  packetSocket = ns.network.PacketSocketHelper()
96 
97  stas.Create(2)
98  ap.Create(1)
99 
100  # give packet socket powers to nodes.
101  packetSocket.Install(stas)
102  packetSocket.Install(ap)
103 
104  wifiPhy = ns.wifi.YansWifiPhyHelper()
105  wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
106  wifiPhy.SetChannel(wifiChannel.Create())
107 
108  ssid = ns.wifi.Ssid("wifi-default")
109  wifiMac = ns.wifi.WifiMacHelper()
110 
111  # setup stas.
112  wifiMac.SetType("ns3::StaWifiMac",
113  "Ssid", ns.wifi.SsidValue(ssid))
114  staDevs = wifi.Install(wifiPhy, wifiMac, stas)
115  # setup ap.
116  wifiMac.SetType("ns3::ApWifiMac",
117  "Ssid", ns.wifi.SsidValue(ssid))
118  wifi.Install(wifiPhy, wifiMac, ap)
119 
120  # mobility.
121  mobility.Install(stas)
122  mobility.Install(ap)
123 
124  ns.core.Simulator.Schedule(ns.core.Seconds(1.0), ns.cppyy.gbl.AdvancePosition, ap.Get(0))
125 
126  socket = ns.network.PacketSocketAddress()
127  socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
128  socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
129  socket.SetProtocol(1)
130 
131  onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", socket.ConvertTo())
132  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
133 
134  apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
135  apps.Start(ns.core.Seconds(0.5))
136  apps.Stop(ns.core.Seconds(43.0))
137 
138  ns.core.Simulator.Stop(ns.core.Seconds(44.0))
139 
140  # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
141  # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
142  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
143  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
144  # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
145  # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
146 
147 
148  ns.core.Simulator.Run()
149  ns.core.Simulator.Destroy()
150 
151  return 0
152 
153 
154 if __name__ == '__main__':
155  sys.exit(main(sys.argv))
156