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