A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-nwk-header.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 * Authors:
7 * Ryo Okuda <c611901200@tokushima-u.ac.jp>
8 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
9 */
10
11#include "zigbee-nwk-header.h"
12
13#include "ns3/address-utils.h"
14
15namespace ns3
16{
17namespace zigbee
18{
19
32
36
37void
39{
40 m_fctrlFrmType = nwkType;
41}
42
48
49void
51{
52 m_fctrlPrtVer = ver;
53}
54
55uint8_t
60
61void
63{
64 m_fctrlDscvRoute = discoverRoute;
65}
66
72
73void
78
79bool
81{
82 return m_fctrlMcst;
83}
84
85bool
90
91void
96
97bool
102
103void
108
111{
112 return (m_dstAddr);
113}
114
115void
120
123{
124 return (m_srcAddr);
125}
126
127void
129{
130 m_radius = radius;
131}
132
133uint8_t
135{
136 return (m_radius);
137}
138
139void
141{
142 m_seqNum = seqNum;
143}
144
145uint8_t
147{
148 return (m_seqNum);
149}
150
151void
157
160{
161 return (m_dstIeeeAddr);
162}
163
164void
170
173{
174 return (m_srcIeeeAddr);
175}
176
177void
182
185{
186 return m_mcstMode;
187}
188
189void
191{
192 m_nonMemberRadius = radius;
193}
194
195uint8_t
200
201void
203{
204 m_maxNonMemberRadius = radius;
205}
206
207uint8_t
212
213void
215{
216 m_fctrlFrmType = static_cast<NwkType>((frameControl) & (0x03)); // Bit 0-1
217 m_fctrlPrtVer = (frameControl >> 2) & (0x0F); // Bit 2-5
218 m_fctrlDscvRoute = static_cast<DiscoverRouteType>((frameControl >> 6) & (0x03)); // Bit 6-7
219 m_fctrlMcst = (frameControl >> 8) & (0x01); // Bit 8
220 m_fctrlSecurity = (frameControl >> 9) & (0x01); // Bit 9
221 m_fctrlSrcRoute = (frameControl >> 10) & (0x01); // Bit 10
222 m_fctrlDstIEEEAddr = (frameControl >> 11) & (0x01); // Bit 11
223 m_fctrlSrcIEEEAddr = (frameControl >> 12) & (0x01); // Bit 12
224 m_fctrlEndDevIni = (frameControl >> 13) & (0x01); // Bit 13
225}
226
227uint16_t
229{
230 uint16_t val = 0;
231
232 val = m_fctrlFrmType & (0x03); // Bit 0-1
233 val |= (m_fctrlPrtVer << 2) & (0x0F << 2); // Bit 2-5
234 val |= (m_fctrlDscvRoute << 6) & (0x03 << 6); // Bit 6-7
235 val |= (m_fctrlMcst << 8) & (0x01 << 8); // Bit 8
236 val |= (m_fctrlSecurity << 9) & (0x01 << 9); // Bit 9
237 val |= (m_fctrlSrcRoute << 10) & (0x01 << 10); // Bit 10
238 val |= (m_fctrlDstIEEEAddr << 11) & (0x01 << 11); // Bit 11
239 val |= (m_fctrlSrcIEEEAddr << 12) & (0x01 << 12); // Bit 12
240 val |= (m_fctrlEndDevIni << 13) & (0x01 << 13); // Bit 13
241
242 return val;
243}
244
245void
247{
248 m_mcstMode = static_cast<MulticastMode>((multicastControl) & (0x03)); // Bit 0-1
249 m_nonMemberRadius = (multicastControl >> 2) & (0x07); // Bit 2-4
250 m_maxNonMemberRadius = (multicastControl >> 5) & (0x07); // Bit 5-7
251}
252
253uint8_t
255{
256 uint8_t mcstCtrl = 0;
257
258 mcstCtrl = m_mcstMode & (0x03); // Bit 0-1
259 mcstCtrl |= (m_nonMemberRadius << 2) & (0x07 << 2); // Bit 2-4
260 mcstCtrl |= (m_maxNonMemberRadius << 5) & (0x07 << 5); // Bit 5-7
261
262 return mcstCtrl;
263}
264
265void
267{
268 Buffer::Iterator i = start;
269
270 uint16_t frameControl = GetFrameControl();
271 i.WriteHtolsbU16(frameControl);
272
273 WriteTo(i, m_dstAddr);
274 WriteTo(i, m_srcAddr);
275 i.WriteU8(m_radius);
276 i.WriteU8(m_seqNum);
277
279 {
281 }
282
284 {
286 }
287
288 if (m_fctrlMcst)
289 {
291 }
292
293 // TODO: Add Source route subframe when supported
294}
295
298{
299 Buffer::Iterator i = start;
300 uint16_t frameControl = i.ReadLsbtohU16();
301 SetFrameControl(frameControl);
304 SetRadius(i.ReadU8());
305 SetSeqNum(i.ReadU8());
306
308 {
310 }
311
313 {
315 }
316
317 if (m_fctrlMcst)
318 {
319 uint8_t mcstCtrl = i.ReadU8();
320 SetMulticastControl(mcstCtrl);
321 }
322
323 // TODO: Add Source route subframe when supported
324
325 return i.GetDistanceFrom(start);
326}
327
330{
331 /*
332 * Each NWK header will have:
333 *
334 * Frame Control : 2 octet
335 *
336 * Routing Fields:
337 * Destination address : 2 octet
338 * Source address : 2 octet
339 * Radius : 1 octet
340 * Sequence number : 1 octet
341 * Destination IEEE address : 0/8 octet
342 * Source IEEE address : 0/8 octet
343 * Multicast control : 0/1 octet
344 * Source route subframe : variable
345 */
346
347 uint32_t size = 8;
348
350 {
351 size += 8;
352 }
353
355 {
356 size += 8;
357 }
358
359 if (m_fctrlMcst)
360 {
361 size += 1;
362 }
363
364 // TODO: Add Source route subframe when supported.
365
366 return (size);
367}
368
369TypeId
371{
372 static TypeId tid = TypeId("ns3::zigbee::ZigbeeNwkHeader")
373 .SetParent<Header>()
374 .SetGroupName("Zigbee")
375 .AddConstructor<ZigbeeNwkHeader>();
376 return tid;
377}
378
379TypeId
381{
382 return GetTypeId();
383}
384
385void
386ZigbeeNwkHeader::Print(std::ostream& os) const
387{
388 os << "\n";
389 switch (m_fctrlFrmType)
390 {
391 case DATA:
392 os << "Frame Type = DATA ";
393 break;
394 case NWK_COMMAND:
395 os << "Frame Type = NWK_COMMAND ";
396 break;
397 case INTER_PAN:
398 os << "Frame Type = INTER_PAN ";
399 break;
400 }
401 os << " | Protocol Version = " << static_cast<uint32_t>(m_fctrlPrtVer)
402 << " | Security Enabled = " << static_cast<uint32_t>(m_fctrlSecurity)
403 << " | Source Route = " << static_cast<uint32_t>(m_fctrlSrcRoute);
404
405 switch (m_fctrlDscvRoute)
406 {
408 os << " | Enable Route Discovery = SUPPRESS ";
409 break;
411 os << " | Enable Route Discovery = ENABLED ";
412 break;
413 }
414
415 os << "\nDst Addr = " << m_dstAddr << " | Src Addr = " << m_srcAddr
416 << " | Sequence Num = " << static_cast<uint32_t>(m_seqNum)
417 << " | Radius = " << static_cast<uint32_t>(m_radius);
418
420 {
421 os << "\nDst IEEE Addr = " << m_dstIeeeAddr;
422 }
423
425 {
426 os << " | Src IEEE Addr = " << m_srcIeeeAddr;
427 }
428
429 if (m_fctrlMcst)
430 {
431 switch (m_mcstMode)
432 {
433 case MEMBER:
434 os << " | Mcst mode: MEMBER ";
435 break;
436 case NONMEMBER:
437 os << " | Mcst mode: NONMEMBER ";
438 break;
439 }
440
441 os << " | Non Member radius = " << static_cast<uint32_t>(m_nonMemberRadius)
442 << " | Max Non Member radius = " << static_cast<uint32_t>(m_maxNonMemberRadius);
443 }
444 os << "\n";
445
446 // TODO: Add Source route subframe when supported.
447}
448} // namespace zigbee
449} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void WriteHtolsbU16(uint16_t data)
Definition buffer.cc:891
void WriteU8(uint8_t data)
Definition buffer.h:870
uint16_t ReadLsbtohU16()
Definition buffer.cc:1053
uint32_t GetDistanceFrom(const Iterator &o) const
Definition buffer.cc:769
Protocol header serialization and deserialization.
Definition header.h:33
This class can contain 16 bit addresses.
an EUI-64 address
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Represent the NWK Header with the Frame Control and Routing fields Zigbee Specification r22....
uint8_t GetMulticastControl() const
Get The Multicast control field.
void SetMulticast()
Set to true the value of the multicast flag in the frame control field.
void SetDstIeeeAddr(Mac64Address dst)
Set the destination IEEE address.
bool GetEndDeviceInitiator() const
Get whether or not the source of this message was an end device.
void SetDiscoverRoute(enum DiscoverRouteType discoverRoute)
Suppress or enable route discovery for this frame.
void SetSrcAddr(Mac16Address addr)
Set Source address.
void SetFrameControl(uint16_t frameControl)
Set the Frame control field.
uint8_t m_radius
Radius (1 Octet)
void SetEndDeviceInitiator()
This flag is used by the NWK to indicate if the the initiator device of the message is an end device ...
bool m_fctrlDstIEEEAddr
Destination IEEE address flag (Bit 11)
bool m_fctrlSecurity
Security flag (Bit 9)
Mac64Address m_srcIeeeAddr
Source IEEE address (0 or 8 Octets)
uint8_t m_fctrlPrtVer
Protocol version (Bit 2-5)
uint8_t m_maxNonMemberRadius
Max non member radius sub-field (Bit 5-7)
bool m_fctrlSrcRoute
Source route flag (Bit 10)
uint32_t GetSerializedSize() const override
uint32_t Deserialize(Buffer::Iterator start) override
DiscoverRouteType m_fctrlDscvRoute
Discover route (Bit 6-7)
uint16_t GetFrameControl() const
Get the Frame control field.
void SetProtocolVer(uint8_t ver)
Set the Protocol version.
static TypeId GetTypeId()
Get the type ID.
MulticastMode GetMulticastMode() const
Get the multicast mode.
bool m_fctrlEndDevIni
End device initiator flag (Bit 13)
bool IsMulticast() const
Inform whether or not the current frame is used in multicast.
void SetSeqNum(uint8_t seqNum)
Set the Sequence number.
void SetNonMemberRadius(uint8_t radius)
Set the non-member radius.
uint8_t m_nonMemberRadius
Non member radius sub-field (Bits 2-4)
uint8_t GetNonMemberRadius() const
Get the non-member radius.
uint8_t GetRadius() const
Get the Radius.
Mac64Address GetDstIeeeAddr() const
Get the destination IEEE address.
bool IsSecurityEnabled() const
Inform whether or not security is enabled.
uint8_t GetSeqNum() const
Get the frame Sequence number.
bool m_fctrlMcst
Multicast flag (Bit 8)
Mac64Address GetSrcIeeeAddr() const
Get the source IEEE address.
uint8_t m_seqNum
Sequence number (1 Octet)
Mac16Address m_dstAddr
Destination address (2 Octets)
void SetSrcIeeeAddr(Mac64Address src)
Set the source IEEE address.
NwkType GetFrameType() const
Set the Frame Control field "Frame Type" bits.
void Serialize(Buffer::Iterator start) const override
void SetRadius(uint8_t radius)
Set the Radius.
void SetMulticastMode(MulticastMode mode)
Set the multicast mode.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetFrameType(enum NwkType nwkType)
Set the Frame type used (Data or Command)
uint8_t GetMaxNonMemberRadius() const
Get the maximum non-member radius.
Mac64Address m_dstIeeeAddr
Destination IEEE address (0 or 8 Octets)
void SetMaxNonMemberRadius(uint8_t radius)
Set the maximum non-member radius.
NwkType m_fctrlFrmType
Frame type (Bit 0-1)
Mac16Address GetDstAddr() const
Get the Destination address.
void SetDstAddr(Mac16Address addr)
Set Destination address.
uint8_t GetProtocolVer() const
Get Frame protocol version.
DiscoverRouteType GetDiscoverRoute() const
Get the status of frame discovery route (suppress or enabled)
Mac16Address m_srcAddr
Source address (2 Octets)
Mac16Address GetSrcAddr() const
Get the Source address.
void Print(std::ostream &os) const override
MulticastMode m_mcstMode
Multicast mode sub-field (Bits 0-1)
void SetMulticastControl(uint8_t multicastControl)
Set the Multicast control field (mcstMode, nonMemberRadius, maxNonMemberRadius)
bool m_fctrlSrcIEEEAddr
Source IEEE address flag (Bit 12)
DiscoverRouteType
Zigbee Specification r22.1.0, Values of the discover route sub-field (Table 3-47)
@ SUPPRESS_ROUTE_DISCOVERY
Suppress route discovery.
@ ENABLE_ROUTE_DISCOVERY
Enable route discovery.
NwkType
Zigbee Specification r22.1.0, Values of the frame Type Sub-field (Table 3-46)
@ NWK_COMMAND
Network command frame type.
@ DATA
Data frame type.
@ INTER_PAN
Inter-Pan frame type.
MulticastMode
Zigbee Specification r22.1.0, Multicast Mode Sub-Field (Table 3-7)
@ MEMBER
Member multicast mode.
@ NONMEMBER
Non member multicast mode.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.