A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
radiotap-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
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, Include., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include <iomanip>
22 #include <cmath>
23 #include "ns3/log.h"
24 #include "radiotap-header.h"
25 
26 NS_LOG_COMPONENT_DEFINE ("RadiotapHeader");
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (RadiotapHeader);
31 
33  : m_length (8),
34  m_present (0),
35  m_tsft (0),
36  m_flags (FRAME_FLAG_NONE),
37  m_rate (0),
38  m_channelFreq (0),
39  m_channelFlags (CHANNEL_FLAG_NONE),
40  m_antennaSignal (0),
41  m_antennaNoise (0)
42 {
43  NS_LOG_FUNCTION (this);
44 }
45 
47 {
48  static TypeId tid = TypeId ("ns3::RadiotapHeader")
49  .SetParent<Header> ()
50  .AddConstructor<RadiotapHeader> ()
51  ;
52  return tid;
53 }
54 
55 TypeId
57 {
58  return GetTypeId ();
59 }
60 
61 uint32_t
63 {
64  NS_LOG_FUNCTION (this);
65  return m_length;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION (this << &start);
72 
73  start.WriteU8 (0); // major version of radiotap header
74  start.WriteU8 (0); // pad field
75  start.WriteU16 (m_length); // entire length of radiotap data + header
76  start.WriteU32 (m_present); // bits describing which fields follow header
77 
78  //
79  // Time Synchronization Function Timer (when the first bit of the MPDU
80  // arrived at the MAC)
81  //
82  if (m_present & RADIOTAP_TSFT) // bit 0
83  {
84  start.WriteU64 (m_tsft);
85  }
86 
87  //
88  // Properties of transmitted and received frames.
89  //
90  if (m_present & RADIOTAP_FLAGS) // bit 1
91  {
92  start.WriteU8 (m_flags);
93  }
94 
95  //
96  // TX/RX data rate in units of 500 kbps
97  //
98  if (m_present & RADIOTAP_RATE) // bit 2
99  {
100  start.WriteU8 (m_rate);
101  }
102 
103  //
104  // Tx/Rx frequency in MHz, followed by flags.
105  //
106  if (m_present & RADIOTAP_CHANNEL) // bit 3
107  {
108  start.WriteU16 (m_channelFreq);
109  start.WriteU16 (m_channelFlags);
110  }
111 
112  //
113  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
114  // reference.
115  //
116  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
117  {
118  start.WriteU8 (m_antennaSignal);
119  }
120 
121  //
122  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
123  // reference.
124  //
125  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
126  {
127  start.WriteU8 (m_antennaNoise);
128  }
129 }
130 
131 uint32_t
133 {
134  NS_LOG_FUNCTION (this << &start);
135 
136  uint8_t tmp = start.ReadU8 (); // major version of radiotap header
137  NS_ASSERT_MSG (tmp == 0x00, "RadiotapHeader::Deserialize(): Unexpected major version");
138  start.ReadU8 (); // pad field
139 
140  m_length = start.ReadU16 (); // entire length of radiotap data + header
141  m_present = start.ReadU32 (); // bits describing which fields follow header
142 
143  uint32_t bytesRead = 8;
144 
145  //
146  // Time Synchronization Function Timer (when the first bit of the MPDU arrived at the MAC)
147  //
148  if (m_present & RADIOTAP_TSFT) // bit 0
149  {
150  m_tsft = start.ReadU64 ();
151  bytesRead += 8;
152  }
153 
154  //
155  // Properties of transmitted and received frames.
156  //
157  if (m_present & RADIOTAP_FLAGS) // bit 1
158  {
159  m_flags = start.ReadU8 ();
160  ++bytesRead;
161  }
162 
163  //
164  // TX/RX data rate in units of 500 kbps
165  //
166  if (m_present & RADIOTAP_RATE) // bit 2
167  {
168  m_rate = start.ReadU8 ();
169  ++bytesRead;
170  }
171 
172  //
173  // Tx/Rx frequency in MHz, followed by flags.
174  //
175  if (m_present & RADIOTAP_CHANNEL) // bit 3
176  {
177  m_channelFreq = start.ReadU16 ();
178  m_channelFlags = start.ReadU16 ();
179  bytesRead += 4;
180  }
181 
182  //
183  // The hop set and pattern for frequency-hopping radios. We don't need it but
184  // still need to account for it.
185  //
186  if (m_present & RADIOTAP_FHSS) // bit 4
187  {
188  start.ReadU8 ();
189  ++bytesRead;
190  }
191 
192  //
193  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
194  // reference.
195  //
196  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
197  {
198  m_antennaSignal = start.ReadU8 ();
199  ++bytesRead;
200  }
201 
202  //
203  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
204  // reference.
205  //
206  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
207  {
208  m_antennaNoise = start.ReadU8 ();
209  ++bytesRead;
210  }
211 
212  NS_ASSERT_MSG (m_length == bytesRead, "RadiotapHeader::Deserialize(): expected and actual lengths inconsistent");
213  return bytesRead;
214 }
215 
216 void
217 RadiotapHeader::Print (std::ostream &os) const
218 {
219  NS_LOG_FUNCTION (this << &os);
220  os << " tsft=" << m_tsft
221  << " flags=" << std::hex << m_flags << std::dec
222  << " rate=" << (uint16_t) m_rate
223  << " freq=" << m_channelFreq
224  << " chflags=" << std::hex << (uint32_t)m_channelFlags << std::dec
225  << " signal=" << (int16_t) m_antennaSignal
226  << " noise=" << (int16_t) m_antennaNoise;
227 }
228 
229 void
230 RadiotapHeader::SetTsft (uint64_t value)
231 {
232  NS_LOG_FUNCTION (this << value);
233  m_tsft = value;
234 
235  if (!(m_present & RADIOTAP_TSFT))
236  {
238  m_length += 8;
239  }
240 
241  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
242 }
243 
244 uint64_t
246 {
247  NS_LOG_FUNCTION (this);
248  return m_tsft;
249 }
250 
251 void
253 {
254  NS_LOG_FUNCTION (this << static_cast<uint32_t> (flags));
255  m_flags = flags;
256 
257  if (!(m_present & RADIOTAP_FLAGS))
258  {
260  m_length += 1;
261  }
262 
263  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
264 }
265 
266 uint8_t
268 {
269  NS_LOG_FUNCTION (this);
270  return m_flags;
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION (this << static_cast<uint32_t> (rate));
277  m_rate = rate;
278 
279  if (!(m_present & RADIOTAP_RATE))
280  {
282  m_length += 1;
283  }
284 
285  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
286 }
287 
288 uint8_t
290 {
291  NS_LOG_FUNCTION (this);
292  return m_rate;
293 }
294 
295 void
296 RadiotapHeader::SetChannelFrequencyAndFlags (uint16_t frequency, uint16_t flags)
297 {
298  NS_LOG_FUNCTION (this << frequency << flags);
299  m_channelFreq = frequency;
300  m_channelFlags = flags;
301 
302  if (!(m_present & RADIOTAP_CHANNEL))
303  {
305  m_length += 4;
306  }
307 
308  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
309 }
310 
311 uint16_t
313 {
314  NS_LOG_FUNCTION (this);
315  return m_channelFreq;
316 }
317 
318 uint16_t
320 {
321  NS_LOG_FUNCTION (this);
322  return m_channelFlags;
323 }
324 
325 void
327 {
328  NS_LOG_FUNCTION (this << signal);
329 
331  {
333  m_length += 1;
334  }
335  if (signal > 127)
336  {
337  m_antennaSignal = 127;
338  }
339  else if (signal < -128)
340  {
341  m_antennaSignal = -128;
342  }
343  else
344  {
345  m_antennaSignal = static_cast<int8_t> (floor (signal + 0.5));
346  }
347 
348  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
349 }
350 
351 uint8_t
353 {
354  NS_LOG_FUNCTION (this);
355  return m_antennaSignal;
356 }
357 
358 void
360 {
361  NS_LOG_FUNCTION (this << noise);
362 
364  {
366  m_length += 1;
367  }
368  if (noise > 127.0)
369  {
370  m_antennaNoise = 127;
371  }
372  else if (noise < -128.0)
373  {
374  m_antennaNoise = -128;
375  }
376  else
377  {
378  m_antennaNoise = static_cast<int8_t> (floor (noise + 0.5));
379  }
380 
381  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
382 }
383 
384 uint8_t
386 {
387  NS_LOG_FUNCTION (this);
388  return m_antennaNoise;
389 }
390 
391 } // namespace ns3
uint16_t ReadU16(void)
Definition: buffer.h:1036
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t ReadU32(void)
Definition: buffer.cc:1001
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
uint64_t GetTsft(void) const
Get the Time Synchronization Function Timer (TSFT) value.
virtual void Print(std::ostream &os) const
This method is used by Packet::Print to print the content of the header as ascii data to a C++ output...
uint8_t GetRate(void) const
Get the transmit/receive channel frequency in units of megahertz.
virtual uint32_t Deserialize(Buffer::Iterator start)
This method is used by Packet::RemoveHeader to re-create a header from the byte buffer of a packet...
virtual uint32_t GetSerializedSize(void) const
This method is used by Packet::AddHeader to store the header into the byte buffer of a packet...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint16_t m_channelFreq
Tx/Rx frequency in MHz.
virtual TypeId GetInstanceTypeId(void) const
void SetAntennaNoisePower(double noise)
Set the RF noise power at the antenna as a decibel difference from an arbitrary, fixed reference...
uint8_t GetAntennaNoisePower(void) const
Get the RF noise power at the antenna as a decibel difference from an arbitrary, fixed reference...
void SetTsft(uint64_t tsft)
Set the Time Synchronization Function Timer (TSFT) value.
static TypeId GetTypeId(void)
Get the type ID.
iterator in a Buffer instance
Definition: buffer.h:98
void SetChannelFrequencyAndFlags(uint16_t frequency, uint16_t flags)
Set the transmit/receive channel frequency and flags.
uint64_t m_tsft
Time Synchronization Function Timer (when the first bit of the MPDU arrived at the MAC) ...
void WriteU16(uint16_t data)
Definition: buffer.cc:899
int8_t m_antennaNoise
RF noise power at the antenna, dB difference from an arbitrary, fixed reference.
void WriteU64(uint64_t data)
Definition: buffer.cc:919
uint16_t m_channelFlags
Tx/Rx channel flags.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
void SetFrameFlags(uint8_t flags)
Set the frame flags of the transmitted or received frame.
void SetRate(uint8_t rate)
Set the transmit/receive channel frequency in units of megahertz.
int8_t m_antennaSignal
RF signal power at the antenna, dB difference from an arbitrary, fixed reference. ...
uint8_t m_flags
Properties of transmitted and received frames.
uint8_t GetFrameFlags(void) const
Get the frame flags of the transmitted or received frame.
uint16_t GetChannelFlags(void) const
Get the channel flags of the transmitted or received frame.
uint32_t m_present
bits describing which fields follow header
uint64_t ReadU64(void)
Definition: buffer.cc:1018
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
uint8_t GetAntennaSignalPower(void) const
Get the RF signal power at the antenna as a decibel difference from an arbitrary, fixed reference...
void WriteU8(uint8_t data)
Definition: buffer.h:876
uint8_t ReadU8(void)
Definition: buffer.h:1028
uint16_t m_length
entire length of radiotap data + header
uint8_t m_rate
TX/RX data rate in units of 500 kbps.
virtual void Serialize(Buffer::Iterator start) const
This method is used by Packet::AddHeader to store the header into the byte buffer of a packet...
void WriteU32(uint32_t data)
Definition: buffer.cc:907
uint16_t GetChannelFrequency(void) const
Get the transmit/receive data rate in units of 500 kbps.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void SetAntennaSignalPower(double signal)
Set the RF signal power at the antenna as a decibel difference from an arbitrary, fixed reference...