A Discrete-Event Network Simulator
API
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{
28
29NS_OBJECT_ENSURE_REGISTERED(LrWpanMacTrailer);
30
32
34 : m_fcs(0),
35 m_calcFcs(false)
36{
37}
38
41{
42 static TypeId tid = TypeId("ns3::LrWpanMacTrailer")
44 .SetGroupName("LrWpan")
45 .AddConstructor<LrWpanMacTrailer>();
46 return tid;
47}
48
51{
52 return GetTypeId();
53}
54
55void
56LrWpanMacTrailer::Print(std::ostream& os) const
57{
58 os << " FCS = " << m_fcs;
59}
60
63{
65}
66
67void
69{
71 start.WriteU16(m_fcs);
72}
73
76{
78 m_fcs = start.ReadU16();
79
81}
82
83uint16_t
85{
86 return m_fcs;
87}
88
89void
91{
92 if (m_calcFcs)
93 {
94 uint16_t size = p->GetSize();
95 uint8_t* serial_packet = new uint8_t[size];
96
97 p->CopyData(serial_packet, size);
98
99 m_fcs = GenerateCrc16(serial_packet, size);
100 delete[] serial_packet;
101 }
102}
103
104/* Be sure to have removed the trailer and only the trailer
105 * from the packet before to use CheckFcs */
106bool
108{
109 if (!m_calcFcs)
110 {
111 return true;
112 }
113 else
114 {
115 uint16_t checkFcs;
116 uint16_t size = p->GetSize();
117 uint8_t* serial_packet = new uint8_t[size];
118
119 p->CopyData(serial_packet, size);
120
121 checkFcs = GenerateCrc16(serial_packet, size);
122 delete[] serial_packet;
123 return (checkFcs == GetFcs());
124 }
125}
126
127void
129{
130 m_calcFcs = enable;
131 if (!enable)
132 {
133 m_fcs = 0;
134 }
135}
136
137bool
139{
140 return m_calcFcs;
141}
142
143uint16_t
145{
146 int i;
147 uint16_t accumulator = 0;
148
149 for (i = 0; i < length; ++i)
150 {
151 accumulator ^= *data;
152 accumulator = (accumulator >> 8) | (accumulator << 8);
153 accumulator ^= (accumulator & 0xff00) << 4;
154 accumulator ^= (accumulator >> 8) >> 4;
155 accumulator ^= (accumulator & 0xff00) >> 5;
156 ++data;
157 }
158 return accumulator;
159}
160
161} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
Represent the Mac Trailer with the Frame Check Sequence field.
bool m_calcFcs
Only if m_calcFcs is true, FCS values will be calculated and used in the trailer.
uint32_t GetSerializedSize() const override
static TypeId GetTypeId()
Get the type ID.
static const uint16_t LR_WPAN_MAC_FCS_LENGTH
The length in octets of the IEEE 802.15.4 MAC FCS field.
uint16_t GenerateCrc16(uint8_t *data, int length)
Calculate the 16-bit FCS value.
uint16_t m_fcs
The FCS value stored in this trailer.
LrWpanMacTrailer()
Default constructor for a MAC trailer with disabled FCS calculation.
uint32_t Deserialize(Buffer::Iterator start) override
uint16_t GetFcs() const
Get this trailers FCS value.
bool IsFcsEnabled()
Query if FCS calculation is enabled for this trailer.
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
void Serialize(Buffer::Iterator start) const override
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
void Print(std::ostream &os) const override
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
Protocol trailer serialization and deserialization.
Definition: trailer.h:41
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
def start()
Definition: core.py:1861
uint8_t data[writeSize]