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  ;
32 
34  : m_length (8),
35  m_present (0),
36  m_tsft (0),
37  m_flags (FRAME_FLAG_NONE),
38  m_rate (0),
39  m_channelFreq (0),
40  m_channelFlags (CHANNEL_FLAG_NONE),
41  m_antennaSignal (0),
42  m_antennaNoise (0)
43 {
44  NS_LOG_FUNCTION (this);
45 }
46 
48 {
49  static TypeId tid = TypeId ("ns3::RadiotapHeader")
50  .SetParent<Header> ()
51  .AddConstructor<RadiotapHeader> ()
52  ;
53  return tid;
54 }
55 
56 TypeId
58 {
59  return GetTypeId ();
60 }
61 
62 uint32_t
64 {
65  NS_LOG_FUNCTION (this);
66  return m_length;
67 }
68 
69 void
71 {
72  NS_LOG_FUNCTION (this << &start);
73 
74  start.WriteU8 (0); // major version of radiotap header
75  start.WriteU8 (0); // pad field
76  start.WriteU16 (m_length); // entire length of radiotap data + header
77  start.WriteU32 (m_present); // bits describing which fields follow header
78 
79  //
80  // Time Synchronization Function Timer (when the first bit of the MPDU
81  // arrived at the MAC)
82  //
83  if (m_present & RADIOTAP_TSFT) // bit 0
84  {
85  start.WriteU64 (m_tsft);
86  }
87 
88  //
89  // Properties of transmitted and received frames.
90  //
91  if (m_present & RADIOTAP_FLAGS) // bit 1
92  {
93  start.WriteU8 (m_flags);
94  }
95 
96  //
97  // TX/RX data rate in units of 500 kbps
98  //
99  if (m_present & RADIOTAP_RATE) // bit 2
100  {
101  start.WriteU8 (m_rate);
102  }
103 
104  //
105  // Tx/Rx frequency in MHz, followed by flags.
106  //
107  if (m_present & RADIOTAP_CHANNEL) // bit 3
108  {
109  start.WriteU16 (m_channelFreq);
110  start.WriteU16 (m_channelFlags);
111  }
112 
113  //
114  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
115  // reference.
116  //
117  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
118  {
119  start.WriteU8 (m_antennaSignal);
120  }
121 
122  //
123  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
124  // reference.
125  //
126  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
127  {
128  start.WriteU8 (m_antennaNoise);
129  }
130 }
131 
132 uint32_t
134 {
135  NS_LOG_FUNCTION (this << &start);
136 
137  uint8_t tmp = start.ReadU8 (); // major version of radiotap header
138  NS_ASSERT_MSG (tmp == 0x00, "RadiotapHeader::Deserialize(): Unexpected major version");
139  start.ReadU8 (); // pad field
140 
141  m_length = start.ReadU16 (); // entire length of radiotap data + header
142  m_present = start.ReadU32 (); // bits describing which fields follow header
143 
144  uint32_t bytesRead = 8;
145 
146  //
147  // Time Synchronization Function Timer (when the first bit of the MPDU arrived at the MAC)
148  //
149  if (m_present & RADIOTAP_TSFT) // bit 0
150  {
151  m_tsft = start.ReadU64 ();
152  bytesRead += 8;
153  }
154 
155  //
156  // Properties of transmitted and received frames.
157  //
158  if (m_present & RADIOTAP_FLAGS) // bit 1
159  {
160  m_flags = start.ReadU8 ();
161  ++bytesRead;
162  }
163 
164  //
165  // TX/RX data rate in units of 500 kbps
166  //
167  if (m_present & RADIOTAP_RATE) // bit 2
168  {
169  m_rate = start.ReadU8 ();
170  ++bytesRead;
171  }
172 
173  //
174  // Tx/Rx frequency in MHz, followed by flags.
175  //
176  if (m_present & RADIOTAP_CHANNEL) // bit 3
177  {
178  m_channelFreq = start.ReadU16 ();
179  m_channelFlags = start.ReadU16 ();
180  bytesRead += 4;
181  }
182 
183  //
184  // The hop set and pattern for frequency-hopping radios. We don't need it but
185  // still need to account for it.
186  //
187  if (m_present & RADIOTAP_FHSS) // bit 4
188  {
189  start.ReadU8 ();
190  ++bytesRead;
191  }
192 
193  //
194  // RF signal power at the antenna, decibel difference from an arbitrary, fixed
195  // reference.
196  //
197  if (m_present & RADIOTAP_DBM_ANTSIGNAL) // bit 5
198  {
199  m_antennaSignal = start.ReadU8 ();
200  ++bytesRead;
201  }
202 
203  //
204  // RF noise power at the antenna, decibel difference from an arbitrary, fixed
205  // reference.
206  //
207  if (m_present & RADIOTAP_DBM_ANTNOISE) // bit 6
208  {
209  m_antennaNoise = start.ReadU8 ();
210  ++bytesRead;
211  }
212 
213  NS_ASSERT_MSG (m_length == bytesRead, "RadiotapHeader::Deserialize(): expected and actual lengths inconsistent");
214  return bytesRead;
215 }
216 
217 void
218 RadiotapHeader::Print (std::ostream &os) const
219 {
220  NS_LOG_FUNCTION (this << &os);
221  os << " tsft=" << m_tsft
222  << " flags=" << std::hex << m_flags << std::dec
223  << " rate=" << (uint16_t) m_rate
224  << " freq=" << m_channelFreq
225  << " chflags=" << std::hex << (uint32_t)m_channelFlags << std::dec
226  << " signal=" << (int16_t) m_antennaSignal
227  << " noise=" << (int16_t) m_antennaNoise;
228 }
229 
230 void
231 RadiotapHeader::SetTsft (uint64_t value)
232 {
233  NS_LOG_FUNCTION (this << value);
234  m_tsft = value;
235 
236  if (!(m_present & RADIOTAP_TSFT))
237  {
239  m_length += 8;
240  }
241 
242  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
243 }
244 
245 uint64_t
247 {
248  NS_LOG_FUNCTION (this);
249  return m_tsft;
250 }
251 
252 void
254 {
255  NS_LOG_FUNCTION (this << static_cast<uint32_t> (flags));
256  m_flags = flags;
257 
258  if (!(m_present & RADIOTAP_FLAGS))
259  {
261  m_length += 1;
262  }
263 
264  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
265 }
266 
267 uint8_t
269 {
270  NS_LOG_FUNCTION (this);
271  return m_flags;
272 }
273 
274 void
276 {
277  NS_LOG_FUNCTION (this << static_cast<uint32_t> (rate));
278  m_rate = rate;
279 
280  if (!(m_present & RADIOTAP_RATE))
281  {
283  m_length += 1;
284  }
285 
286  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
287 }
288 
289 uint8_t
291 {
292  NS_LOG_FUNCTION (this);
293  return m_rate;
294 }
295 
296 void
297 RadiotapHeader::SetChannelFrequencyAndFlags (uint16_t frequency, uint16_t flags)
298 {
299  NS_LOG_FUNCTION (this << frequency << flags);
300  m_channelFreq = frequency;
301  m_channelFlags = flags;
302 
303  if (!(m_present & RADIOTAP_CHANNEL))
304  {
306  m_length += 4;
307  }
308 
309  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
310 }
311 
312 uint16_t
314 {
315  NS_LOG_FUNCTION (this);
316  return m_channelFreq;
317 }
318 
319 uint16_t
321 {
322  NS_LOG_FUNCTION (this);
323  return m_channelFlags;
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this << signal);
330 
332  {
334  m_length += 1;
335  }
336  if (signal > 127)
337  {
338  m_antennaSignal = 127;
339  }
340  else if (signal < -128)
341  {
342  m_antennaSignal = -128;
343  }
344  else
345  {
346  m_antennaSignal = static_cast<int8_t> (floor (signal + 0.5));
347  }
348 
349  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
350 }
351 
352 uint8_t
354 {
355  NS_LOG_FUNCTION (this);
356  return m_antennaSignal;
357 }
358 
359 void
361 {
362  NS_LOG_FUNCTION (this << noise);
363 
365  {
367  m_length += 1;
368  }
369  if (noise > 127.0)
370  {
371  m_antennaNoise = 127;
372  }
373  else if (noise < -128.0)
374  {
375  m_antennaNoise = -128;
376  }
377  else
378  {
379  m_antennaNoise = static_cast<int8_t> (floor (noise + 0.5));
380  }
381 
382  NS_LOG_LOGIC (this << " m_length=" << m_length << " m_present=0x" << std::hex << m_present << std::dec);
383 }
384 
385 uint8_t
387 {
388  NS_LOG_FUNCTION (this);
389  return m_antennaNoise;
390 }
391 
392 } // namespace ns3
uint16_t ReadU16(void)
Definition: buffer.h:845
Protocol header serialization and deserialization.
Definition: header.h:42
uint32_t ReadU32(void)
Definition: buffer.cc:997
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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...
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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)
iterator in a Buffer instance
Definition: buffer.h:98
NS_LOG_COMPONENT_DEFINE("RadiotapHeader")
void SetChannelFrequencyAndFlags(uint16_t frequency, uint16_t flags)
Set the transmit/receive channel frequency and flags.
void WriteU16(uint16_t data)
Definition: buffer.cc:895
void WriteU64(uint64_t data)
Definition: buffer.cc:915
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
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.
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.
uint64_t ReadU64(void)
Definition: buffer.cc:1014
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
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:690
uint8_t ReadU8(void)
Definition: buffer.h:819
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:903
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:611
void SetAntennaSignalPower(double signal)
Set the RF signal power at the antenna as a decibel difference from an arbitrary, fixed reference...