A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-nwk-direct-join.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Tokushima University, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors:
7 *
8 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
9 */
10
11/**
12 * This example shows the procedure to form a multi-hop network using a direct-join
13 * procedure (a.k.a. orphaning procedure). The procedure requires a sequence of primitive
14 * calls on a specific order in the indicated devices
15 *
16 *
17 * 1-Network-Formation 3-Join(dev1) 6-Join(dev2)
18 * 2-Direct-Join (dev1) 4-Router-Start(dev1)
19 * 5-Direct-Join (dev2)
20 *
21 * Zigbee Coordinator(ZC) Router(ZR) End Device
22 * (dev 0) ------------------------------------ (dev 1) ---------------------------- (dev 2)
23 * [00:00:00:00:00:00:CA:FE] [00:00:00:00:00:00:00:01] [00:00:00:00:00:00:00:02]
24 * [00:00] [short addr assigned by ZC] [short addr assigned by ZR]
25 *
26 * 1- Channel scanning, pan id selection, start network (initiation of zigbee coordinator)
27 * 2- Manual registration of the joined device 1 into the zigbee coordinator
28 * 3- Confirmation of the joined device 1 with the coordinator
29 * 4- Initiate device 1 as a router
30 * 5- Manual registration of device 2 into the zigbee router (device 1)
31 * 6- Confirmation of the joined device 2 with the router.
32 */
33
34#include "ns3/constant-position-mobility-model.h"
35#include "ns3/core-module.h"
36#include "ns3/log.h"
37#include "ns3/lr-wpan-module.h"
38#include "ns3/packet.h"
39#include "ns3/propagation-delay-model.h"
40#include "ns3/propagation-loss-model.h"
41#include "ns3/simulator.h"
42#include "ns3/single-model-spectrum-channel.h"
43#include "ns3/zigbee-module.h"
44
45#include <iostream>
46
47using namespace ns3;
48using namespace ns3::lrwpan;
49using namespace ns3::zigbee;
50
51NS_LOG_COMPONENT_DEFINE("ZigbeeDirectJoin");
52
53static void
55{
56 std::cout << "Received packet of size " << p->GetSize() << "\n";
57}
58
59static void
61{
62 std::cout << "NlmeNetworkFormationConfirmStatus = " << params.m_status << "\n";
63}
64
65static void
67{
68 std::cout << "NlmeDirectJoinConfirmStatus = " << params.m_status << "\n";
69}
70
71static void
73{
74 if (params.m_status == NwkStatus::SUCCESS)
75 {
76 std::cout << " The device join the network SUCCESSFULLY with short address "
77 << params.m_networkAddress << "\n";
78 }
79 else
80 {
81 std::cout << " The device FAILED to join the network with status " << params.m_status
82 << "\n";
83 }
84}
85
86int
87main(int argc, char* argv[])
88{
91
93 nodes.Create(3);
94
95 //// Configure MAC
96
97 LrWpanHelper lrWpanHelper;
98 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
99 Ptr<LrWpanNetDevice> dev0 = lrwpanDevices.Get(0)->GetObject<LrWpanNetDevice>();
100 Ptr<LrWpanNetDevice> dev1 = lrwpanDevices.Get(1)->GetObject<LrWpanNetDevice>();
101 Ptr<LrWpanNetDevice> dev2 = lrwpanDevices.Get(2)->GetObject<LrWpanNetDevice>();
102
103 dev0->GetMac()->SetExtendedAddress("00:00:00:00:00:00:CA:FE");
104 // dev0->GetMac()->SetShortAddress("00:00");
105
106 dev1->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:01");
107 dev2->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:02");
108
112
115
116 channel->AddPropagationLossModel(propModel);
117 channel->SetPropagationDelayModel(delayModel);
118
119 dev0->SetChannel(channel);
120 dev1->SetChannel(channel);
121 dev2->SetChannel(channel);
122
123 //// Configure NWK
124
125 ZigbeeHelper zigbee;
126 ZigbeeStackContainer zigbeeStackContainer = zigbee.Install(lrwpanDevices);
127
128 Ptr<ZigbeeStack> zstack0 = zigbeeStackContainer.Get(0)->GetObject<ZigbeeStack>();
129 Ptr<ZigbeeStack> zstack1 = zigbeeStackContainer.Get(1)->GetObject<ZigbeeStack>();
130 Ptr<ZigbeeStack> zstack2 = zigbeeStackContainer.Get(2)->GetObject<ZigbeeStack>();
131
132 //// Configure Nodes Mobility
133
134 Ptr<ConstantPositionMobilityModel> sender0Mobility =
136 sender0Mobility->SetPosition(Vector(0, 0, 0));
137 dev0->GetPhy()->SetMobility(sender0Mobility);
138
139 Ptr<ConstantPositionMobilityModel> sender1Mobility =
141 sender1Mobility->SetPosition(Vector(0, 10, 0));
142 dev1->GetPhy()->SetMobility(sender1Mobility);
143
144 Ptr<ConstantPositionMobilityModel> sender2Mobility =
146 sender2Mobility->SetPosition(Vector(0, 20, 0));
147 dev2->GetPhy()->SetMobility(sender2Mobility);
148
149 // NWK callbacks hooks
150
151 zstack0->GetNwk()->SetNlmeNetworkFormationConfirmCallback(
153
154 zstack0->GetNwk()->SetNlmeDirectJoinConfirmCallback(
156
157 zstack0->GetNwk()->SetNldeDataIndicationCallback(
159
160 zstack1->GetNwk()->SetNldeDataIndicationCallback(
162
163 zstack2->GetNwk()->SetNldeDataIndicationCallback(
165
166 zstack1->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack1));
167
168 zstack2->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack2));
169
170 // 1 - Initiate the Zigbee coordinator, start the network
172 netFormParams.m_scanChannelList.channelPageCount = 1;
174 netFormParams.m_scanDuration = 0;
175 netFormParams.m_superFrameOrder = 15;
176 netFormParams.m_beaconOrder = 15;
177
179 Seconds(0),
181 zstack0->GetNwk(),
182 netFormParams);
183
184 // Configure the capability information used in the joining devices.
185 CapabilityInformation capaInfo;
186 capaInfo.SetDeviceType(zigbee::MacDeviceType::ROUTER);
187 capaInfo.SetAllocateAddrOn(true);
188
189 // 2- Register dev 1 (Mac64Addr .....00:01) to Zigbee coordinator (dev 0) directly
190 NlmeDirectJoinRequestParams directParams;
191 directParams.m_capabilityInfo = capaInfo.GetCapability();
192 directParams.m_deviceAddr = Mac64Address("00:00:00:00:00:00:00:01");
193
195 Seconds(5),
197 zstack0->GetNwk(),
198 directParams);
199
200 // 3- Use join request(type= DIRECT_OR_REJOIN) to initiate an orphaning procedure and request
201 // the information registered in the coordinator in the previous step.
202 // Notes :
203 // - ScanDuration is fixed for DIRECT_OR_REJOIN type (macResponseWaitTime)
204 // and therefore, the scanDuration parameter is ignored.
205 // - Future communications can fail if extendendPanId is set incorrectly,
206 // this value is the value of the PAN coordinator extended address (IEEEAddress).
207 // This value cannot be verified during a DIRECT_OR_REJOIN join type.
208 NlmeJoinRequestParams joinParams;
209 joinParams.m_rejoinNetwork = zigbee::JoiningMethod::DIRECT_OR_REJOIN;
210 joinParams.m_scanChannelList.channelPageCount = 1;
212 joinParams.m_capabilityInfo = capaInfo.GetCapability();
213 joinParams.m_extendedPanId = Mac64Address("00:00:00:00:00:00:CA:FE").ConvertToInt();
214
216 MilliSeconds(5500),
218 zstack1->GetNwk(),
219 joinParams);
220
221 // 4 - Use start-router on device 1, to initiate it as router
222 // (i.e. it becomes able to accept request from other devices to join the network)
223 NlmeStartRouterRequestParams startRouterParams;
225 MilliSeconds(5600),
227 zstack1->GetNwk(),
228 startRouterParams);
229
230 NlmeDirectJoinRequestParams directParams2;
231 directParams2.m_capabilityInfo = capaInfo.GetCapability();
232 directParams2.m_deviceAddr = Mac64Address("00:00:00:00:00:00:00:02");
233
235 MilliSeconds(6000),
237 zstack1->GetNwk(),
238 directParams2);
239
240 NlmeJoinRequestParams joinParams2;
241 joinParams2.m_rejoinNetwork = zigbee::JoiningMethod::DIRECT_OR_REJOIN;
242 joinParams2.m_scanChannelList.channelPageCount = 1;
244 joinParams2.m_capabilityInfo = capaInfo.GetCapability();
245 joinParams2.m_extendedPanId = Mac64Address("00:00:00:00:00:00:CA:FE").ConvertToInt();
246
248 MilliSeconds(6100),
250 zstack2->GetNwk(),
251 joinParams2);
252
255
257 return 0;
258}
helps to manage and create IEEE 802.15.4 NetDevice objects
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
an EUI-64 address
uint64_t ConvertToInt() const
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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 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
Setup a Zigbee stack to be used with LrWpanNetDevice.
zigbee::ZigbeeStackContainer Install(NetDeviceContainer c)
Install the Zigbee stack on top of an existing LrWpanNetDevice.
Network layer to device interface.
void NlmeDirectJoinRequest(NlmeDirectJoinRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.16 and 3.6.1.4.3 NLME-DIRECT-JOIN.request Allows the next...
void NlmeStartRouterRequest(NlmeStartRouterRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.9 NLME-START-ROUTER.request This primitive allows the nex...
void NlmeJoinRequest(NlmeJoinRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.13 NLME-JOIN.request This primitive allows the next highe...
void NlmeNetworkFormationRequest(NlmeNetworkFormationRequestParams params)
Zigbee Specification r22.1.0, Section 3.2.2.5 and 3.6.1.1 NLME-NETWORK-FORMATION.request Request the ...
Holds a vector of ns3::ZigbeeStack pointers.
Ptr< ZigbeeStack > Get(uint32_t i) const
Get a stack element from the container.
Zigbee protocol stack to device interface.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
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:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
NodeContainer nodes
static constexpr uint32_t ALL_CHANNELS
Bitmap representing all channels (11~26) LSB b0-b26, b27-b31 MSB Page 0 in Zigbee (250kbps O-QPSK)
Definition zigbee-nwk.h:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
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
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition log.h:102
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:309
channel
Definition third.py:77
uint8_t channelPageCount
The number of the channel page structures contained in the channel list structure.
Definition zigbee-nwk.h:274
std::vector< uint32_t > channelsField
The set of channels for a given page.
Definition zigbee-nwk.h:276
NLDE-DATA.indication params.
Definition zigbee-nwk.h:305
NLME-DIRECT-JOIN.confirm params.
Definition zigbee-nwk.h:447
NLME-DIRECT-JOIN.request params.
Definition zigbee-nwk.h:434
Mac64Address m_deviceAddr
The EUI-64 bit address of the device directly joined.
Definition zigbee-nwk.h:435
uint8_t m_capabilityInfo
The operating capabilities of the device being directly joined.
Definition zigbee-nwk.h:436
NLME-JOIN.confirm params.
Definition zigbee-nwk.h:537
NLME-JOIN.request params.
Definition zigbee-nwk.h:515
JoiningMethod m_rejoinNetwork
This parameter controls the method of joining the network.
Definition zigbee-nwk.h:518
ChannelList m_scanChannelList
The list of all channel pages and the associated channels that shall be scanned.
Definition zigbee-nwk.h:520
uint8_t m_capabilityInfo
The operating capabilities of the device being directly joined (Bit map).
Definition zigbee-nwk.h:524
uint64_t m_extendedPanId
The 64 bit PAN identifier of the the network to join.
Definition zigbee-nwk.h:516
NLME-NETWORK-FORMATION.confirm params.
Definition zigbee-nwk.h:393
NLME-NETWORK-FORMATION.request params.
Definition zigbee-nwk.h:348
uint8_t m_superFrameOrder
The superframe order.
Definition zigbee-nwk.h:355
ChannelList m_scanChannelList
A structure that contain a description on the pages and their channels to be scanned.
Definition zigbee-nwk.h:349
uint8_t m_scanDuration
The time spent of each channel in symbols: aBaseSuperframeDuriantion * (2n+1).
Definition zigbee-nwk.h:352
NLME-START-ROUTER.request params.
Definition zigbee-nwk.h:588
static void NwkDataIndication(Ptr< ZigbeeStack > stack, NldeDataIndicationParams params, Ptr< Packet > p)
static void NwkJoinConfirm(Ptr< ZigbeeStack > stack, NlmeJoinConfirmParams params)
static void NwkDirectJoinConfirm(Ptr< ZigbeeStack > stack, NlmeDirectJoinConfirmParams params)
static void NwkNetworkFormationConfirm(Ptr< ZigbeeStack > stack, NlmeNetworkFormationConfirmParams params)