A Discrete-Event Network Simulator
API
uan-channel.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-channel.h"
21 
22 #include "uan-net-device.h"
24 #include "uan-phy.h"
25 #include "uan-prop-model-ideal.h"
26 #include "uan-prop-model.h"
27 #include "uan-transducer.h"
28 #include "uan-tx-mode.h"
29 
30 #include "ns3/log.h"
31 #include "ns3/mobility-model.h"
32 #include "ns3/net-device.h"
33 #include "ns3/node.h"
34 #include "ns3/object.h"
35 #include "ns3/packet.h"
36 #include "ns3/pointer.h"
37 #include "ns3/simulator.h"
38 #include "ns3/string.h"
39 
40 namespace ns3
41 {
42 
43 NS_LOG_COMPONENT_DEFINE("UanChannel");
44 
45 NS_OBJECT_ENSURE_REGISTERED(UanChannel);
46 
47 TypeId
49 {
50  static TypeId tid = TypeId("ns3::UanChannel")
51  .SetParent<Channel>()
52  .SetGroupName("Uan")
53  .AddConstructor<UanChannel>()
54  .AddAttribute("PropagationModel",
55  "A pointer to the propagation model.",
56  StringValue("ns3::UanPropModelIdeal"),
58  MakePointerChecker<UanPropModel>())
59  .AddAttribute("NoiseModel",
60  "A pointer to the model of the channel ambient noise.",
61  StringValue("ns3::UanNoiseModelDefault"),
63  MakePointerChecker<UanNoiseModel>());
64 
65  return tid;
66 }
67 
69  : Channel(),
70  m_prop(nullptr),
71  m_cleared(false)
72 {
73 }
74 
76 {
77 }
78 
79 void
81 {
82  if (m_cleared)
83  {
84  return;
85  }
86  m_cleared = true;
87  auto it = m_devList.begin();
88  for (; it != m_devList.end(); it++)
89  {
90  if (it->first)
91  {
92  it->first->Clear();
93  it->first = nullptr;
94  }
95  if (it->second)
96  {
97  it->second->Clear();
98  it->second = nullptr;
99  }
100  }
101  m_devList.clear();
102  if (m_prop)
103  {
104  m_prop->Clear();
105  m_prop = nullptr;
106  }
107  if (m_noise)
108  {
109  m_noise->Clear();
110  m_noise = nullptr;
111  }
112 }
113 
114 void
116 {
117  Clear();
119 }
120 
121 void
123 {
124  NS_LOG_DEBUG("Set Prop Model " << this);
125  m_prop = prop;
126 }
127 
128 std::size_t
130 {
131  return m_devList.size();
132 }
133 
135 UanChannel::GetDevice(std::size_t i) const
136 {
137  return m_devList[i].first;
138 }
139 
140 void
142 {
143  NS_LOG_DEBUG("Adding dev/trans pair number " << m_devList.size());
144  m_devList.emplace_back(dev, trans);
145 }
146 
147 void
148 UanChannel::TxPacket(Ptr<UanTransducer> src, Ptr<Packet> packet, double txPowerDb, UanTxMode txMode)
149 {
150  Ptr<MobilityModel> senderMobility = nullptr;
151 
152  NS_LOG_DEBUG("Channel scheduling");
153  for (auto i = m_devList.begin(); i != m_devList.end(); i++)
154  {
155  if (src == i->second)
156  {
157  senderMobility = i->first->GetNode()->GetObject<MobilityModel>();
158  break;
159  }
160  }
161  NS_ASSERT(senderMobility);
162  uint32_t j = 0;
163  auto i = m_devList.begin();
164  for (; i != m_devList.end(); i++)
165  {
166  if (src != i->second)
167  {
168  NS_LOG_DEBUG("Scheduling " << i->first->GetMac()->GetAddress());
169  Ptr<MobilityModel> rcvrMobility = i->first->GetNode()->GetObject<MobilityModel>();
170  Time delay = m_prop->GetDelay(senderMobility, rcvrMobility, txMode);
171  UanPdp pdp = m_prop->GetPdp(senderMobility, rcvrMobility, txMode);
172  double rxPowerDb =
173  txPowerDb - m_prop->GetPathLossDb(senderMobility, rcvrMobility, txMode);
174 
175  NS_LOG_DEBUG("txPowerDb="
176  << txPowerDb << "dB, rxPowerDb=" << rxPowerDb << "dB, distance="
177  << senderMobility->GetDistanceFrom(rcvrMobility) << "m, delay=" << delay);
178 
179  uint32_t dstNodeId = i->first->GetNode()->GetId();
180  Ptr<Packet> copy = packet->Copy();
182  delay,
184  this,
185  j,
186  copy,
187  rxPowerDb,
188  txMode,
189  pdp);
190  }
191  j++;
192  }
193 }
194 
195 void
197 {
198  NS_ASSERT(noise);
199  m_noise = noise;
200 }
201 
202 void
203 UanChannel::SendUp(uint32_t i, Ptr<Packet> packet, double rxPowerDb, UanTxMode txMode, UanPdp pdp)
204 {
205  NS_LOG_DEBUG("Channel: In sendup");
206  m_devList[i].second->Receive(packet, rxPowerDb, txMode, pdp);
207 }
208 
209 double
211 {
213  double noise = m_noise->GetNoiseDbHz(fKhz);
214  return noise;
215 }
216 
217 } // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
Keep track of the current position and velocity of an object.
double GetDistanceFrom(Ptr< const MobilityModel > position) const
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Channel class used by UAN devices.
Definition: uan-channel.h:47
void SetNoiseModel(Ptr< UanNoiseModel > noise)
Set the noise model this channel will use to determine ambient channel noise.
Definition: uan-channel.cc:196
double GetNoiseDbHz(double fKhz)
Get the noise level on the channel.
Definition: uan-channel.cc:210
void AddDevice(Ptr< UanNetDevice > dev, Ptr< UanTransducer > trans)
Adds device to receiver list for this channel.
Definition: uan-channel.cc:141
UanChannel()
Constructor.
Definition: uan-channel.cc:68
Ptr< UanPropModel > m_prop
The propagation model.
Definition: uan-channel.h:119
static TypeId GetTypeId()
Register this type.
Definition: uan-channel.cc:48
Ptr< UanNoiseModel > m_noise
The noise model.
Definition: uan-channel.h:120
~UanChannel() override
Dummy destructor, see DoDispose.
Definition: uan-channel.cc:75
void DoDispose() override
Destructor implementation.
Definition: uan-channel.cc:115
virtual void TxPacket(Ptr< UanTransducer > src, Ptr< Packet > packet, double txPowerDb, UanTxMode txmode)
Send a packet out on the channel.
Definition: uan-channel.cc:148
std::size_t GetNDevices() const override
Definition: uan-channel.cc:129
bool m_cleared
Has Clear ever been called on the channel.
Definition: uan-channel.h:122
Ptr< NetDevice > GetDevice(std::size_t i) const override
Definition: uan-channel.cc:135
UanDeviceList m_devList
The list of devices on this channel.
Definition: uan-channel.h:118
void SendUp(uint32_t i, Ptr< Packet > packet, double rxPowerDb, UanTxMode txMode, UanPdp pdp)
Send a packet up to the receiving UanTransducer.
Definition: uan-channel.cc:203
void Clear()
Clear all pointer references.
Definition: uan-channel.cc:80
void SetPropagationModel(Ptr< UanPropModel > prop)
Set the propagation model this channel will use for path loss/propagation delay.
Definition: uan-channel.cc:122
The power delay profile returned by propagation models.
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:43
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227