Command.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 
24 // -- END LICENSE BLOCK ------------------------------------------------
25 
26 //----------------------------------------------------------------------
33 //----------------------------------------------------------------------
34 
36 
38 
39 
40 namespace sick {
41 namespace cola2 {
42 
43 Command::Command(Cola2Session& session, const uint16_t& command_type, const uint16_t& command_mode)
44  : m_session(session)
45  , m_command_mode(command_mode)
46  , m_command_type(command_type)
47 {
50  m_tcp_parser_ptr = std::make_shared<sick::data_processing::ParseTCPPacket>();
51 }
52 
54 {
55  m_execution_mutex.lock();
56 }
57 
58 std::vector<uint8_t> Command::constructTelegram(const std::vector<uint8_t>& telegram) const
59 {
60  auto v = addTelegramData(telegram);
61  return addTelegramHeader(v);
62 }
63 
64 void Command::processReplyBase(const std::vector<uint8_t>& packet)
65 {
66  m_tcp_parser_ptr->parseTCPSequence(packet, *this);
68  m_execution_mutex.unlock();
69 }
70 
72 {
73  boost::mutex::scoped_lock lock(m_execution_mutex);
74 }
75 
77 {
78  return m_was_successful;
79 }
80 
81 uint8_t Command::getCommandType() const
82 {
83  return m_command_type;
84 }
85 
86 void Command::setCommandType(const uint8_t& command_type)
87 {
88  m_command_type = command_type;
89 }
90 
91 uint8_t Command::getCommandMode() const
92 {
93  return m_command_mode;
94 }
95 
96 void Command::setCommandMode(const uint8_t& command_mode)
97 {
98  m_command_mode = command_mode;
99 }
100 
101 uint32_t Command::getSessionID() const
102 {
103  return m_session_id;
104 }
105 
106 void Command::setSessionID(const uint32_t& session_id)
107 {
108  m_session_id = session_id;
109 }
110 
111 uint16_t Command::getRequestID() const
112 {
113  return m_request_id;
114 }
115 
116 void Command::setRequestID(const uint16_t& request_id)
117 {
118  m_request_id = request_id;
119 }
120 
121 std::vector<uint8_t> Command::expandTelegram(const std::vector<uint8_t>& telegram,
122  size_t additional_bytes) const
123 {
124  // Allocate memory to the desired final size
125  std::vector<uint8_t> output(telegram.size() + additional_bytes);
126  // Copy the original telegram over second, this prevents potential reallocating if .resize() was
127  // used
128  std::copy(telegram.begin(), telegram.end(), output.begin());
129  return output;
130 }
131 
132 std::vector<uint8_t> Command::addTelegramHeader(const std::vector<uint8_t>& telegram) const
133 {
134  std::vector<uint8_t> header = prepareHeader();
135  std::vector<uint8_t>::iterator data_ptr = header.begin();
136  writeDataToDataPtr(data_ptr, telegram);
137  // Add telegram to end of new header, this may resize header
138  header.insert(header.end(), telegram.begin(), telegram.end());
139  return header;
140 }
141 
142 std::vector<uint8_t> Command::prepareHeader() const
143 {
144  return std::vector<uint8_t>(18);
145 }
146 
147 std::vector<uint8_t> Command::getDataVector() const
148 {
149  return m_data_vector;
150 }
151 
152 void Command::setDataVector(const std::vector<uint8_t>& data)
153 {
154  m_data_vector = data;
155 }
156 
157 void Command::writeDataToDataPtr(std::vector<uint8_t>::iterator data_ptr,
158  const std::vector<uint8_t>& telegram) const
159 {
160  writeCola2StxToDataPtr(data_ptr);
161  writeLengthToDataPtr(data_ptr, telegram);
162  writeCola2HubCntrToDataPtr(data_ptr);
163  writeCola2NoCToDataPtr(data_ptr);
164  writeSessionIdToDataPtr(data_ptr);
165  writeRequestIdToDataPtr(data_ptr);
166  writeCommandTypeToDataPtr(data_ptr);
167  writeCommandModeToDataPtr(data_ptr);
168 }
169 
170 void Command::writeCola2StxToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
171 {
172  uint32_t cola2_stx = 0x02020202;
173  read_write_helper::writeUint32BigEndian(data_ptr + 0, cola2_stx);
174 }
175 
176 void Command::writeLengthToDataPtr(std::vector<uint8_t>::iterator data_ptr,
177  const std::vector<uint8_t>& telegram) const
178 {
179  uint32_t length = 10 + telegram.size();
180  read_write_helper::writeUint32BigEndian(data_ptr + 4, length);
181 }
182 
183 void Command::writeCola2HubCntrToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
184 {
185  uint8_t cola2_hub_cntr = 0x00;
186  read_write_helper::writeUint8BigEndian(data_ptr + 8, cola2_hub_cntr);
187 }
188 
189 void Command::writeCola2NoCToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
190 {
191  uint8_t cola2_noc = 0x00;
192  read_write_helper::writeUint8BigEndian(data_ptr + 9, cola2_noc);
193 }
194 
195 void Command::writeSessionIdToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
196 {
198 }
199 
200 void Command::writeRequestIdToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
201 {
203 }
204 
205 void Command::writeCommandTypeToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
206 {
208 }
209 
210 void Command::writeCommandModeToDataPtr(std::vector<uint8_t>::iterator data_ptr) const
211 {
213 }
214 
215 } // namespace cola2
216 } // namespace sick
void writeUint32BigEndian(std::vector< uint8_t >::iterator it, const uint32_t v)
Writes an unsigned 32-bit integer to a buffer at offset in big endian encoding.
void setCommandMode(const uint8_t &command_mode)
Sets the command mode.
Definition: Command.cpp:96
void writeUint8BigEndian(std::vector< uint8_t >::iterator it, const uint8_t v)
Writes an unsigned 8-bit integer to a buffer at offset in big endian encoding.
void setSessionID(const uint32_t &session_id)
Sets the session ID.
Definition: Command.cpp:106
void setDataVector(const std::vector< uint8_t > &data)
Sets the data vector.
Definition: Command.cpp:152
std::vector< uint8_t > constructTelegram(const std::vector< uint8_t > &telegram) const
Adds the data to the telegram and afterwards the header with the correct length.
Definition: Command.cpp:58
void writeRequestIdToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:200
std::vector< uint8_t > getDataVector() const
Returns the data vector.
Definition: Command.cpp:147
uint16_t getRequestID() const
Returns the request id of the command.
Definition: Command.cpp:111
void writeCola2NoCToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:189
void writeCommandTypeToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:205
boost::mutex m_execution_mutex
Definition: Command.h:200
uint8_t getCommandType() const
Returns the command type.
Definition: Command.cpp:81
void setRequestID(const uint16_t &request_id)
Sets the request ID of the command.
Definition: Command.cpp:116
void writeCommandModeToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:210
void setCommandType(const uint8_t &command_type)
Sets the command type.
Definition: Command.cpp:86
Command(sick::cola2::Cola2Session &session, const uint16_t &command_type, const uint16_t &command_mode)
Constructor of the command. Sets the common variables for a command to the sensor.
Definition: Command.cpp:43
uint32_t getSessionID() const
Returns the current session ID.
Definition: Command.cpp:101
std::vector< uint8_t > prepareHeader() const
Definition: Command.cpp:142
std::shared_ptr< sick::data_processing::ParseTCPPacket > m_tcp_parser_ptr
Definition: Command.h:198
void waitForCompletion()
Scooped call to the mutex, which will block until the reply was processed.
Definition: Command.cpp:71
void writeLengthToDataPtr(std::vector< uint8_t >::iterator data_ptr, const std::vector< uint8_t > &telegram) const
Definition: Command.cpp:176
void writeUint16BigEndian(std::vector< uint8_t >::iterator it, const uint16_t v)
Writes an unsigned 16-bit integer to a buffer at offset in big endian encoding.
uint32_t m_session_id
Definition: Command.h:207
std::vector< uint8_t > expandTelegram(const std::vector< uint8_t > &telegram, size_t additional_bytes) const
Definition: Command.cpp:121
void writeSessionIdToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:195
sick::cola2::Cola2Session & m_session
Definition: Command.h:192
void processReplyBase(const std::vector< uint8_t > &packet)
Parses the da incoming data package and then processes it with the inherited processReply. Afterwards the mutex will be unlocked to allow new commands to be send.
Definition: Command.cpp:64
uint8_t getCommandMode() const
Returns the command mode.
Definition: Command.cpp:91
uint16_t getNextRequestID()
Returns the next request ID. The request ID is used to match the return packages of the sensor to the...
void lockExecutionMutex()
Locks a mutex to prevent other commands being executed in parallel.
Definition: Command.cpp:53
void writeCola2HubCntrToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:183
void writeDataToDataPtr(std::vector< uint8_t >::iterator data_ptr, const std::vector< uint8_t > &telegram) const
Definition: Command.cpp:157
uint32_t getSessionID() const
Returns the current session ID.
std::vector< uint8_t > addTelegramHeader(const std::vector< uint8_t > &telegram) const
Definition: Command.cpp:132
std::vector< uint8_t > m_data_vector
Definition: Command.h:210
uint8_t m_command_type
Definition: Command.h:205
Establishes a cola2 session with a sensor and enables execution of commands in this session...
Definition: Cola2Session.h:72
bool wasSuccessful() const
Returns if the command was successfully parsed.
Definition: Command.cpp:76
virtual bool processReply()=0
uint16_t m_request_id
Definition: Command.h:208
uint8_t m_command_mode
Definition: Command.h:204
virtual std::vector< uint8_t > addTelegramData(const std::vector< uint8_t > &telegram) const =0
void writeCola2StxToDataPtr(std::vector< uint8_t >::iterator data_ptr) const
Definition: Command.cpp:170


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Fri Apr 2 2021 02:45:41