A Discrete-Event Network Simulator
API
lr-wpan-mlme.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
18  */
19 
20 /*
21  * Coordinator End Device
22  * N0 <---------------- N1
23  * (dev0) (dev1)
24  *
25  * This example demonstrate the usage of the MAC primitives involved in
26  * direct transmissions for the beacon enabled mode of IEEE 802.15.4-2011.
27  * A single packet is sent from an end device to the coordinator during the CAP
28  * of the first incoming superframe.
29  *
30  * This example do not demonstrate a full protocol stack usage.
31  * For full protocol stack usage refer to 6lowpan examples.
32  *
33  */
34 
35 #include <ns3/constant-position-mobility-model.h>
36 #include <ns3/core-module.h>
37 #include <ns3/log.h>
38 #include <ns3/lr-wpan-module.h>
39 #include <ns3/packet.h>
40 #include <ns3/propagation-delay-model.h>
41 #include <ns3/propagation-loss-model.h>
42 #include <ns3/simulator.h>
43 #include <ns3/single-model-spectrum-channel.h>
44 
45 #include <iostream>
46 
47 using namespace ns3;
48 
49 void
51 {
52  NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Received BEACON packet of size ");
53 }
54 
55 void
57 {
58  NS_LOG_UNCOND(Simulator::Now().GetSeconds()
59  << " secs | Received DATA packet of size " << p->GetSize());
60 }
61 
62 void
64 {
65  // In the case of transmissions with the Ack flag activated, the transaction is only
66  // successful if the Ack was received.
67  if (params.m_status == LrWpanMacStatus::SUCCESS)
68  {
69  NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Transmission successfully sent");
70  }
71 }
72 
73 void
75 {
76  NS_LOG_UNCOND(Simulator::Now().GetSeconds()
77  << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
78 }
79 
80 void
82 {
83  if (params.m_status == LrWpanMacStatus::SUCCESS)
84  {
85  NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "Beacon status SUCCESSFUL");
86  }
87 }
88 
89 int
90 main(int argc, char* argv[])
91 {
94  LogComponentEnable("LrWpanMac", LOG_LEVEL_INFO);
95  LogComponentEnable("LrWpanCsmaCa", LOG_LEVEL_INFO);
96 
97  LrWpanHelper lrWpanHelper;
98 
99  // Create 2 nodes, and a NetDevice for each one
100  Ptr<Node> n0 = CreateObject<Node>();
101  Ptr<Node> n1 = CreateObject<Node>();
102 
103  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
104  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
105 
106  dev0->SetAddress(Mac16Address("00:01"));
107  dev1->SetAddress(Mac16Address("00:02"));
108 
109  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
111  CreateObject<LogDistancePropagationLossModel>();
113  CreateObject<ConstantSpeedPropagationDelayModel>();
114  channel->AddPropagationLossModel(propModel);
115  channel->SetPropagationDelayModel(delayModel);
116 
117  dev0->SetChannel(channel);
118  dev1->SetChannel(channel);
119 
120  n0->AddDevice(dev0);
121  n1->AddDevice(dev1);
122 
124  Ptr<ConstantPositionMobilityModel> sender0Mobility =
125  CreateObject<ConstantPositionMobilityModel>();
126  sender0Mobility->SetPosition(Vector(0, 0, 0));
127  dev0->GetPhy()->SetMobility(sender0Mobility);
128  Ptr<ConstantPositionMobilityModel> sender1Mobility =
129  CreateObject<ConstantPositionMobilityModel>();
130 
131  sender1Mobility->SetPosition(Vector(0, 10, 0)); // 10 m distance
132  dev1->GetPhy()->SetMobility(sender1Mobility);
133 
135 
137  cb0 = MakeCallback(&StartConfirm);
138  dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
139 
142  dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
143 
146  dev1->GetMac()->SetMlmeBeaconNotifyIndicationCallback(cb3);
147 
150  dev1->GetMac()->SetMcpsDataIndicationCallback(cb4);
151 
154  dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
155 
157  // Note: We manually associate the devices to a PAN coordinator
158  // (i.e. bootstrap is not used);
159  // The PAN COORDINATOR does not need to associate or set its
160  // PAN Id or its own coordinator id, these are set
161  // by the MLME-start.request primitive when used.
162 
163  dev1->GetMac()->SetPanId(5);
164  dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
165 
167 
169  params.m_panCoor = true;
170  params.m_PanId = 5;
171  params.m_bcnOrd = 14;
172  params.m_sfrmOrd = 6;
174  Seconds(2.0),
176  dev0->GetMac(),
177  params);
178 
180 
181  Ptr<Packet> p1 = Create<Packet>(5);
182  McpsDataRequestParams params2;
183  params2.m_dstPanId = 5;
184  params2.m_srcAddrMode = SHORT_ADDR;
185  params2.m_dstAddrMode = SHORT_ADDR;
186  params2.m_dstAddr = Mac16Address("00:01");
187  params2.m_msduHandle = 0;
188  // params2.m_txOptions = TX_OPTION_ACK; // Enable direct transmission with Ack
189 
191  // Examples of time parameters for transmissions in the first incoming superframe. //
193 
194  // 2.981 sec No time to finish CCA in CAP, the transmission at this time will cause
195  // the packet to be deferred to the next superframe.
196 
197  // 2.982 sec No time to finish random backoff delay in CAP, the transmission at this
198  // time will cause the packet to be deferred to the next superframe.
199 
200  // 2.93 sec Enough time, the packet can be transmitted within the CAP of the first
201  // superframe
202 
203  // MCPS-DATA.request Beacon enabled Direct Transmission (dev1)
204  // Frame transmission from End Device to Coordinator (Direct transmission)
206  Seconds(2.93),
208  dev1->GetMac(),
209  params2,
210  p1);
211 
212  Simulator::Stop(Seconds(600));
213  Simulator::Run();
214 
216  return 0;
217 }
helps to manage and create IEEE 802.15.4 NetDevice objects
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.
Definition: lr-wpan-mac.cc:384
void MlmeStartRequest(MlmeStartRequestParams params) override
IEEE 802.15.4-2006, section 7.1.14.1 MLME-START.request Request to allow a PAN coordinator to initiat...
Definition: lr-wpan-mac.cc:584
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
@ SHORT_ADDR
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:74
void BeaconIndication(MlmeBeaconNotifyIndicationParams params)
Definition: lr-wpan-mlme.cc:50
void TransEndIndication(McpsDataConfirmParams params)
Definition: lr-wpan-mlme.cc:63
void StartConfirm(MlmeStartConfirmParams params)
Definition: lr-wpan-mlme.cc:81
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:56
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:302
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:704
@ SUCCESS
Definition: ff-mac-common.h:62
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
channel
Definition: third.py:88
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MCPS-DATA.confirm params.
MCPS-DATA.indication params.
MCPS-DATA.request params.
LrWpanAddressMode m_srcAddrMode
Source address mode.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
uint16_t m_dstPanId
Destination PAN identifier.
Mac16Address m_dstAddr
Destination address.
uint8_t m_msduHandle
MSDU handle.
MLME-BEACON-NOTIFY.indication params.
MLME-START.confirm params.
MLME-START.request params.