A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-aps-data-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 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/core-module.h"
12#include "ns3/log.h"
13#include "ns3/lr-wpan-module.h"
14#include "ns3/packet.h"
15#include "ns3/propagation-delay-model.h"
16#include "ns3/propagation-loss-model.h"
17#include "ns3/rng-seed-manager.h"
18#include "ns3/simulator.h"
19#include "ns3/single-model-spectrum-channel.h"
20#include "ns3/zigbee-module.h"
21
22#include <iomanip>
23#include <iostream>
24
25using namespace ns3;
26using namespace ns3::lrwpan;
27using namespace ns3::zigbee;
28
29NS_LOG_COMPONENT_DEFINE("zigbee-aps-data-test");
30
31/**
32 * @ingroup zigbee-test
33 * @ingroup tests
34 *
35 * Zigbee RREQ transmission retries test case
36 */
38{
39 public:
41 ~ZigbeeApsDataTestCase() override;
42
43 private:
44 /**
45 * Callback for APSDE-DATA.indication
46 * This callback is called when a data packet is received by the APS layer.
47 *
48 * @param testcase The ZigbeeApsDataTestCase instance
49 * @param stack The Zigbee stack that received the data
50 * @param params The parameters of the APSDE-DATA.indication
51 * @param asdu The received packet
52 */
53 static void ApsDataIndication(ZigbeeApsDataTestCase* testcase,
54 Ptr<ZigbeeStack> stack,
56 Ptr<Packet> asdu);
57
58 /**
59 * Callback for NLME-NETWORK-DISCOVERY.confirm
60 * This callback is called when a network discovery has been performed.
61 *
62 * @param testcase The ZigbeeApsDataTestCase instance
63 * @param stack The Zigbee stack that received the confirmation
64 * @param params The parameters of the NLME-NETWORK-DISCOVERY.confirm
65 */
67 Ptr<ZigbeeStack> stack,
69
70 /**
71 * Send data to a unicast destination.
72 * This function sends a data packet from stackSrc to stackDst.
73 *
74 * @param stackSrc The source Zigbee stack
75 * @param stackDst The destination Zigbee stack
76 */
77 static void SendDataUcstDst(Ptr<ZigbeeStack> stackSrc, Ptr<ZigbeeStack> stackDst);
78
79 void DoRun() override;
80
81 uint16_t m_dstEndpoint; //!< The destination endpoint
82};
83
85 : TestCase("Zigbee: APS layer data test")
86{
87 m_dstEndpoint = 0;
88}
89
93
94void
96 Ptr<ZigbeeStack> stack,
98 Ptr<Packet> asdu)
99{
100 testcase->m_dstEndpoint = params.m_dstEndPoint;
101}
102
103void
105 Ptr<ZigbeeStack> stack,
107{
108 if (params.m_status == NwkStatus::SUCCESS)
109 {
110 NlmeJoinRequestParams joinParams;
111
113 capaInfo.SetDeviceType(MacDeviceType::ENDDEVICE);
114 capaInfo.SetAllocateAddrOn(true);
115
116 joinParams.m_rejoinNetwork = JoiningMethod::ASSOCIATION;
117 joinParams.m_capabilityInfo = capaInfo.GetCapability();
118 joinParams.m_extendedPanId = params.m_netDescList[0].m_extPanId;
119
120 Simulator::ScheduleNow(&ZigbeeNwk::NlmeJoinRequest, stack->GetNwk(), joinParams);
121 }
122 else
123 {
124 NS_ABORT_MSG("Unable to discover networks | status: " << params.m_status);
125 }
126}
127
128void
130{
131 // UCST transmission to a single 16-bit address destination
132 // Data is transmitted from device stackSrc to device stackDst.
133
135
136 // Because we currently do not have ZDO or ZCL or AF, clusterId
137 // and profileId numbers are non-sensical.
138 ApsdeDataRequestParams dataReqParams;
139 // creates a BitMap with transmission options
140 // Default, use 16 bit address destination (No option), equivalent to bitmap 0x00
141 ZigbeeApsTxOptions txOptions;
142 dataReqParams.m_txOptions = txOptions.GetTxOptions();
143 dataReqParams.m_useAlias = false;
144 dataReqParams.m_srcEndPoint = 3;
145 dataReqParams.m_clusterId = 5; // Arbitrary value
146 dataReqParams.m_profileId = 2; // Arbitrary value
147 dataReqParams.m_dstAddrMode = ApsDstAddressMode::DST_ADDR16_DST_ENDPOINT_PRESENT;
148 dataReqParams.m_dstAddr16 = stackDst->GetNwk()->GetNetworkAddress();
149 dataReqParams.m_dstEndPoint = 4;
150
151 Simulator::ScheduleNow(&ZigbeeAps::ApsdeDataRequest, stackSrc->GetAps(), dataReqParams, p);
152}
153
154void
156{
157 // Transmit data using the APS layer.
158
159 // Zigbee Coordinator --------------> Zigbee EndDevice(destination endpoint:4)
160
161 // This test transmit a single packet to an enddevice with endpoint 4.
162 // The data transmission is done using the mode DST_ADDR16_DST_ENDPOINT_PRESENT (Mode 0x02).
163 // No fragmentations or Acknowledge is used
164 // Verification that the devices have join the network is performed
165 // (i.e., All devices have valid network addresses).
166
169
171 nodes.Create(3);
172
173 //// Add the PHY and MAC, configure the channel
174
175 LrWpanHelper lrWpanHelper;
176 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
177 Ptr<LrWpanNetDevice> dev0 = lrwpanDevices.Get(0)->GetObject<LrWpanNetDevice>();
178 Ptr<LrWpanNetDevice> dev1 = lrwpanDevices.Get(1)->GetObject<LrWpanNetDevice>();
179 Ptr<LrWpanNetDevice> dev2 = lrwpanDevices.Get(2)->GetObject<LrWpanNetDevice>();
180
181 dev0->GetMac()->SetExtendedAddress("00:00:00:00:00:00:CA:FE");
182 dev1->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:01");
183 dev2->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:02");
184
188
191
192 channel->AddPropagationLossModel(propModel);
193 channel->SetPropagationDelayModel(delayModel);
194
195 dev0->SetChannel(channel);
196 dev1->SetChannel(channel);
197 dev2->SetChannel(channel);
198
199 // Add Zigbee stack with NWK and APS
200
201 ZigbeeHelper zigbeeHelper;
202 ZigbeeStackContainer zigbeeStackContainer = zigbeeHelper.Install(lrwpanDevices);
203
204 Ptr<ZigbeeStack> zstack0 = zigbeeStackContainer.Get(0)->GetObject<ZigbeeStack>();
205 Ptr<ZigbeeStack> zstack1 = zigbeeStackContainer.Get(1)->GetObject<ZigbeeStack>();
206 Ptr<ZigbeeStack> zstack2 = zigbeeStackContainer.Get(2)->GetObject<ZigbeeStack>();
207
208 // reprodusable results from random events occurring inside the stack.
209 zstack0->GetNwk()->AssignStreams(0);
210 zstack1->GetNwk()->AssignStreams(10);
211 zstack2->GetNwk()->AssignStreams(20);
212
213 //// Configure Nodes Mobility
214
216 dev0Mobility->SetPosition(Vector(0, 0, 0));
217 dev0->GetPhy()->SetMobility(dev0Mobility);
218
220 dev1Mobility->SetPosition(Vector(50, 0, 0));
221 dev1->GetPhy()->SetMobility(dev1Mobility);
222
224 dev2Mobility->SetPosition(Vector(0, 50, 0));
225 dev2->GetPhy()->SetMobility(dev2Mobility);
226
227 // Configure APS hooks
228 zstack1->GetAps()->SetApsdeDataIndicationCallback(
229 MakeBoundCallback(&ApsDataIndication, this, zstack1));
230
231 zstack2->GetAps()->SetApsdeDataIndicationCallback(
232 MakeBoundCallback(&ApsDataIndication, this, zstack2));
233
234 // Configure NWK hooks
235 // We do not have ZDO, we are required to use the NWK
236 // directly to perform association.
237 zstack1->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
239 zstack2->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
241
242 // Configure NWK hooks (for managing Network Joining)
243
244 // 1 - Initiate the Zigbee coordinator on a channel
246 netFormParams.m_scanChannelList.channelPageCount = 1;
247 netFormParams.m_scanChannelList.channelsField[0] = 0x00001800; // BitMap: channel 11 and 12
248 netFormParams.m_scanDuration = 0;
249 netFormParams.m_superFrameOrder = 15;
250 netFormParams.m_beaconOrder = 15;
251
252 Simulator::ScheduleWithContext(zstack0->GetNode()->GetId(),
253 Seconds(1),
255 zstack0->GetNwk(),
256 netFormParams);
257
259 netDiscParams.m_scanChannelList.channelPageCount = 1;
260 netDiscParams.m_scanChannelList.channelsField[0] = 0x00000800; // BitMap: Channels 11
261 netDiscParams.m_scanDuration = 2;
262 Simulator::ScheduleWithContext(zstack1->GetNode()->GetId(),
263 Seconds(2),
265 zstack1->GetNwk(),
266 netDiscParams);
267
269 netDiscParams.m_scanChannelList.channelPageCount = 1;
270 netDiscParams.m_scanChannelList.channelsField[0] = 0x00000800; // BitMap: Channels 11~14
271 netDiscParams.m_scanDuration = 2;
272 Simulator::ScheduleWithContext(zstack2->GetNode()->GetId(),
273 Seconds(3),
275 zstack2->GetNwk(),
276 netDiscParams2);
277
278 // Send data to a single UCST destination (16-bit address)
279 // The destination address is unknown until compilation, we extract
280 // it from the stack directly.
281 Simulator::Schedule(Seconds(4), &SendDataUcstDst, zstack0, zstack2);
282
284
285 // Check that devices actually joined the network and have different 16-bit addresses.
286
287 NS_TEST_EXPECT_MSG_NE(zstack1->GetNwk()->GetNetworkAddress(),
288 Mac16Address("FF:FF"),
289 "The dev 1 was unable to join the network");
290
291 NS_TEST_EXPECT_MSG_NE(zstack2->GetNwk()->GetNetworkAddress(),
292 Mac16Address("FF:FF"),
293 "The dev 1 was unable to join the network");
294
295 NS_TEST_EXPECT_MSG_NE(zstack0->GetNwk()->GetNetworkAddress(),
296 zstack1->GetNwk()->GetNetworkAddress(),
297 "Error, devices 0 and 1 have the same 16 bit MAC address");
298
299 NS_TEST_EXPECT_MSG_NE(zstack1->GetNwk()->GetNetworkAddress(),
300 zstack2->GetNwk()->GetNetworkAddress(),
301 "Error, devices 1 and 2 have the same 16 bit MAC address");
302
303 // Check that the packet was received to the correct preconfigured destination endpoint.
304
306 4,
307 "Packet was not received in the correct destination endpoint");
308
310}
311
312/**
313 * @ingroup zigbee-test
314 * @ingroup tests
315 *
316 * Zigbee APS Data TestSuite
317 */
319{
320 public:
322};
323
325 : TestSuite("zigbee-aps-data-test", Type::UNIT)
326{
327 AddTestCase(new ZigbeeApsDataTestCase, TestCase::Duration::QUICK);
328}
329
330static ZigbeeApsDataTestSuite zigbeeApsDataTestSuite; //!< Static variable for test initialization
Zigbee RREQ transmission retries test case.
uint16_t m_dstEndpoint
The destination endpoint.
static void NwkNetworkDiscoveryConfirm(ZigbeeApsDataTestCase *testcase, Ptr< ZigbeeStack > stack, NlmeNetworkDiscoveryConfirmParams params)
Callback for NLME-NETWORK-DISCOVERY.confirm This callback is called when a network discovery has been...
void DoRun() override
Implementation to actually run this TestCase.
static void SendDataUcstDst(Ptr< ZigbeeStack > stackSrc, Ptr< ZigbeeStack > stackDst)
Send data to a unicast destination.
static void ApsDataIndication(ZigbeeApsDataTestCase *testcase, Ptr< ZigbeeStack > stack, ApsdeDataIndicationParams params, Ptr< Packet > asdu)
Callback for APSDE-DATA.indication This callback is called when a data packet is received by the APS ...
Zigbee APS Data 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.
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 SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
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 EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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 the Capability Information Bit fields See zigbe Specification r22.1....
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 ApsdeDataRequest(ApsdeDataRequestParams params, Ptr< Packet > asdu)
Zigbee Specification r22.1.0, Section 2.2.4.1.1 APSDE-DATA.request Request the transmission of data t...
Definition zigbee-aps.cc:81
Helper class used to craft the transmission options bitmap used by the APSDE-DATA....
Definition zigbee-aps.h:442
uint8_t GetTxOptions() const
Get the complete bitmap containing the Tx options.
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.
#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: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
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:439
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition test.h:656
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Zigbee Specification r22.1.0, Section 2.2.4.1.3 APSDE-DATA.indications params.
Definition zigbee-aps.h:165
Zigbee Specification r22.1.0, Section 2.2.4.1.1 APSDE-DATA.request params.
Definition zigbee-aps.h:123
uint16_t m_profileId
The application profile ID.
Definition zigbee-aps.h:129
uint16_t m_clusterId
The application cluster ID.
Definition zigbee-aps.h:130
Mac16Address m_dstAddr16
The destination 16-bit address.
Definition zigbee-aps.h:126
uint8_t m_txOptions
Transmission options.
Definition zigbee-aps.h:133
uint8_t m_srcEndPoint
The source endpoint.
Definition zigbee-aps.h:131
bool m_useAlias
Indicates if alias is used in this transmission.
Definition zigbee-aps.h:134
ApsDstAddressMode m_dstAddrMode
Destination address mode.
Definition zigbee-aps.h:124
uint8_t m_dstEndPoint
The destination endpoint.
Definition zigbee-aps.h:128
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
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
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-DISCOVERY.confirm params.
Definition zigbee-nwk.h:500
NLME-NETWORK-DISCOVERY.request params.
Definition zigbee-nwk.h:461
uint8_t m_scanDuration
A value used to calculate the length of time to spend.
Definition zigbee-nwk.h:464
ChannelList m_scanChannelList
The list of all channel pages and the associated channels that shall be scanned.
Definition zigbee-nwk.h:462
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
static ZigbeeApsDataTestSuite zigbeeApsDataTestSuite
Static variable for test initialization.