1 #include "otsdaq/FECore/FESlowControlsChannel.h"
2 #include "otsdaq/Macros/BinaryStringMacros.h"
3 #include "otsdaq/Macros/CoutMacros.h"
12 #define __MF_SUBJECT__ "SlowControls"
13 #define mfSubject_ (interfaceUID_ + "-" + channelName_)
31 FESlowControlsChannel::FESlowControlsChannel(
const std::string& interfaceUID,
32 const std::string& channelName,
33 const std::string& dataType,
34 unsigned int universalDataSize,
35 unsigned int universalAddressSize,
36 const std::string& universalAddress,
37 unsigned int universalDataBitOffset,
40 bool monitoringEnabled,
41 bool recordChangesOnly,
42 time_t delayBetweenSamples,
44 const std::string& savePath,
45 const std::string& saveFileRadix,
46 bool saveBinaryFormat,
49 const std::string& lolo,
50 const std::string& lo,
51 const std::string& hi,
52 const std::string& hihi)
53 : interfaceUID_(interfaceUID)
54 , channelName_(channelName)
56 , fullChannelName_(interfaceUID_ +
":" + channelName_)
58 , universalDataBitOffset_(universalDataBitOffset)
59 , txPacketSequenceNumber_(0)
60 , readAccess_(readAccess)
61 , writeAccess_(writeAccess)
62 , monitoringEnabled_(monitoringEnabled)
63 , recordChangesOnly_(recordChangesOnly)
64 , delayBetweenSamples_(delayBetweenSamples < 1 ? 1 : delayBetweenSamples)
65 , saveEnabled_(saveEnabled)
67 , saveFileRadix_(saveFileRadix)
68 , saveBinaryFormat_(saveBinaryFormat)
69 , alarmsEnabled_(alarmsEnabled)
70 , latchAlarms_(latchAlarms)
76 , saveFullFileName_(savePath_ +
"/" + saveFileRadix_ +
"-" + underscoreString(fullChannelName_) +
"-" + std::to_string(time(0)) +
77 (saveBinaryFormat_ ?
".dat" :
".txt"))
79 __GEN_COUTV__(dataType_);
80 __GEN_COUT__ <<
"universalAddressSize = " << universalAddressSize << __E__;
81 __GEN_COUT__ <<
"universalAddress = " << universalAddress << __E__;
93 if(dataType_[dataType_.size() - 1] ==
'b')
94 sscanf(&dataType_[0],
"%u", &sizeOfDataTypeBits_);
95 else if(dataType_ ==
"char" || dataType_ ==
"unsigned char")
96 sizeOfDataTypeBits_ =
sizeof(char) * 8;
97 else if(dataType_ ==
"short" || dataType_ ==
"unsigned short")
98 sizeOfDataTypeBits_ =
sizeof(short) * 8;
99 else if(dataType_ ==
"int" || dataType_ ==
"unsigned int")
100 sizeOfDataTypeBits_ =
sizeof(int) * 8;
101 else if(dataType_ ==
"long long" || dataType_ ==
"unsigned long long")
102 sizeOfDataTypeBits_ =
sizeof(
long long) * 8;
103 else if(dataType_ ==
"float")
104 sizeOfDataTypeBits_ =
sizeof(float) * 8;
105 else if(dataType_ ==
"double")
106 sizeOfDataTypeBits_ =
sizeof(double) * 8;
109 __GEN_SS__ <<
"ChannelDataType '" << dataType_ <<
"' is invalid. "
110 <<
"Valid data types (w/size in bytes) are as follows: "
112 <<
", char (" <<
sizeof(char) <<
"B), unsigned char (" <<
sizeof(
unsigned char) <<
"B), short (" <<
sizeof(short) <<
"B), unsigned short ("
113 <<
sizeof(
unsigned short) <<
"B), int (" <<
sizeof(int) <<
"B), unsigned int (" <<
sizeof(
unsigned int) <<
"B), long long ("
114 <<
sizeof(
long long) <<
"B), unsigned long long (" <<
sizeof(
unsigned long long) <<
"B), float (" <<
sizeof(float) <<
"B), double ("
115 <<
sizeof(
double) <<
"B)." << __E__;
116 __GEN_COUT_ERR__ <<
"\n" << ss.str();
120 if(sizeOfDataTypeBits_ > 64)
122 __GEN_SS__ <<
"Invalid Data Type '" << dataType_ <<
"' (" << sizeOfDataTypeBits_
124 ". Size in bits must be less than or equal to 64-bits."
126 __GEN_COUT_ERR__ <<
"\n" << ss.str();
130 if(universalDataSize * 8 < sizeOfDataTypeBits_)
132 __GEN_SS__ <<
"Invalid Data Type '" << dataType_ <<
"' (" << sizeOfDataTypeBits_ <<
"-bits) or Universal Data Size of " << universalDataSize * 8
133 <<
"-bits. Data Type size must be less than or equal to Universal Data Size." << __E__;
134 __GEN_COUT_ERR__ <<
"\n" << ss.str();
138 universalAddress_.resize(universalAddressSize);
141 convertStringToBuffer(universalAddress, universalAddress_);
143 catch(
const std::runtime_error& e)
145 __GEN_SS__ <<
"Failed to extract universalAddress '" << universalAddress <<
"'..." << __E__;
150 sizeOfDataTypeBytes_ = (sizeOfDataTypeBits_ / 8 + ((universalDataBitOffset_ % 8) ? 1 : 0));
152 lolo_.resize(sizeOfDataTypeBytes_);
153 lo_.resize(sizeOfDataTypeBytes_);
154 hi_.resize(sizeOfDataTypeBytes_);
155 hihi_.resize(sizeOfDataTypeBytes_);
161 convertStringToBuffer(lolo, lolo_,
true);
163 catch(
const std::runtime_error& e)
165 __GEN_SS__ <<
"Failed to extract lolo '" << lolo <<
"'..." << __E__;
171 convertStringToBuffer(lo, lo_,
true);
173 catch(
const std::runtime_error& e)
175 __GEN_SS__ <<
"Failed to extract lo '" << lo <<
"'..." << __E__;
181 convertStringToBuffer(hi, hi_,
true);
183 catch(
const std::runtime_error& e)
185 __GEN_SS__ <<
"Failed to extract hi '" << hi <<
"'..." << __E__;
191 convertStringToBuffer(hihi, hihi_,
true);
193 catch(
const std::runtime_error& e)
195 __GEN_SS__ <<
"Failed to extract hihi '" << hihi <<
"'..." << __E__;
202 sample_.resize(sizeOfDataTypeBytes_);
203 lastSample_.resize(sizeOfDataTypeBytes_);
207 __GEN_COUT__ <<
"Constructed." << __E__;
211 FESlowControlsChannel::~FESlowControlsChannel(
void) {}
214 void FESlowControlsChannel::print(std::ostream& out)
const
216 out <<
"Slow Controls Channel for Interface '" << interfaceUID_ <<
"': " << channelName_ << __E__;
219 <<
"dataType_: " << dataType_ << __E__;
221 <<
"sizeOfDataTypeBits_: " << sizeOfDataTypeBits_ << __E__;
223 <<
"universalAddress_: " << BinaryStringMacros::binaryNumberToHexString(universalAddress_,
"0x",
" ") << __E__;
225 <<
"universalDataBitOffset_: " << universalDataBitOffset_ << __E__;
227 <<
"readAccess_: " << readAccess_ << __E__;
229 <<
"writeAccess_: " << writeAccess_ << __E__;
231 <<
"monitoringEnabled_: " << monitoringEnabled_ << __E__;
233 <<
"recordChangesOnly_: " << recordChangesOnly_ << __E__;
235 <<
"delayBetweenSamples_: " << delayBetweenSamples_ << __E__;
237 <<
"saveEnabled_: " << saveEnabled_ << __E__;
239 <<
"savePath_: " << savePath_ << __E__;
241 <<
"saveFileRadix_: " << saveFileRadix_ << __E__;
243 <<
"saveBinaryFormat_: " << saveBinaryFormat_ << __E__;
245 <<
"alarmsEnabled_: " << alarmsEnabled_ << __E__;
247 <<
"latchAlarms_: " << latchAlarms_ << __E__;
249 <<
"savePath_: " << savePath_ << __E__;
251 <<
"saveFullFileName_: " << saveFullFileName_ << __E__;
258 std::string FESlowControlsChannel::underscoreString(
const std::string& str)
261 retStr.reserve(str.size());
262 for(
unsigned int i = 0; i < str.size(); ++i)
263 if((str[i] >=
'a' && str[i] <=
'z') || (str[i] >=
'A' && str[i] <=
'Z') || (str[i] >=
'0' && str[i] <=
'9'))
264 retStr.push_back(str[i]);
266 retStr.push_back(
'_');
275 void FESlowControlsChannel::convertStringToBuffer(
const std::string& inString, std::string& buffer,
bool useDataType )
277 __GEN_COUT__ <<
"Input Str Sz= \t" << inString.size() << __E__;
278 __GEN_COUT__ <<
"Input Str Val= \t'" << inString <<
"'" << __E__;
279 __GEN_COUT__ <<
"Output buffer Sz= \t" << buffer.size() << __E__;
281 if(useDataType && (dataType_ ==
"float" || dataType_ ==
"double"))
283 __GEN_COUT__ <<
"Floating point spec'd" << __E__;
284 if(dataType_ ==
"float" && buffer.size() ==
sizeof(float))
286 sscanf(&inString[0],
"%f", (
float*)&buffer[0]);
287 __GEN_COUT__ <<
"float: " << *((
float*)&buffer[0]) << __E__;
289 else if(dataType_ ==
"double" && buffer.size() ==
sizeof(double))
291 sscanf(&inString[0],
"%lf", (
double*)&buffer[0]);
292 __GEN_COUT__ <<
"double: " << *((
double*)&buffer[0]) << __E__;
296 __GEN_SS__ <<
"Invalid floating point spec! "
297 <<
"dataType_=" << dataType_ <<
" buffer.size()=" << buffer.size() << __E__;
298 __GEN_COUT_ERR__ <<
"\n" << ss.str();
304 for(
int i = (
int)buffer.size() - 1; i >= 0; --i)
305 ss << std::hex << (
int)((buffer[i] >> 4) & 0xF) << (int)((buffer[i]) & 0xF) <<
" " << std::dec;
307 __GEN_COUT__ <<
"\n" << ss.str();
313 unsigned long long val;
314 if(!StringMacros::getNumber(inString, val))
316 __GEN_SS__ <<
"Invalid unsigned number format in string " << inString << __E__;
323 for(; i <
sizeof(
long long) && i < buffer.size(); ++i)
324 buffer[i] = ((
char*)&val)[i];
327 for(; i < buffer.size(); ++i)
330 __GEN_COUT__ <<
"Resulting Number Buffer: " << BinaryStringMacros::binaryNumberToHexString(buffer,
"0x",
" ") << __E__;
334 for(
unsigned int i = 0; i < buffer.size(); ++i)
348 if(inString.size() > 2 && inString[0] ==
'0' && inString[1] ==
'x')
352 __GEN_COUT__ <<
"Hex." << __E__;
356 for(
unsigned int i = 0; i < inString.size(); ++i)
358 j = (inString.size() - 1 - i);
359 if(inString[i] >=
'0' && inString[i] <=
'9')
360 val = inString[i] - 48;
361 else if(inString[i] >=
'A' && inString[i] <=
'F')
362 val = inString[i] - 55;
363 else if(inString[i] >=
'a' && inString[i] <=
'f')
364 val = inString[i] - 87;
368 buffer[j / 2] |= val << ((j % 2) * 4);
375 __GEN_COUT__ <<
"Decimal." << __E__;
376 unsigned long long val;
378 if(!useDataType || dataType_[0] ==
'u')
380 sscanf(&inString[0],
"%llu", &val);
384 sscanf(&inString[0],
"%lld", (
long long*)&val);
387 for(
unsigned int i = 0; i <
sizeof(
long long) && i < buffer.size(); ++i)
388 buffer[i] = ((
char*)&val)[i];
395 for(
int i = (
int)buffer.size() - 1; i >= 0; --i)
396 ss << std::hex << (
int)((buffer[i] >> 4) & 0xF) << (int)((buffer[i]) & 0xF) <<
" " << std::dec;
398 __GEN_COUT__ <<
"\n" << ss.str();
405 void FESlowControlsChannel::handleSample(
const std::string& universalReadValue, std::string& txBuffer, FILE* fpAggregate,
bool aggregateIsBinaryFormat)
407 __GEN_COUT__ <<
"txBuffer size=" << txBuffer.size() << __E__;
411 extractSample(universalReadValue);
428 if(recordChangesOnly_)
430 if(lastSampleTime_ && lastSample_ == sample_)
432 __GEN_COUT__ <<
"no change." << __E__;
437 __GEN_COUT__ <<
"new value!" << __E__;
440 lastSampleTime_ = time(0);
441 lastSample_ = sample_;
448 if(monitoringEnabled_)
460 __GEN_COUT__ <<
"before txBuffer sz=" << txBuffer.size() << __E__;
461 txBuffer.push_back(0);
462 txBuffer.push_back(txPacketSequenceNumber_++);
464 txBuffer.resize(txBuffer.size() +
sizeof(lastSampleTime_));
465 memcpy(&txBuffer[txBuffer.size() -
sizeof(lastSampleTime_)] , &lastSampleTime_ ,
sizeof(lastSampleTime_));
467 unsigned int tmpSz = fullChannelName_.size();
469 txBuffer.resize(txBuffer.size() +
sizeof(tmpSz));
470 memcpy(&txBuffer[txBuffer.size() -
sizeof(tmpSz)] , &tmpSz ,
sizeof(tmpSz));
472 txBuffer += fullChannelName_;
474 txBuffer.push_back((
unsigned char)sample_.size());
475 txBuffer.push_back((
unsigned char)sizeOfDataTypeBits_);
478 __GEN_COUT__ <<
"after txBuffer sz=" << txBuffer.size() << __E__;
481 __GEN_SS__ <<
"txBuffer: \n";
482 for(
unsigned int i = 0; i < txBuffer.size(); ++i)
484 ss << std::hex << (int)((txBuffer[i] >> 4) & 0xF) << (int)((txBuffer[i]) & 0xF) <<
" " << std::dec;
489 __GEN_COUT__ <<
"\n" << ss.str();
495 alarmMask = checkAlarms(txBuffer);
498 std::string* alarmValueArray[] = {&lolo_, &lo_, &hi_, &hihi_};
506 if(aggregateIsBinaryFormat)
516 __GEN_COUT__ <<
"Aggregate Binary File Format: " <<
sizeof(lastSampleTime_) <<
" " << sample_.size() << __E__;
519 fwrite(&lastSampleTime_,
sizeof(lastSampleTime_), 1,
522 unsigned int tmpSz = fullChannelName_.size();
523 fwrite(&tmpSz,
sizeof(tmpSz), 1, fpAggregate);
524 fwrite(&fullChannelName_[0], fullChannelName_.size(), 1,
527 unsigned char tmpChar = (
unsigned char)sample_.size();
528 fwrite(&tmpChar, 1, 1, fpAggregate);
530 tmpChar = (
unsigned char)sizeOfDataTypeBits_;
531 fwrite(&tmpChar, 1, 1, fpAggregate);
532 fwrite(&sample_[0], sample_.size(), 1, fpAggregate);
537 for(time_t i = 1; i < 5; ++i, alarmMask >>= 1)
542 sizeof(lastSampleTime_),
547 unsigned int tmpSz = fullChannelName_.size();
548 fwrite(&tmpSz,
sizeof(tmpSz), 1, fpAggregate);
549 fwrite(&fullChannelName_[0], fullChannelName_.size(), 1,
552 unsigned char tmpChar = (
unsigned char)sample_.size();
553 fwrite(&tmpChar, 1, 1, fpAggregate);
555 tmpChar = (
unsigned char)sizeOfDataTypeBits_;
556 fwrite(&tmpChar, 1, 1, fpAggregate);
557 fwrite(&(*alarmValueArray[i - 1])[0], (*alarmValueArray[i - 1]).size(), 1,
569 __GEN_COUT__ <<
"Aggregate Text File Format: " << dataType_ << __E__;
571 fprintf(fpAggregate,
"%lu\n", lastSampleTime_);
572 fprintf(fpAggregate,
"%s\n", fullChannelName_.c_str());
574 if(dataType_[dataType_.size() - 1] ==
'b')
576 std::stringstream ss;
578 for(
unsigned int i = 0; i < sample_.size(); ++i)
579 ss << std::hex << (
int)((sample_[i] >> 4) & 0xF) << (int)((sample_[i]) & 0xF) << std::dec;
580 fprintf(fpAggregate,
"%s\n", ss.str().c_str());
582 else if(dataType_ ==
"char")
583 fprintf(fpAggregate,
"%d\n", *((
char*)(&sample_[0])));
584 else if(dataType_ ==
"unsigned char")
585 fprintf(fpAggregate,
"%u\n", *((
unsigned char*)(&sample_[0])));
586 else if(dataType_ ==
"short")
587 fprintf(fpAggregate,
"%d\n", *((
short*)(&sample_[0])));
588 else if(dataType_ ==
"unsigned short")
589 fprintf(fpAggregate,
"%u\n", *((
unsigned short*)(&sample_[0])));
590 else if(dataType_ ==
"int")
591 fprintf(fpAggregate,
"%d\n", *((
int*)(&sample_[0])));
592 else if(dataType_ ==
"unsigned int")
593 fprintf(fpAggregate,
"%u\n", *((
unsigned int*)(&sample_[0])));
594 else if(dataType_ ==
"long long")
595 fprintf(fpAggregate,
"%lld\n", *((
long long*)(&sample_[0])));
596 else if(dataType_ ==
"unsigned long long")
597 fprintf(fpAggregate,
"%llu\n", *((
unsigned long long*)(&sample_[0])));
598 else if(dataType_ ==
"float")
599 fprintf(fpAggregate,
"%f\n", *((
float*)(&sample_[0])));
600 else if(dataType_ ==
"double")
601 fprintf(fpAggregate,
"%f\n", *((
double*)(&sample_[0])));
607 for(time_t i = 1; i < 5; ++i, checkMask <<= 1)
608 if(alarmMask & checkMask)
610 fprintf(fpAggregate,
"%lu\n", i);
611 fprintf(fpAggregate,
"%s\n", fullChannelName_.c_str());
613 if(dataType_[dataType_.size() - 1] ==
'b')
615 std::stringstream ss;
617 for(
unsigned int j = 0; j < (*alarmValueArray[i - 1]).size(); ++i)
618 ss << std::hex << (
int)(((*alarmValueArray[i - 1])[j] >> 4) & 0xF) << (
int)(((*alarmValueArray[i - 1])[j]) & 0xF) << std::dec;
619 fprintf(fpAggregate,
"%s\n", ss.str().c_str());
621 else if(dataType_ ==
"char")
622 fprintf(fpAggregate,
"%d\n", *((
char*)(&(*alarmValueArray[i - 1])[0])));
623 else if(dataType_ ==
"unsigned char")
624 fprintf(fpAggregate,
"%u\n", *((
unsigned char*)(&(*alarmValueArray[i - 1])[0])));
625 else if(dataType_ ==
"short")
626 fprintf(fpAggregate,
"%d\n", *((
short*)(&(*alarmValueArray[i - 1])[0])));
627 else if(dataType_ ==
"unsigned short")
628 fprintf(fpAggregate,
"%u\n", *((
unsigned short*)(&(*alarmValueArray[i - 1])[0])));
629 else if(dataType_ ==
"int")
630 fprintf(fpAggregate,
"%d\n", *((
int*)(&(*alarmValueArray[i - 1])[0])));
631 else if(dataType_ ==
"unsigned int")
632 fprintf(fpAggregate,
"%u\n", *((
unsigned int*)(&(*alarmValueArray[i - 1])[0])));
633 else if(dataType_ ==
"long long")
634 fprintf(fpAggregate,
"%lld\n", *((
long long*)(&(*alarmValueArray[i - 1])[0])));
635 else if(dataType_ ==
"unsigned long long")
636 fprintf(fpAggregate,
"%llu\n", *((
unsigned long long*)(&(*alarmValueArray[i - 1])[0])));
637 else if(dataType_ ==
"float")
638 fprintf(fpAggregate,
"%f\n", *((
float*)(&(*alarmValueArray[i - 1])[0])));
639 else if(dataType_ ==
"double")
640 fprintf(fpAggregate,
"%f\n", *((
double*)(&(*alarmValueArray[i - 1])[0])));
651 FILE* fp = fopen(saveFullFileName_.c_str(), saveBinaryFormat_ ?
"ab" :
"a");
654 __GEN_COUT_ERR__ <<
"Failed to open slow controls channel file: " << saveFullFileName_ << __E__;
659 if(saveBinaryFormat_)
661 __GEN_COUT__ <<
"Binary File Format: " <<
sizeof(lastSampleTime_) <<
" " << sample_.size() << __E__;
662 fwrite(&lastSampleTime_,
sizeof(lastSampleTime_), 1, fp);
663 fwrite(&sample_[0], sample_.size(), 1, fp);
667 for(time_t i = 1; i < 5; ++i, alarmMask >>= 1)
670 fwrite(&i,
sizeof(lastSampleTime_), 1, fp);
671 fwrite(&(*alarmValueArray[i - 1])[0], (*alarmValueArray[i - 1]).size(), 1, fp);
676 __GEN_COUT__ <<
"Text File Format: " << dataType_ << __E__;
678 fprintf(fp,
"%lu\n", lastSampleTime_);
680 if(dataType_[dataType_.size() - 1] ==
'b')
682 std::stringstream ss;
684 for(
unsigned int i = 0; i < sample_.size(); ++i)
685 ss << std::hex << (
int)((sample_[i] >> 4) & 0xF) << (int)((sample_[i]) & 0xF) << std::dec;
686 fprintf(fp,
"%s\n", ss.str().c_str());
688 else if(dataType_ ==
"char")
689 fprintf(fp,
"%d\n", *((
char*)(&sample_[0])));
690 else if(dataType_ ==
"unsigned char")
691 fprintf(fp,
"%u\n", *((
unsigned char*)(&sample_[0])));
692 else if(dataType_ ==
"short")
693 fprintf(fp,
"%d\n", *((
short*)(&sample_[0])));
694 else if(dataType_ ==
"unsigned short")
695 fprintf(fp,
"%u\n", *((
unsigned short*)(&sample_[0])));
696 else if(dataType_ ==
"int")
697 fprintf(fp,
"%d\n", *((
int*)(&sample_[0])));
698 else if(dataType_ ==
"unsigned int")
699 fprintf(fp,
"%u\n", *((
unsigned int*)(&sample_[0])));
700 else if(dataType_ ==
"long long")
701 fprintf(fp,
"%lld\n", *((
long long*)(&sample_[0])));
702 else if(dataType_ ==
"unsigned long long")
703 fprintf(fp,
"%llu\n", *((
unsigned long long*)(&sample_[0])));
704 else if(dataType_ ==
"float")
705 fprintf(fp,
"%f\n", *((
float*)(&sample_[0])));
706 else if(dataType_ ==
"double")
707 fprintf(fp,
"%f\n", *((
double*)(&sample_[0])));
713 for(time_t i = 1; i < 5; ++i, checkMask <<= 1)
714 if(alarmMask & checkMask)
716 fprintf(fp,
"%lu\n", i);
718 if(dataType_[dataType_.size() - 1] ==
'b')
720 std::stringstream ss;
722 for(
unsigned int j = 0; j < (*alarmValueArray[i - 1]).size(); ++i)
723 ss << std::hex << (
int)(((*alarmValueArray[i - 1])[j] >> 4) & 0xF) << (
int)(((*alarmValueArray[i - 1])[j]) & 0xF) << std::dec;
724 fprintf(fp,
"%s\n", ss.str().c_str());
726 else if(dataType_ ==
"char")
727 fprintf(fp,
"%d\n", *((
char*)(&(*alarmValueArray[i - 1])[0])));
728 else if(dataType_ ==
"unsigned char")
729 fprintf(fp,
"%u\n", *((
unsigned char*)(&(*alarmValueArray[i - 1])[0])));
730 else if(dataType_ ==
"short")
731 fprintf(fp,
"%d\n", *((
short*)(&(*alarmValueArray[i - 1])[0])));
732 else if(dataType_ ==
"unsigned short")
733 fprintf(fp,
"%u\n", *((
unsigned short*)(&(*alarmValueArray[i - 1])[0])));
734 else if(dataType_ ==
"int")
735 fprintf(fp,
"%d\n", *((
int*)(&(*alarmValueArray[i - 1])[0])));
736 else if(dataType_ ==
"unsigned int")
737 fprintf(fp,
"%u\n", *((
unsigned int*)(&(*alarmValueArray[i - 1])[0])));
738 else if(dataType_ ==
"long long")
739 fprintf(fp,
"%lld\n", *((
long long*)(&(*alarmValueArray[i - 1])[0])));
740 else if(dataType_ ==
"unsigned long long")
741 fprintf(fp,
"%llu\n", *((
unsigned long long*)(&(*alarmValueArray[i - 1])[0])));
742 else if(dataType_ ==
"float")
743 fprintf(fp,
"%f\n", *((
float*)(&(*alarmValueArray[i - 1])[0])));
744 else if(dataType_ ==
"double")
745 fprintf(fp,
"%f\n", *((
double*)(&(*alarmValueArray[i - 1])[0])));
758 void FESlowControlsChannel::extractSample(
const std::string& universalReadValue)
763 __GEN_SS__ <<
"Universal Read: ";
764 for(
unsigned int i = 0; i < universalReadValue.size(); ++i)
765 ss << std::hex << (
int)((universalReadValue[i] >> 4) & 0xF) << (int)((universalReadValue[i]) & 0xF) <<
" " << std::dec;
767 __GEN_COUT__ <<
"\n" << ss.str();
768 __GEN_COUT__ <<
"Universal Read: " << BinaryStringMacros::binaryNumberToHexString(universalReadValue,
"0x",
" ") << __E__;
772 BinaryStringMacros::extractValueFromBinaryString(universalReadValue, sample_, sizeOfDataTypeBits_);
774 __GEN_COUT__ <<
"Sample size in bytes: " << sample_.size() <<
"\t in bits: " << sizeOfDataTypeBits_ << __E__;
776 __GEN_COUT__ <<
"sample: " << BinaryStringMacros::binaryNumberToHexString(sample_,
"0x",
" ") << __E__;
784 void FESlowControlsChannel::clearAlarms(
int targetAlarm)
786 if(targetAlarm == -1 || targetAlarm == 0)
787 loloAlarmed_ =
false;
788 if(targetAlarm == -1 || targetAlarm == 1)
790 if(targetAlarm == -1 || targetAlarm == 2)
792 if(targetAlarm == -1 || targetAlarm == 3)
793 hihiAlarmed_ =
false;
801 char FESlowControlsChannel::checkAlarms(std::string& txBuffer)
815 char createPacketMask = 0;
817 if(dataType_[dataType_.size() - 1] ==
'b')
819 else if(dataType_ ==
"char")
821 else if(dataType_ ==
"unsigned char")
823 else if(dataType_ ==
"short")
825 else if(dataType_ ==
"unsigned short")
827 else if(dataType_ ==
"int")
829 else if(dataType_ ==
"unsigned int")
831 else if(dataType_ ==
"long long")
833 else if(dataType_ ==
"unsigned long long")
835 else if(dataType_ ==
"float")
837 else if(dataType_ ==
"double")
842 __GEN_COUT__ <<
"Using unsigned long long for alarms." << __E__;
844 if((!loloAlarmed_ || !latchAlarms_) && *((
unsigned long long*)&sample_[0]) <= *((
unsigned long long*)&lolo_[0]))
847 createPacketMask |= 1 << 0;
850 if((!loAlarmed_ || !latchAlarms_) && *((
unsigned long long*)&sample_[0]) <= *((
unsigned long long*)&lo_[0]))
853 createPacketMask |= 1 << 1;
856 if((!hiAlarmed_ || !latchAlarms_) && *((
unsigned long long*)&sample_[0]) >= *((
unsigned long long*)&hi_[0]))
859 createPacketMask |= 1 << 2;
862 if((!hihiAlarmed_ || !latchAlarms_) && *((
unsigned long long*)&sample_[0]) >= *((
unsigned long long*)&hihi_[0]))
865 createPacketMask |= 1 << 3;
868 else if(useType == 1)
870 __GEN_COUT__ <<
"Using long long for alarms." << __E__;
872 if((!loloAlarmed_ || !latchAlarms_) && *((
long long*)&sample_[0]) <= *((
long long*)&lolo_[0]))
875 createPacketMask |= 1 << 0;
878 if((!loAlarmed_ || !latchAlarms_) && *((
long long*)&sample_[0]) <= *((
long long*)&lo_[0]))
881 createPacketMask |= 1 << 1;
884 if((!hiAlarmed_ || !latchAlarms_) && *((
long long*)&sample_[0]) >= *((
long long*)&hi_[0]))
887 createPacketMask |= 1 << 2;
890 if((!hihiAlarmed_ || !latchAlarms_) && *((
long long*)&sample_[0]) >= *((
long long*)&hihi_[0]))
893 createPacketMask |= 1 << 3;
896 else if(useType == 2)
898 __GEN_COUT__ <<
"Using float for alarms." << __E__;
900 if((!loloAlarmed_ || !latchAlarms_) && *((
float*)&sample_[0]) <= *((
float*)&lolo_[0]))
903 createPacketMask |= 1 << 0;
906 if((!loAlarmed_ || !latchAlarms_) && *((
float*)&sample_[0]) <= *((
float*)&lo_[0]))
909 createPacketMask |= 1 << 1;
912 if((!hiAlarmed_ || !latchAlarms_) && *((
float*)&sample_[0]) >= *((
float*)&hi_[0]))
915 createPacketMask |= 1 << 2;
918 if((!hihiAlarmed_ || !latchAlarms_) && *((
float*)&sample_[0]) >= *((
float*)&hihi_[0]))
921 createPacketMask |= 1 << 3;
924 else if(useType == 3)
926 __GEN_COUT__ <<
"Using double for alarms." << __E__;
928 if((!loloAlarmed_ || !latchAlarms_) && *((
double*)&sample_[0]) <= *((
double*)&lolo_[0]))
931 createPacketMask |= 1 << 0;
934 if((!loAlarmed_ || !latchAlarms_) && *((
double*)&sample_[0]) <= *((
double*)&lo_[0]))
937 createPacketMask |= 1 << 1;
940 if((!hiAlarmed_ || !latchAlarms_) && *((
double*)&sample_[0]) >= *((
double*)&hi_[0]))
943 createPacketMask |= 1 << 2;
946 if((!hihiAlarmed_ || !latchAlarms_) && *((
double*)&sample_[0]) >= *((
double*)&hihi_[0]))
949 createPacketMask |= 1 << 3;
954 std::string* alarmValueArray[] = {&lolo_, &lo_, &hi_, &hihi_};
956 if(monitoringEnabled_)
960 for(
int i = 0; i < 4; ++i, checkMask <<= 1)
961 if(createPacketMask & checkMask)
974 __GEN_COUT__ <<
"Create packet type " << i + 1 <<
" alarm value = " << *alarmValueArray[i] << __E__;
976 __GEN_COUT__ <<
"before txBuffer sz=" << txBuffer.size() << __E__;
977 txBuffer.push_back(i + 1);
978 txBuffer.push_back(txPacketSequenceNumber_++);
980 txBuffer.resize(txBuffer.size() +
sizeof(lastSampleTime_));
981 memcpy(&txBuffer[txBuffer.size() -
sizeof(lastSampleTime_)] , &lastSampleTime_ ,
sizeof(lastSampleTime_));
983 unsigned int tmpSz = fullChannelName_.size();
985 txBuffer.resize(txBuffer.size() +
sizeof(tmpSz));
986 memcpy(&txBuffer[txBuffer.size() -
sizeof(tmpSz)] , &tmpSz ,
sizeof(tmpSz));
988 txBuffer += fullChannelName_;
990 txBuffer.push_back((
unsigned char)(*alarmValueArray[i]).size());
991 txBuffer.push_back((
unsigned char)sizeOfDataTypeBits_);
993 txBuffer += (*alarmValueArray[i]);
994 __GEN_COUT__ <<
"after txBuffer sz=" << txBuffer.size() << __E__;
997 __GEN_SS__ <<
"txBuffer: \n";
998 for(
unsigned int i = 0; i < txBuffer.size(); ++i)
1000 ss << std::hex << (int)((txBuffer[i] >> 4) & 0xF) << (int)((txBuffer[i]) & 0xF) <<
" " << std::dec;
1005 __GEN_COUT__ <<
"\n" << ss.str();
1010 return createPacketMask;