• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

epics/apps/crucible/stream/src/StreamCore.h (r4864/r2310)

Go to the documentation of this file.
00001 /***************************************************************
00002 * StreamDevice Support                                         *
00003 *                                                              *
00004 * (C) 1999 Dirk Zimoch (zimoch@delta.uni-dortmund.de)          *
00005 * (C) 2005 Dirk Zimoch (dirk.zimoch@psi.ch)                    *
00006 *                                                              *
00007 * This is the kernel of StreamDevice.                          *
00008 * Please refer to the HTML files in ../doc/ for a detailed     *
00009 * documentation.                                               *
00010 *                                                              *
00011 * If you do any changes in this file, you are not allowed to   *
00012 * redistribute it any more. If there is a bug or a missing     *
00013 * feature, send me an email and/or your patch. If I accept     *
00014 * your changes, they will go to the next release.              *
00015 *                                                              *
00016 * DISCLAIMER: If this software breaks something or harms       *
00017 * someone, it's your problem.                                  *
00018 *                                                              *
00019 ***************************************************************/
00020 
00021 #ifndef StreamCore_h
00022 #define StreamCore_h
00023 
00024 #include "StreamProtocol.h"
00025 #include "StreamFormatConverter.h"
00026 #include "StreamBusInterface.h"
00027 
00028 /**************************************
00029  virtual methods:
00030 
00031 bool getFieldAddress(const char* fieldname, StreamBuffer& address)
00032   If a format sting contains a field name (like "%(EGU)s") a value should
00033   be taken from / put to that field instead of the default location.
00034   This function must convert the fieldname into some address structure and
00035   put the structure into the address buffer (e.g. address.set(addressStruct)).
00036   The address structure must be suitable for bytewise copying. I.e. memcpy()
00037   to so some other memory location must not invalidate its contents. No
00038   constructor, destructor or copy operator will be called.
00039   formatValue() and matchValue() get a pointer to the structure.
00040   getFieldAddress() must return true on success and false on failure.
00041 
00042 bool formatValue(const StreamFormat& format, const void* fieldaddress)
00043   This function should first get a value from fieldaddress (if not NULL) or
00044   the default location (which may depend on format.type).
00045   The printValue(format,XXX) function suitable for format.type should be
00046   called to print value. If value is an array, printValue() should be called
00047   for each element. The separator string will be added automatically.
00048   formatValue() must return true on success and false on failure.
00049 
00050 bool matchValue(const StreamFormat& format, const void* fieldaddress)
00051   This function should first call the scanValue(format,XXX) function
00052   suitable for format.type. If scanValue() returns true, the scanned value
00053   should be put into fieldaddress (if not NULL) or the default location
00054   (which may depend on format.type).
00055   If value is an array, scanValue() should be called for each element. It
00056   returns false if there is no more element available. The separator string
00057   is matched automatically.
00058   matchValue() must return true on success and false on failure.
00059 
00060 
00061 void protocolStartHook()
00062 void protocolFinishHook(ProtocolResult)
00063 void startTimer(unsigned short timeout)
00064 void lockRequest(unsigned short timeout)
00065 void unlock()
00066 void writeRequest(unsigned short timeout)
00067 void readRequest(unsigned short replytimeout, unsigned short timeout)
00068 void readAsyn(unsigned short timeout)
00069 void acceptEvent(unsigned short mask, unsigned short timeout)
00070 
00071 ***************************************/
00072 
00073 enum Flags {
00074     // 0x00FFFFFF reserved for StreamCore
00075     None = 0x0000,
00076     IgnoreExtraInput = 0x0001,
00077     InitRun = 0x0002,
00078     AsyncMode = 0x0004,
00079     GotValue = 0x0008,
00080     BusOwner = 0x0010,
00081     Separator = 0x0020,
00082     ScanTried = 0x0040,
00083     AcceptInput = 0x0100,
00084     AcceptEvent = 0x0200,
00085     LockPending = 0x0400,
00086     WritePending = 0x0800,
00087     WaitPending = 0x1000,
00088     BusPending = LockPending|WritePending|WaitPending,
00089     ClearOnStart = InitRun|AsyncMode|GotValue|BusOwner|Separator|ScanTried|
00090                     AcceptInput|AcceptEvent|BusPending
00091 };
00092 
00093 struct StreamFormat;
00094 
00095 class StreamCore :
00096     StreamProtocolParser::Client,
00097     StreamBusInterface::Client
00098 {
00099 protected:
00100     enum ProtocolResult {
00101         Success, LockTimeout, WriteTimeout, ReplyTimeout, ReadTimeout,
00102         ScanError, FormatError, Abort, Fault
00103     };
00104 
00105     enum StartMode {
00106         StartNormal, StartInit, StartAsync
00107     };
00108 
00109     class MutexLock
00110     {
00111         StreamCore* stream;
00112 
00113     public:
00114         MutexLock(StreamCore* _stream) : stream(_stream)
00115             { _stream->lockMutex(); }
00116         ~MutexLock()
00117             { stream->releaseMutex(); }
00118     };
00119 
00120     friend class MutexLock;
00121 
00122     StreamCore* next;
00123     static StreamCore* first;
00124 
00125     char* streamname;
00126     unsigned long flags;
00127 
00128     bool attachBus(const char* busname, int addr, const char* param);
00129     void releaseBus();
00130 
00131     bool startProtocol(StartMode);
00132     void finishProtocol(ProtocolResult);
00133     void timerCallback();
00134 
00135     bool printValue(const StreamFormat& format, long value);
00136     bool printValue(const StreamFormat& format, double value);
00137     bool printValue(const StreamFormat& format, char* value);
00138     long scanValue(const StreamFormat& format, long& value);
00139     long scanValue(const StreamFormat& format, double& value);
00140     long scanValue(const StreamFormat& format, char* value, long maxlen);
00141     long scanValue(const StreamFormat& format);
00142 
00143     StreamBuffer protocolname;
00144     unsigned long lockTimeout;
00145     unsigned long writeTimeout;
00146     unsigned long replyTimeout;
00147     unsigned long readTimeout;
00148     unsigned long pollPeriod;
00149     unsigned long maxInput;
00150     bool inTerminatorDefined;
00151     bool outTerminatorDefined;
00152     StreamBuffer inTerminator;
00153     StreamBuffer outTerminator;
00154     StreamBuffer separator;
00155     StreamBuffer commands;        // the normal protocol
00156     StreamBuffer onInit;          // init protocol (optional)
00157     StreamBuffer onWriteTimeout;  // error handler (optional)
00158     StreamBuffer onReplyTimeout;  // error handler (optional)
00159     StreamBuffer onReadTimeout;   // error handler (optional)
00160     StreamBuffer onMismatch;      // error handler (optional)
00161     const char* commandIndex;     // current position
00162     const char* activeCommand;    // start of current command
00163     StreamBuffer outputLine;
00164     StreamBuffer inputBuffer;
00165     StreamBuffer inputLine;
00166     long consumedInput;
00167     ProtocolResult runningHandler;
00168     StreamBuffer fieldAddress;
00169 
00170     StreamIoStatus lastInputStatus;
00171     bool unparsedInput;
00172 
00173     StreamCore(const StreamCore&); // undefined
00174     bool compile(StreamProtocolParser::Protocol*);
00175     bool evalCommand();
00176     bool evalOut();
00177     bool evalIn();
00178     bool evalEvent();
00179     bool evalWait();
00180     bool evalExec();
00181     bool evalConnect();
00182     bool evalDisconnect();
00183     bool formatOutput();
00184     bool matchInput();
00185     bool matchSeparator();
00186     void printSeparator();
00187 
00188 // StreamProtocolParser::Client methods
00189     bool compileCommand(StreamProtocolParser::Protocol*,
00190         StreamBuffer&, const char* command, const char*& args);
00191     bool getFieldAddress(const char* fieldname,
00192         StreamBuffer& address) = 0;
00193 
00194 // StreamBusInterface::Client methods
00195     void lockCallback(StreamIoStatus status);
00196     void writeCallback(StreamIoStatus status);
00197     long readCallback(StreamIoStatus status,
00198         const void* input, long size);
00199     void eventCallback(StreamIoStatus status);
00200     void execCallback(StreamIoStatus status);
00201     void connectCallback(StreamIoStatus status);
00202     void disconnectCallback(StreamIoStatus status);
00203     const char* getInTerminator(size_t& length);
00204     const char* getOutTerminator(size_t& length);
00205 
00206 // virtual methods
00207     virtual void protocolStartHook() {}
00208     virtual void protocolFinishHook(ProtocolResult) {}
00209     virtual void startTimer(unsigned long timeout) = 0;
00210     virtual bool formatValue(const StreamFormat&, const void* fieldaddress) = 0;
00211     virtual bool matchValue (const StreamFormat&, const void* fieldaddress) = 0;
00212     virtual void lockMutex() = 0;
00213     virtual void releaseMutex() = 0;
00214     virtual bool execute();
00215 
00216 public:
00217     StreamCore();
00218     virtual ~StreamCore();
00219     bool parse(const char* filename, const char* protocolname);
00220     void printProtocol();
00221     const char* name() { return streamname; }
00222 };
00223 
00224 #endif

Generated on Tue Dec 10 2013 04:52:22 for ROCsoft by  doxygen 1.7.1