UWBIRMac.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        UWBIRMac.cc
00003  *
00004  * author:      Jerome Rousselot <jerome.rousselot@csem.ch>
00005  *
00006  * copyright:   (C) 2008-2009 Centre Suisse d'Electronique et Microtechnique (CSEM) SA
00007  *        Systems Engineering
00008  *              Real-Time Software and Networking
00009  *              Jaquet-Droz 1, CH-2002 Neuchatel, Switzerland.
00010  *
00011  *              This program is free software; you can redistribute it
00012  *              and/or modify it under the terms of the GNU General Public
00013  *              License as published by the Free Software Foundation; either
00014  *              version 2 of the License, or (at your option) any later
00015  *              version.
00016  *              For further information see file COPYING
00017  *              in the top level directory
00018  * description: All MAC designed for use with UWB-IR should derive from this
00019  *        class. It provides the necessary functions to build UWBIR
00020  *        packets and to receive them.
00021  ***************************************************************************/
00022 //
00023 // See the following publications for more information:
00024 // [1] An Ultra Wideband Impulse Radio PHY Layer Model for Network Simulation,
00025 // J. Rousselot, J.-D. Decotignie, Simulation: Transactions of the Society
00026 // for Computer Simulation, 2010 (submitted).
00027 // [2] A High-Precision Ultra Wideband Impulse Radio Physical Layer Model
00028 // for Network Simulation, Jérôme Rousselot, Jean-Dominique Decotignie,
00029 // Second International Omnet++ Workshop,Simu'TOOLS, Rome, 6 Mar 09.
00030 // http://portal.acm.org/citation.cfm?id=1537714
00031 //
00032 
00033 #include "UWBIRMac.h"
00034 #include <iostream>
00035 #include <math.h>
00036 #include "MacToUWBIRPhyControlInfo.h"
00037 
00038 using namespace std;
00039 
00040 Define_Module(UWBIRMac);
00041 
00042 void UWBIRMac::initialize(int stage) {
00043   BaseMacLayer::initialize(stage);
00044   if (stage == 0) {
00045     debug = par("debug").boolValue();
00046     stats = par("stats").boolValue();
00047     trace = par("trace").boolValue();
00048     prf = par("PRF");
00049     assert(prf == 4 || prf == 16);
00050     packetsAlwaysValid = par("packetsAlwaysValid");
00051     rsDecoder = par("RSDecoder").boolValue();
00052     phy = FindModule<MacToPhyInterface*>::findSubModule(
00053         this->getParentModule());
00054     initCounters();
00055     catPacket = utility->getCategory(&packet);
00056   }
00057 }
00058 
00059 void UWBIRMac::initCounters() {
00060   totalRxBits = 0;
00061   errRxBits = 0;
00062   nbReceivedPacketsRS = 0;
00063   nbReceivedPacketsNoRS = 0;
00064   nbSentPackets = 0;
00065   nbSymbolErrors = 0;
00066   nbSymbolsReceived = 0;
00067   nbHandledRxPackets = 0;
00068   nbFramesDropped = 0;
00069   packetsBER.setName("packetsBER");
00070   meanPacketBER.setName("meanPacketBER");
00071   dataLengths.setName("dataLengths");
00072   sentPulses.setName("sentPulses");
00073   receivedPulses.setName("receivedPulses");
00074   erroneousSymbols.setName("nbErroneousSymbols");
00075   packetSuccessRate.setName("packetSuccessRate");
00076   packetSuccessRateNoRS.setName("packetSuccessRateNoRS");
00077   ber.setName("ber");
00078   RSErrorRate.setName("ser");
00079   success.setName("success");
00080   successNoRS.setName("successNoRS");
00081 }
00082 
00083 void UWBIRMac::finish() {
00084   if (stats) {
00085     recordScalar("Erroneous bits", errRxBits);
00086     recordScalar("nbSymbolErrors", nbSymbolErrors);
00087     recordScalar("Total received bits", totalRxBits);
00088     recordScalar("Average BER", errRxBits / totalRxBits);
00089     recordScalar("nbReceivedPacketsRS", nbReceivedPacketsRS);
00090     recordScalar("nbFramesDropped", nbFramesDropped);
00091     recordScalar("nbReceivedPacketsnoRS", nbReceivedPacketsNoRS);
00092     if (rsDecoder) {
00093       recordScalar("nbReceivedPackets", nbReceivedPacketsRS);
00094     } else {
00095       recordScalar("nbReceivedPackets", nbReceivedPacketsNoRS);
00096     }
00097     recordScalar("nbSentPackets", nbSentPackets);
00098   }
00099 }
00100 
00101 void UWBIRMac::prepareData(UWBIRMacPkt* packet, IEEE802154A::config cfg) {
00102   // generate signal
00103   //int nbSymbols = packet->getByteLength() * 8 + 92; // to move to ieee802154a.h
00104   debugEV << "prepare Data for a packet with " << packet->getByteLength() << " data bytes." << endl;
00105   if(prf == 4) {
00106     IEEE802154A::setConfig(IEEE802154A::cfg_mandatory_4M);
00107   } else if (prf == 16) {
00108     IEEE802154A::setConfig(IEEE802154A::cfg_mandatory_16M);
00109   }
00110   IEEE802154A::setPSDULength(packet->getByteLength());
00111   IEEE802154A::signalAndData res = IEEE802154A::generateIEEE802154AUWBSignal(simTime());
00112   Signal* theSignal = res.first;
00113   vector<bool>* data = res.second;
00114   int nbSymbols = data->size();
00115   if (trace) {
00116     int nbItems = 0;
00117     ConstMapping* power = theSignal->getTransmissionPower();
00118     ConstMappingIterator* iter = power->createConstIterator();
00119     iter->jumpToBegin();
00120     while (iter->hasNext()) {
00121       nbItems++;
00122       sentPulses.recordWithTimestamp(simTime()
00123           + iter->getPosition().getTime(), iter->getValue());
00124       iter->next();
00125       simtime_t t = simTime() + iter->getPosition().getTime();
00126       //debugEV << "nbItemsTx=" << nbItems << ", t= " << t <<  ", value=" << iter->getValue() << "." << endl;
00127     }
00128   }
00129 
00130   // save bit values
00131   //packet->setBitValuesArraySize(data->size());
00132   for (int pos = 0; pos < nbSymbols; pos = pos + 1) {
00133     packet->pushBitvalue(data->at(pos));
00134     //packet->setBitValues(pos, data->at(pos));
00135   }
00136   delete data;
00137 
00138   packet->setNbSymbols(nbSymbols);
00139 
00140   // attach control info
00141   MacToUWBIRPhyControlInfo* macPhycInfo = new MacToUWBIRPhyControlInfo(theSignal, IEEE802154A::getConfig());
00142   packet->setControlInfo(macPhycInfo);
00143 
00144 }
00145 
00146 bool UWBIRMac::validatePacket(UWBIRMacPkt *mac) {
00147   nbHandledRxPackets++;
00148   if (!packetsAlwaysValid) {
00149     PhyToMacControlInfo * phyToMac = dynamic_cast<PhyToMacControlInfo*> (mac->getControlInfo());
00150     DeciderResultUWBIR * res = dynamic_cast<DeciderResultUWBIR*>(phyToMac->getDeciderResult());
00151     const std::vector<bool> * decodedBits = res->getDecodedBits();
00152     int bitsToDecode = mac->getNbSymbols();
00153     nbSymbolsReceived = nbSymbolsReceived + ceil(bitsToDecode / IEEE802154A::RSSymbolLength);
00154     int nbBitErrors = 0;
00155     int pktSymbolErrors = 0;
00156     bool currSymbolError = false;
00157 
00158     for (int i = 0; i < bitsToDecode; i++) {
00159       // Start of a new symbol
00160       if (i % IEEE802154A::RSSymbolLength == 0) {
00161         currSymbolError = false;
00162       }
00163       // bit error
00164       if (decodedBits->at(i) != mac->popBitValue()) {
00165         nbBitErrors++;
00166         debugEV<< "Found an error at position " << i << "." << endl;
00167         // symbol error
00168         if(!currSymbolError) {
00169           currSymbolError = true;
00170           pktSymbolErrors = pktSymbolErrors + 1;
00171         }
00172       }
00173     }
00174 
00175     debugEV << "Found " << nbBitErrors << " bit errors in MAC packet." << endl;
00176     double packetBER = static_cast<double>(nbBitErrors)/static_cast<double>(bitsToDecode);
00177     packetsBER.record(packetBER);
00178     meanBER.collect(packetBER);
00179     meanPacketBER.record(meanBER.getMean());
00180     if(trace) {
00181       erroneousSymbols.record(pktSymbolErrors);
00182     }
00183 
00184     // ! If this condition is true then the next one will also be true
00185     if(nbBitErrors == 0) {
00186       successNoRS.record(1);
00187       nbReceivedPacketsNoRS++;
00188       packet.setNbPacketsReceivedNoRS(packet.getNbPacketsReceivedNoRS()+1);
00189     } else {
00190       successNoRS.record(0);
00191     }
00192 
00193     if (pktSymbolErrors <= IEEE802154A::RSMaxSymbolErrors) {
00194       success.record(1);
00195       nbReceivedPacketsRS++;
00196       packet.setNbPacketsReceived(packet.getNbPacketsReceived()+1);
00197       utility->publishBBItem(catPacket, &packet, -1);
00198     } else {
00199       success.record(0);
00200       nbFramesDropped++;
00201     }
00202 
00203     if(stats) {
00204       totalRxBits += bitsToDecode;
00205       errRxBits += nbBitErrors;
00206       nbSymbolErrors += pktSymbolErrors;
00207       ber.record(errRxBits/totalRxBits);
00208       packetSuccessRate.record( ((double) nbReceivedPacketsRS)/nbHandledRxPackets);
00209       packetSuccessRateNoRS.record( ((double) nbReceivedPacketsNoRS)/nbHandledRxPackets);
00210       RSErrorRate.record( ((double) nbSymbolErrors) / nbSymbolsReceived);
00211     }
00212 
00213     // validate message
00214     bool success = false;
00215 
00216     success = (nbBitErrors == 0 || (rsDecoder && pktSymbolErrors <= IEEE802154A::RSMaxSymbolErrors) );
00217 
00218     return success;
00219   }
00220   return true;
00221 }
00222 
00223 void UWBIRMac::handleLowerMsg(cPacket *msg) {
00224   UWBIRMacPkt *mac = static_cast<UWBIRMacPkt *> (msg);
00225 
00226   if (validatePacket(mac)) {
00227     int dest = mac->getDestAddr();
00228     int src = mac->getSrcAddr();
00229     if ((dest == myMacAddr)) {
00230       debugEV<< "message with mac addr " << src
00231       << " for me (dest=" << dest
00232       << ") -> forward packet to upper layer\n";
00233       sendUp(decapsMsg(mac));
00234     } else {
00235       debugEV << "message with mac addr " << src
00236       << " not for me (dest=" << dest
00237       << ") -> delete (my MAC=" << myMacAddr << ")\n";
00238       delete mac;
00239     }
00240   } else {
00241     debugEV << "Errors in message ; dropping mac packet." << endl;
00242     delete mac;
00243   }
00244 }
00245