A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-zdo.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 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#include "zigbee-zdo.h"
12
15
16#include "ns3/log.h"
17#include "ns3/packet.h"
18#include "ns3/simulator.h"
19#include "ns3/zigbee-profiles.h"
20
21namespace ns3
22{
23namespace zigbee
24{
25
26NS_LOG_COMPONENT_DEFINE("ZigbeeZdo");
28
29TypeId
31{
32 static TypeId tid = TypeId("ns3::zigbee::ZigbeeZdo")
34 .SetGroupName("Zigbee")
35 .AddConstructor<ZigbeeZdo>();
36 return tid;
37}
38
43
44void
49
54
55void
61
62void
67
68void
70{
71 m_aps = aps;
72}
73
76{
77 return m_aps;
78}
79
80void
82{
83 m_nwk = nwk;
84}
85
88{
89 return m_nwk;
90}
91
92void
94{
95 NS_LOG_FUNCTION(this);
96
97 NS_ABORT_MSG_IF(params.m_profileId != ZigbeeProfiles::ZDP_PROFILE,
98 "Error, in ZDO the profile must be 0");
99 NS_ABORT_MSG_IF(params.m_dstEndPoint != 0, "Error, in ZDO the endpoint must be 0");
100
101 // All response commands use cluster identifiers greater than or equal to 0x8000,
102 // while all request commands use cluster identifiers less than 0x8000.
103 if (params.m_clusterId < ZdpClustersServerCmds::NWK_ADDR_RSP)
104 {
105 // CLIENT REQUESTS
106 switch (params.m_clusterId)
107 {
109 HandleNwkAddrRequest(asdu, params);
110 break;
111
112 default:
113 break;
114 }
115 }
116 else
117 {
118 // SERVER RESPONSES
119 switch (params.m_clusterId)
120 {
122 HandleNwkAddrResponse(asdu, params);
123 break;
124
125 default:
126 break;
127 }
128 }
129}
130
131void
133{
134 NS_LOG_FUNCTION(this);
135
136 if (params.m_requestType == NwkAddrRequestType::EXTENDED_RESPONSE)
137 {
138 // TODO: Add support to requests associated devices.
139 return;
140 }
141
142 Ptr<Packet> zdpFrame = Create<Packet>();
143
144 // The ZDP frame
145 NwkAddrRequestCmd nwkAddrRequestCmd;
146 nwkAddrRequestCmd.SetTsn(m_tsn.GetValue());
147 m_tsn++;
148 nwkAddrRequestCmd.SetIeeeAddress(params.m_ieeeAddr);
149 nwkAddrRequestCmd.SetRequestType(static_cast<uint8_t>(params.m_requestType));
150 nwkAddrRequestCmd.SetStartIndex(params.m_startIndex);
151 zdpFrame->AddHeader(nwkAddrRequestCmd);
152
153 ApsdeDataRequestParams apsdeParams;
155 apsdeParams.m_dstAddr16 = params.m_dstAddr;
156 apsdeParams.m_dstEndPoint = 0x00; // ZDO endpoint
159 apsdeParams.m_srcEndPoint = 0x00; // ZDO endpoint
160 apsdeParams.m_asduLength = zdpFrame->GetSize();
161 apsdeParams.m_txOptions = 0; // No special transmission options
162 apsdeParams.m_useAlias = false; // Not using alias
163 apsdeParams.m_radius = 0; // Default radius
164
165 m_aps->ApsdeDataRequest(apsdeParams, zdpFrame);
166}
167
168void
170{
171 NS_LOG_FUNCTION(this);
172
173 Ptr<Packet> zdpFrame = Create<Packet>();
174
175 // The ZDP frame
176 NwkAddrResponseCmd nwkAddrResponseCmd;
177 nwkAddrResponseCmd.SetTsn(params.m_tsn);
178 nwkAddrResponseCmd.SetStatus(static_cast<uint8_t>(params.m_status));
179 nwkAddrResponseCmd.SetIeeeAddrRemoteDev(params.m_ieeeAddrRemoteDev);
180 nwkAddrResponseCmd.SetNwkAddrRemoteDev(params.m_nwkAddrRemoteDev);
181 zdpFrame->AddHeader(nwkAddrResponseCmd);
182
183 ApsdeDataRequestParams apsdeParams;
185 apsdeParams.m_dstAddr16 = params.m_dstAddr;
186 apsdeParams.m_dstEndPoint = 0x00; // ZDO endpoint
189 apsdeParams.m_srcEndPoint = 0x00; // ZDO endpoint
190 apsdeParams.m_asduLength = zdpFrame->GetSize();
191 apsdeParams.m_txOptions = 0; // No special transmission options
192 apsdeParams.m_useAlias = false; // Not using alias
193 apsdeParams.m_radius = 0; // Default radius
194
195 m_aps->ApsdeDataRequest(apsdeParams, zdpFrame);
196}
197
198void
200{
201 NS_LOG_FUNCTION(this);
202
203 // Get the information from the request command
204 NwkAddrRequestCmd requestCmd;
205 asdu->RemoveHeader(requestCmd);
206
207 Mac64Address requestedIeeeAddr = requestCmd.GetIeeeAddress();
208
209 // Zigbee broadcast addresses.
210 bool isBroadcast = dataParams.m_dstAddr16 == "FF:FF" || // All devices
211 dataParams.m_dstAddr16 == "FF:FD" || // macRxOnWhenIdle = TRUE devices
212 dataParams.m_dstAddr16 == "FF:FC" || // All routers and coordinator
213 dataParams.m_dstAddr16 == "FF:FB"; // Low power routers
214
215 // Compare the requested IEEE address to our own IEEE address
216 // or to an address known to the NWK address map. The address map
217 // is used instead of the neighbor table because the latter might not always
218 // contain the necessary information and some stacks use it instead
219 // of the neighbor table (opossed to what is described in the specification)
220 Mac16Address matchedNwkAddr;
221 bool matchFound = false;
222
223 if (m_nwk->GetIeeeAddress() == requestedIeeeAddr)
224 {
225 // I am the device the NWK_addr_req is looking for.
226 matchedNwkAddr = m_nwk->GetNetworkAddress();
227 matchFound = true;
228 }
229 else if (m_nwk->GetNwkAddrByIeeeAddr(requestedIeeeAddr, matchedNwkAddr))
230 {
231 matchFound = true;
232 }
233
234 NwkAddrResponseParams responseParams;
235 responseParams.m_dstAddr = dataParams.m_srcAddress16;
236 responseParams.m_tsn = requestCmd.GetTsn();
237 responseParams.m_ieeeAddrRemoteDev = requestedIeeeAddr;
238
239 if (!matchFound)
240 {
241 // No match: a broadcast request is silently discarded, a unicast request
242 // is answered with a DEVICE_NOT_FOUND status and an unknown short address.
243 if (isBroadcast)
244 {
245 return;
246 }
247
248 responseParams.m_status = ZdpStatus::DEVICE_NOT_FOUND;
249 responseParams.m_nwkAddrRemoteDev = Mac16Address("FF:FF");
250 NwkAddrResponse(responseParams);
251 return;
252 }
253
254 // A match was found: the request type determines the response.
255 responseParams.m_nwkAddrRemoteDev = matchedNwkAddr;
256
257 switch (static_cast<NwkAddrRequestType>(requestCmd.GetRequestType()))
258 {
260 responseParams.m_status = ZdpStatus::SUCCESS;
261 NwkAddrResponse(responseParams);
262 break;
263
265 // TODO: An extended response must also include the list of devices
266 // associated to the matched device (NumAssocDev, StartIndex and
267 // NWKAddrAssocDevList fields).
268 break;
269
270 default:
271 // Reserved request type value: a unicast request is answered with an
272 // INV_REQUESTTYPE status, a broadcast request is discarded.
273 if (!isBroadcast)
274 {
275 responseParams.m_status = ZdpStatus::INV_REQUESTTYPE;
276 NwkAddrResponse(responseParams);
277 }
278 break;
279 }
280}
281
282void
284{
285 NS_LOG_FUNCTION(this);
286
287 // Get the information from the response command
288 NwkAddrResponseCmd responseCmd;
289 asdu->RemoveHeader(responseCmd);
290
291 if (static_cast<ZdpStatus>(responseCmd.GetStatus()) == ZdpStatus::SUCCESS)
292 {
293 // If the response was successful,
294 // update the NWK address map with the information received in the response.
295 m_nwk->UpdateNwkAddrMap(responseCmd.GetNwkAddrRemoteDev(),
296 responseCmd.GetIeeeAddrRemoteDev());
297 }
298
299 // Notify the application framework (AF) that a NWK_addr_rsp was received.
300 if (!m_zdoNwkAddrRspIndCallback.IsNull())
301 {
302 NwkAddrRespIndParams responseIndParams;
303 responseIndParams.m_status = static_cast<ZdpStatus>(responseCmd.GetStatus());
304 responseIndParams.m_ieeeAddrRemoteDev = responseCmd.GetIeeeAddrRemoteDev();
305 responseIndParams.m_nwkAddrRemoteDev = responseCmd.GetNwkAddrRemoteDev();
306 m_zdoNwkAddrRspIndCallback(responseIndParams);
307 }
308}
309
310void
315
316} // namespace zigbee
317} // namespace ns3
This class can contain 16 bit addresses.
an EUI-64 address
virtual void DoInitialize()
Initialize() implementation.
Definition object.cc:438
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
virtual void DoDispose()
Destructor implementation.
Definition object.cc:431
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Implements the NWK_addr_req command See Zigbee Specification r22.1.0, Section 2.4....
void SetRequestType(uint8_t type)
Set the request type, either single device or extended response.
void SetTsn(uint8_t tsn)
Set the transaction sequence number.
uint8_t GetTsn() const
Get the transaction sequence number.
uint8_t GetRequestType() const
Get the request type previously set for the network address request.
void SetIeeeAddress(Mac64Address ieeeAddress)
Set the IEEE address used as base for the network address request.
void SetStartIndex(uint8_t index)
Set the start index.
Mac64Address GetIeeeAddress()
Get the IEEE address set for the network address request.
NWK_addr_rsp Command Frame See Zigbee Specification Section 2.4.4.2.1, Figure 2-64.
Mac16Address GetNwkAddrRemoteDev()
Get the network address of the remote device.
void SetTsn(uint8_t tsn)
Set the transaction sequence number of the NWK_addr_rsp command.
void SetIeeeAddrRemoteDev(Mac64Address ieeeAddrRemoteDev)
Set the IEEE address of the remote device.
void SetStatus(uint8_t status)
Set the status of the NWK_addr_rsp command.
Mac64Address GetIeeeAddrRemoteDev()
Get the IEEE address of the remote device.
void SetNwkAddrRemoteDev(Mac16Address nwkAddrRemoteDev)
Set the network address of the remote device.
uint8_t GetStatus() const
Get the status of the NWK_addr_rsp command.
Callback< void, NwkAddrRespIndParams > ZdoNwkAddrRspIndCallback
This callback is called after a ASDU has successfully received and APS push it to deliver it to the n...
Definition zigbee-zdo.h:156
void HandleNwkAddrRequest(Ptr< Packet > asdu, ApsdeDataIndicationParams dataParams)
Effect on Receipt (NWK_addr_req) See Zigbee Specification 2017, Section 2.4.3.1.1.
Ptr< ZigbeeNwk > m_nwk
Pointer to the Zigbee Network Layer (NWK) object.
Definition zigbee-zdo.h:251
Ptr< ZigbeeNwk > GetNwk() const
Get the underlying APS used by the current Zigbee ZDO.
Definition zigbee-zdo.cc:87
ZdoNwkAddrRspIndCallback m_zdoNwkAddrRspIndCallback
This callback is used to to notify the result of a NWK_addr_req request in the ZDO to the Application...
Definition zigbee-zdo.h:249
void NwkAddrRequest(NwkAddrRequestParams params)
This function is used to send a Network Address (16-bit address)Request to a remote device when the I...
void ApsDataIndication(ApsdeDataIndicationParams params, Ptr< Packet > p)
This function is called when the Zigbee Stack receives an APSDE-DATA.indication with destination endp...
Definition zigbee-zdo.cc:93
SequenceNumber8 m_tsn
The transaction sequence number.
Definition zigbee-zdo.h:253
void NotifyConstructionCompleted() override
Notifier called once the ObjectBase is fully constructed.
Definition zigbee-zdo.cc:45
void SetAps(Ptr< ZigbeeAps > aps)
Set the underlying APS to use in this Zigbee ZDO.
Definition zigbee-zdo.cc:69
void SetNwk(Ptr< ZigbeeNwk > nwk)
Set the underlying NWK to use in this Zigbee ZDO.
Definition zigbee-zdo.cc:81
void DoInitialize() override
Initialize() implementation.
Definition zigbee-zdo.cc:56
Ptr< ZigbeeAps > GetAps() const
Get the underlying APS used by the current Zigbee ZDO.
Definition zigbee-zdo.cc:75
void SetZdoNwkAddrRspIndCallback(ZdoNwkAddrRspIndCallback c)
Set the indication callback to be called when a response (NWK_addr_rsp) to an NWK_addr_req is receive...
void NwkAddrResponse(NwkAddrResponseParams params)
This function is used to send a Network Address (16-bit address) Response to a remote device in reply...
static TypeId GetTypeId()
Get the TypeId of ZigbeeZdo class.
Definition zigbee-zdo.cc:30
Ptr< ZigbeeAps > m_aps
Pointer to the Zigbee Application Support Sub-layer (APS) object.
Definition zigbee-zdo.h:252
void HandleNwkAddrResponse(Ptr< Packet > asdu, ApsdeDataIndicationParams dataParams)
Effect on Receipt (NWK_addr_rsp) See Zigbee Specification 2017, Section 2.4.4.2.1.
void DoDispose() override
Destructor implementation.
Definition zigbee-zdo.cc:63
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:99
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
NwkAddrRequestType
Zigbee Device Profile (ZDP) enumeration of request types for NWK_addr_req command.
Definition zigbee-zdo.h:78
@ EXTENDED_RESPONSE
Request type for an extended response, not currently supported.
Definition zigbee-zdo.h:80
@ SINGLE_DEVICE_RESPONSE
Request type for a single device response.
Definition zigbee-zdo.h:79
@ DST_ADDR16_DST_ENDPOINT_PRESENT
16-bit destination address and destination endpoint present.
Definition zigbee-aps.h:51
@ NWK_ADDR_REQ
Network Address Request command (Optional).
@ NWK_ADDR_RSP
Network Address Response command (Mandatory).
ZdpStatus
Zigbee Device Profile (ZDP) enumeration of status codes.
Definition zigbee-zdo.h:38
@ DEVICE_NOT_FOUND
The device with the specified IEEE address was not found in the network.
Definition zigbee-zdo.h:41
@ SUCCESS
The request was successful.
Definition zigbee-zdo.h:39
@ INV_REQUESTTYPE
The request type is invalid or not supported.
Definition zigbee-zdo.h:40
@ ZDP_PROFILE
Zigbee Device Profile (ZDP).
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:166
Mac16Address m_dstAddr16
The destination 16-bit address.
Definition zigbee-aps.h:170
Mac16Address m_srcAddress16
The 16-bit address.
Definition zigbee-aps.h:177
Zigbee Specification r22.1.0, Section 2.2.4.1.1 APSDE-DATA.request params.
Definition zigbee-aps.h:124
uint16_t m_profileId
The application profile ID.
Definition zigbee-aps.h:130
uint16_t m_clusterId
The application cluster ID.
Definition zigbee-aps.h:131
Mac16Address m_dstAddr16
The destination 16-bit address.
Definition zigbee-aps.h:127
uint8_t m_txOptions
Transmission options.
Definition zigbee-aps.h:134
uint8_t m_srcEndPoint
The source endpoint.
Definition zigbee-aps.h:132
uint32_t m_asduLength
The ASDU length.
Definition zigbee-aps.h:133
bool m_useAlias
Indicates if alias is used in this transmission.
Definition zigbee-aps.h:135
ApsDstAddressMode m_dstAddrMode
Destination address mode.
Definition zigbee-aps.h:125
uint8_t m_dstEndPoint
The destination endpoint.
Definition zigbee-aps.h:129
uint8_t m_radius
Radius (Number of hops this message travels).
Definition zigbee-aps.h:138
ZDO Network Address Request Parameters.
Definition zigbee-zdo.h:90
ZDO Network Address Response Indication Parameters.
Definition zigbee-zdo.h:130
Mac16Address m_nwkAddrRemoteDev
The network address (16-bit address) of the remote device matching the request.
Definition zigbee-zdo.h:135
Mac64Address m_ieeeAddrRemoteDev
The IEEE address (64-bit address) of the remote device matching the request.
Definition zigbee-zdo.h:133
ZdpStatus m_status
The status of the NWK_addr_req being responded to.
Definition zigbee-zdo.h:131
ZDO Network Address Response Parameters.
Definition zigbee-zdo.h:106
uint8_t m_tsn
The transaction sequence number, must match the one used in the corresponding NWK_addr_req.
Definition zigbee-zdo.h:109
Mac16Address m_nwkAddrRemoteDev
The network address (16-bit address) of the remote device matching the request.
Definition zigbee-zdo.h:115
Mac16Address m_dstAddr
The destination of the response (the device that issued the NWK_addr_req).
Definition zigbee-zdo.h:107
ZdpStatus m_status
The status of the NWK_addr_req being responded to.
Definition zigbee-zdo.h:111
Mac64Address m_ieeeAddrRemoteDev
The IEEE address (64-bit address) of the remote device matching the request.
Definition zigbee-zdo.h:113