iop_serial.h

Go to the documentation of this file.
00001 /** @file iop_serial.h Process image via serial line */
00002 
00003 /*
00004    FAU Discrete Event Systems Library (libfaudes)
00005 
00006    Copyright (C) 2011, Thomas Moor.
00007 
00008 */
00009 
00010 
00011 
00012 #ifndef FAUDES_IOP_SERIAL_H
00013 #define FAUDES_IOP_SERIAL_H
00014 
00015 
00016 // Include core-libary and iodevice
00017 #include "corefaudes.h"
00018 #include "iop_vdevice.h"
00019 #include "iop_sdevice.h"
00020 
00021 
00022 // If configured to have sspi device 
00023 #ifdef FAUDES_IODEVICE_SERIAL
00024 
00025 // Include serial specifics (posix systems)
00026 #include <fcntl.h>  
00027 #include <errno.h>  
00028 #include <termios.h> 
00029 
00030 
00031 
00032 namespace faudes {
00033 
00034 
00035 
00036 /**
00037  * Processimage synchronisation via serial line interface
00038  *
00039  * This device is derived from the signal based sDevice to
00040  * emulate component interconnection to behave like parallel 
00041  * digital wiring, however, physically using the 
00042  * serial interface with a simple commonication protocol.
00043  *
00044  * A typical application scenario is to simulate a physical
00045  * plant on one PC and to implement decentralized controllers 
00046  * on further PCs. The distributed controllers 
00047  * may use the rtxx protocol or the simplenet protocol to communicate 
00048  * via ethernet. The interface with the physical plant 
00049  * is modelled as a so called process image that indicates 
00050  * line levels. The spiDevice synchronizes the process images among the 
00051  * components. In particular, plant simulation incl. the interaction
00052  * with the controllers will not interfere with the communication among
00053  * the controllers. This is a requirement for the evaluation of
00054  * real time communication protocols such as rtxx.
00055  *
00056  * The spiDevice distinguishes between one master component 
00057  * and an arbitrary number of slave components. The master must be
00058  * connected to each slave via a dedicated serial port. Every input 
00059  * signal from the master must match an output signal of no more than one slave.
00060  * Any output signal of the master may be considered as an input of
00061  * any slave.
00062  *
00063  * To configure a spiDevice, you must
00064  * - specify the role using either the tag 
00065  *   <tt>&lt;Role value="master"/&gt;</tt> or <tt>&lt;Role value="slave"/&gt;</tt>
00066  * - specify the serial interface, using tags of the form 
00067  *   &lt;DeviceFile value="/dev/ttyS0"/&gt, &lt;DeviceFile value="/dev/ttyS1"/&gt etc
00068  * - setup events to correspond to edges of line levels, see alo faudes::sDevice
00069  *
00070  *
00071  *
00072  * Note: this device compiles under Linux only. 
00073  * It must be explicitely enabled Makefile.plugin.
00074  * 
00075  * @ingroup  IODevicePlugin
00076  */
00077 
00078 class spiDevice : public sDevice {
00079 
00080 FAUDES_TYPE_DECLARATION(SpiDevice,spiDevice,sDevice)
00081 
00082  public:
00083 
00084   /**
00085    * Default constructor
00086    */
00087   spiDevice(void);
00088 
00089   /**
00090    * Copy constructor (not implemented!)
00091    */
00092   spiDevice(const spiDevice&) : sDevice() {};
00093 
00094   /**
00095    * Explicit destructor.
00096    */
00097   virtual ~spiDevice(void);
00098 
00099 
00100   /**
00101    * Clear all configuration (implies Stop)
00102    */
00103   virtual void Clear(void);
00104 
00105 
00106   /**
00107    * 
00108    * Compile to internal data-structures.
00109    * 
00110    * Exception in misconfiguration/inconsistencies
00111    *
00112    */
00113   virtual void Compile(void);
00114 
00115   /**
00116    * Activate the device. 
00117    * This function opens/initializes serial lines and 
00118    * starts the -background thread for edage detection.
00119    *
00120    * @exception Exception
00121    *   - not yet configured (id not configured)
00122    *   - failed to open serial lines (id not configured)
00123    */
00124   virtual void Start(void);
00125 
00126 
00127   /**
00128    * Deactivate the device. This function shuts down the seriale lines,
00129    * stops the background thread and sets all output signals to 0.
00130    */
00131   virtual void Stop(void);
00132 
00133 
00134 protected:
00135    
00136 
00137   /**
00138    * IO Hook, inputs
00139    *
00140    * @return 
00141    *  True on success.
00142    *
00143    */
00144   virtual bool DoReadSignalsPre(void);
00145 
00146   /**
00147    * IO Hook, inputs
00148    */
00149   virtual void DoReadSignalsPost(void);
00150 
00151   /**
00152    * Get input signal.
00153    *
00154    * Extract bit value from image.
00155    *
00156    * @param bitaddr
00157    *   Abstract bit address
00158    * @return
00159    *  True for logic level high;
00160    */
00161   virtual bool DoReadSignal(int bitaddr);
00162   
00163   /**
00164    * IO Hook, outputs
00165    *
00166    * @return 
00167    *  True on success
00168    *
00169    */
00170   virtual bool DoWriteSignalsPre(void);
00171 
00172   /**
00173    * IO Hook, outputs
00174    *
00175    */
00176   virtual void DoWriteSignalsPost(void);
00177 
00178   /**
00179    * Set output signal.
00180    *
00181    * Set value of bit in process image.
00182    *
00183    * @param bitaddr
00184    *   Abstract bit address
00185    * @param value
00186    *   True for logic level high;
00187    *
00188    */
00189   virtual void DoWriteSignal(int bitaddr, bool value);
00190 
00191 
00192   /**
00193    * Loop hook.
00194    *
00195    * This function is called once during each cycle of the
00196    * backgroud thread. It implements the actual communication via the serial
00197    * interfaces. To each slave, the master will send its process image and
00198    * await for the slave to reply by the image. In it's reply, the slave will
00199    * set/clr its output lines. To keep things simple, the process image is
00200    * passed on completely, even those lines that are not relevant for the
00201    * respective client. 
00202    *
00203    */
00204   virtual void DoLoopCallback(void);
00205 
00206   /** 
00207    * Read non-event-related configuration data from tokenreader
00208    *
00209    * @param rTr
00210    *   TokenReader to read from
00211    * @param rLabel
00212    *   Section to read
00213    * @param pContext
00214    *   Read context to provide contextual information
00215    *
00216    */
00217   void DoReadPreface(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
00218 
00219   /**  
00220    * Write non-event-related configuration data to tokenreader
00221    *
00222    * @param rTw
00223    *   TokenWriter to write
00224    * @param rLabel
00225    *   Section to write
00226    * @param pContext
00227    *   Context to provide contextual information
00228    * 
00229    */
00230   void DoWritePreface(TokenWriter& rTw, const std::string& rLabel,  const Type* pContext) const ;
00231 
00232 
00233   /** Role: master/slave */
00234   bool mMaster;
00235 
00236   /** Interface(s) */
00237   std::vector<std::string> mDeviceFiles;
00238   std::vector<int>         mPorts;
00239 
00240   /** process image from serial line */
00241   char* mpImage;
00242   char* pInputImage;
00243   char* pOutputImage;
00244   char* mpOutputMask;
00245 
00246 
00247 
00248 }; //class spiDevice
00249 
00250 } //namespace 
00251 
00252 
00253 #endif // configured
00254 
00255 #endif // include

libFAUDES 2.23h --- 2014.04.03 --- c++ api documentaion by doxygen