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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author:
18 * kwong yin <kwong-sang.yin@boeing.com>
19 * Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
20 * Erwan Livolant <erwan.livolant@inria.fr>
21 */
22#include "lr-wpan-mac-trailer.h"
23
24#include <ns3/packet.h>
25
26namespace ns3
27{
28namespace lrwpan
29{
30
32
33/// The length in octets of the IEEE 802.15.4 MAC FCS field
34constexpr uint16_t LR_WPAN_MAC_FCS_LENGTH = 2;
35
37 : m_fcs(0),
38 m_calcFcs(false)
39{
40}
41
44{
45 static TypeId tid = TypeId("ns3::LrWpanMacTrailer")
47 .SetGroupName("LrWpan")
48 .AddConstructor<LrWpanMacTrailer>();
49 return tid;
50}
51
54{
55 return GetTypeId();
56}
57
58void
59LrWpanMacTrailer::Print(std::ostream& os) const
60{
61 os << " FCS = " << m_fcs;
62}
63
66{
68}
69
70void
72{
73 start.Prev(LR_WPAN_MAC_FCS_LENGTH);
74 start.WriteU16(m_fcs);
75}
76
79{
80 start.Prev(LR_WPAN_MAC_FCS_LENGTH);
81 m_fcs = start.ReadU16();
82
84}
85
86uint16_t
88{
89 return m_fcs;
90}
91
92void
94{
95 if (m_calcFcs)
96 {
97 uint16_t size = p->GetSize();
98 auto serial_packet = new uint8_t[size];
99
100 p->CopyData(serial_packet, size);
101
102 m_fcs = GenerateCrc16(serial_packet, size);
103 delete[] serial_packet;
104 }
105}
106
107/* Be sure to have removed the trailer and only the trailer
108 * from the packet before to use CheckFcs */
109bool
111{
112 if (!m_calcFcs)
113 {
114 return true;
115 }
116 else
117 {
118 uint16_t checkFcs;
119 uint16_t size = p->GetSize();
120 auto serial_packet = new uint8_t[size];
121
122 p->CopyData(serial_packet, size);
123
124 checkFcs = GenerateCrc16(serial_packet, size);
125 delete[] serial_packet;
126 return (checkFcs == GetFcs());
127 }
128}
129
130void
132{
133 m_calcFcs = enable;
134 if (!enable)
135 {
136 m_fcs = 0;
137 }
138}
139
140bool
142{
143 return m_calcFcs;
144}
145
146uint16_t
148{
149 int i;
150 uint16_t accumulator = 0;
151
152 for (i = 0; i < length; ++i)
153 {
154 accumulator ^= *data;
155 accumulator = (accumulator >> 8) | (accumulator << 8);
156 accumulator ^= (accumulator & 0xff00) << 4;
157 accumulator ^= (accumulator >> 8) >> 4;
158 accumulator ^= (accumulator & 0xff00) >> 5;
159 ++data;
160 }
161 return accumulator;
162}
163
164} // namespace lrwpan
165} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
Introspection did not find any typical Config paths.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
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:46
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]