DABC (Data Acquisition Backbone Core)  2.9.9
Device.h
Go to the documentation of this file.
1 /********************************************************************
2  * The Data Acquisition Backbone Core (DABC)
3  ********************************************************************
4  * Copyright (C) 2009-
5  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
6  * Planckstr. 1
7  * 64291 Darmstadt
8  * Germany
9  * Contact: http://dabc.gsi.de
10  ********************************************************************
11  * This software can be used under the GPL license agreements as stated
12  * in LICENSE.txt file which is part of the distribution.
13  ********************************************************************/
14 #ifndef SAFTDABC_Device
15 #define SAFTDABC_Device
16 
17 #include "dabc/Device.h"
18 #include "dabc/Object.h"
19 #include "dabc/MemoryPool.h"
20 #include "dabc/threads.h"
21 
22 
23 //#include "mbs/MbsTypeDefs.h"
24 
25 // JAM 2019: switch between old and new saftlib
26 //#define DABC_SAFT_USE_2_0 1
27 // NOTE that this definition is usually handled by makefile using option "make saft=1 oldsaft=1"
28 
29 
30 
31 #include <map>
32 
33 #ifndef DABC_SAFT_USE_2_0
34 #include <giomm.h>
35 #include <glib.h>
36 #endif
37 
38 
39 #include <saftlib/SAFTd.h>
40 #include <saftlib/TimingReceiver.h>
41 #include <saftlib/SoftwareActionSink.h>
42 #include <saftlib/SoftwareCondition.h>
43 
44 #include <saftlib/Input.h>
45 
46 
47 
48 // for CommonFunctions:
49  #include <ctime>
50  #include <sys/time.h>
51  #include <cstdio>
52  #include <cstring>
53  #include <sstream>
54  #include <iostream>
55  #include <iomanip>
56  #include <inttypes.h>
57 
58 
59 
60 
61 
62 namespace saftdabc
63 {
64 
65 class Input;
66 
67 class Device: public dabc::Device
68 {
69 
70 public:
71 
72  Device (const std::string &name, dabc::Command cmd);
73  virtual ~Device ();
74 
76  void OnThreadAssigned();
77 
79  virtual bool DestroyByOwnThread();
80 
81 
82  virtual const char* ClassName () const
83  {
84  return "saftdabc::Device";
85  }
86 
87 
88  virtual int ExecuteCommand (dabc::Command cmd);
89 
91 
93  bool ClearConditions ();
94 
98  bool RegisterInputCondition (saftdabc::Input* receiver, std::string& ioname);
99 
104 #ifdef DABC_SAFT_USE_2_0
105  bool RegisterEventCondition (saftdabc::Input* receiver, uint64_t id, uint64_t mask, int64_t offset, unsigned char acceptflags);
106 #else
107  bool RegisterEventCondition (saftdabc::Input* receiver, guint64 id, guint64 mask, gint64 offset, unsigned char acceptflags);
108 #endif
109 
112 #ifdef DABC_SAFT_USE_2_0
113  const std::string GetInputDescription(uint64_t event);
114 #else
115  const std::string GetInputDescription(guint64 event);
116 #endif
117 
119  void AddEventStatistics (unsigned numevents);
120 
122  void SetInfo(const std::string &info, bool forceinfo=true);
123 
124 protected:
125 
128  void RunGlibMainLoop();
129 
130 
131  virtual void ObjectCleanup ();
132 
133 
134 
135  void SetDevInfoParName(const std::string &name)
136  {
137  fDevInfoName = name;
138  }
139 
140 
141 
142 protected:
143 
145  std::string fBoardName;
146 
147 
149  std::string fDevInfoName;
150 
151 #ifdef DABC_SAFT_USE_2_0
152 
153  std::shared_ptr<saftlib::TimingReceiver_Proxy> fTimingReceiver;
154 
156  std::vector<std::shared_ptr<saftlib::SoftwareCondition_Proxy> > fConditionProxies;
157 
159  std::vector<std::shared_ptr<saftlib::SoftwareActionSink_Proxy> > fActionSinks;
160 
163  std::map<std::string, uint64_t> fMap_PrefixName;
164 
165 #else
167  Glib::RefPtr<Glib::MainLoop> fGlibMainloop;
168 
170  Glib::RefPtr<saftlib::TimingReceiver_Proxy> fTimingReceiver;
171 
173  std::vector<Glib::RefPtr<saftlib::SoftwareCondition_Proxy> > fConditionProxies;
174 
176  std::vector<Glib::RefPtr<saftlib::SoftwareActionSink_Proxy> > fActionSinks;
177 
180  std::map<Glib::ustring, guint64> fMap_PrefixName;
181 #endif
182 
183 
184 
187 
188 
189 
190 
191 
192 
193 
194 };
195 
196 
197 
198  class DeviceRef : public dabc::DeviceRef {
199 
201 
202  public:
203 
205  bool ClearConditions () const
206  {
207  return (null () ? false : GetObject ()->ClearConditions ());
208  }
209 
210  bool RegisterInputCondition (saftdabc::Input* receiver, std::string& ioname)
211  {
212  return (null () ? false : GetObject ()->RegisterInputCondition (receiver, ioname));
213 
214  }
215 
216 #ifdef DABC_SAFT_USE_2_0
217  bool RegisterEventCondition (saftdabc::Input* receiver, uint64_t id, uint64_t mask, int64_t offset, unsigned char acceptflags)
218 #else
219  bool RegisterEventCondition (saftdabc::Input* receiver, guint64 id, guint64 mask, gint64 offset, unsigned char acceptflags)
220 #endif
221  {
222  return (null () ? false : GetObject ()->RegisterEventCondition (receiver, id, mask, offset, acceptflags));
223  }
224 
225 #ifdef DABC_SAFT_USE_2_0
226  const std::string GetInputDescription(uint64_t event)
227 #else
228  const std::string GetInputDescription(guint64 event)
229 #endif
230 
231 
232  {
233  return (null() ? std::string("") : GetObject ()->GetInputDescription(event));
234  }
235 
236  void AddEventStatistics (unsigned numevents)
237  {
238  if(GetObject()) GetObject ()->AddEventStatistics(numevents);
239  }
240  void SetInfo(const std::string &info, bool forceinfo=true)
241  {
242  if(GetObject()) GetObject ()->SetInfo(info, forceinfo);
243  }
244 
245 
246  };
247 
248 
251 // JAM TODO: may this be exported in saftlib?
252 
253 #ifdef DABC_SAFT_USE_2_0
254  // modes for printing to cout
255  const uint32_t PMODE_NONE = 0x0;
256  const uint32_t PMODE_DEC = 0x1;
257  const uint32_t PMODE_HEX = 0x2;
258  const uint32_t PMODE_VERBOSE = 0x4;
259 
260  // formatting of mask for action sink
261  uint64_t tr_mask(int i // number of bits
262  );
263 
264  // formatting of date for output
265  std::string tr_formatDate(uint64_t time, // time [ns]
266  uint32_t pmode // mode for printing
267  );
268 
269  // formatting of action event ID for output
270  std::string tr_formatActionEvent(uint64_t id, // 64bit event ID
271  uint32_t pmode // mode for printing
272  );
273 
274  // formatting of action param for output; the format depends also on evtNo, except if evtNo == 0xFFFF FFFF
275  std::string tr_formatActionParam(uint64_t param, // 64bit parameter
276  uint32_t evtNo, // evtNo (currently 12 bit) - part of the 64 bit event ID
277  uint32_t pmode // mode for printing
278  );
279 
280  // formatting of action flags for output
281  std::string tr_formatActionFlags(uint16_t flags, // 16bit flags
282  uint64_t delay, // used in case action was delayed
283  uint32_t pmode // mode for printing
284  );
285 
286 
287 
288 #else
289 
290 
291  // modes for printing to cout
292  const guint32 PMODE_NONE = 0x0;
293  const guint32 PMODE_DEC = 0x1;
294  const guint32 PMODE_HEX = 0x2;
295  const guint32 PMODE_VERBOSE = 0x4;
296 
297  // formatting of mask for action sink
298  guint64 tr_mask(int i // number of bits
299  );
300 
301  // formatting of date for output
302  std::string tr_formatDate(guint64 time, // time [ns]
303  guint32 pmode // mode for printing
304  );
305 
306  // formatting of action event ID for output
307  std::string tr_formatActionEvent(guint64 id, // 64bit event ID
308  guint32 pmode // mode for printing
309  );
310 
311  // formatting of action param for output; the format depends also on evtNo, except if evtNo == 0xFFFF FFFF
312  std::string tr_formatActionParam(guint64 param, // 64bit parameter
313  guint32 evtNo, // evtNo (currently 12 bit) - part of the 64 bit event ID
314  guint32 pmode // mode for printing
315  );
316 
317  // formatting of action flags for output
318  std::string tr_formatActionFlags(guint16 flags, // 16bit flags
319  guint64 delay, // used in case action was delayed
320  guint32 pmode // mode for printing
321  );
322 
323 
324 
325 #endif
326 
327 
328 } // namespace
329 
330 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Represents command with its arguments.
Definition: Command.h:99
Reference on dabc::Device class
Definition: Device.h:79
Base class for device implementation.
Definition: Device.h:43
posix pthread mutex
Definition: threads.h:61
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Base class for transport implementations.
Definition: Transport.h:37
const std::string GetInputDescription(guint64 event)
Definition: Device.h:228
bool ClearConditions() const
cleanup conditions
Definition: Device.h:205
bool RegisterEventCondition(saftdabc::Input *receiver, guint64 id, guint64 mask, gint64 offset, unsigned char acceptflags)
Definition: Device.h:219
void AddEventStatistics(unsigned numevents)
Definition: Device.h:236
bool RegisterInputCondition(saftdabc::Input *receiver, std::string &ioname)
Definition: Device.h:210
void SetInfo(const std::string &info, bool forceinfo=true)
Definition: Device.h:240
std::string fBoardName
name of the saftd attached board name, e.g.
Definition: Device.h:145
void RunGlibMainLoop()
this function keeps glib main loop alive.
Definition: Device.cxx:73
dabc::Mutex fConditionMutex
Mutex to protect list of condition proxies.
Definition: Device.h:186
virtual bool DestroyByOwnThread()
here we may insert some actions to the device cleanup methods
Definition: Device.cxx:92
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Device.h:82
const std::string GetInputDescription(guint64 event)
Return text description of input belonging to given event id, e.g.
Definition: Device.cxx:361
void SetDevInfoParName(const std::string &name)
Definition: Device.h:135
virtual void ObjectCleanup()
Central cleanup method for worker.
Definition: Device.cxx:107
virtual ~Device()
Definition: Device.cxx:86
std::string fDevInfoName
Name of info parameter for device messages.
Definition: Device.h:149
Device(const std::string &name, dabc::Command cmd)
Definition: Device.cxx:29
Glib::RefPtr< saftlib::TimingReceiver_Proxy > fTimingReceiver
handle for the timing receiver instance
Definition: Device.h:170
bool ClearConditions()
clean up all existing saftlib conditions
Definition: Device.cxx:431
std::map< Glib::ustring, guint64 > fMap_PrefixName
Translation table IO name <> prefix like in saft-io-ctl implementation.
Definition: Device.h:180
void OnThreadAssigned()
need to start timeout here
Definition: Device.cxx:61
void SetInfo(const std::string &info, bool forceinfo=true)
set info parameter.
Definition: Device.cxx:178
bool RegisterEventCondition(saftdabc::Input *receiver, guint64 id, guint64 mask, gint64 offset, unsigned char acceptflags)
Register input event per id to snoop on NewCondition(bool active, guint64 id, guint64 mask,...
Definition: Device.cxx:296
virtual dabc::Transport * CreateTransport(dabc::Command cmd, const dabc::Reference &port)
Definition: Device.cxx:154
virtual int ExecuteCommand(dabc::Command cmd)
Main method where commands are executed.
Definition: Device.cxx:114
std::vector< Glib::RefPtr< saftlib::SoftwareActionSink_Proxy > > fActionSinks
Helper vector to connect action sinks.
Definition: Device.h:176
Glib::RefPtr< Glib::MainLoop > fGlibMainloop
the glib main loop
Definition: Device.h:167
std::vector< Glib::RefPtr< saftlib::SoftwareCondition_Proxy > > fConditionProxies
Helper vector to connect conditions.
Definition: Device.h:173
void AddEventStatistics(unsigned numevents)
add numevents to the event ratemeter.
Definition: Device.cxx:425
bool RegisterInputCondition(saftdabc::Input *receiver, std::string &ioname)
Register input of name to snoop on NewCondition(bool active, guint64 id, guint64 mask,...
Definition: Device.cxx:193
The saftlib timing message input implementation for DABC.
Definition: Input.h:65
GSI Simplified API for Timing Library (saftlib) plug-in for dabc.
Definition: Definitions.h:57
const guint32 PMODE_NONE
Definition: Device.h:292
guint64 tr_mask(int i)
const guint32 PMODE_DEC
Definition: Device.h:293
const guint32 PMODE_HEX
Definition: Device.h:294
std::string tr_formatActionParam(guint64 param, guint32 evtNo, guint32 pmode)
Definition: Device.cxx:566
const guint32 PMODE_VERBOSE
Definition: Device.h:295
std::string tr_formatDate(guint64 time, guint32 pmode)
Definition: Device.cxx:513
std::string tr_formatActionEvent(guint64 id, guint32 pmode)
Definition: Device.cxx:541
std::string tr_formatActionFlags(guint16 flags, guint64 delay, guint32 pmode)
Definition: Device.cxx:593