A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-zdo-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Tokushima University, Tokushima, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author:
7 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
8 */
9
10#include "ns3/constant-position-mobility-model.h"
11#include "ns3/log.h"
12#include "ns3/lr-wpan-helper.h"
13#include "ns3/net-device-container.h"
14#include "ns3/node-container.h"
15#include "ns3/node.h"
16#include "ns3/packet.h"
17#include "ns3/propagation-delay-model.h"
18#include "ns3/propagation-loss-model.h"
19#include "ns3/rng-seed-manager.h"
20#include "ns3/simulator.h"
21#include "ns3/single-model-spectrum-channel.h"
22#include "ns3/test.h"
23#include "ns3/zigbee-helper.h"
24#include "ns3/zigbee-stack-container.h"
25#include "ns3/zigbee-stack.h"
26
27#include <iomanip>
28#include <iostream>
29
30using namespace ns3;
31using namespace ns3::lrwpan;
32using namespace ns3::zigbee;
33
34NS_LOG_COMPONENT_DEFINE("zigbee-zdo-test");
35
36/**
37 * @ingroup zigbee-test
38 * @ingroup tests
39 *
40 * Zigbee ZDO Commands test
41 */
43{
44 public:
47
48 private:
49 /**
50 * Callback for ZDO NWK_addr_rsp indication
51 * This callback is called when a NWK_addr_rsp is received.
52 *
53 * @param testcase The ZigbeeZdoCommandsTestCase instance
54 * @param stack The Zigbee stack that received the indication
55 * @param params The parameters of the NWK_addr_rsp indication
56 */
57 static void ZdoNwkAddrRspInd(ZigbeeZdoCommandsTestCase* testcase,
58 Ptr<ZigbeeStack> stack,
60
61 /**
62 * Callback for NLME-NETWORK-DISCOVERY.confirm
63 * This callback is called when a network discovery has been performed.
64 *
65 * @param testcase The ZigbeeApsDataTestCase instance
66 * @param stack The Zigbee stack that received the confirmation
67 * @param params The parameters of the NLME-NETWORK-DISCOVERY.confirm
68 */
70 Ptr<ZigbeeStack> stack,
72
73 void DoRun() override;
74
75 Mac16Address m_requestedNwkAddr; //!< The 16-bit address requested by the ZDO command.
76};
77
79 : TestCase("Zigbee: ZDO commands test")
80{
81}
82
86
87void
89 Ptr<ZigbeeStack> stack,
91{
92 if (params.m_status == ZdpStatus::SUCCESS)
93 {
94 testcase->m_requestedNwkAddr = params.m_nwkAddrRemoteDev;
95
96 std::cout << "NWK_addr_rsp received | status: SUCCESS "
97 << " | IEEE address: " << params.m_ieeeAddrRemoteDev
98 << " | NWK address: " << params.m_nwkAddrRemoteDev << std::endl;
99 }
100 else
101 {
102 std::cout << "NWK_addr_rsp not received or received with error status\n";
103 }
104}
105
106void
108 Ptr<ZigbeeStack> stack,
110{
111 if (params.m_status == NwkStatus::SUCCESS)
112 {
113 NlmeJoinRequestParams joinParams;
114
117 capaInfo.SetAllocateAddrOn(true);
118
120 joinParams.m_capabilityInfo = capaInfo.GetCapability();
121 joinParams.m_extendedPanId = params.m_netDescList[0].m_extPanId;
122
123 Simulator::ScheduleNow(&ZigbeeNwk::NlmeJoinRequest, stack->GetNwk(), joinParams);
124 }
125 else
126 {
127 NS_ABORT_MSG("Unable to discover networks | status: " << params.m_status);
128 }
129}
130
131void
133{
134 // Transmit commands and receiving a response using Zigbee's ZDO
135 // The topogy includes 3 devices with the following formation:
136
137 // Zigbee Coordinator ------- Router -------> Zigbee EndDevice
138
139 // In this test a ZDO NWK_Addr_req command is sent to the network to
140 // inquire the 16 bit address of the device with the 64-bit address
141 // [00:00:00:00:00:00:00:02] which correspond to the End Device.
142 // Upon receiving this request, the EndDevice responds to the coordinator.
143 // The coordinator updates its nwkAddressMap internally and reports the
144 // results of the request.
145
148
150 nodes.Create(3);
151
152 //// Add the PHY and MAC, configure the channel
153
154 LrWpanHelper lrWpanHelper;
155 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
156 Ptr<LrWpanNetDevice> dev0 = lrwpanDevices.Get(0)->GetObject<LrWpanNetDevice>();
157 Ptr<LrWpanNetDevice> dev1 = lrwpanDevices.Get(1)->GetObject<LrWpanNetDevice>();
158 Ptr<LrWpanNetDevice> dev2 = lrwpanDevices.Get(2)->GetObject<LrWpanNetDevice>();
159
160 dev0->GetMac()->SetExtendedAddress("00:00:00:00:00:00:CA:FE");
161 dev1->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:01");
162 dev2->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:02");
163
167
170
171 channel->AddPropagationLossModel(propModel);
172 channel->SetPropagationDelayModel(delayModel);
173
174 dev0->SetChannel(channel);
175 dev1->SetChannel(channel);
176 dev2->SetChannel(channel);
177
178 // Add Zigbee stack with NWK | APS | ZDO
179 ZigbeeHelper zigbeeHelper;
180 ZigbeeStackContainer zigbeeStackContainer = zigbeeHelper.Install(lrwpanDevices);
181
182 Ptr<ZigbeeStack> zstack0 = zigbeeStackContainer.Get(0)->GetObject<ZigbeeStack>();
183 Ptr<ZigbeeStack> zstack1 = zigbeeStackContainer.Get(1)->GetObject<ZigbeeStack>();
184 Ptr<ZigbeeStack> zstack2 = zigbeeStackContainer.Get(2)->GetObject<ZigbeeStack>();
185
186 // reprodusable results from random events occurring inside the stack (selected discretely)
187 zstack0->GetNwk()->AssignStreams(1);
188 zstack1->GetNwk()->AssignStreams(20);
189 zstack2->GetNwk()->AssignStreams(25);
190
191 //// Configure Nodes Mobility
192
194 dev0Mobility->SetPosition(Vector(0, 0, 0));
195 dev0->GetPhy()->SetMobility(dev0Mobility);
196
198 dev1Mobility->SetPosition(Vector(50, 0, 0));
199 dev1->GetPhy()->SetMobility(dev1Mobility);
200
202 dev2Mobility->SetPosition(Vector(0, 50, 0));
203 dev2->GetPhy()->SetMobility(dev2Mobility);
204
205 // Configure ZDO hooks to obtain responses from ZDO commands.
206 zstack0->GetZdo()->SetZdoNwkAddrRspIndCallback(
207 MakeBoundCallback(&ZdoNwkAddrRspInd, this, zstack0));
208
209 // Configure NWK hooks
210 // We have ZDO, but we do not have a Commission Manager
211 // (entity to simplify and handle of Network formation and Joins)
212 // Therefore, we use the NWK directly to perform association.
213 zstack1->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
215 zstack2->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
217
218 // Configure NWK hooks (for managing Network Joining)
219
220 // 1 - Initiate the Zigbee coordinator on a channel
222 netFormParams.m_scanChannelList.channelPageCount = 1;
223 netFormParams.m_scanChannelList.channelsField[0] = 0x00001800; // BitMap: channel 11 and 12
224 netFormParams.m_scanDuration = 0;
225 netFormParams.m_superFrameOrder = 15;
226 netFormParams.m_beaconOrder = 15;
227
228 Simulator::ScheduleWithContext(zstack0->GetNode()->GetId(),
229 Seconds(1),
231 zstack0->GetNwk(),
232 netFormParams);
233
235 netDiscParams.m_scanChannelList.channelPageCount = 1;
236 netDiscParams.m_scanChannelList.channelsField[0] = 0x00000800; // BitMap: Channels 11
237 netDiscParams.m_scanDuration = 2;
238 Simulator::ScheduleWithContext(zstack1->GetNode()->GetId(),
239 Seconds(2),
241 zstack1->GetNwk(),
242 netDiscParams);
243
245 netDiscParams.m_scanChannelList.channelPageCount = 1;
246 netDiscParams.m_scanChannelList.channelsField[0] = 0x00000800; // BitMap: Channels 11~14
247 netDiscParams.m_scanDuration = 2;
248 Simulator::ScheduleWithContext(zstack2->GetNode()->GetId(),
249 Seconds(3),
251 zstack2->GetNwk(),
252 netDiscParams2);
253
254 // Dev 0 (Coordinator) sends a NWK_Addr_req to dev2 (last end device) to inquire its
255 // nwkAddress (16-bit address) based on its known IEEE Addr.
256 NwkAddrRequestParams zdoReqParams;
257 zdoReqParams.m_dstAddr = Mac16Address("FF:FF");
258 zdoReqParams.m_ieeeAddr = Mac64Address("00:00:00:00:00:00:00:02");
259 Simulator::ScheduleWithContext(zstack0->GetNode()->GetId(),
260 Seconds(4),
262 zstack0->GetZdo(),
263 zdoReqParams);
264
265 // Dev 0 (Coordinator) sends a NWK_Addr_req to all devices to
266 // inquire the nwkAddress (16-bit address) of the device with IEEE Addr 00:00:00:00:00:00:00:03.
267 NwkAddrRequestParams zdoNwkAddrReqParams;
269 zdoNwkAddrReqParams.m_ieeeAddr = Mac64Address("00:00:00:00:00:00:00:02");
270 zdoNwkAddrReqParams.m_dstAddr = Mac16Address("FF:FF");
271 Simulator::ScheduleWithContext(zstack0->GetNode()->GetId(),
272 Seconds(5),
274 zstack0->GetZdo(),
275 zdoNwkAddrReqParams);
276
278
279 // Verify the dev2 64-bit address is correctly assigned.
280 NS_TEST_EXPECT_MSG_EQ(zstack2->GetNwk()->GetIeeeAddress().ConvertToInt(),
281 Mac64Address("00:00:00:00:00:00:00:02").ConvertToInt(),
282 "Dev2 (stack2) is not using the correct 64-bit IEEE address");
283
284 // Check that the coordinator received the correct results after a NWK_Addr_req
285 NS_TEST_EXPECT_MSG_EQ(zstack2->GetNwk()->GetNetworkAddress().ConvertToInt(),
286 m_requestedNwkAddr.ConvertToInt(),
287 "Error: The obtained address via NWK_Addr_req do not correspond to the "
288 "address used by the device");
289
290 // Check that the coordinator updated its nwkAddressMap after receiving the NWK_Addr_rsp from
291 // dev2
292 Mac16Address registeredAddress;
293 zstack0->GetNwk()->GetNwkAddrByIeeeAddr(Mac64Address("00:00:00:00:00:00:00:02"),
294 registeredAddress);
295
297 zstack2->GetNwk()->GetNetworkAddress().ConvertToInt(),
298 registeredAddress.ConvertToInt(),
299 "Error: The nwkAddressMap do not have a matching entry even after a NWK_Addr_req CMD");
300
302}
303
304/**
305 * @ingroup zigbee-test
306 * @ingroup tests
307 *
308 * Zigbee ZDO TestSuite
309 */
311{
312 public:
314};
315
321
322static ZigbeeZdoTestSuite zigbeeZdoTestSuite; //!< Static variable for test initialization
Zigbee ZDO Commands test.
void DoRun() override
Implementation to actually run this TestCase.
Mac16Address m_requestedNwkAddr
The 16-bit address requested by the ZDO command.
static void ZdoNwkAddrRspInd(ZigbeeZdoCommandsTestCase *testcase, Ptr< ZigbeeStack > stack, NwkAddrRespIndParams params)
Callback for ZDO NWK_addr_rsp indication This callback is called when a NWK_addr_rsp is received.
static void NwkNetworkDiscoveryConfirm(ZigbeeZdoCommandsTestCase *testcase, Ptr< ZigbeeStack > stack, NlmeNetworkDiscoveryConfirmParams params)
Callback for NLME-NETWORK-DISCOVERY.confirm This callback is called when a network discovery has been...
Zigbee ZDO TestSuite.
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.
This class can contain 16 bit addresses.
uint16_t ConvertToInt() const
an EUI-64 address
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
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.
Represent the Capability Information Bit fields See zigbe Specification r22.1.0, Table 3-62.
uint8_t GetCapability() const
Used to obtain the complete capability information bit map.
void SetDeviceType(MacDeviceType devType)
Set the device type bit for the capability information field.
void SetAllocateAddrOn(bool value)
Set the Allocate Addr On for the capability information field.
void NlmeNetworkDiscoveryRequest(NlmeNetworkDiscoveryRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.3 NLME-NETWORK-DISCOVERY.request Allows the next higher l...
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.
void NwkAddrRequest(NwkAddrRequestParams params)
This function is used to send a Network Address (16-bit address)Request to a remote device when the I...
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:753
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
@ SINGLE_DEVICE_RESPONSE
Request type for a single device response.
Definition zigbee-zdo.h:79
@ ENDDEVICE
End device or router treated as an end device.
NodeContainer nodes
@ ASSOCIATION
The device is requesting to join a network through association.
Definition zigbee-nwk.h:109
@ SUCCESS
The request was successful.
Definition zigbee-zdo.h:39
@ SUCCESS
The operation was completed successfully.
Definition zigbee-nwk.h:154
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t channelPageCount
The number of the channel page structures contained in the channel list structure.
Definition zigbee-nwk.h:275
std::vector< uint32_t > channelsField
The set of channels for a given page.
Definition zigbee-nwk.h:277
NLME-JOIN.request params.
Definition zigbee-nwk.h:517
JoiningMethod m_rejoinNetwork
This parameter controls the method of joining the network.
Definition zigbee-nwk.h:520
uint8_t m_capabilityInfo
The operating capabilities of the device being directly joined (Bit map).
Definition zigbee-nwk.h:526
uint64_t m_extendedPanId
The 64 bit PAN identifier of the the network to join.
Definition zigbee-nwk.h:518
NLME-NETWORK-DISCOVERY.confirm params.
Definition zigbee-nwk.h:502
NLME-NETWORK-DISCOVERY.request params.
Definition zigbee-nwk.h:463
uint8_t m_scanDuration
A value used to calculate the length of time to spend.
Definition zigbee-nwk.h:466
ChannelList m_scanChannelList
The list of all channel pages and the associated channels that shall be scanned.
Definition zigbee-nwk.h:464
NLME-NETWORK-FORMATION.request params.
Definition zigbee-nwk.h:349
uint8_t m_superFrameOrder
The superframe order.
Definition zigbee-nwk.h:356
ChannelList m_scanChannelList
A structure that contain a description on the pages and their channels to be scanned.
Definition zigbee-nwk.h:350
uint8_t m_scanDuration
The time spent of each channel in symbols: aBaseSuperframeDuriantion * (2n+1).
Definition zigbee-nwk.h:353
ZDO Network Address Request Parameters.
Definition zigbee-zdo.h:90
NwkAddrRequestType m_requestType
Request type of this command.
Definition zigbee-zdo.h:94
Mac16Address m_dstAddr
The destination of the request, unicast or broadcast (0xFFFF).
Definition zigbee-zdo.h:91
Mac64Address m_ieeeAddr
The known IEEE Address(64-bit address) of the device that we are request its network address (16-bit ...
Definition zigbee-zdo.h:92
ZDO Network Address Response Indication Parameters.
Definition zigbee-zdo.h:130
static ZigbeeZdoTestSuite zigbeeZdoTestSuite
Static variable for test initialization.