CoLaParameterWriter.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
20 // -- END LICENSE BLOCK ------------------------------------------------
21 
23 
26 
27 namespace visionary {
28 
30  : m_type(type)
31  , m_name(name)
32 {
34 }
35 
37 
39 {
40  m_buffer.push_back(static_cast<uint8_t>(sInt));
41  return *this;
42 }
43 
45 {
46  m_buffer.push_back(uSInt);
47  return *this;
48 }
49 
51 {
52  const int16_t value = nativeToColaByteOrder(integer);
53  m_buffer.insert(m_buffer.end(),
54  reinterpret_cast<const uint8_t*>(&value),
55  reinterpret_cast<const uint8_t*>(&value) + 2);
56  return *this;
57 }
58 
60 {
61  const uint16_t value = nativeToColaByteOrder(uInt);
62  m_buffer.insert(m_buffer.end(),
63  reinterpret_cast<const uint8_t*>(&value),
64  reinterpret_cast<const uint8_t*>(&value) + 2);
65  return *this;
66 }
67 
69 {
70  const int32_t value = nativeToColaByteOrder(dInt);
71  m_buffer.insert(m_buffer.end(),
72  reinterpret_cast<const uint8_t*>(&value),
73  reinterpret_cast<const uint8_t*>(&value) + 4);
74  return *this;
75 }
76 
78 {
79  const uint32_t value = nativeToColaByteOrder(uDInt);
80  m_buffer.insert(m_buffer.end(),
81  reinterpret_cast<const uint8_t*>(&value),
82  reinterpret_cast<const uint8_t*>(&value) + 4);
83  return *this;
84 }
85 
87 {
88  const float value = nativeToColaByteOrder(real);
89  m_buffer.insert(m_buffer.end(),
90  reinterpret_cast<const uint8_t*>(&value),
91  reinterpret_cast<const uint8_t*>(&value) + 4);
92  return *this;
93 }
94 
96 {
97  const double value = nativeToColaByteOrder(lReal);
98  m_buffer.insert(m_buffer.end(),
99  reinterpret_cast<const uint8_t*>(&value),
100  reinterpret_cast<const uint8_t*>(&value) + 8);
101  return *this;
102 }
103 
105 {
106  *this << static_cast<uint8_t>(boolean);
107  return *this;
108 }
109 
111 {
112  uint32_t valueUDInt = 0;
113 
115  md5_byte_t byteData[16] = {0};
116  {
117  md5_state_t md5_state{};
118  md5_init(&md5_state);
119  md5_append(&md5_state, reinterpret_cast<unsigned char const*>(str.c_str()), str.size());
120  md5_finish(&md5_state, byteData);
121  }
122 
123  // 128 bit to 32 bit using XOR
124  int byte0 = byteData[0] ^ byteData[4] ^ byteData[8] ^ byteData[12];
125  int byte1 = byteData[1] ^ byteData[5] ^ byteData[9] ^ byteData[13];
126  int byte2 = byteData[2] ^ byteData[6] ^ byteData[10] ^ byteData[14];
127  int byte3 = byteData[3] ^ byteData[7] ^ byteData[11] ^ byteData[15];
128  valueUDInt = static_cast<uint32_t>(byte0 | (byte1 << 8) | (byte2 << 16) | (byte3 << 24));
129 
130  // Add as UDInt, it is already big endian
131  parameterUDInt(valueUDInt);
132 
133  return *this;
134 }
135 
137 {
138  // Add length of string
139  parameterUInt(static_cast<uint16_t>(str.length()));
140 
141  // Add string
142  m_buffer.insert(m_buffer.end(), str.begin(), str.end());
143 
144  return *this;
145 }
146 
148 {
149  m_buffer.insert(m_buffer.end(), str, str + strlen(str));
150  return *this;
151 }
152 
154 {
155  return parameterSInt(sInt);
156 }
157 
159 {
160  return parameterUSInt(uSInt);
161 }
162 
164 {
165  return parameterInt(integer);
166 }
167 
169 {
170  return parameterUInt(uInt);
171 }
172 
174 {
175  return parameterDInt(dInt);
176 }
177 
179 {
180  return parameterUDInt(uDInt);
181 }
182 
184 {
185  return parameterReal(real);
186 }
187 
189 {
190  return parameterLReal(lReal);
191 }
192 
194 {
195  return parameterBool(boolean);
196 }
197 
199 {
200  // Copy buffer
201  std::vector<uint8_t> buffer = m_buffer;
202 
203  return CoLaCommand(buffer);
204 }
205 
207 {
208  // Write command type
209  switch (type)
210  {
212  *this << "sRN ";
213  break;
215  *this << "sRA ";
216  break;
218  *this << "sWN ";
219  break;
221  *this << "sWA ";
222  break;
224  *this << "sMN ";
225  break;
227  *this << "sAN ";
228  break;
230  *this << "sFA";
231  break;
232  default:
233  return;
234  }
235 
236  // Write command name
237  *this << name << " ";
238 }
239 
240 } // namespace visionary
visionary::CoLaParameterWriter::operator<<
CoLaParameterWriter & operator<<(const char *str)
Add a string parameter, must be null-terminated.
Definition: CoLaParameterWriter.cpp:147
visionary::CoLaParameterWriter::parameterUSInt
CoLaParameterWriter & parameterUSInt(const uint8_t uSInt)
Add a unsigned short (8-bit, range [0, 255]).
Definition: CoLaParameterWriter.cpp:44
visionary::CoLaParameterWriter::parameterInt
CoLaParameterWriter & parameterInt(const int16_t integer)
Add a signed int (16-bit).
Definition: CoLaParameterWriter.cpp:50
VisionaryEndian.h
visionary
Definition: AuthenticationLegacy.h:25
visionary::CoLaParameterWriter
Builder for constructing CoLaCommands.
Definition: CoLaParameterWriter.h:35
visionary::CoLaCommand
Definition: CoLaCommand.h:32
visionary::CoLaParameterWriter::parameterBool
CoLaParameterWriter & parameterBool(const bool boolean)
Add a boolean as a byte, with 0 representing false, and 1 representing true.
Definition: CoLaParameterWriter.cpp:104
md5_state_s
Definition: MD5.h:67
md5_finish
void md5_finish(md5_state_t *pms, md5_byte_t digest[16])
Definition: MD5.cpp:360
visionary::CoLaParameterWriter::CoLaParameterWriter
CoLaParameterWriter(CoLaCommandType::Enum type, const char *name)
Construct a new CoLaParameterWriter.
Definition: CoLaParameterWriter.cpp:29
visionary::CoLaCommandType::WRITE_VARIABLE
@ WRITE_VARIABLE
Definition: CoLaCommandType.h:34
CoLaParameterWriter.h
md5_init
void md5_init(md5_state_t *pms)
Definition: MD5.cpp:313
MD5.h
visionary::CoLaParameterWriter::parameterFlexString
CoLaParameterWriter & parameterFlexString(const std::string &str)
Add a string as a flex string.
Definition: CoLaParameterWriter.cpp:136
visionary::CoLaCommandType::READ_VARIABLE
@ READ_VARIABLE
Definition: CoLaCommandType.h:32
md5_byte_t
unsigned char md5_byte_t
Definition: MD5.h:63
visionary::CoLaParameterWriter::m_buffer
std::vector< uint8_t > m_buffer
Definition: CoLaParameterWriter.h:40
visionary::CoLaParameterWriter::parameterUDInt
CoLaParameterWriter & parameterUDInt(const uint32_t uDInt)
Add an unsigned double int (32-bit, range [0, 4294967295]).
Definition: CoLaParameterWriter.cpp:77
visionary::CoLaParameterWriter::m_type
CoLaCommandType::Enum m_type
Definition: CoLaParameterWriter.h:38
md5_append
void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
Definition: MD5.cpp:322
visionary::CoLaCommandType::WRITE_VARIABLE_RESPONSE
@ WRITE_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:35
visionary::CoLaCommandType::METHOD_INVOCATION
@ METHOD_INVOCATION
Definition: CoLaCommandType.h:36
visionary::CoLaCommandType::READ_VARIABLE_RESPONSE
@ READ_VARIABLE_RESPONSE
Definition: CoLaCommandType.h:33
visionary::CoLaParameterWriter::parameterSInt
CoLaParameterWriter & parameterSInt(const int8_t sInt)
Add a signed short (8-bit, range [-128, 127]).
Definition: CoLaParameterWriter.cpp:38
visionary::CoLaCommandType::Enum
Enum
Definition: CoLaCommandType.h:28
visionary::CoLaParameterWriter::parameterUInt
CoLaParameterWriter & parameterUInt(const uint16_t uInt)
Add a unsigned int (16-bit, range [0, 65535]).
Definition: CoLaParameterWriter.cpp:59
visionary::CoLaParameterWriter::~CoLaParameterWriter
~CoLaParameterWriter()
Definition: CoLaParameterWriter.cpp:36
visionary::nativeToColaByteOrder
T nativeToColaByteOrder(T x)
Definition: VisionaryEndian.h:218
visionary::CoLaParameterWriter::parameterLReal
CoLaParameterWriter & parameterLReal(const double lReal)
Add a IEEE-754 double precision (64-bit).
Definition: CoLaParameterWriter.cpp:95
visionary::CoLaCommandType::COLA_ERROR
@ COLA_ERROR
Definition: CoLaCommandType.h:38
visionary::CoLaParameterWriter::m_name
const char * m_name
Definition: CoLaParameterWriter.h:39
visionary::CoLaParameterWriter::parameterReal
CoLaParameterWriter & parameterReal(const float real)
Add a IEEE-754 single precision (32-bit).
Definition: CoLaParameterWriter.cpp:86
visionary::CoLaCommandType::METHOD_RETURN_VALUE
@ METHOD_RETURN_VALUE
Definition: CoLaCommandType.h:37
visionary::CoLaParameterWriter::parameterPasswordMD5
CoLaParameterWriter & parameterPasswordMD5(const std::string &str)
Add the given password as a UDInt, using MD5 hashing.
Definition: CoLaParameterWriter.cpp:110
visionary::CoLaParameterWriter::writeHeader
void writeHeader(CoLaCommandType::Enum type, const char *name)
Definition: CoLaParameterWriter.cpp:206
visionary::CoLaParameterWriter::parameterDInt
CoLaParameterWriter & parameterDInt(const int32_t dInt)
Add an signed double int (32-bit).
Definition: CoLaParameterWriter.cpp:68
visionary::CoLaParameterWriter::build
const CoLaCommand build()
Definition: CoLaParameterWriter.cpp:198


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