A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-data.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tom Henderson <thomas.r.henderson@boeing.com>
18 */
19
20/*
21 * Try to send data end-to-end through a LrWpanMac <-> LrWpanPhy <->
22 * SpectrumChannel <-> LrWpanPhy <-> LrWpanMac chain
23 *
24 * Trace Phy state changes, and Mac DataIndication and DataConfirm events
25 * to stdout
26 */
27#include <ns3/constant-position-mobility-model.h>
28#include <ns3/core-module.h>
29#include <ns3/log.h>
30#include <ns3/lr-wpan-module.h>
31#include <ns3/packet.h>
32#include <ns3/propagation-delay-model.h>
33#include <ns3/propagation-loss-model.h>
34#include <ns3/simulator.h>
35#include <ns3/single-model-spectrum-channel.h>
36
37#include <iostream>
38
39using namespace ns3;
40using namespace ns3::lrwpan;
41
42/**
43 * Function called when a Data indication is invoked
44 * \param params MCPS data indication parameters
45 * \param p packet
46 */
47static void
49{
50 NS_LOG_UNCOND("Received packet of size " << p->GetSize());
51}
52
53/**
54 * Function called when a Data confirm is invoked
55 * \param params MCPS data confirm parameters
56 */
57static void
59{
60 NS_LOG_UNCOND("LrWpanMcpsDataConfirmStatus = " << params.m_status);
61}
62
63/**
64 * Function called when a the PHY state changes
65 * \param context context
66 * \param now time at which the function is called
67 * \param oldState old PHY state
68 * \param newState new PHY state
69 */
70static void
71StateChangeNotification(std::string context,
72 Time now,
73 PhyEnumeration oldState,
74 PhyEnumeration newState)
75{
76 NS_LOG_UNCOND(context << " state change at " << now.As(Time::S) << " from "
79}
80
81int
82main(int argc, char* argv[])
83{
84 bool verbose = false;
85 bool extended = false;
86
87 CommandLine cmd(__FILE__);
88
89 cmd.AddValue("verbose", "turn on all log components", verbose);
90 cmd.AddValue("extended", "use extended addressing", extended);
91
92 cmd.Parse(argc, argv);
93
94 LrWpanHelper lrWpanHelper;
95 if (verbose)
96 {
97 lrWpanHelper.EnableLogComponents();
98 }
99
100 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
101 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
102
103 // Create 2 nodes, and a NetDevice for each one
104 Ptr<Node> n0 = CreateObject<Node>();
105 Ptr<Node> n1 = CreateObject<Node>();
106
107 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
108 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
109
110 if (!extended)
111 {
112 dev0->SetAddress(Mac16Address("00:01"));
113 dev1->SetAddress(Mac16Address("00:02"));
114 }
115 else
116 {
117 Ptr<LrWpanMac> mac0 = dev0->GetMac();
118 Ptr<LrWpanMac> mac1 = dev1->GetMac();
119 mac0->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
120 mac1->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
121 }
122
123 // Each device must be attached to the same channel
124 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
126 CreateObject<LogDistancePropagationLossModel>();
128 CreateObject<ConstantSpeedPropagationDelayModel>();
129 channel->AddPropagationLossModel(propModel);
130 channel->SetPropagationDelayModel(delayModel);
131
132 dev0->SetChannel(channel);
133 dev1->SetChannel(channel);
134
135 // To complete configuration, a LrWpanNetDevice must be added to a node
136 n0->AddDevice(dev0);
137 n1->AddDevice(dev1);
138
139 // Trace state changes in the phy
140 dev0->GetPhy()->TraceConnect("TrxState",
141 std::string("phy0"),
143 dev1->GetPhy()->TraceConnect("TrxState",
144 std::string("phy1"),
146
147 Ptr<ConstantPositionMobilityModel> sender0Mobility =
148 CreateObject<ConstantPositionMobilityModel>();
149 sender0Mobility->SetPosition(Vector(0, 0, 0));
150 dev0->GetPhy()->SetMobility(sender0Mobility);
151 Ptr<ConstantPositionMobilityModel> sender1Mobility =
152 CreateObject<ConstantPositionMobilityModel>();
153 // Configure position 10 m distance
154 sender1Mobility->SetPosition(Vector(0, 10, 0));
155 dev1->GetPhy()->SetMobility(sender1Mobility);
156
159 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
160
163 dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
164
167 dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
168
171 dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
172
173 // Tracing
174 lrWpanHelper.EnablePcapAll(std::string("lr-wpan-data"), true);
175 AsciiTraceHelper ascii;
176 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("lr-wpan-data.tr");
177 lrWpanHelper.EnableAsciiAll(stream);
178
179 // The below should trigger two callbacks when end-to-end data is working
180 // 1) DataConfirm callback is called
181 // 2) DataIndication callback is called with value of 50
182 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
184 params.m_dstPanId = 0;
185 if (!extended)
186 {
187 params.m_srcAddrMode = SHORT_ADDR;
188 params.m_dstAddrMode = SHORT_ADDR;
189 params.m_dstAddr = Mac16Address("00:02");
190 }
191 else
192 {
193 params.m_srcAddrMode = EXT_ADDR;
194 params.m_dstAddrMode = EXT_ADDR;
195 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
196 }
197 params.m_msduHandle = 0;
198 params.m_txOptions = TX_OPTION_ACK;
199 // dev0->GetMac ()->McpsDataRequest (params, p0);
201 Seconds(0.0),
203 dev0->GetMac(),
204 params,
205 p0);
206
207 // Send a packet back at time 2 seconds
208 Ptr<Packet> p2 = Create<Packet>(60); // 60 bytes of dummy data
209 if (!extended)
210 {
211 params.m_dstAddr = Mac16Address("00:01");
212 }
213 else
214 {
215 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
216 }
218 Seconds(2.0),
220 dev1->GetMac(),
221 params,
222 p2);
223
225
227 return 0;
228}
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
helps to manage and create IEEE 802.15.4 NetDevice objects
void EnableLogComponents()
Helper to enable all LrWpan log components with one statement.
static std::string LrWpanPhyEnumerationPrinter(lrwpan::PhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
an EUI-64 address
Definition: mac64-address.h:46
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p) override
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:386
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:115
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:64
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
static void DataConfirm(McpsDataConfirmParams params)
Function called when a Data confirm is invoked.
Definition: lr-wpan-data.cc:58
static void StateChangeNotification(std::string context, Time now, PhyEnumeration oldState, PhyEnumeration newState)
Function called when a the PHY state changes.
Definition: lr-wpan-data.cc:71
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
Definition: lr-wpan-data.cc:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:706
@ extended
Definition: ff-mac-common.h:85
ns cmd
Definition: second.py:40
ns channel
Definition: third.py:88
FtrParams params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
bool verbose
MCPS-DATA.indication params.