DeciderUWBIRED.h

00001 /*
00002  * DeciderUWBIRED.h
00003  * Author: Jerome Rousselot <jerome.rousselot@csem.ch>
00004  * Copyright: (C) 2008-2010 Centre Suisse d'Electronique et Microtechnique (CSEM) SA
00005  *              Wireless Embedded Systems
00006  *              Jaquet-Droz 1, CH-2002 Neuchatel, Switzerland.
00007  */
00008 
00009 #ifndef _UWBIRENERGYDETECTIONDECIDERV2_H
00010 #define _UWBIRENERGYDETECTIONDECIDERV2_H
00011 
00012 #include <vector>
00013 #include <map>
00014 #include <math.h>
00015 
00016 #include "Signal_.h"
00017 #include "Mapping.h"
00018 #include "AirFrame_m.h"
00019 #include "Decider.h"
00020 #include "DeciderResultUWBIR.h"
00021 #include "AlohaMacLayer.h"
00022 #include "IEEE802154A.h"
00023 #include "UWBIRPacket.h"
00024 #include "BaseUtility.h"
00025 #include "MacToPhyInterface.h"
00026 
00027 using namespace std;
00028 
00029 class PhyLayerUWBIR;
00030 
00066 class DeciderUWBIRED: public Decider {
00067 private:
00068   bool trace, stats;
00069   long nbRandomBits;
00070   long nbFailedSyncs, nbSuccessfulSyncs;
00071   double nbSymbols, allThresholds;
00072   double vsignal2, vnoise2, snirs, snirEvals, pulseSnrs;
00073   double packetSNIR, packetSignal, packetNoise, packetSamples;
00074   IEEE802154A::config cfg;
00075   double snrLastPacket; 
00076 protected:
00077   double syncThreshold;
00078   bool syncAlwaysSucceeds;
00079   bool channelSensing;
00080   bool synced;
00081   bool alwaysFailOnDataInterference;
00082   UWBIRPacket packet;
00083   int catUWBIRPacket;
00084   BaseUtility* utility;
00085   double epulseAggregate, enoiseAggregate;
00086   map<Signal*, int> currentSignals;
00087   cOutVector receivedPulses;
00088   cOutVector syncThresholds;
00089   PhyLayerUWBIR* uwbiface;
00090   Signal* tracking;
00091   int nbFramesWithInterference, nbFramesWithoutInterference;
00092   int nbCancelReceptions;
00093   int nbFinishTrackingFrames;
00094   int nbFinishNoiseFrames;
00095 
00096   enum {
00097     FIRST, HEADER_OVER, SIGNAL_OVER
00098   };
00099   vector<ConstMapping*> receivingPowers;
00100   ConstMapping* signalPower; // = signal->getReceivingPower();
00101   // store relative offsets between signals starts
00102   vector<simtime_t> offsets;
00103   AirFrameVector airFrameVector;
00104   // Create an iterator for each potentially colliding airframe
00105   AirFrameVector::iterator airFrameIter;
00106 
00107   typedef ConcatConstMapping<std::multiplies<double> > MultipliedMapping;
00108 
00109 public:
00110   const static double noiseVariance = 101.085E-12; // P=-116.9 dBW // 404.34E-12;   v²=s²=4kb T R B (T=293 K)
00111   const static double peakPulsePower = 1.3E-3; //1.3E-3 W peak power of pulse to reach  0dBm during burst; // peak instantaneous power of the transmitted pulse (A=0.6V) : 7E-3 W. But peak limit is 0 dBm
00112 
00113   DeciderUWBIRED(DeciderToPhyInterface* iface,
00114       PhyLayerUWBIR* _uwbiface,
00115       double _syncThreshold, bool _syncAlwaysSucceeds, bool _stats,
00116       bool _trace, bool alwaysFailOnDataInterference=false) :
00117     Decider(iface), trace(_trace),
00118         stats(_stats), nbRandomBits(0), nbFailedSyncs(0),
00119         nbSuccessfulSyncs(0), nbSymbols(0), allThresholds(0), vsignal2(0), vnoise2(0), snirEvals(0), pulseSnrs(0), syncThreshold(_syncThreshold),
00120         syncAlwaysSucceeds(_syncAlwaysSucceeds),
00121         channelSensing(false), synced(false), alwaysFailOnDataInterference(alwaysFailOnDataInterference),
00122         uwbiface(_uwbiface), tracking(0), nbFramesWithInterference(0), nbFramesWithoutInterference(0),
00123         nbCancelReceptions(0), nbFinishTrackingFrames(0), nbFinishNoiseFrames(0){
00124 
00125     receivedPulses.setName("receivedPulses");
00126     syncThresholds.setName("syncThresholds");
00127     utility = iface->getUtility();
00128     catUWBIRPacket = utility->getCategory(&packet);
00129   };
00130 
00131   virtual simtime_t processSignal(AirFrame* frame);
00132 
00133   double getAvgThreshold() {
00134     if (nbSymbols > 0)
00135       return allThresholds / nbSymbols;
00136     else
00137       return 0;
00138   };
00139 
00140   double getNoiseValue() {
00141      return normal(0, sqrt(noiseVariance));
00142   }
00143 
00144   void cancelReception();
00145 
00146   void finish();
00147 
00150   enum UWBIRED_CTRL_KIND {
00151       SYNC_SUCCESS=MacToPhyInterface::LAST_BASE_PHY_KIND+1,
00152       SYNC_FAILURE,
00153       // add other control messages kinds here (from decider to mac, e.g. CCA)
00154   };
00155 
00156   // compatibility function to allow running MAC layers that depend on channel state information
00157   // from PHY layer. Returns last SNR
00158   virtual ChannelState getChannelState();
00159 
00160 protected:
00161   bool decodePacket(Signal* signal, vector<bool> * receivedBits);
00162   simtime_t handleNewSignal(Signal* s);
00163   simtime_t handleHeaderOver(map<Signal*, int>::iterator& it);
00164   virtual bool attemptSync(Signal* signal);
00165   simtime_t
00166       handleSignalOver(map<Signal*, int>::iterator& it, AirFrame* frame);
00167   // first value is energy from signal, other value is total window energy
00168   pair<double, double> integrateWindow(int symbol, simtime_t now,
00169       simtime_t burst, Signal* signal);
00170 
00171   simtime_t handleChannelSenseRequest(ChannelSenseRequest* request);
00172 
00173 };
00174 
00175 #endif  /* _UWBIRENERGYDETECTIONDECIDERV2_H */
00176