A Discrete-Event Network Simulator
API
lr-wpan-ed-scan.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Tokushima University, 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 <alramonet@is.tokushima-u.ac.jp>
18  */
19 
20 /* [00:01] [00:02] [00:03]
21  * PAN 5 Coordinator End Device PAN 7 Coordinator
22  * N0 (dev0) ----------------N1 (dev1) ------------------------------ N2 (dev2)
23  * Channel 12 ED Scan Channels 11-14 Channel 14
24  *
25  * |--------10 m----------------|----------30 m -----------------------|
26  *
27  * This example demonstrate the usage of the MAC ED Scan primitive as
28  * described by IEEE 802.15.4-2011.
29  *
30  * At the beginning of the simulation, PAN coordinators N0 (Channel 12) and N2 (Channel 14)
31  * start transmitting beacon frames on their respective channels. At the same time,
32  * end device N1 will scan through channels (11,12,13 and 14) looking for energy.
33  * i.e. multiple energy scans on each channel (PLME-ED calls). The results of the Max energy
34  * read registered for each channel is shown after the last channel scan is finished.
35  *
36  * The radio uses the default Sensibility and Rx Power provided by the
37  * LogDistancePropagationLossModel. The simulation might take a few seconds to complete.
38  *
39  * ED range: 0 - 255
40  */
41 
42 #include <ns3/constant-position-mobility-model.h>
43 #include <ns3/core-module.h>
44 #include <ns3/log.h>
45 #include <ns3/lr-wpan-module.h>
46 #include <ns3/packet.h>
47 #include <ns3/propagation-delay-model.h>
48 #include <ns3/propagation-loss-model.h>
49 #include <ns3/simulator.h>
50 #include <ns3/single-model-spectrum-channel.h>
51 
52 #include <iostream>
53 
54 using namespace ns3;
55 
56 static void
58 {
59  if (params.m_status == LrWpanMacStatus::SUCCESS && params.m_scanType == MLMESCAN_ED)
60  {
61  std::cout << Simulator::Now().As(Time::S) << "| Scan status SUCCESSFUL\n";
62  std::cout << "Results for Energy Scan:"
63  << "\nPage: " << params.m_chPage << "\n";
64  for (std::size_t i = 0; i < params.m_energyDetList.size(); i++)
65  {
66  std::cout << "Channel " << static_cast<uint32_t>(i + 11) << ": "
67  << +params.m_energyDetList[i] << "\n";
68  }
69  }
70 }
71 
72 int
73 main(int argc, char* argv[])
74 {
76 
77  // Create 2 PAN coordinator nodes, and 1 end device
78  Ptr<Node> n0 = CreateObject<Node>();
79  Ptr<Node> n1 = CreateObject<Node>();
80  Ptr<Node> n2 = CreateObject<Node>();
81 
82  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice>();
83  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice>();
84  Ptr<LrWpanNetDevice> dev2 = CreateObject<LrWpanNetDevice>();
85 
86  dev0->SetAddress(Mac16Address("00:01"));
87  dev1->SetAddress(Mac16Address("00:02"));
88  dev2->SetAddress(Mac16Address("00:03"));
89 
90  // Configure Spectrum channel
91  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
93  CreateObject<LogDistancePropagationLossModel>();
95  CreateObject<ConstantSpeedPropagationDelayModel>();
96  channel->AddPropagationLossModel(propModel);
97  channel->SetPropagationDelayModel(delayModel);
98 
99  dev0->SetChannel(channel);
100  dev1->SetChannel(channel);
101  dev2->SetChannel(channel);
102 
103  n0->AddDevice(dev0);
104  n1->AddDevice(dev1);
105  n2->AddDevice(dev2);
106 
107  // MAC layer Callbacks hooks
109  scb = MakeCallback(&ScanConfirm);
110  dev1->GetMac()->SetMlmeScanConfirmCallback(scb);
111 
112  Ptr<ConstantPositionMobilityModel> PanCoordinatorN0Mobility =
113  CreateObject<ConstantPositionMobilityModel>();
114  PanCoordinatorN0Mobility->SetPosition(Vector(0, 0, 0));
115  dev0->GetPhy()->SetMobility(PanCoordinatorN0Mobility);
116 
117  Ptr<ConstantPositionMobilityModel> endDeviceN1Mobility =
118  CreateObject<ConstantPositionMobilityModel>();
119  endDeviceN1Mobility->SetPosition(Vector(10, 0, 0));
120  dev1->GetPhy()->SetMobility(endDeviceN1Mobility);
121 
122  Ptr<ConstantPositionMobilityModel> PanCoordinatorN2Mobility =
123  CreateObject<ConstantPositionMobilityModel>();
124  PanCoordinatorN2Mobility->SetPosition(Vector(40, 0, 0));
125  dev2->GetPhy()->SetMobility(PanCoordinatorN2Mobility);
126 
127  // PAN coordinator N0 starts transmit beacons on channel 12
129  params.m_panCoor = true;
130  params.m_PanId = 5;
131  params.m_bcnOrd = 3;
132  params.m_sfrmOrd = 3;
133  params.m_logCh = 12;
135  Seconds(2.0),
137  dev0->GetMac(),
138  params);
139 
140  // PAN coordinator N2 transmit beacons on channel 14
141  MlmeStartRequestParams params2;
142  params2.m_panCoor = true;
143  params2.m_PanId = 7;
144  params2.m_bcnOrd = 3;
145  params2.m_sfrmOrd = 3;
146  params2.m_logCh = 14;
148  Seconds(2.0),
150  dev2->GetMac(),
151  params2);
152 
153  // Device 1 initiate channels scan on channels 11, 12, 13, and 14 looking for energy
154  // Scan Channels represented by bits 0-26 (27 LSB)
155  // ch 14 ch 11
156  // | |
157  // 0x7800 = 0000000000000000111100000000000
158  // scanDuration per channel = aBaseSuperframeDuration * (2^14+1) = 251.65 secs
159  MlmeScanRequestParams scanParams;
160  scanParams.m_chPage = 0;
161  scanParams.m_scanChannels = 0x7800;
162  scanParams.m_scanDuration = 14;
163  scanParams.m_scanType = MLMESCAN_ED;
165  Seconds(2.0),
167  dev1->GetMac(),
168  scanParams);
169 
170  Simulator::Stop(Seconds(2000));
171  Simulator::Run();
172 
174  return 0;
175 }
void MlmeScanRequest(MlmeScanRequestParams params) override
IEEE 802.15.4-2011, section 6.2.10.1 MLME-SCAN.request Request primitive used to initiate a channel s...
Definition: lr-wpan-mac.cc:623
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
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ S
second
Definition: nstime.h:116
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static void ScanConfirm(MlmeScanConfirmParams params)
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:704
@ SUCCESS
Definition: ff-mac-common.h:62
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ 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
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 ##.
MLME-SCAN.confirm params.
MLME-SCAN.request params.
uint32_t m_scanChannels
The channel numbers to be scanned.
uint32_t m_chPage
The channel page on which to perform scan.
uint8_t m_scanDuration
The factor (0-14) used to calculate the length of time to spend scanning.
LrWpanMlmeScanType m_scanType
Indicates the type of scan performed as described in IEEE 802.15.4-2011 (5.1.2.1).
MLME-START.request params.
uint8_t m_logCh
Logical channel on which to start using the new superframe configuration.
bool m_panCoor
On true this device will become coordinator.
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
uint16_t m_PanId
Pan Identifier used by the device.
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.