00001
00002 #ifndef DAQC_API_HH
00003 #define DAQC_API_HH
00004
00005
00006
00007 #include <sys/time.h>
00008 #include <time.h>
00009 #include <vector>
00010 #include <string>
00011 #include <map>
00012 #include "gmutex.hh"
00013
00049
00050 #ifndef DAQD_PORT
00051 #define DAQD_PORT 8088
00052 #endif
00053 #define NDS2_PORT 31200
00054
00065 enum chantype {
00067 cUnknown,
00068
00070 cOnline,
00071
00073 cRaw,
00074
00076 cRDS,
00077
00079 cSTrend,
00080
00082 cMTrend,
00083
00085 cTestPoint
00086
00087 };
00088
00092 enum datatype {
00093 _undefined = 0,
00094 _16bit_integer = 1,
00095 _32bit_integer = 2,
00096 _64bit_integer = 3,
00097 _32bit_float = 4,
00098 _64bit_double = 5,
00099 _32bit_complex = 6
00100 };
00101
00109 struct DAQDChannel {
00111 std::string mName;
00113 chantype mChanType;
00115 double mRate;
00117 datatype mDatatype;
00119 int mBOffset;
00121 int mStatus;
00123 float mGain;
00125 float mSlope;
00127 float mOffset;
00129 std::string mUnit;
00130
00131 DAQDChannel(void)
00132 : mChanType(cUnknown), mRate(0.0), mDatatype(_undefined), mBOffset(0),
00133 mStatus(0), mGain(1.0), mSlope(1.0), mOffset(0.0)
00134 {}
00135
00137 long nwords(double dt) const {
00138 return long(dt*mRate + 0.5);
00139 }
00140
00142 static chantype cvt_str_chantype(const std::string& str);
00143
00145 static const char* cvt_chantype_str(chantype typ);
00146
00148 static datatype cvt_str_datatype(const std::string& str);
00149
00151 static const char* cvt_datatype_str(datatype typ);
00152
00154 static int datatype_size(datatype typ);
00155 };
00156
00161 struct DAQDRecHdr {
00163 int Blen;
00165 int Secs;
00167 int GPS;
00169 int NSec;
00171 int SeqNum;
00172 };
00173
00183 class DAQC_api {
00184 public:
00186 typedef std::vector<DAQDChannel> chan_list;
00187
00189 typedef chan_list::iterator channel_iter;
00190
00192 typedef chan_list::const_iterator const_channel_iter;
00193
00194 typedef double wait_time;
00195
00196 typedef unsigned long count_type;
00197
00198 public:
00201 DAQC_api(void);
00202
00205 virtual ~DAQC_api(void);
00206
00215 virtual int open (const std::string& ipaddr, int ipport,
00216 long buflen = 16384) = 0;
00217
00220 virtual void close() = 0;
00221
00224 virtual void flush() = 0;
00225
00230 virtual bool isOpen(void) const;
00231
00236 virtual bool isOn(void) const;
00237
00242 virtual int AddChannel(const DAQDChannel& chns);
00243
00248 virtual int AddChannel(const std::string& chan, chantype ty, double rate);
00249
00256 virtual int Available(chantype typ, long gps, chan_list& list,
00257 wait_time timeout=-1) = 0;
00258
00261 const_channel_iter chan_begin(void) const;
00262
00265 const_channel_iter chan_end(void) const;
00266
00269 const_channel_iter FindChannel(const std::string& chan) const;
00270
00273 channel_iter FindChannel(const std::string& chan);
00274
00282 virtual int GetChannelData(const std::string& chan, float* data,
00283 long maxlen) const;
00284
00292 virtual int GetData(wait_time timeout = -1);
00293
00297 virtual int GetData(char** buf, wait_time timeout = -1);
00298
00303 virtual unsigned long nRequest(void) const;
00304
00313 virtual int RequestOnlineData (double stride, wait_time timeout) = 0;
00314
00324 virtual int RequestData (unsigned long start, unsigned long duration,
00325 wait_time timeout = -1) = 0;
00326
00331 virtual void RmChannel(const std::string& chan);
00332
00339 virtual void setAbort (bool* abort);
00340
00347 virtual void setDebug(int debug=1);
00348 virtual void setDebug(bool debug=true);
00349
00354 virtual int StopWriter() = 0;
00355
00360 virtual int Times(chantype type, unsigned long& start,
00361 unsigned long& duration, wait_time timeout = -1);
00362
00367 virtual float Version(void) const;
00368
00381 virtual int WaitforData (bool poll = false) = 0;
00382
00383 protected:
00386 void SwapHeader(void);
00387
00390 int SwapData(void);
00391
00392 int CVHex(const char* text, int N);
00393
00402 virtual int RecvData(wait_time timeout=-1) = 0;
00403
00408 virtual int RecvFloat(float& data, wait_time timeout=-1);
00409
00414 virtual int RecvInt(int& data, wait_time timeout=-1);
00415
00421 virtual int RecvRec(char *buf, long len, bool readall=false,
00422 wait_time maxwait=-1) = 0;
00423
00427 virtual int RecvReconfig(count_type len, wait_time maxwait=-1) = 0;
00428
00429
00437 virtual int RecvStr(std::string& str, wait_time maxwait=-1);
00438
00439 public:
00440 class recvBuf {
00441 public:
00442 typedef unsigned long size_type;
00443 public:
00444 recvBuf(size_type length=0);
00445 ~recvBuf(void);
00446 size_type capacity(void) const;
00447 void clear(void);
00448 char* ref_data(void);
00449 const char* ref_data() const;
00450 DAQDRecHdr& ref_header();
00451 const DAQDRecHdr& ref_header() const;
00452 void reserve(size_type length);
00453 private:
00454 size_type mLength;
00455 DAQDRecHdr mHeader;
00456 char* mData;
00457 };
00458
00461 recvBuf mRecvBuf;
00462
00463 protected:
00466 mutable thread::recursivemutex mux;
00467
00470 bool mOpened;
00471
00474 int mDebug;
00475
00481 enum writer_type {
00482 NoWriter,
00483 NameWriter,
00484 DataWriter,
00485 FrameWriter,
00486 FastWriter,
00487 NDS2Writer
00488 } mWriterType;
00489
00492 int mOffline;
00493
00496 int mVersion;
00497
00500 int mRevision;
00501
00504 bool* mAbort;
00505
00508 chan_list mRequest_List;
00509
00510 };
00511
00512
00513 inline DAQC_api::const_channel_iter
00514 DAQC_api::chan_begin(void) const {
00515 return mRequest_List.begin();
00516 }
00517
00518 inline DAQC_api::const_channel_iter
00519 DAQC_api::chan_end(void) const {
00520 return mRequest_List.end();
00521 }
00522
00523 inline bool
00524 DAQC_api::isOpen(void) const {
00525 return mOpened;
00526 }
00527
00528 inline float
00529 DAQC_api::Version(void) const {
00530 return mVersion + 0.01*mRevision;
00531 }
00532
00533 inline bool
00534 DAQC_api::isOn() const {
00535 return mWriterType != NoWriter;
00536 }
00537
00538 inline unsigned long
00539 DAQC_api::nRequest(void) const {
00540 return mRequest_List.size();
00541 }
00542
00543 inline DAQC_api::recvBuf::size_type
00544 DAQC_api::recvBuf::capacity(void) const {
00545 return mLength;
00546 }
00547
00548 inline char*
00549 DAQC_api::recvBuf::ref_data(void) {
00550 return mData;
00551 }
00552
00553 inline const char*
00554 DAQC_api::recvBuf::ref_data() const {
00555 return mData;
00556 }
00557
00558 inline DAQDRecHdr&
00559 DAQC_api::recvBuf::ref_header() {
00560 return mHeader;
00561 }
00562
00563 inline const DAQDRecHdr&
00564 DAQC_api::recvBuf:: ref_header() const {
00565 return mHeader;
00566 }
00567
00570 #endif // DAQSOCKET_HH
00571