| [3537] | 1 | #ifndef  BRPAQU_H_SEEN
 | 
|---|
 | 2 | #define  BRPAQU_H_SEEN
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 | /* ----------------------------------------
 | 
|---|
 | 5 |      Projet BAORadio --- LAL 
 | 
|---|
 | 6 |      Juin 2008 , M.Taurigna , R. Ansari 
 | 
|---|
 | 7 | -------------------------------------------  */
 | 
|---|
 | 8 | 
 | 
|---|
 | 9 | #include <stdio.h>
 | 
|---|
 | 10 | #include <iostream>
 | 
|---|
| [3671] | 11 | #include <exception>
 | 
|---|
| [3537] | 12 | #include <string>
 | 
|---|
 | 13 | #include "brtypes.h"
 | 
|---|
 | 14 | 
 | 
|---|
| [3592] | 15 | 
 | 
|---|
| [3537] | 16 | using namespace std;
 | 
|---|
 | 17 | 
 | 
|---|
| [3671] | 18 | // ---------------------------------------------------------
 | 
|---|
 | 19 | // ----- Classe d'exception pour Acquisition BAORadio ------
 | 
|---|
 | 20 | #define BREX_MAXMSGLEN 160
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | class BAORadioException : public std::exception {
 | 
|---|
 | 23 |   public:
 | 
|---|
 | 24 |   explicit BAORadioException(const char * m) throw() ; 
 | 
|---|
 | 25 |   explicit BAORadioException(const string& m) throw() ;
 | 
|---|
 | 26 |   virtual ~BAORadioException() throw() ; 
 | 
|---|
 | 27 |   //! Implementation of std::exception what() method, returning the exception message
 | 
|---|
 | 28 |   virtual const char* what() const throw();
 | 
|---|
 | 29 |   virtual string const Msg() const ; 
 | 
|---|
 | 30 |  private:
 | 
|---|
 | 31 |   char msg_[BREX_MAXMSGLEN];
 | 
|---|
 | 32 | };
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | 
 | 
|---|
| [3623] | 36 | // ------------------------------------------------
 | 
|---|
 | 37 | // ----- Classe TwoByteComplex ------
 | 
|---|
| [3592] | 38 | // On definit une classe TwoByteComplex() pour manipuler une paire de byte 
 | 
|---|
 | 39 | // representant partie relle et imaginaire d'un nombre complexe
 | 
|---|
 | 40 | // Remarque Byte = unsigned char 
 | 
|---|
 | 41 | // Remarque SByte = char = signed char 
 | 
|---|
 | 42 | class TwoByteComplex {
 | 
|---|
 | 43 |  public:
 | 
|---|
 | 44 |   explicit TwoByteComplex() { val_[0] = val_[1] = 0; }
 | 
|---|
 | 45 |   //  explicit TwoByteComplex(TwoByteComplex const & a) { val_[0] = a.val_[0]; val_[1] = 0; }
 | 
|---|
 | 46 |   explicit TwoByteComplex(Byte re, Byte im) { val_[0] = re; val_[1] = im; }
 | 
|---|
 | 47 |   explicit TwoByteComplex(SByte re, SByte im) { val_[0] = re; val_[1] = im; }
 | 
|---|
 | 48 |   explicit TwoByteComplex(int re, int im) { val_[0] = re; val_[1] = im; }
 | 
|---|
 | 49 |   explicit TwoByteComplex(float re, float im) { val_[0] = re; val_[1] = im; }
 | 
|---|
 | 50 |   explicit TwoByteComplex(double re, double im) { val_[0] = re; val_[1] = im; }
 | 
|---|
 | 51 |   
 | 
|---|
| [3659] | 52 |   inline SByte& realB() { return  val_[0]; }
 | 
|---|
 | 53 |   inline SByte& imagB() { return  val_[1]; }
 | 
|---|
| [3592] | 54 | 
 | 
|---|
| [3659] | 55 |   inline int realI() { return  (int)(val_)[0]; }
 | 
|---|
 | 56 |   inline int imagI() { return  (int)(val_)[1]; }
 | 
|---|
| [3592] | 57 | 
 | 
|---|
| [3659] | 58 |   inline double realD() { return  (double)(val_)[0]; }
 | 
|---|
| [3671] | 59 |   inline double imagD() { return  (double)(val_)[1]; }
 | 
|---|
| [3592] | 60 | 
 | 
|---|
| [3774] | 61 |   inline float  module2F() { return ((float)(val_)[0]*(float)(val_)[0]+(float)(val_)[1]*(float)(val_)[1]); }
 | 
|---|
 | 62 |   inline double module2D() { return ((double)(val_)[0]*(double)(val_)[0]+(double)(val_)[1]*(double)(val_)[1]); }
 | 
|---|
 | 63 | 
 | 
|---|
| [3659] | 64 |   SByte val_[2];
 | 
|---|
| [3592] | 65 | };
 | 
