[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 |
|
---|
[4016] | 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 |
|
---|
[3537] | 23 | #include <stdio.h>
|
---|
| 24 | #include <iostream>
|
---|
[3671] | 25 | #include <exception>
|
---|
[3537] | 26 | #include <string>
|
---|
| 27 | #include "brtypes.h"
|
---|
| 28 |
|
---|
[3592] | 29 |
|
---|
[3537] | 30 | using namespace std;
|
---|
| 31 |
|
---|
[3671] | 32 | // ---------------------------------------------------------
|
---|
| 33 | // ----- Classe d'exception pour Acquisition BAORadio ------
|
---|
[4016] | 34 | /*!
|
---|
| 35 | \class BAORadioException
|
---|
| 36 | \ingroup TAcq
|
---|
| 37 |
|
---|
| 38 | \brief Base exception class for 21 cm acquisition programs.
|
---|
| 39 | */
|
---|
| 40 |
|
---|
[3671] | 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 |
|
---|
[3623] | 57 | // ------------------------------------------------
|
---|
| 58 | // ----- Classe TwoByteComplex ------
|
---|
[3592] | 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
|
---|
[4016] | 63 | /*!
|
---|
| 64 | \class TwoByteComplex
|
---|
| 65 | \ingroup TAcq
|
---|
| 66 |
|
---|
| 67 | \brief Simple class to represent complex numbers formed from two 1 byte signed integers.
|
---|
| 68 | */
|
---|
[3592] | 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 |
|
---|
[3659] | 79 | inline SByte& realB() { return val_[0]; }
|
---|
| 80 | inline SByte& imagB() { return val_[1]; }
|
---|
[3592] | 81 |
|
---|
[3659] | 82 | inline int realI() { return (int)(val_)[0]; }
|
---|
| 83 | inline int imagI() { return (int)(val_)[1]; }
|
---|
[3592] | 84 |
|
---|
[3659] | 85 | inline double realD() { return (double)(val_)[0]; }
|
---|
[3671] | 86 | inline double imagD() { return (double)(val_)[1]; }
|
---|
[3592] | 87 |
|
---|
[3774] | 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 |
|
---|
[3659] | 91 | SByte val_[2];
|
---|
[3592] | 92 | };
|
---|
| 93 |
|
---|
[3671] | 94 | // --------------------------------------------------------
|
---|
| 95 | // ----- Classe BRPaquet , constante et enum associe ------
|
---|
| 96 | // Representation, manipulation des paquets/frames
|
---|
| 97 | // envoyes par les cartes ADC
|
---|
| 98 | // --------------------------------------------------------
|
---|
[3623] | 99 |
|
---|
| 100 | // OFFSET : au cas ou le firmware reception ajoute des mots avant le header
|
---|
[3592] | 101 | #define OFFSET 0
|
---|
[3537] | 102 | // Taille entete/trailer en octets
|
---|
| 103 | #define BRHDRSIZE 24
|
---|
| 104 | // Actuellement le trailer est vire ...
|
---|
[3592] | 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 */
|
---|
[3537] | 112 | // Offset du time-tag
|
---|
[3592] | 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
|
---|
[3537] | 122 |
|
---|
[3671] | 123 | enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 };
|
---|
| 124 | enum ModeAcq { RawData=1, FFT };
|
---|
[3592] | 125 |
|
---|
| 126 | // Definition des actions sur la conversion de format (swap...) des donnees arrivant
|
---|
| 127 | enum BRDataFmtConv {
|
---|
[3671] | 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,
|
---|
[3592] | 132 | };
|
---|
| 133 |
|
---|
[3671] | 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
|
---|
[3537] | 141 | // Structure correspondant a HEADER-DATA-TRAILER
|
---|
| 142 | class BRPaquet {
|
---|
| 143 | public:
|
---|
[3623] | 144 | // Cree d'un objet BRPaquet avec copie/swap depuis src -> dst (si src != NULL)
|
---|
[3671] | 145 | BRPaquet(Byte* src, Byte* dst, int paqsz, BRDataFmtConv fgconv=BR_Copy);
|
---|
[3658] | 146 | // Cree d'un objet BRPaquet de taille paqsz sur la zone dst
|
---|
[3671] | 147 | BRPaquet(Byte* srcdst, int paqsz);
|
---|
[3694] | 148 | BRPaquet(int paqsz=16424); // Set aussi de constructeur par defaut
|
---|
[3683] | 149 | BRPaquet(BRPaquet const& paq);
|
---|
[3671] | 150 |
|
---|
[3658] | 151 | // ~BRPaquet();
|
---|
[3683] | 152 | inline void Set(Byte* dst) { dst_=dst; return; }
|
---|
| 153 | inline void Set(Byte* dst, int paqsz) { dst_=dst; sz_=paqsz; return; }
|
---|
[3537] | 154 |
|
---|
[3671] | 155 | // Pour la copie/reduction de taille de paquet
|
---|
| 156 | void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0);
|
---|
| 157 |
|
---|
[3537] | 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
|
---|
[3623] | 165 | inline Byte* Begin() { return dst_+OFFSET; }
|
---|
[3592] | 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); }
|
---|
[3659] | 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 |
|
---|
[3537] | 182 | // Valeurs differentes zones HDR/TRL
|
---|
[3592] | 183 | inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));}
|
---|
[3623] | 184 | inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));}
|
---|
| 185 | inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));}
|
---|
| 186 |
|
---|
[3592] | 187 | inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
|
---|
[3623] | 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
|
---|
[3592] | 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) ;}
|
---|
[3537] | 195 |
|
---|
[3623] | 196 | inline UInt32 FrameCounter() {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);}
|
---|
[3592] | 197 | inline UInt32 TimeTag1() {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));}
|
---|
| 198 | inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);}
|
---|
[3635] | 199 | inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);}
|
---|
[3592] | 200 |
|
---|
| 201 | inline UInt32 PaqId() {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));}
|
---|
| 202 | inline UInt16 PaqLen() {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));}
|
---|
| 203 |
|
---|
[3623] | 204 | UInt16 ChannelID();
|
---|
| 205 | UInt16 ModeAcquisition();
|
---|
[3592] | 206 |
|
---|
| 207 | // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));}
|
---|
[3623] | 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; }
|
---|
[3592] | 214 |
|
---|
[3537] | 215 | // pour faire un print de la structure
|
---|
[3623] | 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); }
|
---|
[3537] | 219 |
|
---|
[3592] | 220 | // fonction appelee par le constructeur pour reordonner les donnees FFT
|
---|
[3671] | 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
|
---|
[3659] | 226 | static void ReorderFFTData(SByte* src, SByte* dst, int sz);
|
---|
[3671] | 227 | static const char* FmtConvToString(BRDataFmtConv fgconv);
|
---|
[3674] | 228 | static const char* ReducActionToString(BRPaqReducAction rac);
|
---|
| 229 |
|
---|
[3592] | 230 | // protected:
|
---|
[3537] | 231 | // donnees membres
|
---|
| 232 | int sz_; //taille du paquet
|
---|
| 233 | Byte* dst_;
|
---|
[3658] | 234 |
|
---|
[3537] | 235 | };
|
---|
| 236 |
|
---|
[3592] | 237 | // --------------------------------------------------------------------------
|
---|
| 238 | // Classe pour effectuer des verifications d'integrite sur les paquets/frames
|
---|
| 239 | // --------------------------------------------------------------------------
|
---|
| 240 |
|
---|
| 241 | class BRPaqChecker {
|
---|
| 242 | public:
|
---|
[3659] | 243 | // if cktrl==true, check header AND trailer, ==false check header only
|
---|
[3640] | 244 | BRPaqChecker(bool cktrl=true, int maxprt=0);
|
---|
[3683] | 245 | BRPaqChecker(BRPaqChecker const& pck);
|
---|
| 246 |
|
---|
[3592] | 247 | ~BRPaqChecker();
|
---|
[3623] | 248 |
|
---|
[3683] | 249 | inline bool SetCheckTrailerFlag(bool cktrl=true) { cktrl_=cktrl; return cktrl_; }
|
---|
| 250 | inline int SetMaxPrint(int maxprt=0) { maxprt=maxprt_; return maxprt_; }
|
---|
| 251 |
|
---|
[3623] | 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 |
|
---|
[3592] | 258 | // Verifie le paquet, renvoie true si OK
|
---|
[3659] | 259 | bool Check(BRPaquet& paq, UInt64& numframe);
|
---|
| 260 | inline bool Check(BRPaquet& paq) { UInt64 nf; return Check(paq, nf); }
|
---|
| 261 |
|
---|
[3592] | 262 | // Imprime le compte de paquets ...
|
---|
[3640] | 263 | ostream & Print(ostream& os) const;
|
---|
| 264 | inline ostream & Print() const { return Print(cout); }
|
---|
[3671] | 265 | // renvoie le resume de la statistique paquets sous forme de chaine
|
---|
| 266 | string Summary(bool detail=false) const;
|
---|
[3592] | 267 |
|
---|
[3659] | 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; }
|
---|
[3623] | 274 |
|
---|
[3659] | 275 | protected:
|
---|
| 276 | UInt64 totnframes; // Nombre totale de frames/paquets traites
|
---|
[3683] | 277 | UInt64 nframeok; // Nombre totale de frames/paquets avec HDR/TRL OK
|
---|
[3659] | 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 |
|
---|
[3640] | 282 | bool cktrl_; // Verifie aussi le trailer si true
|
---|
[3659] | 283 | UInt64 cnt_saut; // Nb de fois ou DeltaFrameCounter>1
|
---|
| 284 | UInt32 maxprt_; // Nb maxi de print paquets perdus / probleme
|
---|
[3640] | 285 |
|
---|
[3623] | 286 | UInt64 hdrtag_;
|
---|
| 287 | UInt64 trltag_;
|
---|
[3592] | 288 | };
|
---|
| 289 |
|
---|
[3659] | 290 | // Definition de l'operator << overloading - Appel de Print()
|
---|
[3640] | 291 | inline ostream& operator << (ostream& s, BRPaqChecker const & chk)
|
---|
| 292 | { return chk.Print(s); }
|
---|
| 293 |
|
---|
[3537] | 294 | #endif
|
---|
[3592] | 295 |
|
---|
| 296 |
|
---|