A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
31 
33  : m_fcs (0),
34  m_calcFcs (false)
35 {
36 }
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::LrWpanMacTrailer")
42  .SetParent<Trailer> ()
43  .AddConstructor<LrWpanMacTrailer> ()
44  ;
45  return tid;
46 }
47 
48 TypeId
50 {
51  return GetTypeId ();
52 }
53 
54 void
55 LrWpanMacTrailer::Print (std::ostream &os) const
56 {
57  os << " FCS = " << m_fcs;
58 }
59 
60 uint32_t
62 {
64 }
65 
66 void
68 {
70  start.WriteU16 (m_fcs);
71 }
72 
73 uint32_t
75 {
77  m_fcs = start.ReadU16 ();
78 
80 }
81 
82 uint16_t
84 {
85  return m_fcs;
86 }
87 
88 void
90 {
91  if (m_calcFcs)
92  {
93  uint16_t size = p->GetSize ();
94  uint8_t *serial_packet = new uint8_t[size];
95 
96  p->CopyData (serial_packet, size);
97 
98  m_fcs = GenerateCrc16 (serial_packet, size);
99  delete[] serial_packet;
100  }
101 }
102 
103 /* Be sure to have removed the trailer and only the trailer
104  * from the packet before to use CheckFcs */
105 bool
107 {
108  if (!m_calcFcs)
109  {
110  return true;
111  }
112  else
113  {
114  uint16_t checkFcs;
115  uint16_t size = p->GetSize ();
116  uint8_t *serial_packet = new uint8_t[size];
117 
118  p->CopyData (serial_packet, size);
119 
120  checkFcs = GenerateCrc16 (serial_packet, size);
121  delete[] serial_packet;
122  return (checkFcs == GetFcs ());
123  }
124 }
125 
126 void
128 {
129  m_calcFcs = enable;
130  if (!enable)
131  {
132  m_fcs = 0;
133  }
134 }
135 
136 bool
138 {
139  return m_calcFcs;
140 }
141 
142 uint16_t
143 LrWpanMacTrailer::GenerateCrc16 (uint8_t *data, int length)
144 {
145  int i;
146  uint16_t accumulator = 0;
147 
148  for (i = 0; i < length; ++i)
149  {
150  accumulator ^= *data;
151  accumulator = (accumulator >> 8) | (accumulator << 8);
152  accumulator ^= (accumulator & 0xff00) << 4;
153  accumulator ^= (accumulator >> 8) >> 4;
154  accumulator ^= (accumulator & 0xff00) >> 5;
155  ++data;
156  }
157  return accumulator;
158 }
159 
160 } //namespace ns3
uint16_t ReadU16(void)
Definition: buffer.h:1036
void EnableFcs(bool enable)
Enable or disable FCS calculation for this trailer.
bool IsFcsEnabled(void)
Query if FCS calculation is enabled for this trailer.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
iterator in a Buffer instance
Definition: buffer.h:98
virtual uint32_t GetSerializedSize(void) const
virtual uint32_t Deserialize(Buffer::Iterator start)
void Prev(void)
go backward by one byte
Definition: buffer.h:858
uint8_t data[writeSize]
void WriteU16(uint16_t data)
Definition: buffer.cc:899
LrWpanMacTrailer(void)
Default constructor for a MAC trailer with disabled FCS calculation.
virtual TypeId GetInstanceTypeId(void) const
void SetFcs(Ptr< const Packet > p)
Calculate and set the FCS value based on the given packet.
uint16_t GetFcs(void) const
Get this trailers FCS value.
Protocol trailer serialization and deserialization.
Definition: trailer.h:40
bool m_calcFcs
Only if m_calcFcs is true, FCS values will be calculated and used in the trailer. ...
virtual void Print(std::ostream &os) const
static const uint16_t LR_WPAN_MAC_FCS_LENGTH
The length in octets of the IEEE 802.15.4 MAC FCS field.
virtual void Serialize(Buffer::Iterator start) const
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
uint16_t GenerateCrc16(uint8_t *data, int length)
Calculate the 16-bit FCS value.
bool CheckFcs(Ptr< const Packet > p)
Check the FCS of a given packet against the FCS value stored in the trailer.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
uint16_t m_fcs
The FCS value stored in this trailer.
static TypeId GetTypeId(void)
Get the type ID.