/* Copyright (c) 20011 Hovanes Egiyan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "BaseIUVirtBoard.hh" //! Default constructor BaseIUVirtBoard::BaseIUVirtBoard() : BaseIUModule(), bvbPar() { if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::BaseIUVirtBoard() : Default constructor is invoked" << " The BaseIUVirtBoard object is at " << this << endl; } return; } //! Main constructor, the first argument is actually a reference to //! the bus. The second argument comes separately since the buss //! does not carry any address (it is 0) BaseIUVirtBoard::BaseIUVirtBoard(const BaseIUModule& module, int addr) : BaseIUModule(module, addr), bvbPar() { if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::BaseIUVirtBoard() : Constructing board with address " << hex << showbase << bmAddress << " on port " << bmDevice->GetPortName() << endl; cout << "The BaseIUVirtBoard object is at " << this << endl; } //! Initialize the map with parameters // InitParameters(); return; } //! Copy constructor BaseIUVirtBoard::BaseIUVirtBoard(const BaseIUVirtBoard& board) : BaseIUModule(board) { //! Initialize the map with parameters if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::BaseIUVirtBoard() : Copy constructor invoked" << endl; } //! After all initialize, copy the content *this = operator=(board); return; } BaseIUVirtBoard::~BaseIUVirtBoard() { if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::~BaseIUVirtBoard() : Destructor for board with address " << bmAddress << " has been called" << endl; } MtxLock objLock(bmMutex); //! Loop over all parameters of the board and delete them map >::iterator itType; for (itType = bvbPar.begin(); itType != bvbPar.end(); itType++) { string parType = itType->first; map& nameMap = itType->second; map::iterator itName; for (itName = nameMap.begin(); itName != nameMap.end(); itName++) { string parName = itName->first; BaseIUVirtPar* par = itName->second; if (par != 0) { if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::~BaseIUVirtBoard() Will delete parameter " << hex << showbase << par->GetType() << " : " << par->GetName() << endl; } delete par; } } } return; } BaseIUVirtBoard& BaseIUVirtBoard::operator=(const BaseIUVirtBoard& constBoard) { if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::operator=() : Assignment operator is called" << endl; } if (this == &constBoard) return *this; BaseIUVirtBoard& board = const_cast(constBoard); BaseIUModule::operator=(board); MtxLock objLock(bmMutex); //! Clear the map of parameters since it is going to be //! recreated below this->bvbPar.clear(); //! Loop over all parameters in the map of the board and copy the //! content of these maps into the map of this objects map >::iterator itType; map >& typeMap = board.GetParMap(); for (itType = typeMap.begin(); itType != typeMap.end(); itType++) { string parType = itType->first; map& nameMap = itType->second; map::iterator itName; // cout << "Go through type " << parType << endl; for (itName = nameMap.begin(); itName != nameMap.end(); itName++) { string parName = itName->first; BaseIUVirtPar* par = itName->second; // cout << "Name is " << parName << endl; //! Create a clone of the parameter polymorphically and assigned //! its address to the corresponding map element of this object //! The map element will be created if it does not exist (should not) BaseIUVirtPar* tmpPar = par->CloneParameter(); // cout << "Will assign " << parName << endl; this->bvbPar[parType][parName]; // cout << "Map should be ready"<< endl; this->bvbPar[parType][parName] = tmpPar; } } if (bmDebFlag > 1) { cout << "BaseIUVirtBoard::operator= : Board assignment is complete " << endl; } return *this; } void BaseIUVirtBoard::InitParameters(){ cout << "BaseIUVirtBoard::InitParameters() : Using wrong InitParameters() method " << endl; return; } //! Sync this board. At this point it is assumed that the readback for //! all parameters was requested. bool BaseIUVirtBoard::Sync( bool syncFromHW ) { cout << "BaseIUVirtBoard::Sync() : Using wrong Sync() method" << endl; return true; } //! Sync only one parameter explicitly with the name matching to the name and type //! of the parameter given in the argument list bool BaseIUVirtBoard::SyncOnePar( BaseIUVirtPar& fakePar, bool syncFromHW ) { MtxLock objLock( bmMutex ); if ( bmDebFlag > 1 ) { cout << "BaseIUVirtBoard::SyncOnePar(): Will sync parameter " << hex << showbase << fakePar.GetType() << " : " << fakePar.GetName() << " on board with address " << bmAddress << endl; } if ( bvbPar.count( fakePar.GetType() ) > 0 && bvbPar[fakePar.GetType()].count( fakePar.GetName() ) > 0 ) { BaseIUVirtPar* realPar = bvbPar[fakePar.GetType()][fakePar.GetName()]; // cout << "Parameter address is " << hex << showbase << realPar << endl; bool syncSuccessful = realPar->Sync( syncFromHW ); // If syncing successful then reset the silent time for this parameter if( realPar->GetStatus() > NO_ALARM && syncSuccessful ) cout << "BaseIUVirtBoard::SyncOnePar: Will reset alarm time to 0 for parameter " << fakePar.GetType() << " : " << fakePar.GetName() << " on board with address " << hex << showbase << bmAddress << dec << endl; if( syncSuccessful ) realPar->SetSilentTime( 0 ); return syncSuccessful; } else { return false; } } //! Find one parameter in the FIFO with the name matching to the name and type //! of the parameter given in the argument list. Return true if there was a //! message from this board for the parameter for that name, and falso if //! there was no response. bool BaseIUVirtBoard::FindOnePar( BaseIUVirtPar& fakePar ) { MtxLock objLock( bmMutex ); if ( bmDebFlag > 1 ) { cout << "BaseIUVirtBoard::FindOnePar(): Will find parameter " << hex << showbase << fakePar.GetType() << " : " << fakePar.GetName() << " on board with address " << bmAddress << dec << endl; } bool msgIsFound = false; // cout << "Number of parameters of " << fakePar.GetType() << " type is " << // bvbPar.count( fakePar.GetType() ) << endl; if( bvbPar.count( fakePar.GetType() ) > 0 ) // cout << "Number of parameters with " << fakePar.GetType() << ":" << fakePar.GetName() // << " name is " << bvbPar[fakePar.GetType()].count( fakePar.GetName() ) << endl; if ( bvbPar.count( fakePar.GetType() ) > 0 && bvbPar[fakePar.GetType()].count( fakePar.GetName() ) > 0 ) { BaseIUVirtPar* realPar = bvbPar[fakePar.GetType()][fakePar.GetName()]; // cout << "Parameter pointer that was found is " << realPar << endl; if ( realPar != 0 ) msgIsFound = realPar->FindMsgInFIFO(); } // cout << "FindOnePar will return value " << msgIsFound << " for " // << fakePar.GetType() << ":" << fakePar.GetName() << endl; return msgIsFound; } //! Get the pointer to the parameter with a given type and name //! after checking its existence BaseIUVirtPar* BaseIUVirtBoard::GetParameter(string type, string name) { MtxLock objLock(bmMutex); if (bvbPar.count(type) == 0) return 0; if (bvbPar[type].count(name) == 0) return 0; return bvbPar[type][name]; }