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 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 and 64-bit MAC addresses.
130 // Note: Extended addresses must ALWAYS be present. If the devices are using the extended
131 // address mode, short addresses should use the short address FF:FE. A short address of FF:FF
132 // indicates that the devices is not associated to any device.
133 if (!extended)
134 {
135 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
136 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
137 dev0->GetMac()->SetShortAddress(Mac16Address("00:01"));
138 dev1->GetMac()->SetShortAddress(Mac16Address("00:02"));
139 }
140 else
141 {
142 dev0->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
143 dev1->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
144 dev0->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
145 dev1->GetMac()->SetShortAddress(Mac16Address("FF:FE"));
146 }
147
148 // Trace state changes in the phy
149 dev0->GetPhy()->TraceConnect("TrxState",
150 std::string("phy0"),
152 dev1->GetPhy()->TraceConnect("TrxState",
153 std::string("phy1"),
155
156 Ptr<ConstantPositionMobilityModel> sender0Mobility =
158 sender0Mobility->SetPosition(Vector(0, 0, 0));
159 dev0->GetPhy()->SetMobility(sender0Mobility);
160 Ptr<ConstantPositionMobilityModel> sender1Mobility =
162 // Configure position 10 m distance
163 sender1Mobility->SetPosition(Vector(0, 10, 0));
164 dev1->GetPhy()->SetMobility(sender1Mobility);
165
168 dev0->GetMac()->SetMcpsDataConfirmCallback(cb0);
169
172 dev0->GetMac()->SetMcpsDataIndicationCallback(cb1);
173
176 dev1->GetMac()->SetMcpsDataConfirmCallback(cb2);
177
180 dev1->GetMac()->SetMcpsDataIndicationCallback(cb3);
181
182 // The below should trigger two callbacks when end-to-end data is working
183 // 1) DataConfirm callback is called
184 // 2) DataIndication callback is called with value of 50
185 Ptr<Packet> p0 = Create<Packet>(50); // 50 bytes of dummy data
187 params.m_dstPanId = 0;
188 if (!extended)
189 {
190 params.m_srcAddrMode = SHORT_ADDR;
191 params.m_dstAddrMode = SHORT_ADDR;
192 params.m_dstAddr = Mac16Address("00:02");
193 }
194 else
195 {
196 params.m_srcAddrMode = EXT_ADDR;
197 params.m_dstAddrMode = EXT_ADDR;
198 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:02");
199 }
200 params.m_msduHandle = 0;
201 params.m_txOptions = TX_OPTION_ACK;
202 // dev0->GetMac ()->McpsDataRequest (params, p0);
204 Seconds(0),
206 dev0->GetMac(),
207 params,
208 p0);
209
210 // Send a packet back at time 2 seconds
211 Ptr<Packet> p2 = Create<Packet>(60); // 60 bytes of dummy data
212 if (!extended)
213 {
214 params.m_dstAddr = Mac16Address("00:01");
215 }
216 else
217 {
218 params.m_dstExtAddr = Mac64Address("00:00:00:00:00:00:00:01");
219 }
221 Seconds(2),
223 dev1->GetMac(),
224 params,
225 p2);
226
228
230 return 0;
231}
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:578
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:403
@ 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:1345
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