A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-nwk-routing-grid.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 * Author:
7 *
8 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
9 */
10
11/**
12 * Many to one routing example in a grid topology.
13 *
14 * Topology:
15 *
16 * Grid Topology: 50 nodes separated by 30 m around them, 20 nodes per row for
17 * the first two rows
18 *
19 * (Node 0)
20 * |
21 * v
22 * * * * * * * * * * * * * * * * * * * * *
23 * * * * * * * * * * * * * * * * * * * * *
24 * * * * * * * * * * * <--- (Node 49)
25 *
26 * This example is a more complex version of zigbee-nwk-routing.cc.
27 * The top left node is the coordinator while the rest of the nodes join
28 * the network sequentially and they initiate as routers.
29 *
30 * After all devices join the network a MANY-TO-ONE route discovery is issued
31 * to create the routes from all the nodes towards the concentrator (Node 0).
32 *
33 * At the end of the example, the contents of all the tables (Neighbor, Discovery, routing)
34 * in the source node (Node 49) are displayed.
35 *
36 * Also the trace route from Node 49 to Node 0 is displayed.
37 * No data is transmitted in this example.
38 * All devices are routers excepting the first node which is the coordinator.
39 *
40 */
41
42#include "ns3/core-module.h"
43#include "ns3/log.h"
44#include "ns3/lr-wpan-module.h"
45#include "ns3/mobility-module.h"
46#include "ns3/packet.h"
47#include "ns3/propagation-delay-model.h"
48#include "ns3/propagation-loss-model.h"
49#include "ns3/simulator.h"
50#include "ns3/single-model-spectrum-channel.h"
51#include "ns3/zigbee-module.h"
52
53#include <iostream>
54
55using namespace ns3;
56using namespace ns3::lrwpan;
57using namespace ns3::zigbee;
58
60
61static void
63{
64 std::cout << "\n";
65 std::cout << "Traceroute to destination [" << dst << "] (Time: " << Simulator::Now().As(Time::S)
66 << "):\n";
67 Mac16Address target = src;
68 uint32_t count = 1;
69 while (target != Mac16Address("FF:FF") && target != dst)
70 {
71 Ptr<ZigbeeStack> zstack;
72
73 for (auto i = zigbeeStacks.Begin(); i != zigbeeStacks.End(); i++)
74 {
75 zstack = *i;
76 if (zstack->GetNwk()->GetNetworkAddress() == target)
77 {
78 break;
79 }
80 }
81
82 bool neighbor = false;
83 target = zstack->GetNwk()->FindRoute(dst, neighbor);
84 if (target == Mac16Address("FF:FF"))
85 {
86 std::cout << count << ". Node " << zstack->GetNode()->GetId() << " ["
87 << zstack->GetNwk()->GetNetworkAddress() << " | "
88 << zstack->GetNwk()->GetIeeeAddress() << "]: "
89 << " Destination Unreachable\n";
90 }
91 else
92 {
93 std::cout << count << ". Node " << zstack->GetNode()->GetId() << " ["
94 << zstack->GetNwk()->GetNetworkAddress() << " | "
95 << zstack->GetNwk()->GetIeeeAddress() << "]: "
96 << "NextHop [" << target << "] ";
97 if (neighbor)
98 {
99 std::cout << "(*Neighbor)\n";
100 }
101 else
102 {
103 std::cout << "\n";
104 }
105 count++;
106 }
107 }
108 std::cout << "\n";
109}
110
111static void
112CreateManyToOneRoutes(Ptr<ZigbeeStack> zigbeeStackConcentrator, Ptr<ZigbeeStack> zigbeeStackSrc)
113{
114 // Generate all the routes to the concentrator device
115 NlmeRouteDiscoveryRequestParams routeDiscParams;
116 routeDiscParams.m_dstAddrMode = NO_ADDRESS;
118 zigbeeStackConcentrator->GetNwk(),
119 routeDiscParams);
120
121 // Give a few seconds to allow the creation of the route and
122 // then print the route trace and tables from the source
124 &TraceRoute,
125 zigbeeStackSrc->GetNwk()->GetNetworkAddress(),
126 zigbeeStackConcentrator->GetNwk()->GetNetworkAddress());
127
128 // Print the content of the source device tables (Neighbor, Discovery, Routing)
132 zigbeeStackSrc->GetNwk(),
133 stream);
134
137 zigbeeStackSrc->GetNwk(),
138 stream);
139
142 zigbeeStackSrc->GetNwk(),
143 stream);
144}
145
146static void
148{
149 std::cout << "Received packet of size " << p->GetSize() << "\n";
150}
151
152static void
154{
155 std::cout << "NlmeNetworkFormationConfirmStatus = " << params.m_status << "\n";
156}
157
158static void
160{
161 // See Zigbee Specification r22.1.0, 3.6.1.4.1
162 // This method implements a simplistic version of the method implemented
163 // in a zigbee APL layer. In this layer a candidate Extended PAN Id must
164 // be selected and a NLME-JOIN.request must be issued.
165
166 if (params.m_status == NwkStatus::SUCCESS)
167 {
168 std::cout << " Network discovery confirm Received. Networks found "
169 << "(" << params.m_netDescList.size() << ")\n";
170
171 for (const auto& netDescriptor : params.m_netDescList)
172 {
173 std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << std::dec
174 << "\n"
175 << " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh) << std::hex
176 << "\n"
177 << " Pan Id: 0x" << netDescriptor.m_panId << std::hex << "\n"
178 << " stackprofile: " << std::dec
179 << static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n"
180 << " ----------------\n ";
181 }
182
183 NlmeJoinRequestParams joinParams;
184
186 capaInfo.SetDeviceType(ROUTER);
187 capaInfo.SetAllocateAddrOn(true);
188
189 joinParams.m_rejoinNetwork = zigbee::JoiningMethod::ASSOCIATION;
190 joinParams.m_capabilityInfo = capaInfo.GetCapability();
191 joinParams.m_extendedPanId = params.m_netDescList[0].m_extPanId;
192
193 Simulator::ScheduleNow(&ZigbeeNwk::NlmeJoinRequest, stack->GetNwk(), joinParams);
194 }
195 else
196 {
197 std::cout << " WARNING: Unable to discover networks | status: " << params.m_status << "\n";
198 }
199}
200
201static void
203{
204 if (params.m_status == NwkStatus::SUCCESS)
205 {
206 std::cout << Simulator::Now().As(Time::S)
207 << " The device joined the network SUCCESSFULLY with short address [" << std::hex
208 << params.m_networkAddress << "] on the Extended PAN Id: " << std::hex
209 << params.m_extendedPanId << "\n"
210 << std::dec;
211
212 // 3 - After dev is associated, it should be started as a router
213 // (i.e. it becomes able to accept request from other devices to join the network)
214 NlmeStartRouterRequestParams startRouterParams;
216 stack->GetNwk(),
217 startRouterParams);
218 }
219 else
220 {
221 std::cout << Simulator::Now().As(Time::S)
222 << " The device FAILED to join the network with status " << params.m_status
223 << "\n";
224 }
225}
226
227static void
229{
230 std::cout << "NlmeRouteDiscoveryConfirmStatus = " << params.m_status << "\n";
231}
232
233int
234main(int argc, char* argv[])
235{
237 // Enable logs for further details
238 // LogComponentEnable("ZigbeeNwk", LOG_LEVEL_DEBUG);
239
241 nodes.Create(50);
242
244 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
245 "MinX",
246 DoubleValue(0.0),
247 "MinY",
248 DoubleValue(0.0),
249 "DeltaX",
250 DoubleValue(30.0),
251 "DeltaY",
252 DoubleValue(30.0),
253 "GridWidth",
254 UintegerValue(20),
255 "LayoutType",
256 StringValue("RowFirst"));
257
258 mobility.Install(nodes);
259
265
266 channel->AddPropagationLossModel(propModel);
267 channel->SetPropagationDelayModel(delayModel);
268
269 LrWpanHelper lrWpanHelper;
270 lrWpanHelper.SetChannel(channel);
271
272 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
273
274 // Set the extended address to all devices (EUI-64)
275 // Device must ALWAYS have IEEE Address (Extended address) assigned.
276 // Network address (short address) are assigned by the the JOIN mechanism
277 // In this case we use the helper to assign a sequential extended addresses
278 // to all nodes in the simulation.
279 lrWpanHelper.SetExtendedAddresses(lrwpanDevices);
280
281 ZigbeeHelper zigbeeHelper;
282 zigbeeStacks = zigbeeHelper.Install(lrwpanDevices);
283
284 // NWK callbacks hooks
285 // These hooks are usually directly connected to the APS layer
286 // In this case, there is no APS layer, therefore, we connect the event outputs
287 // of all devices directly to our static functions in this example.
288 for (auto i = zigbeeStacks.Begin(); i != zigbeeStacks.End(); i++)
289 {
290 Ptr<ZigbeeStack> zstack = *i;
291 // NLME-NETWORK-FORMATION.Confirm
292 zstack->GetNwk()->SetNlmeNetworkFormationConfirmCallback(
294 // NLDE-DATA.Indication
295 zstack->GetNwk()->SetNldeDataIndicationCallback(
297 // NLDE-NETWORK-DISCOVERY.Confirm
298 zstack->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
300 // NLME-JOIN.Confirm
301 zstack->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack));
302 // NLME-ROUTE-DISCOVERY.Confirm
303 zstack->GetNwk()->SetNlmeRouteDiscoveryConfirmCallback(
305 }
306
307 for (auto i = zigbeeStacks.Begin(); i != zigbeeStacks.End(); i++)
308 {
309 int index = std::distance(zigbeeStacks.Begin(), i);
310 Ptr<ZigbeeStack> zstack = *i;
311
312 // Assign streams to the zigbee stacks based on the index to obtain
313 // reprodusable results from random events occurring inside the stack.
314 // For example, to obtain the same assigned short address in each device.
315 zstack->GetNwk()->AssignStreams(index);
316
317 if (index == 0)
318 {
319 // 1 - Initiate the Zigbee coordinator, start the network
320 // ALL_CHANNELS = 0x07FFF800 (Channels 11~26)
322 netFormParams.m_scanChannelList.channelPageCount = 1;
324 netFormParams.m_scanDuration = 0;
325 netFormParams.m_superFrameOrder = 15;
326 netFormParams.m_beaconOrder = 15;
327
328 Simulator::ScheduleWithContext(zstack->GetNode()->GetId(),
329 MilliSeconds(index * 500),
331 zstack->GetNwk(),
332 netFormParams);
333 }
334 else
335 {
336 // 2- Let devices discovery the coordinator or routers and join the network, after
337 // this, it will become router itself(call to NLME-START-ROUTER.request). We
338 // continue doing the same with the rest of the devices which will discover the
339 // previously added routers and join the network
341 netDiscParams.m_scanChannelList.channelPageCount = 1;
342 netDiscParams.m_scanChannelList.channelsField[0] = 0x00007800; // BitMap: Channels 11~14
343 netDiscParams.m_scanDuration = 0;
344
345 Simulator::ScheduleWithContext(zstack->GetNode()->GetId(),
346 Seconds(2 + index * 10),
348 zstack->GetNwk(),
349 netDiscParams);
350 }
351 }
352
353 // 3- Create the routes towards the concentrator (Node 0)
354 // Print the trace route from Node 49 to the concentrator (Node 0)
355 // Print the contents of tables in the source of trace (Node 49)
358 zigbeeStacks.Get(0),
359 zigbeeStacks.Get(49));
360
364 return 0;
365}
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
helps to manage and create IEEE 802.15.4 NetDevice objects
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel associated to this helper.
void SetExtendedAddresses(NetDeviceContainer c)
Set the extended 64 bit addresses (EUI-64) for a group of LrWpanNetDevices.
NetDeviceContainer Install(NodeContainer c)
Install a LrWpanNetDevice and the associated structures (e.g., channel) in the nodes.
This class can contain 16 bit addresses.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
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 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 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:595
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:403
@ S
second
Definition nstime.h:105
Hold an unsigned integer type.
Definition uinteger.h:34
Setup a Zigbee stack to be used with LrWpanNetDevice.
zigbee::ZigbeeStackContainer Install(NetDeviceContainer c)
Install the Zigbee stack on top of an existing LrWpanNetDevice.
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 PrintRoutingTable(Ptr< OutputStreamWrapper > stream) const
Print the entries in the routing table.
void NlmeStartRouterRequest(NlmeStartRouterRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.9 NLME-START-ROUTER.request This primitive allows the nex...
void PrintNeighborTable(Ptr< OutputStreamWrapper > stream) const
Print the entries in the neighbor table.
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 PrintRouteDiscoveryTable(Ptr< OutputStreamWrapper > stream)
Print the entries in the route discovery table.
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 ...
void NlmeRouteDiscoveryRequest(NlmeRouteDiscoveryRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.33.3 and 3.6.3.5 NLME-ROUTE-DISCOVERY....
Holds a vector of ns3::ZigbeeStack pointers.
Iterator End() const
Get an iterator which indicates past the last ZigbeeStack in the container.
Iterator Begin() const
Get and iterator which refers to the first ZigbeeStack in the container.
Ptr< ZigbeeStack > Get(uint32_t i) const
Get a stack element from the container.
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:436
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
@ ROUTER
Router device.
@ NO_ADDRESS
No destination address.
Definition zigbee-nwk.h:76
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.
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_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
mobility
Definition third.py:92
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-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
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.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-ROUTE-DISCOVERY.confirm params.
Definition zigbee-nwk.h:419
NLME-ROUTE-DISCOVERY.request params.
Definition zigbee-nwk.h:404
AddressMode m_dstAddrMode
Specifies the kind of destination address.
Definition zigbee-nwk.h:405
NLME-START-ROUTER.request params.
Definition zigbee-nwk.h:588
static void CreateManyToOneRoutes(Ptr< ZigbeeStack > zigbeeStackConcentrator, Ptr< ZigbeeStack > zigbeeStackSrc)
static void NwkNetworkDiscoveryConfirm(Ptr< ZigbeeStack > stack, NlmeNetworkDiscoveryConfirmParams params)
static void TraceRoute(Mac16Address src, Mac16Address dst)
static void NwkDataIndication(Ptr< ZigbeeStack > stack, NldeDataIndicationParams params, Ptr< Packet > p)
static void NwkJoinConfirm(Ptr< ZigbeeStack > stack, NlmeJoinConfirmParams params)
ZigbeeStackContainer zigbeeStacks
static void NwkRouteDiscoveryConfirm(Ptr< ZigbeeStack > stack, NlmeRouteDiscoveryConfirmParams params)
static void NwkNetworkFormationConfirm(Ptr< ZigbeeStack > stack, NlmeNetworkFormationConfirmParams params)