A Discrete-Event Network Simulator
API
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 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("RadiotapHeader");
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  .SetGroupName("Network")
51 
52  .AddConstructor<RadiotapHeader> ()
53  ;
54  return tid;
55 }
56 
57 TypeId
59 {
60  return GetTypeId ();
61 }
62 
63 uint32_t
65 {
66  NS_LOG_FUNCTION (this);
67  return m_length;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this << &start);
74 
75  start.WriteU8 (0); // major version of radiotap header
76  start.WriteU8 (0); // pad field
77  start.WriteU16 (m_length); // entire length of radiotap data + header
78  start.WriteU32 (m_present); // bits describing which fields follow header
79 
80  //
81  // Time Synchronization Function Timer (when the first bit of the MPDU
82  // arrived at the MAC)
83  //
84  if (m_present & RADIOTAP_TSFT) // bit 0
85  {
86  start.WriteU64 (m_tsft);
87  }
88 
89  //
90  // Properties of transmitted and received frames.
91  //
92  if (m_present & RADIOTAP_FLAGS) // bit 1
93  {
94  start.WriteU8 (m_flags);
95  }
96 
97  //
98  // TX/RX data rate in units of 500 kbps
99  //
100  if (m_present & RADIOTAP_RATE) // bit 2
101  {
102  start.WriteU8 (m_rate);
103  }
104 
105  //
106  // Tx/Rx frequency in MHz, followed by flags.
107  //
108  if (m_present & RADIOTAP_CHANNEL) // bit 3
109  {
110  start.WriteU16 (m_channelFreq);
111  start.WriteU16 (m_channelFlags);
112  }
113 
114  //
115  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
116  // reference.
117  //
118  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
119  {
120  start.WriteU8 (m_antennaSignal);
121  }
122 
123  //
124  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
125  // reference.
126  //
127  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
128  {
129  start.WriteU8 (m_antennaNoise);
130  }
131 }
132 
133 uint32_t
135 {
136  NS_LOG_FUNCTION (this << &start);
137 
138  uint8_t tmp = start.ReadU8 (); // major version of radiotap header
139  NS_ASSERT_MSG (tmp == 0x00, "RadiotapHeader::Deserialize(): Unexpected major version");
140  start.ReadU8 (); // pad field
141 
142  m_length = start.ReadU16 (); // entire length of radiotap data + header
143  m_present = start.ReadU32 (); // bits describing which fields follow header
144 
145  uint32_t bytesRead = 8;
146 
147  //
148  // Time Synchronization Function Timer (when the first bit of the MPDU arrived at the MAC)
149  //
150  if (m_present & RADIOTAP_TSFT) // bit 0
151  {
152  m_tsft = start.ReadU64 ();
153  bytesRead += 8;
154  }
155 
156  //
157  // Properties of transmitted and received frames.
158  //
159  if (m_present & RADIOTAP_FLAGS) // bit 1
160  {
161  m_flags = start.ReadU8 ();
162  ++bytesRead;
163  }
164 
165  //
166  // TX/RX data rate in units of 500 kbps
167  //
168  if (m_present & RADIOTAP_RATE) // bit 2
169  {
170  m_rate = start.ReadU8 ();
171  ++bytesRead;
172  }
173 
174  //
175  // Tx/Rx frequency in MHz, followed by flags.
176  //
177  if (m_present & RADIOTAP_CHANNEL) // bit 3
178  {
179  m_channelFreq = start.ReadU16 ();
180  m_channelFlags = start.ReadU16 ();
181  bytesRead += 4;
182  }
183 
184  //
185  // The hop set and pattern for frequency-hopping radios. We don't need it but
186  // still need to account for it.
187  //
188  if (m_present & RADIOTAP_FHSS) // bit 4
189  {
190  start.ReadU8 ();
191  ++bytesRead;
192  }
193 
194  //
195  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
196  // reference.
197  //
198  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
199  {
200  m_antennaSignal = start.ReadU8 ();
201  ++bytesRead;
202  }
203 
204  //
205  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
206  // reference.
207  //
208  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
209  {
210  m_antennaNoise = start.ReadU8 ();
211  ++bytesRead;
212  }
213 
214  NS_ASSERT_MSG (m_length == bytesRead, "RadiotapHeader::Deserialize(): expected and actual lengths inconsistent");
215  return bytesRead;
216 }
217 
218 void
219 RadiotapHeader::Print (std::ostream &os) const
220 {
221  NS_LOG_FUNCTION (this << &os);
222  os << " tsft=" << m_tsft
223  << " flags=" << std::hex << m_flags << std::dec
224  << " rate=" << (uint16_t) m_rate
225  << " freq=" << m_channelFreq
226  << " chflags=" << std::hex << (uint32_t)m_channelFlags << std::dec
227  << " signal=" << (int16_t) m_antennaSignal
228  << " noise=" << (int16_t) m_antennaNoise;
229 }
230 
231 void
232 RadiotapHeader::SetTsft (uint64_t value)
233 {
234  NS_LOG_FUNCTION (this << value);
235  m_tsft = value;
236 
237  if (!(m_present & RADIOTAP_TSFT))
238  {
240  m_length += 8;
241  }
242 
243  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
244 }
245 
246 uint64_t
248 {
249  NS_LOG_FUNCTION (this);
250  return m_tsft;
251 }
252 
253 void
255 {
256  NS_LOG_FUNCTION (this << static_cast<uint32_t> (flags));
257  m_flags = flags;
258 
259  if (!(m_present & RADIOTAP_FLAGS))
260  {
262  m_length += 1;
263  }
264 
265  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
266 }
267 
268 uint8_t
270 {
271  NS_LOG_FUNCTION (this);
272  return m_flags;
273 }
274 
275 void
277 {
278  NS_LOG_FUNCTION (this << static_cast<uint32_t> (rate));
279  m_rate = rate;
280 
281  if (!(m_present & RADIOTAP_RATE))
282  {
284  m_length += 1;
285  }
286 
287  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
288 }
289 
290 uint8_t
292 {
293  NS_LOG_FUNCTION (this);
294  return m_rate;
295 }
296 
297 void
298 RadiotapHeader::SetChannelFrequencyAndFlags (uint16_t frequency, uint16_t flags)
299 {
300  NS_LOG_FUNCTION (this << frequency << flags);
301  m_channelFreq = frequency;
302  m_channelFlags = flags;
303 
304  if (!(m_present & RADIOTAP_CHANNEL))
305  {
307  m_length += 4;
308  }
309 
310  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
311 }
312 
313 uint16_t
315 {
316  NS_LOG_FUNCTION (this);
317  return m_channelFreq;
318 }
319 
320 uint16_t
322 {
323  NS_LOG_FUNCTION (this);
324  return m_channelFlags;
325 }
326 
327 void
329 {
330  NS_LOG_FUNCTION (this << signal);
331 
333  {
335  m_length += 1;
336  }
337  if (signal > 127)
338  {
339  m_antennaSignal = 127;
340  }
341  else if (signal < -128)
342  {
343  m_antennaSignal = -128;
344  }
345  else
346  {
347  m_antennaSignal = static_cast<int8_t> (floor (signal + 0.5));
348  }
349 
350  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
351 }
352 
353 uint8_t
355 {
356  NS_LOG_FUNCTION (this);
357  return m_antennaSignal;
358 }
359 
360 void
362 {
363  NS_LOG_FUNCTION (this << noise);
364 
366  {
368  m_length += 1;
369  }
370  if (noise > 127.0)
371  {
372  m_antennaNoise = 127;
373  }
374  else if (noise < -128.0)
375  {
376  m_antennaNoise = -128;
377  }
378  else
379  {
380  m_antennaNoise = static_cast<int8_t> (floor (noise + 0.5));
381  }
382 
383  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
384 }
385 
386 uint8_t
388 {
389  NS_LOG_FUNCTION (this);
390  return m_antennaNoise;
391 }
392 
393 } // 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 an Object subclass with the TypeId system.
Definition: object-base.h:44
Radiotap header implementation.
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.
def start()
Definition: core.py:1482
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:201
uint16_t m_channelFreq
Tx/Rx frequency in MHz.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
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:252
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. ...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:90
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void SetAntennaSignalPower(double signal)
Set the RF signal power at the antenna as a decibel difference from an arbitrary, fixed reference...