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 * 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
47using namespace ns3;
48
49void
51{
52 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Received BEACON packet of size ");
53}
54
55void
57{
58 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
59 << " secs | Received DATA packet of size " << p->GetSize());
60}
61
62void
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
73void
75{
76 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
77 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
78}
79
80void
82{
83 if (params.m_status == LrWpanMacStatus::SUCCESS)
84 {
85 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "Beacon status SUCCESSFUL");
86 }
87}
88
89int
90main(int argc, char* argv[])
91{
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
123 ///////////////// Mobility ///////////////////////
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
134 /////// MAC layer Callbacks hooks/////////////
135
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
156 //////////// Manual device association ////////////////////
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
166 ///////////////////// Start transmitting beacons from coordinator ////////////////////////
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
179 ///////////////////// Transmission of data Packets from end device //////////////////////
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
190 /////////////////////////////////////////////////////////////////////////////////////
191 // Examples of time parameters for transmissions in the first incoming superframe. //
192 /////////////////////////////////////////////////////////////////////////////////////
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
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
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
@ 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
ns channel
Definition: third.py:88
FtrParams 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.