/** @file * $Author: kusanagi $ * $Date: 2004/07/06 13:31:18 $ * $Revision: 1.2 $ * * Implementation of the functions of a sample Algorithm class. * This file can be used as a templace to develop your own algorithm. */ #include #include #include "TechmodelAlgorithm.h" #include "EventReader.h" #include "ReaderAlgorithms.h" #include "Exception.h" #include "stdio.h" extern "C" { #include "CRC.h" } #define BYTE unsigned char using namespace pamela; using namespace pamela::techmodel; static log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.EventReader"); static unsigned int prevPckCounter = 0; static unsigned int prevPckOBT = 0; /** * Constructor. */ EventReader::EventReader(void): TechmodelAlgorithm(0, "TechmodelEventReader") { cat << log4cpp::Priority::DEBUG << "Constructor " << "\n " << log4cpp::CategoryStream::ENDLINE; Header = new EventHeader(); /* TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Alarm, new AlarmReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Khb, new KhbReader())); */ TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Physics, new PhysicsReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ForcedPkt, new ForcedPktReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunHeader, new RunHeaderReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunTrailer, new RunTrailerReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TabDump, new TabDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Tmtc, new TmtcReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Mcmd, new McmdReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCal, new CalibCalReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPed,new CalibCalPedReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCal, new CalibAcReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk1, new CalibTrk1Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk2, new CalibTrk2Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrd, new CalibTrdReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTof, new CalibTofReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibS4, new CalibS4Reader())); /* TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::HA_Header_E5, new E5Reader())); */ } /** * Get a string with the version info of the algorithm. */ std::string EventReader::GetVersionInfo(void) const { return "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.2 2004/07/06 13:31:18 kusanagi Exp $\n"; } /** * Initialize the algorithm with a special run. This will initialize the * event reader routines for all packet types. */ void EventReader::Init(PamelaRun *run) { SetInputStream(run); //Create the structure of directories and create xxx.Header.root files run->WriteHeaders(this, &Header); //Create the xxx.root in it's specific directory for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); i != TechmodelAlgorithmMap.end(); i++) { cat << log4cpp::Priority::DEBUG << " Initializing algo " << i->second->GetAlgorithmName() << "\n " << log4cpp::CategoryStream::ENDLINE; i->second->Init(run); } Run = dynamic_cast(run); } static void SkipToNextHeader(ifstream *); /** * Read the next event header, call the reader algorithms that * correspond to its packet type, and read the event trailer. */ // 15 June 2004 ---note------------------ // A nice refacoring would require to not read again the packet data (which is // read in the UnpackPscuHeader() and put directly in the // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... // 15 June 2004 ---note------------------ void EventReader::RunEvent(int EventNumber) { cat << log4cpp::Priority::ERROR << "Start Unpacking........" << "\n " << log4cpp::CategoryStream::ENDLINE; //for now i'll suppose that the raw file starts immediately with the right bytes //to insert a GetPacketStart() while (!InputFile->eof()){ try { if (FindStart()) { if(UnpackPscuHeader()){ const PacketType* type = Header->GetPscuHeader()->GetPacketType(); AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); if (i != TechmodelAlgorithmMap.end()) { TechmodelAlgorithm *EventAlgorithm = i->second; EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); Run->FillTrees(type); Header->GetCounter()->Increment(type); } else { cat << log4cpp::Priority::ERROR << "No way to read events of type" << type->GetName() << "\n " << log4cpp::CategoryStream::ENDLINE; throw Exception("No way to read events of type " + type->GetName()); } } //else { //**TO BE DONE** <>// //Have to be defined a "WrongCRC" packet::Type? //} } } catch (...) { //This have to be more detailed. More exceptions type are needed. cat << log4cpp::Priority::ERROR << "Couldn't read the event. Skipping to the next header." << "\n " << log4cpp::CategoryStream::ENDLINE; } } Header->GetCounter()->PrintCounters(); } static void SkipToNextHeader(ifstream* TechmodelFile) { // yeah. } /** * Unpack the PSCU header from a file into the structure. */ int EventReader::UnpackPscuHeader(void) throw (std::exception) { int response; char buff[16]; InputFile->read(buff, sizeof(buff)); prevPckCounter = 0; prevPckOBT = 0; unsigned char PacketId1 = buff[3]; unsigned char PacketId2 = buff[4]; unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); unsigned char CRC = buff[15]; if (Counter < prevPckCounter){ cat << log4cpp::Priority::ERROR << " Packet counter is less than before of " << (prevPckCounter - Counter) << "\n " << log4cpp::CategoryStream::ENDLINE; } if (OrbitalTime < prevPckOBT){ cat << log4cpp::Priority::WARN << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime) << "\n " << log4cpp::CategoryStream::ENDLINE; } if ((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]){ long int initPos = InputFile->tellg(); long int finalPos; Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); Header->GetPscuHeader()->SetCounter(Counter); Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); //PacketLength is the length of the whole DATApacket starting from the first byte after the header //plus the CRC legth (which varies for each type of packet) Header->GetPscuHeader()->SetPacketLenght(PacketLenght); Header->GetPscuHeader()->SetCRC(CRC); //commented out because of the above test code InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur); FindStart(); finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); if(finalPos == 0){ cat << log4cpp::Priority::INFO << " Correct packet length \n" << "\n " << log4cpp::CategoryStream::ENDLINE; } if (finalPos > 0 && finalPos < 64) { cat << log4cpp::Priority::WARN << " Correct packet length: Padded of " << finalPos << " bytes \n" << "\n " << log4cpp::CategoryStream::ENDLINE; } if (finalPos > 64){ cat << log4cpp::Priority::ERROR << " Wrong packet length? (because of wrong padding?) \n" << "\n " << log4cpp::CategoryStream::ENDLINE; } InputFile->seekg(initPos, std::ios::beg); response = true; } else { cat << log4cpp::Priority::ERROR << " WRONG CRC FOR HEADER " << "\n " << log4cpp::CategoryStream::ENDLINE; response = false; } char tmpId1[4]; char tmpId2[4]; char tmpLength[12]; char tmpStart[100]; char tmpCRC[4]; sprintf(tmpId1, "%02X", PacketId1); sprintf(tmpId2, "%02X", PacketId2); sprintf(tmpLength, "%06X", PacketLenght); sprintf(tmpStart, "%X", ((long int)(InputFile->tellg()) - 16)); sprintf(tmpCRC, "%02X", CRC); cat << log4cpp::Priority::INFO << "\n Packet Counter (decimal) : " << Counter << "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 << "\n Orbital Time (decimal) : " << OrbitalTime << "\n Lenght : " << tmpLength << "\n CRC : " << tmpCRC << "\n Header Start Position : " << tmpStart << "\n " << log4cpp::CategoryStream::ENDLINE; return response; } /** * Unpack the trailer of a PSCU event into the structure. */ void EventReader::UnpackPscuTrailer(void) throw (std::exception) { } /** * Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence */ int EventReader::FindStart(void) throw (std::exception) { //search an hexadecimal sequence in a file //subSign ------> pointer to the sequence buffer //subSignDim ------> dimension of the buffer // at exit // return true if founds a match (else false) // subF point rigth after the match, if found. Else EOF. //Maurizio 15/11/2002----------------------- const unsigned char subSign[3]={0xFA, 0xFE, 0xDE}; int subSignDim = 3; //------------------------------------------ int subIndex = 0; char dataByte; /*InputFile->read(fileByte, 1); if ( *fileByte == *(subSign+subIndex)){ if (subIndex++ == (subSignDim-1)) { InputFile->seekg(-(subIndex), std::ios::cur); return 1; } }*/ while (!InputFile->eof()) { InputFile->get(dataByte); if (dataByte == (char)(*(subSign+subIndex))){ if (subIndex++ == (subSignDim-1)) { InputFile->seekg(-(subIndex), std::ios::cur); return 1; } } else { if (InputFile->eof()) { throw Exception("Extra bytes over the last packets"); } else { return 0; } //InputFile->seekg(-(subIndex+1), std::ios::cur); //subIndex = 0; } } return 0; } ClassImp(EventReader)