TimingEventSourceSCU.cpp

Go to the documentation of this file.
00001 //Copyright GSI 2012
00002 #include <sstream>
00003 #include <fstream>
00004 #include <malloc.h>
00005 #include <sys/time.h>
00006 
00007 #include <fesa-core-gsi/RealTime/TimingEventSourceSCU.h>
00008 #include <fesa-core-gsi/Synchronization/TimingContextSCU.h>
00009 #include <fesa-core-gsi/Synchronization/GSICycleDescriptor.h>
00010 #include <fesa-core-gsi/Exception/GSIException.h>
00011 #include <fesa-core/Core/FesaDefs.h>
00012 #include <fesa-core/Utilities/ParserElements/EventElement.h>
00013 #include <fesa-core/Utilities/ParserElements/ParserElementDefs.h>
00014 
00015 namespace fesaGSI
00016 {
00017 
00018     TimingEventSourceSCU::TimingEventSourceSCU() :
00019         fesa::AbstractEventSource(fesa::TIMING_EVENT_SOURCE_NAME, fesa::TimingSource)
00020     {
00021         int status;                                     /* return status of system calls */
00022 
00023                 /* open irq device */
00024                 status = fileDescriptor_ = open(SCU_TIMING_DEVICE_NAME.c_str(), O_RDWR);
00025                 if (status == -1)
00026                 {
00027                         throw GSIException(__FILE__, __LINE__, FESAGSIErrorWhileConnectingToSCUTiming.c_str(),SCU_TIMING_DEVICE_NAME.c_str());
00028                 }
00029     }
00030 
00031     TimingEventSourceSCU::~TimingEventSourceSCU()
00032     {
00033         close(fileDescriptor_);
00034     }
00035 
00036     fesa::RTEvent* TimingEventSourceSCU::wait()
00037     {
00038         // This loop is necessary in case of error on TimLib call. For errors other
00039         // than TimLibTIMEOUT we have to insert our own delay to avoid to loop
00040         // in TimLibCall very fast consuming all CPU time.
00041         while(true)
00042         {
00043             fesa::RTEvent* theEvent = 0;
00044             fesa::MultiplexingContext* context = 0;
00045             try
00046             {
00047 
00048                 unsigned int cnt_value_irq;     /* buffer to hold data */
00049                 int status;                                     /* return status of system calls */
00050 
00051                 /* blocking read */
00052                 status = read(fileDescriptor_, &cnt_value_irq, sizeof(int));
00053                 if (status == -1)
00054                 {
00055                         throw GSIException(__FILE__, __LINE__, FESAGSIErrorWhileReceivingInterrupt.c_str());
00056                 }
00057 
00058                                 long timestamp = cnt_value_irq;
00059                 std::string cycleName = "AnyCycle";
00060                 context = new TimingContextSCU(timestamp,cycleName);
00061                 theEvent = new fesa::RTEvent("SCUTestClass::START_CYCLE#CTIM#32",context ,this);
00062 
00063                 // Everything is fine goes out the infinite loop and fires the event
00064                 return theEvent;
00065             }
00066             catch (fesa::FesaException& ex)//this also covers FESAGSIExceptions
00067             {
00068                 if (context != 0)
00069                     delete context;
00070                 if (theEvent != 0)
00071                     delete theEvent;
00072 
00073                 //std::stringstream buf;
00074                 //buf << " Event was not fired, because the following exception occured: " << ex.getMessage().c_str();
00075                 //pLog->logError(fesa::FesaLogger::alert, fesa::FesaLogger::fwkSender) << buf.str() << std::endl;
00076             }
00077             catch (...)
00078             {
00079                 if (context != 0)
00080                     delete context;
00081                 if (theEvent != 0)
00082                     delete theEvent;
00083 
00084                 //std::stringstream buf;
00085                 //buf << " GSITimingEventSource.cpp: Event was not fired, because an unknown exception occured. ";
00086                 //pLog->logError(fesa::FesaLogger::alert, fesa::FesaLogger::fwkSender) << buf.str() << std::endl;
00087             }
00088         }
00089     }
00090 
00091     void TimingEventSourceSCU::connect(boost::shared_ptr<fesa::EventElement>& eventElement)
00092     {
00093         std::string concreteEventName = eventElement->getSourceTypeSpecificData(fesa::TIMING_TAG_CONCRETE_NAME);
00094 
00095         // objectName are formatted like this:
00096         // "ClassName::BX.WCY200-CT#CTIM#202" or "ClassName::EX.SCY-MTG#LTIM#16 or ClassName::EX.SCY-MTG#LTIM-HARD#16"
00097 
00098         //size_t posSep0= concreteEventName.find(':');
00099         size_t posSep1 = concreteEventName.find('#');
00100         size_t posSep2 = concreteEventName.rfind('#', concreteEventName.size());
00101         if (posSep1 == std::string::npos || posSep2 == std::string::npos)
00102         {
00103             throw GSIException(__FILE__, __LINE__, FESAGSIErrorWrongTimingName.c_str(), concreteEventName.c_str());
00104         }
00105 
00106         //     throw GSIException(__FILE__, __LINE__, FESAGSIErrorWhileConnectingTimingEvent.c_str(),
00107         //                   concreteEventName.c_str());
00108 
00109         if (false)
00110         {
00111             throw GSIException(__FILE__, __LINE__, FESAGSIErrorWhileConnectingTimingEvent.c_str(),
00112                             concreteEventName.c_str());
00113         }
00114 
00115         return;
00116     }
00117 
00118 }//end namespace

Generated on 25 Jan 2013 for fesa-core-gsi by  doxygen 1.6.1