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 std::cout << "Received packet of size: " << p->GetSize()
40 << " | LQI: " << static_cast<uint16_t>(params.m_mpduLinkQuality)
41 << " | RSSI: " << static_cast<int16_t>(params.m_rssi) << " dBm\n";
42}
43
44/**
45 * Function called when a Data confirm is invoked
46 * @param params MCPS data confirm parameters
47 */
48static void
50{
51 std::cout << "LrWpanMcpsDataConfirmStatus = " << static_cast<uint16_t>(params.m_status) << "\n";
52}
53
54/**
55 * Function called when a the PHY state changes
56 * @param context context
57 * @param now time at which the function is called
58 * @param oldState old PHY state
59 * @param newState new PHY state
60 */
61static void
62StateChangeNotification(std::string context,
63 Time now,
64 PhyEnumeration oldState,
65 PhyEnumeration newState)
66{
67 std::cout << context << " state change at " << now.As(Time::S) << " from "
70}
71
72int
73main(int argc, char* argv[])
74{
75 bool verbose = false;
76 bool extended = false;
77
78 // CommandLine options are also required by PyViz visualizer
79 CommandLine cmd(__FILE__);
80
81 cmd.AddValue("verbose", "turn on all log components", verbose);
82 cmd.AddValue("extended", "use extended addressing", extended);
83
84 cmd.Parse(argc, argv);
85
86 if (verbose)
87 {
91 }
92
93 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
94 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
95
96 // Create 2 nodes, and a NetDevice for each one
99
102
103 // Each device must be attached to the same channel
109 channel->AddPropagationLossModel(propModel);
110 channel->SetPropagationDelayModel(delayModel);
111
112 dev0->SetChannel(channel);
113 dev1->SetChannel(channel);
114
115 // To complete configuration, a LrWpanNetDevice must be added to a node
116 n0->AddDevice(dev0);
117 n1->AddDevice(dev1);
118
119 // Note: This setup, which has been done manually here, can be simplified using the LrWpanHelper
120 // class. The LrWpanHelper can be used to set up the propagation loss and delay models in many
121 // devices in a simpler way. The following is an equivalent, simplified setup:
122 //
123 // LrWpanHelper lrWpanHelper;
124 // lrWpanHelper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
125 // lrWpanHelper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
126 // NodeContainer nodes;
127 // nodes.Create(2);
128 // NetDeviceContainer devices = lrWpanHelper.Install(nodes);
129 // Ptr<LrWpanNetDevice> dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
130 // Ptr<LrWpanNetDevice> dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
131
132 // Set 16-bit and 64-bit MAC addresses.
133 // Note: Extended addresses must ALWAYS be present. If the devices are using the extended
134 // address mode, short addresses should use the short address FF:FE. A short address of FF:FF
135 // indicates that the devices is not associated to any device.
136 if (!extended)
137 {
138 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
139 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
140 dev0->GetMac()->SetShortAddress(Mac16Address("00:01"));
141 dev1->GetMac()->SetShortAddress(Mac16Address("00:02"));
142 }
143 else
144 {
145 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
146 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
147 dev0->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
148 dev1->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
149 }
150
151 // Trace state changes in the phy
152 dev0->GetPhy()->TraceConnect("TrxState",
153 std::string("phy0"),
155 dev1->GetPhy()->TraceConnect("TrxState",
156 std::string("phy1"),
158
159 Ptr<ConstantPositionMobilityModel> sender0Mobility =
161 sender0Mobility->SetPosition(Vector(0, 0, 0));
162 n0->AggregateObject(sender0Mobility);
163
164 Ptr<ConstantPositionMobilityModel> sender1Mobility =
166 sender1Mobility->SetPosition(Vector(0, 10, 0));
167 n1->AggregateObject(sender1Mobility);
168
171 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
172
175 dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
176
179 dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
180
183 dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
184
185 // The below should trigger two callbacks when end-to-end data is working
186 // 1) DataConfirm callback is called
187 // 2) DataIndication callback is called with value of 50
188 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
190 params.m_dstPanId = 0;
191 if (!extended)
192 {
193 params.m_srcAddrMode = SHORT_ADDR;
194 params.m_dstAddrMode = SHORT_ADDR;
195 params.m_dstAddr = Mac16Address("00:02");
196 }
197 else
198 {
199 params.m_srcAddrMode = EXT_ADDR;
200 params.m_dstAddrMode = EXT_ADDR;
201 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
202 }
203 params.m_msduHandle = 0;
204 params.m_txOptions = TX_OPTION_ACK;
205
206 // dev0->GetMac ()->McpsDataRequest (params, p0);
208 Seconds(0),
210 dev0->GetMac(),
211 params,
212 p0);
213
214 // Send a packet back at time 2 seconds
215 Ptr<Packet> p2 = Create<Packet>(60); // 60 bytes of dummy data
216 if (!extended)
217 {
218 params.m_dstAddr = Mac16Address("00:01");
219 }
220 else
221 {
222 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
223 }
225 Seconds(2),
227 dev1->GetMac(),
228 params,
229 p2);
230
233
235 return 0;
236}
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.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:408
@ S
second
Definition nstime.h:106
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.
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:690
Callback< void, McpsDataIndicationParams, Ptr< Packet > > McpsDataIndicationCallback
This callback is called after a Mcps has successfully received a frame and wants to deliver it to the...
PhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Callback< void, McpsDataConfirmParams > McpsDataConfirmCallback
This callback is called after a McpsDataRequest has been called from the higher layer.
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:54
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
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:279
LogLevel
Logging severity classes and levels.
Definition log.h:86
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:297
@ 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