|---|
 | 66 | 
 | 
|---|
| [3671] | 67 | // --------------------------------------------------------
 | 
|---|
 | 68 | // ----- Classe BRPaquet , constante et enum associe ------
 | 
|---|
 | 69 | //     Representation, manipulation des paquets/frames 
 | 
|---|
 | 70 | //     envoyes par les cartes ADC  
 | 
|---|
 | 71 | // --------------------------------------------------------
 | 
|---|
| [3623] | 72 | 
 | 
|---|
 | 73 | // OFFSET : au cas ou le firmware reception ajoute des mots avant le header 
 | 
|---|
| [3592] | 74 | #define OFFSET 0 
 | 
|---|
| [3537] | 75 | // Taille entete/trailer en octets 
 | 
|---|
 | 76 | #define BRHDRSIZE 24
 | 
|---|
 | 77 | // Actuellement le trailer est vire ...
 | 
|---|
| [3592] | 78 | // Reza, 28 Jan 2009  le trailer fait 16 octets 
 | 
|---|
 | 79 | #define BRTRLSIZE 16  
 | 
|---|
 | 80 | /* REZA passe a 16 octets  #define BRTRLSIZE 40 */
 | 
|---|
 | 81 | //Offset Mode Acq 
 | 
|---|
 | 82 | #define BRMODACQOFF  20 /*RezaMOD #define BRMODACQOFF  24 */
 | 
|---|
 | 83 | // Offset ChanId
 | 
|---|
 | 84 | #define BRCHANIDOFF 20 /*RezaMOD #define BRCHANIDOFF 24 */
 | 
|---|
| [3537] | 85 | // Offset du time-tag
 | 
|---|
| [3592] | 86 | #define BRTMTAGOFF 12 /*RezaMOD #define BRTMTAGOFF 16 */
 | 
|---|
 | 87 | // offset FrameCounter
 | 
|---|
 | 88 | #define BRFRCPTOFF 16 /*RezaMOD#define BRFRCPTOFF 20 */
 | 
|---|
 | 89 | // offset Paquet Id 
 | 
|---|
 | 90 | #define BRPKTIDOFF 20  /*RezaMOD #define BRPKTIDOFF 24 */
 | 
|---|
 | 91 | // offset paquet lenght = FrameDataLength 
 | 
|---|
 | 92 | #define BRPKTLENOFF 8 /*RezaMOD #define BRPKTLENOFF 12 */
 | 
|---|
 | 93 | // offset position chariot ???
 | 
|---|
 | 94 | //#define BRPCOFF 16
 | 
|---|
| [3537] | 95 | 
 | 
|---|
| [3671] | 96 | enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 };
 | 
|---|
 | 97 | enum ModeAcq { RawData=1, FFT };
 | 
|---|
| [3592] | 98 | 
 | 
|---|
 | 99 | // Definition des actions sur la conversion de format (swap...) des donnees arrivant
 | 
|---|
 | 100 | enum BRDataFmtConv {
 | 
|---|
| [3671] | 101 |   BR_DoNothing, BR_Copy, BR_SwapAll, BR_Swap32, BR_CopyHDR, BR_SwapHDR, 
 | 
|---|
 | 102 |   BR_FFTOneChan, BR_FFTTwoChan,  
 | 
|---|
 | 103 |   BR_FFTOneChanSwapAll, BR_FFTTwoChanSwapAll, 
 | 
|---|
 | 104 |   BR_FFTOneChanSwap32, BR_FFTTwoChanSwap32,
 | 
|---|
| [3592] | 105 | };
 | 
|---|
 | 106 | 
 | 
|---|
| [3671] | 107 | // Definition des action pour recopie de paquet avec reduction de taille de paquet
 | 
|---|
 | 108 | //    Copie complete (--> egalite de taille, 1,2 canaux, K0 -> Keep first two bytes (Continu/Nyquist) 
 | 
|---|
 | 109 | enum BRPaqReducAction {
 | 
|---|
 | 110 |   BR_CopyRA, BR_OneChanReduc, BR_TwoChanReduc, BR_OneChanReducK0, BR_TwoChanReducK0
 | 
|---|
 | 111 | };
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 | //---- Classe BRPaquet 
 | 
