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