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 | 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 |
|
---|
64 | SByte val_[2];
|
---|
65 | };
|
---|
66 |
|
---|
67 | // --------------------------------------------------------
|
---|
68 | // ----- Classe BRPaquet , constante et enum associe ------
|
---|
69 | // Representation, manipulation des paquets/frames
|
---|
70 | // envoyes par les cartes ADC
|
---|
71 | // --------------------------------------------------------
|
---|
72 |
|
---|
73 | // OFFSET : au cas ou le firmware reception ajoute des mots avant le header
|
---|
74 | #define OFFSET 0
|
---|
75 | // Taille entete/trailer en octets
|
---|
76 | #define BRHDRSIZE 24
|
---|
77 | // Actuellement le trailer est vire ...
|
---|
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 */
|
---|
85 | // Offset du time-tag
|
---|
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
|
---|
95 |
|
---|
96 | enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 };
|
---|
97 | enum ModeAcq { RawData=1, FFT };
|
---|
98 |
|
---|
99 | // Definition des actions sur la conversion de format (swap...) des donnees arrivant
|
---|
100 | enum BRDataFmtConv {
|
---|
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,
|
---|
105 | };
|
---|
106 |
|
---|
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
|
---|
114 | // Structure correspondant a HEADER-DATA-TRAILER
|
---|
115 | class BRPaquet {
|
---|
116 | public:
|
---|
117 | // Cree d'un objet BRPaquet avec copie/swap depuis src -> dst (si src != NULL)
|
---|
118 | BRPaquet(Byte* src, Byte* dst, int paqsz, BRDataFmtConv fgconv=BR_Copy);
|
---|
119 | // Cree d'un objet BRPaquet de taille paqsz sur la zone dst
|
---|
120 | BRPaquet(Byte* srcdst, int paqsz);
|
---|
121 | BRPaquet(int paqsz=16424); // Set aussi de constructeur par defaut
|
---|
122 | BRPaquet(BRPaquet const& paq);
|
---|
123 |
|
---|
124 | // ~BRPaquet();
|
---|
125 | inline void Set(Byte* dst) { dst_=dst; return; }
|
---|
126 | inline void Set(Byte* dst, int paqsz) { dst_=dst; sz_=paqsz; return; }
|
---|
127 |
|
---|
128 | // Pour la copie/reduction de taille de paquet
|
---|
129 | void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0);
|
---|
130 |
|
---|
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
|
---|
138 | inline Byte* Begin() { return dst_+OFFSET; }
|
---|
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); }
|
---|
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 |
|
---|
155 | // Valeurs differentes zones HDR/TRL
|
---|
156 | inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));}
|
---|
157 | inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));}
|
---|
158 | inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));}
|
---|
159 |
|
---|
160 | inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
|
---|
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
|
---|
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) ;}
|
---|
168 |
|
---|
169 | inline UInt32 FrameCounter() {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);}
|
---|
170 | inline UInt32 TimeTag1() {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));}
|
---|
171 | inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);}
|
---|
172 | inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);}
|
---|
173 |
|
---|
174 | inline UInt32 PaqId() {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));}
|
---|
175 | inline UInt16 PaqLen() {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));}
|
---|
176 |
|
---|
177 | UInt16 ChannelID();
|
---|
178 | UInt16 ModeAcquisition();
|
---|
179 |
|
---|
180 | // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));}
|
---|
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; }
|
---|
187 |
|
---|
188 | // pour faire un print de la structure
|
---|
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); }
|
---|
192 |
|
---|
193 | // fonction appelee par le constructeur pour reordonner les donnees FFT
|
---|
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
|
---|
199 | static void ReorderFFTData(SByte* src, SByte* dst, int sz);
|
---|
200 | static const char* FmtConvToString(BRDataFmtConv fgconv);
|
---|
201 | static const char* ReducActionToString(BRPaqReducAction rac);
|
---|
202 |
|
---|
203 | // protected:
|
---|
204 | // donnees membres
|
---|
205 | int sz_; //taille du paquet
|
---|
206 | Byte* dst_;
|
---|
207 |
|
---|
208 | };
|
---|
209 |
|
---|
210 | // --------------------------------------------------------------------------
|
---|
211 | // Classe pour effectuer des verifications d'integrite sur les paquets/frames
|
---|
212 | // --------------------------------------------------------------------------
|
---|
213 |
|
---|
214 | class BRPaqChecker {
|
---|
215 | public:
|
---|
216 | // if cktrl==true, check header AND trailer, ==false check header only
|
---|
217 | BRPaqChecker(bool cktrl=true, int maxprt=0);
|
---|
218 | BRPaqChecker(BRPaqChecker const& pck);
|
---|
219 |
|
---|
220 | ~BRPaqChecker();
|
---|
221 |
|
---|
222 | inline bool SetCheckTrailerFlag(bool cktrl=true) { cktrl_=cktrl; return cktrl_; }
|
---|
223 | inline int SetMaxPrint(int maxprt=0) { maxprt=maxprt_; return maxprt_; }
|
---|
224 |
|
---|
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 |
|
---|
231 | // Verifie le paquet, renvoie true si OK
|
---|
232 | bool Check(BRPaquet& paq, UInt64& numframe);
|
---|
233 | inline bool Check(BRPaquet& paq) { UInt64 nf; return Check(paq, nf); }
|
---|
234 |
|
---|
235 | // Imprime le compte de paquets ...
|
---|
236 | ostream & Print(ostream& os) const;
|
---|
237 | inline ostream & Print() const { return Print(cout); }
|
---|
238 | // renvoie le resume de la statistique paquets sous forme de chaine
|
---|
239 | string Summary(bool detail=false) const;
|
---|
240 |
|
---|
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; }
|
---|
247 |
|
---|
248 | protected:
|
---|
249 | UInt64 totnframes; // Nombre totale de frames/paquets traites
|
---|
250 | UInt64 nframeok; // Nombre totale de frames/paquets avec HDR/TRL OK
|
---|
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 |
|
---|
255 | bool cktrl_; // Verifie aussi le trailer si true
|
---|
256 | UInt64 cnt_saut; // Nb de fois ou DeltaFrameCounter>1
|
---|
257 | UInt32 maxprt_; // Nb maxi de print paquets perdus / probleme
|
---|
258 |
|
---|
259 | UInt64 hdrtag_;
|
---|
260 | UInt64 trltag_;
|
---|
261 | };
|
---|
262 |
|
---|
263 | // Definition de l'operator << overloading - Appel de Print()
|
---|
264 | inline ostream& operator << (ostream& s, BRPaqChecker const & chk)
|
---|
265 | { return chk.Print(s); }
|
---|
266 |
|
---|
267 | #endif
|
---|
268 |
|
---|
269 |
|
---|