00001 #ifndef ROC_MESSAGE_H
00002 #define ROC_MESSAGE_H
00003
00004 #include <stdint.h>
00005 #include <iostream>
00006
00007 namespace roc {
00008
00009
00010 enum MessageFormat {
00011 formatEth1 = 0,
00012 formatOptic1 = 1,
00013 formatEth2 = 2,
00014 formatOptic2 = 3,
00015 formatNormal = 4
00016 };
00017
00018 enum MessageTypes {
00019 MSG_NOP = 0,
00020 MSG_HIT = 1,
00021 MSG_EPOCH = 2,
00022 MSG_SYNC = 3,
00023 MSG_AUX = 4,
00024 MSG_EPOCH2 = 5,
00025 MSG_GET4 = 6,
00026 MSG_SYS = 7
00027 };
00028
00029 enum SysMessageTypes {
00030 SYSMSG_DAQ_START = 1,
00031 SYSMSG_DAQ_FINISH = 2,
00032 SYSMSG_NX_PARITY = 3,
00033 SYSMSG_SYNC_PARITY = 4,
00034 SYSMSG_DAQ_RESUME = 5,
00035 SYSMSG_FIFO_RESET = 6,
00036 SYSMSG_USER = 7,
00037 SYSMSG_PCTIME = 8,
00038 SYSMSG_ADC = 9,
00039 SYSMSG_PACKETLOST = 10,
00040 SYSMSG_GET4_EVENT = 11,
00041 SYSMSG_CLOSYSYNC_ERROR = 12,
00042 SYSMSG_TS156_SYNC = 13
00043
00044 };
00045
00046 enum SysMessageUserTypes {
00047 SYSMSG_USER_CALIBR_ON = 7,
00048 SYSMSG_USER_CALIBR_OFF = 8,
00049 SYSMSG_USER_RECONFIGURE = 9
00050 };
00051
00052 enum MessagePrintMask {
00053 msg_print_Prefix = 1,
00054 msg_print_Data = 2,
00055 msg_print_Hex = 4,
00056 msg_print_Human = 8
00057 };
00058
00059 class Message {
00060
00061 protected:
00062 uint64_t data;
00063
00064 public:
00065 Message() : data(0) {}
00066
00067 Message(const Message& src) : data(src.data) {}
00068
00069 void assign(const Message& src) { data = src.data; }
00070
00071 Message& operator=(const Message& src) { assign(src); return *this; }
00072
00073 inline void reset() { data = 0; }
00074
00075 inline uint32_t getField(uint32_t shift, uint32_t len) const
00076 { return (data >> shift) & ((((uint32_t)1) << len) - 1); }
00077
00078 inline void setField(uint32_t shift, uint32_t len, uint32_t value)
00079 { data = (data & ~(((((uint64_t) 1) << len) - 1) << shift)) | (((uint64_t) value) << shift); }
00080
00081 inline uint8_t getBit(uint32_t shift) const
00082 { return (data >> shift) & 1; }
00083
00084 inline void setBit(uint32_t shift, uint8_t value)
00085 { data = value ? (data | (((uint64_t) 1) << shift)) : (data & ~(((uint64_t) 1) << shift)) ; }
00086
00087
00088
00089
00091 inline uint8_t getMessageType() const { return getField(0, 4); }
00092
00094
00098 inline uint16_t getRocNumber() const { return getField(48, 16); }
00099
00101 inline void setMessageType(uint8_t v) { setField(0, 4, v); }
00102
00104 inline void setRocNumber(uint16_t v) { setField(48, 16, v); }
00105
00106
00107
00108
00110
00116 inline uint8_t getNxNumber() const { return getField(6, 2); }
00117
00119
00128 inline uint8_t getNxLtsMsb() const { return getField(8, 3); }
00129
00131
00138 inline uint16_t getNxTs() const { return getField(11, 14); }
00139
00141 inline uint8_t getNxChNum() const { return getField(25, 7); }
00142
00143
00144
00146 inline uint16_t getNxAdcValue() const { return getField(33, 12); }
00147
00149
00155 inline uint8_t getNxPileup() const { return getBit(45); }
00156
00158
00164 inline uint8_t getNxOverflow() const { return getBit(46); }
00165
00167
00172 inline uint8_t getNxLastEpoch() const { return getBit(47); }
00173
00174
00176 inline void setNxNumber(uint8_t v) { setField(6, 2, v); }
00177
00179 inline void setNxLtsMsb(uint8_t v) { setField(8, 3, v); }
00180
00182 inline void setNxTs(uint16_t v) { setField(11, 14, v); }
00183
00185 inline void setNxChNum(uint8_t v) { setField(25, 7, v); }
00186
00187
00188
00190 inline void setNxAdcValue(uint16_t v) { setField(33, 12, v); }
00191
00193 inline void setNxPileup(uint8_t v) { setBit(45, v); }
00194
00196 inline void setNxOverflow(uint8_t v) { setBit(46,v ); }
00197
00199 inline void setNxLastEpoch(uint8_t v) { setBit(47, v); }
00200
00201
00202
00203
00204
00206
00207
00208 inline uint32_t getEpochNumber() const { return data >> 8; }
00209
00210
00212 inline uint8_t getEpochMissed() const { return getField(40, 8); }
00213
00215 inline void setEpochNumber(uint32_t v) { setField(8, 32, v); }
00216
00218 inline void setEpochMissed(uint8_t v) { setField(40, 8, v); }
00219
00220
00221
00222
00224 inline uint8_t getSyncChNum() const { return getField(6, 2); }
00225
00227 inline uint16_t getSyncTs() const { return getField(8, 13) << 1; }
00228
00230 inline uint8_t getSyncEpochLSB() const { return getBit(21); }
00231
00233 inline uint32_t getSyncData() const { return getField(22, 24); }
00234
00236 inline uint8_t getSyncStFlag() const { return getField(46, 2); }
00237
00238
00240 inline void setSyncChNum(uint8_t v) { setField(6, 2, v); }
00241
00243 inline void setSyncTs(uint16_t v) { setField(8, 13, v >> 1); }
00244
00246 inline void setSyncEpochLSB(uint8_t v) { setBit(21, v); }
00247
00249 inline void setSyncData(uint32_t v) { setField(22, 24, v); }
00250
00252 inline void setSyncStFlag(uint8_t v) { setField(46, 2, v); }
00253
00254
00255
00256
00258 inline uint8_t getAuxChNum() const { return getField(6, 7); }
00259
00261 inline uint16_t getAuxTs() const { return getField(13, 13) << 1; }
00262
00264 inline uint8_t getAuxEpochLSB() const { return getBit(26); }
00265
00267 inline uint8_t getAuxFalling() const { return getBit(27); }
00268
00270 inline uint8_t getAuxOverflow() const { return getBit(28); }
00271
00272
00274 inline void setAuxChNum(uint8_t v) { setField(6, 7, v); }
00275
00277 inline void setAuxTs(uint16_t v) { setField(13, 13, v >> 1); }
00278
00280 inline void setAuxEpochLSB(uint8_t v) { setBit(26, v); }
00281
00283 inline void setAuxFalling(uint8_t v) { setBit(27, v); }
00284
00286 inline void setAuxOverflow(uint8_t v) { setBit(28, v); }
00287
00288
00289
00292 inline uint32_t getEpoch2EpochMissmatch() const { return getBit(4); }
00293
00295 inline uint32_t getEpoch2EpochLost() const { return getBit(5); }
00296
00298 inline uint32_t getEpoch2DataLost() const { return getBit(6); }
00299
00301 inline uint32_t getEpoch2Sync() const { return getBit(7); }
00302
00306 inline uint32_t getEpoch2StampTime() const { return getField(8, 4); }
00307
00309
00310
00311
00312 inline uint32_t getEpoch2Number() const { return data >> 12; }
00313
00316 inline uint32_t getEpoch2ChipNumber() const { return getField(44, 4); }
00317
00320 inline uint32_t getEpoch2CRC() const { return getField(40, 8); }
00321
00323 inline void setEpoch2EpochMissmatch(uint32_t v) { setBit(4, v); }
00324
00326 inline void setEpoch2EpochLost(uint32_t v) { setBit(5, v); }
00327
00329 inline void setEpoch2DataLost(uint32_t v) { setBit(6, v); }
00330
00332 inline void setEpoch2Sync(uint32_t v) { setBit(7, v); }
00333
00337 inline void setEpoch2StampTime(uint32_t v) { setField(8, 4, v); }
00338
00340 inline void setEpoch2Number(uint32_t v) { setField(12, 32, v); }
00341
00344 inline void setEpoch2ChipNumber(uint32_t v) { setField(44, 4, v); }
00345
00348 inline void setEpoch2CRC(uint32_t v) { setField(40, 8, v); }
00349
00350
00351
00353 inline uint8_t getGet4Number() const { return getField(6, 6); }
00354
00356 inline uint8_t getGet4ChNum() const { return getField(12, 2); }
00357
00359 inline uint32_t getGet4Ts() const { return getField(14, 19); }
00360
00362 inline uint32_t getGet4Edge() const { return getBit(33); }
00363
00366 inline uint32_t getGet4CRC() const { return getField(40, 8); }
00367
00368
00370 inline void setGet4Number(uint8_t v) { setField(6, 6, v); }
00371
00373 inline void setGet4ChNum(uint8_t v) { setField(12, 2, v); }
00374
00376 inline void setGet4Ts(uint32_t v) { setField(14, 19, v); }
00377
00379 inline void setGet4Edge(uint32_t v) { setBit(33, v); }
00380
00383 inline void setGet4CRC(uint32_t v) { setField(40, 8, v); }
00384
00385
00386
00387
00388
00390 inline uint8_t getSysMesType() const { return getField(8, 8); }
00391
00393
00394
00395 inline uint32_t getSysMesData() const { return data >> 16; }
00396
00398 inline void setSysMesType(uint8_t v) { setField(8, 8, v); }
00399
00401 inline void setSysMesData(uint32_t v) { setField(16, 32, v); }
00402
00403
00404
00405
00407 inline bool isNopMsg() const { return getMessageType() == MSG_NOP; }
00409 inline bool isHitMsg() const { return getMessageType() == MSG_HIT; }
00411 inline bool isEpochMsg() const { return getMessageType() == MSG_EPOCH;}
00413 inline bool isSyncMsg() const { return getMessageType() == MSG_SYNC; }
00415 inline bool isAuxMsg() const { return getMessageType() == MSG_AUX; }
00417 inline bool isEpoch2Msg() const { return getMessageType() == MSG_EPOCH2;}
00419 inline bool isGet4Msg() const { return getMessageType() == MSG_GET4; }
00421 inline bool isSysMsg() const { return getMessageType() == MSG_SYS; }
00422
00424 inline bool isStartDaqMsg() const
00425 { return isSysMsg() && (getSysMesType() == SYSMSG_DAQ_START); }
00427 inline bool isStopDaqMsg() const
00428 { return isSysMsg() && (getSysMesType() == SYSMSG_DAQ_FINISH); }
00429
00430
00431 void printData(unsigned kind = msg_print_Prefix | msg_print_Data, uint32_t epoch = 0) const;
00432
00433 void printData(std::ostream& os, unsigned kind = msg_print_Human, uint32_t epoch = 0) const;
00434
00435 uint64_t getMsgFullTime(uint32_t epoch) const;
00436
00437 double getMsgFullTimeD(uint32_t epoch) const;
00438
00440 inline static uint64_t FullTimeStamp(uint32_t epoch, uint16_t stamp)
00441 { return ((uint64_t) epoch << 14) | (stamp & 0x3fff); }
00442
00444 inline static uint64_t FullTimeStamp2(uint32_t epoch, uint32_t stamp)
00445 { return ((uint64_t) epoch << 19) | (stamp & 0x7ffff); }
00446
00447
00448 static uint64_t CalcDistance(uint64_t start, uint64_t stop);
00449
00450 static double CalcDistanceD(double start, double stop);
00451
00452
00453
00454 static uint32_t RawSize(int fmt);
00455
00456 bool convertFromOld(void* src);
00457
00458 bool convertToOld(void* tgt);
00459
00460 bool assign(void* src, int fmt = formatNormal);
00461
00462 bool copyto(void* tgt, int fmt = formatNormal);
00463 };
00464
00465 }
00466
00467
00468 #endif