src
CoLaCommand.cpp
Go to the documentation of this file.
1
// -- BEGIN LICENSE BLOCK ----------------------------------------------
20
// -- END LICENSE BLOCK ------------------------------------------------
21
22
#include "
sick_safevisionary_base/CoLaCommand.h
"
23
24
#include "
sick_safevisionary_base/VisionaryEndian.h
"
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)
50
m_type
=
CoLaCommandType::READ_VARIABLE
;
51
else
if
(typeStr.compare(
"sRA"
) == 0)
52
m_type
=
CoLaCommandType::READ_VARIABLE_RESPONSE
;
53
else
if
(typeStr.compare(
"sWN"
) == 0)
54
m_type
=
CoLaCommandType::WRITE_VARIABLE
;
55
else
if
(typeStr.compare(
"sWA"
) == 0)
56
m_type
=
CoLaCommandType::WRITE_VARIABLE_RESPONSE
;
57
else
if
(typeStr.compare(
"sMN"
) == 0)
58
m_type
=
CoLaCommandType::METHOD_INVOCATION
;
59
else
if
(typeStr.compare(
"sAN"
) == 0)
60
m_type
=
CoLaCommandType::METHOD_RETURN_VALUE
;
61
else
if
(typeStr.compare(
"sFA"
) == 0)
62
m_type
=
CoLaCommandType::COLA_ERROR
;
63
64
if
(
m_type
==
CoLaCommandType::COLA_ERROR
)
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
}
72
else
if
(
m_type
==
CoLaCommandType::NETWORK_ERROR
)
73
{
74
m_error
=
CoLaError::NETWORK_ERROR
;
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
91
CoLaCommand::~CoLaCommand
() {}
92
93
const
std::vector<uint8_t>&
CoLaCommand::getBuffer
()
94
{
95
return
m_buffer
;
96
}
97
98
CoLaCommandType::Enum
CoLaCommand::getType
()
99
{
100
return
m_type
;
101
}
102
103
const
char
*
CoLaCommand::getName
()
104
{
105
return
m_name
.c_str();
106
}
107
108
size_t
CoLaCommand::getParameterOffset
()
109
{
110
return
m_parameterOffset
;
111
}
112
113
CoLaError::Enum
CoLaCommand::getError
()
114
{
115
return
m_error
;
116
}
117
118
CoLaCommand
CoLaCommand::networkErrorCommand
()
119
{
120
return
CoLaCommand
(
CoLaCommandType::NETWORK_ERROR
,
CoLaError::NETWORK_ERROR
,
""
);
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