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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tom Henderson <thomas.r.henderson@boeing.com>
7 */
8
9/*
10 * Try to send data end-to-end through a LrWpanMac <-> LrWpanPhy <->
11 * SpectrumChannel <-> LrWpanPhy <-> LrWpanMac chain
12 *
13 * Trace Phy state changes, and Mac DataIndication and DataConfirm events
14 * to stdout
15 */
16#include <ns3/constant-position-mobility-model.h>
17#include <ns3/core-module.h>
18#include <ns3/log.h>
19#include <ns3/lr-wpan-module.h>
20#include <ns3/packet.h>
21#include <ns3/propagation-delay-model.h>
22#include <ns3/propagation-loss-model.h>
23#include <ns3/simulator.h>
24#include <ns3/single-model-spectrum-channel.h>
25
26#include <iostream>
27
28using namespace ns3;
29using namespace ns3::lrwpan;
30
31/**
32 * Function called when a Data indication is invoked
33 * \param params MCPS data indication parameters
34 * \param p packet
35 */
36static void
38{
39 NS_LOG_UNCOND("Received packet of size " << p->GetSize());
40}
41
42/**
43 * Function called when a Data confirm is invoked
44 * \param params MCPS data confirm parameters
45 */
46static void
48{
49 NS_LOG_UNCOND("LrWpanMcpsDataConfirmStatus = " << params.m_status);
50}
51
52/**
53 * Function called when a the PHY state changes
54 * \param context context
55 * \param now time at which the function is called
56 * \param oldState old PHY state
57 * \param newState new PHY state
58 */
59static void
60StateChangeNotification(std::string context,
61 Time now,
62 PhyEnumeration oldState,
63 PhyEnumeration newState)
64{
65 NS_LOG_UNCOND(context << " state change at " << now.As(Time::S) << " from "
68}
69
70int
71main(int argc, char* argv[])
72{
73 bool verbose = false;
74 bool extended = false;
75
76 CommandLine cmd(__FILE__);
77
78 cmd.AddValue("verbose", "turn on all log components", verbose);
79 cmd.AddValue("extended", "use extended addressing", extended);
80
81 cmd.Parse(argc, argv);
82
83 if (verbose)
84 {
88 }
89
90 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
91 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
92
93 // Create 2 nodes, and a NetDevice for each one
96
99
100 // Each device must be attached to the same channel
106 channel->AddPropagationLossModel(propModel);
107 channel->SetPropagationDelayModel(delayModel);
108
109 dev0->SetChannel(channel);
110 dev1->SetChannel(channel);
111
112 // To complete configuration, a LrWpanNetDevice must be added to a node
113 n0->AddDevice(dev0);
114 n1->AddDevice(dev1);
115
116 // Note: This setup, which has been done manually here, can be simplified using the LrWpanHelper
117 // class. The LrWpanHelper can be used to set up the propagation loss and delay models in many
118 // devices in a simpler way. The following is an equivalent, simplified setup:
119 //
120 // LrWpanHelper lrWpanHelper;
121 // lrWpanHelper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
122 // lrWpanHelper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
123 // NodeContainer nodes;
124 // nodes.Create(2);
125 // NetDeviceContainer devices = lrWpanHelper.Install(nodes);
126 // Ptr<LrWpanNetDevice> dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
127 // Ptr<LrWpanNetDevice> dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
128
129 // Set 16-bit short addresses if extended is false, otherwise use 64-bit extended addresses
130 if (!extended)
131 {
132 dev0->SetAddress(Mac16Address("00:01"));
133 dev1->SetAddress(Mac16Address("00:02"));
134 }
135 else
136 {
137 Ptr<LrWpanMac> mac0 = dev0->GetMac();
138 Ptr<LrWpanMac> mac1 = dev1->GetMac();
139 mac0->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
140 mac1->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
141 }
142
143 // Trace state changes in the phy
144 dev0->GetPhy()->TraceConnect("TrxState",
145 std::string("phy0"),
147 dev1->GetPhy()->TraceConnect("TrxState",
148 std::string("phy1"),
150
151 Ptr<ConstantPositionMobilityModel> sender0Mobility =
153 sender0Mobility->SetPosition(Vector(0, 0, 0));
154 dev0->GetPhy()->SetMobility(sender0Mobility);
155 Ptr<ConstantPositionMobilityModel> sender1Mobility =
157 // Configure position 10 m distance
158 sender1Mobility->SetPosition(Vector(0, 10, 0));
159 dev1->GetPhy()->SetMobility(sender1Mobility);
160
163 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
164
167 dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
168
171 dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
172
175 dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
176
177 // The below should trigger two callbacks when end-to-end data is working
178 // 1) DataConfirm callback is called
179 // 2) DataIndication callback is called with value of 50
180 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
182 params.m_dstPanId = 0;
183 if (!extended)
184 {
185 params.m_srcAddrMode = SHORT_ADDR;
186 params.m_dstAddrMode = SHORT_ADDR;
187 params.m_dstAddr = Mac16Address("00:02");
188 }
189 else
190 {
191 params.m_srcAddrMode = EXT_ADDR;
192 params.m_dstAddrMode = EXT_ADDR;
193 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
194 }
195 params.m_msduHandle = 0;
196 params.m_txOptions = TX_OPTION_ACK;
197 // dev0->GetMac ()->McpsDataRequest (params, p0);
199 Seconds(0.0),
201 dev0->GetMac(),
202 params,
203 p0);
204
205 // Send a packet back at time 2 seconds
206 Ptr<Packet> p2 = Create<Packet>(60); // 60 bytes of dummy data
207 if (!extended)
208 {
209 params.m_dstAddr = Mac16Address("00:01");
210 }
211 else
212 {
213 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
214 }
216 Seconds(2.0),
218 dev1->GetMac(),
219 params,
220 p2);
221
223
225 return 0;
226}
Parse command-line arguments.
static std::string LrWpanPhyEnumerationPrinter(lrwpan::PhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
This class can contain 16 bit addresses.
an EUI-64 address
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
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.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:53
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
static void DataConfirm(McpsDataConfirmParams params)
Function called when a Data confirm is invoked.
static void StateChangeNotification(std::string context, Time now, PhyEnumeration oldState, PhyEnumeration newState)
Function called when a the PHY state changes.
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
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:684
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:107
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
@ extended
channel
Definition third.py:77
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
bool verbose