A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-mac-trailer.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:
7 * kwong yin <kwong-sang.yin@boeing.com>
8 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
9 * Erwan Livolant <erwan.livolant@inria.fr>
10 */
11#include "lr-wpan-mac-trailer.h"
12
13#include <ns3/packet.h>
14
15namespace ns3
16{
17namespace lrwpan
18{
19
20NS_OBJECT_ENSURE_REGISTERED(LrWpanMacTrailer);
21
22/// The length in octets of the IEEE 802.15.4 MAC FCS field
23constexpr uint16_t LR_WPAN_MAC_FCS_LENGTH = 2;
24
26 : m_fcs(0),
27 m_calcFcs(false)
28{
29}
30
33{
34 static TypeId tid = TypeId("ns3::lrwpan::LrWpanMacTrailer")
35 .AddDeprecatedName("ns3::LrWpanMacTrailer")
37 .SetGroupName("LrWpan")
38 .AddConstructor<LrWpanMacTrailer>();
39 return tid;
40}
41
47
48void
49LrWpanMacTrailer::Print(std::ostream& os) const
50{
51 os << " FCS = " << m_fcs;
52}
53
59
60void
62{
63 start.Prev(LR_WPAN_MAC_FCS_LENGTH);
64 start.WriteU16(m_fcs);
65}
66
69{
70 start.Prev(LR_WPAN_MAC_FCS_LENGTH);
71 m_fcs = start.ReadU16();
72
74}
75
76uint16_t
78{
79 return m_fcs;
80}
81
82void
84{
85 if (m_calcFcs)
86 {
87 uint16_t size = p->GetSize();
88 auto serial_packet = new uint8_t[size];
89
90 p->CopyData(serial_packet, size);
91
92 m_fcs = GenerateCrc16(serial_packet, size);
93 delete[] serial_packet;
94 }
95}
96
97/* Be sure to have removed the trailer and only the trailer
98 * from the packet before to use CheckFcs */
99bool
101{
102 if (!m_calcFcs)
103 {
104 return true;
105 }
106 else
107 {
108 uint16_t checkFcs;
109 uint16_t size = p->GetSize();
110 auto serial_packet = new uint8_t[size];
111
112 p->CopyData(serial_packet, size);
113
114 checkFcs = GenerateCrc16(serial_packet, size);
115 delete[] serial_packet;
116 return (checkFcs == GetFcs());
117 }
118}
119
120void
122{
123 m_calcFcs = enable;
124 if (!enable)
125 {
126 m_fcs = 0;
127 }
128}
129
130bool
132{
133 return m_calcFcs;
134}
135
136uint16_t
138{
139 int i;
140 uint16_t accumulator = 0;
141
142 for (i = 0; i < length; ++i)
143 {
144 accumulator ^= *data;
145 accumulator = (accumulator >> 8) | (accumulator << 8);
146 accumulator ^= (accumulator & 0xff00) << 4;
147 accumulator ^= (accumulator >> 8) >> 4;
148 accumulator ^= (accumulator & 0xff00) >> 5;
149 ++data;
150 }
151 return accumulator;
152}
153
154} // namespace lrwpan
155} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
Smart pointer class similar to boost::intrusive_ptr.
Protocol trailer serialization and deserialization.
Definition trailer.h:30
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 Trailer with the Frame Check Sequence field.
void Serialize(Buffer::Iterator start) const override
bool IsFcsEnabled() const
Query if FCS calculation is enabled for this trailer.
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
uint32_t Deserialize(Buffer::Iterator start) override
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
uint32_t GetSerializedSize() const override
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
bool m_calcFcs
Only if m_calcFcs is true, FCS values will be calculated and used in the trailer.
uint16_t m_fcs
The FCS value stored in this trailer.
LrWpanMacTrailer()
Default constructor for a MAC trailer with disabled FCS calculation.
uint16_t GetFcs() const
Get this trailers FCS value.
static TypeId GetTypeId()
Get the type ID.
uint16_t GenerateCrc16(uint8_t *data, int length)
Calculate the 16-bit FCS value.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void Print(std::ostream &os) const override
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
constexpr uint16_t LR_WPAN_MAC_FCS_LENGTH
The length in octets of the IEEE 802.15.4 MAC FCS field.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]