DESERT 3.6.1
Loading...
Searching...
No Matches
uwahoi-phy.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwahoi-phy.h"
40#include <fstream>
41#include <sstream>
42#include <phymac-clmsg.h>
43#include "mac.h"
44
48static class UwAhoiPhyClass : public TclClass
49{
50public:
52 : TclClass("Module/UW/AHOI/PHY")
53 {
54 }
55 TclObject *
56 create(int, const char *const *)
57 {
58 return (new UwAhoiPhy);
59 }
61/*
62L{ 25, 50, 95, 120, 140, 160, 180, 190 },
63P_SUCC{ 0.923077*39/40, 0.913793*58/60, 0.924528*53/60, 0.876712*73/80,
64 0.61643*73/80, 0.75*28/40, 0.275862*29/40, 0 }*/
67 , pdr_file_name_("dbs/ahoi/default_pdr.csv")
68 , sir_file_name_("dbs/ahoi/default_sir.csv")
69 , pdr_token_separator_(',')
70 , range2pdr_()
71 , sir2pdr_()
72 , initLUT_(false)
73{ // binding to TCL variables
74 Interference_Model = "MEANPOWER";
75}
76
80
81int
82UwAhoiPhy::command(int argc, const char *const *argv)
83{
84 if (argc == 2) {
85 if (strcasecmp(argv[1], "initLUT") == 0) {
87 return TCL_OK;
88 }
89 } else if (argc == 3) {
90 if (strcasecmp(argv[1], "setRangePDRFileName") == 0) {
91 string tmp_ = ((char *) argv[2]);
92 if (tmp_.size() == 0) {
93 fprintf(stderr, "Empty string for the file name");
94 return TCL_ERROR;
95 }
96 pdr_file_name_ = tmp_;
97 return TCL_OK;
98 } else if (strcasecmp(argv[1], "setSIRFileName") == 0) {
99 string tmp_ = ((char *) argv[2]);
100 if (tmp_.size() == 0) {
101 fprintf(stderr, "Empty string for the file name");
102 return TCL_ERROR;
103 }
104 sir_file_name_ = tmp_;
105 return TCL_OK;
106 } else if (strcasecmp(argv[1], "setLUTSeparator") == 0) {
107 string tmp_ = ((char *) argv[2]);
108 if (tmp_.size() == 0) {
109 fprintf(stderr, "Empty char for the file name");
110 return TCL_ERROR;
111 }
112 pdr_token_separator_ = tmp_.at(0);
113 return TCL_OK;
114 }
115 }
116 return UnderwaterPhysical::command(argc, argv);
117}
118
119void
121{
122 ifstream input_file_;
123 string line_;
124 char *tmp_ = new char[pdr_file_name_.length() + 1];
125 strcpy(tmp_, pdr_file_name_.c_str());
126 input_file_.open(tmp_);
127 if (input_file_.is_open()) {
128 // skip first 2 lines
129 if (debug_)
130 std::cout << "UwAhoiPhy::initializeRangeLUT()" << endl;
131 while (std::getline(input_file_, line_)) {
132 // TODO: retrive the right row and break the loop
133 std::istringstream line_stream(line_);
134 std::string token;
135 std::getline(line_stream, token, pdr_token_separator_);
136 double d = stod(token);
137 std::getline(line_stream, token, pdr_token_separator_);
138 double p = stod(token);
139 range2pdr_[d] = p;
140 if (debug_)
141 std::cout << d << " " << p << endl;
142 }
143 } else {
144 cerr << "Impossible to open file " << pdr_file_name_ << endl;
145 }
146
147 ifstream input_file_2;
148 tmp_ = new char[sir_file_name_.length() + 1];
149 strcpy(tmp_, sir_file_name_.c_str());
150 input_file_2.open(tmp_);
151 if (input_file_2.is_open()) {
152 // skip first 2 lines
153 if (debug_)
154 std::cout << "UwAhoiPhy::initializeSIRLUT()" << endl;
155 while (std::getline(input_file_2, line_)) {
156 // TODO: retrive the right row and break the loop
157 std::istringstream line_stream(line_);
158 std::string token;
159 std::getline(line_stream, token, pdr_token_separator_);
160 double sir = stod(token);
161 std::getline(line_stream, token, pdr_token_separator_);
162 double p = stod(token);
163 sir2pdr_[sir] = p;
164 if (debug_)
165 std::cout << sir << " " << p << endl;
166 }
167 } else {
168 cerr << "Impossible to open file " << sir_file_name_ << endl;
169 }
170
171 initLUT_ = true;
172}
173
174void
176{
177 if (!initLUT_)
178 cerr << "UwAhoiPhy ERROR: FIRST INITIALIZE LUT!" << endl;
179 hdr_cmn *ch = HDR_CMN(p);
180 hdr_MPhy *ph = HDR_MPHY(p);
181 hdr_mac *mach = HDR_MAC(p);
182 counter interferent_pkts;
183 static int mac_addr = -1;
184 ClMsgPhy2MacAddr msg;
185 sendSyncClMsg(&msg);
186 mac_addr = msg.getAddr();
187 if (PktRx != 0) {
188 if (PktRx == p) {
189 double per_ni; // packet error rate due to noise and/or interference
190 double per_n; // packet error rate due to noise only
191 double x = RNG::defaultrng()->uniform_double();
192 per_n = getPER(ph->Pr / ph->Pn, p);
193 bool error_n = x <= per_n;
194 error_n = 0;
195 bool error_ni = 0;
196 double interference = interference_ ?
198 if (!error_n) {
199 per_ni = interference?getPER(ph->Pr/interference,p):getPER(0,p);
200 error_ni = x <= per_ni;
201 }
202 if (time_ready_to_end_rx_ > Scheduler::instance().clock()) {
203 Rx_Time_ = Rx_Time_ + ph->duration - time_ready_to_end_rx_ +
204 Scheduler::instance().clock();
205 } else {
206 Rx_Time_ += ph->duration;
207 }
209 Scheduler::instance().clock() + ph->duration;
210 Energy_Rx_ += consumedEnergyRx(ph->duration);
211
212 ch->error() = error_ni || error_n;
213 if (debug_) {
214 if (error_ni == 1) {
215 std::cout
216 << NOW << " UwAhoiPhy(" << mac_addr
217 << ")::endRx() packet " << ch->uid()
218 << " contains errors due to noise and interference."
219 << std::endl;
220 } else if (error_n == 1) {
221 std::cout << NOW << " UwAhoiPhy(" << mac_addr
222 << ")::endRx() packet " << ch->uid()
223 << " contains errors due to noise." << std::endl;
224 }
225 }
226 if (error_n) {
227 incrErrorPktsNoise();
228 if (mach->ftype() != MF_CONTROL) {
230 } else if (mach->ftype() == MF_CONTROL) {
232 }
233 } else if (error_ni) {
234 if (mach->ftype() != MF_CONTROL) {
235 incrErrorPktsInterf();
237 if (interferent_pkts.second >= 1) {
239 } else {
240 if (interferent_pkts.first > 0) {
242 }
243 }
244 } else if (mach->ftype() == MF_CONTROL) {
247 if (interferent_pkts.first > 0) {
249 }
250 }
251 }
252 sendUp(p);
253 PktRx = 0;
254 } else {
255 dropPacket(p);
256 }
257 } else {
258 dropPacket(p);
259 }
260}
261
262double
263UwAhoiPhy::getPER(double _sir, Packet *_p)
264{
265 double pdr = matchDistancePDR(getDistance(_p));
266
267 if(_sir > 0) {
268 pdr = pdr * matchSIR_PDR(_sir);
269 }
270 return 1 - pdr;
271}
272
273double
275{
276 hdr_MPhy *ph = HDR_MPHY(_p);
277 double x_src = (ph->srcPosition)->getX();
278 double y_src = (ph->srcPosition)->getY();
279 double z_src = (ph->srcPosition)->getZ();
280 double x_dst = (ph->dstPosition)->getX();
281 double y_dst = (ph->dstPosition)->getY();
282 double z_dst = (ph->dstPosition)->getZ();
283 return sqrt(pow(x_src - x_dst, 2.0) + pow(y_src - y_dst, 2.0) +
284 pow(z_src - z_dst, 2.0));
285}
286
287double
289{ // success probability of AHOI
290 if (debug_)
291 std::cout << NOW
292 << " UwAhoiPhy()::matchDistancePDR(double distance)"
293 << "distance = " << distance << std::endl;
294 if (distance <= range2pdr_.begin()->first)
295 return range2pdr_.begin()->second;
296
297 PdrLut::iterator it = range2pdr_.lower_bound(distance);
298 if (it == range2pdr_.end())
299 return (--it)->second;
300 double l_sup = it->first;
301 double p_sup = it->second;
302 it--;
303 double l_inf = it->first;
304 double p_inf = it->second;
305 if (debug_) {
306 std::cout << " Distance between " << l_inf << " and " << l_sup;
307 std::cout << " Succ Prob between " << p_inf << " and " << p_sup;
308 }
309 return linearInterpolator(distance, l_inf, p_inf, l_sup, p_sup);
310}
311
312double
314{ // success probability of AHOI
315 double sir_db = 10*log10(sir/10);
316 if (debug_)
317 std::cout << NOW
318 << " UwAhoiPhy()::matchSIR_PDR(double sir)"
319 << "sir = " << sir << std::endl;
320 if (sir_db <= sir2pdr_.begin()->first)
321 return sir2pdr_.begin()->second;
322
323 PdrLut::iterator it = sir2pdr_.lower_bound(sir_db);
324 if (it == sir2pdr_.end())
325 return (--it)->second;
326 double l_sup = it->first;
327 double p_sup = it->second;
328 it--;
329 double l_inf = it->first;
330 double p_inf = it->second;
331 if (debug_) {
332 std::cout << " SIR between " << l_inf << " and " << l_sup;
333 std::cout << " Succ Prob between " << p_inf << " and " << p_sup;
334 }
335 return linearInterpolator(sir_db, l_inf, p_inf, l_sup, p_sup);
336}
337
338double
340 double x, double x1, double y1, double x2, double y2)
341{
342 double m = (y1 - y2) / (x1 - x2);
343 double q = y1 - m * x1;
344 if (debug_)
345 std::cout << NOW << " UwAhoiPhy::linearInterpolator( double x, "
346 "double x1, double y1, double x2, double y2 )"
347 << "m = " << m << " q= " << q << std::endl;
348 return m * x + q;
349}
350
void incrTot_pkts_lost()
Increment the number of packets discarded.
Definition uwphysical.h:287
std::string Interference_Model
Interference calcuation mode chosen: CHUNK model or MEANPOWER model.
Definition uwphysical.h:441
uwinterference * interference_
Pointer to the interference module.
Definition uwphysical.h:444
virtual int command(int, const char *const *) override
TCL command interpreter.
void incrErrorCtrlPktsInterf()
Increment the number of CTRL packets discarded due to interference.
Definition uwphysical.h:305
double Energy_Rx_
Energy (in Joule) spent by the node in transmission.
Definition uwphysical.h:426
void incrCollisionCTRL()
Increment the number of CTRL pkts discarded due to a collision.
Definition uwphysical.h:323
void incrCollisionDATAvsCTRL()
Increment the number of collisions DATA/CTRL.
Definition uwphysical.h:314
double time_ready_to_end_rx_
Used to keep track of the rx time.
Definition uwphysical.h:420
virtual double consumedEnergyRx(const double &_duration) const
Compute the energy (in Joule) spent by the modem in reception.
Definition uwphysical.h:192
double Rx_Time_
Time (in seconds) spent by the node in reception.
Definition uwphysical.h:423
void incrCollisionDATA()
Increment the number of DATA pkts discarded due to a collision.
Definition uwphysical.h:332
void incrTotCrtl_pkts_lost()
Increment the number of CTRL packets discarded.
Definition uwphysical.h:296
Adds the module for UwCbrModuleClass in ns2.
TclObject * create(int, const char *const *)
virtual int command(int, const char *const *)
TCL command interpreter.
virtual double matchSIR_PDR(double sir)
Return the PER via linear interpolation.
virtual double matchDistancePDR(double distance)
Return the PER via linear interpolation.
virtual void endRx(Packet *p)
Handles the end of a packet reception.
PdrLut sir2pdr_
Definition uwahoi-phy.h:138
virtual double getPER(double snr, Packet *)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
string pdr_file_name_
Definition uwahoi-phy.h:134
string sir_file_name_
Definition uwahoi-phy.h:135
UwAhoiPhy()
Constructor of UwAhoiPhy class.
virtual double linearInterpolator(double x, double x1, double y1, double x2, double y2)
Return y via linear interpolation given two points.
virtual double getDistance(Packet *)
Return the distance between source and destination.
char pdr_token_separator_
Definition uwahoi-phy.h:136
bool initLUT_
Definition uwahoi-phy.h:139
PdrLut range2pdr_
Definition uwahoi-phy.h:137
virtual ~UwAhoiPhy()
Destructor of UwAhoiPhy class.
virtual void initializeLUT()
virtual double getInterferencePower(Packet *p)
Compute the average interference power for the given packet.
UwAhoiPhyClass class_module_UwAhoiPhy
Definition of UwAhoiPhy class.
std::pair< int, int > counter
counter of collisions
const double q