A Discrete-Event Network Simulator
API
uan-transducer-hd.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Leonard Tracy <lentracy@gmail.com>
18  */
19 
20 #include "uan-transducer-hd.h"
21 
22 #include "uan-channel.h"
23 #include "uan-phy.h"
24 #include "uan-prop-model.h"
25 
26 #include "ns3/double.h"
27 #include "ns3/log.h"
28 #include "ns3/pointer.h"
29 #include "ns3/simulator.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("UanTransducerHd");
35 
36 NS_OBJECT_ENSURE_REGISTERED(UanTransducerHd);
37 
39  : UanTransducer(),
40  m_state(RX),
41  m_endTxTime(Seconds(0)),
42  m_cleared(false),
43  m_rxGainDb(0)
44 {
45 }
46 
48 {
49 }
50 
51 void
53 {
54  if (m_cleared)
55  {
56  return;
57  }
58  m_cleared = true;
59  if (m_channel)
60  {
61  m_channel->Clear();
62  m_channel = nullptr;
63  }
64 
65  auto it = m_phyList.begin();
66  for (; it != m_phyList.end(); it++)
67  {
68  if (*it)
69  {
70  (*it)->Clear();
71  *it = nullptr;
72  }
73  }
74  auto ait = m_arrivalList.begin();
75  for (; ait != m_arrivalList.end(); ait++)
76  {
77  ait->GetPacket() = nullptr;
78  }
79  m_phyList.clear();
80  m_arrivalList.clear();
82 }
83 
84 void
86 {
87  Clear();
89 }
90 
91 TypeId
93 {
94  static TypeId tid = TypeId("ns3::UanTransducerHd")
96  .SetGroupName("Uan")
97  .AddConstructor<UanTransducerHd>()
98  .AddAttribute("RxGainDb",
99  "Gain in Db added to incoming signal at receiver.",
100  DoubleValue(0),
102  MakeDoubleChecker<double>());
103  return tid;
104 }
105 
108 {
109  return m_state;
110 }
111 
112 bool
114 {
115  return m_state == RX;
116 }
117 
118 bool
120 {
121  return m_state == TX;
122 }
123 
126 {
127  return m_arrivalList;
128 }
129 
130 void
132 {
133  m_rxGainDb = gainDb;
134 }
135 
136 double
138 {
139  return m_rxGainDb;
140 }
141 
142 double
144 {
145  NS_LOG_FUNCTION(this << rxPowerDb << mode);
146  rxPowerDb += GetRxGainDb();
147  NS_LOG_DEBUG("Rx power after RX gain = " << rxPowerDb << " db re uPa");
148  return rxPowerDb;
149 }
150 
151 void
152 UanTransducerHd::Receive(Ptr<Packet> packet, double rxPowerDb, UanTxMode txMode, UanPdp pdp)
153 {
154  NS_LOG_FUNCTION(this << packet << rxPowerDb << txMode << pdp);
155  // Apply receiver gain in dB
156  rxPowerDb = ApplyRxGainDb(rxPowerDb, txMode);
157 
158  UanPacketArrival arrival(packet, rxPowerDb, txMode, pdp, Simulator::Now());
159 
160  m_arrivalList.push_back(arrival);
161  Time txDelay = Seconds(packet->GetSize() * 8.0 / txMode.GetDataRateBps());
162  Simulator::Schedule(txDelay, &UanTransducerHd::RemoveArrival, this, arrival);
163  NS_LOG_DEBUG(Now().As(Time::S) << " Transducer in receive");
164  if (m_state == RX)
165  {
166  NS_LOG_DEBUG("Transducer state = RX");
167  auto it = m_phyList.begin();
168  for (; it != m_phyList.end(); it++)
169  {
170  NS_LOG_DEBUG("Calling StartRx");
171  (*it)->StartRxPacket(packet, rxPowerDb, txMode, pdp);
172  }
173  }
174 }
175 
176 void
177 UanTransducerHd::Transmit(Ptr<UanPhy> src, Ptr<Packet> packet, double txPowerDb, UanTxMode txMode)
178 {
179  if (m_state == TX)
180  {
182  src->NotifyTxDrop(packet); // traced source netanim
183  }
184  else
185  {
186  m_state = TX;
187  src->NotifyTxBegin(packet); // traced source netanim
188  }
189 
190  Time delay = Seconds(packet->GetSize() * 8.0 / txMode.GetDataRateBps());
191  NS_LOG_DEBUG("Transducer transmitting: TX delay = "
192  << delay << " seconds for packet size " << packet->GetSize()
193  << " bytes and rate = " << txMode.GetDataRateBps() << " bps");
194  auto it = m_phyList.begin();
195  for (; it != m_phyList.end(); it++)
196  {
197  if (src != (*it))
198  {
199  (*it)->NotifyTransStartTx(packet, txPowerDb, txMode);
200  }
201  }
202  m_channel->TxPacket(Ptr<UanTransducer>(this), packet, txPowerDb, txMode);
203 
204  delay = std::max(delay, m_endTxTime - Simulator::Now());
205 
207  m_endTxTime = Simulator::Now() + delay;
208  Simulator::Schedule(delay, &UanPhy::NotifyTxEnd, src, packet); // traced source netanim
209 }
210 
211 void
213 {
214  NS_ASSERT(m_state == TX);
215  m_state = RX;
216  m_endTxTime = Seconds(0);
217 }
218 
219 void
221 {
222  NS_LOG_DEBUG("Transducer setting channel");
223  m_channel = chan;
224 }
225 
228 {
229  return m_channel;
230 }
231 
232 void
234 {
235  m_phyList.push_back(phy);
236 }
237 
240 {
241  return m_phyList;
242 }
243 
244 void
246 {
247  // Remove entry from arrival list
248  auto it = m_arrivalList.begin();
249  for (; it != m_arrivalList.end(); it++)
250  {
251  if (it->GetPacket() == arrival.GetPacket())
252  {
253  m_arrivalList.erase(it);
254  break;
255  }
256  }
257  auto ait = m_phyList.begin();
258  for (; ait != m_phyList.end(); ait++)
259  {
260  (*ait)->NotifyIntChange();
261  }
262 }
263 
264 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
@ S
second
Definition: nstime.h:116
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Class consisting of packet arrival information (Time, RxPower, mode, PDP).
Ptr< Packet > GetPacket() const
Get the arriving packet.
The power delay profile returned by propagation models.
void NotifyTxEnd(Ptr< const Packet > packet)
Called when the transducer finishes transmitting a packet.
Definition: uan-phy.cc:116
Half duplex implementation of transducer object.
void EndTx()
Handle end of transmission event.
State GetState() const override
Get the transducer state.
void SetChannel(Ptr< UanChannel > chan) override
Attach this transducer to a channel.
void Clear() override
Clears all pointer references.
bool IsTx() const override
Is the state transmitting?
Ptr< UanChannel > GetChannel() const override
Get the attached channel.
State m_state
Transducer state.
bool m_cleared
Flab when we've been cleared.
static TypeId GetTypeId()
Register this type.
void Transmit(Ptr< UanPhy > src, Ptr< Packet > packet, double txPowerDb, UanTxMode txMode) override
Transmit a packet from this transducer.
double m_rxGainDb
Receive gain in dB.
Ptr< UanChannel > m_channel
The attached channel.
const UanPhyList & GetPhyList() const override
Get the list of physical layer above this transducer.
ArrivalList m_arrivalList
List of arriving packets which overlap in time.
~UanTransducerHd() override
Dummy destructor, see DoDispose.
void RemoveArrival(UanPacketArrival arrival)
Remove an entry from the arrival list.
void SetRxGainDb(double gainDb) override
Set the receiver gain.
bool IsRx() const override
Is the state receiving (or available for reception)?
EventId m_endTxEvent
Event scheduled for end of transmission.
void AddPhy(Ptr< UanPhy >) override
Attach a physical network layer above this transducer.
double GetRxGainDb() override
Get the receiver gain added to signal at receiver in dB.
double ApplyRxGainDb(double rxPowerDb, UanTxMode mode) override
Apply receiver gain in dB to the received power.
UanTransducerHd()
Constructor.
void Receive(Ptr< Packet > packet, double rxPowerDb, UanTxMode txMode, UanPdp pdp) override
Notify this object that a new packet has arrived at this nodes location.
Time m_endTxTime
Time at which transmission will be completed.
void DoDispose() override
Destructor implementation.
const ArrivalList & GetArrivalList() const override
Get the list of overlapped (in time) packets at this transducer.
UanPhyList m_phyList
List of physical layers attached above this tranducer.
Virtual base for Transducer objects.
State
Transducer state.
@ TX
Transmitting.
std::list< Ptr< UanPhy > > UanPhyList
List of UanPhy objects.
std::list< UanPacketArrival > ArrivalList
List of arriving packets overlapping in time.
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:43
uint32_t GetDataRateBps() const
Get the data rate of the transmit mode.
Definition: uan-tx-mode.cc:46
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:46
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43
phy
Definition: third.py:89
@ RX
The PHY layer is receiving a packet.