otsdaq  3.09.00
FEVInterface.h
1 #ifndef _ots_FEVInterface_h_
2 #define _ots_FEVInterface_h_
3 
4 #define TRACEMF_USE_VERBATIM 1 // for trace longer path filenames
5 #include "artdaq/DAQdata/Globals.hh"
6 
7 #include "otsdaq/Configurable/Configurable.h"
8 #include "otsdaq/FECore/FESlowControlsWorkLoop.h"
9 #include "otsdaq/FiniteStateMachine/VStateMachine.h"
10 #include "otsdaq/WorkLoopManager/WorkLoop.h"
11 
12 #include "otsdaq/CoreSupervisors/CoreSupervisorBase.h"
13 
14 #include "otsdaq/SupervisorInfo/AllSupervisorInfo.h" //to send errors to Gateway, e.g.
15 
16 #include "otsdaq/FECore/FESlowControlsChannel.h"
17 #include "otsdaq/SOAPUtilities/SOAPMessenger.h" //for xdaq::ApplicationDescriptor communication
18 
19 #include <array>
20 #include <iostream>
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include <thread>
25 #include <vector>
26 
27 #include "otsdaq/Macros/CoutMacros.h"
28 
29 #define __ARGS__ \
30  [[maybe_unused]] const frontEndMacroStruct_t & feMacroStruct, \
31  [[maybe_unused]] FEVInterface::frontEndMacroConstArgs_t argsIn, \
32  [[maybe_unused]] FEVInterface::frontEndMacroArgs_t argsOut
33 
34 #define __GET_ARG_IN_NO_DEFAULT__(X, Y) getFEMacroConstArgumentValue<Y>(argsIn, X)
35 #define __GET_ARG_IN_DEFAULT__(X, Y, D) getFEMacroConstArgumentValue<Y>(argsIn, X, D)
36 #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
37 #define __GET_ARG_IN_CHOOSER__(...) \
38  GET_4TH_ARG(__VA_ARGS__, __GET_ARG_IN_DEFAULT__, __GET_ARG_IN_NO_DEFAULT__, )
39 #define __GET_ARG_IN__(...) __GET_ARG_IN_CHOOSER__(__VA_ARGS__)(__VA_ARGS__)
40 #define __GET_ARG_OUT__(X, Y) getFEMacroArgumentValue<Y>(argsOut, X)
41 
42 #define __SET_ARG_IN__(X, Y) FEVInterface::emplaceFEMacroArgumentValue(argsIn, X, Y)
43 #define __SET_ARG_OUT__(X, Y) FEVInterface::setFEMacroArgumentValue(argsOut, X, Y)
44 #define __SET_PCT_DONE__(X) setFEMacroPercentDone(X)
45 
46 // clang-format off
47 #define PLOTLY_PLOT "Plotly_Plot"
70 
71 namespace ots
72 {
73 class FEVInterfacesManager;
74 
84 class FEVInterface : public WorkLoop, public Configurable, public VStateMachine
85 {
86  friend class FEVInterfacesManager;
87 
88  public:
90  const std::string& interfaceUID,
91  const ConfigurationTree& theXDAQContextConfigTree,
92  const std::string& configurationPath);
93 
94  virtual ~FEVInterface(void);
95 
96  virtual void setParentPointers(CoreSupervisorBase* supervisor,
97  FEVInterfacesManager* manager);
98 
99  FEVInterfacesManager* parentInterfaceManager_ = nullptr;
100 
101  const std::string& getInterfaceUID (void) const { return interfaceUID_; }
102  const std::string& getInterfaceType (void) const { return interfaceType_; }
103 
104  virtual void universalRead (char* address, char* returnValue) = 0;
105  virtual void universalWrite (char* address, char* writeValue) = 0;
106  const unsigned int& getUniversalAddressSize (void) { return universalAddressSize_; }
107  const unsigned int& getUniversalDataSize (void) { return universalDataSize_; }
108  virtual void universalBlockRead (char* address, char* returnValue, unsigned int numberOfBytes) { throw std::runtime_error("UNDEFINED BLOCK READ"); /* to make compiler happy, use params */ __COUTV__((void*)address); __COUTV__((void*)returnValue); __COUTV__(numberOfBytes); }
110  virtual void universalBlockWrite (char* address, char* writeValue, unsigned int numberOfBytes) { throw std::runtime_error("UNDEFINED BLOCK WRITE"); /* to make compiler happy, use params */ __COUTV__((void*)address); __COUTV__((void*)writeValue); __COUTV__(numberOfBytes); }
111 
112 
113  void runSequenceOfCommands (const std::string& treeLinkName);
114 
115  static void sendAsyncExceptionToGateway (FEVInterface* fe, const std::string& errMsg, bool isPauseException, bool isStopException);
116 
119  virtual void configure (void)
120  {
121  __COUT__ << "\t Configure" << std::endl;
123  "LinkToConfigureSequence"); /*Run Configure Sequence Commands*/
124  }
125  virtual void start (std::string /*runNumber*/)
126  {
127  __COUT__ << "\t Start" << std::endl;
128  runSequenceOfCommands("LinkToStartSequence"); /*Run Start Sequence Commands*/
129  }
130  virtual void stop (void)
131  {
132  __COUT__ << "\t Stop" << std::endl;
133  runSequenceOfCommands("LinkToStopSequence"); /*Run Stop Sequence Commands*/
134  }
135  virtual void halt (void) { stop(); }
136  virtual void pause (void) { stop(); }
137  virtual void resume (void) { start(""); }
138  virtual bool running (void) { /*while(WorkLoop::continueWorkLoop_){;}*/ return false; }
144  virtual void configureSlowControls (void);
145  void addSlowControlsChannels (ConfigurationTree slowControlsGroupLink, std::map<std::string /* ROC UID*/, FESlowControlsChannel>* mapOfSlowControlsChannels);
146 
147  virtual void resetSlowControlsChannelIterator (void);
149  virtual unsigned int getSlowControlsChannelCount (void);
150  bool slowControlsRunning (void);
151  void startSlowControlsWorkLoop (void) { slowControlsWorkLoop_.startWorkLoop(); }
152  void stopSlowControlsWorkLoop (void) { slowControlsWorkLoop_.stopWorkLoop(); }
153 
154  static const std::string UNKNOWN_TYPE;
155 
156  protected:
158  std::map<std::string, FESlowControlsChannel> mapOfSlowControlsChannels_;
159  std::map<std::string,
160  FESlowControlsChannel>::iterator slowControlsChannelsIterator_;
161  FESlowControlsWorkLoop slowControlsWorkLoop_;
162  std::atomic<bool> slowControlsWorkLoopShouldRun_{true};
168  public:
170  static const std::string DEFAULT; //for default arg values in FE macro definitions
171 
172  using frontEndMacroArg_t = std::pair<const std::string /* arg name */, std::string /* arg return value */>;
173  using frontEndMacroArgs_t = std::vector<frontEndMacroArg_t>&;
174  using frontEndMacroConstArgs_t = const std::vector<frontEndMacroArg_t>&;
175  struct frontEndMacroStruct_t;
176  using frontEndMacroFunction_t = void (ots::FEVInterface::*)(__ARGS__);
178  {
180  const std::string& feMacroName,
181  const frontEndMacroFunction_t& feMacroFunction,
182  const std::vector<std::string>& namesOfInputArgs,
183  const std::vector<std::string>& namesOfOutputArgs,
184  const std::string& requiredUserPermissions = "1" /*Level definition: 0:no-access,1:=user,255:=admin*/,
185  const std::string& allowedCallingFrontEnds = "*" /*StringMacros:: wild card set match string (i.e. string-to-set, then wild-card-set match)*/,
186  const std::string& feMacroTooltip = "")
187  : feMacroName_ (feMacroName)
188  , macroFunction_ (feMacroFunction)
189  , namesOfInputArguments_ (namesOfInputArgs)
190  , namesOfOutputArguments_ (namesOfOutputArgs)
191  , requiredUserPermissions_ (requiredUserPermissions)
192  , allowedCallingFrontEnds_ (allowedCallingFrontEnds)
193  , feMacroTooltip_ (feMacroTooltip)
194  {
195  }
196 
197  const std::string feMacroName_;
199  const std::vector<std::string> namesOfInputArguments_, namesOfOutputArguments_;
200  const std::string requiredUserPermissions_;
201  const std::string allowedCallingFrontEnds_;
202  const std::string feMacroTooltip_;
203  }; //end frontEndMacroStruct_t
204  const std::map<std::string, frontEndMacroStruct_t>& getMapOfFEMacroFunctions(void)
205  {
206  return mapOfFEMacroFunctions_;
207  }
208  void runSelfFrontEndMacro (
209  const std::string& feMacroName,
210  const std::vector<FEVInterface::frontEndMacroArg_t>& inputArgs,
211  std::vector<FEVInterface::frontEndMacroArg_t>& outputArgs);
218  {
219  macroStruct_t(const std::string& macroString);
220 
221  enum
222  {
223  OP_TYPE_READ,
224  OP_TYPE_WRITE,
225  OP_TYPE_DELAY,
226  };
227 
228  struct readOp_t
229  {
230  uint64_t address_;
231  bool addressIsVar_;
232  std::string addressVarName_;
233  bool dataIsVar_;
234  std::string dataVarName_;
235  }; // end macroStruct_t::writeOp_t declaration
236 
237  struct writeOp_t
238  {
239  uint64_t address_;
240  bool addressIsVar_;
241  std::string addressVarName_;
242  uint64_t data_;
243  bool dataIsVar_;
244  std::string dataVarName_;
245  }; // end macroStruct_t::writeOp_t declaration
246 
247  struct delayOp_t
248  {
249  uint64_t delay_;
250  bool delayIsVar_;
251  std::string delayVarName_;
252  }; // end macroStruct_t::writeOp_t declaration
253 
254  std::string macroName_;
255  std::vector<std::pair<unsigned int /*op type*/,
256  unsigned int /*index in specific type vector*/> >
257  operations_;
258  std::vector<macroStruct_t::readOp_t> readOps_;
259  std::vector<macroStruct_t::writeOp_t> writeOps_;
260  std::vector<macroStruct_t::delayOp_t> delayOps_;
261  std::set<std::string> namesOfInputArguments_, namesOfOutputArguments_;
262  bool lsbf_;
263  }; // end macroStruct_t declaration
264  protected:
266  std::map<std::string /*name*/, uint64_t /*value*/>& variableMap);
267 
268  public:
275  template<class T>
276  void sendToFrontEnd (const std::string& targetInterfaceID, const T& value) const;
277  void runFrontEndMacro (const std::string& targetInterfaceID,const std::string& feMacroName, const std::vector<FEVInterface::frontEndMacroArg_t>& inputArgs, std::vector<FEVInterface::frontEndMacroArg_t>& outputArgs) const;
278 
279  void PauseResumeSlowControls (__ARGS__);
280 
285  template<class T>
286  void receiveFromFrontEnd (const std::string& requester, T& retValue, unsigned int timeoutInSeconds = 1) const;
288  void receiveFromFrontEnd (const std::string& requester, std::string& retValue, unsigned int timeoutInSeconds = 1) const;
291  template<class T>
292  T receiveFromFrontEnd (const std::string& requester = "*", unsigned int timeoutInSeconds = 1) const;
294  std::string receiveFromFrontEnd (const std::string& requester = "*", unsigned int timeoutInSeconds = 1) const;
295 
299  protected:
300  bool workLoopThread (toolbox::task::WorkLoop* workLoop);
301 
302  std::string interfaceUID_, interfaceType_;
303  std::string mfSubject_;
304 
305  unsigned int universalAddressSize_ = 0;
306  unsigned int universalDataSize_ = 0;
307 
308 
311  std::map<std::string, frontEndMacroStruct_t> mapOfFEMacroFunctions_;
313  const std::string& feMacroName,
314  frontEndMacroFunction_t feMacroFunction,
315  const std::vector<std::string>& namesOfInputArgs,
316  const std::vector<std::string>& namesOfOutputArgs,
317  uint8_t requiredUserPermissions = 1 /*Level definition: 0:no-access,1:=user,255:=admin*/,
318  const std::string& allowedCallingFEs = "*" /*StringMacros:: wild card set match string (i.e. string-to-set, then wild-card-set match)*/,
319  const std::string& feMacroTooltip = "");
321  const std::string& feMacroName,
322  frontEndMacroFunction_t feMacroFunction,
323  const std::vector<std::string>& namesOfInputArgs,
324  const std::vector<std::string>& namesOfOutputArgs,
325  const std::string& requiredUserPermissions = WebUsers::DEFAULT_USER_GROUP + ":1" /*Level definition: 0:no-access,1:=user,255:=admin*/,
326  const std::string& allowedCallingFEs = "*" /*StringMacros:: wild card set match string (i.e. string-to-set, then wild-card-set match)*/,
327  const std::string& feMacroTooltip = "");
328 
329  public:
330  static const std::string& getFEMacroConstArgument (frontEndMacroConstArgs_t args, const std::string& argName);
331  static std::string& getFEMacroArgument (frontEndMacroArgs_t args, const std::string& argName);
332  protected:
333  template<class T>
334  std::string& setFEMacroArgumentValue (frontEndMacroArgs_t args, const std::string& argName, const T& value) const;
335  public:
336  template<class T>
337  static std::string& emplaceFEMacroArgumentValue (frontEndMacroArgs_t args, const std::string& argName, const T& value);
338 
340  void setFEMacroPercentDone (unsigned int percentDone);
341  int getFEMacroPercentDone (std::thread::id threadID) const;
342  void clearFEMacroPercentDone (std::thread::id threadID);
343 
344  protected:
345  mutable std::mutex feMacroPercentDoneMutex_;
346  std::map<std::thread::id, int> feMacroPercentDoneMap_;
347 
348 }; // end FEVInterface class
349 
350 template<class T>
351 T getFEMacroConstArgumentValue (FEVInterface::frontEndMacroConstArgs_t args, const std::string& argName, const T& defaultValue = T());
352 
354 template<>
355 std::string getFEMacroConstArgumentValue<std::string> (FEVInterface::frontEndMacroConstArgs_t args, const std::string& argName, const std::string& defaultValue);
356 
357 template<class T>
358 T getFEMacroArgumentValue (FEVInterface::frontEndMacroArgs_t args, const std::string& argName);
359 
361 template<>
362 std::string getFEMacroArgumentValue<std::string> (FEVInterface::frontEndMacroArgs_t argsIn, const std::string& argName);
363 
365 #include "otsdaq/FECore/FEVInterface.icc"
366 
367 // clang-format on
368 
369 } // namespace ots
370 
371 #endif
void runMacro(FEVInterface::macroStruct_t &macro, std::map< std::string, uint64_t > &variableMap)
runMacro
virtual void resetSlowControlsChannelIterator(void)
virtual in case channels are handled in multiple maps, for example
void(ots::FEVInterface::*)(const frontEndMacroStruct_t &feMacroStruct, FEVInterface::frontEndMacroConstArgs_t argsIn, FEVInterface::frontEndMacroArgs_t argsOut) frontEndMacroFunction_t
void function (vector-of-inputs, vector-of-outputs)
Definition: FEVInterface.h:176
virtual void configureSlowControls(void)
end State Machine handlers
static void sendAsyncExceptionToGateway(FEVInterface *fe, const std::string &errMsg, bool isPauseException, bool isStopException)
void runSequenceOfCommands(const std::string &treeLinkName)
virtual void configure(void)
Definition: FEVInterface.h:119
std::map< std::string, frontEndMacroStruct_t > mapOfFEMacroFunctions_
Map of FE Macro functions members.
Definition: FEVInterface.h:311
static const std::string & getFEMacroConstArgument(frontEndMacroConstArgs_t args, const std::string &argName)
< for specialized access to FE Macro in/out arguments
bool universalBlockReadImplementationConfirmed
is confirmed by slow controls handling (for example) that universalBlockRead is implemented by the FE...
Definition: FEVInterface.h:109
void addSlowControlsChannels(ConfigurationTree slowControlsGroupLink, std::map< std::string, FESlowControlsChannel > *mapOfSlowControlsChannels)
static const std::string DEFAULT
end Slow Controls
Definition: FEVInterface.h:170
std::string mfSubject_
for GEN_COUT decorations which would be safe in destructors, e.g. mirror interfaceUID_
Definition: FEVInterface.h:303
virtual FESlowControlsChannel * getNextSlowControlsChannel(void)
virtual in case channels are handled in multiple maps, for example
void runFrontEndMacro(const std::string &targetInterfaceID, const std::string &feMacroName, const std::vector< FEVInterface::frontEndMacroArg_t > &inputArgs, std::vector< FEVInterface::frontEndMacroArg_t > &outputArgs) const
virtual void universalRead(char *address, char *returnValue)=0
throw std::runtime_error exception on error/timeout
void runSelfFrontEndMacro(const std::string &feMacroName, const std::vector< FEVInterface::frontEndMacroArg_t > &inputArgs, std::vector< FEVInterface::frontEndMacroArg_t > &outputArgs)
void setFEMacroPercentDone(unsigned int percentDone)
FE Macro progress reporting (for async macros via FESupervisor)
T receiveFromFrontEnd(const std::string &requester="*", unsigned int timeoutInSeconds=1) const
static std::string & getFEMacroArgument(frontEndMacroArgs_t args, const std::string &argName)
virtual bool running(void)
Definition: FEVInterface.h:138
bool slowControlsRunning(void)
slow controls workloop calls this
virtual unsigned int getSlowControlsChannelCount(void)
virtual in case channels are handled in multiple maps, for example
void receiveFromFrontEnd(const std::string &requester, T &retValue, unsigned int timeoutInSeconds=1) const
bool workLoopThread(toolbox::task::WorkLoop *workLoop)
end FE Communication helpers //////
void sendToFrontEnd(const std::string &targetInterfaceID, const T &value) const
end FE Macros
void registerFEMacroFunction(const std::string &feMacroName, frontEndMacroFunction_t feMacroFunction, const std::vector< std::string > &namesOfInputArgs, const std::vector< std::string > &namesOfOutputArgs, uint8_t requiredUserPermissions=1, const std::string &allowedCallingFEs="*", const std::string &feMacroTooltip="")
std::map< std::string, FESlowControlsChannel > mapOfSlowControlsChannels_
Slow Controls members.
Definition: FEVInterface.h:158
defines used also by OtsConfigurationWizardSupervisor
< members fully define a front-end macro function
Definition: FEVInterface.h:178
const frontEndMacroFunction_t macroFunction_
Note: must be called using this instance.
Definition: FEVInterface.h:198
macroStruct_t(const std::string &macroString)
macroStruct_t constructor
bool lsbf_
least significant byte first
Definition: FEVInterface.h:262