DESERT 3.5.1
Loading...
Searching...
No Matches
uwem-phy.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2023 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 "uwem-phy.h"
40#include "uwem-mpropagation.h"
41#include <float.h>
42
43static class UwElectroMagneticPhyClass : public TclClass
44{
45public:
47 : TclClass("Module/UW/ElectroMagnetic/PHY")
48 {
49 }
50 TclObject *
51 create(int, const char *const *)
52 {
53 return (new UwElectroMagneticPhy);
54 }
56
58 : lut_file_name_("")
59 , lut_token_separator_(',')
60 , rxPowerThreshold_(-200)
61{
62 if (!MPhy_Bpsk::initialized) {
63 MPhy_Bpsk::modid =
64 MPhy::registerModulationType(ELECTROMAGNETIC_MODULATION_TYPE);
65 MPhy_Bpsk::initialized = true;
66 }
67 bind("rxPowerThreshold_", &rxPowerThreshold_);
68 MPhy_Bpsk();
69}
70
71int
72UwElectroMagneticPhy::command(int argc, const char *const *argv)
73{
74 if (argc == 2) {
75 if (strcasecmp(argv[1], "useLUT") == 0) {
77 return TCL_OK;
78 }
79 } else if (argc == 3) {
80 if (strcasecmp(argv[1], "setLUTFileName") == 0) {
81 string tmp_ = ((char *) argv[2]);
82 if (tmp_.size() == 0) {
83 fprintf(stderr, "Empty string for the file name");
84 return TCL_ERROR;
85 }
86 lut_file_name_ = tmp_;
87 return TCL_OK;
88 } else if (strcasecmp(argv[1], "setLUTSeparator") == 0) {
89 string tmp_ = ((char *) argv[2]);
90 if (tmp_.size() == 0) {
91 fprintf(stderr, "Empty char for the file name");
92 return TCL_ERROR;
93 }
94 lut_token_separator_ = tmp_.at(0);
95 return TCL_OK;
96 }
97 }
98 return MPhy_Bpsk::command(argc, argv);
99}
100
101void
103{
104 hdr_MPhy *ph = HDR_MPHY(p);
105 if ((PktRx == 0) && (txPending == false)) {
106 double rx_power = 10 * log10(getRxPower(p));
107 if (rx_power > rxPowerThreshold_){
108 if (ph->modulationType == MPhy_Bpsk::modid) {
109 PktRx = p;
110 Phy2MacStartRx(p);
111 return;
112 } else {
113 if (debug_)
114 std::cout << "UwElectroMagneticPhy::Drop Packet::Wrong modulation"
115 << std::endl;
116 }
117 } else {
118 if (debug_){
119 cout << NOW << " UwElectroMagneticPhy: Drop Packet::Below Threshold : received power = "
120 << rx_power
121 << ", threshold = " << rxPowerThreshold_
122 << endl;
123 }
124 }
125 } else {
126 if (debug_)
127 std::cout << "UwElectroMagneticPhy::Drop Packet::Synced onto another packet "
128 "PktRx = "
129 << PktRx << ", pending = " << txPending << std::endl;
130 }
131}
132
133
134void
136{
137 if(PktRx != 0 ){
138 hdr_cmn *ch = HDR_CMN(p);
139 if (PktRx == p) {
140 if (propagation_) {
141 double rx_power = 10 * log10(getRxPower(p));
142 if (debug_)
143 std::cout << NOW << " UwElectroMagneticPhy: RSSI= " << rx_power << std::endl;
144
145 double PER = getPER(rx_power);
146 if (debug_)
147 std::cout << NOW << " UwElectroMagneticPhy: interpolated PER = " << PER << std::endl;
148
149 // random experiment
150 double x = RNG::defaultrng()->uniform_double();
151
152 if (x > PER) {
153 // no errors
154 ch->error() = 0;
155 } else {
156 // at least one interferent packet
157 ch->error() = 1;
158
159 }
160 } else {
161 // no propagation model set
162 ch->error() = 1;
163 }
164
165 sendUp(p);
166 PktRx = 0;
167 } else {
168 Packet::free(p);
169 }
170 }
171 else{
172 Packet::free(p);
173 }
174
175}
176
177double
179 hdr_MPhy *ph = HDR_MPHY(p);
180
181 if(debug_){
182 std::cout << " UwElectroMagneticPhy: TxAntenna= " << ph->srcAntenna->getGain(p) << std::endl;
183 std::cout << " UwElectroMagneticPhy: RxAntenna= " << ph->dstAntenna->getGain(p) << std::endl;
184 }
185
186 double tot_attenuation =propagation_->getGain(p);
187
188 // rx_power in dB
189 double rx_power = TxPower_ + ph->srcAntenna->getGain(p) + ph->dstAntenna->getGain(p) - tot_attenuation;
190 // linear rx_power
191 rx_power = pow(10,(rx_power/10));
192
193
194 return rx_power;
195}
196
197double
199{
200 // rx_power founded in the LUT
201 RSSIMap::iterator it = lut_map.lower_bound(rx_power);
202
203 if (it != lut_map.end() && it->first == rx_power) {
204 if (debug_)
205 std::cout << rx_power << " " << it->first << " " << it->second
206 << std::endl;
207 return it->second;
208 }
209
210 // rx_power exceeds the LUT
211 if (it == lut_map.end() && it->first != rx_power) {
212 return 0;
213 }
214 if (it == lut_map.begin() && it->first != rx_power) {
215 return 1;
216 }
217
218 // rx_power intermediate => interpolation
219 RSSIMap::iterator u_it = it;
220 it--;
221 return linearInterpolator(rx_power, it->first, it->second, u_it->first, u_it->second)/100;
222}
223
224double
226 double x, double x1, double y1, double x2, double y2)
227{
228 assert(x1 != x2);
229 double m = (y1 - y2) / (x1 - x2);
230 double q = y1 - m * x1;
231 return m * x + q;
232}
233
234void
236{
237 ifstream input_file_;
238 string line_;
239 input_file_.open(lut_file_name_.c_str());
240 if (input_file_.is_open()) {
241 double d;
242 double n;
243 while (std::getline(input_file_, line_)) {
244 ::std::stringstream line_stream(line_);
245 line_stream >> d;
246 line_stream.ignore(256, lut_token_separator_);
247 line_stream >> n;
248 lut_map[d] = n;
249 }
250 input_file_.close();
251 } else {
252 cerr << "Impossible to open file " << lut_file_name_ << endl;
253 }
254}
TclObject * create(int, const char *const *)
Definition uwem-phy.cpp:51
UwElectroMagneticPhy()
Constructor of UwElectroMagneticPhy class.
Definition uwem-phy.cpp:57
virtual double linearInterpolator(double x, double x1, double y1, double x2, double y2)
Definition uwem-phy.cpp:225
virtual void initializeLUT()
Inizialize LUT of c_variable values.
Definition uwem-phy.cpp:235
void startRx(Packet *p)
Definition uwem-phy.cpp:102
RSSIMap lut_map
Lookup table map of the RSSI versus PER.
Definition uwem-phy.h:116
double getPER(double rx_power)
Definition uwem-phy.cpp:198
double getRxPower(Packet *p)
Definition uwem-phy.cpp:178
void endRx(Packet *p)
Definition uwem-phy.cpp:135
virtual int command(int, const char *const *)
TCL command interpreter.
Definition uwem-phy.cpp:72
UwElectroMagneticPhyClass class_UwElectroMagneticPhyClass
Definition of UwEm class.
#define ELECTROMAGNETIC_MODULATION_TYPE
Definition uwem-phy.h:60
const double q