DCSModulesAPI  1.0.0
DCS sub project containing all the control modules as libraries.
internal.h
Go to the documentation of this file.
1 #pragma once
2 #include "../include/DCS_ModuleEngineControl.h"
3 #include "../../DCS_Utils/include/DCS_ModuleUtils.h"
4 
5 #include <Windows.h>
6 #include <winusb.h>
7 #include <queue>
8 
23 namespace DCS
24 {
29  namespace Serial
30  {
36  {
37  DWORD baudRate;
38  BYTE byteSize;
39  BYTE stopBits;
40  BYTE parity;
41 
42  CHAR eofChar;
43 
44  DWORD readIntervalTimeout;
45  DWORD readTotalTimeoutConstant;
46  DWORD readTotalTimeoutMultiplier;
47  DWORD writeTotalTimeoutConstant;
48  DWORD writeTotalTimeoutMultiplier;
49  };
50 
55  DCS_INTERNAL_TEST HANDLE init_handle(LPCSTR portName, DWORD rwAccess, SerialArgs args);
56 
61  DCS_INTERNAL_TEST BOOL write_bytes(HANDLE hComm, LPCSTR charArray, DWORD NbytesToWrite);
62 
67  DCS_INTERNAL_TEST BOOL read_bytes(HANDLE hComm, LPTSTR buffer, DWORD bufferSize, LPDWORD readBufferSize);
68 
73  DCS_INTERNAL_TEST BOOL close_handle(HANDLE hComm);
74 
79  DCS_INTERNAL_TEST i32 enumerate_ports(char* buffer, DCS::i32 buff_size);
80 
85  DCS_INTERNAL_TEST void comnumber_to_string(char pname[7], DCS::u8 n);
86  }
87 
95  namespace USerial
96  {
102  {
103  UCHAR PipeInId;
104  UCHAR PipeOutId;
105  };
106 
112  {
113  WINUSB_INTERFACE_HANDLE usb_handle;
114  HANDLE dev_handle;
115  PIPE_ID pipe_id;
116  };
117 
125  DCS_INTERNAL_TEST USBIntHandle init_usb_handle(std::string VID_PID);
126 
131  DCS_INTERNAL_TEST ULONG write_bulk_bytes(USBIntHandle hnd, PUCHAR buffer, DWORD size);
132 
137  DCS_INTERNAL_TEST ULONG read_bulk_bytes(USBIntHandle hnd, PUCHAR buffer, DWORD size);
138 
143  DCS_INTERNAL_TEST BOOL term_usb_handle(USBIntHandle handle);
144  }
145 
150  namespace Coms
151  {
152  class CmdBuffer;
153 
158  DCS_INTERNAL_TEST CmdBuffer& GetCmdBuffer();
159 
165  {
170  Command()
171  {
172  id = 0;
173  full_cmd = "";
174  axis = 0;
176  }
177 
182  template<typename T>
183  Command(Control::UnitTarget target, const char* cmd, u16 axis, T argument)
184  {
185  this->target = target;
186  this->axis = axis;
187  this->id = NextId();
188  this->wait_response = false;
189 
190  full_cmd = std::to_string(axis) + cmd + std::to_string(argument) + ";";
191  }
192 
197  template<>
198  Command(Control::UnitTarget target, const char* cmd, u16 axis, const char* argument)
199  {
200  this->target = target;
201  this->axis = axis;
202  this->id = NextId();
203 
204  if(argument[0] == '?')
205  this->wait_response = true;
206  else
207  this->wait_response = false;
208 
209  full_cmd = std::to_string(axis) + cmd + argument + ";";
210  }
211 
216  Command(Control::UnitTarget target, const char* cmd, u16 axis = 0)
217  {
218  this->target = target;
219  this->axis = axis;
220  this->id = NextId();
221  this->wait_response = false;
222 
223  full_cmd = (axis > 0 ? std::to_string(axis) : "") + cmd + ";";
224  }
225 
230  template<typename T>
231  Command(Control::UnitTarget target, const char* cmd, T argument)
232  {
233  this->target = target;
234  this->axis = 0;
235  this->id = NextId();
236  this->wait_response = false;
237 
238  full_cmd = cmd + std::to_string(argument) + ";";
239  }
240 
245  template<>
246  Command(Control::UnitTarget target, const char* cmd, const char* argument)
247  {
248  this->target = target;
249  this->axis = 0;
250  this->id = NextId();
251 
252  if (argument[0] == '?')
253  this->wait_response = true;
254  else
255  this->wait_response = false;
256 
257  full_cmd = cmd + std::string(argument) + ";";
258  }
259 
264  static Command Custom(Control::UnitTarget target, const char* cmd, bool response = false)
265  {
266  Command c;
267  c.target = target;
268  c.axis = 0;
269  c.id = NextId();
270  c.wait_response = response;
271 
272  c.full_cmd = cmd;
273  return c;
274  }
275 
280  static const u64 NextId()
281  {
282  static u64 nid = 0;
283  return nid++;
284  }
285 
290  Command operator+(const Command& rhs)
291  {
292  if (target != rhs.target)
293  {
294  LOG_ERROR("Cannot concatenate two commands for different targets.");
295  return Command();
296  }
297  else
298  {
299  Command res;
300  res.id = rhs.id;
301  res.axis = 0;
302  res.wait_response = wait_response || rhs.wait_response;
303  res.full_cmd = full_cmd + rhs.full_cmd;
304  return res;
305  }
306  }
307 
308 
309 #pragma warning( push )
310 #pragma warning( disable : 4251 )
311  std::string full_cmd;
312 #pragma warning( pop )
313 
314  u64 id;
315  u16 axis;
316  Control::UnitTarget target;
317  bool wait_response;
318  };
319 
320 #pragma warning( push )
321 #pragma warning( disable : 4251 )
322 
330  {
331  public:
332  using Type = Command;
333  CmdBuffer() {};
334  ~CmdBuffer() {};
335 
340  Type process()
341  {
342  std::unique_lock<std::mutex> lock(m);
343  if (q.empty())
344  {
345  c.wait(lock);
346  }
347  if (!q.empty())
348  {
349  Type copy = q.front();
350  q.pop();
351  return copy;
352  }
353  return Type();
354  }
355 
360  std::string schedule(const Type& value)
361  {
362  std::unique_lock<std::mutex> lock(m);
363  q.push(value);
364  lock.unlock();
365  c.notify_one();
366 
367  if (value.wait_response)
368  {
369  std::unique_lock<std::mutex> lock(mr);
370  cr.wait(lock);
371  return response_buffer;
372  }
373 
374  response_buffer = "";
375  return response_buffer;
376  }
377 
382  void reply(std::string response)
383  {
384  std::unique_lock<std::mutex> lock(mr);
385  response_buffer = response;
386  lock.unlock();
387  cr.notify_one();
388  }
389 
394  void notify_unblock()
395  {
396  c.notify_all();
397  }
398 
403  int size()
404  {
405  std::unique_lock<std::mutex> lock(m);
406  int s = (int)q.size();
407  lock.unlock();
408  return s;
409  }
410 
411  private:
412  std::queue<Type> q;
413  std::mutex m;
414  std::condition_variable c;
415 
416  std::mutex mr;
417  std::condition_variable cr;
418  std::string response_buffer;
419  };
420 #pragma warning( pop )
421  }
422 }
Definition: internal.h:164
Definition: internal.h:101
UnitTarget
Enumerates the diferent devices acessible via COM/USB ports.
Definition: DCS_ModuleEngineControl.h:31
Definition: internal.h:111
Definition: internal.h:35
unsigned short u16
Equivalent to uint_16t.
Definition: DCS_ModuleUtils.h:56
signed long i32
Equivalent to int_32t.
Definition: DCS_ModuleUtils.h:53
The ESP301 rotation stage controller target.
Definition: registry.h:70
Definition: internal.h:329
#define LOG_ERROR(msg,...)
Alias to LOG_LVL(Error, msg, VA_ARGS)
Definition: DCS_ModuleUtils.h:37
#define DCS_INTERNAL_TEST
Defines the export interface acessible via the dll-interface applicable for internal functions only...
Definition: exports.h:137
unsigned long long u64
Equivalent to uint_64t.
Definition: DCS_ModuleUtils.h:52
unsigned char u8
Equivalent to uint_8t.
Definition: DCS_ModuleUtils.h:58