Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 enum Flags {
00074
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;
00156 StreamBuffer onInit;
00157 StreamBuffer onWriteTimeout;
00158 StreamBuffer onReplyTimeout;
00159 StreamBuffer onReadTimeout;
00160 StreamBuffer onMismatch;
00161 const char* commandIndex;
00162 const char* activeCommand;
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&);
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
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
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
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