iop_d3ripURT.h
Go to the documentation of this file.
1 /** @file iop_d3ripURT.h iodevice for d3ripURT protocol and friends */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2011 Ece Schmidt, Klaus Schmidt, Ulas Turan
7 
8 */
9 
10 
11 
12 
13 #ifndef FAUDES_IOP_D3RIP_URT_H
14 #define FAUDES_IOP_D3RIP_URT_H
15 
16 
17 
18 #include "corefaudes.h"
19 
20 //only compile with D3RIP-URT protocol operation
21 #ifdef FAUDES_IODEVICE_D3RIP_URT
22 
23 
24 #include "iop_vdevice.h"
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <mqueue.h>
29 #include <sys/stat.h>
30 #include <vector>
31 #include <map>
32 
33 namespace faudes {
34 
35 #define D3RIP_RT_MESSAGE_MAX_LENGTH 186
36 #define D3RIP_URT_MAX_PARAMETERS_COUNT 5
37 #define D3RIP_URT_MAX_EVENT_COUNT 5
38 
39 enum {
40 D3RIP_URT_EVENT_ID=0,
41 D3RIP_URT_CHANNEL_TO_TRANSMIT,
42 D3RIP_URT_PARAMETER_COUNT,
43 D3RIP_URT_HEADER_SIZE
44 };
45 
46 enum {
47 D3RIP_URT_DESTIONATION_NODE=0,
48 D3RIP_URT_DESTIONATION_CHANNEL,
49 D3RIP_URT_ELIGIBILITY_TIME,
50 D3RIP_URT_DEADLINE_TIME,
51 D3RIP_URT_PARAMETER_SIZE
52 };
53 
54 typedef struct {
55  int destinationNode;
56  int destinationChannel;
57  int eligibilityTime;
58  int deadlineTime;
59 }ParameterRecord;
60 
61 /**
62  * Configuration of D3RIP_URT Output Event parameters
63  *
64  * This class assists to extract the content of Ouput Event specification by using TokenReader.
65  * Note that D3RIP related parameters should be attached to each Ouput Event.
66  *
67  */
68 
69 class AttributeD3ripURTOutput : public AttributeVoid {
70 
71 FAUDES_TYPE_DECLARATION(Void,AttributeD3ripURTOutput,AttributeVoid)
72 
73 public:
74 
75  /** Default constructor (no triggers) */
76  AttributeD3ripURTOutput(void) : AttributeVoid() {};
77 
78  /** Copy - constructor */
79  AttributeD3ripURTOutput(const AttributeD3ripURTOutput& rOtherAttr) : AttributeVoid()
80  { DoAssign(rOtherAttr); };
81 
82  /** Test for default value (never) */
83  virtual bool IsDefault(void) const {return false;};
84 
85  /** Clear */
86  virtual void Clear(void) {
87  mChannelToTransmit=0;
88  };
89 
90  /** Channel number for transmitting the event */
91  int mChannelToTransmit;
92 
93  /** Consistent event IDs should be given at all controllers */
94  int mEventId;
95 
96  /** Bunch of D3RIP related parameters */
97  std::vector<ParameterRecord> mParameterRecords;
98 
99  protected:
100 
101  /**
102  * Copy method
103  *
104  * @param rSrcAttr
105  * Source to copy from
106  * @return
107  * Ref to this attribute
108  */
109  void DoAssign(const AttributeD3ripURTOutput& rSrcAttr);
110 
111  /**
112  * Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
113  *
114  * If the current token indicates an output mapping, the method reads that
115  * section. Else it does nothing. Exceptions may only be thrown
116  * on invalid data within the section. The label argument is ignored, we the hardcoded
117  * output for output device attributes. The context argument is ignored.
118  *
119  * @param rTr
120  * TokenReader to read from
121  * @param rLabel
122  * Section to read
123  * @param pContext
124  * Read context to provide contextual information
125  *
126  * @exception Exception
127  * - IO error (id 1)
128  */
129  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
130 
131  /**
132  * Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
133  *
134  * Writes the output mapping data. The label argument is ignored, we use
135  * the hardcoded section "Output". The context argument is ignored.
136  *
137  * @param rTw
138  * TokenWriter to write to
139  * @param rLabel
140  * Section to write
141  * @param pContext
142  * Read context to provide contextual information
143  *
144  * @exception Exception
145  * - IO error (id 2)
146  */
147  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
148 
149 }; // end class AttributeD3ripURTOutput
150 
151 
152 
153 
154 
155 
156 /**
157  * Configuration of D3RIP_URT Input Event parameters
158  *
159  * This class assists to extract the content of Input Event specification by using TokenReader.
160  * It should be noted that only mEventId is required for Input Events. It is not necessary to specify
161  * D3RIP related parameters for Input Events
162  *
163  */
164 
165 class AttributeD3ripURTInput : public AttributeVoid {
166 
167 FAUDES_TYPE_DECLARATION(Void,AttributeD3ripURTInput,AttributeVoid)
168 
169 public:
170 
171  /** Default constructor (no triggers) */
172  AttributeD3ripURTInput(void) : AttributeVoid() {};
173 
174  /** Copy constructor */
175  AttributeD3ripURTInput (const AttributeD3ripURTInput& rOtherAttr) : AttributeVoid()
176  { DoAssign(rOtherAttr); };
177 
178  /** Test for default value (never) */
179  virtual bool IsDefault(void) const {return false;};
180 
181  /** Clear */
182  virtual void Clear(void) { /* set myself to default values */;};
183 
184  /** Consistent event IDs should be given at all controllers */
185  int mEventId;
186 
187  protected:
188 
189  /**
190  * Copy method
191  *
192  * @param rSrcAttr
193  * Source to copy from
194  * @return
195  * Ref to this attribute
196  */
197  void DoAssign(const AttributeD3ripURTInput& rSrcAttr);
198 
199  /**
200  * Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
201  *
202  * If the current token indicates a input mapping, the method reads that
203  * section. Else it does nothing. Exceptions may only be thrown
204  * on invalid data within the section. The label argument is ignored, we use the
205  * hardcoded section "Input" for input attributes. The context argument is ignored.
206  *
207  * @param rTr
208  * TokenReader to read from
209  * @param rLabel
210  * Section to read
211  * @param pContext
212  * Read context to provide contextual information
213  *
214  * @exception Exception
215  * - IO error (id 1)
216  */
217  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
218 
219 
220  /**
221  * Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
222  *
223  * Writes the input mapping data.The label argument is ignored, we use the
224  * hardcoded section "Input". The context argument is ignored.
225  *
226  * @param rTw
227  * TokenWriter to write to
228  * @param rLabel
229  * Section to write
230  * @param pContext
231  * Read context to provide contextual information
232  *
233  * @exception Exception
234  * - IO error (id 2)
235  */
236 
237  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
238 
239 }; // end class AttributeD3ripURTInput
240 
241 
242 
243 
244 /**
245  * Configuration of an d3ripURT event as input or output
246  *
247  * This class is derived from the AttributeDeviceEvent to specialise
248  * for d3ripURT based input and output mapping.
249  *
250  */
251 
252 class AttributeD3ripURTEvent : public AttributeDeviceEvent {
253 
254 FAUDES_TYPE_DECLARATION(Void,AttributeD3ripURTEvent,AttributeDeviceEvent)
255 
256  public:
257 
258  /** Default constructor (no mapping at all) */
259  AttributeD3ripURTEvent(void);
260 
261  /** Copy constructor */
262  AttributeD3ripURTEvent(const AttributeD3ripURTEvent& rOtherAttr);
263 
264  /** Test for default value (never) */
265  virtual bool IsDefault(void) const {return false;};
266 
267  /** Clear */
268  virtual void Clear(void) { AttributeDeviceEvent::Clear(); };
269 
270  /** Get output mapping */
271  const AttributeD3ripURTOutput* Outputp(void) const {
272  return static_cast<AttributeD3ripURTOutput*>(mpOutputAttribute); };
273 
274  /** Get input mapping */
275  const AttributeD3ripURTInput* Inputp(void) const {
276  return static_cast<AttributeD3ripURTInput*>(mpInputAttribute); };
277 
278  protected:
279 
280  /** DoAssign */
281  void DoAssign(const AttributeD3ripURTEvent& rSrc)
283 
284  /** Prototype, input (construct on first use static) */
285  static const AttributeD3ripURTInput* InputPrototypep(void);
286 
287  /** Prototype, output (construct on first use static) */
288  static const AttributeD3ripURTOutput* OutputPrototypep(void);
289 
290 }; // class AttributeD3ripURTEvent
291 
292 
293 /**
294  * Provides the interface between simfaudes and D3RIP protocol running on the same host
295  *
296  * @section SecIodeviceD3RipUrtDev1 Networking
297  * The d3ripURTDevice assists in the exchange of the logical events belonged to the
298  * controllers over network utilizing D3RIP. It is similar to the SimpleNet device
299  * in some manner, however, d3ripURTDevice provides deterministic time bounds for
300  * the transmission of logical events through Ethernet. This device just provides
301  * required access to the underlying D3RIP implementation from the libFAUDES simulator.
302  * Note that D3RIP implementation and IEEE 1588 Synchronization deamon should be seperately
303  * provided for the desired operation.
304  *
305  * @section SecIodeviceD3RipUrtDev1 Protocol Details
306  * Though d3ripURTDevice does not implement the D3RIP, it should be aware of the corresponding
307  * protocol parameters assigned for each output logical event. Token IO is used in section
308  * "D3ripURTDevice" while extracting those parameters. Detailed information about protocol related
309  * parameters can be obtained by referring to the D3RIP specification. A simple controller may have:
310  *
311  * @code
312  * <D3ripURTDevice name="ControllerB_Net">
313  * <!-- Time scale in ms/ftiu -->
314  * <TimeScale value="1000"/>
315  *
316  * <!-- Event configuration -->
317  * <EventConfiguration>
318  *
319  * <Event name="?lambda" iotype="output">
320  * <EventId value="1"/>
321  * <ChannelToTransmit value="1"/>
322  * <ParameterRecord name="11">
323  * <DestinationNode value="1"/>
324  * <DestinationChannel value="1"/>
325  * <EligibilityTime value="2" />
326  * <DeadlineTime value="5"/>
327  * </ParameterRecord>
328  * </Event>
329  *
330  * <Event name="!lambda" iotype="input">
331  * <EventId value="2"/>
332  * </Event>
333  *
334  * <Event name="lambda" iotype="output">
335  * <EventId value="3"/>
336  * <ChannelToTransmit value="1"/>
337  * <ParameterRecord name="11">
338  * <DestinationNode value="2"/>
339  * <DestinationChannel value="1"/>
340  * <EligibilityTime value="2" />
341  * <DeadlineTime value="5"/>
342  * </ParameterRecord>
343  * </Event>
344  *
345  * </EventConfiguration>
346  *
347  * </D3ripURTDevice>
348  * @endcode
349  *
350  *
351  * @section SecIodeviceD3RipUrtDev1 Notes
352  * This device compiles flags under Linux only and should be linked to realtime library (-lrt).
353  *
354  * @ingroup IODevicePlugin
355  */
356 
357 class d3ripURTDevice : public vDevice {
358 
359  // provide access to background tasks
360  friend void* DoListenCLModule(void*);
361 
362 FAUDES_TYPE_DECLARATION(D3RipUrtDevice,d3ripURTDevice,vDevice)
363 
364 
365 
366 public:
367 
368  /**
369  * Array to store Event parameters statically in the form:
370  * ||| EventID | Channel To Transmit | Parameter Count || DESTIONATION_NODE | DESTIONATION_CHANNEL | ELIGIBILITY_TIME | DEADLINE_TIME | ... |||
371  */
372  unsigned char mEventParameters[D3RIP_URT_MAX_EVENT_COUNT][D3RIP_URT_HEADER_SIZE+D3RIP_URT_PARAMETER_SIZE*D3RIP_URT_MAX_PARAMETERS_COUNT];
373 
374  /** Status for receiving events from D3RIP */
375  int mContinueListening;
376 
377  /** Used for sending events to D3RIP */
378  mqd_t mMQueueToSend;
379 
380  /** Used for receiving events from D3RIP */
381  mqd_t mMQueueToReceive;
382 
383  /** Thread created for listening incoming events */
384  pthread_t mThreadListenModule;
385 
386  /** will be modified by WriteOutput and send by FlushOutput */
387  unsigned char mCommunicationRequestCount;
388 
389  /** Buffer for outgoing events */
390  ParameterRecord mCommunicationRequests[D3RIP_URT_MAX_EVENT_COUNT*D3RIP_URT_MAX_PARAMETERS_COUNT];
391 
392  /** Stores the total number of events in the system configuration */
393  unsigned char mEventCount;
394 
395  /** Stores the total number of events to be send over D3RIP */
396  unsigned char mEventsToSendCount;
397 
398  /** Stores the Ids for outgoing events */
399  unsigned char mEventIdsToSend[D3RIP_URT_MAX_EVENT_COUNT];
400 
401  /** It is needed to assign a common Ids to the logical events at all controller nodes for the sake of consistency */
402  std::map<std::string, int> mEventIdMap;
403 
404  /**
405  * Default constructor
406  */
407  d3ripURTDevice(void);
408 
409  /**
410  * Copy constructor [not functional, since DoAssign is not implemented]
411  */
412  d3ripURTDevice(const d3ripURTDevice& rSrc) : vDevice()
413  { DoAssign(rSrc); };
414 
415  /**
416  * Explicit destructor.
417  */
418  virtual ~d3ripURTDevice(void);
419 
420 
421  /**
422  * Clear all configuration.
423  * This implies Stop().
424  */
425  virtual void Clear(void);
426 
427  /**
428  *
429  * Build up internal data-structure(e.g. signal-event - mapping and extract D3RIP parameters)
430  *
431  */
432  virtual void Compile(void);
433 
434 
435  /**
436  * Activate the device. This function enables output execution and input reading.
437  *
438  * Configure for real-time operation and create D3RIP listener thread.
439  *
440  * @exception Exception
441  * - Not yet configured (id 551)
442  */
443  virtual void Start(void);
444 
445  /**
446  * Deactivate the device. This function disables output execution and input reading.
447  */
448  virtual void Stop(void);
449 
450  /** Clear dynamic data and restart device */
451  virtual void Reset(void);
452 
453 
454  /**
455  * Prepare the outgoing buffer regarding D3RIP specifications
456  *
457  * @exception Exception
458  * - unknown output event (id 65)
459  */
460  virtual void WriteOutput(Idx output);
461 
462  /**
463  * Forward all ready-to-send events to the D3RIP for tranmission to other controllers
464  *
465  * @exception Exception
466  * - unknown output event (id 65)
467  */
468  void FlushOutput(unsigned char channel);
469 
470  protected:
471 
472  /** Overall configuration (with actual type) */
473  TaNameSet<AttributeD3ripURTEvent>* pConfiguration;
474 
475 
476  /**
477  * Writes non-event-related configuration to TokenWriter
478  *
479  * Device data will be written bottom-to-top along the class-hierarchy,
480  * see also vDevice::DoWritePreface.
481  *
482  * Note: in order to keep the outputfile-layout as simple as possible no label will
483  * be used to separate this data-section.
484  *
485  * @param rTw
486  * TokenWriter to write
487  * @param rLabel
488  * Section to write
489  * @param pContext
490  * Context to provide contextual information
491  *
492  * */
493  void DoWritePreface(TokenWriter& rTw, const std::string& rLabel, const Type* pContext=0) const;
494 
495  /** Reads non-event-related configuration from TokenReader
496  *
497  * Device date is read bottom-to-top along the class-hierarchy;
498  * see also vDevice::DoReadPreface.
499  *
500  *
501  * Note: in order to keep the inputfile-layout as simple as possible no label will
502  * be used to separate this data-section.
503  *
504  * @param rTr
505  * TokenReader to read from
506  * @param rLabel
507  * Section to read
508  * @param pContext
509  * Read context to provide contextual information
510  *
511  * */
512  virtual void DoReadPreface(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
513 
514  // dummies
515  void DoAssign(const d3ripURTDevice& rSrcAttr){};
516  virtual bool DoEqual(const d3ripURTDevice& rSrcAttr) const { return false;};
517 
518  private:
519 
520 };
521 
522 void* DoListenCLModule(void* pD3ripURTDevice);
523 }
524 
525 #endif // configure
526 #endif // include
527 
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
virtual void Clear(void)
Clear.
Definition: iop_vdevice.cpp:82
void DoAssign(const AttributeDeviceEvent &rSrcAttr)
Assignment.
Definition: iop_vdevice.cpp:90
Includes all libFAUDES headers, no plugins.
Virtual device for interface definition
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen