44#include "clmsg-stats.h"
45#include <phymac-clmsg.h>
53#define DEBUG(level,text) {if (debug_ >= level) {std::cout << NOW << " UwHMMPhysical error: "<< text << std::endl;}}
63 double sinr,
double ber,
double per,
bool error,
MCLink::ChState channel_state)
73 : TclClass(
"Module/UW/HMMPHYSICAL")
77 create(
int argc,
const char *
const * argv)
102 Tcl &tcl = Tcl::instance();
105 if (strcasecmp(argv[1],
"getPktsTotBad") == 0) {
109 if (strcasecmp(argv[1],
"getPktsTotMedium") == 0) {
113 if (strcasecmp(argv[1],
"getPktsTotGood") == 0) {
118 if (strcasecmp(argv[1],
"setMCLink") == 0) {
121 auto link =
dynamic_cast<MCLink *
>(TclObject::lookup(argv[3]));
126 std::cerr <<
"TCL: failed cast on MCLink" << std::endl;
129 std::cerr <<
"TCL error: setMCLink mac MCLink" << std::endl;
144 hdr_cmn *ch = HDR_CMN(p);
145 hdr_MPhy *ph = HDR_MPHY(p);
146 hdr_mac *mach = HDR_MAC(p);
147 const int mac_addr = mach->macSA();
153 auto link =
link_map.find(mac_addr);
155 auto ch_state = (*link->second).updateChState();
156 bool error_hmm =
false;
157 double ber_hmm = (*link->second).getBER();
158 double per_hmm =
ber2per(ber_hmm,p);
159 double chance_hmm = RNG::defaultrng()->uniform_double();
160 if (per_hmm > chance_hmm) {
169 int nbits = ch->size() * 8;
170 double interf_power = 0.0;
171 double perr_interf = 0.0;
172 double sinr = ph->Pr / ph->Pn;
173 bool error_interf =
false;
174 double chance_interf = RNG::defaultrng()->uniform_double();
178 const PowerChunkList &power_chunk_list =
180 for (PowerChunkList::const_iterator itInterf =
181 power_chunk_list.begin();
182 itInterf != power_chunk_list.end();
184 int nbits2 = itInterf->second * BitRate_;
185 interf_power = itInterf->first;
186 if (interf_power > 0.0) {
188 ph->Pr / interf_power, nbits2, p);
189 chance_interf = RNG::defaultrng()->uniform_double();
190 error_interf = chance_interf < perr_interf;
198 sinr = ph->Pr / (ph->Pn + interf_power);
199 if (interf_power > 0.0) {
201 ph->Pr / interf_power, nbits, p);
202 error_interf = chance_interf < perr_interf;
205 std::cerr <<
"Please choose the interference model "
213 interf_power = ph->Pi;
214 sinr = ph->Pr / (ph->Pn + ph->Pi);
215 if (interf_power > 0.0) {
216 perr_interf =
getPER(ph->Pr / ph->Pi, nbits, p);
217 error_interf = chance_interf < perr_interf;
224 uwphystats->
updateStats(getId(),getStackId(), ph->Pr,
225 ph->Pn, interf_power, sinr, ber_hmm, per_hmm,
226 (error_hmm || error_interf),ch_state);
234 Scheduler::instance().clock();
239 Scheduler::instance().clock() + ph->duration;
242 ch->error() = (error_hmm || error_interf);
246 if (mach->ftype() != MF_CONTROL) {
247 incrErrorPktsInterf();
249 if (interferent_pkts.second >= 1) {
252 if (interferent_pkts.first > 0) {
256 }
else if (mach->ftype() == MF_CONTROL) {
259 if (interferent_pkts.first > 0) {
264 incrErrorPktsNoise();
266 }
else if (error_hmm) {
267 incrErrorPktsNoise();
268 if (mach->ftype() != MF_CONTROL) {
270 }
else if (mach->ftype() == MF_CONTROL) {
278 DEBUG(-100,
"McLink not set for origin MAC: "<< mac_addr)
291 if (HDR_CMN(p)->size() > 0 && ber >= 0 && ber <=1) {
292 return 1-pow(1-ber, 8*(HDR_CMN(p)->size()));
294 DEBUG(0,
"called ber2per() with invalid packet size or BER")
MCLink class models the BER of a directed link between two nodes.
UnderwaterHMMPhysical models an hidden Markov Model phy channel.
virtual double ber2per(double ber, Packet *p)
Returns the packet error rate by using the BER from HMM and the size of a packet.
virtual void endRx(Packet *p) override
Handles the end of a packet reception.
int getPktsTotMedium() const
void incrTotPkts(MCLink::ChState ch_state)
increase the counter of packets sent taking into account the channel state
void setMCLink(int mac, MCLink *link)
Adds the Markov Chain transition matrix for each source MAC.
virtual ~UnderwaterHMMPhysical()
Destructor of UnderwaterHMMPhysical class.
int getPktsTotGood() const
int getPktsTotBad() const
virtual int command(int, const char *const *)
TCL command interpreter.
UnderwaterHMMPhysical()
Constructor of UnderwaterHMMPhysical class.
std::map< int, MCLink * > link_map
maps source mac to associated MCLink
virtual void endRx(Packet *p)
Handles the end of a packet reception.
void incrTot_pkts_lost()
Increment the number of packets discarded.
std::string Interference_Model
Interference calcuation mode chosen: CHUNK model or MEANPOWER model.
virtual double consumedEnergyRx(const double &_duration)
Compute the energy (in Joule) spent by the modem in reception.
uwinterference * interference_
Pointer to the interference model module.
void incrErrorCtrlPktsInterf()
Increment the number of CTRL packets discarded due to interference.
virtual double getPER(double snr, int nbits, Packet *)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
double Energy_Rx_
Energy (in Joule) spent by the node in transmission.
void incrCollisionCTRL()
Increment the number of CTRL pkts discarded due to a collision.
void incrCollisionDATAvsCTRL()
Increment the number of collisions DATA/CTRL.
double time_ready_to_end_rx_
Used to keep track of the arrival time.
double Rx_Time_
Time (in seconds) spent by the node in reception.
virtual int command(int, const char *const *)
TCL command interpreter.
void incrCollisionDATA()
Increment the number of DATA pkts discarded due to a collision.
void incrTotCrtl_pkts_lost()
Increment the number of CTRL packets discarded.
TclObject * create(int argc, const char *const *argv)
virtual Stats * clone() const
Virtual method used by the Module class in order to copy its stats an a generic fashion,...
MCLink::ChState channel_state
HMM channel state.
virtual void updateStats(int mod_id, int stck_id, double rx_pwr, double noise_pwr, double interf_pwr, double sinr, double ber, double per, bool error, MCLink::ChState channel_state=MCLink::ChState::NOT_DEFINED)
Method to update stats with the param of last received packet.
virtual void updateStats(int mod_id, int stck_id, double rx_pwr, double noise_pwr, double interf_pwr, double sinr, double ber, double per, bool error)
Method to update stats with the param of last received packet.
virtual counter getCounters(Packet *p)
Returns the counters of collisions.
virtual double getInterferencePower(Packet *p)
Compute the average interference power for the given packet.
Definition of MCLink class.
UwHMMPhysicalClass class_module_uwhmmphysical
Definition of UwHMMPhysical class.
std::pair< int, int > counter
counter of collisions
Definition of ClMsgUwMmac class.
Utilities to manage stats.
#define DEBUG(level, text)