SopasBase.hpp
Go to the documentation of this file.
1 //
2 // SopasBase.h
3 //
4 // Created on: 18.07.2011
5 // Author: sick
6 //
7 
8 #ifndef SOPASBASE_H
9 #define SOPASBASE_H
10 
11 #include "../BasicDatatypes.hpp"
12 #include "../datatypes/Scan.hpp"
13 #include "../interfaces/tcp.hpp"
14 
15 #include "colaa.hpp"
16 #include "colab.hpp"
17 #include <map> // for std::map
18 #include "../tools/Mutex.hpp"
19 
20 namespace devices
21 {
22 
23 class SopasEventMessage;
24 class SopasAnswer;
25 
35 class SopasBase
36 {
37 public:
38  static const std::string EVENTNAME_SUBSCRIBE_EVALCASES;
39  static const std::string EVENTNAME_SUBSCRIBE_SCANS;
40  static const std::string METHODNAME_LOGIN;
41  static const std::string METHODNAME_LOGOUT;
42  static const std::string METHODNAME_SET_SCANCONFIG;
43  static const std::string METHODNAME_START_MEASURE;
44  static const std::string METHODNAME_STOP_MEASURE;
45  static const std::string VARIABLENAME_DEVICEIDENT;
46  static const std::string VARIABLENAME_SCANCONFIG;
47  static const std::string VARIABLENAME_DATAOUTPUTRANGE;
48  static const std::string VARIABLENAME_SCANDATACONFIG;
49 
50  // sopas commands
51  static const std::string COMMAND_Read_Variable_ByIndex;
52  static const std::string COMMAND_Write_Variable_ByIndex;
53  static const std::string COMMAND_Invoke_Method_ByIndex;
54  static const std::string COMMAND_Method_Result_ByIndex;
55  static const std::string COMMAND_Register_Event_ByIndex;
56  static const std::string COMMAND_Send_Event_ByIndex; // receive data event
57 
58  static const std::string COMMAND_Read_Variable_Answer;
59  static const std::string COMMAND_Write_Variable_Answer;
60  static const std::string COMMAND_Invoke_Method_Answer;
61  static const std::string COMMAND_Method_Result_Answer;
62  static const std::string COMMAND_Register_Event_Answer;
63  static const std::string COMMAND_Event_Acknowledge;
64 
65  static const std::string COMMAND_Read_Variable_ByName;
66  static const std::string COMMAND_Write_Variable_ByName;
67  static const std::string COMMAND_Invoke_Method_ByName;
68  static const std::string COMMAND_Method_Result_ByName;
69  static const std::string COMMAND_Register_Event_ByName;
70  static const std::string COMMAND_Send_Event_ByName; // receive data event
71 
72  static const UINT16 INDEX_DEVICE_IDENT;
73 
75  {
78  };
79 
81  {
84  };
85 
88  {
90  // MSG_READ_VARIABLE, ///< Read Variable
91  // MSG_WRITE_VARIABLE, ///< Write Variable
92  // MSG_INVOKE_METHOD, ///< Invoke Method
93  // MSG_METHOD_RESULT, ///< Method Result
94  // MSG_REGISTER_EVENT, ///< Register Event
104  };
105 
106  typedef void (*DecoderFunction)(SopasEventMessage& frame); // Decoder for events
107 
109  SopasBase();
110 
112  virtual ~SopasBase();
113 
115 
127  virtual bool init(SopasProtocol protocol,
128  std::string ipAddress,
129  UINT16 portNumber,
130  bool weWantScanData,
131  bool weWantFieldData,
132  bool readOnlyMode,
133  Tcp::DisconnectFunction disconnectFunction,
134  void* obj);
135 
137  bool connect();
138 
140  bool isConnected();
141 
149  bool disconnect();
150 
157 
158 
159  void setReadOnlyMode(bool mode);
160 
161  bool isReadOnly();
162 
171  bool invokeMethod(const std::string& methodeName, BYTE* parameters, UINT16 parametersLength, SopasAnswer*& answer);
172 
181  bool invokeMethod(UINT16 index, BYTE* parameters, UINT16 parametersLength, SopasAnswer*& answer);
182 
189  bool readVariable(const std::string& variableName, SopasAnswer*& answer);
190 
197  bool readVariable(UINT16 index, SopasAnswer*& answer);
198 
206  bool writeVariable(const std::string& variableName, BYTE* parameters, UINT16 parametersLength);
207 
215  bool writeVariable(UINT16 index, BYTE* parameters, UINT16 parametersLength);
216 
222  bool registerEvent(const std::string& eventName);
223 
229  bool registerEvent(UINT16 index);
230 
236  bool unregisterEvent(const std::string& eventName);
237 
243  bool unregisterEvent(UINT16 index);
244 
250  void setEventCallbackFunction(DecoderFunction decoderFunction, const std::string& eventName)
251  {
252  m_decoderFunctionMapByName[eventName] = decoderFunction;
253  }
254 
260  void setEventCallbackFunction(DecoderFunction decoderFunction, UINT16 eventIndex)
261  {
262  m_decoderFunctionMapByIndex[eventIndex] = decoderFunction;
263  }
264 
265 
266  double makeAngleValid(double angle);
267 
268  const std::string& getScannerName() const { return m_scannerName; }
269  const std::string& getScannerVersion() const { return m_scannerVersion; }
270 
271  // Convert a SOPAS error code to readable text
272  static std::string convertSopasErrorCodeToText(UINT16 errorCode);
273 
274 protected:
275 
277  {
279  RI = 1,
280  WI = 2,
281  MI = 3,
282  AI = 4,
283  EI = 5,
284  SI = 6,
285  RA = 7,
286  WA = 8,
287  MA = 9,
288  AA = 10,
289  EA = 11,
290  SA = 12,
291  RN = 20,
292  AN = 21,
293  SN = 22,
294  FA = 50
296  };
297 
298  enum State
299  {
304  ,
308  // , RUNNING
309  };
310 
321  bool receiveAnswer(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer*& answer);
322  bool receiveAnswer_CoLa_A(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer*& answer );
323  bool receiveAnswer_CoLa_B(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer*& answer );
324  bool receiveAnswer(SopasCommand cmd, UINT16 index, UINT32 timeout, SopasAnswer*& answer );
325  bool receiveAnswer_CoLa_A(SopasCommand cmd, UINT16 index, UINT32 timeout, SopasAnswer*& answer);
326  bool receiveAnswer_CoLa_B(SopasCommand cmd, UINT16 index, UINT32 timeout, SopasAnswer*& answer);
327 
333  void sendCommandBuffer(UINT8* buffer, UINT16 len);
334 
335  SopasCommand colaA_decodeCommand(std::string* rxData);
336 
338  SopasCommand stringToSopasCommand(const std::string& cmdString);
339  std::string sopasCommandToString(SopasCommand cmd);
340 
341 protected:
342  // Decoder functions that need to be overwritten by derived classes
343  virtual void evalCaseResultDecoder(SopasEventMessage& msg) = 0;
344  virtual void scanDataDecoder(SopasEventMessage& msg) = 0;
345 
350 
353  std::string m_scannerName;
354  std::string m_scannerVersion;
355 
356  bool m_beVerbose; // true = Show extended status traces
357 
359 
360 private:
361  // TCP
362  bool openTcpConnection();
363  void closeTcpConnection();
364 
366  static void readCallbackFunctionS(void* obj, UINT8* buffer, UINT32& numOfBytes);
367  void readCallbackFunction(UINT8* buffer, UINT32& numOfBytes);
368 
371 
373  void processFrame(SopasEventMessage& frame);
376  void copyFrameToResposeBuffer(UINT32 frameLength);
377  void removeFrameFromReceiveBuffer(UINT32 frameLength);
378 
379  // SOPAS / Cola
382  void colaA_decodeScannerTypeAndVersion(std::string* rxData);
384 
385 private:
386  typedef std::map<std::string, DecoderFunction> DecoderFunctionMapByName;
387  typedef std::map<UINT16, DecoderFunction> DecoderFunctionMapByIndex;
388  DecoderFunctionMapByName m_decoderFunctionMapByName;
389  DecoderFunctionMapByIndex m_decoderFunctionMapByIndex;
390 
391 // DecoderFunction m_scanDecoderFunction;
392 // DecoderFunction m_evalCaseDecoderFunction;
393 
394  typedef std::map<UINT16, std::string> IndexToNameMap;
395  IndexToNameMap m_indexToNameMap;
396 
397  // Response buffer
401 
402  // Receive buffer
405 
406  // TCP
408  std::string m_ipAddress;
410 
412 };
413 
414 
417 {
418 public:
421 
424 
432  SopasEventMessage(BYTE* buffer, SopasBase::SopasProtocol protocol, UINT32 frameLength);
433 
435  {
436  return m_protocol;
437  }
438 
440  {
441  return m_encoding;
442  }
443 
445  {
446  return m_messageType;
447  }
448 
449  UINT32 size() const
450  {
451  return m_frameLength;
452  }
453 
455  UINT32 getPayLoadLength() const;
456 
457  std::string getCommandString() const;
458 
460  BYTE* getPayLoad();
461 
463  INT32 getVariableIndex();
464 
466  std::string getVariableName();
467 
468  bool isValid() const { return (m_buffer != NULL); }
469 
470 private:
471  void detectEncoding();
472 
473  void detectMessageType();
474 
475 private:
481 };
482 
485 {
486 public:
488  SopasAnswer(const BYTE* answer, UINT32 answerLength);
489 
491  ~SopasAnswer();
492 
493  BYTE* getBuffer() { return m_answerBuffer; }
494 
495  UINT32 size() { return m_answerLength; }
496 
497  bool isValid() { return (m_answerBuffer != NULL); }
498 
499 private:
502 };
503 
504 } // namespace devices
505 
506 #endif // SOPASBASE_H
SopasBase::SopasMessageType getMessageType() const
Definition: SopasBase.hpp:444
DecoderFunctionMapByIndex m_decoderFunctionMapByIndex
Definition: SopasBase.hpp:389
void processFrame_CoLa_A(SopasEventMessage &frame)
Definition: SopasBase.cpp:1147
void closeTcpConnection()
Definition: SopasBase.cpp:240
static const std::string COMMAND_Read_Variable_ByName
Definition: SopasBase.hpp:65
Command Language binary.
Definition: SopasBase.hpp:77
SopasBase::SopasMessageType m_messageType
Definition: SopasBase.hpp:480
State m_state
Device info.
Definition: SopasBase.hpp:352
Write Variable Answer.
Definition: SopasBase.hpp:286
bool registerEvent(const std::string &eventName)
Registers an event by name.
Definition: SopasBase.cpp:1963
unsigned char BYTE
bool receiveAnswer_CoLa_A(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer *&answer)
Definition: SopasBase.cpp:687
SopasMessageType
types of answers of the sensor
Definition: SopasBase.hpp:87
static const std::string VARIABLENAME_DEVICEIDENT
Definition: SopasBase.hpp:45
bool m_weWantScanData
Flag to enable/disable scan data reception.
Definition: SopasBase.hpp:348
static const std::string VARIABLENAME_SCANCONFIG
Definition: SopasBase.hpp:46
void colaB_decodeScannerTypeAndVersion(UINT8 *buffer, UINT16 pos)
Definition: SopasBase.cpp:1628
Class that encapsulates a buffer that was sent as return to a sync call. (variable / method) ...
Definition: SopasBase.hpp:484
Read Variable (by name)
Definition: SopasBase.hpp:291
uint16_t UINT16
static std::string convertSopasErrorCodeToText(UINT16 errorCode)
Definition: SopasBase.cpp:918
static const std::string COMMAND_Register_Event_Answer
Definition: SopasBase.hpp:62
static const std::string EVENTNAME_SUBSCRIBE_EVALCASES
Definition: SopasBase.hpp:38
static const std::string VARIABLENAME_SCANDATACONFIG
Definition: SopasBase.hpp:48
static const std::string COMMAND_Register_Event_ByName
Definition: SopasBase.hpp:69
Mutex m_receiveDataMutex
Access mutex for buffer.
Definition: SopasBase.hpp:400
const std::string & getScannerVersion() const
Definition: SopasBase.hpp:269
static const std::string COMMAND_Method_Result_Answer
Definition: SopasBase.hpp:61
Class that represents a message that was sent by a sensor. (Event message)
Definition: SopasBase.hpp:416
virtual ~SopasBase()
Destructor.
Definition: SopasBase.cpp:69
SopasCommand colaA_decodeCommand(std::string *rxData)
Definition: SopasBase.cpp:1370
SopasBase::SopasEncoding getEncodingType() const
Definition: SopasBase.hpp:439
void(* DisconnectFunction)(void *obj)
Definition: tcp.hpp:53
static const std::string METHODNAME_LOGOUT
Definition: SopasBase.hpp:41
uint32_t UINT32
static const std::string COMMAND_Invoke_Method_ByIndex
Definition: SopasBase.hpp:53
SopasEncoding m_encoding
ByName or ByIndex.
Definition: SopasBase.hpp:381
read/write variable, invoke methods by index (indexes will be generated !!!)
Definition: SopasBase.hpp:83
Definition: Mutex.hpp:16
static const std::string METHODNAME_LOGIN
Definition: SopasBase.hpp:40
Object has been constructed. Use init() to go into CONNECTED state.
Definition: SopasBase.hpp:301
SopasBase::SopasProtocol m_protocol
Definition: SopasBase.hpp:477
bool isConnected()
Returns true if the tcp connection is established.
Definition: SopasBase.cpp:179
Read Variable Answer.
Definition: SopasBase.hpp:285
static const std::string METHODNAME_STOP_MEASURE
Definition: SopasBase.hpp:44
SopasBase()
Default constructor.
Definition: SopasBase.cpp:59
DecoderFunctionMapByName m_decoderFunctionMapByName
Definition: SopasBase.hpp:388
UINT32 m_numberOfBytesInResponseBuffer
Number of bytes in buffer.
Definition: SopasBase.hpp:398
void readCallbackFunction(UINT8 *buffer, UINT32 &numOfBytes)
Definition: SopasBase.cpp:261
static const std::string COMMAND_Write_Variable_ByName
Definition: SopasBase.hpp:66
Invoke Method.
Definition: SopasBase.hpp:281
Event Acknowledge -Answer to register event.
Definition: SopasBase.hpp:101
void processFrame_CoLa_B(SopasEventMessage &frame)
Definition: SopasBase.cpp:1236
UINT32 m_numberOfBytesInReceiveBuffer
Number of bytes in buffer.
Definition: SopasBase.hpp:403
static const std::string COMMAND_Invoke_Method_Answer
Definition: SopasBase.hpp:60
void copyFrameToResposeBuffer(UINT32 frameLength)
Definition: SopasBase.cpp:1317
static const std::string COMMAND_Send_Event_ByIndex
Definition: SopasBase.hpp:56
bool receiveAnswer(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer *&answer)
Take answer from read thread and decode it. Waits for a certain answer by name. Event data (scans) ar...
Definition: SopasBase.cpp:541
static const std::string COMMAND_Register_Event_ByIndex
Definition: SopasBase.hpp:55
read/write variable, invoke methods by name
Definition: SopasBase.hpp:82
bool openTcpConnection()
Definition: SopasBase.cpp:221
static const std::string COMMAND_Read_Variable_Answer
Definition: SopasBase.hpp:58
Invoke Method Answer.
Definition: SopasBase.hpp:287
static const std::string COMMAND_Write_Variable_ByIndex
Definition: SopasBase.hpp:52
std::string sopasCommandToString(SopasCommand cmd)
Definition: SopasBase.cpp:1462
static const std::string COMMAND_Write_Variable_Answer
Definition: SopasBase.hpp:59
std::map< UINT16, DecoderFunction > DecoderFunctionMapByIndex
Definition: SopasBase.hpp:387
void removeFrameFromReceiveBuffer(UINT32 frameLength)
Definition: SopasBase.cpp:1343
bool writeVariable(const std::string &variableName, BYTE *parameters, UINT16 parametersLength)
Write a variable to the sensor by name.
Definition: SopasBase.cpp:1836
void setEventCallbackFunction(DecoderFunction decoderFunction, UINT16 eventIndex)
Definition: SopasBase.hpp:260
std::string m_ipAddress
Definition: SopasBase.hpp:408
Definition: tcp.hpp:30
Method Result (ny name)
Definition: SopasBase.hpp:292
SopasEventMessage findFrameInReceiveBuffer()
Depending on the protocol the start and end of a frame will be found.
Definition: SopasBase.cpp:333
Method Result Answer.
Definition: SopasBase.hpp:288
Method Result.
Definition: SopasBase.hpp:282
void setReadOnlyMode(bool mode)
Definition: SopasBase.cpp:202
IndexToNameMap m_indexToNameMap
Definition: SopasBase.hpp:395
int32_t INT32
void processFrame(SopasEventMessage &frame)
Reads one frame from receive buffer and decodes it.
Definition: SopasBase.cpp:1125
SopasCommand stringToSopasCommand(const std::string &cmdString)
Converts strings in sopas answer buffer to SopasCommand enum.
Definition: SopasBase.cpp:1377
bool m_fieldEventIsRegistered
Definition: SopasBase.hpp:347
static const std::string EVENTNAME_SUBSCRIBE_SCANS
Definition: SopasBase.hpp:39
bool m_weWantFieldData
Flag to enable/disable protection field data reception.
Definition: SopasBase.hpp:349
static const UINT16 INDEX_DEVICE_IDENT
Definition: SopasBase.hpp:72
static const std::string COMMAND_Event_Acknowledge
Definition: SopasBase.hpp:63
UINT8 m_receiveBuffer[25000]
Low-Level receive buffer for all data (25000 should be enough for NAV300 Events)
Definition: SopasBase.hpp:404
std::string m_scannerName
Read from scanner.
Definition: SopasBase.hpp:353
void(* DecoderFunction)(SopasEventMessage &frame)
Definition: SopasBase.hpp:106
virtual void evalCaseResultDecoder(SopasEventMessage &msg)=0
SopasProtocol m_protocol
Used protocol (ColaA oder ColaB)
Definition: SopasBase.hpp:380
virtual void scanDataDecoder(SopasEventMessage &msg)=0
void colaA_decodeScannerTypeAndVersion(std::string *rxData)
Definition: SopasBase.cpp:1598
const std::string & getScannerName() const
Definition: SopasBase.hpp:268
Event Acknowledge.
Definition: SopasBase.hpp:290
std::map< UINT16, std::string > IndexToNameMap
Definition: SopasBase.hpp:394
void setEventCallbackFunction(DecoderFunction decoderFunction, const std::string &eventName)
Definition: SopasBase.hpp:250
bool action_getScannerTypeAndVersion()
Reads the scanner type and version variable from the sensor and stores it in the member variables...
Definition: SopasBase.cpp:1550
bool disconnect()
Closes the connection to the LMS. This is the opposite of init().
Definition: SopasBase.cpp:187
Read Variable.
Definition: SopasBase.hpp:279
static const std::string METHODNAME_START_MEASURE
Definition: SopasBase.hpp:43
static const std::string COMMAND_Method_Result_ByIndex
Definition: SopasBase.hpp:54
SopasBase::SopasEncoding m_encoding
Definition: SopasBase.hpp:479
Register Event Answer.
Definition: SopasBase.hpp:289
Register Event.
Definition: SopasBase.hpp:283
bool receiveAnswer_CoLa_B(SopasCommand cmd, std::string name, UINT32 timeout, SopasAnswer *&answer)
Definition: SopasBase.cpp:783
static const std::string VARIABLENAME_DATAOUTPUTRANGE
Definition: SopasBase.hpp:47
bool readVariable(const std::string &variableName, SopasAnswer *&answer)
Reads a variable from the sensor by name.
Definition: SopasBase.cpp:1756
void sendCommandBuffer(UINT8 *buffer, UINT16 len)
Sends the content of the buffer via TCP to the sensor.
Definition: SopasBase.cpp:507
static const std::string COMMAND_Invoke_Method_ByName
Definition: SopasBase.hpp:67
Command Language ASCI.
Definition: SopasBase.hpp:76
static const std::string COMMAND_Read_Variable_ByIndex
Definition: SopasBase.hpp:51
std::string m_scannerVersion
Read from scanner.
Definition: SopasBase.hpp:354
UINT8 m_responseBuffer[1024]
Receive buffer for everything except scan data and eval case data.
Definition: SopasBase.hpp:399
double makeAngleValid(double angle)
Definition: SopasBase.cpp:2148
SopasBase::SopasProtocol getProtocolType() const
Definition: SopasBase.hpp:434
Send Event (by name, receive)
Definition: SopasBase.hpp:293
~SopasEventMessage()
Destructor.
Definition: SopasBase.hpp:423
bool connect()
Connects to a sensor via tcp and reads the device name.
Definition: SopasBase.cpp:124
std::map< std::string, DecoderFunction > DecoderFunctionMapByName
Definition: SopasBase.hpp:386
bool unregisterEvent(const std::string &eventName)
Unregisters an event by name.
Definition: SopasBase.cpp:2104
static const std::string COMMAND_Send_Event_ByName
Definition: SopasBase.hpp:70
bool invokeMethod(const std::string &methodeName, BYTE *parameters, UINT16 parametersLength, SopasAnswer *&answer)
Invoke a method on the sensor.
Definition: SopasBase.cpp:1650
bool m_scanEventIsRegistered
Definition: SopasBase.hpp:346
static void readCallbackFunctionS(void *obj, UINT8 *buffer, UINT32 &numOfBytes)
Function that will be called on incomming data via tcp.
Definition: SopasBase.cpp:251
uint8_t UINT8
static const std::string COMMAND_Method_Result_ByName
Definition: SopasBase.hpp:68
virtual bool init(SopasProtocol protocol, std::string ipAddress, UINT16 portNumber, bool weWantScanData, bool weWantFieldData, bool readOnlyMode, Tcp::DisconnectFunction disconnectFunction, void *obj)
Initialization.
Definition: SopasBase.cpp:96
Write Variable.
Definition: SopasBase.hpp:280
static const std::string METHODNAME_SET_SCANCONFIG
Definition: SopasBase.hpp:42


libsick_ldmrs
Author(s): SICK AG , Martin Günther , Jochen Sprickerhof
autogenerated on Sat Jun 8 2019 17:57:33