A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
15
namespace
ns3
16
{
17
namespace
lrwpan
18
{
19
20
NS_OBJECT_ENSURE_REGISTERED
(LrWpanMacTrailer);
21
22
/// The length in octets of the IEEE 802.15.4 MAC FCS field
23
constexpr
uint16_t
LR_WPAN_MAC_FCS_LENGTH
= 2;
24
25
LrWpanMacTrailer::LrWpanMacTrailer
()
26
: m_fcs(0),
27
m_calcFcs(false)
28
{
29
}
30
31
TypeId
32
LrWpanMacTrailer::GetTypeId
()
33
{
34
static
TypeId
tid =
TypeId
(
"ns3::lrwpan::LrWpanMacTrailer"
)
35
.
AddDeprecatedName
(
"ns3::LrWpanMacTrailer"
)
36
.
SetParent
<
Trailer
>()
37
.SetGroupName(
"LrWpan"
)
38
.AddConstructor<
LrWpanMacTrailer
>();
39
return
tid;
40
}
41
42
TypeId
43
LrWpanMacTrailer::GetInstanceTypeId
()
const
44
{
45
return
GetTypeId
();
46
}
47
48
void
49
LrWpanMacTrailer::Print
(std::ostream& os)
const
50
{
51
os <<
" FCS = "
<<
m_fcs
;
52
}
53
54
uint32_t
55
LrWpanMacTrailer::GetSerializedSize
()
const
56
{
57
return
LR_WPAN_MAC_FCS_LENGTH
;
58
}
59
60
void
61
LrWpanMacTrailer::Serialize
(
Buffer::Iterator
start)
const
62
{
63
start.Prev(
LR_WPAN_MAC_FCS_LENGTH
);
64
start.WriteU16(
m_fcs
);
65
}
66
67
uint32_t
68
LrWpanMacTrailer::Deserialize
(
Buffer::Iterator
start)
69
{
70
start.Prev(
LR_WPAN_MAC_FCS_LENGTH
);
71
m_fcs
= start.ReadU16();
72
73
return
LR_WPAN_MAC_FCS_LENGTH
;
74
}
75
76
uint16_t
77
LrWpanMacTrailer::GetFcs
()
const
78
{
79
return
m_fcs
;
80
}
81
82
void
83
LrWpanMacTrailer::SetFcs
(
Ptr<const Packet>
p)
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 */
99
bool
100
LrWpanMacTrailer::CheckFcs
(
Ptr<const Packet>
p)
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
120
void
121
LrWpanMacTrailer::EnableFcs
(
bool
enable)
122
{
123
m_calcFcs
= enable;
124
if
(!enable)
125
{
126
m_fcs
= 0;
127
}
128
}
129
130
bool
131
LrWpanMacTrailer::IsFcsEnabled
()
const
132
{
133
return
m_calcFcs
;
134
}
135
136
uint16_t
137
LrWpanMacTrailer::GenerateCrc16
(uint8_t*
data
,
int
length)
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
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::Trailer
Protocol trailer serialization and deserialization.
Definition
trailer.h:30
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::AddDeprecatedName
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition
type-id.cc:862
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
ns3::lrwpan::LrWpanMacTrailer
Represent the Mac Trailer with the Frame Check Sequence field.
Definition
lr-wpan-mac-trailer.h:31
ns3::lrwpan::LrWpanMacTrailer::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
lr-wpan-mac-trailer.cc:61
ns3::lrwpan::LrWpanMacTrailer::IsFcsEnabled
bool IsFcsEnabled() const
Query if FCS calculation is enabled for this trailer.
Definition
lr-wpan-mac-trailer.cc:131
ns3::lrwpan::LrWpanMacTrailer::EnableFcs
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
Definition
lr-wpan-mac-trailer.cc:121
ns3::lrwpan::LrWpanMacTrailer::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
lr-wpan-mac-trailer.cc:68
ns3::lrwpan::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:83
ns3::lrwpan::LrWpanMacTrailer::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
lr-wpan-mac-trailer.cc:55
ns3::lrwpan::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:100
ns3::lrwpan::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:112
ns3::lrwpan::LrWpanMacTrailer::m_fcs
uint16_t m_fcs
The FCS value stored in this trailer.
Definition
lr-wpan-mac-trailer.h:106
ns3::lrwpan::LrWpanMacTrailer::LrWpanMacTrailer
LrWpanMacTrailer()
Default constructor for a MAC trailer with disabled FCS calculation.
Definition
lr-wpan-mac-trailer.cc:25
ns3::lrwpan::LrWpanMacTrailer::GetFcs
uint16_t GetFcs() const
Get this trailers FCS value.
Definition
lr-wpan-mac-trailer.cc:77
ns3::lrwpan::LrWpanMacTrailer::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
lr-wpan-mac-trailer.cc:32
ns3::lrwpan::LrWpanMacTrailer::GenerateCrc16
uint16_t GenerateCrc16(uint8_t *data, int length)
Calculate the 16-bit FCS value.
Definition
lr-wpan-mac-trailer.cc:137
ns3::lrwpan::LrWpanMacTrailer::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
lr-wpan-mac-trailer.cc:43
ns3::lrwpan::LrWpanMacTrailer::Print
void Print(std::ostream &os) const override
Definition
lr-wpan-mac-trailer.cc:49
uint32_t
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
lr-wpan-mac-trailer.h
ns3::lrwpan::LR_WPAN_MAC_FCS_LENGTH
constexpr 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.cc:23
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
data
uint8_t data[writeSize]
Definition
socket-bound-tcp-static-routing.cc:41
src
lr-wpan
model
lr-wpan-mac-trailer.cc
Generated on Fri Nov 8 2024 13:59:02 for ns-3 by
1.11.0