|---|
| [3537] | 114 | // Structure correspondant a HEADER-DATA-TRAILER
 | 
|---|
 | 115 | class BRPaquet {
 | 
|---|
 | 116 |  public:
 | 
|---|
| [3623] | 117 | // Cree d'un objet BRPaquet avec copie/swap depuis src -> dst (si src != NULL)
 | 
|---|
| [3671] | 118 |   BRPaquet(Byte* src, Byte* dst, int paqsz, BRDataFmtConv fgconv=BR_Copy);
 | 
|---|
| [3658] | 119 |   // Cree d'un objet BRPaquet de taille paqsz sur la zone dst   
 | 
|---|
| [3671] | 120 |   BRPaquet(Byte* srcdst, int paqsz);
 | 
|---|
| [3694] | 121 |   BRPaquet(int paqsz=16424);   // Set aussi de constructeur par defaut
 | 
|---|
| [3683] | 122 |   BRPaquet(BRPaquet const& paq);
 | 
|---|
| [3671] | 123 | 
 | 
|---|
| [3658] | 124 |   //  ~BRPaquet();
 | 
|---|
| [3683] | 125 |   inline void Set(Byte* dst)   { dst_=dst; return; }
 | 
|---|
 | 126 |   inline void Set(Byte* dst, int paqsz)   { dst_=dst;  sz_=paqsz; return; }
 | 
|---|
| [3537] | 127 | 
 | 
|---|
| [3671] | 128 |   // Pour la copie/reduction de taille de paquet 
 | 
|---|
 | 129 |   void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0);
 | 
|---|
 | 130 | 
 | 
|---|
| [3537] | 131 |   // Acces diverses tailles 
 | 
|---|
 | 132 |   inline int PaquetSize() { return sz_; }
 | 
|---|
 | 133 |   inline int DataSize() { return sz_-(BRHDRSIZE+BRTRLSIZE); }
 | 
|---|
 | 134 |   inline int HeaderSize() { return BRHDRSIZE; }
 | 
|---|
 | 135 |   inline int TrailerSize() { return BRTRLSIZE; }
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |   // Acces differentes zone memoire
 | 
|---|
| [3623] | 138 |   inline Byte* Begin() { return dst_+OFFSET; }
 | 
|---|
| [3592] | 139 |   inline Byte* Data1() { return dst_+BRHDRSIZE+OFFSET; }
 | 
|---|
 | 140 |   inline Byte* Data2() { return dst_+BRHDRSIZE+(DataSize()/2)+OFFSET; }
 | 
|---|
 | 141 |   inline Byte* Header() { return dst_+OFFSET; }
 | 
|---|
 | 142 |   inline Byte* Trailer() { return (dst_+sz_-BRTRLSIZE+OFFSET); }
 | 
|---|
| [3659] | 143 | 
 | 
|---|
 | 144 |   // Acces aux differentes zone de donnees en signed byte (-127 ... 127), donnees FFT
 | 
|---|
 | 145 |   inline SByte* BeginS() { return (SByte*)(dst_+OFFSET); }
 | 
|---|
 | 146 |   inline SByte* Data1S() { return (SByte*)(dst_+BRHDRSIZE+OFFSET); }
 | 
|---|
 | 147 |   inline SByte* Data2S() { return (SByte*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |   // Acces aux differentes zone de donnees en TwoByteComplex pour donnees FFT
 | 
|---|
 | 150 |   inline int DataSizeC() { return (sz_-(BRHDRSIZE+BRTRLSIZE))/2; }
 | 
|---|
 | 151 |   inline TwoByteComplex* BeginC() { return (TwoByteComplex*)(dst_+OFFSET); }
 | 
|---|
 | 152 |   inline TwoByteComplex* Data1C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+OFFSET); }
 | 
|---|
 | 153 |   inline TwoByteComplex* Data2C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
 | 
|---|
 | 154 | 
 | 
|---|
| [3537] | 155 |   // Valeurs differentes zones HDR/TRL
 | 
|---|
| [3592] | 156 |   inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));}
 | 
|---|
| [3623] | 157 |   inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));}
 | 
|---|
 | 158 |   inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));}
 | 
|---|
 | 159 | 
 | 
|---|
| [3592] | 160 |   inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
 | 
|---|
| [3623] | 161 |   inline UInt32 TRLMarker2() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET+1)));}
 | 
