VisionaryControl.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 <cassert>
7 #include <limits> // for int max
8 
9 #include "AuthenticationLegacy.h"
10 #include "AuthenticationSecure.h"
11 #include "CoLa2ProtocolHandler.h"
12 #include "CoLaBProtocolHandler.h"
13 #include "CoLaParameterReader.h"
14 #include "CoLaParameterWriter.h"
15 #include "ControlSession.h"
16 #include "TcpSocket.h"
17 #include "VisionaryControl.h"
18 #include "VisionaryEndian.h"
19 
20 namespace visionary {
21 
23  : m_protocolType(INVALID_PROTOCOL), m_sessionTimeout_ms(0), m_connectTimeout_ms(0), m_autoReconnect(false)
24 {
25 }
26 
28 
30  const std::string& hostname,
31  uint32_t sessionTimeout_ms,
32  bool autoReconnect,
33  uint32_t connectTimeout_ms)
34 {
35  m_protocolType = type;
36  m_hostname = hostname;
37  m_sessionTimeout_ms = sessionTimeout_ms;
38  m_connectTimeout_ms = connectTimeout_ms;
39  m_autoReconnect = autoReconnect;
40  m_pProtocolHandler = nullptr;
41  m_pTransport = nullptr;
42 
43  std::unique_ptr<TcpSocket> pTransport(new TcpSocket());
44 
45  if (INVALID_PROTOCOL == type)
46  {
47  return false;
48  }
49 
50  const uint16_t portno = static_cast<uint16_t>(type); // type equals to port number
51  if (pTransport->connect(hostname, portno, connectTimeout_ms) != 0)
52  {
53  return false;
54  }
55 
56  std::unique_ptr<IProtocolHandler> pProtocolHandler;
57 
58  switch (type)
59  {
60  case COLA_B:
61  pProtocolHandler = std::unique_ptr<IProtocolHandler>(new CoLaBProtocolHandler(*pTransport));
62  break;
63  case COLA_2:
64  pProtocolHandler = std::unique_ptr<IProtocolHandler>(new CoLa2ProtocolHandler(*pTransport));
65  break;
66  default:
67  assert(false /* unsupported protocol*/);
68  return false;
69  }
70 
71  const uint32_t sessionTimeout_s = sessionTimeout_ms / 1000;
72  const uint32_t max_sessionTimeout_s = std::numeric_limits<uint8_t>::max();
73 
74  if (sessionTimeout_s > max_sessionTimeout_s)
75  {
76  return false;
77  }
78 
79  if (!pProtocolHandler->openSession(static_cast<uint8_t>(sessionTimeout_s)))
80  {
81  pTransport->shutdown();
82  return false;
83  }
84 
85  std::unique_ptr<ControlSession> pControlSession;
86  pControlSession = std::unique_ptr<ControlSession>(new ControlSession(*pProtocolHandler));
87 
88  std::unique_ptr<IAuthentication> pAuthentication;
89  switch (type)
90  {
91  case COLA_B:
92  pAuthentication = std::unique_ptr<IAuthentication>(new AuthenticationLegacy(*this));
93  break;
94  case COLA_2:
95  pAuthentication = std::unique_ptr<IAuthentication>(new AuthenticationSecure(*this));
96  break;
97  default:
98  assert(false /* unsupported protocol*/);
99  return false;
100  }
101 
102  m_pTransport = std::move(pTransport);
103  m_pProtocolHandler = std::move(pProtocolHandler);
104  m_pControlSession = std::move(pControlSession);
105  m_pAuthentication = std::move(pAuthentication);
106 
107  return true;
108 }
109 
111 {
112  if (m_pAuthentication)
113  {
114  (void)m_pAuthentication->logout();
115  m_pAuthentication = nullptr;
116  }
117  if (m_pProtocolHandler)
118  {
119  m_pProtocolHandler->closeSession();
120  m_pProtocolHandler = nullptr;
121  }
122  if (m_pTransport)
123  {
124  m_pTransport->shutdown();
125  m_pTransport = nullptr;
126  }
127  if (m_pControlSession)
128  {
129  m_pControlSession = nullptr;
130  }
131 }
132 
133 bool VisionaryControl::login(IAuthentication::UserLevel userLevel, const std::string& password)
134 {
135  return m_pAuthentication->login(userLevel, password);
136 }
137 
139 {
140  return m_pAuthentication->logout();
141 }
142 
144 {
146 
148  if (response.getError() == CoLaError::OK)
149  {
151  }
152  else
153  {
154  return "";
155  }
156 }
157 
158 bool VisionaryControl::burstAcquisition(uint16_t burstLen)
159 {
160  const CoLaCommand command =
162 
164 
165  return response.getError() == CoLaError::OK;
166 }
167 
169 {
171 
173 
174  return response.getError() == CoLaError::OK;
175 }
176 
178 {
181 
182  return response.getError() == CoLaError::OK;
183 }
184 
186 {
189 
190  return response.getError() == CoLaError::OK;
191 }
192 
194 {
197 
198  return response.getError() == CoLaError::OK;
199 }
200 
202 {
204  (m_pControlSession != nullptr) ? m_pControlSession->send(command) : CoLaCommand(std::vector<uint8_t>());
205 
206  if (m_autoReconnect
207  && (response.getError() == CoLaError::SESSION_UNKNOWN_ID || response.getError() == CoLaError::NETWORK_ERROR))
208  {
209  if (m_pTransport)
210  {
211  m_pTransport->shutdown();
212  }
214  if (success)
215  {
217  }
218  }
219 
220  return response;
221 }
222 
224 {
226 
228  if (response.getError() == CoLaError::OK)
229  {
231  }
232  else
233  {
234  return 2114u; // default blob port
235  }
236 }
237 
238 } // namespace visionary
response
const std::string response
VisionaryControl.h
assert
Definition: mpl/assert.hpp:79
visionary::IAuthentication::UserLevel
UserLevel
Available CoLa user levels.
Definition: IAuthentication.h:15
VisionaryEndian.h
TcpSocket.h
visionary::CoLaParameterReader
Class for reading data from a CoLaCommand.
Definition: CoLaParameterReader.h:17
visionary::VisionaryControl::GetBlobPort
uint16_t GetBlobPort()
Get blob port address.
Definition: VisionaryControl.cpp:223
visionary::VisionaryControl::startAcquisition
bool startAcquisition()
Start streaming the data by calling the "PLAYSTART" method on the device. Works only when acquisition...
Definition: VisionaryControl.cpp:168
visionary
Definition: MD5.cpp:44
AuthenticationSecure.h
visionary::CoLaParameterWriter
Builder for constructing CoLaCommands.
Definition: CoLaParameterWriter.h:19
visionary::CoLaCommand
Definition: CoLaCommand.h:17
visionary::VisionaryControl::~VisionaryControl
~VisionaryControl()
command
ROSLIB_DECL std::string command(const std::string &cmd)
visionary::VisionaryControl::m_sessionTimeout_ms
uint32_t m_sessionTimeout_ms
Definition: VisionaryControl.h:132
visionary::CoLaError::OK
@ OK
No error.
Definition: CoLaError.h:19
visionary::VisionaryControl::VisionaryControl
VisionaryControl()
Definition: VisionaryControl.cpp:22
visionary::CoLaError::NETWORK_ERROR
@ NETWORK_ERROR
Network error (not sent with messages).
Definition: CoLaError.h:16
visionary::VisionaryControl::getDataStreamConfig
bool getDataStreamConfig()
Tells the device that there is a streaming channel by invoking a method named GetBlobClientConfig.
Definition: VisionaryControl.cpp:193
visionary::VisionaryControl::ProtocolType
ProtocolType
The numbers used for the protocols are the port numbers.
Definition: VisionaryControl.h:23
visionary::VisionaryControl::getDeviceIdent
std::string getDeviceIdent()
Get device information by calling the "DeviceIdent" method on the device.
Definition: VisionaryControl.cpp:143
visionary::VisionaryControl::m_hostname
std::string m_hostname
Definition: VisionaryControl.h:131
visionary::VisionaryControl::COLA_B
@ COLA_B
Definition: VisionaryControl.h:27
CoLaParameterWriter.h
CoLa2ProtocolHandler.h
visionary::VisionaryControl::m_autoReconnect
bool m_autoReconnect
Definition: VisionaryControl.h:134
visionary::VisionaryControl::m_pControlSession
std::unique_ptr< ControlSession > m_pControlSession
Definition: VisionaryControl.h:128
ControlSession.h
visionary::ControlSession
Definition: ControlSession.h:13
visionary::CoLaCommandType::READ_VARIABLE
@ READ_VARIABLE
Definition: CoLaCommandType.h:16
visionary::VisionaryControl::stopAcquisition
bool stopAcquisition()
Stops the data stream. Works always, also when acquisition is already stopped before.
Definition: VisionaryControl.cpp:185
visionary::VisionaryControl::m_pProtocolHandler
std::unique_ptr< IProtocolHandler > m_pProtocolHandler
Definition: VisionaryControl.h:126
visionary::VisionaryControl::m_connectTimeout_ms
uint32_t m_connectTimeout_ms
Definition: VisionaryControl.h:133
visionary::AuthenticationLegacy
Definition: AuthenticationLegacy.h:11
visionary::VisionaryControl::COLA_2
@ COLA_2
Definition: VisionaryControl.h:28
visionary::VisionaryControl::login
bool login(IAuthentication::UserLevel userLevel, const std::string &password)
Definition: VisionaryControl.cpp:133
visionary::VisionaryControl::INVALID_PROTOCOL
@ INVALID_PROTOCOL
Definition: VisionaryControl.h:25
visionary::VisionaryControl::m_protocolType
ProtocolType m_protocolType
Definition: VisionaryControl.h:130
visionary::VisionaryControl::open
bool open(ProtocolType type, const std::string &hostname, uint32_t sessionTimeout_ms=kSessionTimeout_ms, bool autoReconnect=true, uint32_t connectTimeout_ms=kSessionTimeout_ms)
Definition: VisionaryControl.cpp:29
visionary::CoLaBProtocolHandler
Definition: CoLaBProtocolHandler.h:17
visionary::VisionaryControl::m_pTransport
std::unique_ptr< TcpSocket > m_pTransport
Definition: VisionaryControl.h:125
visionary::CoLaCommandType::METHOD_INVOCATION
@ METHOD_INVOCATION
Definition: CoLaCommandType.h:20
visionary::CoLaError::SESSION_UNKNOWN_ID
@ SESSION_UNKNOWN_ID
The CoLa2 session id is not valid, either it timed out or it never existed.
Definition: CoLaError.h:107
visionary::CoLaParameterWriter::parameterUInt
CoLaParameterWriter & parameterUInt(const uint16_t uInt)
Add a unsigned int (16-bit, range [0, 65535]).
Definition: CoLaParameterWriter.cpp:45
visionary::CoLaParameterReader::readUInt
uint16_t readUInt()
Read a unsigned int (16 bit, range [0, 65535]) and advances position by 2 bytes. Throws an std::out_o...
Definition: CoLaParameterReader.cpp:54
visionary::VisionaryControl::logout
bool logout()
Logout from the device.
Definition: VisionaryControl.cpp:138
visionary::VisionaryControl::close
void close()
Definition: VisionaryControl.cpp:110
visionary::VisionaryControl::stepAcquisition
bool stepAcquisition()
Trigger a single image on the device. Works only when acquisition is stopped.
Definition: VisionaryControl.cpp:177
CoLaBProtocolHandler.h
visionary::VisionaryControl::sendCommand
CoLaCommand sendCommand(const CoLaCommand &command)
Send a CoLaBCommand to the device and waits for the result.
Definition: VisionaryControl.cpp:201
visionary::AuthenticationSecure
Definition: AuthenticationSecure.h:28
CoLaParameterReader.h
visionary::VisionaryControl::m_pAuthentication
std::unique_ptr< IAuthentication > m_pAuthentication
Definition: VisionaryControl.h:127
boost::move
BOOST_MOVE_FORCEINLINE ::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
Definition: utility_core.hpp:212
visionary::CoLaParameterReader::readFlexString
std::string readFlexString()
Read a flex string, and advance position according to string size. Throws an std::out_of_range except...
Definition: CoLaParameterReader.cpp:99
visionary::TcpSocket
Definition: TcpSocket.h:19
visionary::VisionaryControl::burstAcquisition
bool burstAcquisition(uint16_t burstLen)
Start streaming a burst of data by calling the "PLAYBURST" method on the device. Works only when acqu...
Definition: VisionaryControl.cpp:158
AuthenticationLegacy.h
visionary::CoLa2ProtocolHandler
Definition: CoLa2ProtocolHandler.h:17
visionary::CoLaParameterWriter::build
const CoLaCommand build()
Definition: CoLaParameterWriter.cpp:181


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