/* * File: Device.cc * Author: lpiccoli * * Created on May 28, 2010, 11:38 AM */ #include #include "Device.h" #include "FileChannel.h" #include "CaChannel.h" #include "FcomChannel.h" #include "NullChannel.h" #include "Log.h" #include "PatternManager.h" #include #include #include #include #include USING_FF_NAMESPACE /** * Device constructor, allocates the internal buffer according to the specified * size. * * @param name device name (used by ChannelAccess and/or FCOM to access real * device) * @param bufferSize number of data points that the internal buffer can hold * @param patternMask * @param patternIndex * @author L.Piccoli */ Device::Device(std::string slotName, std::string name, int bufferSize, PatternMask patternMask, int patternIndex) : _slotName(slotName), _name(name), _buffer(0), _bufferSize(bufferSize), _next(0), _communicationChannel(0), _patternMask(patternMask), _patternIndex(patternIndex), _hihiPv(slotName + " " + name + "HIHI", 1000), _highPv(slotName + " " + name + "HIGH", 500), _lowPv(slotName + " " + name + "LOW", -500), _loloPv(slotName + " " + name + "LOLO", -1000), _caModePv(slotName + " " + name + "CAMODE", false), _usedPv(slotName + " " + name + "USED", false), _devNamePv(slotName + " " + name + "DEVNAME", "noname"), _fbckCommunicationChannel(NULL), _setFbckPv(true), _usedByLoopPv(slotName + " " + name + "USEDBYLOOP", true), _channelCountPv(slotName + " " + name + "DATACNT", 0), _channelErrorCountPv(slotName + " " + name + "DATAERRORS", 0) { _buffer = new DataPoint[bufferSize]; for (int i = 0; i < _bufferSize; ++i) { _buffer[i]._status = DataPoint::EMPTY; } } /** * Device destructor, deletes the internal buffer */ Device::Device(const Device& orig) { delete [] _buffer; } Device::~Device() { } /** * Set the communication channel for this Device. * * @param newChannel the new communication channel * @author L.Piccoli */ void Device::setCommunicationChannel(CommunicationChannel *newChannel) { _communicationChannel = newChannel; } /** * Clear contents of internal buffer and reset pointers. * * @author L.Piccoli */ void Device::clear() { for (int i = 0; i < _bufferSize; ++i) { _buffer[i]._status = DataPoint::EMPTY; } _next = 0; } /** * Set the pattern mask for this device. * * @param patternMask the new pattern mask * @author L.Piccoli */ void Device::setPatternMask(PatternMask patternMask) { _patternMask = patternMask; } /** * Returns the pattern mask associated with the device. * * @return pattern mask for this device * @author L.Piccoli */ PatternMask Device::getPatternMask() { return _patternMask; } /** * Verifies if this device is the same as the specified other device. * The devices are considered the same if the name and the pattern masks are the * same. * * @param other the other device being compared * @returns true if names and patterns are the same, false otherwise * @author L.Piccoli */ bool Device::operator==(const Device &other) { if (_name == other._name && _patternMask == other._patternMask) { return true; } else { return false; } } /** * Checks whether a this device and another one are different. Devices are * different if either name or pattern mask or both are different. * * @param other the other device being compared * @returns true if names and pattern masks are the same, false otherwise * @author L.Piccoli */ bool Device::operator!=(const Device &other) { return !(*this == other); } /** * * TODO: This relies on the Pattern::operator< which is not totally implemented! * * @author L.Piccoli */ bool Device::operator<(const Device &other) { // If names begin with 'M', 'A' or 'S' then the order // depends on the number following the first letter. // The PV database is configured such that the devices are named: // M1, M2..., M10, M11,... and ordering by string does not work // because M10 < M1 -> we need M1 < M10. // We need to extract the device index from the name and compare those. if (_name[0] == other._name[0] && (_name[0] == 'M' || _name[0] == 'A' || _name[0] == 'S')) { if (_name.size() < other._name.size()) { return true; } else if (_name.size() > other._name.size()) { return false; } } if (_name.compare(other._name) > 0) { return false; } else if (_name.compare(other._name) < 0) { return true; } else { return _patternMask < other._patternMask; } } /** * Display information about the device. * * @author L.Piccoli */ void Device::show() { std::string used = "true"; std::string channel = "CA"; if (_caModePv == false) { channel = "FCOM"; } if (_usedPv == false) { used = "false"; } std::cout << _name << ": " << _devNamePv.getValue() << " (" << channel << ", used=" << used << ", index=" << _patternIndex << ") "; if (_communicationChannel == NULL) { std::cout << "[NO CHANNEL]"; } else { std::cout << "[read=" << _communicationChannel->getReadCount() << "/err=" << _communicationChannel->getReadErrorCount() << ", write=" << _communicationChannel->getWriteCount() << "/err=" << _communicationChannel->getWriteErrorCount() << "] "; } } /** * Disconnects the device from the CommunicationChannels. This method is * invoked when a Loop is stopped. * * Reconnection occurs when the Loop is started (see reconfigure() method). * * @author L.Piccoli */ void Device::disconnect() { delete _communicationChannel; delete _fbckCommunicationChannel; _communicationChannel = NULL; _fbckCommunicationChannel = NULL; Log::getInstance() << "INFO: " << _devNamePv.getValue().c_str() << " disconnected." << Log::flush; } /** * Reconfigure the device. This involves: * - Check device name through $(LOOP):*DEVNAME PV * - Verify whether actual device is accessible through ChannelAccess or FCOM * * @param accessType * @param patternIndex (optional, default value -1). Used if FileChannel is * used * @return 0 on success, -1 on failure * @author L.Piccoli */ int Device::configure(CommunicationChannel::AccessType accessType, int patternIndex) { if (_communicationChannel != NULL) { disconnect(); } std::string name = _devNamePv.getValue(); // If _devName starts with "FILE:", and NO_CHANNEL is selected, // change accessType to WRITE_ONLY -> this is for debugging StateDevices // allowing values to be written to file instead of being discarded if (accessType == CommunicationChannel::NO_CHANNEL && isFile()) { accessType = CommunicationChannel::WRITE_ONLY; } if (accessType == CommunicationChannel::NO_CHANNEL) { _communicationChannel = new NullChannel(accessType); _fbckCommunicationChannel = new NullChannel(accessType); return 0; } // Check if the device name indicates that the channel to be used is of // type FileChannel (for testing/debugging only) // Attention: the string can have up to 40 characters only! if (isFile()) { if (createFileChannel(name, patternIndex, accessType) != 0) { return -6; } return 0; } // If device name starts with "NULL" create a NullChannel if (isNull()) { _communicationChannel = new NullChannel(accessType); _fbckCommunicationChannel = new NullChannel(accessType); return 0; } if (name == "") { Log::getInstance() << "ERROR: Device has empty DEVNAME" << Log::flush; return -2; } if (_caModePv == true) { try { _communicationChannel = new CaChannel(accessType, name); } catch (std::exception &e) { Log::getInstance() << "ERROR: failed to create CaChannel for PV " << name.c_str() << Log::flush; return -3; } } else { try { _communicationChannel = new FcomChannel(accessType, name); } catch (std::exception &e) { Log::getInstance() << "ERROR: failed to create FcomChannel for PV " << name.c_str() << Log::flush; return -4; } } // Create a ChannelAccess connection to set the FBCK device _fbckCommunicationChannel = NULL; if (!isFile() && _setFbckPv) { if (createFbckPv(accessType) != 0) { Log::getInstance() << "ERROR: failed to create FBCK PV for " << name.c_str() << Log::flush; return -5; } } return 0; } /** * If the Device is supposed to have a FBCK PV to be controlled, this method creates * a CaChannel to it. * * The name of the FBCK PV for measurement devices and actuators that are not RF * (PV does not start with ACCL) have the last PV field name replaced with the string * "FBCK", e.g.: * * BPMS:LI20:230:X -> BPMS:LI20:230:FBCK * * RF actuators whose PV name starts with ACCL must have the "FBCK" string appended * to the original nome, e.g.: * * ACCL:LI24:2:ADES -> ACCL:LI24:2:ADES:FBCK * * If the device uses FCOM, then the name will have a ":GETFCOM_x" added to the name. * this string must be removed before appending/replacing with the "FBCK" string. * * @return 0 on success, -1 on failure * @author L.Piccoli */ int Device::createFbckPv(CommunicationChannel::AccessType accessType) { // If name contains ":GETFCOM_x" then we must remove it before // appending ":FBCK" std::string deviceName = _devNamePv.getValue(); std::string fcomString = ":GETFCOM_"; size_t found = deviceName.find(fcomString); if (found != std::string::npos) { deviceName = deviceName.substr(0, (int)(found)); } std::string acclName = "ACCL:"; found = deviceName.find(acclName); std::string fbckName; size_t lastColon = deviceName.find_last_of(":");// + 1; if (lastColon == std::string::npos) { Log::getInstance() << "ERROR: failed to create FBCK CaChannel" << Log::flush; return -1; } // If has ACCL, then append ":FBCK" to the PV name if (found != std::string::npos) { fbckName = deviceName; fbckName += ":FBCK"; } // if there is no ACCL, remove last part of PV name and add ":FBCK" else { fbckName = deviceName.substr(0, lastColon); fbckName += ":FBCK"; } try { _fbckCommunicationChannel = new CaChannel(accessType, fbckName); } catch (std::exception &e) { Log::getInstance() << "ERROR: failed to create FBCK CaChannel for PV " << fbckName.c_str() << Log::flush; return -1; } return 0; } /** * Create a FileChannel instance if the Device is configured to read/write * to files. Device name must start with string 'FILE:'. * * If there are two or more patterns registered with the PatternManager, * then the file name is appended to the pattern index returned for the * pattern hold by the device. * * @return 0 if file could be opened for reading or writing, -1 on failure * @author L.Piccoli */ int Device::createFileChannel(std::string name, int patternIndex, CommunicationChannel::AccessType accessType) { std::string fileName = name.substr(5, name.npos); if (patternIndex > 0) { fileName = getFileChannelName(fileName, patternIndex); } try { _communicationChannel = new FileChannel(accessType, fileName, true); return 0; } catch (std::exception &e) { Log::getInstance() << "ERROR: failed to create FileChannel for file named " << fileName.c_str() << " -> Possible issue with permissions." << Log::flush; return -1; } return 0; } std::string Device::getFileChannelName(std::string fileName, int patternIndex) { std::ostringstream name; name << fileName << "-P" << _patternIndex; Log::getInstance() << "INFO: fileChannel name " << name.str().c_str() << Log::flush; return name.str(); } /** * @author L.Piccoli */ int Device::setFeedbackPv(bool state) { if (_fbckCommunicationChannel != NULL) { if (state) { Log::getInstance() << "INFO: Turning FBCK PV ON " << _fbckCommunicationChannel->getName().c_str() << " P" << _patternIndex << Log::flush; return _fbckCommunicationChannel->write(1.0); } else { Log::getInstance() << "INFO: Turning FBCK PV OFF for " << _fbckCommunicationChannel->getName().c_str() << " P" << _patternIndex << Log::flush; return _fbckCommunicationChannel->write(0.0); } } /* else { std::cout << "INFO: " << _name << " -> turning FBCK (NULL) "; if (state) { std::cout << "ON" << std::endl; } else { std::cout << "OFF" << std::endl; } } */ return 0; } /** * Return the last value set to the FBCK PV. * * @return true if FBCK PV is ON, false if it is OFF, or in case the * _fbckCommunicationChannel is not valid. * @author L.Piccoli */ bool Device::getFeedbackPv() { if (_fbckCommunicationChannel != NULL) { if (_fbckCommunicationChannel->getLastValue() == 0.0) { return false; } else { return true; } } return false; } /** * Returns whether the device name starts with the string 'FILE:', * which indicates that this device is connectod to a file (for reading * or writing). * * @return true if device name starts with 'FILE:', false otherwise. * @author L.Piccoli */ bool Device::isFile() { if (_devNamePv.getValue().compare(0, 5, "FILE:") == 0) { return true; } return false; } /** * Returs whether the device name starts with the string 'NULL', * indicating that the NullChannel is used as communication channel. * * @return true if device name starts with 'NULL', false otherwise. * @author L.Piccoli */ bool Device::isNull() { if (_devNamePv.getValue().compare(0, 4, "NULL") == 0) { return true; } return false; } /** * Return whether the device is configured to use FCOM. * The device may not be using FCOM if its name starts with 'FILE:' or 'NULL' * * @return true if the CAMODE PV is false, false if CAMODE is true * @author L.Piccoli */ bool Device::isFcom() { return !_caModePv.getValue(); } /** * Returns the name of the device set through the DEVNAME PV * * @return the device name set in the DEVNAME PV * @author L.Piccoli */ std::string Device::getDeviceName() { return _devNamePv.getValue(); } void Device::setUsedBy(bool used) { _usedByLoopPv = used; _usedByLoopPv.scanIoRequest(); } bool Device::getUsedBy() { return _usedByLoopPv.getValue(); }