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
47using namespace ns3;
48
49static void
51{
52 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
53 << " secs | Received BEACON packet of size " << p->GetSize());
54}
55
56static void
58{
59 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
60 << " secs | Received DATA packet of size " << p->GetSize());
61}
62
63static void
65{
66 // In the case of transmissions with the Ack flag activated, the transaction is only
67 // successful if the Ack was received.
69 {
70 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << " secs | Transmission successfully sent");
71 }
72}
73
74static void
76{
77 NS_LOG_UNCOND(Simulator::Now().GetSeconds()
78 << "s Coordinator Received DATA packet (size " << p->GetSize() << " bytes)");
79}
80
81static void
83{
84 if (params.m_status == MLMESTART_SUCCESS)
85 {
86 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "Beacon status SUCESSFUL");
87 }
88}
89
90int
91main(int argc, char* argv[])
92{
96 LogComponentEnable("LrWpanCsmaCa", LOG_LEVEL_INFO);
97
98 LrWpanHelper lrWpanHelper;
99
100 // Create 2 nodes, and a NetDevice for each one
101 Ptr<Node> n0 = CreateObject<Node>();
102 Ptr<Node> n1 = CreateObject<Node>();
103
104 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
105 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
106
107 dev0->SetAddress(Mac16Address("00:01"));
108 dev1->SetAddress(Mac16Address("00:02"));
109
110 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
112 CreateObject<LogDistancePropagationLossModel>();
114 CreateObject<ConstantSpeedPropagationDelayModel>();
115 channel->AddPropagationLossModel(propModel);
116 channel->SetPropagationDelayModel(delayModel);
117
118 dev0->SetChannel(channel);
119 dev1->SetChannel(channel);
120
121 n0->AddDevice(dev0);
122 n1->AddDevice(dev1);
123
125 Ptr<ConstantPositionMobilityModel> sender0Mobility =
126 CreateObject<ConstantPositionMobilityModel>();
127 sender0Mobility->SetPosition(Vector(0, 0, 0));
128 dev0->GetPhy()->SetMobility(sender0Mobility);
129 Ptr<ConstantPositionMobilityModel> sender1Mobility =
130 CreateObject<ConstantPositionMobilityModel>();
131
132 sender1Mobility->SetPosition(Vector(0, 10, 0)); // 10 m distance
133 dev1->GetPhy()->SetMobility(sender1Mobility);
134
136
139 dev0->GetMac()->SetMlmeStartConfirmCallback(cb0);
140
143 dev1->GetMac()->SetMcpsDataConfirmCallback(cb1);
144
147 dev1->GetMac()->SetMlmeBeaconNotifyIndicationCallback(cb3);
148
151 dev1->GetMac()->SetMcpsDataIndicationCallback(cb4);
152
155 dev0->GetMac()->SetMcpsDataIndicationCallback(cb5);
156
158 // Note: We manually associate the devices to a PAN coordinator
159 // because currently there is no automatic association behavior (bootstrap);
160 // The PAN COORDINATOR does not need to associate or set its
161 // PAN Id or its own coordinator id, these are set
162 // by the MLME-start.request primitive when used.
163
164 dev1->GetMac()->SetPanId(5);
165 dev1->GetMac()->SetAssociatedCoor(Mac16Address("00:01"));
166
168
170 params.m_panCoor = true;
171 params.m_PanId = 5;
172 params.m_bcnOrd = 14;
173 params.m_sfrmOrd = 6;
174 Simulator::ScheduleWithContext(1,
175 Seconds(2.0),
176 &LrWpanMac::MlmeStartRequest,
177 dev0->GetMac(),
178 params);
179
181
182 Ptr<Packet> p1 = Create<Packet>(5);
183 McpsDataRequestParams params2;
184 params2.m_dstPanId = 5;
185 params2.m_srcAddrMode = SHORT_ADDR;
186 params2.m_dstAddrMode = SHORT_ADDR;
187 params2.m_dstAddr = Mac16Address("00:01");
188 params2.m_msduHandle = 0;
189 // params2.m_txOptions = TX_OPTION_ACK; // Enable direct transmission with Ack
190
192 // Examples of time parameters for transmissions in the first incoming superframe. //
194
195 // 2.981 sec No time to finish CCA in CAP, the transmission at this time will cause
196 // the packet to be deferred to the next superframe.
197
198 // 2.982 sec No time to finish random backoff delay in CAP, the transmission at this
199 // time will cause the packet to be deferred to the next superframe.
200
201 // 2.93 sec Enough time, the packet can be transmitted within the CAP of the first
202 // superframe
203
204 // MCPS-DATA.request Beacon enabled Direct Transmission (dev1)
205 // Frame transmission from End Device to Coordinator (Direct transmission)
206 Simulator::ScheduleWithContext(1,
207 Seconds(2.93),
208 &LrWpanMac::McpsDataRequest,
209 dev1->GetMac(),
210 params2,
211 p1);
212
213 Simulator::Stop(Seconds(600));
214 Simulator::Run();
215
216 Simulator::Destroy();
217 return 0;
218}
helps to manage and create IEEE 802.15.4 NetDevice objects
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:863
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
@ IEEE_802_15_4_SUCCESS
Definition: lr-wpan-mac.h:194
@ MLMESTART_SUCCESS
Definition: lr-wpan-mac.h:215
@ SHORT_ADDR
Definition: lr-wpan-mac.h:156
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:75
static void StartConfirm(MlmeStartConfirmParams params)
Definition: lr-wpan-mlme.cc:82
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:57
static void TransEndIndication(McpsDataConfirmParams params)
Definition: lr-wpan-mlme.cc:64
static void BeaconIndication(MlmeBeaconNotifyIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:691
@ 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:107
void LogComponentEnable(const char *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:358
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:380
channel
Definition: third.py:81
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:361
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:363
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:373
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:345
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:346
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:347
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:348
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:349
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:351
MLME-BEACON-NOTIFY.indication params.
Definition: lr-wpan-mac.h:547
MLME-START.confirm params.
Definition: lr-wpan-mac.h:536
LrWpanMlmeStartConfirmStatus m_status
The status of a MLME-start.request.
Definition: lr-wpan-mac.h:537
MLME-START.request params.
Definition: lr-wpan-mac.h:418
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:429
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
Definition: lr-wpan-mac.h:426
uint16_t m_PanId
Pan Identifier used by the device.
Definition: lr-wpan-mac.h:419
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:428