A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-active-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/*
21 * [00:01] [00:02] [00:03]
22 * PAN Coordinator 1 (PAN: 5) End Device PAN Coordinator 2 (PAN: 7)
23 * |--------100 m----------------|----------106 m -----------------------|
24 * Channel 12 (Active Scan channels 11-14) Channel 14
25 *
26 *
27 * This example demonstrate the usage of the MAC MLME-SCAN.request (ACTIVE scan) primitive as
28 * described by IEEE 802.15.4-2011.
29 * At the beginning of the simulation, PAN coordinators are set to
30 * non-beacon enabled mode and wait for any beacon requests.
31 *
32 * The end device initiate an Active scan where a beacon request command is transmitted on
33 * on each channel. If a beacon coordinator is present and in range in the channel, it responds with
34 * a beacon which contains the PAN descriptor with useful information for the association process
35 * (channel number, Pan ID, coord address, link quality indicator).
36 *
37 * LQI range: 0 - 255
38 * Where 255 is the MAX possible value used to described how clearly the packet was heard.
39 * Typically, a value below 127 is considered a link with poor quality.
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
54using namespace ns3;
55
56static void
58{
59 if (params.m_status == LrWpanMacStatus::SUCCESS)
60 {
61 std::cout << Simulator::Now().As(Time::S) << "| Active scan status SUCCESSFUL (Completed)"
62 << "\n";
63 if (!params.m_panDescList.empty())
64 {
65 std::cout << "Device [" << device->GetMac()->GetShortAddress()
66 << "] found the following PANs:\n";
67 for (long unsigned int i = 0; i < params.m_panDescList.size(); i++)
68 {
69 std::cout << "PAN DESCRIPTOR " << i << ":\n"
70 << "Pan Id: " << params.m_panDescList[i].m_coorPanId
71 << "\nChannel: " << static_cast<uint32_t>(params.m_panDescList[i].m_logCh)
72 << "\nLQI: "
73 << static_cast<uint32_t>(params.m_panDescList[i].m_linkQuality)
74 << "\nCoordinator Short Address: "
75 << params.m_panDescList[i].m_coorShortAddr << "\n\n";
76 }
77 }
78 else
79 {
80 std::cout << "No PANs found (Could not find any beacons)\n";
81 }
82 }
83 else
84 {
85 std::cout << "Something went wrong, scan could not be completed\n";
86 }
87}
88
89int
90main(int argc, char* argv[])
91{
93
94 // Create 2 PAN coordinator nodes, and 1 end device
95 Ptr<Node> coord1 = CreateObject<Node>();
96 Ptr<Node> endNode = CreateObject<Node>();
97 Ptr<Node> coord2 = CreateObject<Node>();
98
99 Ptr<LrWpanNetDevice> coord1NetDevice = CreateObject<LrWpanNetDevice>();
100 Ptr<LrWpanNetDevice> endNodeNetDevice = CreateObject<LrWpanNetDevice>();
101 Ptr<LrWpanNetDevice> coord2NetDevice = CreateObject<LrWpanNetDevice>();
102
103 coord1NetDevice->SetAddress(Mac16Address("00:01"));
104 endNodeNetDevice->SetAddress(Mac16Address("00:02"));
105 coord2NetDevice->SetAddress(Mac16Address("00:03"));
106
107 // Configure Spectrum channel
108 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel>();
110 CreateObject<LogDistancePropagationLossModel>();
112 CreateObject<ConstantSpeedPropagationDelayModel>();
113 channel->AddPropagationLossModel(propModel);
114 channel->SetPropagationDelayModel(delayModel);
115
116 coord1NetDevice->SetChannel(channel);
117 endNodeNetDevice->SetChannel(channel);
118 coord2NetDevice->SetChannel(channel);
119
120 coord1->AddDevice(coord1NetDevice);
121 endNode->AddDevice(endNodeNetDevice);
122 coord2->AddDevice(coord2NetDevice);
123
124 // Mobility
126 CreateObject<ConstantPositionMobilityModel>();
127 coord1Mobility->SetPosition(Vector(0, 0, 0));
128 coord1NetDevice->GetPhy()->SetMobility(coord1Mobility);
129
130 Ptr<ConstantPositionMobilityModel> endNodeMobility =
131 CreateObject<ConstantPositionMobilityModel>();
132 endNodeMobility->SetPosition(Vector(100, 0, 0));
133 endNodeNetDevice->GetPhy()->SetMobility(endNodeMobility);
134
136 CreateObject<ConstantPositionMobilityModel>();
137 coord2Mobility->SetPosition(Vector(206, 0, 0));
138 coord2NetDevice->GetPhy()->SetMobility(coord2Mobility);
139
140 // MAC layer Callbacks hooks
141 endNodeNetDevice->GetMac()->SetMlmeScanConfirmCallback(
142 MakeBoundCallback(&ScanConfirm, endNodeNetDevice));
143
144 /////////////////
145 // ACTIVE SCAN //
146 /////////////////
147
148 // PAN coordinator N0 (PAN 5) is set to channel 12 in non-beacon mode but answer to beacon
149 // requests.
151 params.m_panCoor = true;
152 params.m_PanId = 5;
153 params.m_bcnOrd = 15;
154 params.m_sfrmOrd = 15;
155 params.m_logCh = 12;
157 Seconds(2.0),
159 coord1NetDevice->GetMac(),
160 params);
161
162 // PAN coordinator N2 (PAN 7) is set to channel 14 in non-beacon mode but answer to beacon
163 // requests.
165 params2.m_panCoor = true;
166 params2.m_PanId = 7;
167 params2.m_bcnOrd = 15;
168 params2.m_sfrmOrd = 15;
169 params2.m_logCh = 14;
171 Seconds(2.0),
173 coord2NetDevice->GetMac(),
174 params2);
175
176 // End device N1 broadcast a single BEACON REQUEST for each channel (11, 12, 13, and 14).
177 // If a coordinator is present and in range, it will respond with a beacon broadcast.
178 // Scan Channels are represented by bits 0-26 (27 LSB)
179 // ch 14 ch 11
180 // | |
181 // 0x7800 = 0000000000000000111100000000000
182 MlmeScanRequestParams scanParams;
183 scanParams.m_chPage = 0;
184 scanParams.m_scanChannels = 0x7800;
185 scanParams.m_scanDuration = 14;
186 scanParams.m_scanType = MLMESCAN_ACTIVE;
188 Seconds(3.0),
190 endNodeNetDevice->GetMac(),
191 scanParams);
192
195
197 return 0;
198}
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
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
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
@ MLMESCAN_ACTIVE
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:765
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static void ScanConfirm(Ptr< LrWpanNetDevice > device, MlmeScanConfirmParams params)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
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 ##.
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.