A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-mac-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: kwong yin <kwong-sang.yin@boeing.com>
7 */
9
10#include <ns3/address-utils.h>
11
12namespace ns3
13{
14namespace lrwpan
15{
16
17NS_OBJECT_ENSURE_REGISTERED(LrWpanMacHeader);
18
19// TODO: Test Compressed PAN Id, Security Enabled, different size Key
20
22{
23 SetType(LRWPAN_MAC_DATA); // Assume Data frame
24 SetSecDisable(); // Assume there is No Aux Sec but
25 SetNoFrmPend(); // No Frame Pending
26 SetNoAckReq(); // No Ack Frame will be expected from recipient
27 SetNoPanIdComp(); // No PAN Id Compression since no addresses
28 SetFrmCtrlRes(0); // Initialize the 3 reserved bits to 0
29 SetDstAddrMode(NOADDR); // Assume there will be no src and dst address
31 SetFrameVer(1); // Indicates an IEEE 802.15.4 frame
32}
33
35{
36 SetType(wpanMacType);
37 SetSeqNum(seqNum);
38 SetSecDisable(); // Assume there is No Aux Sec but
39 SetNoFrmPend(); // No Frame Pending
40 SetNoAckReq(); // No Ack Frame will be expected from recipient
41 SetNoPanIdComp(); // No PAN Id Compression since no addresses
42 SetFrmCtrlRes(0); // Initialize the 3 reserved bits to 0
43 SetDstAddrMode(NOADDR); // Assume there will be no src and dst address
45 SetFrameVer(1); // Indicates an IEEE 802.15.4 frame
46}
47
51
54{
55 switch (m_fctrlFrmType)
56 {
57 case 0:
58 return LRWPAN_MAC_BEACON;
59 case 1:
60 return LRWPAN_MAC_DATA;
61 case 2:
63 case 3:
64 return LRWPAN_MAC_COMMAND;
65 default:
67 }
68}
69
70uint16_t
72{
73 uint16_t val = 0;
74
75 val = m_fctrlFrmType & (0x07); // Bit 0-2
76 val |= (m_fctrlSecU << 3) & (0x01 << 3); // Bit 3
77 val |= (m_fctrlFrmPending << 4) & (0x01 << 4); // Bit 4
78 val |= (m_fctrlAckReq << 5) & (0x01 << 5); // Bit 5
79 val |= (m_fctrlPanIdComp << 6) & (0x01 << 6); // Bit 6
80 val |= (m_fctrlReserved << 7) & (0x07 << 7); // Bit 7-9
81 val |= (m_fctrlDstAddrMode << 10) & (0x03 << 10); // Bit 10-11
82 val |= (m_fctrlFrmVer << 12) & (0x03 << 12); // Bit 12-13
83 val |= (m_fctrlSrcAddrMode << 14) & (0x03 << 14); // Bit 14-15
84 return val;
85}
86
87bool
89{
90 return (m_fctrlSecU == 1);
91}
92
93bool
95{
96 return (m_fctrlFrmPending == 1);
97}
98
99bool
101{
102 return (m_fctrlAckReq == 1);
103}
104
105bool
107{
108 return (m_fctrlPanIdComp == 1);
109}
110
111uint8_t
116
117uint8_t
122
123uint8_t
125{
126 return m_fctrlFrmVer;
127}
128
129uint8_t
134
135uint8_t
137{
138 return m_SeqNum;
139}
140
141uint16_t
143{
144 return m_addrDstPanId;
145}
146
152
158
159uint16_t
161{
162 return m_addrSrcPanId;
163}
164
170
176
177uint8_t
179{
180 uint8_t val = 0;
181
182 val = m_secctrlSecLevel & (0x7); // Bit 0-2
183 val |= (m_secctrlKeyIdMode << 3) & (0x3 << 3); // Bit 3-4
184 val |= (m_secctrlReserved << 5) & (0x7 << 5); // Bit 5-7
185
186 return val;
187}
188
191{
192 return m_auxFrmCntr;
193}
194
195uint8_t
200
201uint8_t
206
207uint8_t
212
218
219uint64_t
224
225uint8_t
230
231bool
236
237bool
239{
241}
242
243bool
248
249bool
254
255void
257{
258 m_fctrlFrmType = wpanMacType;
259}
260
261void
263{
264 m_fctrlFrmType = (frameControl) & (0x07); // Bit 0-2
265 m_fctrlSecU = (frameControl >> 3) & (0x01); // Bit 3
266 m_fctrlFrmPending = (frameControl >> 4) & (0x01); // Bit 4
267 m_fctrlAckReq = (frameControl >> 5) & (0x01); // Bit 5
268 m_fctrlPanIdComp = (frameControl >> 6) & (0x01); // Bit 6
269 m_fctrlReserved = (frameControl >> 7) & (0x07); // Bit 7-9
270 m_fctrlDstAddrMode = (frameControl >> 10) & (0x03); // Bit 10-11
271 m_fctrlFrmVer = (frameControl >> 12) & (0x03); // Bit 12-13
272 m_fctrlSrcAddrMode = (frameControl >> 14) & (0x03); // Bit 14-15
273}
274
275void
280
281void
286
287void
292
293void
298
299void
304
305void
310
311void
316
317void
322
323void
325{
326 m_fctrlReserved = res;
327}
328
329void
331{
332 m_fctrlDstAddrMode = addrMode;
333}
334
335void
337{
338 m_fctrlFrmVer = ver;
339}
340
341void
343{
344 m_fctrlSrcAddrMode = addrMode;
345}
346
347void
349{
350 m_SeqNum = seqNum;
351}
352
353void
355{
356 m_addrSrcPanId = panId;
357 m_addrShortSrcAddr = addr;
358}
359
360void
362{
363 m_addrSrcPanId = panId;
364 m_addrExtSrcAddr = addr;
365}
366
367void
369{
370 m_addrDstPanId = panId;
371 m_addrShortDstAddr = addr;
372}
373
374void
376{
377 m_addrDstPanId = panId;
378 m_addrExtDstAddr = addr;
379}
380
381void
383{
384 m_secctrlSecLevel = (secControl) & (0x07); // Bit 0-2
385 m_secctrlKeyIdMode = (secControl >> 3) & (0x03); // Bit 3-4
386 m_secctrlReserved = (secControl >> 5) & (0x07); // Bit 5-7
387}
388
389void
391{
392 m_auxFrmCntr = frmCntr;
393}
394
395void
397{
398 m_secctrlSecLevel = secLevel;
399}
400
401void
403{
404 m_secctrlKeyIdMode = keyIdMode;
405}
406
407void
412
413void
415{
416 m_auxKeyIdKeyIndex = keyIndex;
417}
418
419void
420LrWpanMacHeader::SetKeyId(uint32_t keySrc, uint8_t keyIndex)
421{
422 m_auxKeyIdKeyIndex = keyIndex;
423 m_auxKeyIdKeySrc32 = keySrc;
424}
425
426void
427LrWpanMacHeader::SetKeyId(uint64_t keySrc, uint8_t keyIndex)
428{
429 m_auxKeyIdKeyIndex = keyIndex;
430 m_auxKeyIdKeySrc64 = keySrc;
431}
432
433TypeId
435{
436 static TypeId tid = TypeId("ns3::lrwpan::LrWpanMacHeader")
437 .AddDeprecatedName("ns3::LrWpanMacHeader")
438 .SetParent<Header>()
439 .SetGroupName("LrWpan")
440 .AddConstructor<LrWpanMacHeader>();
441 return tid;
442}
443
444TypeId
446{
447 return GetTypeId();
448}
449
450void
451LrWpanMacHeader::Print(std::ostream& os) const
452{
453 os << " Frame Type = " << (uint32_t)m_fctrlFrmType
454 << ", Sec Enable = " << (uint32_t)m_fctrlSecU
455 << ", Frame Pending = " << (uint32_t)m_fctrlFrmPending
456 << ", Ack Request = " << (uint32_t)m_fctrlAckReq
457 << ", PAN ID Compress = " << (uint32_t)m_fctrlPanIdComp
458 << ", Frame Vers = " << (uint32_t)m_fctrlFrmVer
459 << ", Dst Addrs Mode = " << (uint32_t)m_fctrlDstAddrMode
460 << ", Src Addr Mode = " << (uint32_t)m_fctrlSrcAddrMode;
461
462 os << ", Sequence Num = " << static_cast<uint16_t>(m_SeqNum);
463
464 switch (m_fctrlDstAddrMode)
465 {
466 case NOADDR:
467 break;
468 case SHORTADDR:
469 os << ", Dst Addr Pan ID = " << static_cast<uint16_t>(m_addrDstPanId)
470 << ", m_addrShortDstAddr = " << m_addrShortDstAddr;
471 break;
472 case EXTADDR:
473 os << ", Dst Addr Pan ID = " << static_cast<uint16_t>(m_addrDstPanId)
474 << ", m_addrExtDstAddr = " << m_addrExtDstAddr;
475 break;
476 }
477
478 switch (m_fctrlSrcAddrMode)
479 {
480 case NOADDR:
481 break;
482 case SHORTADDR:
483 os << ", Src Addr Pan ID = " << static_cast<uint16_t>(m_addrSrcPanId)
484 << ", m_addrShortSrcAddr = " << m_addrShortSrcAddr;
485 break;
486 case EXTADDR:
487 os << ", Src Addr Pan ID = " << static_cast<uint32_t>(m_addrSrcPanId)
488 << ", m_addrExtSrcAddr = " << m_addrExtDstAddr;
489 break;
490 }
491
492 if (IsSecEnable())
493 {
494 os << " Security Level = " << static_cast<uint32_t>(m_secctrlSecLevel)
495 << ", Key Id Mode = " << static_cast<uint32_t>(m_secctrlKeyIdMode)
496 << ", Frame Counter = " << static_cast<uint32_t>(m_auxFrmCntr);
497
498 switch (m_secctrlKeyIdMode)
499 {
500 case IMPLICIT:
501 break;
502 case NOKEYSOURCE:
503 os << ", Key Id - Key Index = " << static_cast<uint32_t>(m_auxKeyIdKeyIndex);
504 break;
505 case SHORTKEYSOURCE:
506 os << ", Key Id - Key Source 32 =" << static_cast<uint32_t>(m_auxKeyIdKeySrc32)
507 << ", Key Id - Key Index = " << static_cast<uint32_t>(m_auxKeyIdKeyIndex);
508 break;
509 case LONGKEYSOURCE:
510 os << ", Key Id - Key Source 64 =" << static_cast<uint64_t>(m_auxKeyIdKeySrc64)
511 << ", Key Id - Key Index = " << static_cast<uint32_t>(m_auxKeyIdKeyIndex);
512 break;
513 }
514 }
515}
516
519{
520 /*
521 * Each mac header will have
522 * Frame Control : 2 octet
523 * Sequence Number : 1 Octet
524 * Dst PAN Id : 0/2 Octet
525 * Dst Address : 0/2/8 octet
526 * Src PAN Id : 0/2 octet
527 * Src Address : 0/2/8 octet
528 * Aux Sec Header : 0/5/6/10/14 octet
529 */
530
531 uint32_t size = 3;
532
533 switch (m_fctrlDstAddrMode)
534 {
535 case NOADDR:
536 break;
537 case SHORTADDR:
538 size += 4;
539 break;
540 case EXTADDR:
541 size += 10;
542 break;
543 }
544
545 switch (m_fctrlSrcAddrMode)
546 {
547 case NOADDR:
548 break;
549 case SHORTADDR:
550 // check if PAN Id compression is enabled
551 if (!IsPanIdComp())
552 {
553 size += 4;
554 }
555 else
556 {
557 size += 2;
558 }
559 break;
560 case EXTADDR:
561 // check if PAN Id compression is enabled
562 if (!IsPanIdComp())
563 {
564 size += 10;
565 }
566 else
567 {
568 size += 8;
569 }
570 break;
571 }
572
573 // check if security is enabled
574 if (IsSecEnable())
575 {
576 size += 5;
577 switch (m_secctrlKeyIdMode)
578 {
579 case IMPLICIT:
580 break;
581 case NOKEYSOURCE:
582 size += 1;
583 break;
584 case SHORTKEYSOURCE:
585 size += 5;
586 break;
587 case LONGKEYSOURCE:
588 size += 9;
589 break;
590 }
591 }
592 return size;
593}
594
595void
597{
598 Buffer::Iterator i = start;
599 uint16_t frameControl = GetFrameControl();
600
601 i.WriteHtolsbU16(frameControl);
602 i.WriteU8(GetSeqNum());
603
604 switch (m_fctrlDstAddrMode)
605 {
606 case NOADDR:
607 break;
608 case SHORTADDR:
611 break;
612 case EXTADDR:
615 break;
616 }
617
618 switch (m_fctrlSrcAddrMode)
619 {
620 case NOADDR:
621 break;
622 case SHORTADDR:
623 if (!IsPanIdComp())
624 {
626 }
628 break;
629 case EXTADDR:
630 if (!IsPanIdComp())
631 {
633 }
635 break;
636 }
637
638 if (IsSecEnable())
639 {
642
643 switch (m_secctrlKeyIdMode)
644 {
645 case IMPLICIT:
646 break;
647 case NOKEYSOURCE:
649 break;
650 case SHORTKEYSOURCE:
653 break;
654 case LONGKEYSOURCE:
657 break;
658 }
659 }
660}
661
664{
665 Buffer::Iterator i = start;
666 uint16_t frameControl = i.ReadLsbtohU16();
667 SetFrameControl(frameControl);
668
669 SetSeqNum(i.ReadU8());
670 switch (m_fctrlDstAddrMode)
671 {
672 case NOADDR:
673 break;
674 case SHORTADDR:
677 break;
678 case EXTADDR:
681 break;
682 }
683
684 switch (m_fctrlSrcAddrMode)
685 {
686 case NOADDR:
687 break;
688 case SHORTADDR:
689 if (!IsPanIdComp())
690 {
692 }
693 else
694 {
695 if (m_fctrlDstAddrMode > 0)
696 {
698 }
699 }
701 break;
702 case EXTADDR:
703 if (!IsPanIdComp())
704 {
706 }
707 else
708 {
709 if (m_fctrlDstAddrMode > 0)
710 {
712 }
713 }
715 break;
716 }
717
718 if (IsSecEnable())
719 {
722
723 switch (m_secctrlKeyIdMode)
724 {
725 case IMPLICIT:
726 break;
727 case NOKEYSOURCE:
728 SetKeyId(i.ReadU8());
729 break;
730 case SHORTKEYSOURCE:
731 SetKeyId(i.ReadLsbtohU32(), i.ReadU8());
732 break;
733 case LONGKEYSOURCE:
734 SetKeyId(i.ReadLsbtohU64(), i.ReadU8());
735 break;
736 }
737 }
738 return i.GetDistanceFrom(start);
739}
740
741} // namespace lrwpan
742} // 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
void WriteHtolsbU32(uint32_t data)
Definition buffer.cc:899
uint16_t ReadLsbtohU16()
Definition buffer.cc:1053
uint64_t ReadLsbtohU64()
Definition buffer.cc:1083
void WriteHtolsbU64(uint64_t data)
Definition buffer.cc:909
uint32_t GetDistanceFrom(const Iterator &o) const
Definition buffer.cc:769
uint32_t ReadLsbtohU32()
Definition buffer.cc:1065
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:48
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Represent the Mac Header with the Frame Control and Sequence Number fields.
uint8_t GetSecControl() const
Get the Auxiliary Security Header - Security Control Octet.
void SetFrmPend()
Set the Frame Control field "Frame Pending" bit to true.
uint32_t GetSerializedSize() const override
void SetSecLevel(uint8_t secLevel)
Set the Security Control field "Security Level" bits (3 bits)
LrWpanMacType GetType() const
Get the header type.
uint8_t m_fctrlReserved
Frame Control field Bit 7-9.
bool IsSecEnable() const
Check if Security Enabled bit of Frame Control is enabled.
uint8_t GetKeyIdIndex() const
Get the Auxiliary Security Header - Key Identifier - Key Index.
uint8_t m_secctrlKeyIdMode
Auxiliary security header - Security Control field - Bit 3-4 will indicate size of Key Id.
uint8_t m_auxKeyIdKeyIndex
Auxiliary security header - Key Index (1 Octet)
void SetSecControl(uint8_t secLevel)
Set the auxiliary security header "Security Control" octet.
uint64_t m_auxKeyIdKeySrc64
Auxiliary security header - Key Source (8 Octets)
Mac16Address GetShortSrcAddr() const
Get the Source Short address.
void SetType(LrWpanMacType wpanMacType)
Set the Frame Control field "Frame Type" bits.
void SetDstAddrMode(uint8_t addrMode)
Set the Destination address mode.
uint16_t m_addrDstPanId
Dst PAN id (0 or 2 Octets)
void SetNoAckReq()
Set the Frame Control field "Ack. Request" bit to false.
uint8_t GetSecLevel() const
Get the Auxiliary Security Header - Security Control - Security Level bits.
void SetSecCtrlReserved(uint8_t res)
Set the Security Control field "Reserved" bits (3 bits)
void SetDstAddrFields(uint16_t panId, Mac16Address addr)
Set Destination address fields.
Mac64Address GetExtSrcAddr() const
Get the Source Extended address.
static TypeId GetTypeId()
Get the type ID.
uint32_t m_auxKeyIdKeySrc32
Auxiliary security header - Key Source (4 Octets)
uint32_t Deserialize(Buffer::Iterator start) override
bool IsData() const
Returns true if the header is a data.
uint8_t m_fctrlFrmType
Frame Control field Bit 0-2 = 0 - Beacon, 1 - Data, 2 - Ack, 3 - Command.
uint64_t GetKeyIdSrc64() const
Get the Auxiliary Security Header - Key Identifier - Key Source (4 Octets)
void SetFrameControl(uint16_t frameControl)
Set the whole Frame Control field.
Mac16Address m_addrShortDstAddr
Dst Short addr (0 or 2 Octets)
uint8_t m_SeqNum
Sequence Number (1 Octet)
bool IsBeacon() const
Returns true if the header is a beacon.
void Print(std::ostream &os) const override
LrWpanMacType
The possible MAC types, see IEEE 802.15.4-2006, Table 79.
@ LRWPAN_MAC_ACKNOWLEDGMENT
LRWPAN_MAC_ACKNOWLEDGMENT.
@ LRWPAN_MAC_RESERVED
LRWPAN_MAC_RESERVED.
@ LRWPAN_MAC_COMMAND
LRWPAN_MAC_COMMAND.
@ LRWPAN_MAC_BEACON
LRWPAN_MAC_BEACON.
uint32_t GetFrmCounter() const
Get the Auxiliary Security Header - Frame Counter Octets.
void SetPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to true.
Mac16Address GetShortDstAddr() const
Get the Destination Short address.
void Serialize(Buffer::Iterator start) const override
Mac16Address m_addrShortSrcAddr
Src Short addr (0 or 2 Octets)
uint16_t GetFrameControl() const
Get the Frame control field.
uint8_t GetKeyIdMode() const
Get the Auxiliary Security Header - Security Control - Key Identifier Mode bits.
void SetSrcAddrMode(uint8_t addrMode)
Set the Source address mode.
bool IsCommand() const
Returns true if the header is a command.
uint16_t m_addrSrcPanId
Src PAN id (0 or 2 Octets)
uint32_t GetKeyIdSrc32() const
Get the Auxiliary Security Header - Key Identifier - Key Source (2 Octets)
uint8_t GetSecCtrlReserved() const
Get the Auxiliary Security Header - Security Control - Reserved bits.
void SetFrmCtrlRes(uint8_t res)
Set the Frame Control field "Reserved" bits.
void SetKeyId(uint8_t keyIndex)
Set the Key Index.
uint8_t GetSeqNum() const
Get the frame Sequence number.
void SetFrameVer(uint8_t ver)
Set the Frame version.
uint8_t m_secctrlSecLevel
Auxiliary security header - Security Control field - Bit 0-2.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint8_t GetFrameVer() const
Get the Frame Version of Frame control field.
void SetFrmCounter(uint32_t frmCntr)
Set the auxiliary security header "Frame Counter" octet.
void SetSrcAddrFields(uint16_t panId, Mac16Address addr)
Set Source address fields.
Mac64Address m_addrExtDstAddr
Dst Ext addr (0 or 8 Octets)
uint8_t m_fctrlDstAddrMode
Frame Control field Bit 10-11 = 0 - No DstAddr, 2 - ShtDstAddr, 3 - ExtDstAddr.
Mac64Address m_addrExtSrcAddr
Src Ext addr (0 or 8 Octets)
bool IsFrmPend() const
Check if Frame Pending bit of Frame Control is enabled.
uint8_t m_secctrlReserved
Auxiliary security header - Security Control field - Bit 5-7.
uint16_t GetSrcPanId() const
Get the Source PAN ID.
void SetNoPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to false.
uint8_t m_fctrlSrcAddrMode
Frame Control field Bit 14-15 = 0 - No SrcAddr, 2 - ShtSrcAddr, 3 - ExtSrcAddr.
uint16_t GetDstPanId() const
Get the Destination PAN ID.
uint8_t m_fctrlFrmPending
Frame Control field Bit 4.
uint8_t m_fctrlPanIdComp
Frame Control field Bit 6 = 0 - no compression, 1 - using only DstPanId for both Src and DstPanId.
uint8_t m_fctrlFrmVer
Frame Control field Bit 12-13.
uint8_t m_fctrlAckReq
Frame Control field Bit 5.
void SetNoFrmPend()
Set the Frame Control field "Frame Pending" bit to false.
uint32_t m_auxFrmCntr
Auxiliary security header - Frame Counter (4 Octets)
void SetAckReq()
Set the Frame Control field "Ack. Request" bit to true.
bool IsAckReq() const
Check if Ack.
uint8_t GetDstAddrMode() const
Get the Dest.
uint8_t m_fctrlSecU
Frame Control field Bit 3 = 0 - no AuxSecHdr , 1 - security enabled AuxSecHdr present.
void SetSecEnable()
Set the Frame Control field "Security Enabled" bit to true.
void SetSecDisable()
Set the Frame Control field "Security Enabled" bit to false.
Mac64Address GetExtDstAddr() const
Get the Destination Extended address.
bool IsAcknowledgment() const
Returns true if the header is an ack.
uint8_t GetSrcAddrMode() const
Get the Source Addressing Mode of Frame control field.
bool IsPanIdComp() const
Check if PAN ID Compression bit of Frame Control is enabled.
void SetSeqNum(uint8_t seqNum)
Set the Sequence number.
uint8_t GetFrmCtrlRes() const
Get the Reserved bits of Frame control field.
void SetKeyIdMode(uint8_t keyIdMode)
Set the Security Control field "Key Identifier Mode" bits (2 bits)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
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.