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 
43 {
44  NS_LOG_UNCOND ("Received packet of size " << p->GetSize ());
45 }
46 
47 static void DataConfirm (McpsDataConfirmParams params)
48 {
49  NS_LOG_UNCOND ("LrWpanMcpsDataConfirmStatus = " << params.m_status);
50 }
51 
52 static void StateChangeNotification (std::string context, Time now, LrWpanPhyEnumeration oldState, LrWpanPhyEnumeration newState)
53 {
54  NS_LOG_UNCOND (context << " state change at " << now.GetSeconds ()
55  << " from " << LrWpanHelper::LrWpanPhyEnumerationPrinter (oldState)
56  << " to " << LrWpanHelper::LrWpanPhyEnumerationPrinter (newState));
57 }
58 
59 int main (int argc, char *argv[])
60 {
61  bool verbose = false;
62  bool extended = false;
63 
65 
66  cmd.AddValue ("verbose", "turn on all log components", verbose);
67  cmd.AddValue ("extended", "use extended addressing", extended);
68 
69  cmd.Parse (argc, argv);
70 
71  LrWpanHelper lrWpanHelper;
72  if (verbose)
73  {
74  lrWpanHelper.EnableLogComponents ();
75  }
76 
77  // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark.
78  // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
79 
80  // Create 2 nodes, and a NetDevice for each one
81  Ptr<Node> n0 = CreateObject <Node> ();
82  Ptr<Node> n1 = CreateObject <Node> ();
83 
84  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
85  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
86 
87  if (!extended)
88  {
89  dev0->SetAddress (Mac16Address ("00:01"));
90  dev1->SetAddress (Mac16Address ("00:02"));
91  }
92  else
93  {
94  Ptr<LrWpanMac> mac0 = dev0->GetMac();
95  Ptr<LrWpanMac> mac1 = dev1->GetMac();
96  mac0->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:01"));
97  mac1->SetExtendedAddress (Mac64Address ("00:00:00:00:00:00:00:02"));
98  }
99 
100  // Each device must be attached to the same channel
101  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
102  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
103  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
104  channel->AddPropagationLossModel (propModel);
105  channel->SetPropagationDelayModel (delayModel);
106 
107  dev0->SetChannel (channel);
108  dev1->SetChannel (channel);
109 
110  // To complete configuration, a LrWpanNetDevice must be added to a node
111  n0->AddDevice (dev0);
112  n1->AddDevice (dev1);
113 
114  // Trace state changes in the phy
115  dev0->GetPhy ()->TraceConnect ("TrxState", std::string ("phy0"), MakeCallback (&StateChangeNotification));
116  dev1->GetPhy ()->TraceConnect ("TrxState", std::string ("phy1"), MakeCallback (&StateChangeNotification));
117 
118  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
119  sender0Mobility->SetPosition (Vector (0,0,0));
120  dev0->GetPhy ()->SetMobility (sender0Mobility);
121  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
122  // Configure position 10 m distance
123  sender1Mobility->SetPosition (Vector (0,10,0));
124  dev1->GetPhy ()->SetMobility (sender1Mobility);
125 
127  cb0 = MakeCallback (&DataConfirm);
128  dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0);
129 
131  cb1 = MakeCallback (&DataIndication);
132  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1);
133 
135  cb2 = MakeCallback (&DataConfirm);
136  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2);
137 
139  cb3 = MakeCallback (&DataIndication);
140  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3);
141 
142  // Tracing
143  lrWpanHelper.EnablePcapAll (std::string ("lr-wpan-data"), true);
144  AsciiTraceHelper ascii;
145  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("lr-wpan-data.tr");
146  lrWpanHelper.EnableAsciiAll (stream);
147 
148  // The below should trigger two callbacks when end-to-end data is working
149  // 1) DataConfirm callback is called
150  // 2) DataIndication callback is called with value of 50
151  Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data
152  McpsDataRequestParams params;
153  params.m_dstPanId = 0;
154  if (!extended)
155  {
156  params.m_srcAddrMode = SHORT_ADDR;
157  params.m_dstAddrMode = SHORT_ADDR;
158  params.m_dstAddr = Mac16Address ("00:02");
159  }
160  else
161  {
162  params.m_srcAddrMode = EXT_ADDR;
163  params.m_dstAddrMode = EXT_ADDR;
164  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:02");
165  }
166  params.m_msduHandle = 0;
167  params.m_txOptions = TX_OPTION_ACK;
168 // dev0->GetMac ()->McpsDataRequest (params, p0);
171  dev0->GetMac (), params, p0);
172 
173  // Send a packet back at time 2 seconds
174  Ptr<Packet> p2 = Create<Packet> (60); // 60 bytes of dummy data
175  if (!extended)
176  {
177  params.m_dstAddr = Mac16Address ("00:01");
178  }
179  else
180  {
181  params.m_dstExtAddr = Mac64Address ("00:00:00:00:00:00:00:01");
182  }
185  dev1->GetMac (), params, p2);
186 
187  Simulator::Run ();
188 
190  return 0;
191 }
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:159
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
static std::string LrWpanPhyEnumerationPrinter(LrWpanPhyEnumeration e)
Transform the LrWpanPhyEnumeration enumeration into a printable string.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
static void StateChangeNotification(std::string context, Time now, LrWpanPhyEnumeration oldState, LrWpanPhyEnumeration newState)
Definition: lr-wpan-data.cc:52
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
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:250
channel
Definition: third.py:85
an EUI-64 address
Definition: mac64-address.h:43
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-data.cc:42
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:57
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 ...
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:171
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:174
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
void SetPosition(const Vector &position)
void SetExtendedAddress(Mac64Address address)
Set the extended address of this MAC.
Definition: lr-wpan-mac.cc:229
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1475
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
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...
LrWpanPhyEnumeration
IEEE802.15.4-2006 PHY Emumerations Table 18 in section 6.2.3.
Definition: lr-wpan-phy.h:105
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
helps to manage and create IEEE 802.15.4 NetDevice objects
void EnableLogComponents(void)
Helper to enable all LrWpan log components with one statement.
static void DataConfirm(McpsDataConfirmParams params)
Definition: lr-wpan-data.cc:47
bool verbose
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:182