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 = " << static_cast<uint16_t>(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 options are also required by PyViz visualizer
77 CommandLine cmd(__FILE__);
78
79 cmd.AddValue("verbose", "turn on all log components", verbose);
80 cmd.AddValue("extended", "use extended addressing", extended);
81
82 cmd.Parse(argc, argv);
83
84 if (verbose)
85 {
89 }
90
91 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices
92 // or wireshark. GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
93
94 // Create 2 nodes, and a NetDevice for each one
97
100
101 // Each device must be attached to the same channel
107 channel->AddPropagationLossModel(propModel);
108 channel->SetPropagationDelayModel(delayModel);
109
110 dev0->SetChannel(channel);
111 dev1->SetChannel(channel);
112
113 // To complete configuration, a LrWpanNetDevice must be added to a node
114 n0->AddDevice(dev0);
115 n1->AddDevice(dev1);
116
117 // Note: This setup, which has been done manually here, can be simplified using the LrWpanHelper
118 // class. The LrWpanHelper can be used to set up the propagation loss and delay models in many
119 // devices in a simpler way. The following is an equivalent, simplified setup:
120 //
121 // LrWpanHelper lrWpanHelper;
122 // lrWpanHelper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
123 // lrWpanHelper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
124 // NodeContainer nodes;
125 // nodes.Create(2);
126 // NetDeviceContainer devices = lrWpanHelper.Install(nodes);
127 // Ptr<LrWpanNetDevice> dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
128 // Ptr<LrWpanNetDevice> dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
129
130 // Set 16-bit and 64-bit MAC addresses.
131 // Note: Extended addresses must ALWAYS be present. If the devices are using the extended
132 // address mode, short addresses should use the short address FF:FE. A short address of FF:FF
133 // indicates that the devices is not associated to any device.
134 if (!extended)
135 {
136 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
137 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
138 dev0->GetMac()->SetShortAddress(Mac16Address("00:01"));
139 dev1->GetMac()->SetShortAddress(Mac16Address("00:02"));
140 }
141 else
142 {
143 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
144 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
145 dev0->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
146 dev1->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
147 }
148
149 // Trace state changes in the phy
150 dev0->GetPhy()->TraceConnect("TrxState",
151 std::string("phy0"),
153 dev1->GetPhy()->TraceConnect("TrxState",
154 std::string("phy1"),
156
157 Ptr<ConstantPositionMobilityModel> sender0Mobility =
159 sender0Mobility->SetPosition(Vector(0, 0, 0));
160 n0->AggregateObject(sender0Mobility);
161
162 Ptr<ConstantPositionMobilityModel> sender1Mobility =
164 sender1Mobility->SetPosition(Vector(0, 10, 0));
165 n1->AggregateObject(sender1Mobility);
166
169 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
170
173 dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
174
177 dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
178
181 dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
182
183 // The below should trigger two callbacks when end-to-end data is working
184 // 1) DataConfirm callback is called
185 // 2) DataIndication callback is called with value of 50
186 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
188 params.m_dstPanId = 0;
189 if (!extended)
190 {
191 params.m_srcAddrMode = SHORT_ADDR;
192 params.m_dstAddrMode = SHORT_ADDR;
193 params.m_dstAddr = Mac16Address("00:02");
194 }
195 else
196 {
197 params.m_srcAddrMode = EXT_ADDR;
198 params.m_dstAddrMode = EXT_ADDR;
199 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
200 }
201 params.m_msduHandle = 0;
202 params.m_txOptions = TX_OPTION_ACK;
203 // dev0->GetMac ()->McpsDataRequest (params, p0);
205 Seconds(0),
207 dev0->GetMac(),
208 params,
209 p0);
210
211 // Send a packet back at time 2 seconds
212 Ptr<Packet> p2 = Create<Packet>(60); // 60 bytes of dummy data
213 if (!extended)
214 {
215 params.m_dstAddr = Mac16Address("00:01");
216 }
217 else
218 {
219 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
220 }
222 Seconds(2),
224 dev1->GetMac(),
225 params,
226 p2);
227
230
232 return 0;
233}
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:96
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:107
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
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
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:1381
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