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 the Zigbee Stack and use only the NWK layer
124
125 ZigbeeHelper zigbeeHelper;
126 zigbeeHelper.SetNwkLayerOnly();
127 ZigbeeStackContainer zigbeeStackContainer = zigbeeHelper.Install(lrwpanDevices);
128
129 Ptr<ZigbeeStack> zstack0 = zigbeeStackContainer.Get(0)->GetObject<ZigbeeStack>();
130 Ptr<ZigbeeStack> zstack1 = zigbeeStackContainer.Get(1)->GetObject<ZigbeeStack>();
131 Ptr<ZigbeeStack> zstack2 = zigbeeStackContainer.Get(2)->GetObject<ZigbeeStack>();
132
133 //// Configure Nodes Mobility
134
135 Ptr<ConstantPositionMobilityModel> sender0Mobility =
137 sender0Mobility->SetPosition(Vector(0, 0, 0));
138 dev0->GetPhy()->SetMobility(sender0Mobility);
139
140 Ptr<ConstantPositionMobilityModel> sender1Mobility =
142 sender1Mobility->SetPosition(Vector(0, 10, 0));
143 dev1->GetPhy()->SetMobility(sender1Mobility);
144
145 Ptr<ConstantPositionMobilityModel> sender2Mobility =
147 sender2Mobility->SetPosition(Vector(0, 20, 0));
148 dev2->GetPhy()->SetMobility(sender2Mobility);
149
150 // NWK callbacks hooks
151
152 zstack0->GetNwk()->SetNlmeNetworkFormationConfirmCallback(
154
155 zstack0->GetNwk()->SetNlmeDirectJoinConfirmCallback(
157
158 zstack0->GetNwk()->SetNldeDataIndicationCallback(
160
161 zstack1->GetNwk()->SetNldeDataIndicationCallback(
163
164 zstack2->GetNwk()->SetNldeDataIndicationCallback(
166
167 zstack1->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack1));
168
169 zstack2->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack2));
170
171 // 1 - Initiate the Zigbee coordinator, start the network
173 netFormParams.m_scanChannelList.channelPageCount = 1;
175 netFormParams.m_scanDuration = 0;
176 netFormParams.m_superFrameOrder = 15;
177 netFormParams.m_beaconOrder = 15;
178
180 Seconds(0),
182 zstack0->GetNwk(),
183 netFormParams);
184
185 // Configure the capability information used in the joining devices.
186 CapabilityInformation capaInfo;
187 capaInfo.SetDeviceType(zigbee::MacDeviceType::ROUTER);
188 capaInfo.SetAllocateAddrOn(true);
189
190 // 2- Register dev 1 (Mac64Addr .....00:01) to Zigbee coordinator (dev 0) directly
191 NlmeDirectJoinRequestParams directParams;
192 directParams.m_capabilityInfo = capaInfo.GetCapability();
193 directParams.m_deviceAddr = Mac64Address("00:00:00:00:00:00:00:01");
194
196 Seconds(5),
198 zstack0->GetNwk(),
199 directParams);
200
201 // 3- Use join request(type= DIRECT_OR_REJOIN) to initiate an orphaning procedure and request
202 // the information registered in the coordinator in the previous step.
203 // Notes :
204 // - ScanDuration is fixed for DIRECT_OR_REJOIN type (macResponseWaitTime)
205 // and therefore, the scanDuration parameter is ignored.
206 // - Future communications can fail if extendendPanId is set incorrectly,
207 // this value is the value of the PAN coordinator extended address (IEEEAddress).
208 // This value cannot be verified during a DIRECT_OR_REJOIN join type.
209 NlmeJoinRequestParams joinParams;
210 joinParams.m_rejoinNetwork = zigbee::JoiningMethod::DIRECT_OR_REJOIN;
211 joinParams.m_scanChannelList.channelPageCount = 1;
213 joinParams.m_capabilityInfo = capaInfo.GetCapability();
214 joinParams.m_extendedPanId = Mac64Address("00:00:00:00:00:00:CA:FE").ConvertToInt();
215
217 MilliSeconds(5500),
219 zstack1->GetNwk(),
220 joinParams);
221
222 // 4 - Use start-router on device 1, to initiate it as router
223 // (i.e. it becomes able to accept request from other devices to join the network)
224 NlmeStartRouterRequestParams startRouterParams;
226 MilliSeconds(5600),
228 zstack1->GetNwk(),
229 startRouterParams);
230
231 NlmeDirectJoinRequestParams directParams2;
232 directParams2.m_capabilityInfo = capaInfo.GetCapability();
233 directParams2.m_deviceAddr = Mac64Address("00:00:00:00:00:00:00:02");
234
236 MilliSeconds(6000),
238 zstack1->GetNwk(),
239 directParams2);
240
241 NlmeJoinRequestParams joinParams2;
242 joinParams2.m_rejoinNetwork = zigbee::JoiningMethod::DIRECT_OR_REJOIN;
243 joinParams2.m_scanChannelList.channelPageCount = 1;
245 joinParams2.m_capabilityInfo = capaInfo.GetCapability();
246 joinParams2.m_extendedPanId = Mac64Address("00:00:00:00:00:00:CA:FE").ConvertToInt();
247
249 MilliSeconds(6100),
251 zstack2->GetNwk(),
252 joinParams2);
253
256
258 return 0;
259}
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:578
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.
void SetNwkLayerOnly()
If this is set, the helper will only create Zigbee stacks that contain only the NWK layer.
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:1345
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1357
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:284
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:302
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)