#ifndef BRPAQU_H_SEEN #define BRPAQU_H_SEEN /* ---------------------------------------- Projet BAORadio --- LAL Juin 2008 , M.Taurigna , R. Ansari ------------------------------------------- */ #include #include #include #include #include "brtypes.h" using namespace std; // --------------------------------------------------------- // ----- Classe d'exception pour Acquisition BAORadio ------ #define BREX_MAXMSGLEN 160 class BAORadioException : public std::exception { public: explicit BAORadioException(const char * m) throw() ; explicit BAORadioException(const string& m) throw() ; virtual ~BAORadioException() throw() ; //! Implementation of std::exception what() method, returning the exception message virtual const char* what() const throw(); virtual string const Msg() const ; private: char msg_[BREX_MAXMSGLEN]; }; // ------------------------------------------------ // ----- Classe TwoByteComplex ------ // On definit une classe TwoByteComplex() pour manipuler une paire de byte // representant partie relle et imaginaire d'un nombre complexe // Remarque Byte = unsigned char // Remarque SByte = char = signed char class TwoByteComplex { public: explicit TwoByteComplex() { val_[0] = val_[1] = 0; } // explicit TwoByteComplex(TwoByteComplex const & a) { val_[0] = a.val_[0]; val_[1] = 0; } explicit TwoByteComplex(Byte re, Byte im) { val_[0] = re; val_[1] = im; } explicit TwoByteComplex(SByte re, SByte im) { val_[0] = re; val_[1] = im; } explicit TwoByteComplex(int re, int im) { val_[0] = re; val_[1] = im; } explicit TwoByteComplex(float re, float im) { val_[0] = re; val_[1] = im; } explicit TwoByteComplex(double re, double im) { val_[0] = re; val_[1] = im; } inline SByte& realB() { return val_[0]; } inline SByte& imagB() { return val_[1]; } inline int realI() { return (int)(val_)[0]; } inline int imagI() { return (int)(val_)[1]; } inline double realD() { return (double)(val_)[0]; } inline double imagD() { return (double)(val_)[1]; } SByte val_[2]; }; // -------------------------------------------------------- // ----- Classe BRPaquet , constante et enum associe ------ // Representation, manipulation des paquets/frames // envoyes par les cartes ADC // -------------------------------------------------------- // OFFSET : au cas ou le firmware reception ajoute des mots avant le header #define OFFSET 0 // Taille entete/trailer en octets #define BRHDRSIZE 24 // Actuellement le trailer est vire ... // Reza, 28 Jan 2009 le trailer fait 16 octets #define BRTRLSIZE 16 /* REZA passe a 16 octets #define BRTRLSIZE 40 */ //Offset Mode Acq #define BRMODACQOFF 20 /*RezaMOD #define BRMODACQOFF 24 */ // Offset ChanId #define BRCHANIDOFF 20 /*RezaMOD #define BRCHANIDOFF 24 */ // Offset du time-tag #define BRTMTAGOFF 12 /*RezaMOD #define BRTMTAGOFF 16 */ // offset FrameCounter #define BRFRCPTOFF 16 /*RezaMOD#define BRFRCPTOFF 20 */ // offset Paquet Id #define BRPKTIDOFF 20 /*RezaMOD #define BRPKTIDOFF 24 */ // offset paquet lenght = FrameDataLength #define BRPKTLENOFF 8 /*RezaMOD #define BRPKTLENOFF 12 */ // offset position chariot ??? //#define BRPCOFF 16 enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 }; enum ModeAcq { RawData=1, FFT }; // Definition des actions sur la conversion de format (swap...) des donnees arrivant enum BRDataFmtConv { BR_DoNothing, BR_Copy, BR_SwapAll, BR_Swap32, BR_CopyHDR, BR_SwapHDR, BR_FFTOneChan, BR_FFTTwoChan, BR_FFTOneChanSwapAll, BR_FFTTwoChanSwapAll, BR_FFTOneChanSwap32, BR_FFTTwoChanSwap32, }; // Definition des action pour recopie de paquet avec reduction de taille de paquet // Copie complete (--> egalite de taille, 1,2 canaux, K0 -> Keep first two bytes (Continu/Nyquist) enum BRPaqReducAction { BR_CopyRA, BR_OneChanReduc, BR_TwoChanReduc, BR_OneChanReducK0, BR_TwoChanReducK0 }; //---- Classe BRPaquet // Structure correspondant a HEADER-DATA-TRAILER class BRPaquet { public: // Cree d'un objet BRPaquet avec copie/swap depuis src -> dst (si src != NULL) BRPaquet(Byte* src, Byte* dst, int paqsz, BRDataFmtConv fgconv=BR_Copy); // Cree d'un objet BRPaquet de taille paqsz sur la zone dst BRPaquet(Byte* srcdst, int paqsz); BRPaquet(int paqsz=16424); // Set aussi de constructeur par defaut BRPaquet(BRPaquet const& paq); // ~BRPaquet(); inline void Set(Byte* dst) { dst_=dst; return; } inline void Set(Byte* dst, int paqsz) { dst_=dst; sz_=paqsz; return; } // Pour la copie/reduction de taille de paquet void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0); // Acces diverses tailles inline int PaquetSize() { return sz_; } inline int DataSize() { return sz_-(BRHDRSIZE+BRTRLSIZE); } inline int HeaderSize() { return BRHDRSIZE; } inline int TrailerSize() { return BRTRLSIZE; } // Acces differentes zone memoire inline Byte* Begin() { return dst_+OFFSET; } inline Byte* Data1() { return dst_+BRHDRSIZE+OFFSET; } inline Byte* Data2() { return dst_+BRHDRSIZE+(DataSize()/2)+OFFSET; } inline Byte* Header() { return dst_+OFFSET; } inline Byte* Trailer() { return (dst_+sz_-BRTRLSIZE+OFFSET); } // Acces aux differentes zone de donnees en signed byte (-127 ... 127), donnees FFT inline SByte* BeginS() { return (SByte*)(dst_+OFFSET); } inline SByte* Data1S() { return (SByte*)(dst_+BRHDRSIZE+OFFSET); } inline SByte* Data2S() { return (SByte*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); } // Acces aux differentes zone de donnees en TwoByteComplex pour donnees FFT inline int DataSizeC() { return (sz_-(BRHDRSIZE+BRTRLSIZE))/2; } inline TwoByteComplex* BeginC() { return (TwoByteComplex*)(dst_+OFFSET); } inline TwoByteComplex* Data1C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+OFFSET); } inline TwoByteComplex* Data2C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); } // Valeurs differentes zones HDR/TRL inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));} inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));} inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));} inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));} inline UInt32 TRLMarker2() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET+1)));} inline UInt64 TRLMarker64() {return *((UInt64*)(dst_+(sz_-BRTRLSIZE+OFFSET)));} // Informations diverses sur le paquet/mode d'acquisition inline UInt16 ModeAcq() {return *((UInt16*)(dst_+(BRMODACQOFF+OFFSET)));} inline UInt16 ChanId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0x1800)>> 12) ;} inline UInt16 ChipId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0xC00)>> 10) ;} inline UInt32 FrameCounter() {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);} inline UInt32 TimeTag1() {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));} inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);} inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);} inline UInt32 PaqId() {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));} inline UInt16 PaqLen() {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));} UInt16 ChannelID(); UInt16 ModeAcquisition(); // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));} // Fonctions utiles pour remplir un objet BRPaquet void SetHDRMarker64(UInt64 htag); void SetTRLMarker64(UInt64 ttag); void SetFrameCounter(UInt32 fc); void SetTimeTag(UInt64 timtag); inline void SetPaqLen(UInt16 len) { *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET))) = len; } // pour faire un print de la structure ostream& Print(ostream & os, int nelt=8, bool prht=true); inline ostream& Print(int nelt=8, bool prht=true) { return Print(cout, nelt, prht); } // fonction appelee par le constructeur pour reordonner les donnees FFT // --- Remise en ordre avec swap complet sur 8 bytes static void ReorderFFTDataSwapAll(SByte* src, SByte* dst, int sz); // --- Remise en ordre avec echanges des mots de 4 bytes static void ReorderFFTDataSwap32(SByte* src, SByte* dst, int sz); // --- Remise en ordre seulement static void ReorderFFTData(SByte* src, SByte* dst, int sz); static const char* FmtConvToString(BRDataFmtConv fgconv); static const char* ReducActionToString(BRPaqReducAction rac); // protected: // donnees membres int sz_; //taille du paquet Byte* dst_; }; // -------------------------------------------------------------------------- // Classe pour effectuer des verifications d'integrite sur les paquets/frames // -------------------------------------------------------------------------- class BRPaqChecker { public: // if cktrl==true, check header AND trailer, ==false check header only BRPaqChecker(bool cktrl=true, int maxprt=0); BRPaqChecker(BRPaqChecker const& pck); ~BRPaqChecker(); inline bool SetCheckTrailerFlag(bool cktrl=true) { cktrl_=cktrl; return cktrl_; } inline int SetMaxPrint(int maxprt=0) { maxprt=maxprt_; return maxprt_; } UInt64 DefineHDRTag(UInt32 hdr1=0x76543210, UInt32 hdr2=0xFEDCBA98); UInt64 DefineTRLTag(UInt32 trl1=0x55555555, UInt32 trl2=0xAAAAAAAA); inline UInt64 HDRTag() { return hdrtag_; } inline UInt64 TRLTag() { return trltag_; } // Verifie le paquet, renvoie true si OK bool Check(BRPaquet& paq, UInt64& numframe); inline bool Check(BRPaquet& paq) { UInt64 nf; return Check(paq, nf); } // Imprime le compte de paquets ... ostream & Print(ostream& os) const; inline ostream & Print() const { return Print(cout); } // renvoie le resume de la statistique paquets sous forme de chaine string Summary(bool detail=false) const; // Acces aux differents compteurs inline UInt64 NbPaqTotal() { return totnframes; } inline UInt64 NbPaqOK() { return nframeok; } inline UInt64 NbPaqLost() { return lostframes; } inline UInt64 NbGaps() { return cnt_saut; } inline UInt64 LastFrameNum() { return lastframenum; } protected: UInt64 totnframes; // Nombre totale de frames/paquets traites UInt64 nframeok; // Nombre totale de frames/paquets avec HDR/TRL OK UInt64 lostframes; // Nombre totale de frames/paquets perdus UInt16 frclst; // derniere valeur du frame-counter UInt64 lastframenum; // Dernier numero de frame(=frame-counter, sans modulo 65535) bool cktrl_; // Verifie aussi le trailer si true UInt64 cnt_saut; // Nb de fois ou DeltaFrameCounter>1 UInt32 maxprt_; // Nb maxi de print paquets perdus / probleme UInt64 hdrtag_; UInt64 trltag_; }; // Definition de l'operator << overloading - Appel de Print() inline ostream& operator << (ostream& s, BRPaqChecker const & chk) { return chk.Print(s); } #endif