CoLaCommand.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
20 // -- END LICENSE BLOCK ------------------------------------------------
21 
23 
25 #include <string>
26 
27 namespace visionary {
28 
29 CoLaCommand::CoLaCommand(CoLaCommandType::Enum commandType, CoLaError::Enum error, const char* name)
30  : m_buffer()
31  , m_type(commandType)
32  , m_name(name)
33  , m_parameterOffset(0)
34  , m_error(error)
35 {
36 }
37 
38 CoLaCommand::CoLaCommand(std::vector<uint8_t> buffer)
39  : m_buffer(buffer)
40  , m_type(CoLaCommandType::UNKNOWN)
41  , m_name("")
42  , m_parameterOffset(0)
43  , m_error(CoLaError::OK)
44 {
45  // Read type from header
46  if (buffer.size() < 3)
47  return;
48  std::string typeStr(reinterpret_cast<char*>(&buffer[0]), 3);
49  if (typeStr.compare("sRN") == 0)
51  else if (typeStr.compare("sRA") == 0)
53  else if (typeStr.compare("sWN") == 0)
55  else if (typeStr.compare("sWA") == 0)
57  else if (typeStr.compare("sMN") == 0)
59  else if (typeStr.compare("sAN") == 0)
61  else if (typeStr.compare("sFA") == 0)
63 
65  {
66  m_parameterOffset = 3; // sFA
67 
68  // Read error code
69  m_error = static_cast<CoLaError::Enum>(
70  readUnalignColaByteOrder<std::uint16_t>(&buffer[m_parameterOffset]));
71  }
73  {
75  }
76  else if (m_type != CoLaCommandType::UNKNOWN)
77  {
78  // Find name and parameter start
79  for (size_t i = 4; i < buffer.size(); i++)
80  {
81  if (buffer.at(i) == ' ')
82  {
83  m_name = std::string(reinterpret_cast<const char*>(&buffer[4]), i - 4);
84  m_parameterOffset = i + 1; // Skip space
85  break;
86  }
87  }
88  }
89 }
90 
92 
93 const std::vector<uint8_t>& CoLaCommand::getBuffer()
94 {
95  return m_buffer;
96 }
97 
99 {
100  return m_type;
101 }
102 
103 const char* CoLaCommand::getName()
104 {
105  return m_name.c_str();
106 }
107 
109 {
110  return m_parameterOffset;
111 }
112 
114 {
115  return m_error;
116 }
117 
119 {
121 }
122 
123 } // namespace visionary
VisionaryEndian.h
visionary::CoLaCommand::networkErrorCommand
static CoLaCommand networkErrorCommand()
Create a command for network errors.
Definition: CoLaCommand.cpp:118
visionary
Definition: AuthenticationLegacy.h:25
visionary::CoLaCommand
Definition: CoLaCommand.h:32
visionary::CoLaCommand::CoLaCommand
CoLaCommand(CoLaCommandType::Enum commandType, CoLaError::Enum error, const char *name)
Construct a new CoLaCommand with the given command type, error, and name, but without any data.
Definition: CoLaCommand.cpp:29
visionary::CoLaError::NETWORK_ERROR
@ NETWORK_ERROR
Network error (not sent with messages).
Definition: CoLaError.h:32
visionary::CoLaCommand::getError
CoLaError::Enum getError()
Get error.
Definition: CoLaCommand.cpp:113
visionary::CoLaCommand::m_type
CoLaCommandType::Enum m_type
Definition: CoLaCommand.h:36
visionary::CoLaCommandType::WRITE_VARIABLE
@ WRITE_VARIABLE
Definition: CoLaCommandType.h:34
visionary::CoLaCommand::m_error
CoLaError::Enum m_error
Definition: CoLaCommand.h:39
visionary::CoLaCommandType::READ_VARIABLE
@ READ_VARIABLE
Definition: CoLaCommandType.h:32
visionary::CoLaError::Enum
Enum
Possible CoLa errors.
Definition: CoLaError.h:28
visionary::CoLaCommand::getBuffer
const std::vector< uint8_t > & getBuffer()
Get the binary data buffer.
Definition: CoLaCommand.cpp:93
visionary::CoLaCommandType::NETWORK_ERROR
@ NETWORK_ERROR
Definition: CoLaCommandType.h:30
visionary::CoLaCommandType::WRITE_VARIABLE_RESPONSE
@ WRITE_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:35
visionary::CoLaCommand::~CoLaCommand
~CoLaCommand()
Definition: CoLaCommand.cpp:91
visionary::CoLaCommand::getName
const char * getName()
Get the name of command.
Definition: CoLaCommand.cpp:103
visionary::CoLaCommandType::METHOD_INVOCATION
@ METHOD_INVOCATION
Definition: CoLaCommandType.h:36
visionary::CoLaCommandType::READ_VARIABLE_RESPONSE
@ READ_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:33
visionary::CoLaCommand::getParameterOffset
size_t getParameterOffset()
Get offset in bytes to where first parameter starts.
Definition: CoLaCommand.cpp:108
visionary::CoLaCommandType::Enum
Enum
Definition: CoLaCommandType.h:28
visionary::CoLaCommandType::UNKNOWN
@ UNKNOWN
Definition: CoLaCommandType.h:31
visionary::CoLaCommandType::COLA_ERROR
@ COLA_ERROR
Definition: CoLaCommandType.h:38
visionary::CoLaCommand::m_name
std::string m_name
Definition: CoLaCommand.h:37
visionary::CoLaCommand::getType
CoLaCommandType::Enum getType()
Get the type of command.
Definition: CoLaCommand.cpp:98
CoLaCommand.h
visionary::CoLaCommand::m_parameterOffset
size_t m_parameterOffset
Definition: CoLaCommand.h:38
visionary::CoLaCommandType::METHOD_RETURN_VALUE
@ METHOD_RETURN_VALUE
Definition: CoLaCommandType.h:37
visionary::CoLaCommand::m_buffer
std::vector< uint8_t > m_buffer
Definition: CoLaCommand.h:35


sick_safevisionary_base
Author(s):
autogenerated on Sat Oct 21 2023 02:24:26