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
179{
180 m_fctrlFrmType = static_cast<NwkType>((frameControl) & (0x03)); // Bit 0-1
181 m_fctrlPrtVer = (frameControl >> 2) & (0x0F); // Bit 2-5
182 m_fctrlDscvRoute = static_cast<DiscoverRouteType>((frameControl >> 6) & (0x03)); // Bit 6-7
183 m_fctrlMcst = (frameControl >> 8) & (0x01); // Bit 8
184 m_fctrlSecurity = (frameControl >> 9) & (0x01); // Bit 9
185 m_fctrlSrcRoute = (frameControl >> 10) & (0x01); // Bit 10
186 m_fctrlDstIEEEAddr = (frameControl >> 11) & (0x01); // Bit 11
187 m_fctrlSrcIEEEAddr = (frameControl >> 12) & (0x01); // Bit 12
188 m_fctrlEndDevIni = (frameControl >> 13) & (0x01); // Bit 13
189}
190
191uint16_t
193{
194 uint16_t val = 0;
195
196 val = m_fctrlFrmType & (0x03); // Bit 0-1
197 val |= (m_fctrlPrtVer << 2) & (0x0F << 2); // Bit 2-5
198 val |= (m_fctrlDscvRoute << 6) & (0x03 << 6); // Bit 6-7
199 val |= (m_fctrlMcst << 8) & (0x01 << 8); // Bit 8
200 val |= (m_fctrlSecurity << 9) & (0x01 << 9); // Bit 9
201 val |= (m_fctrlSrcRoute << 10) & (0x01 << 10); // Bit 10
202 val |= (m_fctrlDstIEEEAddr << 11) & (0x01 << 11); // Bit 11
203 val |= (m_fctrlSrcIEEEAddr << 12) & (0x01 << 12); // Bit 12
204 val |= (m_fctrlEndDevIni << 13) & (0x01 << 13); // Bit 13
205
206 return val;
207}
208
209void
211{
212 m_mcstMode = static_cast<MulticastMode>((multicastControl) & (0x03)); // Bit 0-1
213 m_nonMemberRadius = (multicastControl >> 2) & (0x07); // Bit 2-4
214 m_maxNonMemberRadius = (multicastControl >> 5) & (0x07); // Bit 5-7
215}
216
217uint8_t
219{
220 uint8_t val = 0;
221
222 val = m_mcstMode & (0x03); // Bit 0-1
223 val |= (m_nonMemberRadius << 2) & (0x07 << 2); // Bit 2-4
224 val |= (m_maxNonMemberRadius << 5) & (0x07 << 5); // Bit 5-7
225
226 return val;
227}
228
229void
231{
232 Buffer::Iterator i = start;
233
234 uint16_t frameControl = GetFrameControl();
235 i.WriteHtolsbU16(frameControl);
236
237 WriteTo(i, m_dstAddr);
238 WriteTo(i, m_srcAddr);
239 i.WriteU8(m_radius);
240 i.WriteU8(m_seqNum);
241
243 {
245 }
246
248 {
250 }
251
252 if (m_fctrlMcst)
253 {
255 }
256
257 // TODO: Add Source route subframe when supported
258}
259
262{
263 Buffer::Iterator i = start;
264 uint16_t frameControl = i.ReadLsbtohU16();
265 SetFrameControl(frameControl);
268 SetRadius(i.ReadU8());
269 SetSeqNum(i.ReadU8());
270
272 {
274 }
275
277 {
279 }
280
281 if (m_fctrlMcst)
282 {
283 uint8_t multicastControl = i.ReadU8();
284 SetMulticastControl(multicastControl);
285 }
286
287 // TODO: Add Source route subframe when supported
288
289 return i.GetDistanceFrom(start);
290}
291
294{
295 /*
296 * Each NWK header will have:
297 *
298 * Frame Control : 2 octet
299 *
300 * Routing Fields:
301 * Destination address : 2 octet
302 * Source address : 2 octet
303 * Radius : 1 octet
304 * Sequence number : 1 octet
305 * Destination IEEE address : 0/8 octet
306 * Source IEEE address : 0/8 octet
307 * Multicast control : 0/1 octet
308 * Source route subframe : variable
309 */
310
311 uint32_t size = 8;
312
314 {
315 size += 8;
316 }
317
319 {
320 size += 8;
321 }
322
323 if (m_fctrlMcst)
324 {
325 size += 1;
326 }
327
328 // TODO: Add Source route subframe when supported.
329
330 return (size);
331}
332
333TypeId
335{
336 static TypeId tid = TypeId("ns3::zigbee::ZigbeeNwkHeader")
337 .SetParent<Header>()
338 .SetGroupName("Zigbee")
339 .AddConstructor<ZigbeeNwkHeader>();
340 return tid;
341}
342
343TypeId
345{
346 return GetTypeId();
347}
348
349void
350ZigbeeNwkHeader::Print(std::ostream& os) const
351{
352 os << "\n";
353 switch (m_fctrlFrmType)
354 {
355 case DATA:
356 os << "Frame Type = DATA ";
357 break;
358 case NWK_COMMAND:
359 os << "Frame Type = NWK_COMMAND ";
360 break;
361 case INTER_PAN:
362 os << "Frame Type = INTER_PAN ";
363 break;
364 }
365 os << " | Protocol Version = " << static_cast<uint32_t>(m_fctrlPrtVer)
366 << " | Security Enabled = " << static_cast<uint32_t>(m_fctrlSecurity)
367 << " | Source Route = " << static_cast<uint32_t>(m_fctrlSrcRoute);
368
369 switch (m_fctrlDscvRoute)
370 {
372 os << " | Enable Route Discovery = SUPPRESS ";
373 break;
375 os << " | Enable Route Discovery = ENABLED ";
376 break;
377 }
378
379 os << "\nDst Addr = " << m_dstAddr << " | Src Addr = " << m_srcAddr
380 << " | Sequence Num = " << static_cast<uint32_t>(m_seqNum)
381 << " | Radius = " << static_cast<uint32_t>(m_radius);
382
384 {
385 os << "\nDst IEEE Addr = " << m_dstIeeeAddr;
386 }
387
389 {
390 os << " | Src IEEE Addr = " << m_srcIeeeAddr;
391 }
392
393 if (m_fctrlMcst)
394 {
395 switch (m_mcstMode)
396 {
397 case MEMBER:
398 os << " | Mcst mode: MEMBER ";
399 break;
400 case NONMEMBER:
401 os << " | Mcst mode: NONMEMBER ";
402 break;
403 }
404
405 os << " | Non Member radius = " << static_cast<uint32_t>(m_nonMemberRadius)
406 << " | Max Non Member radius = " << static_cast<uint32_t>(m_maxNonMemberRadius);
407 }
408 os << "\n";
409
410 // TODO: Add Source route subframe when supported.
411}
412} // namespace zigbee
413} // 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.
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.
uint8_t m_nonMemberRadius
Non member radius sub-field (Bits 2-4)
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.
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)
Mac64Address m_dstIeeeAddr
Destination IEEE address (0 or 8 Octets)
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.
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.