|
libFAUDES
Sections
Index
|
iop_xdevice.hGo to the documentation of this file.00001 /** @file iop_xdevice.h Virtual device for interface definition */ 00002 00003 /* 00004 FAU Discrete Event Systems Library (libfaudes) 00005 00006 Copyright (C) 2008, Thomas Moor 00007 Exclusive copyright is granted to Klaus Schmidt 00008 00009 */ 00010 00011 00012 00013 #ifndef FAUDES_XDEVICE_H 00014 #define FAUDES_XDEVICE_H 00015 00016 #include "corefaudes.h" 00017 #include "tp_include.h" 00018 #include "iop_vdevice.h" 00019 00020 00021 00022 00023 00024 namespace faudes { 00025 00026 00027 00028 00029 /** 00030 * Container of devices. 00031 * 00032 * The xDevices is a container of vDevices. Sensor readings are 00033 * combined in a union fashion over all participating devices, 00034 * actuator writings are dispatched to the relevant device. Formally, 00035 * the xDevice class is derived from the vDevice class to provide the 00036 * same interaface to a simulator. Ie, the application does not 00037 * need to know whether is acts on a single vDevice or an xDevice. This 00038 * is also true for configuration from file, but of cause not for programatical 00039 * configuration. An xDevice may not be configureb to have individual 00040 * actuators or sensors, but to hold particular vDevices. 00041 * 00042 * Technical detail: the xDevice uses the vDevice interface to register a 00043 * common event fifo buffer and a pthread condition. Thus, the xDevice only works 00044 * with devices that support this configuration feature. 00045 * 00046 * @ingroup IODevicePlugin 00047 */ 00048 00049 class xDevice : public vDevice { 00050 public: 00051 00052 // Note write-permision 00053 00054 /** Iterator for const access to individual devices*/ 00055 typedef std::vector<vDevice*>::const_iterator Iterator; 00056 00057 00058 /** 00059 * Default constructor 00060 */ 00061 xDevice(void); 00062 00063 /** 00064 * Construct on heap from token reader. 00065 * 00066 * This constructor examines the token strean, determines the coressponding 00067 * class and constructs the device on the heap. Todo: the implementation 00068 * of this function is a hack, there must be proper 00069 * solution to this issue. 00070 * 00071 * @param rTr 00072 * TokenReader to read from 00073 * @return 00074 * vDevice pointer 00075 * 00076 * @exception Exception 00077 * - token mismatch (id 552) 00078 * - IO errors (id 1) 00079 */ 00080 static xDevice* FromTokenReader(TokenReader& rTr); 00081 00082 /** 00083 * Construct on heap from file. 00084 * 00085 * This constructor examines the file, determines the coressponding 00086 * class and constructs the device on the heap. 00087 * 00088 * @param rFileName 00089 * Filename 00090 * @return 00091 * vDevice pointer 00092 * 00093 * @exception Exception 00094 * - token mismatch (id 552) 00095 * - IO errors (id 1) 00096 */ 00097 static xDevice* FromFile(const std::string& rFileName); 00098 00099 /** 00100 * Explicit destructor. 00101 */ 00102 virtual ~xDevice(void); 00103 00104 00105 /** 00106 * Set Iterator to first device 00107 * 00108 * @return 00109 * Iterator to first device 00110 */ 00111 Iterator Begin(void) const { return mDevices.begin() ; }; 00112 00113 /** 00114 * Set Iterator to last device 00115 * 00116 * @return 00117 * Iterator to last device 00118 */ 00119 Iterator End(void) const { return mDevices.end() ; }; 00120 00121 /** 00122 * 00123 * Get number of devices 00124 */ 00125 Idx Size(void) const { return mDevices.size(); }; 00126 00127 00128 /** 00129 * Insert a new device. 00130 * An xDevice is configured by inserting vDevices. An xDevice cannot be 00131 * configured by individual actuator/sensro events. 00132 * 00133 * Note: by inserting a device into an xdevice its ownership 00134 * will be assumed by the xdevice and therewith the right to destoy 00135 * it. 00136 * 00137 * @param device 00138 * Pointer to vDevice 00139 */ 00140 void Insert(vDevice* device); 00141 00142 /** 00143 * Insert a new device by Filename 00144 * 00145 * An xDevice is configured by inserting vDevices. An xDevice cannot be 00146 * configured by individual actuator/sensro events. 00147 * 00148 * Note: by inserting a device into an xdevice its ownership 00149 * will be assumed by the xdevice and therewith the right to destoy 00150 * 00151 * For information on how to write such a file see the "iodevice" - tutorial 00152 * 00153 * @param rFileName 00154 * Configuration-file to build up device 00155 */ 00156 00157 void Insert(const std::string& rFileName); 00158 00159 00160 00161 /** 00162 * Dummy. An xDevice does not provide event based configuration. Use Insert instead. This function 00163 * will throw an execption 00164 */ 00165 void Configure(Idx event, const AttributeDeviceEvent& attr); 00166 00167 /** 00168 * Dummy. An xDevice does not provide event based configuration. Use Insert instead. This function 00169 * will throw an execption 00170 */ 00171 void Configure(const EventSet& rEvents); 00172 00173 /** 00174 * 00175 * Build up internal data structures. I.e. (event-idx,int) - map 00176 */ 00177 void Compile(void) ; 00178 00179 00180 /** 00181 * Clear all configuarations and destroy existing devices 00182 * 00183 */ 00184 void Clear(void); 00185 00186 00187 /** reset all dynamic state, iw call reset on each device */ 00188 void Reset(void); 00189 00190 /** Test for reset request */ 00191 bool ResetRequest(void); 00192 00193 /** 00194 * Activate the devices. xxx This function enables actuator execution and sensor reading. 00195 * 00196 * @exception Exception 00197 * - Not yet configured (id 551) 00198 */ 00199 void Start(void); 00200 00201 /** 00202 * Deactivate the device. This function disables actuator execution and sensor reading. 00203 * 00204 */ 00205 void Stop(void); 00206 00207 /** 00208 * Get status. This function returns the current status of the device. 00209 * In derived classes that use background threads for sensor reading etc, 00210 * a device may change its status without notice. Dont forget to reimplement 00211 * this method with an appropriate mutex. 00212 * 00213 */ 00214 DeviceState Status(void); 00215 00216 /** 00217 * Run actuator command. 00218 * 00219 * @exception Exception 00220 * - unknown actuator event (id 65) 00221 * 00222 */ 00223 void WriteActuator(Idx actuator); 00224 00225 /** 00226 * Report global fauDES-time 00227 * Note: per convention we take the time of the first 00228 * device inserted in xDevice as global time 00229 * 00230 * @return 00231 * fauDES-time 00232 */ 00233 tpTime::Type CurrentTime(void); 00234 00235 00236 /** 00237 * Report global fauDES-time 00238 * Note: per convention we take the time of the first 00239 * device inserted in xDevice as global time 00240 * 00241 * @return 00242 * fauDES-time 00243 */ 00244 long int CurrentTimeMs(void); 00245 00246 00247 /** 00248 * Set physical time in ftu. 00249 * 00250 * @param now 00251 * now in faudes time units (ftu). 00252 */ 00253 virtual void CurrentTime(tpTime::Type now); 00254 00255 00256 /** 00257 * Set physical time in ms. 00258 * 00259 * @param nowms 00260 * now in msec 00261 */ 00262 virtual void CurrentTimeMs(long int nowms); 00263 00264 00265 00266 protected: 00267 00268 //Notice: not const -> write-perimision 00269 typedef std::vector<vDevice*>::iterator iterator; 00270 00271 00272 /** 00273 * Actual method to read device configuration from tokenreader. 00274 * 00275 * DoRead basically calls the DoWrite-function of all devices which are part of xDevice 00276 * 00277 * @param rTr 00278 * TokenReader to read from 00279 * @param rLabel 00280 * Section to read 00281 * @param pContext 00282 * Read context to provide contextual information 00283 * 00284 * @exception Exception 00285 * - IO error (id 1) 00286 */ 00287 virtual void DoRead(TokenReader& rTr, const std::string& rLabel = "", const Type* pContext=0); 00288 00289 /** 00290 * Actual method to write the device configuration to a TokenWriter. 00291 * 00292 * DoWrite basically calls the DoWrite-function of all devices which are part of xDevice 00293 * 00294 * @param rTw 00295 * Reference to TokenWriter 00296 * @param rLabel 00297 * Label of section to write 00298 * @param pContext 00299 * Read context to provide contextual information 00300 * @exception Exception 00301 * - IO errors (id 2) 00302 */ 00303 virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const; 00304 00305 /** Return first Device */ 00306 iterator Begin(void) { return mDevices.begin() ; }; 00307 00308 /** Return last Device */ 00309 iterator End(void) { return mDevices.end() ; }; 00310 00311 /** Vector of member-devices */ 00312 std::vector<vDevice*> mDevices; 00313 00314 /** Vector of member-device-names*/ 00315 std::vector<std::string> mDeviceNames; 00316 00317 /** Compiled data: Sensor map to map sensor idx to device no */ 00318 std::map<Idx,int> mSensorToDevice; 00319 00320 /** Compiled data: Actuator Map to map sensor idx to device no */ 00321 std::map<Idx,int> mActuatorToDevice; 00322 00323 /** Current device state: remember last stop/down command */ 00324 bool lastCommandWasStart; 00325 00326 00327 }; 00328 00329 00330 00331 } 00332 00333 00334 #endif 00335 |
libFAUDES 2.16b --- 2010-9-8 --- c++ source docu by doxygen 1.6.3