File: wifi-ap.cc

package info (click to toggle)
ns3 3.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 106,848 kB
  • sloc: cpp: 633,577; python: 15,491; ansic: 6,773; makefile: 1,959; sh: 1,021; pascal: 632; javascript: 167; perl: 102
file content (231 lines) | stat: -rw-r--r-- 5,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2005,2006,2007 INRIA
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */

#include "ns3/athstats-helper.h"
#include "ns3/boolean.h"
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-model.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-socket-address.h"
#include "ns3/packet-socket-helper.h"
#include "ns3/ssid.h"
#include "ns3/string.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/yans-wifi-helper.h"

using namespace ns3;

/// True for verbose output.
static bool g_verbose = true;

/**
 * MAC-level TX trace.
 *
 * @param context The context.
 * @param p The packet.
 */
void
DevTxTrace(std::string context, Ptr<const Packet> p)
{
    if (g_verbose)
    {
        std::cout << " TX p: " << *p << std::endl;
    }
}

/**
 * MAC-level RX trace.
 *
 * @param context The context.
 * @param p The packet.
 */
void
DevRxTrace(std::string context, Ptr<const Packet> p)
{
    if (g_verbose)
    {
        std::cout << " RX p: " << *p << std::endl;
    }
}

/**
 * PHY-level RX OK trace
 *
 * @param context The context.
 * @param packet The packet.
 * @param snr The SNR.
 * @param mode The wifi mode.
 * @param preamble The preamble.
 */
void
PhyRxOkTrace(std::string context,
             Ptr<const Packet> packet,
             double snr,
             WifiMode mode,
             WifiPreamble preamble)
{
    if (g_verbose)
    {
        std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
    }
}

/**
 * PHY-level RX error trace
 *
 * @param context The context.
 * @param packet The packet.
 * @param snr The SNR.
 */
void
PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
{
    if (g_verbose)
    {
        std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
    }
}

/**
 * PHY-level TX trace.
 *
 * @param context The context.
 * @param packet The packet.
 * @param mode The wifi mode.
 * @param preamble The preamble.
 * @param txPower The TX power.
 */
void
PhyTxTrace(std::string context,
           Ptr<const Packet> packet,
           WifiMode mode,
           WifiPreamble preamble,
           uint8_t txPower)
{
    if (g_verbose)
    {
        std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
    }
}

/**
 * PHY state trace.
 *
 * @param context The context.
 * @param start Start time of the state.
 * @param duration Duration of the state.
 * @param state The state.
 */
void
PhyStateTrace(std::string context, Time start, Time duration, WifiPhyState state)
{
    if (g_verbose)
    {
        std::cout << " state=" << state << " start=" << start << " duration=" << duration
                  << std::endl;
    }
}

/**
 * Move a node position by 5m on the x axis every second, up to 210m.
 *
 * @param node The node.
 */
static void
AdvancePosition(Ptr<Node> node)
{
    Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
    Vector pos = mobility->GetPosition();
    pos.x += 5.0;
    if (pos.x >= 210.0)
    {
        return;
    }
    mobility->SetPosition(pos);

    Simulator::Schedule(Seconds(1), &AdvancePosition, node);
}

int
main(int argc, char* argv[])
{
    CommandLine cmd(__FILE__);
    cmd.AddValue("verbose", "Print trace information if true", g_verbose);
    cmd.Parse(argc, argv);

    Packet::EnablePrinting();

    WifiHelper wifi;
    MobilityHelper mobility;
    NodeContainer stas;
    NodeContainer ap;
    NetDeviceContainer staDevs;
    PacketSocketHelper packetSocket;

    stas.Create(2);
    ap.Create(1);

    // give packet socket powers to nodes.
    packetSocket.Install(stas);
    packetSocket.Install(ap);

    WifiMacHelper wifiMac;
    YansWifiPhyHelper wifiPhy;
    YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
    wifiPhy.SetChannel(wifiChannel.Create());
    Ssid ssid = Ssid("wifi-default");
    // setup stas.
    wifiMac.SetType("ns3::StaWifiMac",
                    "ActiveProbing",
                    BooleanValue(true),
                    "Ssid",
                    SsidValue(ssid));
    staDevs = wifi.Install(wifiPhy, wifiMac, stas);
    // setup ap.
    wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
    wifi.Install(wifiPhy, wifiMac, ap);

    // mobility.
    mobility.Install(stas);
    mobility.Install(ap);

    Simulator::Schedule(Seconds(1), &AdvancePosition, ap.Get(0));

    PacketSocketAddress socket;
    socket.SetSingleDevice(staDevs.Get(0)->GetIfIndex());
    socket.SetPhysicalAddress(staDevs.Get(1)->GetAddress());
    socket.SetProtocol(1);

    OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
    onoff.SetConstantRate(DataRate("500kb/s"));

    ApplicationContainer apps = onoff.Install(stas.Get(0));
    apps.Start(Seconds(0.5));
    apps.Stop(Seconds(43));

    Simulator::Stop(Seconds(44));

    Config::Connect("/NodeList/*/DeviceList/*/Mac/MacTx", MakeCallback(&DevTxTrace));
    Config::Connect("/NodeList/*/DeviceList/*/Mac/MacRx", MakeCallback(&DevRxTrace));
    Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxOk", MakeCallback(&PhyRxOkTrace));
    Config::Connect("/NodeList/*/DeviceList/*/Phy/State/RxError", MakeCallback(&PhyRxErrorTrace));
    Config::Connect("/NodeList/*/DeviceList/*/Phy/State/Tx", MakeCallback(&PhyTxTrace));
    Config::Connect("/NodeList/*/DeviceList/*/Phy/State/State", MakeCallback(&PhyStateTrace));

    AthstatsHelper athstats;
    athstats.EnableAthstats("athstats-sta", stas);
    athstats.EnableAthstats("athstats-ap", ap);

    Simulator::Run();

    Simulator::Destroy();

    return 0;
}