1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * uan-helper.cc
4  *
5  *  Copyright (c) 2008 University of Washington
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation;
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  Created on: 9-Oct-2008
21  *  Author: Leonard Tracy <lentracy@u.washington.edu>
22  *
23  *
24  */
25 
26 #include "uan-helper.h"
27 #include "ns3/uan-net-device.h"
28 #include "ns3/uan-mac.h"
29 #include "ns3/uan-phy.h"
30 #include "ns3/uan-channel.h"
31 #include "ns3/uan-prop-model.h"
32 #include "ns3/uan-prop-model-ideal.h"
33 #include "ns3/uan-transducer.h"
34 #include "ns3/mobility-model.h"
35 #include "ns3/log.h"
36 #include "ns3/uan-tx-mode.h"
37 #include "ns3/config.h"
38 #include "ns3/simulator.h"
39 #include "ns3/uan-noise-model-default.h"
40 
41 #include <sstream>
42 #include <string>
43 
44 namespace ns3 {
45 
46 NS_LOG_COMPONENT_DEFINE ("UanHelper");
47 
48 /**
49  * Ascii trace callback on Phy transmit events.
50  *
51  * \param os The output stream.
52  * \param context The node and device ids.
53  * \param packet The transmitted packet.
54  * \param txPowerDb The transmission power.
55  * \param mode The transmission mode.
56  */
AsciiPhyTxEvent(std::ostream * os,std::string context,Ptr<const Packet> packet,double txPowerDb,UanTxMode mode)57 static void AsciiPhyTxEvent (std::ostream *os, std::string context,
58                              Ptr<const Packet> packet, double txPowerDb, UanTxMode mode)
59 {
60   NS_UNUSED (txPowerDb);
61   *os << "+ " << Simulator::Now ().GetSeconds () << " " << context << " " << *packet << std::endl;
62 }
63 
64 /**
65  * Ascii trace callback on successful packet reception.
66  *
67  * \param os The output stream.
68  * \param context The node and device ids.
69  * \param packet The received packet.
70  * \param snr The received signal to noise ratio.
71  * \param mode The channel transmission mode.
72  */
AsciiPhyRxOkEvent(std::ostream * os,std::string context,Ptr<const Packet> packet,double snr,UanTxMode mode)73 static void AsciiPhyRxOkEvent (std::ostream *os, std::string context,
74                                Ptr<const Packet> packet, double snr, UanTxMode mode)
75 {
76   NS_UNUSED (snr);
77   *os << "r " << Simulator::Now ().GetSeconds () << " " << context << " " << *packet << std::endl;
78 }
79 
UanHelper()80 UanHelper::UanHelper ()
81 {
82   m_mac.SetTypeId ("ns3::UanMacAloha");
83   m_phy.SetTypeId ("ns3::UanPhyGen");
84   m_transducer.SetTypeId ("ns3::UanTransducerHd");
85 
86 }
87 
~UanHelper()88 UanHelper::~UanHelper ()
89 {
90 
91 }
92 
93 
94 void
SetMac(std::string macType,std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)95 UanHelper::SetMac (std::string macType,
96                    std::string n0, const AttributeValue &v0,
97                    std::string n1, const AttributeValue &v1,
98                    std::string n2, const AttributeValue &v2,
99                    std::string n3, const AttributeValue &v3,
100                    std::string n4, const AttributeValue &v4,
101                    std::string n5, const AttributeValue &v5,
102                    std::string n6, const AttributeValue &v6,
103                    std::string n7, const AttributeValue &v7)
104 {
105   m_mac = ObjectFactory ();
106   m_mac.SetTypeId (macType);
107   m_mac.Set (n0, v0);
108   m_mac.Set (n1, v1);
109   m_mac.Set (n2, v2);
110   m_mac.Set (n3, v3);
111   m_mac.Set (n4, v4);
112   m_mac.Set (n5, v5);
113   m_mac.Set (n6, v6);
114   m_mac.Set (n7, v7);
115 }
116 
117 void
SetPhy(std::string phyType,std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)118 UanHelper::SetPhy (std::string phyType,
119                    std::string n0, const AttributeValue &v0,
120                    std::string n1, const AttributeValue &v1,
121                    std::string n2, const AttributeValue &v2,
122                    std::string n3, const AttributeValue &v3,
123                    std::string n4, const AttributeValue &v4,
124                    std::string n5, const AttributeValue &v5,
125                    std::string n6, const AttributeValue &v6,
126                    std::string n7, const AttributeValue &v7)
127 {
128   m_phy = ObjectFactory ();
129   m_phy.SetTypeId (phyType);
130   m_phy.Set (n0, v0);
131   m_phy.Set (n1, v1);
132   m_phy.Set (n2, v2);
133   m_phy.Set (n3, v3);
134   m_phy.Set (n4, v4);
135   m_phy.Set (n5, v5);
136   m_phy.Set (n6, v6);
137   m_phy.Set (n7, v7);
138 
139 }
140 
141 void
SetTransducer(std::string type,std::string n0,const AttributeValue & v0,std::string n1,const AttributeValue & v1,std::string n2,const AttributeValue & v2,std::string n3,const AttributeValue & v3,std::string n4,const AttributeValue & v4,std::string n5,const AttributeValue & v5,std::string n6,const AttributeValue & v6,std::string n7,const AttributeValue & v7)142 UanHelper::SetTransducer (std::string type,
143                           std::string n0, const AttributeValue &v0,
144                           std::string n1, const AttributeValue &v1,
145                           std::string n2, const AttributeValue &v2,
146                           std::string n3, const AttributeValue &v3,
147                           std::string n4, const AttributeValue &v4,
148                           std::string n5, const AttributeValue &v5,
149                           std::string n6, const AttributeValue &v6,
150                           std::string n7, const AttributeValue &v7)
151 {
152   m_transducer = ObjectFactory ();
153   m_transducer.SetTypeId (type);
154   m_transducer.Set (n0, v0);
155   m_transducer.Set (n1, v1);
156   m_transducer.Set (n2, v2);
157   m_transducer.Set (n3, v3);
158   m_transducer.Set (n4, v4);
159   m_transducer.Set (n5, v5);
160   m_transducer.Set (n6, v6);
161   m_transducer.Set (n7, v7);
162 
163 }
164 
165 void
EnableAscii(std::ostream & os,uint32_t nodeid,uint32_t deviceid)166 UanHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
167 {
168   Packet::EnablePrinting ();
169   std::ostringstream oss;
170 
171   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::UanNetDevice/Phy/RxOk";
172   Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyRxOkEvent, &os));
173 
174   oss.str ("");
175 
176   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::UanNetDevice/Phy/Tx";
177   Config::Connect (oss.str (), MakeBoundCallback (&AsciiPhyTxEvent, &os));
178 }
179 
180 void
EnableAscii(std::ostream & os,NetDeviceContainer d)181 UanHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
182 {
183   for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
184     {
185       Ptr<NetDevice> dev = *i;
186       EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
187     }
188 }
189 
190 void
EnableAscii(std::ostream & os,NodeContainer n)191 UanHelper::EnableAscii (std::ostream &os, NodeContainer n)
192 {
193   NetDeviceContainer devs;
194   for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
195     {
196       Ptr<Node> node = *i;
197       for (uint32_t j = 0; j < node->GetNDevices (); ++j)
198         {
199           devs.Add (node->GetDevice (j));
200         }
201     }
202   EnableAscii (os, devs);
203 }
204 
205 void
EnableAsciiAll(std::ostream & os)206 UanHelper::EnableAsciiAll (std::ostream &os)
207 {
208   EnableAscii (os, NodeContainer::GetGlobal ());
209 }
210 
211 NetDeviceContainer
Install(NodeContainer c) const212 UanHelper::Install (NodeContainer c) const
213 {
214   Ptr<UanChannel> channel = CreateObject<UanChannel> ();
215   Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
216   channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
217   channel->SetNoiseModel (noise);
218 
219   return Install (c, channel);
220 }
221 
222 NetDeviceContainer
Install(NodeContainer c,Ptr<UanChannel> channel) const223 UanHelper::Install (NodeContainer c, Ptr<UanChannel> channel) const
224 {
225   NetDeviceContainer devices;
226   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
227     {
228       Ptr<Node> node = *i;
229 
230       Ptr<UanNetDevice> device = Install (node, channel);
231 
232       devices.Add (device);
233       NS_LOG_DEBUG ("node=" << node << ", mob=" << node->GetObject<MobilityModel> ());
234     }
235   return devices;
236 }
237 
238 Ptr<UanNetDevice>
Install(Ptr<Node> node,Ptr<UanChannel> channel) const239 UanHelper::Install (Ptr<Node> node, Ptr<UanChannel> channel) const
240 {
241   Ptr<UanNetDevice> device = CreateObject<UanNetDevice> ();
242 
243   Ptr<UanMac> mac = m_mac.Create<UanMac> ();
244   Ptr<UanPhy> phy = m_phy.Create<UanPhy> ();
245   Ptr<UanTransducer> trans = m_transducer.Create<UanTransducer> ();
246 
247   mac->SetAddress (Mac8Address::Allocate ());
248   device->SetMac (mac);
249   device->SetPhy (phy);
250   device->SetTransducer (trans);
251   device->SetChannel (channel);
252 
253   node->AddDevice (device);
254 
255   return device;
256 }
257 
258 int64_t
AssignStreams(NetDeviceContainer c,int64_t stream)259 UanHelper::AssignStreams (NetDeviceContainer c, int64_t stream)
260 {
261   int64_t currentStream = stream;
262   Ptr<NetDevice> netDevice;
263   for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
264     {
265       netDevice = (*i);
266       Ptr<UanNetDevice> uan = DynamicCast<UanNetDevice> (netDevice);
267       if (uan)
268         {
269           currentStream += uan->GetPhy ()->AssignStreams (currentStream);
270           currentStream += uan->GetMac ()->AssignStreams (currentStream);
271         }
272     }
273   return (currentStream - stream);
274 }
275 
276 } // end namespace ns3
277