|---|
 | 162 |   inline UInt64 TRLMarker64() {return *((UInt64*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 | // Informations diverses sur le paquet/mode d'acquisition
 | 
|---|
| [3592] | 165 |   inline UInt16 ModeAcq()   {return *((UInt16*)(dst_+(BRMODACQOFF+OFFSET)));}
 | 
|---|
 | 166 |   inline UInt16 ChanId()    {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0x1800)>> 12) ;}
 | 
|---|
 | 167 |   inline UInt16 ChipId()    {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0xC00)>> 10) ;}
 | 
|---|
| [3537] | 168 | 
 | 
|---|
| [3623] | 169 |   inline UInt32 FrameCounter()   {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);}
 | 
|---|
| [3592] | 170 |   inline UInt32 TimeTag1()   {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));}
 | 
|---|
 | 171 |   inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);}
 | 
|---|
| [3635] | 172 |   inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);}
 | 
|---|
| [3592] | 173 | 
 | 
|---|
 | 174 |   inline UInt32 PaqId()     {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));}
 | 
|---|
 | 175 |   inline UInt16 PaqLen()    {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));}
 | 
|---|
 | 176 | 
 | 
|---|
| [3623] | 177 |   UInt16  ChannelID();
 | 
|---|
 | 178 |   UInt16  ModeAcquisition();
 | 
|---|
| [3592] | 179 | 
 | 
|---|
 | 180 |   // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));}
 | 
|---|
| [3623] | 181 |   // Fonctions utiles pour remplir un objet BRPaquet 
 | 
|---|
 | 182 |   void SetHDRMarker64(UInt64 htag); 
 | 
|---|
 | 183 |   void SetTRLMarker64(UInt64 ttag); 
 | 
|---|
 | 184 |   void SetFrameCounter(UInt32 fc);  
 | 
|---|
 | 185 |   void SetTimeTag(UInt64 timtag);
 | 
|---|
 | 186 |   inline void SetPaqLen(UInt16 len) { *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET))) = len; }
 | 
|---|
| [3592] | 187 | 
 | 
|---|
| [3537] | 188 |   // pour faire un print de la structure
 | 
|---|
| [3623] | 189 |   ostream& Print(ostream & os, int nelt=8, bool prht=true);
 | 
|---|
 | 190 |   inline ostream& Print(int nelt=8, bool prht=true)
 | 
|---|
 | 191 |     { return Print(cout, nelt, prht); }
 | 
|---|
| [3537] | 192 | 
 | 
|---|
| [3592] | 193 |   // fonction appelee par le constructeur pour reordonner les donnees FFT 
 | 
|---|
| [3671] | 194 |   // --- Remise en ordre avec swap complet sur 8 bytes 
 | 
|---|
 | 195 |   static void ReorderFFTDataSwapAll(SByte* src, SByte* dst, int sz);
 | 
|---|
 | 196 |   // --- Remise en ordre avec echanges des mots de 4 bytes 
 | 
|---|
 | 197 |   static void ReorderFFTDataSwap32(SByte* src, SByte* dst, int sz);
 | 
|---|
 | 198 |   // --- Remise en ordre seulement 
 | 
|---|
| [3659] | 199 |   static void ReorderFFTData(SByte* src, SByte* dst, int sz);
 | 
|---|
| [3671] | 200 |   static const char* FmtConvToString(BRDataFmtConv fgconv);
 | 
|---|
| [3674] | 201 |   static const char* ReducActionToString(BRPaqReducAction rac);
 | 
|---|
 | 202 | 
 | 
|---|
| [3592] | 203 | // protected:
 | 
|---|
| [3537] | 204 |   // donnees membres
 | 
|---|
 | 205 |   int sz_; //taille du paquet 
 | 
|---|
 | 206 |   Byte* dst_;  
 | 
|---|
| [3658] | 207 |   
 | 
|---|
| [3537] | 208 | };
 | 
|---|
 | 209 | 
 | 
|---|
| [3592] | 210 | // --------------------------------------------------------------------------
 | 
|---|
 | 211 | // Classe pour effectuer des verifications d'integrite sur les paquets/frames
 | 
|---|
 | 212 | // --------------------------------------------------------------------------
 | 
|---|
 | 213 | 
 | 
