A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-orphan-scan.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 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:00:00:00:00:00:00:01 | 00:01] [00:00:00:00:00:00:00:02 | ff:ff]
11 * PAN Coordinator 1 (PAN: 5) End Device
12 * |---------------------100m-----------------------|
13 * Channel 12 (Orphan Scan channels 11-14)
14 *
15 *
16 * This example demonstrate the usage of the MAC MLME-SCAN.request (ORPHAN scan) primitive as
17 * described by IEEE 802.15.4-2011 (See Figures 14 and 15).
18 *
19 * Orphan scan is used typically on devices as a result of repeated communication failures
20 * (For example, lossing too many ACK). An orphan scan represents the attempt of a device to
21 * relocate its coordinator. In some situations, it can be used by devices higher layers to not only
22 * rejoin a network but also join a network for the first time (Like in the joining through
23 * orphaning mechanism described in Zigbee networks).
24 *
25 * In this example, the end device is set to scan 4 channels (11~14) for a period of
26 * macResponseWaitTime until it finally gets in contact with the coordinator.
27 * On contact, the coordinator responds to the device (via coordinator realignment command)
28 * an assign it a short address. The detailed sequence of events is as following:
29 *
30 * 1) [Time 2s] The coordinator start a network in channel 12.
31 * 2) [Time 3s] The end device start orphan scan and transmits a orphan
32 * notification cmd on channel 11.
33 * 3) No response is received in channel 11, therefore, the device ends scanning on
34 * channel 11 after macResponseWaitTime and repeats step 2 in channel 12.
35 * 4) [Time 3.00269s] The orphan notification command is received by the coordinator in
36 * channel 12. The coordinator verify the requesting device and replies to the device
37 * with a coordinator realignment command containing the assigned short address [DE:AF].
38 * 5) [Time 3.00646s] The device receives the coordinator realignment command, update its
39 * macPanId, macShortAddress, macCoordShortAddress and macCoordExtAddress.
40 * 6) Scanning of the remaining channels 13 and 14 is cancelled.
41 */
42
43#include <ns3/constant-position-mobility-model.h>
44#include <ns3/core-module.h>
45#include <ns3/log.h>
46#include <ns3/lr-wpan-module.h>
47#include <ns3/packet.h>
48#include <ns3/propagation-delay-model.h>
49#include <ns3/propagation-loss-model.h>
50#include <ns3/simulator.h>
51#include <ns3/single-model-spectrum-channel.h>
52
53#include <iostream>
54
55using namespace ns3;
56using namespace ns3::lrwpan;
57
58static void
60{
61 if (params.m_status == MacStatus::SUCCESS)
62 {
63 std::cout << Simulator::Now().As(Time::S) << " Node " << device->GetNode()->GetId() << " ["
64 << device->GetMac()->GetShortAddress() << " | "
65 << device->GetMac()->GetExtendedAddress()
66 << "] MLME-SCAN.confirm: Active scan status SUCCESSFUL "
67 << "(Coordinator found and address assigned) \n";
68 }
69 else if (params.m_status == MacStatus::NO_BEACON)
70 {
71 std::cout << Simulator::Now().As(Time::S) << " Node " << device->GetNode()->GetId() << " ["
72 << device->GetMac()->GetShortAddress() << " | "
73 << device->GetMac()->GetExtendedAddress()
74 << "] MLME-SCAN.confirm: Could not locate coordinator "
75 << "(Coord realignment command not received) "
76 << "status: " << params.m_status << "\n";
77 }
78 else
79 {
80 std::cout << Simulator::Now().As(Time::S) << " Node " << device->GetNode()->GetId() << " ["
81 << device->GetMac()->GetShortAddress() << " | "
82 << device->GetMac()->GetExtendedAddress()
83 << "] MLME-SCAN.confirm: An error occurred during scanning, "
84 << "status: " << params.m_status << "\n";
85 }
86}
87
88static void
90{
91 // The steps taken by the coordinator on the event of an orphan indication
92 // are meant to be implemented by the next higher layer and are out of the scope of the
93 // standard. In this example, we simply accept the request , assign a fixed short address
94 // [DE:AF] and respond to the requesting device using a MLME-ORPHAN.response.
95
96 std::cout << Simulator::Now().As(Time::S) << " Node " << device->GetNode()->GetId() << " ["
97 << device->GetMac()->GetShortAddress() << " | "
98 << device->GetMac()->GetExtendedAddress()
99 << "] MLME-ORPHAN.indication: Orphan Notification received, processing...\n";
100
101 MlmeOrphanResponseParams respParams;
102 respParams.m_assocMember = true;
103 respParams.m_orphanAddr = params.m_orphanAddr;
104 respParams.m_shortAddr = Mac16Address("DE:AF");
105
106 Simulator::ScheduleNow(&LrWpanMac::MlmeOrphanResponse, device->GetMac(), respParams);
107}
108
109int
110main(int argc, char* argv[])
111{
113
114 // Create 2 PAN coordinator nodes, and 1 end device
115 Ptr<Node> coord1 = CreateObject<Node>();
116 Ptr<Node> endNode = CreateObject<Node>();
117
120
121 // PAN Coordinators configurations require to set both, the EUI-64 (extended address)
122 // and to assign their own short address.
123 coord1NetDevice->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:01"));
124 coord1NetDevice->GetMac()->SetShortAddress(Mac16Address("00:01"));
125
126 // Other devices must have only its EUI-64 and later on, their short address is
127 // potentially assigned by the coordinator.
128 endNodeNetDevice->GetMac()->SetExtendedAddress(Mac64Address("00:00:00:00:00:00:00:02"));
129
130 // Configure Spectrum channel
136 channel->AddPropagationLossModel(propModel);
137 channel->SetPropagationDelayModel(delayModel);
138
139 coord1NetDevice->SetChannel(channel);
140 endNodeNetDevice->SetChannel(channel);
141
142 coord1->AddDevice(coord1NetDevice);
143 endNode->AddDevice(endNodeNetDevice);
144
145 // Mobility
148 coord1Mobility->SetPosition(Vector(0, 0, 0));
149 coord1NetDevice->GetPhy()->SetMobility(coord1Mobility);
150
151 Ptr<ConstantPositionMobilityModel> endNodeMobility =
153 endNodeMobility->SetPosition(Vector(100, 0, 0));
154 endNodeNetDevice->GetPhy()->SetMobility(endNodeMobility);
155
156 // MAC layer Callbacks hooks
157 endNodeNetDevice->GetMac()->SetMlmeScanConfirmCallback(
158 MakeBoundCallback(&ScanConfirm, endNodeNetDevice));
159
160 coord1NetDevice->GetMac()->SetMlmeOrphanIndicationCallback(
161 MakeBoundCallback(&OrphanIndication, coord1NetDevice));
162
163 /////////////////
164 // ORPHAN SCAN //
165 /////////////////
166
167 // PAN coordinator N0 (PAN 5) is set to channel 12 in non-beacon mode
168 // but answer to beacon request and orphan notification commands.
170 params.m_panCoor = true;
171 params.m_PanId = 5;
172 params.m_bcnOrd = 15;
173 params.m_sfrmOrd = 15;
174 params.m_logCh = 12;
176 Seconds(2.0),
178 coord1NetDevice->GetMac(),
179 params);
180
181 // End device N1 is set to scan 4 channels looking for the presence of a coordinator.
182 // On each channel, a single orphan notification command is sent and a response is
183 // waited for a maximum time of macResponseWaitTime. If a reply is received from a
184 // coordinator within this time (coordinator realignment command), the programmed scans on
185 // other channels is suspended.
186 // Scan Channels are represented by bits 0-26 (27 LSB)
187 // ch 14 ch 11
188 // | |
189 // 0x7800 = 0000000000000000111100000000000
190 MlmeScanRequestParams scanParams;
191 scanParams.m_chPage = 0;
192 scanParams.m_scanChannels = 0x7800;
193 scanParams.m_scanType = MLMESCAN_ORPHAN;
195 Seconds(3.0),
197 endNodeNetDevice->GetMac(),
198 scanParams);
199
202
204 return 0;
205}
This class can contain 16 bit addresses.
an EUI-64 address
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 EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
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 MlmeOrphanResponse(MlmeOrphanResponseParams params) override
IEEE 802.15.4-2011, section 6.2.7.2 MLME-ORPHAN.response Primitive used to initiatte a response to an...
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 OrphanIndication(Ptr< LrWpanNetDevice > device, MlmeOrphanIndicationParams params)
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 ##.
MLME-ORPHAN.indication params.
bool m_assocMember
T = allocated with this coord | F = otherwise.
Mac64Address m_orphanAddr
The address of the orphaned device.
Mac16Address m_shortAddr
The short address allocated.
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.
uint32_t m_chPage
The channel page on which to perform scan.