DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
registry.h
Go to the documentation of this file.
1 // THIS FILE WAS AUTOGENERATED //
3 // ANY MODIFICATIONS WILL BE ERASED //
5 // Generated by the DCS pre-processor //
7 
8 
33 #pragma once
34 #include "exports.h"
35 #include <unordered_map>
36 #include <functional>
37 #include <any>
38 #include "../DCS_Utils/include/DCS_ModuleUtils.h"
39 
40 #include "H:\Data\C++\DCScan-ModulesAPI\DCS_Acquisition\include\DCS_ModuleAcquisition.h"
41 #include "H:\Data\C++\DCScan-ModulesAPI\DCS_Core\include\DCS_ModuleCore.h"
42 #include "H:\Data\C++\DCScan-ModulesAPI\DCS_EngineControl\include\DCS_ModuleEngineControl.h"
43 
44 #define SV_CALL_NULL 0x0
45 #define SV_CALL_DCS_DAQ_NewAIVChannel 0x1
46 #define SV_CALL_DCS_DAQ_DeleteAIVChannel 0x2
47 #define SV_CALL_DCS_DAQ_StartAIAcquisition 0x3
48 #define SV_CALL_DCS_DAQ_StopAIAcquisition 0x4
49 #define SV_CALL_DCS_Threading_GetMaxHardwareConcurrency 0x5
50 #define SV_CALL_DCS_Control_IssueGenericCommand 0x6
51 #define SV_CALL_DCS_Control_IssueGenericCommandResponse 0x7
52 #define MAX_CALL 0x8
53 
54 #define SV_ARG_NULL 0x0
55 #define SV_ARG_DCS_DAQ_ChannelRef 0x1
56 #define SV_ARG_DCS_Control_UnitTarget 0x2
57 #define SV_ARG_DCS_DAQ_ChannelLimits 0x3
58 #define SV_ARG_DCS_f64 0x4
59 #define SV_ARG_DCS_Utils_BasicString 0x5
60 
61 #define SV_RET_VOID 0x0
62 #define SV_RET_DCS_Utils_BasicString 0x1
63 #define SV_RET_DCS_u16 0x2
64 
65 #define MAX_SUB 0x3
66 #define SV_EVT_DCS_DAQ_PeakDetectWithAngleEvent 0x1
67 #define SV_EVT_DCS_DAQ_VoltageEvent 0x2
68 #define SV_EVT_DCS_Network_Message_FibSeqEvt 0x3
69 
70 namespace DCS {
71 
81  class Registry {
82  public:
83  struct SVParams;
84  struct SVReturn;
85 
91  static DCS_API const u16 Get(const char* func_signature)
92  {
93  u16 val = 0;
94  auto it = id.find(func_signature);
95  if (it != id.end())
96  val = it->second;
97  else
98  LOG_ERROR("Function signature (%s) not found.", func_signature);
99  return val;
100  }
101 
102  typedef void (*EventCallbackFunc)(u8* data, u8* userData);
103 
107  static DCS_API const i32 SetupEvent(unsigned char* buffer, u8 id, EventCallbackFunc f, u8* userData = nullptr)
108  {
109  memcpy(buffer, &id, sizeof(u8));
110 
111  evt_callbacks.emplace(id, f);
112  evt_userData.emplace(id, userData);
113 
114  return sizeof(u8);
115  }
116 
120  static DCS_API const i32 RemoveEvent(unsigned char* buffer, u8 id)
121  {
122  memcpy(buffer, &id, sizeof(u8));
123 
124  if (id <= MAX_SUB)
125  {
126  evt_callbacks.erase(id);
127  evt_userData.erase(id);
128  }
129 
130  return sizeof(u8);
131  }
132 
133  // HACK : GetEventCallback might fail in index referencing.
134  static DCS_API const EventCallbackFunc GetEventCallback(u8 id)
135  {
136  if (id <= MAX_SUB)
137  return evt_callbacks.at(id);
138  LOG_ERROR("Event id -> %d. No callback found.", id);
139  return nullptr;
140  }
141 
142  static DCS_API const bool CheckEvent(u8 id)
143  {
144  if (id <= MAX_SUB)
145  return subscriptions.at(id);
146  return false;
147  }
148 
149  static DCS_API const u8 GetEvent(const char* func)
150  {
151  return evt_named_func.at(func);
152  }
153 
154  // HACK : GetEventUserData might fail in index referencing.
155  static DCS_API u8* GetEventUserData(u8 id)
156  {
157  if (id <= MAX_SUB)
158  return evt_userData.at(id);
159  return nullptr;
160  }
161 
162  static DCS_API void SetEvent(u8 id)
163  {
164  if (id <= MAX_SUB)
165  subscriptions[id] = true;
166  }
167 
168  static DCS_API void UnsetEvent(u8 id)
169  {
170  if (id <= MAX_SUB)
171  subscriptions[id] = false;
172  }
173 
174  static DCS_API SVReturn Execute(SVParams params);
175 
176  private:
177  template<typename T>
178  static inline T convert_from_byte(const unsigned char* data, i32 offset, i32 size)
179  {
180  if(offset >= size)
181  {
182  LOG_ERROR("Data conversion overflow.");
183  return T();
184  }
185 
186  return *((T*)(data + offset));
187  }
188 
189  template<typename T>
190  static inline void convert_to_byte(T value, unsigned char* buffer, i32 offset, i32 size)
191  {
192  if(offset >= size)
193  {
194  LOG_ERROR("Data conversion overflow.");
195  return;
196  }
197  memcpy(&buffer[offset], (unsigned char*)&value, sizeof(T));
198  }
199 
200  inline static std::unordered_map<const char*, u16> id =
201  {
202  {"DCS::DAQ::NewAIVChannel", 0x1},
203  {"DCS::DAQ::DeleteAIVChannel", 0x2},
204  {"DCS::DAQ::StartAIAcquisition", 0x3},
205  {"DCS::DAQ::StopAIAcquisition", 0x4},
206  {"DCS::Threading::GetMaxHardwareConcurrency", 0x5},
207  {"DCS::Control::IssueGenericCommand", 0x6},
208  {"DCS::Control::IssueGenericCommandResponse", 0x7}
209  };
210 
211  inline static std::unordered_map<u8, bool> subscriptions =
212  {
216  };
217 
218  inline static std::unordered_map<const char*, u8> evt_named_func =
219  {
220  {"DCS::DAQ::PeakDetectWithAngleEvent", SV_EVT_DCS_DAQ_PeakDetectWithAngleEvent},
221  {"DCS::DAQ::VoltageEvent", SV_EVT_DCS_DAQ_VoltageEvent},
222  {"DCS::Network::Message::FibSeqEvt", SV_EVT_DCS_Network_Message_FibSeqEvt}
223  };
224 
225  inline static std::unordered_map<u16, const char*> r_id_debug =
226  {
227  {0x1, "SV_CALL_DCS_DAQ_NewAIVChannel"},
228  {0x2, "SV_CALL_DCS_DAQ_DeleteAIVChannel"},
229  {0x3, "SV_CALL_DCS_DAQ_StartAIAcquisition"},
230  {0x4, "SV_CALL_DCS_DAQ_StopAIAcquisition"},
231  {0x5, "SV_CALL_DCS_Threading_GetMaxHardwareConcurrency"},
232  {0x6, "SV_CALL_DCS_Control_IssueGenericCommand"},
233  {0x7, "SV_CALL_DCS_Control_IssueGenericCommandResponse"}
234  };
235 
236  inline static std::unordered_map<u8, EventCallbackFunc> evt_callbacks;
237  inline static std::unordered_map<u8, u8*> evt_userData;
238 
239  public:
244  {
245  public:
250  const u16 getFunccode() const
251  {
252  return fcode;
253  }
254 
261  template<typename T>
262  const T getArg(u64 i) const
263  {
264  T rv;
265  try
266  {
267  rv = std::any_cast<T>(args.at(i));
268  }
269  catch(const std::bad_any_cast& e)
270  {
271  LOG_ERROR("Bad SVParams getArg(%d) %s.", i, e.what());
272  }
273  return rv;
274  }
275 
283  static const SVParams GetParamsFromData(const unsigned char* payload, i32 size);
284 
294  template<typename... Args>
295  static i32 GetDataFromParams(unsigned char* buffer, u16 fcode, Args... args)
296  {
297  std::vector<std::any> p = {args...};
298  i32 it = sizeof(u16);
299  memcpy(buffer, &fcode, sizeof(u16));
300 
301  if(p.size() > 0)
302  {
303  switch(fcode)
304  {
306  {
307  auto A0_v = std::any_cast<DCS::Utils::BasicString>(p.at(0));
309  cpyArgToBuffer(buffer, (u8*)&A0_v, A0_t, sizeof(DCS::Utils::BasicString), it);
310  auto A1_v = std::any_cast<DCS::Utils::BasicString>(p.at(1));
312  cpyArgToBuffer(buffer, (u8*)&A1_v, A1_t, sizeof(DCS::Utils::BasicString), it);
313  auto A2_v = std::any_cast<DCS::DAQ::ChannelRef>(p.at(2));
315  cpyArgToBuffer(buffer, (u8*)&A2_v, A2_t, sizeof(DCS::DAQ::ChannelRef), it);
316  auto A3_v = std::any_cast<DCS::DAQ::ChannelLimits>(p.at(3));
318  cpyArgToBuffer(buffer, (u8*)&A3_v, A3_t, sizeof(DCS::DAQ::ChannelLimits), it);
319  break;
320  }
322  {
323  auto A0_v = std::any_cast<DCS::Utils::BasicString>(p.at(0));
325  cpyArgToBuffer(buffer, (u8*)&A0_v, A0_t, sizeof(DCS::Utils::BasicString), it);
326  break;
327  }
329  {
330  auto A0_v = std::any_cast<DCS::f64>(p.at(0));
331  u8 A0_t = SV_ARG_DCS_f64;
332  cpyArgToBuffer(buffer, (u8*)&A0_v, A0_t, sizeof(DCS::f64), it);
333  break;
334  }
336  {
337  auto A0_v = std::any_cast<DCS::Control::UnitTarget>(p.at(0));
339  cpyArgToBuffer(buffer, (u8*)&A0_v, A0_t, sizeof(DCS::Control::UnitTarget), it);
340  auto A1_v = std::any_cast<DCS::Utils::BasicString>(p.at(1));
342  cpyArgToBuffer(buffer, (u8*)&A1_v, A1_t, sizeof(DCS::Utils::BasicString), it);
343  break;
344  }
346  {
347  auto A0_v = std::any_cast<DCS::Control::UnitTarget>(p.at(0));
349  cpyArgToBuffer(buffer, (u8*)&A0_v, A0_t, sizeof(DCS::Control::UnitTarget), it);
350  auto A1_v = std::any_cast<DCS::Utils::BasicString>(p.at(1));
352  cpyArgToBuffer(buffer, (u8*)&A1_v, A1_t, sizeof(DCS::Utils::BasicString), it);
353  break;
354  }
355  default:
356  LOG_ERROR("GetDataFromParams() function code (fcode) not found.");
357  LOG_ERROR("Maybe function signature naming is invalid, or function does not take any arguments.");
358  break;
359  }
360  }
361 
362  return it;
363  }
364 
365  private:
366  SVParams(u16 fc, std::vector<std::any> args) : fcode(fc), args(args) { }
367 
368  private:
369  static void cpyArgToBuffer(unsigned char* buffer, u8* value, u8 type, i32 argSize, i32& it);
370 
371  private:
372  u16 fcode;
373 #pragma warning( push )
374 #pragma warning( disable : 4251 )
375  std::vector<std::any> args;
376 #pragma warning( pop )
377  };
378 
379 #pragma pack(push, 1)
380 
384  {
385  i8 type;
386  u8 ptr[1024];
387  };
388 #pragma pack(pop)
389  };
390 }
#define SV_ARG_DCS_DAQ_ChannelLimits
Refers to argument DCS::DAQ::ChannelLimits
Definition: registry.h:57
#define SV_EVT_DCS_DAQ_PeakDetectWithAngleEvent
A event refering to DCS::DAQ::PeakDetectWithAngleEvent
Definition: registry.h:66
static DCS_API const i32 RemoveEvent(unsigned char *buffer, u8 id)
Set up event to unsubscribe by ID SV_EVT_*.
Definition: registry.h:120
#define DCS_API
Defines the export interface acessible via the dll-interface. Only important for SHARED LIB Compile M...
Definition: exports.h:116
Holds messages return types.
Definition: registry.h:383
static DCS_API const i32 SetupEvent(unsigned char *buffer, u8 id, EventCallbackFunc f, u8 *userData=nullptr)
Set up event to subscribe by ID SV_EVT_*.
Definition: registry.h:107
UnitTarget
Enumerates the diferent devices acessible via COM/USB ports.
Definition: DCS_ModuleEngineControl.h:31
#define SV_ARG_DCS_f64
Refers to argument DCS::f64
Definition: registry.h:58
#define SV_EVT_DCS_DAQ_VoltageEvent
A event refering to DCS::DAQ::VoltageEvent
Definition: registry.h:67
double f64
Equivalent to double.
Definition: DCS_ModuleUtils.h:61
#define SV_ARG_DCS_DAQ_ChannelRef
Refers to argument DCS::DAQ::ChannelRef
Definition: registry.h:55
A very simple string buffer to hold char values. Used for string information manipulations via tcp/ip...
Definition: DCS_ModuleUtils.h:121
Autogenerated class responsible for registering any function calls that might be requested via tcp/ip...
Definition: registry.h:81
#define SV_CALL_DCS_Control_IssueGenericCommandResponse
A call to DCS::Control::IssueGenericCommandResponse
Definition: registry.h:51
unsigned short u16
Equivalent to uint_16t.
Definition: DCS_ModuleUtils.h:56
signed long i32
Equivalent to int_32t.
Definition: DCS_ModuleUtils.h:53
#define SV_ARG_DCS_Control_UnitTarget
Refers to argument DCS::Control::UnitTarget
Definition: registry.h:56
#define SV_ARG_DCS_Utils_BasicString
Refers to argument DCS::Utils::BasicString
Definition: registry.h:59
#define SV_CALL_DCS_DAQ_DeleteAIVChannel
A call to DCS::DAQ::DeleteAIVChannel
Definition: registry.h:46
signed char i8
Equivalent to int_8t.
Definition: DCS_ModuleUtils.h:57
#define SV_CALL_DCS_Control_IssueGenericCommand
A call to DCS::Control::IssueGenericCommand
Definition: registry.h:50
ChannelRef
Classifies a channel reference connector for the DAQ.
Definition: DCS_ModuleAcquisition.h:40
Definition: registry.h:70
#define SV_CALL_DCS_DAQ_NewAIVChannel
A call to DCS::DAQ::NewAIVChannel
Definition: registry.h:45
const T getArg(u64 i) const
Get the i&#39;th positional argument. Mostly internal use.
Definition: registry.h:262
#define LOG_ERROR(msg,...)
Alias to LOG_LVL(Error, msg, VA_ARGS)
Definition: DCS_ModuleUtils.h:37
static DCS_API const u16 Get(const char *func_signature)
Get a function id [SV_CALL_*] by function name. Syntax: ["ns::func"] Example: "DCS::Threading::GetMax...
Definition: registry.h:91
Channel measurement numeric limits.
Definition: DCS_ModuleAcquisition.h:56
unsigned long long u64
Equivalent to uint_64t.
Definition: DCS_ModuleUtils.h:52
#define SV_CALL_DCS_DAQ_StartAIAcquisition
A call to DCS::DAQ::StartAIAcquisition
Definition: registry.h:47
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58
Auto-generated class that allows for buffer <-> parameters conversion.
Definition: registry.h:243
#define SV_EVT_DCS_Network_Message_FibSeqEvt
A event refering to DCS::Network::Message::FibSeqEvt
Definition: registry.h:68
static i32 GetDataFromParams(unsigned char *buffer, u16 fcode, Args...args)
Fill a byte buffer with a list of arguments. use this function to populate the buffer passed to DCS::...
Definition: registry.h:295
const u16 getFunccode() const
Retrieve the function code of the currently held parameter list.
Definition: registry.h:250