A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
lr-wpan-mac-trailer.cc
Go to the documentation of this file.
1
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2011 The Boeing Company
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author:
19
* kwong yin <kwong-sang.yin@boeing.com>
20
* Sascha Alexander Jopen <jopen@cs.uni-bonn.de>
21
* Erwan Livolant <erwan.livolant@inria.fr>
22
*/
23
#include "
lr-wpan-mac-trailer.h
"
24
#include <ns3/packet.h>
25
26
namespace
ns3
{
27
28
NS_OBJECT_ENSURE_REGISTERED
(LrWpanMacTrailer);
29
30
const
uint16_t
LrWpanMacTrailer::LR_WPAN_MAC_FCS_LENGTH
= 2;
31
32
LrWpanMacTrailer::LrWpanMacTrailer
(
void
)
33
: m_fcs (0),
34
m_calcFcs (false)
35
{
36
}
37
38
TypeId
39
LrWpanMacTrailer::GetTypeId
(
void
)
40
{
41
static
TypeId
tid =
TypeId
(
"ns3::LrWpanMacTrailer"
)
42
.
SetParent
<
Trailer
> ()
43
.SetGroupName (
"LrWpan"
)
44
.AddConstructor<
LrWpanMacTrailer
> ()
45
;
46
return
tid;
47
}
48
49
TypeId
50
LrWpanMacTrailer::GetInstanceTypeId
(
void
)
const
51
{
52
return
GetTypeId
();
53
}
54
55
void
56
LrWpanMacTrailer::Print
(std::ostream &os)
const
57
{
58
os <<
" FCS = "
<<
m_fcs
;
59
}
60
61
uint32_t
62
LrWpanMacTrailer::GetSerializedSize
(
void
)
const
63
{
64
return
LR_WPAN_MAC_FCS_LENGTH
;
65
}
66
67
void
68
LrWpanMacTrailer::Serialize
(
Buffer::Iterator
start
)
const
69
{
70
start
.Prev (
LR_WPAN_MAC_FCS_LENGTH
);
71
start
.WriteU16 (
m_fcs
);
72
}
73
74
uint32_t
75
LrWpanMacTrailer::Deserialize
(
Buffer::Iterator
start
)
76
{
77
start
.Prev (
LR_WPAN_MAC_FCS_LENGTH
);
78
m_fcs
=
start
.ReadU16 ();
79
80
return
LR_WPAN_MAC_FCS_LENGTH
;
81
}
82
83
uint16_t
84
LrWpanMacTrailer::GetFcs
(
void
)
const
85
{
86
return
m_fcs
;
87
}
88
89
void
90
LrWpanMacTrailer::SetFcs
(
Ptr<const Packet>
p)
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 */
106
bool
107
LrWpanMacTrailer::CheckFcs
(
Ptr<const Packet>
p)
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
127
void
128
LrWpanMacTrailer::EnableFcs
(
bool
enable)
129
{
130
m_calcFcs
= enable;
131
if
(!enable)
132
{
133
m_fcs
= 0;
134
}
135
}
136
137
bool
138
LrWpanMacTrailer::IsFcsEnabled
(
void
)
139
{
140
return
m_calcFcs
;
141
}
142
143
uint16_t
144
LrWpanMacTrailer::GenerateCrc16
(uint8_t *
data
,
int
length)
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
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
ns3::LrWpanMacTrailer::GenerateCrc16
uint16_t GenerateCrc16(uint8_t *data, int length)
Calculate the 16-bit FCS value.
Definition:
lr-wpan-mac-trailer.cc:144
ns3::LrWpanMacTrailer::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
lr-wpan-mac-trailer.cc:75
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:45
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition:
packet.h:852
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LrWpanMacTrailer::SetFcs
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
Definition:
lr-wpan-mac-trailer.cc:90
ns3::LrWpanMacTrailer::LR_WPAN_MAC_FCS_LENGTH
static const uint16_t LR_WPAN_MAC_FCS_LENGTH
The length in octets of the IEEE 802.15.4 MAC FCS field.
Definition:
lr-wpan-mac-trailer.h:44
ns3::LrWpanMacTrailer::IsFcsEnabled
bool IsFcsEnabled(void)
Query if FCS calculation is enabled for this trailer.
Definition:
lr-wpan-mac-trailer.cc:138
ns3::LrWpanMacTrailer
Represent the Mac Trailer with the Frame Check Sequence field.
Definition:
lr-wpan-mac-trailer.h:39
ns3::Packet::CopyData
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition:
packet.cc:378
ns3::LrWpanMacTrailer::CheckFcs
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
Definition:
lr-wpan-mac-trailer.cc:107
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
ns3::Ptr< const Packet >
visualizer.core.start
def start()
Definition:
core.py:1855
lr-wpan-mac-trailer.h
data
uint8_t data[writeSize]
Definition:
socket-bound-tcp-static-routing.cc:53
ns3::LrWpanMacTrailer::m_fcs
uint16_t m_fcs
The FCS value stored in this trailer.
Definition:
lr-wpan-mac-trailer.h:119
ns3::LrWpanMacTrailer::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
lr-wpan-mac-trailer.cc:68
ns3::LrWpanMacTrailer::EnableFcs
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
Definition:
lr-wpan-mac-trailer.cc:128
ns3::LrWpanMacTrailer::GetFcs
uint16_t GetFcs(void) const
Get this trailers FCS value.
Definition:
lr-wpan-mac-trailer.cc:84
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:99
ns3::LrWpanMacTrailer::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
lr-wpan-mac-trailer.cc:50
ns3::LrWpanMacTrailer::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
lr-wpan-mac-trailer.cc:39
ns3::LrWpanMacTrailer::LrWpanMacTrailer
LrWpanMacTrailer(void)
Default constructor for a MAC trailer with disabled FCS calculation.
Definition:
lr-wpan-mac-trailer.cc:32
ns3::LrWpanMacTrailer::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
lr-wpan-mac-trailer.cc:62
ns3::LrWpanMacTrailer::m_calcFcs
bool m_calcFcs
Only if m_calcFcs is true, FCS values will be calculated and used in the trailer.
Definition:
lr-wpan-mac-trailer.h:125
ns3::Trailer
Protocol trailer serialization and deserialization.
Definition:
trailer.h:41
ns3::LrWpanMacTrailer::Print
virtual void Print(std::ostream &os) const
Definition:
lr-wpan-mac-trailer.cc:56
src
lr-wpan
model
lr-wpan-mac-trailer.cc
Generated on Fri Oct 1 2021 17:03:13 for ns-3 by
1.8.20