A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-mlme.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
7 */
8
9/*
10 * Coordinator End Device
11 * N0 <---------------- N1
12 * (dev0) (dev1)
13 *
14 * This example demonstrate the usage of the MAC primitives involved in
15 * direct transmissions for the beacon enabled mode of IEEE 802.15.4-2011.
16 * A single packet is sent from an end device to the coordinator during the CAP
17 * of the first incoming superframe.
18 *
19 * This example do not demonstrate a full protocol stack usage.
20 * For full protocol stack usage refer to 6lowpan examples.
21 *
22 */
23
24#include <ns3/constant-position-mobility-model.h>
25#include <ns3/core-module.h>
26#include <ns3/log.h>
27#include <ns3/lr-wpan-module.h>
28#include <ns3/packet.h>
29#include <ns3/propagation-delay-model.h>
30#include <ns3/propagation-loss-model.h>
31#include <ns3/simulator.h>
32#include <ns3/single-model-spectrum-channel.h>
33
34#include <iostream>
35
36using namespace ns3;
37using namespace ns3::lrwpan;
38
39void
41{
42 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Received BEACON packet of size ");
43}
44
45void
47{
48 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
49 << " secs | Received DATA packet of size " << p->GetSize());
50}
51
52void
54{
55 // In the case of transmissions with the Ack flag activated, the transaction is only
56 // successful if the Ack was received.
57 if (params.m_status == MacStatus::SUCCESS)
58 {
59 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Transmission successfully sent");
60 }
61}
62
63void
65{
66 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
67 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
68}
69
70void
72{
73 if (params.m_status == MacStatus::SUCCESS)
74 {
75 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "Beacon status SUCCESSFUL");
76 }
77}
78
79int
80main(int argc, char* argv[])
81{
85 LogComponentEnable("LrWpanCsmaCa", LOG_LEVEL_INFO);
86
87 LrWpanHelper lrWpanHelper;
88
89 // Create 2 nodes, and a NetDevice for each one
90
92 nodes.Create(2);
93
94 // Use LrWpanHelper to create devices and assign them to nodes
95 lrWpanHelper.SetPropagationDelayModel("ns3::ConstantSpeedPropagationDelayModel");
96 lrWpanHelper.AddPropagationLossModel("ns3::LogDistancePropagationLossModel");
98 Ptr<LrWpanNetDevice> dev0 = devices.Get(0)->GetObject<LrWpanNetDevice>();
99 Ptr<LrWpanNetDevice> dev1 = devices.Get(1)->GetObject<LrWpanNetDevice>();
100
101 dev0->SetAddress(Mac16Address("00:01"));
102 dev1->SetAddress(Mac16Address("00:02"));
103
104 ///////////////// Mobility ///////////////////////
105 Ptr<ConstantPositionMobilityModel> sender0Mobility =
107 sender0Mobility->SetPosition(Vector(0, 0, 0));
108 dev0->GetPhy()->SetMobility(sender0Mobility);
109 Ptr<ConstantPositionMobilityModel> sender1Mobility =
111
112 sender1Mobility->SetPosition(Vector(0, 10, 0)); // 10 m distance
113 dev1->GetPhy()->SetMobility(sender1Mobility);
114
115 /////// MAC layer Callbacks hooks/////////////
116
119 dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
120
123 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
124
127 dev1->GetMac()->SetMlmeBeaconNotifyIndicationCallback(cb3);
128
131 dev1->GetMac()->SetMcpsDataIndicationCallback(cb4);
132
135 dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
136
137 //////////// Manual device association ////////////////////
138 // Note: We manually associate the devices to a PAN coordinator
139 // (i.e. bootstrap is not used);
140 // The PAN COORDINATOR does not need to associate or set its
141 // PAN Id or its own coordinator id, these are set
142 // by the MLME-start.request primitive when used.
143
144 dev1->GetMac()->SetPanId(5);
145 dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
146
147 ///////////////////// Start transmitting beacons from coordinator ////////////////////////
148
150 params.m_panCoor = true;
151 params.m_PanId = 5;
152 params.m_bcnOrd = 14;
153 params.m_sfrmOrd = 6;
155 Seconds(2.0),
157 dev0->GetMac(),
158 params);
159
160 ///////////////////// Transmission of data Packets from end device //////////////////////
161
163 McpsDataRequestParams params2;
164 params2.m_dstPanId = 5;
165 params2.m_srcAddrMode = SHORT_ADDR;
166 params2.m_dstAddrMode = SHORT_ADDR;
167 params2.m_dstAddr = Mac16Address("00:01");
168 params2.m_msduHandle = 0;
169 // params2.m_txOptions = TX_OPTION_ACK; // Enable direct transmission with Ack
170
171 /////////////////////////////////////////////////////////////////////////////////////
172 // Examples of time parameters for transmissions in the first incoming superframe. //
173 /////////////////////////////////////////////////////////////////////////////////////
174
175 // 2.981 sec No time to finish CCA in CAP, the transmission at this time will cause
176 // the packet to be deferred to the next superframe.
177
178 // 2.982 sec No time to finish random backoff delay in CAP, the transmission at this
179 // time will cause the packet to be deferred to the next superframe.
180
181 // 2.93 sec Enough time, the packet can be transmitted within the CAP of the first
182 // superframe
183
184 // MCPS-DATA.request Beacon enabled Direct Transmission (dev1)
185 // Frame transmission from End Device to Coordinator (Direct transmission)
187 Seconds(2.93),
189 dev1->GetMac(),
190 params2,
191 p1);
192
195
197 return 0;
198}
helps to manage and create IEEE 802.15.4 NetDevice objects
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
void SetPropagationDelayModel(std::string name, Ts &&... args)
void AddPropagationLossModel(std::string name, Ts &&... args)
This class can contain 16 bit addresses.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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:577
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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...
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.
Network layer to device interface.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
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:1308
NodeContainer nodes
void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
void BeaconIndication(MlmeBeaconNotifyIndicationParams params)
void TransEndIndication(McpsDataConfirmParams params)
void StartConfirm(MlmeStartConfirmParams params)
void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
devices
Definition first.py:31
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
@ 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
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.
MLME-BEACON-NOTIFY.indication params.