/* 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. */ //!================================================================================================= //! //! A base class for modules in the EPICS driver for IU PMT base control //! //!================================================================================================= #ifndef _BaseIUModule_HH_ #define _BaseIUModule_HH_ extern "C" { #include #include #include #include #include #include }; #include #include #include #include using namespace std; #include "MtxLock.hh" #include "BaseIUDevice.hh" //! a base class for module for IU PMT bases to be used //! as base class by the classes for the buses and for the boards class BaseIUModule { protected: //! CANbus device connection object pointer BaseIUDevice* bmDevice; //! An ID for this device . It is used as the board address int bmAddress; //! Mutex for an object pthread_mutex_t bmMutex; //! Mutex attributes for an object pthread_mutexattr_t bmMtxAttr; //! Destructor. It is protected and virtual virtual ~BaseIUModule(); //! Initialize the mutex for the object void InitMutex(); //! Destroy the mutex for the object void CloseMutex(); public: static int bmDummyInt; //! Dummy static variable for initialization static unsigned bmDebFlag; //! Debug level indicator static pthread_mutex_t bmGlobMutex; //! Global mutex for this class static pthread_mutexattr_t bmGlobMtxAttr; //! Global mutex attributes //! Constructor. Initially communication device pointer is set to null BaseIUModule( BaseIUDevice* devPtr = 0, int addr = 0 ); //! Second constructor that looks like a copy constructor //! but allowing to have an address argument BaseIUModule( const BaseIUModule& module, int addr ); //! Copy constructor BaseIUModule( const BaseIUModule& module ); //! Copy constructor BaseIUModule& operator=( const BaseIUModule& module ); //! Function to synchronize modules virtual bool Sync( bool dir = 0 ) = 0; //! Return number of microseconds static inline suseconds_t GetTime() { struct timeval currTime; gettimeofday( &currTime, NULL ); return (1000000 * currTime.tv_sec + currTime.tv_usec); } //! Initialize global mutex static int InitGlobalMutex(); //! Get the pointer to the communication device for this object //! We drop the constness of the bmMutex to be able to place lock. inline BaseIUDevice* GetDevice() const { MtxLock objLock( const_cast(bmMutex) ); return bmDevice; } //! Get the address of the module inline int GetAddress() const { MtxLock objLock( const_cast(bmMutex) ); return bmAddress; } //! Set the pointer to the communication device object inline BaseIUDevice* SetDevice( BaseIUDevice* portPtr ) { MtxLock objLock(bmMutex); return (bmDevice = portPtr); } //! Set the pointer to the communication device object inline int SetAddress( int address ) { MtxLock objLock(bmMutex); return (bmAddress = address); } //! Lock the Global mutex for this class inline static int GlobLock() //! Lock static structures access { return pthread_mutex_lock( &bmGlobMutex ); } //! Unlock the global mutex for this class inline static int GlobUnlock() //! Unlock static structures access { return pthread_mutex_unlock( &bmGlobMutex ); } }; #endif // _BaseIUModule_HH_