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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
7 */
8
9/*
10 * [00:01] [00:02] [00:03]
11 * PAN Coordinator 1 (PAN: 5) End Device PAN Coordinator 2 (PAN: 7)
12 * |--------100 m----------------|----------106 m -----------------------|
13 * Channel 12 (Active Scan channels 11-14) Channel 14
14 *
15 *
16 * This example demonstrate the usage of the MAC MLME-SCAN.request (ACTIVE scan) primitive as
17 * described by IEEE 802.15.4-2011.
18 * At the beginning of the simulation, PAN coordinators are set to
19 * non-beacon enabled mode and wait for any beacon requests.
20 *
21 * The end device initiate an Active scan where a beacon request command is transmitted on
22 * on each channel. If a beacon coordinator is present and in range in the channel, it responds with
23 * a beacon which contains the PAN descriptor with useful information for the association process
24 * (channel number, Pan ID, coord address, link quality indicator).
25 *
26 * LQI range: 0 - 255
27 * Where 255 is the MAX possible value used to described how clearly the packet was heard.
28 * Typically, a value below 127 is considered a link with poor quality.
29 */
30
31#include <ns3/constant-position-mobility-model.h>
32#include <ns3/core-module.h>
33#include <ns3/log.h>
34#include <ns3/lr-wpan-module.h>
35#include <ns3/packet.h>
36#include <ns3/propagation-delay-model.h>
37#include <ns3/propagation-loss-model.h>
38#include <ns3/simulator.h>
39#include <ns3/single-model-spectrum-channel.h>
40
41#include <iostream>
42
43using namespace ns3;
44using namespace ns3::lrwpan;
45
46static void
48{
49 if (params.m_status == MacStatus::SUCCESS)
50 {
51 std::cout << Simulator::Now().As(Time::S) << "| Active scan status SUCCESSFUL (Completed)"
52 << "\n";
53 if (!params.m_panDescList.empty())
54 {
55 std::cout << "Device [" << device->GetMac()->GetShortAddress()
56 << "] found the following PANs:\n";
57 for (long unsigned int i = 0; i < params.m_panDescList.size(); i++)
58 {
59 std::cout << "PAN DESCRIPTOR " << i << ":\n"
60 << "Pan Id: " << params.m_panDescList[i].m_coorPanId
61 << "\nChannel: " << static_cast<uint32_t>(params.m_panDescList[i].m_logCh)
62 << "\nLQI: "
63 << static_cast<uint32_t>(params.m_panDescList[i].m_linkQuality)
64 << "\nCoordinator Short Address: "
65 << params.m_panDescList[i].m_coorShortAddr << "\n\n";
66 }
67 }
68 else
69 {
70 std::cout << "No PANs found (Could not find any beacons)\n";
71 }
72 }
73 else
74 {
75 std::cout << "Something went wrong, scan could not be completed\n";
76 }
77}
78
79int
80main(int argc, char* argv[])
81{
83
84 // Create 2 PAN coordinator nodes, and 1 end device
86 Ptr<Node> endNode = CreateObject<Node>();
88
92
93 coord1NetDevice->SetAddress(Mac16Address("00:01"));
94 endNodeNetDevice->SetAddress(Mac16Address("00:02"));
95 coord2NetDevice->SetAddress(Mac16Address("00:03"));
96
97 // Configure Spectrum channel
103 channel->AddPropagationLossModel(propModel);
104 channel->SetPropagationDelayModel(delayModel);
105
106 coord1NetDevice->SetChannel(channel);
107 endNodeNetDevice->SetChannel(channel);
108 coord2NetDevice->SetChannel(channel);
109
110 coord1->AddDevice(coord1NetDevice);
111 endNode->AddDevice(endNodeNetDevice);
112 coord2->AddDevice(coord2NetDevice);
113
114 // Mobility
117 coord1Mobility->SetPosition(Vector(0, 0, 0));
118 coord1NetDevice->GetPhy()->SetMobility(coord1Mobility);
119
120 Ptr<ConstantPositionMobilityModel> endNodeMobility =
122 endNodeMobility->SetPosition(Vector(100, 0, 0));
123 endNodeNetDevice->GetPhy()->SetMobility(endNodeMobility);
124
127 coord2Mobility->SetPosition(Vector(206, 0, 0));
128 coord2NetDevice->GetPhy()->SetMobility(coord2Mobility);
129
130 // MAC layer Callbacks hooks
131 endNodeNetDevice->GetMac()->SetMlmeScanConfirmCallback(
132 MakeBoundCallback(&ScanConfirm, endNodeNetDevice));
133
134 /////////////////
135 // ACTIVE SCAN //
136 /////////////////
137
138 // PAN coordinator N0 (PAN 5) is set to channel 12 in non-beacon mode but answer to beacon
139 // requests.
141 params.m_panCoor = true;
142 params.m_PanId = 5;
143 params.m_bcnOrd = 15;
144 params.m_sfrmOrd = 15;
145 params.m_logCh = 12;
147 Seconds(2.0),
149 coord1NetDevice->GetMac(),
150 params);
151
152 // PAN coordinator N2 (PAN 7) is set to channel 14 in non-beacon mode but answer to beacon
153 // requests.
155 params2.m_panCoor = true;
156 params2.m_PanId = 7;
157 params2.m_bcnOrd = 15;
158 params2.m_sfrmOrd = 15;
159 params2.m_logCh = 14;
161 Seconds(2.0),
163 coord2NetDevice->GetMac(),
164 params2);
165
166 // End device N1 broadcast a single BEACON REQUEST for each channel (11, 12, 13, and 14).
167 // If a coordinator is present and in range, it will respond with a beacon broadcast.
168 // Scan Channels are represented by bits 0-26 (27 LSB)
169 // ch 14 ch 11
170 // | |
171 // 0x7800 = 0000000000000000111100000000000
172 MlmeScanRequestParams scanParams;
173 scanParams.m_chPage = 0;
174 scanParams.m_scanChannels = 0x7800;
175 scanParams.m_scanDuration = 14;
176 scanParams.m_scanType = MLMESCAN_ACTIVE;
178 Seconds(3.0),
180 endNodeNetDevice->GetMac(),
181 scanParams);
182
185
187 return 0;
188}
This class can contain 16 bit addresses.
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
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 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...
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:83
@ 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
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
channel
Definition third.py:77
params
Fit Fluctuating Two Ray model to the 3GPP TR 38.901 using the Anderson-Darling goodness-of-fit ##.
MlmeScanType m_scanType
Indicates the type of scan performed as described in IEEE 802.15.4-2011 (5.1.2.1).
uint32_t m_scanChannels
The channel numbers to be scanned.
uint8_t m_scanDuration
The factor (0-14) used to calculate the length of time to spend scanning.
uint32_t m_chPage
The channel page on which to perform scan.
uint8_t m_logCh
Logical channel on which to start using the new superframe configuration.
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
bool m_panCoor
On true this device will become coordinator.
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
uint16_t m_PanId
Pan Identifier used by the device.