|---|
 | 214 | class BRPaqChecker {
 | 
|---|
 | 215 | public: 
 | 
|---|
| [3659] | 216 |   // if cktrl==true, check header AND trailer, ==false check header only 
 | 
|---|
| [3640] | 217 |   BRPaqChecker(bool cktrl=true, int maxprt=0);
 | 
|---|
| [3683] | 218 |   BRPaqChecker(BRPaqChecker const& pck);
 | 
|---|
 | 219 | 
 | 
|---|
| [3592] | 220 |   ~BRPaqChecker();
 | 
|---|
| [3623] | 221 | 
 | 
|---|
| [3683] | 222 |   inline bool SetCheckTrailerFlag(bool cktrl=true) { cktrl_=cktrl; return cktrl_; }
 | 
|---|
 | 223 |   inline int SetMaxPrint(int maxprt=0) { maxprt=maxprt_; return maxprt_; }
 | 
|---|
 | 224 | 
 | 
|---|
| [3623] | 225 |   UInt64 DefineHDRTag(UInt32 hdr1=0x76543210, UInt32 hdr2=0xFEDCBA98);
 | 
|---|
 | 226 |   UInt64 DefineTRLTag(UInt32 trl1=0x55555555, UInt32 trl2=0xAAAAAAAA);
 | 
|---|
 | 227 |   
 | 
|---|
 | 228 |   inline UInt64 HDRTag() { return hdrtag_; }
 | 
|---|
 | 229 |   inline UInt64 TRLTag() { return trltag_; }
 | 
|---|
 | 230 | 
 | 
|---|
| [3592] | 231 |   // Verifie le paquet, renvoie true si OK
 | 
|---|
| [3659] | 232 |   bool Check(BRPaquet& paq, UInt64& numframe);
 | 
|---|
 | 233 |   inline bool Check(BRPaquet& paq)  { UInt64 nf; return Check(paq, nf); }
 | 
|---|
 | 234 | 
 | 
|---|
| [3592] | 235 |   // Imprime le compte de paquets ... 
 | 
|---|
| [3640] | 236 |   ostream & Print(ostream& os) const;
 | 
|---|
 | 237 |   inline ostream & Print() const { return Print(cout); }
 | 
|---|
| [3671] | 238 |   // renvoie le resume de la statistique paquets sous forme de chaine
 | 
|---|
 | 239 |   string Summary(bool detail=false) const;
 | 
|---|
| [3592] | 240 | 
 | 
|---|
| [3659] | 241 |   // Acces aux differents compteurs 
 | 
|---|
 | 242 |   inline UInt64 NbPaqTotal() { return totnframes; }
 | 
|---|
 | 243 |   inline UInt64 NbPaqOK() { return nframeok; }
 | 
|---|
 | 244 |   inline UInt64 NbPaqLost() { return lostframes; }
 | 
|---|
 | 245 |   inline UInt64 NbGaps() { return cnt_saut; }
 | 
|---|
 | 246 |   inline UInt64 LastFrameNum() { return lastframenum; }
 | 
|---|
| [3623] | 247 | 
 | 
|---|
| [3659] | 248 | protected:
 | 
|---|
 | 249 |   UInt64 totnframes;    // Nombre totale de frames/paquets traites 
 | 
|---|
| [3683] | 250 |   UInt64 nframeok;      // Nombre totale de frames/paquets avec HDR/TRL OK
 | 
|---|
| [3659] | 251 |   UInt64 lostframes;    // Nombre totale de frames/paquets perdus 
 | 
|---|
 | 252 |   UInt16 frclst;         // derniere valeur du frame-counter 
 | 
|---|
 | 253 |   UInt64 lastframenum;    // Dernier numero de frame(=frame-counter, sans modulo 65535) 
 | 
|---|
 | 254 | 
 | 
|---|
| [3640] | 255 |   bool cktrl_;    // Verifie aussi le trailer si true
 | 
|---|
| [3659] | 256 |   UInt64 cnt_saut;          // Nb de fois ou DeltaFrameCounter>1
 | 
|---|
 | 257 |   UInt32 maxprt_;           // Nb maxi de print paquets perdus / probleme
 | 
|---|
| [3640] | 258 | 
 | 
|---|
| [3623] | 259 |   UInt64 hdrtag_;
 | 
|---|
 | 260 |   UInt64 trltag_; 
 | 
|---|
| [3592] | 261 | };
 | 
|---|
 | 262 | 
 | 
|---|
| [3659] | 263 | // Definition de l'operator << overloading - Appel de Print() 
 | 
|---|
| [3640] | 264 | inline ostream& operator << (ostream& s, BRPaqChecker const & chk)
 | 
|---|
 | 265 |   {  return chk.Print(s);  }
 | 
|---|
 | 266 | 
 | 
|---|
| [3537] | 267 | #endif
 | 
|---|
| [3592] | 268 |  
 | 
|---|
 | 269 |  
 | 
|---|