source: Sophya/trunk/AddOn/TAcq/brpaqu.h@ 3676

Last change on this file since 3676 was 3674, checked in by ansari, 16 years ago

version presque finale mfacq.cc avec reduction de taille de paquets - Reza 14/11/2009

File size: 10.2 KB
Line 
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
16using namespace std;
17
18// ---------------------------------------------------------
19// ----- Classe d'exception pour Acquisition BAORadio ------
20#define BREX_MAXMSGLEN 160
21
22class 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
42class 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
93enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 };
94enum ModeAcq { RawData=1, FFT };
95
96// Definition des actions sur la conversion de format (swap...) des donnees arrivant
97enum 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)
106enum BRPaqReducAction {
107 BR_CopyRA, BR_OneChanReduc, BR_TwoChanReduc, BR_OneChanReducK0, BR_TwoChanReducK0
108};
109
110//---- Classe BRPaquet
111// Structure correspondant a HEADER-DATA-TRAILER
112class 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);
119
120 // ~BRPaquet();
121
122 // Pour la copie/reduction de taille de paquet
123 void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0);
124
125 // Acces diverses tailles
126 inline int PaquetSize() { return sz_; }
127 inline int DataSize() { return sz_-(BRHDRSIZE+BRTRLSIZE); }
128 inline int HeaderSize() { return BRHDRSIZE; }
129 inline int TrailerSize() { return BRTRLSIZE; }
130
131 // Acces differentes zone memoire
132 inline Byte* Begin() { return dst_+OFFSET; }
133 inline Byte* Data1() { return dst_+BRHDRSIZE+OFFSET; }
134 inline Byte* Data2() { return dst_+BRHDRSIZE+(DataSize()/2)+OFFSET; }
135 inline Byte* Header() { return dst_+OFFSET; }
136 inline Byte* Trailer() { return (dst_+sz_-BRTRLSIZE+OFFSET); }
137
138 // Acces aux differentes zone de donnees en signed byte (-127 ... 127), donnees FFT
139 inline SByte* BeginS() { return (SByte*)(dst_+OFFSET); }
140 inline SByte* Data1S() { return (SByte*)(dst_+BRHDRSIZE+OFFSET); }
141 inline SByte* Data2S() { return (SByte*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
142
143 // Acces aux differentes zone de donnees en TwoByteComplex pour donnees FFT
144 inline int DataSizeC() { return (sz_-(BRHDRSIZE+BRTRLSIZE))/2; }
145 inline TwoByteComplex* BeginC() { return (TwoByteComplex*)(dst_+OFFSET); }
146 inline TwoByteComplex* Data1C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+OFFSET); }
147 inline TwoByteComplex* Data2C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
148
149 // Valeurs differentes zones HDR/TRL
150 inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));}
151 inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));}
152 inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));}
153
154 inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
155 inline UInt32 TRLMarker2() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET+1)));}
156 inline UInt64 TRLMarker64() {return *((UInt64*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
157
158// Informations diverses sur le paquet/mode d'acquisition
159 inline UInt16 ModeAcq() {return *((UInt16*)(dst_+(BRMODACQOFF+OFFSET)));}
160 inline UInt16 ChanId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0x1800)>> 12) ;}
161 inline UInt16 ChipId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0xC00)>> 10) ;}
162
163 inline UInt32 FrameCounter() {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);}
164 inline UInt32 TimeTag1() {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));}
165 inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);}
166 inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);}
167
168 inline UInt32 PaqId() {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));}
169 inline UInt16 PaqLen() {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));}
170
171 UInt16 ChannelID();
172 UInt16 ModeAcquisition();
173
174 // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));}
175 // Fonctions utiles pour remplir un objet BRPaquet
176 void SetHDRMarker64(UInt64 htag);
177 void SetTRLMarker64(UInt64 ttag);
178 void SetFrameCounter(UInt32 fc);
179 void SetTimeTag(UInt64 timtag);
180 inline void SetPaqLen(UInt16 len) { *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET))) = len; }
181
182 // pour faire un print de la structure
183 ostream& Print(ostream & os, int nelt=8, bool prht=true);
184 inline ostream& Print(int nelt=8, bool prht=true)
185 { return Print(cout, nelt, prht); }
186
187 // fonction appelee par le constructeur pour reordonner les donnees FFT
188 // --- Remise en ordre avec swap complet sur 8 bytes
189 static void ReorderFFTDataSwapAll(SByte* src, SByte* dst, int sz);
190 // --- Remise en ordre avec echanges des mots de 4 bytes
191 static void ReorderFFTDataSwap32(SByte* src, SByte* dst, int sz);
192 // --- Remise en ordre seulement
193 static void ReorderFFTData(SByte* src, SByte* dst, int sz);
194 static const char* FmtConvToString(BRDataFmtConv fgconv);
195 static const char* ReducActionToString(BRPaqReducAction rac);
196
197// protected:
198 // donnees membres
199 int sz_; //taille du paquet
200 Byte* dst_;
201
202};
203
204// --------------------------------------------------------------------------
205// Classe pour effectuer des verifications d'integrite sur les paquets/frames
206// --------------------------------------------------------------------------
207
208class BRPaqChecker {
209public:
210 // if cktrl==true, check header AND trailer, ==false check header only
211 BRPaqChecker(bool cktrl=true, int maxprt=0);
212 ~BRPaqChecker();
213
214 UInt64 DefineHDRTag(UInt32 hdr1=0x76543210, UInt32 hdr2=0xFEDCBA98);
215 UInt64 DefineTRLTag(UInt32 trl1=0x55555555, UInt32 trl2=0xAAAAAAAA);
216
217 inline UInt64 HDRTag() { return hdrtag_; }
218 inline UInt64 TRLTag() { return trltag_; }
219
220 // Verifie le paquet, renvoie true si OK
221 bool Check(BRPaquet& paq, UInt64& numframe);
222 inline bool Check(BRPaquet& paq) { UInt64 nf; return Check(paq, nf); }
223
224 // Imprime le compte de paquets ...
225 ostream & Print(ostream& os) const;
226 inline ostream & Print() const { return Print(cout); }
227 // renvoie le resume de la statistique paquets sous forme de chaine
228 string Summary(bool detail=false) const;
229
230 // Acces aux differents compteurs
231 inline UInt64 NbPaqTotal() { return totnframes; }
232 inline UInt64 NbPaqOK() { return nframeok; }
233 inline UInt64 NbPaqLost() { return lostframes; }
234 inline UInt64 NbGaps() { return cnt_saut; }
235 inline UInt64 LastFrameNum() { return lastframenum; }
236
237protected:
238 UInt64 totnframes; // Nombre totale de frames/paquets traites
239 UInt64 nframeok; // Nombre totale de frames/paquets avec HDR/TRL OK
240 UInt64 lostframes; // Nombre totale de frames/paquets perdus
241 UInt16 frclst; // derniere valeur du frame-counter
242 UInt64 lastframenum; // Dernier numero de frame(=frame-counter, sans modulo 65535)
243
244 bool cktrl_; // Verifie aussi le trailer si true
245 UInt64 cnt_saut; // Nb de fois ou DeltaFrameCounter>1
246 UInt32 maxprt_; // Nb maxi de print paquets perdus / probleme
247
248 UInt64 hdrtag_;
249 UInt64 trltag_;
250};
251
252// Definition de l'operator << overloading - Appel de Print()
253inline ostream& operator << (ostream& s, BRPaqChecker const & chk)
254 { return chk.Print(s); }
255
256#endif
257
258
Note: See TracBrowser for help on using the repository browser.