/* 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. */ //================================================================================================== // // CLAS to handle a CANbus with a set of IU Bases or chassis // Software Library for Linux // // // Purpose: class for IU Base control on one bus // Date: April 27, 2014 // Author: Hovanes Egiyan // // //================================================================================================== #ifndef _BaseIUVirtBus_HH_ #define _BaseIUVirtBus_HH_ extern "C" { #include #include } ; #include #include #include #include #include #include #include #include "boost/function.hpp" #include "boost/bind.hpp" #include #include "BaseIUMessage.hh" #include "BaseIUCommand.hh" #include "BaseIUDevice.hh" #include "BaseIUModule.hh" #include "BaseIUVirtPar.hh" #include "BaseIUVirtBoard.hh" #include "BaseIUException.hh" using namespace std; //! Adopt the device identification type for connection //! to be the identification type for the buses by inheriting from it. //! Bus ID will also keep track of the type connected to the CAN bus. class BaseIUVirtBusID: public BaseIUDevice::DeviceID { protected: // This is the additional data to define the bus type string bdiType; public: // main constructor BaseIUVirtBusID(string ip = "127.0.0.01", string bus = "", string portName = "", string type = "base") : BaseIUDevice::DeviceID(ip, bus, portName), bdiType(type) { return; } // Copy constructor BaseIUVirtBusID ( const BaseIUVirtBusID& busID ) : BaseIUDevice::DeviceID( busID ) { bdiType = busID.bdiType; return; } BaseIUVirtBusID& operator=( const BaseIUVirtBusID& busID ) { const BaseIUDevice::DeviceID* tmpThisPtr = dynamic_cast(this); const BaseIUDevice::DeviceID* tmpBusPtr = dynamic_cast(&busID); BaseIUDevice::DeviceID* tmpVarThisPtr = const_cast( tmpThisPtr ); BaseIUDevice::DeviceID* tmpVarBusPtr = const_cast(tmpBusPtr); *tmpVarThisPtr = *tmpVarBusPtr; bdiType = busID.bdiType; return *this; } BaseIUVirtBusID& operator+( const unsigned busNum ) { BaseIUDevice::DeviceID* tmpPtr = dynamic_cast(this); *tmpPtr = *dynamic_cast(this) + busNum; return *this; } BaseIUVirtBusID& operator+( const string busName ) { BaseIUDevice::DeviceID* tmpPtr = dynamic_cast(this); *tmpPtr = *dynamic_cast(this) + busName; return *this; } inline string GetType() const { return bdiType; } inline string SetType(const string type) { return (bdiType = type); } }; //! BaseIUVirtBus virtual class to contain information about the //! whole bus module state. It contains an STL map with //! addresses of all boards belonging to this bus. Each object is //! supposed to be constructed in a new thread and its Sync() method //! should be called periodically to synchronize the parameters in //! the object of this class and the corresponding hardware module. //! This class inherits from BaseIUModule class, therefore it //! contains the pointer to the serial port object. The port objects //! are created in the constructor of this class. class BaseIUVirtBus: public BaseIUModule { public: // Type of function returning bool with arguments of type bool and string (SetBit) typedef bool (BaseIUVirtPar::*SetBitPtrType)(const bool val, const string name) ; // Boost function of type void without any parameter typedef boost::function PriorityFuncType; protected: //! Sleep time in microseconds between attempts to //! find parameter readbacks in anagates FIFO. static unsigned long bvbSleepTime; //! Maximum wait time in microseconds after which the //! normal synch is timed out. static long bvbTimeOutTime; //! Maximum wait time in microseconds after which the //! parameter initialization synch is timed out static long bvbTimeOutTimeInit; //! Communication status for this bus bool bvbCommOK; //! Number of communication failure counts unsigned long bvbFailureCount; // Id for this bus, include devceiID and and the type of the bus BaseIUVirtBusID bvbBusID; //! Map of IU base board objects. First key is the board address, //! the value is an object pointer for IU HV base board. map bvbBoard; // Container to keep a list of scheduled priority calls vector bvbPriorityCall; //! Not allowed to invoke the default constructor or //! a constructor with a defined device address BaseIUVirtBus(BaseIUDevice* devPtr = 0) : BaseIUModule(devPtr) { ; } //! Copy constructor protected BaseIUVirtBus(BaseIUVirtBus& bus); //! Copy operator protected. Copying connection device would //! not make much sense. We want one bus attached to a single //! connection device. virtual BaseIUVirtBus& operator=(BaseIUVirtBus& bus); virtual vector GetAddressVector(); //! Check if the chassis in the vector of responding chassis contains //! exactly the same boards as in the initial map of boards virtual bool CheckAddressVector(vector& addrVec); // Find and sync the missing boards (including parameters on these boards). // Return a vector of addresses for the boards that are still missing. virtual vector SyncMissingBoards(vector addrVec, BaseIUVirtPar& par, bool syncFromHW = 0); // Find the missing boards (including parameters on these boards). // Return a vector of addresses for the boards that are still missing. virtual vector FindMissingBoards(vector addrVec, BaseIUVirtPar& par); //! First key is the type, second key is the name of the parameter map > bvbPar; //! Initialize parameters that belong to the bus itself virtual void InitParameters() = 0; // Method to sync the parameters of the bus itself virtual bool SyncPars( vector& addrVec, bool syncFromHW); // Method to sync one parameter by broadcasting and receiving from the whole bus virtual bool SyncParsOnBoards( BaseIUVirtPar* parPtr, vector& addrVec, bool syncFromHW = 0 ); virtual bool SyncBusPar( BaseIUVirtPar* parPtr, vector& addrVec, bool syncFromHW = 0 ); // Increment the new silent time for the parameter specified by the fakePar pointer on the board specified by the vector // of addresses by uSecTime and check the alarm status. virtual void CheckBoardParameterAlarms( BaseIUVirtPar* parPtr, const vector& missingBoards, const long uSecTime ) ; public: //! Main constructor BaseIUVirtBus(BaseIUVirtBusID& busID); //! Destructor virtual ~BaseIUVirtBus(); //! Method to synchronize the information in the bases //! and the in the corresponding object in this class. virtual bool Sync(bool dir = 0); // Set values for all parameters for certain types and name virtual void SetParameter( string type, string name, void* setValPtr ) = 0; //! Return the bus ID virtual inline BaseIUVirtBusID GetBusID() const { return bvbBusID; } //! Method to return address of the STL map containing the //! objects corresponding to the boards on this bus. virtual inline map GetBoardMap() { MtxLock objLock(bmMutex); return bvbBoard; } // Return how many time bus operation failure accured. virtual inline unsigned long GetFailureCount() { MtxLock objLock(bmMutex); return bvbFailureCount; } virtual inline unsigned GetNumberOfRealBoards() { return this->GetBoardMap().size() - 1 ; } //! Method to return the pointer to the object corresponding to the //! board with address boardAddr on this bus. virtual BaseIUVirtBoard* GetBoard(int boardAddr); //! Get the pointer to the parameter with a given type and name virtual BaseIUVirtPar* GetParameter(string type, string name ); // Set a priority call to a function with a void pointer that will be executed somewhere // in the middle of the bus Sync method. virtual void AddPrioritySyncCall( PriorityFuncType callFunc ) { MtxLock objLock(bmMutex); bvbPriorityCall.push_back( callFunc ); return; } // Return the timeout time for regular syncing (syncFromHW = false) or // for initialization syncing (syncFromHW = true). inline virtual long GetTimeouTime( const bool syncFromHW = false ) { if( syncFromHW ) { return ( bvbTimeOutTimeInit ); } else { return bvbTimeOutTime; } } }; #endif // _BaseIUVirtBus_HH_