CoLaCommand.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2023 SICK AG, Waldkirch
3 //
4 // SPDX-License-Identifier: Unlicense
5 
6 #include "CoLaCommand.h"
7 
8 #include <algorithm> // for find
9 #include <string>
10 
11 #include "VisionaryEndian.h"
12 
13 namespace visionary {
14 
16 {
17  using EndianConv = Endian<endian::big, endian::native>;
18 
19  const auto begin = buffer.cbegin();
20  auto it = begin;
21  const auto end = buffer.end();
22 
23  std::string typeStr;
24  typeStr.reserve(3u);
25 
26  // we extract the 3 character type, s??
27  while ((it < end) && (typeStr.size() < 3))
28  {
29  typeStr.push_back(static_cast<char>(*it++));
30  }
31 
32  if (typeStr == "sRN")
34  else if (typeStr == "sRA")
36  else if (typeStr == "sWN")
38  else if (typeStr == "sWA")
40  else if (typeStr == "sMN")
42  else if (typeStr == "sAN")
44  else if (typeStr == "sFA")
46  else
47  m_type = CoLaCommandType::UNKNOWN; // this also catches a too-short type string
48 
49  switch (m_type)
50  {
52  // Read error code
53  m_parameterOffset = static_cast<std::size_t>(it - begin);
54  std::uint16_t erroru16;
55  if (!EndianConv::convertFrom(erroru16, it, end))
56  {
58  return false;
59  }
60  m_error = static_cast<CoLaError::Enum>(erroru16);
61  break;
62 
69  {
70  // we sent a named request, thus expect a named response, where the requested variable/method name is echoed
71  // delimited by spaces
72 
73  if ((it == end) || (static_cast<char>(*it) != ' '))
74  {
75  // no space. exit// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76  m_parameterOffset = static_cast<std::size_t>(it - begin);
78  return false;
79  }
80  // skip space
81  ++it;
82 
83  auto name_start = it;
84  it = std::find(it, end, static_cast<std::uint8_t>(' '));
85  if (it == end)
86  {
87  // space not found
88  m_parameterOffset = static_cast<std::size_t>(it - begin);
90  return false;
91  }
92 
93  // copy name
94  m_name.reserve(static_cast<std::size_t>(it - name_start));
95  for (auto nameit = name_start; nameit < it; ++nameit)
96  {
97  m_name.push_back(static_cast<char>(*nameit));
98  }
99 
100  // skip space
101  ++it;
102 
103  m_parameterOffset = static_cast<std::size_t>(it - begin);
104 
106  }
107  break;
108 
109  default:
110  // something UNKNOWN
111  m_parameterOffset = 0;
114  return false;
115  break;
116  }
117 
118  return true;
119 }
120 
121 CoLaCommand::CoLaCommand(CoLaCommandType::Enum commandType, CoLaError::Enum error, const std::string& name)
122  : m_type(commandType), m_name(name), m_parameterOffset(0u), m_error(error)
123 {
124 }
125 
127  : m_buffer(buffer), m_type(CoLaCommandType::UNKNOWN), m_parameterOffset(0), m_error(CoLaError::UNKNOWN)
128 {
129  if (!fromBuffer(buffer))
130  {
131  // no valid package, we reset all values to UNKNOWN
134  }
135 }
136 
137 CoLaCommand::~CoLaCommand() = default;
138 
140 {
141  return m_buffer;
142 }
143 
145 {
146  return m_type;
147 }
148 
149 const char* CoLaCommand::getName() const
150 {
151  return m_name.c_str();
152 }
153 
155 {
156  return m_parameterOffset;
157 }
158 
160 {
161  return m_error;
162 }
163 
165 {
167 }
168 
169 } // namespace visionary
visionary::CoLaCommand::getParameterOffset
std::size_t getParameterOffset() const
Get offset in bytes to where first parameter starts.
Definition: CoLaCommand.cpp:154
VisionaryEndian.h
visionary::CoLaCommand::networkErrorCommand
static CoLaCommand networkErrorCommand()
Create a command for network errors.
Definition: CoLaCommand.cpp:164
visionary
Definition: MD5.cpp:44
visionary::CoLaCommand::getError
CoLaError::Enum getError() const
Get error.
Definition: CoLaCommand.cpp:159
visionary::CoLaCommand
Definition: CoLaCommand.h:17
visionary::CoLaError::OK
@ OK
No error.
Definition: CoLaError.h:19
visionary::CoLaError::NETWORK_ERROR
@ NETWORK_ERROR
Network error (not sent with messages).
Definition: CoLaError.h:16
visionary::CoLaCommand::m_type
CoLaCommandType::Enum m_type
Definition: CoLaCommand.h:52
visionary::CoLaCommandType::WRITE_VARIABLE
@ WRITE_VARIABLE
Definition: CoLaCommandType.h:18
visionary::CoLaCommand::m_error
CoLaError::Enum m_error
Definition: CoLaCommand.h:55
visionary::CoLaCommandType::READ_VARIABLE
@ READ_VARIABLE
Definition: CoLaCommandType.h:16
visionary::CoLaError::Enum
Enum
Possible CoLa errors.
Definition: CoLaError.h:12
boost::foreach_detail_::begin
auto_any< BOOST_DEDUCED_TYPENAME foreach_iterator< T, C >::type > begin(auto_any_t col, type2type< T, C > *, boost::mpl::true_ *)
Definition: foreach.hpp:660
visionary::CoLaCommand::getBuffer
const ByteBuffer & getBuffer() const
Get the binary data buffer.
Definition: CoLaCommand.cpp:139
boost::foreach_detail_::end
auto_any< BOOST_DEDUCED_TYPENAME foreach_iterator< T, C >::type > end(auto_any_t col, type2type< T, C > *, boost::mpl::true_ *)
Definition: foreach.hpp:700
visionary::CoLaCommand::getName
const char * getName() const
Get the name of command.
Definition: CoLaCommand.cpp:149
visionary::CoLaCommand::getType
CoLaCommandType::Enum getType() const
Get the type of command.
Definition: CoLaCommand.cpp:144
visionary::CoLaCommandType::NETWORK_ERROR
@ NETWORK_ERROR
Definition: CoLaCommandType.h:14
visionary::CoLaCommandType::WRITE_VARIABLE_RESPONSE
@ WRITE_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:19
visionary::CoLaCommand::~CoLaCommand
~CoLaCommand()
visionary::Endian
Definition: VisionaryEndian.h:162
visionary::CoLaCommandType::METHOD_INVOCATION
@ METHOD_INVOCATION
Definition: CoLaCommandType.h:20
visionary::CoLaCommandType::READ_VARIABLE_RESPONSE
@ READ_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:17
visionary::CoLaCommandType::Enum
Enum
Definition: CoLaCommandType.h:12
visionary::CoLaCommand::CoLaCommand
CoLaCommand()
visionary::CoLaCommandType::UNKNOWN
@ UNKNOWN
Definition: CoLaCommandType.h:15
visionary::CoLaCommandType::COLA_ERROR
@ COLA_ERROR
Definition: CoLaCommandType.h:22
visionary::CoLaCommand::fromBuffer
bool fromBuffer(const ByteBuffer &buffer)
Decode a CoLa command from a given buffer.
Definition: CoLaCommand.cpp:15
visionary::CoLaCommand::m_name
std::string m_name
Definition: CoLaCommand.h:53
visionary::CoLaCommand::ByteBuffer
std::vector< std::uint8_t > ByteBuffer
Definition: CoLaCommand.h:20
visionary::CoLaCommand::m_buffer
ByteBuffer m_buffer
Definition: CoLaCommand.h:51
visionary::CoLaCommand::m_parameterOffset
std::size_t m_parameterOffset
Definition: CoLaCommand.h:54
CoLaCommand.h
visionary::CoLaCommandType::METHOD_RETURN_VALUE
@ METHOD_RETURN_VALUE
Definition: CoLaCommandType.h:21
visionary::UNKNOWN
@ UNKNOWN
Definition: AuthenticationSecure.h:15
visionary::CoLaError::UNKNOWN
@ UNKNOWN
Unknown error, internally thrown if SOPAS Scan received an unknown command.
Definition: CoLaError.h:131


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:38:05