A Discrete-Event Network Simulator
API
lr-wpan-data.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 The Boeing Company
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tom Henderson <thomas.r.henderson@boeing.com>
19  */
20 
21 /*
22  * Try to send data end-to-end through a LrWpanMac <-> LrWpanPhy <->
23  * SpectrumChannel <-> LrWpanPhy <-> LrWpanMac chain
24  *
25  * Trace Phy state changes, and Mac DataIndication and DataConfirm events
26  * to stdout
27  */
28 #include <ns3/log.h>
29 #include <ns3/core-module.h>
30 #include <ns3/lr-wpan-module.h>
31 #include <ns3/propagation-loss-model.h>
32 #include <ns3/propagation-delay-model.h>
33 #include <ns3/simulator.h>
34 #include <ns3/single-model-spectrum-channel.h>
35 #include <ns3/constant-position-mobility-model.h>
36 #include <ns3/packet.h>
37 
38 #include <iostream>
39 
40 using namespace ns3;
41 
48 {
49  NS_LOG_UNCOND ("Received packet of size " << p->GetSize ());
50 }
51 
56 static void DataConfirm (McpsDataConfirmParams params)
57 {
58  NS_LOG_UNCOND ("LrWpanMcpsDataConfirmStatus = " << params.m_status);
59 }
60 
68 static void StateChangeNotification (std::string context, Time now, LrWpanPhyEnumeration oldState, LrWpanPhyEnumeration newState)
69 {
70  NS_LOG_UNCOND (context << " state change at " << now.As (Time::S)
71  << " from " << LrWpanHelper::LrWpanPhyEnumerationPrinter (oldState)
72  << " to " << LrWpanHelper::LrWpanPhyEnumerationPrinter (newState));
73 }
74 
75 int main (int argc, char *argv[])
76 {
77  bool verbose = false;
78  bool extended = false;
79 
80  CommandLine cmd (__FILE__);
81 
82  cmd.AddValue ("verbose", "turn on all log components", verbose);
83  cmd.AddValue ("extended", "use extended addressing", extended);
84 
85  cmd.Parse (argc, argv);
86 
87  LrWpanHelper lrWpanHelper;
88  if (verbose)
89  {
90  lrWpanHelper.EnableLogComponents ();
91  }
92 
93  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
94  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
95 
96  // Create 2 nodes, and a NetDevice for each one
97  Ptr<Node> n0 = CreateObject <Node> ();
98  Ptr<Node> n1 = CreateObject <Node> ();
99 
100  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
101  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
102 
103  if (!extended)
104  {
105  dev0->SetAddress (Mac16Address ("00:01"));
106  dev1->SetAddress (Mac16Address ("00:02"));
107  }
108  else
109  {
110  Ptr<LrWpanMac> mac0 = dev0->GetMac ();
111  Ptr<LrWpanMac> mac1 = dev1->GetMac ();
112  mac0->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:01"));
113  mac1->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:02"));
114  }
115 
116  // Each device must be attached to the same channel
117  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
118  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
119  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
120  channel->AddPropagationLossModel (propModel);
121  channel->SetPropagationDelayModel (delayModel);
122 
123  dev0->SetChannel (channel);
124  dev1->SetChannel (channel);
125 
126  // To complete configuration, a LrWpanNetDevice must be added to a node
127  n0->AddDevice (dev0);
128  n1->AddDevice (dev1);
129 
130  // Trace state changes in the phy
131  dev0->GetPhy ()->TraceConnect ("TrxState", std::string ("phy0"), MakeCallback (&StateChangeNotification));
132  dev1->GetPhy ()->TraceConnect ("TrxState", std::string ("phy1"), MakeCallback (&StateChangeNotification));
133 
134  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
135  sender0Mobility->SetPosition (Vector (0,0,0));
136  dev0->GetPhy ()->SetMobility (sender0Mobility);
137  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
138  // Configure position 10 m distance
139  sender1Mobility->SetPosition (Vector (0,10,0));
140  dev1->GetPhy ()->SetMobility (sender1Mobility);
141 
143  cb0 = MakeCallback (&DataConfirm);
144  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
145 
147  cb1 = MakeCallback (&DataIndication);
148  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
149 
151  cb2 = MakeCallback (&DataConfirm);
152  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
153 
155  cb3 = MakeCallback (&DataIndication);
156  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
157 
158  // Tracing
159  lrWpanHelper.EnablePcapAll (std::string ("lr-wpan-data"), true);
160  AsciiTraceHelper ascii;
161  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("lr-wpan-data.tr");
162  lrWpanHelper.EnableAsciiAll (stream);
163 
164  // The below should trigger two callbacks when end-to-end data is working
165  // 1) DataConfirm callback is called
166  // 2) DataIndication callback is called with value of 50
167  Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
168  McpsDataRequestParams params;
169  params.m_dstPanId = 0;
170  if (!extended)
171  {
172  params.m_srcAddrMode = SHORT_ADDR;
173  params.m_dstAddrMode = SHORT_ADDR;
174  params.m_dstAddr = Mac16Address ("00:02");
175  }
176  else
177  {
178  params.m_srcAddrMode = EXT_ADDR;
179  params.m_dstAddrMode = EXT_ADDR;
180  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:02");
181  }
182  params.m_msduHandle = 0;
183  params.m_txOptions = TX_OPTION_ACK;
184 // dev0->GetMac ()->McpsDataRequest (params, p0);
187  dev0->GetMac (), params, p0);
188 
189  // Send a packet back at time 2 seconds
190  Ptr<Packet> p2 = Create<Packet> (60); // 60 bytes of dummy data
191  if (!extended)
192  {
193  params.m_dstAddr = Mac16Address ("00:01");
194  }
195  else
196  {
197  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:01");
198  }
201  dev1->GetMac (), params, p2);
202 
203  Simulator::Run ();
204 
206  return 0;
207 }
ns3::LrWpanHelper
helps to manage and create IEEE 802.15.4 NetDevice objects
Definition: lr-wpan-helper.h:51
DataConfirm
static void DataConfirm(McpsDataConfirmParams params)
Function called when a Data confirm is invoked.
Definition: lr-wpan-data.cc:56
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::AsciiTraceHelperForDevice::EnableAsciiAll
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...
Definition: trace-helper.cc:586
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::Callback< void, McpsDataConfirmParams >
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TX_OPTION_ACK
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:59
ns3::Mac16Address
This class can contain 16 bit addresses.
Definition: mac16-address.h:42
ns3::McpsDataRequestParams::m_dstAddrMode
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:247
ns3::McpsDataRequestParams::m_dstPanId
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
StateChangeNotification
static void StateChangeNotification(std::string context, Time now, LrWpanPhyEnumeration oldState, LrWpanPhyEnumeration newState)
Function called when a the PHY state changes.
Definition: lr-wpan-data.cc:68
ns3::PcapHelperForDevice::EnablePcapAll
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 ...
Definition: trace-helper.cc:439
third.channel
channel
Definition: third.py:92
ns3::MobilityModel::SetPosition
void SetPosition(const Vector &position)
Definition: mobility-model.cc:88
ns3::EXT_ADDR
@ EXT_ADDR
Definition: lr-wpan-mac.h:142
ns3::Time::As
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:429
ns3::Mac64Address
an EUI-64 address
Definition: mac64-address.h:44
ns3::McpsDataRequestParams::m_txOptions
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:252
ns3::McpsDataRequestParams::m_dstExtAddr
Mac64Address m_dstExtAddr
Destination extended address.
Definition: lr-wpan-mac.h:250
ns3::Ptr< Packet >
DataIndication
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Function called when a Data indication is invoked.
Definition: lr-wpan-data.cc:47
ns3::McpsDataConfirmParams
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:261
ns3::AsciiTraceHelper::CreateFileStream
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.
Definition: trace-helper.cc:191
ns3::LrWpanMac::SetExtendedAddress
void SetExtendedAddress(Mac64Address address)
Set the extended address of this MAC.
Definition: lr-wpan-mac.cc:272
ns3::LrWpanNetDevice::GetPhy
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
Definition: lr-wpan-net-device.cc:199
ns3::LrWpanPhyEnumeration
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:106
verbose
bool verbose
Definition: openflow-switch.cc:50
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::LrWpanHelper::EnableLogComponents
void EnableLogComponents(void)
Helper to enable all LrWpan log components with one statement.
Definition: lr-wpan-helper.cc:101
ns3::LrWpanHelper::LrWpanPhyEnumerationPrinter
static std::string LrWpanPhyEnumerationPrinter(LrWpanPhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
Definition: lr-wpan-helper.cc:116
second.cmd
cmd
Definition: second.py:35
ns3::Node::AddDevice
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
NS_LOG_UNCOND
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
Definition: log-macros-enabled.h:269
ns3::LrWpanNetDevice::SetChannel
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Definition: lr-wpan-net-device.cc:183
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::extended
@ extended
Definition: ff-mac-common.h:85
ns3::AsciiTraceHelper
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
ns3::McpsDataIndicationParams
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:271
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Time::S
@ S
second
Definition: nstime.h:115
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::McpsDataConfirmParams::m_status
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:263
ns3::McpsDataRequestParams::m_srcAddrMode
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:246
ns3::McpsDataRequestParams::m_dstAddr
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:249
ns3::SHORT_ADDR
@ SHORT_ADDR
Definition: lr-wpan-mac.h:141
ns3::LrWpanNetDevice::SetAddress
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
Definition: lr-wpan-net-device.cc:256
ns3::LrWpanMac::McpsDataRequest
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p)
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU.
Definition: lr-wpan-mac.cc:293
ns3::McpsDataRequestParams
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:237
ns3::LrWpanNetDevice::GetMac
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
Definition: lr-wpan-net-device.cc:192
ns3::McpsDataRequestParams::m_msduHandle
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:251