00001 #include "otsdaq-core/FECore/FESlowControlsChannel.h"
00002 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00003
00004
00005 #include <iostream>
00006 #include <sstream>
00007 #include <stdexcept>
00008
00009
00010 using namespace ots;
00011
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026
00027
00028
00029 FESlowControlsChannel::FESlowControlsChannel(
00030 const std::string& interfaceUID,
00031 const std::string& channelName,
00032 const std::string& dataType,
00033 unsigned int universalDataSize,
00034 unsigned int universalAddressSize,
00035 const std::string& universalAddress,
00036 unsigned int universalDataBitOffset,
00037
00038 bool readAccess,
00039 bool writeAccess,
00040 bool monitoringEnabled,
00041 bool recordChangesOnly,
00042 time_t delayBetweenSamples,
00043 bool saveEnabled,
00044 const std::string& savePath,
00045 const std::string& saveFileRadix,
00046 bool saveBinaryFormat,
00047 bool alarmsEnabled,
00048 bool latchAlarms,
00049 const std::string& lolo,
00050 const std::string& lo,
00051 const std::string& hi,
00052 const std::string& hihi
00053 )
00054 : interfaceUID_ (interfaceUID),
00055 channelName_ (channelName),
00056 fullChannelName_ (interfaceUID_ + "/" + channelName_),
00057 dataType_ (dataType),
00058 universalDataBitOffset_ (universalDataBitOffset),
00059 txPacketSequenceNumber_ (0),
00060
00061 readAccess_ (readAccess),
00062 writeAccess_ (writeAccess),
00063 monitoringEnabled_ (monitoringEnabled),
00064 recordChangesOnly_ (recordChangesOnly),
00065 delayBetweenSamples_ (delayBetweenSamples),
00066 saveEnabled_ (saveEnabled),
00067 savePath_ (savePath),
00068 saveFileRadix_ (saveFileRadix),
00069 saveBinaryFormat_ (saveBinaryFormat),
00070 alarmsEnabled_ (alarmsEnabled),
00071 latchAlarms_ (latchAlarms),
00072
00073 lastSampleTime_ (0),
00074 loloAlarmed_ (false),
00075 loAlarmed_ (false),
00076 hiAlarmed_ (false),
00077 hihiAlarmed_ (false),
00078 saveFullFileName_ (savePath_ + "/" + saveFileRadix_ + "-" + underscoreString(fullChannelName_) + "-" + std::to_string(time(0)) + (saveBinaryFormat_?".dat":".txt"))
00079 {
00080 __COUT__ << "dataType_ = " << dataType_ << std::endl;
00081 __COUT__ << "universalAddressSize = " << universalAddressSize << std::endl;
00082 __COUT__ << "universalAddress = " << universalAddress << std::endl;
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 if(dataType_[dataType_.size()-1] == 'b')
00095 sscanf(&dataType_[0],"%u",&sizeOfDataTypeBits_);
00096 else if(dataType_ == "char" || dataType_ == "unsigned char")
00097 sizeOfDataTypeBits_ = sizeof(char)*8;
00098 else if(dataType_ == "short" || dataType_ == "unsigned short")
00099 sizeOfDataTypeBits_ = sizeof(short)*8;
00100 else if(dataType_ == "int" || dataType_ == "unsigned int")
00101 sizeOfDataTypeBits_ = sizeof(int)*8;
00102 else if(dataType_ == "long long" || dataType_ == "unsigned long long")
00103 sizeOfDataTypeBits_ = sizeof(long long)*8;
00104 else if(dataType_ == "float")
00105 sizeOfDataTypeBits_ = sizeof(float)*8;
00106 else if(dataType_ == "double")
00107 sizeOfDataTypeBits_ = sizeof(double)*8;
00108 else
00109 {
00110 __SS__ << "Data type '" << dataType_ << "' is invalid. " <<
00111 "Valid data types (w/size in bytes) are as follows: " <<
00112 "#b (# bits)" <<
00113 ", char (" << sizeof(char) <<
00114 "B), unsigned char (" << sizeof(unsigned char) <<
00115 "B), short (" << sizeof(short) <<
00116 "B), unsigned short (" << sizeof(unsigned short) <<
00117 "B), int (" << sizeof(int) <<
00118 "B), unsigned int (" << sizeof(unsigned int) <<
00119 "B), long long (" << sizeof(long long) <<
00120 "B), unsigned long long (" << sizeof(unsigned long long) <<
00121 "B), float (" << sizeof(float) <<
00122 "B), double (" << sizeof(double) <<
00123 "B)." << std::endl;
00124 __COUT_ERR__ << "\n" << ss.str();
00125 throw std::runtime_error(ss.str());
00126 }
00127
00128 if(sizeOfDataTypeBits_ > 64)
00129 {
00130 __SS__ << "Invalid Data Type '" << dataType_ <<
00131 "' (" << sizeOfDataTypeBits_ << "-bits)"
00132 ". Size in bits must be less than or equal to 64-bits." <<
00133 std::endl;
00134 __COUT_ERR__ << "\n" << ss.str();
00135 throw std::runtime_error(ss.str());
00136 }
00137
00138 if(universalDataSize*8 < sizeOfDataTypeBits_)
00139 {
00140 __SS__ << "Invalid Data Type '" << dataType_ <<
00141 "' (" << sizeOfDataTypeBits_ << "-bits) or Universal Data Size of " <<
00142 universalDataSize*8 <<
00143 "-bits. Data Type size must be less than or equal to Universal Data Size." <<
00144 std::endl;
00145 __COUT_ERR__ << "\n" << ss.str();
00146 throw std::runtime_error(ss.str());
00147 }
00148
00149
00150 universalAddress_.resize(universalAddressSize);
00151 convertStringToBuffer(universalAddress,universalAddress_);
00152
00153 sizeOfDataTypeBytes_ = (sizeOfDataTypeBits_/8 +
00154 ((universalDataBitOffset_%8)?1:0));
00155
00156 lolo_.resize(sizeOfDataTypeBytes_);
00157 convertStringToBuffer(lolo,lolo_,true);
00158 lo_.resize(sizeOfDataTypeBytes_);
00159 convertStringToBuffer(lo,lo_,true);
00160 hi_.resize(sizeOfDataTypeBytes_);
00161 convertStringToBuffer(hi,hi_,true);
00162 hihi_.resize(sizeOfDataTypeBytes_);
00163 convertStringToBuffer(hihi,hihi_,true);
00164
00165
00166 sample_.resize(sizeOfDataTypeBytes_);
00167 lastSample_.resize(sizeOfDataTypeBytes_);
00168 }
00169
00170
00171 FESlowControlsChannel::~FESlowControlsChannel(void)
00172 {}
00173
00174
00175
00176
00177 std::string FESlowControlsChannel::underscoreString(const std::string& str)
00178 {
00179 std::string retStr;
00180 retStr.reserve(str.size());
00181 for(unsigned int i=0;i<str.size();++i)
00182 if(
00183 (str[i] >= 'a' && str[i] <= 'z') ||
00184 (str[i] >= 'A' && str[i] <= 'Z') ||
00185 (str[i] >= '0' && str[i] <= '9'))
00186 retStr.push_back(str[i]);
00187 else
00188 retStr.push_back('_');
00189 return retStr;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 void FESlowControlsChannel::convertStringToBuffer(const std::string& inString, std::string& buffer, bool useDataType)
00199 {
00200 __COUT__ << "Input Str Sz= \t" << inString.size() << std::endl;
00201 __COUT__ << "Input Str Val= \t'" << inString << "'" << std::endl;
00202 __COUT__ << "Output buffer Sz= \t" << buffer.size() << std::endl;
00203
00204 if(useDataType &&
00205 (dataType_ == "float" || dataType_ == "double"))
00206 {
00207 __COUT__ << "Floating point spec'd" << std::endl;
00208 if(dataType_ == "float" && buffer.size() == sizeof(float))
00209 {
00210 sscanf(&inString[0],"%f",(float *)&buffer[0]);
00211 __COUT__ << "float: " << *((float *)&buffer[0]) << std::endl;
00212 }
00213 else if(dataType_ == "double" && buffer.size() == sizeof(double))
00214 {
00215 sscanf(&inString[0],"%lf",(double *)&buffer[0]);
00216 __COUT__ << "double: " << *((double *)&buffer[0]) << std::endl;
00217 }
00218 else
00219 {
00220 __SS__ << "Invalid floating point spec! " <<
00221 "dataType_=" << dataType_ <<
00222 " buffer.size()=" << buffer.size() << std::endl;
00223 __COUT_ERR__ << "\n" << ss.str();
00224 throw std::runtime_error(ss.str());
00225 }
00226
00227 {
00228 __SS__ << "0x ";
00229 for(int i=(int)buffer.size()-1;i>=0;--i)
00230 ss << std::hex << (int)((buffer[i]>>4)&0xF) <<
00231 (int)((buffer[i])&0xF) << " " << std::dec;
00232 ss << std::endl;
00233 __COUT__ << "\n" << ss.str();
00234 }
00235 return;
00236 }
00237
00238
00239
00240 for(unsigned int i=0;i<buffer.size();++i)
00241 buffer[i] = 0;
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 if(inString.size())
00254 {
00255 if(inString.size() > 2 && inString[0] == '0' && inString[1] == 'x')
00256 {
00257
00258
00259 __COUT__ << "Hex." << std::endl;
00260
00261 unsigned int j;
00262 unsigned char val;
00263 for(unsigned int i=0;i<inString.size();++i)
00264 {
00265 j = (inString.size()-1-i);
00266 if(inString[i] >= '0' &&
00267 inString[i] <= '9')
00268 val = inString[i] - 48;
00269 else if(inString[i] >= 'A' &&
00270 inString[i] <= 'F')
00271 val = inString[i] - 55;
00272 else if(inString[i] >= 'a' &&
00273 inString[i] <= 'f')
00274 val = inString[i] - 87;
00275 else
00276 val = 0;
00277
00278 buffer[j/2] |= val<<((j%2)*4);
00279 }
00280 }
00281 else
00282 {
00283
00284
00285 __COUT__ << "Decimal." << std::endl;
00286 unsigned long long val;
00287
00288 if(!useDataType || dataType_[0] == 'u')
00289 {
00290 sscanf(&inString[0],"%llu",&val);
00291 }
00292 else
00293 {
00294 sscanf(&inString[0],"%lld",(long long *)&val);
00295 }
00296
00297 for(unsigned int i=0;i<sizeof(long long) && i<buffer.size();++i)
00298 buffer[i] = ((char *)&val)[i];
00299
00300 }
00301 }
00302
00303
00304 {
00305 __SS__ << "0x ";
00306 for(int i=(int)buffer.size()-1;i>=0;--i)
00307 ss << std::hex << (int)((buffer[i]>>4)&0xF) <<
00308 (int)((buffer[i])&0xF) << " " << std::dec;
00309 ss << std::endl;
00310 __COUT__ << "\n" << ss.str();
00311 }
00312 }
00313
00314
00315
00316
00317
00318 void FESlowControlsChannel::handleSample(const std::string& universalReadValue, std::string& txBuffer,
00319 FILE *fpAggregate, bool aggregateIsBinaryFormat)
00320 {
00321 __COUT__ << "txBuffer size=" << txBuffer.size() << std::endl;
00322
00323
00324
00325
00326 extractSample(universalReadValue);
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00342
00343 if(recordChangesOnly_)
00344 {
00345 if(lastSampleTime_ &&
00346 lastSample_ == sample_)
00347 {
00348 __COUT__ << "no change." << std::endl;
00349 return;
00350 }
00351 }
00352
00353 __COUT__ << "new value!" << std::endl;
00354
00355
00356 lastSampleTime_ = time(0);
00357 lastSample_ = sample_;
00358
00359 char alarmMask = 0;
00360
00364 if(monitoringEnabled_)
00365 {
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 __COUT__ << "before txBuffer sz=" << txBuffer.size() << std::endl;
00377 txBuffer.push_back(0);
00378 txBuffer.push_back(txPacketSequenceNumber_++);
00379
00380 txBuffer.resize(txBuffer.size() + sizeof(lastSampleTime_));
00381 memcpy(&txBuffer[txBuffer.size() - sizeof(lastSampleTime_)],&lastSampleTime_,sizeof(lastSampleTime_));
00382
00383 unsigned int tmpSz = fullChannelName_.size();
00384
00385 txBuffer.resize(txBuffer.size() + sizeof(tmpSz));
00386 memcpy(&txBuffer[txBuffer.size() - sizeof(tmpSz)],&tmpSz,sizeof(tmpSz));
00387
00388 txBuffer += fullChannelName_;
00389
00390 txBuffer.push_back((unsigned char)sample_.size());
00391 txBuffer.push_back((unsigned char)sizeOfDataTypeBits_);
00392
00393 txBuffer += sample_;
00394 __COUT__ << "after txBuffer sz=" << txBuffer.size() << std::endl;
00395
00396 {
00397 __SS__ << "txBuffer: \n";
00398 for(unsigned int i=0; i<txBuffer.size(); ++i)
00399 {
00400 ss << std::hex << (int)((txBuffer[i]>>4)&0xF) <<
00401 (int)((txBuffer[i])&0xF) << " " << std::dec;
00402 if(i%8 == 7) ss << std::endl;
00403 }
00404 ss << std::endl;
00405 __COUT__ << "\n" << ss.str();
00406 }
00407 }
00408
00409
00410 if(alarmsEnabled_)
00411 alarmMask = checkAlarms(txBuffer);
00412
00413
00414 std::string *alarmValueArray[] = {
00415 &lolo_, &lo_, &hi_, &hihi_
00416 };
00417
00421 if(fpAggregate)
00422 {
00423
00424 if(aggregateIsBinaryFormat)
00425 {
00426
00427
00428
00429
00430
00431
00432
00433
00434 __COUT__ << "Aggregate Binary File Format: " <<
00435 sizeof(lastSampleTime_) << " " << sample_.size() << std::endl;
00436
00437 {
00438 fwrite(&lastSampleTime_,sizeof(lastSampleTime_),1,fpAggregate);
00439
00440 unsigned int tmpSz = fullChannelName_.size();
00441 fwrite(&tmpSz,sizeof(tmpSz),1,fpAggregate);
00442 fwrite(&fullChannelName_[0],fullChannelName_.size(),1,fpAggregate);
00443
00444 unsigned char tmpChar = (unsigned char)sample_.size();
00445 fwrite(&tmpChar,1,1,fpAggregate);
00446
00447 tmpChar = (unsigned char)sizeOfDataTypeBits_;
00448 fwrite(&tmpChar,1,1,fpAggregate);
00449 fwrite(&sample_[0],sample_.size(),1,fpAggregate);
00450 }
00451
00452
00453 if(alarmMask)
00454 for(time_t i=1;i<5;++i, alarmMask >>= 1)
00455 if(alarmMask & 1)
00456 {
00457 {
00458 fwrite(&i,sizeof(lastSampleTime_),1,fpAggregate);
00459
00460 unsigned int tmpSz = fullChannelName_.size();
00461 fwrite(&tmpSz,sizeof(tmpSz),1,fpAggregate);
00462 fwrite(&fullChannelName_[0],fullChannelName_.size(),1,fpAggregate);
00463
00464 unsigned char tmpChar = (unsigned char)sample_.size();
00465 fwrite(&tmpChar,1,1,fpAggregate);
00466
00467 tmpChar = (unsigned char)sizeOfDataTypeBits_;
00468 fwrite(&tmpChar,1,1,fpAggregate);
00469 fwrite(&(*alarmValueArray[i-1])[0],(*alarmValueArray[i-1]).size(),1,fpAggregate);
00470 }
00471 }
00472 }
00473 else
00474 {
00475
00476
00477
00478
00479
00480 __COUT__ << "Aggregate Text File Format: " << dataType_ << std::endl;
00481
00482 fprintf(fpAggregate,"%lu\n",lastSampleTime_);
00483 fprintf(fpAggregate,"%s\n",fullChannelName_.c_str());
00484
00485 if(dataType_[dataType_.size()-1] == 'b')
00486 {
00487 std::stringstream ss;
00488 ss << "0x";
00489 for(unsigned int i=0; i<sample_.size(); ++i)
00490 ss << std::hex << (int)((sample_[i]>>4)&0xF) <<
00491 (int)((sample_[i])&0xF) << std::dec;
00492 fprintf(fpAggregate,"%s\n",ss.str().c_str());
00493 }
00494 else if(dataType_ == "char")
00495 fprintf(fpAggregate,"%d\n",*((char *)(&sample_[0])));
00496 else if(dataType_ == "unsigned char")
00497 fprintf(fpAggregate,"%u\n",*((unsigned char *)(&sample_[0])));
00498 else if(dataType_ == "short")
00499 fprintf(fpAggregate,"%d\n",*((short *)(&sample_[0])));
00500 else if(dataType_ == "unsigned short")
00501 fprintf(fpAggregate,"%u\n",*((unsigned short *)(&sample_[0])));
00502 else if(dataType_ == "int")
00503 fprintf(fpAggregate,"%d\n",*((int *)(&sample_[0])));
00504 else if(dataType_ == "unsigned int")
00505 fprintf(fpAggregate,"%u\n",*((unsigned int *)(&sample_[0])));
00506 else if(dataType_ == "long long")
00507 fprintf(fpAggregate,"%lld\n",*((long long *)(&sample_[0])));
00508 else if(dataType_ == "unsigned long long")
00509 fprintf(fpAggregate,"%llu\n",*((unsigned long long *)(&sample_[0])));
00510 else if(dataType_ == "float")
00511 fprintf(fpAggregate,"%f\n",*((float *)(&sample_[0])));
00512 else if(dataType_ == "double")
00513 fprintf(fpAggregate,"%f\n",*((double *)(&sample_[0])));
00514
00515
00516
00517 if(alarmMask)
00518 {
00519 char checkMask = 1;
00520 for(time_t i=1;i<5;++i, checkMask <<= 1)
00521 if(alarmMask & checkMask)
00522 {
00523 fprintf(fpAggregate,"%lu\n",i);
00524 fprintf(fpAggregate,"%s\n",fullChannelName_.c_str());
00525
00526 if(dataType_[dataType_.size()-1] == 'b')
00527 {
00528 std::stringstream ss;
00529 ss << "0x";
00530 for(unsigned int j=0; j<(*alarmValueArray[i-1]).size(); ++i)
00531 ss << std::hex << (int)(((*alarmValueArray[i-1])[j]>>4)&0xF) <<
00532 (int)(((*alarmValueArray[i-1])[j])&0xF) << std::dec;
00533 fprintf(fpAggregate,"%s\n",ss.str().c_str());
00534 }
00535 else if(dataType_ == "char")
00536 fprintf(fpAggregate,"%d\n",*((char *)(&(*alarmValueArray[i-1])[0])));
00537 else if(dataType_ == "unsigned char")
00538 fprintf(fpAggregate,"%u\n",*((unsigned char *)(&(*alarmValueArray[i-1])[0])));
00539 else if(dataType_ == "short")
00540 fprintf(fpAggregate,"%d\n",*((short *)(&(*alarmValueArray[i-1])[0])));
00541 else if(dataType_ == "unsigned short")
00542 fprintf(fpAggregate,"%u\n",*((unsigned short *)(&(*alarmValueArray[i-1])[0])));
00543 else if(dataType_ == "int")
00544 fprintf(fpAggregate,"%d\n",*((int *)(&(*alarmValueArray[i-1])[0])));
00545 else if(dataType_ == "unsigned int")
00546 fprintf(fpAggregate,"%u\n",*((unsigned int *)(&(*alarmValueArray[i-1])[0])));
00547 else if(dataType_ == "long long")
00548 fprintf(fpAggregate,"%lld\n",*((long long *)(&(*alarmValueArray[i-1])[0])));
00549 else if(dataType_ == "unsigned long long")
00550 fprintf(fpAggregate,"%llu\n",*((unsigned long long *)(&(*alarmValueArray[i-1])[0])));
00551 else if(dataType_ == "float")
00552 fprintf(fpAggregate,"%f\n",*((float *)(&(*alarmValueArray[i-1])[0])));
00553 else if(dataType_ == "double")
00554 fprintf(fpAggregate,"%f\n",*((double *)(&(*alarmValueArray[i-1])[0])));
00555 }
00556 }
00557 }
00558 }
00559
00563 if(saveEnabled_)
00564 {
00565 FILE *fp = fopen(saveFullFileName_.c_str(),saveBinaryFormat_?"ab":"a");
00566 if(!fp)
00567 {
00568 __COUT_ERR__ << "Failed to open slow controls channel file: " <<
00569 saveFullFileName_ << std::endl;
00570 return;
00571 }
00572
00573
00574 if(saveBinaryFormat_)
00575 {
00576 __COUT__ << "Binary File Format: " << sizeof(lastSampleTime_) << " " << sample_.size() << std::endl;
00577 fwrite(&lastSampleTime_,sizeof(lastSampleTime_),1,fp);
00578 fwrite(&sample_[0],sample_.size(),1,fp);
00579
00580
00581 if(alarmMask)
00582 for(time_t i=1;i<5;++i, alarmMask >>= 1)
00583 if(alarmMask & 1)
00584 {
00585 fwrite(&i,sizeof(lastSampleTime_),1,fp);
00586 fwrite(&(*alarmValueArray[i-1])[0],(*alarmValueArray[i-1]).size(),1,fp);
00587 }
00588 }
00589 else
00590 {
00591 __COUT__ << "Text File Format: " << dataType_ << std::endl;
00592
00593 fprintf(fp,"%lu\n",lastSampleTime_);
00594
00595 if(dataType_[dataType_.size()-1] == 'b')
00596 {
00597 std::stringstream ss;
00598 ss << "0x";
00599 for(unsigned int i=0; i<sample_.size(); ++i)
00600 ss << std::hex << (int)((sample_[i]>>4)&0xF) <<
00601 (int)((sample_[i])&0xF) << std::dec;
00602 fprintf(fp,"%s\n",ss.str().c_str());
00603 }
00604 else if(dataType_ == "char")
00605 fprintf(fp,"%d\n",*((char *)(&sample_[0])));
00606 else if(dataType_ == "unsigned char")
00607 fprintf(fp,"%u\n",*((unsigned char *)(&sample_[0])));
00608 else if(dataType_ == "short")
00609 fprintf(fp,"%d\n",*((short *)(&sample_[0])));
00610 else if(dataType_ == "unsigned short")
00611 fprintf(fp,"%u\n",*((unsigned short *)(&sample_[0])));
00612 else if(dataType_ == "int")
00613 fprintf(fp,"%d\n",*((int *)(&sample_[0])));
00614 else if(dataType_ == "unsigned int")
00615 fprintf(fp,"%u\n",*((unsigned int *)(&sample_[0])));
00616 else if(dataType_ == "long long")
00617 fprintf(fp,"%lld\n",*((long long *)(&sample_[0])));
00618 else if(dataType_ == "unsigned long long")
00619 fprintf(fp,"%llu\n",*((unsigned long long *)(&sample_[0])));
00620 else if(dataType_ == "float")
00621 fprintf(fp,"%f\n",*((float *)(&sample_[0])));
00622 else if(dataType_ == "double")
00623 fprintf(fp,"%f\n",*((double *)(&sample_[0])));
00624
00625
00626
00627 if(alarmMask)
00628 {
00629 char checkMask = 1;
00630 for(time_t i=1;i<5;++i, checkMask <<= 1)
00631 if(alarmMask & checkMask)
00632 {
00633 fprintf(fp,"%lu\n",i);
00634
00635 if(dataType_[dataType_.size()-1] == 'b')
00636 {
00637 std::stringstream ss;
00638 ss << "0x";
00639 for(unsigned int j=0; j<(*alarmValueArray[i-1]).size(); ++i)
00640 ss << std::hex << (int)(((*alarmValueArray[i-1])[j]>>4)&0xF) <<
00641 (int)(((*alarmValueArray[i-1])[j])&0xF) << std::dec;
00642 fprintf(fp,"%s\n",ss.str().c_str());
00643 }
00644 else if(dataType_ == "char")
00645 fprintf(fp,"%d\n",*((char *)(&(*alarmValueArray[i-1])[0])));
00646 else if(dataType_ == "unsigned char")
00647 fprintf(fp,"%u\n",*((unsigned char *)(&(*alarmValueArray[i-1])[0])));
00648 else if(dataType_ == "short")
00649 fprintf(fp,"%d\n",*((short *)(&(*alarmValueArray[i-1])[0])));
00650 else if(dataType_ == "unsigned short")
00651 fprintf(fp,"%u\n",*((unsigned short *)(&(*alarmValueArray[i-1])[0])));
00652 else if(dataType_ == "int")
00653 fprintf(fp,"%d\n",*((int *)(&(*alarmValueArray[i-1])[0])));
00654 else if(dataType_ == "unsigned int")
00655 fprintf(fp,"%u\n",*((unsigned int *)(&(*alarmValueArray[i-1])[0])));
00656 else if(dataType_ == "long long")
00657 fprintf(fp,"%lld\n",*((long long *)(&(*alarmValueArray[i-1])[0])));
00658 else if(dataType_ == "unsigned long long")
00659 fprintf(fp,"%llu\n",*((unsigned long long *)(&(*alarmValueArray[i-1])[0])));
00660 else if(dataType_ == "float")
00661 fprintf(fp,"%f\n",*((float *)(&(*alarmValueArray[i-1])[0])));
00662 else if(dataType_ == "double")
00663 fprintf(fp,"%f\n",*((double *)(&(*alarmValueArray[i-1])[0])));
00664 }
00665 }
00666 }
00667
00668 fclose(fp);
00669 }
00670 }
00671
00672
00673
00674
00675
00676 void FESlowControlsChannel::extractSample(const std::string& universalReadValue)
00677 {
00678
00679
00680 {
00681 __SS__ << "Univ Read: ";
00682 for(unsigned int i=0; i<universalReadValue.size(); ++i)
00683 ss << std::hex << (int)((universalReadValue[i]>>4)&0xF) <<
00684 (int)((universalReadValue[i])&0xF) << " " << std::dec;
00685 ss << std::endl;
00686 __COUT__ << "\n" << ss.str();
00687 }
00688
00689 unsigned int byteOffset = universalDataBitOffset_/8;
00690 unsigned int bitOffset = universalDataBitOffset_%8;
00691 unsigned int bitsLeft = sizeOfDataTypeBits_;
00692
00693
00694 unsigned long long tmp = 0;
00695
00696 if(bitOffset)
00697 {
00698
00699
00700
00701
00702
00703 tmp = ((unsigned long long)(universalReadValue[byteOffset++])) >> bitOffset;
00704 bitsLeft = 8 - bitOffset;
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 }
00715
00716 while(bitsLeft > 7)
00717 {
00718
00719
00720
00721
00722
00723 tmp |= ((unsigned long long)(universalReadValue[byteOffset++])) <<
00724 (sizeOfDataTypeBits_ - bitsLeft);
00725 bitsLeft -= 8;
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735 }
00736
00737 if(bitOffset)
00738 {
00739
00740
00741
00742
00743
00744 tmp |= (((unsigned long long)(universalReadValue[byteOffset])) &
00745 (0xFF >> (8-bitsLeft))) <<
00746 (sizeOfDataTypeBits_ - bitsLeft);
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756 }
00757
00758 __COUT__ << "Temp Long Long Sample: " << tmp << std::endl;
00759
00760 sample_.resize(0);
00761 for(unsigned int i=0; i<(sizeOfDataTypeBits_/8 +
00762 ((universalDataBitOffset_%8)?1:0)); ++i)
00763 sample_.push_back(((char *)&tmp)[i]);
00764
00765 __COUT__ << "sample_.size()= " << sample_.size() << std::endl;
00766
00767 {
00768 __SS__ << "Sample: ";
00769 for(unsigned int i=0; i<sample_.size(); ++i)
00770 ss << std::hex << (int)((sample_[i]>>4)&0xF) <<
00771 (int)((sample_[i])&0xF) << " " << std::dec;
00772 ss << std::endl;
00773 __COUT__ << "\n" << ss.str();
00774 }
00775
00776 }
00777
00778
00779
00780
00781
00782
00783 void FESlowControlsChannel::clearAlarms(int targetAlarm)
00784 {
00785 if(targetAlarm == -1 || targetAlarm == 0)
00786 loloAlarmed_ = false;
00787 if(targetAlarm == -1 || targetAlarm == 1)
00788 loAlarmed_ = false;
00789 if(targetAlarm == -1 || targetAlarm == 2)
00790 hiAlarmed_ = false;
00791 if(targetAlarm == -1 || targetAlarm == 3)
00792 hihiAlarmed_ = false;
00793 }
00794
00795
00796
00797
00798
00799
00800 char FESlowControlsChannel::checkAlarms(std::string& txBuffer)
00801 {
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 int useType = -1;
00815 char createPacketMask = 0;
00816
00817
00818 if(dataType_[dataType_.size()-1] == 'b')
00819 useType = 0;
00820 else if(dataType_ == "char")
00821 useType = 1;
00822 else if(dataType_ == "unsigned char")
00823 useType = 0;
00824 else if(dataType_ == "short")
00825 useType = 1;
00826 else if(dataType_ == "unsigned short")
00827 useType = 0;
00828 else if(dataType_ == "int")
00829 useType = 1;
00830 else if(dataType_ == "unsigned int")
00831 useType = 0;
00832 else if(dataType_ == "long long")
00833 useType = 1;
00834 else if(dataType_ == "unsigned long long")
00835 useType = 0;
00836 else if(dataType_ == "float")
00837 useType = 2;
00838 else if(dataType_ == "double")
00839 useType = 3;
00840
00841
00842 if(useType == 0)
00843 {
00844 __COUT__ << "Using unsigned long long for alarms." << std::endl;
00845
00846 if((!loloAlarmed_ || !latchAlarms_) &&
00847 *((unsigned long long *)&sample_[0]) <=
00848 *((unsigned long long *)&lolo_[0]) )
00849 {
00850 loloAlarmed_ = true;
00851 createPacketMask |= 1 << 0;
00852 }
00853
00854 if((!loAlarmed_ || !latchAlarms_) &&
00855 *((unsigned long long *)&sample_[0]) <=
00856 *((unsigned long long *)&lo_[0]) )
00857 {
00858 loAlarmed_ = true;
00859 createPacketMask |= 1 << 1;
00860 }
00861
00862 if((!hiAlarmed_ || !latchAlarms_) &&
00863 *((unsigned long long *)&sample_[0]) >=
00864 *((unsigned long long *)&hi_[0]) )
00865 {
00866 hiAlarmed_ = true;
00867 createPacketMask |= 1 << 2;
00868 }
00869
00870 if((!hihiAlarmed_ || !latchAlarms_) &&
00871 *((unsigned long long *)&sample_[0]) >=
00872 *((unsigned long long *)&hihi_[0]) )
00873 {
00874 hihiAlarmed_ = true;
00875 createPacketMask |= 1 << 3;
00876 }
00877 }
00878 else if(useType == 1)
00879 {
00880 __COUT__ << "Using long long for alarms." << std::endl;
00881
00882 if((!loloAlarmed_ || !latchAlarms_) &&
00883 *((long long *)&sample_[0]) <=
00884 *((long long *)&lolo_[0]) )
00885 {
00886 loloAlarmed_ = true;
00887 createPacketMask |= 1 << 0;
00888 }
00889
00890 if((!loAlarmed_ || !latchAlarms_) &&
00891 *((long long *)&sample_[0]) <=
00892 *((long long *)&lo_[0]) )
00893 {
00894 loAlarmed_ = true;
00895 createPacketMask |= 1 << 1;
00896 }
00897
00898 if((!hiAlarmed_ || !latchAlarms_) &&
00899 *((long long *)&sample_[0]) >=
00900 *((long long *)&hi_[0]) )
00901 {
00902 hiAlarmed_ = true;
00903 createPacketMask |= 1 << 2;
00904 }
00905
00906 if((!hihiAlarmed_ || !latchAlarms_) &&
00907 *((long long *)&sample_[0]) >=
00908 *((long long *)&hihi_[0]) )
00909 {
00910 hihiAlarmed_ = true;
00911 createPacketMask |= 1 << 3;
00912 }
00913 }
00914 else if(useType == 2)
00915 {
00916 __COUT__ << "Using float for alarms." << std::endl;
00917
00918 if((!loloAlarmed_ || !latchAlarms_) &&
00919 *((float *)&sample_[0]) <=
00920 *((float *)&lolo_[0]) )
00921 {
00922 loloAlarmed_ = true;
00923 createPacketMask |= 1 << 0;
00924 }
00925
00926 if((!loAlarmed_ || !latchAlarms_) &&
00927 *((float *)&sample_[0]) <=
00928 *((float *)&lo_[0]) )
00929 {
00930 loAlarmed_ = true;
00931 createPacketMask |= 1 << 1;
00932 }
00933
00934 if((!hiAlarmed_ || !latchAlarms_) &&
00935 *((float *)&sample_[0]) >=
00936 *((float *)&hi_[0]) )
00937 {
00938 hiAlarmed_ = true;
00939 createPacketMask |= 1 << 2;
00940 }
00941
00942 if((!hihiAlarmed_ || !latchAlarms_) &&
00943 *((float *)&sample_[0]) >=
00944 *((float *)&hihi_[0]) )
00945 {
00946 hihiAlarmed_ = true;
00947 createPacketMask |= 1 << 3;
00948 }
00949 }
00950 else if(useType == 3)
00951 {
00952 __COUT__ << "Using double for alarms." << std::endl;
00953
00954 if((!loloAlarmed_ || !latchAlarms_) &&
00955 *((double *)&sample_[0]) <=
00956 *((double *)&lolo_[0]) )
00957 {
00958 loloAlarmed_ = true;
00959 createPacketMask |= 1 << 0;
00960 }
00961
00962 if((!loAlarmed_ || !latchAlarms_) &&
00963 *((double *)&sample_[0]) <=
00964 *((double *)&lo_[0]) )
00965 {
00966 loAlarmed_ = true;
00967 createPacketMask |= 1 << 1;
00968 }
00969
00970 if((!hiAlarmed_ || !latchAlarms_) &&
00971 *((double *)&sample_[0]) >=
00972 *((double *)&hi_[0]) )
00973 {
00974 hiAlarmed_ = true;
00975 createPacketMask |= 1 << 2;
00976 }
00977
00978 if((!hihiAlarmed_ || !latchAlarms_) &&
00979 *((double *)&sample_[0]) >=
00980 *((double *)&hihi_[0]) )
00981 {
00982 hihiAlarmed_ = true;
00983 createPacketMask |= 1 << 3;
00984 }
00985 }
00986
00987
00988
00989 std::string *alarmValueArray[] = {
00990 &lolo_, &lo_, &hi_, &hihi_
00991 };
00992
00993 if(monitoringEnabled_)
00994 {
00995
00996 char checkMask = 1;
00997 for(int i=0;i<4;++i, checkMask <<= 1)
00998 if(createPacketMask & checkMask)
00999 {
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010 __COUT__ << "Create packet type " << i+1 <<
01011 " alarm value = " << *alarmValueArray[i] << std::endl;
01012
01013 __COUT__ << "before txBuffer sz=" << txBuffer.size() << std::endl;
01014 txBuffer.push_back(i+1);
01015 txBuffer.push_back(txPacketSequenceNumber_++);
01016
01017 txBuffer.resize(txBuffer.size() + sizeof(lastSampleTime_));
01018 memcpy(&txBuffer[txBuffer.size() - sizeof(lastSampleTime_)],&lastSampleTime_,sizeof(lastSampleTime_));
01019
01020 unsigned int tmpSz = fullChannelName_.size();
01021
01022 txBuffer.resize(txBuffer.size() + sizeof(tmpSz));
01023 memcpy(&txBuffer[txBuffer.size() - sizeof(tmpSz)],&tmpSz,sizeof(tmpSz));
01024
01025 txBuffer += fullChannelName_;
01026
01027 txBuffer.push_back((unsigned char)(*alarmValueArray[i]).size());
01028 txBuffer.push_back((unsigned char)sizeOfDataTypeBits_);
01029
01030 txBuffer += (*alarmValueArray[i]);
01031 __COUT__ << "after txBuffer sz=" << txBuffer.size() << std::endl;
01032
01033 {
01034 __SS__ << "txBuffer: \n";
01035 for(unsigned int i=0; i<txBuffer.size(); ++i)
01036 {
01037 ss << std::hex << (int)((txBuffer[i]>>4)&0xF) <<
01038 (int)((txBuffer[i])&0xF) << " " << std::dec;
01039 if(i%8 == 7) ss << std::endl;
01040 }
01041 ss << std::endl;
01042 __COUT__ << "\n" << ss.str();
01043 }
01044 }
01045 }
01046
01047 return createPacketMask;
01048 }
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077