A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-nwk-association-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 * Author:
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 an association-based join.
13 * The procedure requires a sequence of primitive calls on a specific order in the indicated
14 * devices.
15 *
16 *
17 * Network Extended PAN id: 0X000000000000CA:FE (based on the PAN coordinator address)
18 *
19 * 1-Network-Formation(dev0) 2-Network Discovery(dev1) 4-Network
20 * Discovery(dev2) 3-Router Start(dev1)
21 *
22 * Zigbee Coordinator(ZC) Router(ZR1) Router(ZR2)
23 * (dev 0) ------------------------------------ (dev 1) ---------------------------- (dev 2)
24 * [00:00:00:00:00:00:CA:FE] [00:00:00:00:00:00:00:01] [00:00:00:00:00:00:00:02]
25 * [00:00] [short addr assigned by ZC] [short addr assigned by
26 * ZR1]
27 *
28 * 1- Channel scanning, pan id selection, start network (initiation of zigbee coordinator)
29 * 2-
30 */
31
32#include "ns3/constant-position-mobility-model.h"
33#include "ns3/core-module.h"
34#include "ns3/log.h"
35#include "ns3/lr-wpan-module.h"
36#include "ns3/packet.h"
37#include "ns3/propagation-delay-model.h"
38#include "ns3/propagation-loss-model.h"
39#include "ns3/simulator.h"
40#include "ns3/single-model-spectrum-channel.h"
41#include "ns3/zigbee-module.h"
42
43#include <iostream>
44
45using namespace ns3;
46using namespace ns3::lrwpan;
47using namespace ns3::zigbee;
48
49NS_LOG_COMPONENT_DEFINE("ZigbeeAssociationJoin");
50
51static void
53{
54 std::cout << "Received packet of size " << p->GetSize() << "\n";
55}
56
57static void
59{
60 std::cout << "NlmeNetworkFormationConfirmStatus = " << params.m_status << "\n";
61}
62
63static void
65{
66 // See Zigbee Specification r22.1.0, 3.6.1.4.1
67 // This method implements a simplistic version of the method implemented
68 // in a zigbee APL layer. In this layer a candidate Extended PAN Id must
69 // be selected and a NLME-JOIN.request must be issued.
70
71 if (params.m_status == NwkStatus::SUCCESS)
72 {
73 std::cout << " Network discovery confirm Received. Networks found:\n";
74
75 for (const auto& netDescriptor : params.m_netDescList)
76 {
77 std::cout << " ExtPanID: 0x" << std::hex << netDescriptor.m_extPanId << std::dec
78 << " CH: " << static_cast<uint32_t>(netDescriptor.m_logCh) << std::hex
79 << " Pan Id: 0x" << netDescriptor.m_panId << " stackprofile " << std::dec
80 << static_cast<uint32_t>(netDescriptor.m_stackProfile) << "\n";
81 }
82
83 NlmeJoinRequestParams joinParams;
84
86 capaInfo.SetDeviceType(ROUTER);
87 capaInfo.SetAllocateAddrOn(true);
88
89 joinParams.m_rejoinNetwork = zigbee::JoiningMethod::ASSOCIATION;
90 joinParams.m_capabilityInfo = capaInfo.GetCapability();
91 joinParams.m_extendedPanId = params.m_netDescList[0].m_extPanId;
92
93 Simulator::ScheduleNow(&ZigbeeNwk::NlmeJoinRequest, stack->GetNwk(), joinParams);
94 }
95 else
96 {
97 NS_ABORT_MSG("Unable to discover networks | status: " << params.m_status);
98 }
99}
100
101static void
103{
104 if (params.m_status == NwkStatus::SUCCESS)
105 {
106 std::cout << Simulator::Now().As(Time::S)
107 << " The device joined the network SUCCESSFULLY with short address " << std::hex
108 << params.m_networkAddress << " on the Extended PAN Id: " << std::hex
109 << params.m_extendedPanId << "\n"
110 << std::dec;
111
112 // 3 - After dev 1 is associated, it should be started as a router
113 // (i.e. it becomes able to accept request from other devices to join the network)
114 NlmeStartRouterRequestParams startRouterParams;
116 stack->GetNwk(),
117 startRouterParams);
118 }
119 else
120 {
121 std::cout << " The device FAILED to join the network with status " << params.m_status
122 << "\n";
123 }
124}
125
126int
127main(int argc, char* argv[])
128{
131 // LogComponentEnable("LrWpanMac", LOG_LEVEL_DEBUG);
132 // LogComponentEnable("LrWpanPhy", LOG_LEVEL_DEBUG);
133
135 nodes.Create(3);
136
137 //// Configure MAC
138
139 LrWpanHelper lrWpanHelper;
140 NetDeviceContainer lrwpanDevices = lrWpanHelper.Install(nodes);
141 Ptr<LrWpanNetDevice> dev0 = lrwpanDevices.Get(0)->GetObject<LrWpanNetDevice>();
142 Ptr<LrWpanNetDevice> dev1 = lrwpanDevices.Get(1)->GetObject<LrWpanNetDevice>();
143 Ptr<LrWpanNetDevice> dev2 = lrwpanDevices.Get(2)->GetObject<LrWpanNetDevice>();
144
145 dev0->GetMac()->SetExtendedAddress("00:00:00:00:00:00:CA:FE");
146 dev1->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:01");
147 dev2->GetMac()->SetExtendedAddress("00:00:00:00:00:00:00:02");
148
152
155
156 channel->AddPropagationLossModel(propModel);
157 channel->SetPropagationDelayModel(delayModel);
158
159 dev0->SetChannel(channel);
160 dev1->SetChannel(channel);
161 dev2->SetChannel(channel);
162
163 //// Configure NWK
164
165 ZigbeeHelper zigbee;
166 ZigbeeStackContainer zigbeeStackContainer = zigbee.Install(lrwpanDevices);
167
168 Ptr<ZigbeeStack> zstack0 = zigbeeStackContainer.Get(0)->GetObject<ZigbeeStack>();
169 Ptr<ZigbeeStack> zstack1 = zigbeeStackContainer.Get(1)->GetObject<ZigbeeStack>();
170 Ptr<ZigbeeStack> zstack2 = zigbeeStackContainer.Get(2)->GetObject<ZigbeeStack>();
171
172 //// Configure Nodes Mobility
173
175 dev0Mobility->SetPosition(Vector(0, 0, 0));
176 dev0->GetPhy()->SetMobility(dev0Mobility);
177
179 dev1Mobility->SetPosition(Vector(0, 10, 0));
180 dev1->GetPhy()->SetMobility(dev1Mobility);
181
183 dev2Mobility->SetPosition(Vector(0, 20, 0)); // try with distance of 95
184 dev2->GetPhy()->SetMobility(dev2Mobility);
185
186 // NWK callbacks hooks
187
188 zstack0->GetNwk()->SetNlmeNetworkFormationConfirmCallback(
190
191 zstack0->GetNwk()->SetNldeDataIndicationCallback(
193
194 zstack1->GetNwk()->SetNldeDataIndicationCallback(
196
197 zstack2->GetNwk()->SetNldeDataIndicationCallback(
199
200 zstack1->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
202
203 zstack2->GetNwk()->SetNlmeNetworkDiscoveryConfirmCallback(
205
206 zstack2->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack2));
207
208 zstack1->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack1));
209
210 zstack2->GetNwk()->SetNlmeJoinConfirmCallback(MakeBoundCallback(&NwkJoinConfirm, zstack2));
211
212 // 1 - Initiate the Zigbee coordinator, start the network
214 netFormParams.m_scanChannelList.channelPageCount = 1;
216 netFormParams.m_scanDuration = 0;
217 netFormParams.m_superFrameOrder = 15;
218 netFormParams.m_beaconOrder = 15;
219
220 Simulator::ScheduleWithContext(zstack0->GetNode()->GetId(),
221 Seconds(1),
223 zstack0->GetNwk(),
224 netFormParams);
225
226 // 2- Let the dev1 (Router) discovery the coordinator and join the network, after
227 // this, it will become router itself(call to NLME-START-ROUTER.request).
229 netDiscParams.m_scanChannelList.channelPageCount = 1;
230 netDiscParams.m_scanChannelList.channelsField[0] = 0x7800;
231 netDiscParams.m_scanDuration = 14;
232 Simulator::ScheduleWithContext(zstack1->GetNode()->GetId(),
233 Seconds(3),
235 zstack1->GetNwk(),
236 netDiscParams);
237
238 // 4- (Note: Look for step 3 in NetworkDiscoveryRequestConfirm)
239 // Let the dev2 discover the PAN coordinator or a router and join the network,
240 // after this, it will become a router itself
241
243 netDiscParams2.m_scanChannelList.channelPageCount = 1;
244 netDiscParams2.m_scanChannelList.channelsField[0] = 0x7800;
245 netDiscParams2.m_scanDuration = 14;
246 Simulator::ScheduleWithContext(zstack2->GetNode()->GetId(),
247 Seconds(1020),
249 zstack2->GetNwk(),
250 netDiscParams2);
251
254 return 0;
255}
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.
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 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
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
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 NlmeStartRouterRequest(NlmeStartRouterRequestParams params)
Zigbee Specification r22.1.0, section 3.2.2.9 NLME-START-ROUTER.request This primitive allows the nex...
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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
@ ROUTER
Router device.
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-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-START-ROUTER.request params.
Definition zigbee-nwk.h:588
static void NwkNetworkDiscoveryConfirm(Ptr< ZigbeeStack > stack, NlmeNetworkDiscoveryConfirmParams params)
static void NwkDataIndication(Ptr< ZigbeeStack > stack, NldeDataIndicationParams params, Ptr< Packet > p)
static void NwkJoinConfirm(Ptr< ZigbeeStack > stack, NlmeJoinConfirmParams params)
static void NwkNetworkFormationConfirm(Ptr< ZigbeeStack > stack, NlmeNetworkFormationConfirmParams params)