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

Last change on this file since 3709 was 3694, checked in by ansari, 16 years ago

petites optimisations et corrections, Reza 01/12/2009

File size: 10.6 KB
RevLine 
[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
9#include <stdio.h>
10#include <iostream>
[3671]11#include <exception>
[3537]12#include <string>
13#include "brtypes.h"
14
[3592]15
[3537]16using namespace std;
17
[3671]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
[3623]36// ------------------------------------------------
37// ----- Classe TwoByteComplex ------
[3592]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
[3659]52 inline SByte& realB() { return val_[0]; }
53 inline SByte& imagB() { return val_[1]; }
[3592]54
[3659]55 inline int realI() { return (int)(val_)[0]; }
56 inline int imagI() { return (int)(val_)[1]; }
[3592]57
[3659]58 inline double realD() { return (double)(val_)[0]; }
[3671]59 inline double imagD() { return (double)(val_)[1]; }
[3592]60
[3659]61 SByte val_[2];
[3592]62};
63
[3671]64// --------------------------------------------------------
65// ----- Classe BRPaquet , constante et enum associe ------
66// Representation, manipulation des paquets/frames
67// envoyes par les cartes ADC
68// --------------------------------------------------------
[3623]69
70// OFFSET : au cas ou le firmware reception ajoute des mots avant le header
[3592]71#define OFFSET 0
[3537]72// Taille entete/trailer en octets
73#define BRHDRSIZE 24
74// Actuellement le trailer est vire ...
[3592]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 */
[3537]82// Offset du time-tag
[3592]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
[3537]92
[3671]93enum ChannelID { None=0, Ch1, Ch2, Ch1_2, Ch3, Ch4, Ch3_4 };
94enum ModeAcq { RawData=1, FFT };
[3592]95
96// Definition des actions sur la conversion de format (swap...) des donnees arrivant
97enum BRDataFmtConv {
[3671]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,
[3592]102};
103
[3671]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
[3537]111// Structure correspondant a HEADER-DATA-TRAILER
112class BRPaquet {
113 public:
[3623]114// Cree d'un objet BRPaquet avec copie/swap depuis src -> dst (si src != NULL)
[3671]115 BRPaquet(Byte* src, Byte* dst, int paqsz, BRDataFmtConv fgconv=BR_Copy);
[3658]116 // Cree d'un objet BRPaquet de taille paqsz sur la zone dst
[3671]117 BRPaquet(Byte* srcdst, int paqsz);
[3694]118 BRPaquet(int paqsz=16424); // Set aussi de constructeur par defaut
[3683]119 BRPaquet(BRPaquet const& paq);
[3671]120
[3658]121 // ~BRPaquet();
[3683]122 inline void Set(Byte* dst) { dst_=dst; return; }
123 inline void Set(Byte* dst, int paqsz) { dst_=dst; sz_=paqsz; return; }
[3537]124
[3671]125 // Pour la copie/reduction de taille de paquet
126 void CopyFrom(BRPaquet& pq, BRPaqReducAction ract=BR_CopyRA, int offset=0);
127
[3537]128 // Acces diverses tailles
129 inline int PaquetSize() { return sz_; }
130 inline int DataSize() { return sz_-(BRHDRSIZE+BRTRLSIZE); }
131 inline int HeaderSize() { return BRHDRSIZE; }
132 inline int TrailerSize() { return BRTRLSIZE; }
133
134 // Acces differentes zone memoire
[3623]135 inline Byte* Begin() { return dst_+OFFSET; }
[3592]136 inline Byte* Data1() { return dst_+BRHDRSIZE+OFFSET; }
137 inline Byte* Data2() { return dst_+BRHDRSIZE+(DataSize()/2)+OFFSET; }
138 inline Byte* Header() { return dst_+OFFSET; }
139 inline Byte* Trailer() { return (dst_+sz_-BRTRLSIZE+OFFSET); }
[3659]140
141 // Acces aux differentes zone de donnees en signed byte (-127 ... 127), donnees FFT
142 inline SByte* BeginS() { return (SByte*)(dst_+OFFSET); }
143 inline SByte* Data1S() { return (SByte*)(dst_+BRHDRSIZE+OFFSET); }
144 inline SByte* Data2S() { return (SByte*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
145
146 // Acces aux differentes zone de donnees en TwoByteComplex pour donnees FFT
147 inline int DataSizeC() { return (sz_-(BRHDRSIZE+BRTRLSIZE))/2; }
148 inline TwoByteComplex* BeginC() { return (TwoByteComplex*)(dst_+OFFSET); }
149 inline TwoByteComplex* Data1C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+OFFSET); }
150 inline TwoByteComplex* Data2C() { return (TwoByteComplex*)(dst_+BRHDRSIZE+(DataSize()/2)+OFFSET); }
151
[3537]152 // Valeurs differentes zones HDR/TRL
[3592]153 inline UInt32 HDRMarker() {return *((UInt32*)(dst_+OFFSET));}
[3623]154 inline UInt32 HDRMarker2() {return *((UInt32*)(dst_+OFFSET+1));}
155 inline UInt64 HDRMarker64() {return *((UInt64*)(dst_+OFFSET));}
156
[3592]157 inline UInt32 TRLMarker() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
[3623]158 inline UInt32 TRLMarker2() {return *((UInt32*)(dst_+(sz_-BRTRLSIZE+OFFSET+1)));}
159 inline UInt64 TRLMarker64() {return *((UInt64*)(dst_+(sz_-BRTRLSIZE+OFFSET)));}
160
161// Informations diverses sur le paquet/mode d'acquisition
[3592]162 inline UInt16 ModeAcq() {return *((UInt16*)(dst_+(BRMODACQOFF+OFFSET)));}
163 inline UInt16 ChanId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0x1800)>> 12) ;}
164 inline UInt16 ChipId() {return (( *((UInt16*)(dst_+(BRCHANIDOFF+OFFSET))) & 0xC00)>> 10) ;}
[3537]165
[3623]166 inline UInt32 FrameCounter() {return ((*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF0000) >> 16);}
[3592]167 inline UInt32 TimeTag1() {return *((UInt32*)(dst_+(BRTMTAGOFF+OFFSET)));}
168 inline UInt32 TimeTag2() {return (*((UInt32*)(dst_+(BRFRCPTOFF+OFFSET))) &0xFFFF);}
[3635]169 inline UInt64 TimeTag() {return (*((UInt64*)(dst_+(BRTMTAGOFF+OFFSET))) &0x0000FFFFFFFFFFFFULL);}
[3592]170
171 inline UInt32 PaqId() {return *((UInt32*)(dst_+(BRPKTIDOFF+OFFSET)));}
172 inline UInt16 PaqLen() {return *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET)));}
173
[3623]174 UInt16 ChannelID();
175 UInt16 ModeAcquisition();
[3592]176
177 // inline unsigned short PositionChariot() {return *((unsigned short*)(dst_+BRPCOFF+OFFSET));}
[3623]178 // Fonctions utiles pour remplir un objet BRPaquet
179 void SetHDRMarker64(UInt64 htag);
180 void SetTRLMarker64(UInt64 ttag);
181 void SetFrameCounter(UInt32 fc);
182 void SetTimeTag(UInt64 timtag);
183 inline void SetPaqLen(UInt16 len) { *((UInt16*)(dst_+(BRPKTLENOFF+OFFSET))) = len; }
[3592]184
[3537]185 // pour faire un print de la structure
[3623]186 ostream& Print(ostream & os, int nelt=8, bool prht=true);
187 inline ostream& Print(int nelt=8, bool prht=true)
188 { return Print(cout, nelt, prht); }
[3537]189
[3592]190 // fonction appelee par le constructeur pour reordonner les donnees FFT
[3671]191 // --- Remise en ordre avec swap complet sur 8 bytes
192 static void ReorderFFTDataSwapAll(SByte* src, SByte* dst, int sz);
193 // --- Remise en ordre avec echanges des mots de 4 bytes
194 static void ReorderFFTDataSwap32(SByte* src, SByte* dst, int sz);
195 // --- Remise en ordre seulement
[3659]196 static void ReorderFFTData(SByte* src, SByte* dst, int sz);
[3671]197 static const char* FmtConvToString(BRDataFmtConv fgconv);
[3674]198 static const char* ReducActionToString(BRPaqReducAction rac);
199
[3592]200// protected:
[3537]201 // donnees membres
202 int sz_; //taille du paquet
203 Byte* dst_;
[3658]204
[3537]205};
206
[3592]207// --------------------------------------------------------------------------
208// Classe pour effectuer des verifications d'integrite sur les paquets/frames
209// --------------------------------------------------------------------------
210
211class BRPaqChecker {
212public:
[3659]213 // if cktrl==true, check header AND trailer, ==false check header only
[3640]214 BRPaqChecker(bool cktrl=true, int maxprt=0);
[3683]215 BRPaqChecker(BRPaqChecker const& pck);
216
[3592]217 ~BRPaqChecker();
[3623]218
[3683]219 inline bool SetCheckTrailerFlag(bool cktrl=true) { cktrl_=cktrl; return cktrl_; }
220 inline int SetMaxPrint(int maxprt=0) { maxprt=maxprt_; return maxprt_; }
221
[3623]222 UInt64 DefineHDRTag(UInt32 hdr1=0x76543210, UInt32 hdr2=0xFEDCBA98);
223 UInt64 DefineTRLTag(UInt32 trl1=0x55555555, UInt32 trl2=0xAAAAAAAA);
224
225 inline UInt64 HDRTag() { return hdrtag_; }
226 inline UInt64 TRLTag() { return trltag_; }
227
[3592]228 // Verifie le paquet, renvoie true si OK
[3659]229 bool Check(BRPaquet& paq, UInt64& numframe);
230 inline bool Check(BRPaquet& paq) { UInt64 nf; return Check(paq, nf); }
231
[3592]232 // Imprime le compte de paquets ...
[3640]233 ostream & Print(ostream& os) const;
234 inline ostream & Print() const { return Print(cout); }
[3671]235 // renvoie le resume de la statistique paquets sous forme de chaine
236 string Summary(bool detail=false) const;
[3592]237
[3659]238 // Acces aux differents compteurs
239 inline UInt64 NbPaqTotal() { return totnframes; }
240 inline UInt64 NbPaqOK() { return nframeok; }
241 inline UInt64 NbPaqLost() { return lostframes; }
242 inline UInt64 NbGaps() { return cnt_saut; }
243 inline UInt64 LastFrameNum() { return lastframenum; }
[3623]244
[3659]245protected:
246 UInt64 totnframes; // Nombre totale de frames/paquets traites
[3683]247 UInt64 nframeok; // Nombre totale de frames/paquets avec HDR/TRL OK
[3659]248 UInt64 lostframes; // Nombre totale de frames/paquets perdus
249 UInt16 frclst; // derniere valeur du frame-counter
250 UInt64 lastframenum; // Dernier numero de frame(=frame-counter, sans modulo 65535)
251
[3640]252 bool cktrl_; // Verifie aussi le trailer si true
[3659]253 UInt64 cnt_saut; // Nb de fois ou DeltaFrameCounter>1
254 UInt32 maxprt_; // Nb maxi de print paquets perdus / probleme
[3640]255
[3623]256 UInt64 hdrtag_;
257 UInt64 trltag_;
[3592]258};
259
[3659]260// Definition de l'operator << overloading - Appel de Print()
[3640]261inline ostream& operator << (ostream& s, BRPaqChecker const & chk)
262 { return chk.Print(s); }
263
[3537]264#endif
[3592]265
266
Note: See TracBrowser for help on using the repository browser.