Cola2Session.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 
37 namespace sick {
38 namespace cola2 {
39 
40 Cola2Session::Cola2Session(const std::shared_ptr<communication::AsyncTCPClient>& async_tcp_client)
41  : m_async_tcp_client_ptr(async_tcp_client)
42  , m_session_id(0)
43  , m_last_request_id(0)
44 {
45  m_async_tcp_client_ptr->setPacketHandler(boost::bind(&Cola2Session::processPacket, this, _1));
46  m_packet_merger_ptr = std::make_shared<sick::data_processing::TCPPacketMerger>();
47  m_tcp_parser_ptr = std::make_shared<sick::data_processing::ParseTCPPacket>();
48 }
49 
51 {
52  CommandPtr command_ptr = std::make_shared<CreateSession>(boost::ref(*this));
53  return executeCommand(command_ptr);
54 }
55 
57 {
58  CommandPtr command_ptr = std::make_shared<CloseSession>(boost::ref(*this));
59  return executeCommand(command_ptr);
60 }
61 
63 {
64  m_async_tcp_client_ptr->doDisconnect();
65 }
66 
68 {
69  addCommand(command->getRequestID(), command);
71  return true;
72 }
73 
75 {
76  command->lockExecutionMutex(); // lock
77  std::vector<uint8_t> telegram;
78  telegram = command->constructTelegram(telegram);
79  m_async_tcp_client_ptr->doSendAndReceive(telegram);
80  command->waitForCompletion(); // scooped locked to wait, unlocked on data processing
81  return true;
82 }
83 
84 
86 {
87  return m_session_id;
88 }
89 
90 void Cola2Session::setSessionID(const uint32_t& session_id)
91 {
92  m_session_id = session_id;
93 }
94 
96 {
97  addPacketToMerger(packet);
99  {
100  return;
101  }
102  sick::datastructure::PacketBuffer deployed_packet =
103  m_packet_merger_ptr->getDeployedPacketBuffer();
105 }
106 
108 {
109  if (m_packet_merger_ptr->isEmpty() || m_packet_merger_ptr->isComplete())
110  {
111  m_packet_merger_ptr->setTargetSize(m_tcp_parser_ptr->getExpectedPacketLength(packet));
112  }
113  m_packet_merger_ptr->addTCPPacket(packet);
114  return true;
115 }
116 
118 {
119  if (!m_packet_merger_ptr->isComplete())
120  {
121  m_async_tcp_client_ptr->initiateReceive();
122  return false;
123  }
124  return true;
125 }
126 
127 
129  const sick::datastructure::PacketBuffer& packet)
130 {
131  uint16_t request_id = m_tcp_parser_ptr->getRequestID(packet);
132  CommandPtr pending_command;
133  if (findCommand(request_id, pending_command))
134  {
135  pending_command->processReplyBase(*packet.getBuffer());
136  removeCommand(request_id);
137  }
138  return true;
139 }
140 
141 bool Cola2Session::addCommand(const uint16_t& request_id, const CommandPtr& command)
142 {
143  if (m_pending_commands_map.find(request_id) != m_pending_commands_map.end())
144  {
145  return false;
146  }
147  m_pending_commands_map[request_id] = command;
148  return true;
149 }
150 
151 bool Cola2Session::findCommand(const uint16_t& request_id, CommandPtr& command)
152 {
153  if (m_pending_commands_map.find(request_id) == m_pending_commands_map.end())
154  {
155  return false;
156  }
157  command = m_pending_commands_map[request_id];
158  return true;
159 }
160 
161 bool Cola2Session::removeCommand(const uint16_t& request_id)
162 {
163  auto it = m_pending_commands_map.find(request_id);
164  if (it == m_pending_commands_map.end())
165  {
166  return false;
167  }
168  m_pending_commands_map.erase(it);
169  return true;
170 }
171 
173 {
174  if (m_last_request_id == std::numeric_limits<uint16_t>::max())
175  {
176  m_last_request_id = 0;
177  }
178  return ++m_last_request_id;
179 }
180 
181 } // namespace cola2
182 } // namespace sick
bool checkIfPacketIsCompleteAndOtherwiseListenForMorePackets()
bool close()
Closes a session with the sensor. Executes the close session command.
std::shared_ptr< sick::data_processing::TCPPacketMerger > m_packet_merger_ptr
Definition: Cola2Session.h:145
A packetbuffer for the raw data from the sensor.
Definition: PacketBuffer.h:61
std::shared_ptr< sick::cola2::Command > CommandPtr
Typedef for a pointer containing a command to be executed.
Definition: Cola2Session.h:78
std::shared_ptr< std::vector< uint8_t > const > getBuffer() const
Getter to return a copy of the data saved in the PacketBuffer.
void processPacket(const sick::datastructure::PacketBuffer &packet)
std::shared_ptr< sick::data_processing::ParseTCPPacket > m_tcp_parser_ptr
Definition: Cola2Session.h:146
bool addPacketToMerger(const sick::datastructure::PacketBuffer &packet)
bool executeCommand(const CommandPtr &command)
Executes the command passed to the function.
Cola2Session(const std::shared_ptr< communication::AsyncTCPClient > &async_tcp_client)
Constructor of the cola2 session.
bool findCommand(const uint16_t &request_id, CommandPtr &command)
void doDisconnect()
Triggers the disconnection of the tcp socket.
std::shared_ptr< sick::communication::AsyncTCPClient > m_async_tcp_client_ptr
Definition: Cola2Session.h:143
bool open()
Opens a session with the sensor. Executes the create session command.
bool sendTelegramAndListenForAnswer(const CommandPtr &command)
bool addCommand(const uint16_t &request_id, const CommandPtr &command)
void setSessionID(const uint32_t &session_id)
Sets the current session ID.
uint16_t getNextRequestID()
Returns the next request ID. The request ID is used to match the return packages of the sensor to the...
bool removeCommand(const uint16_t &request_id)
bool startProcessingAndRemovePendingCommandAfterwards(const sick::datastructure::PacketBuffer &packet)
uint32_t getSessionID() const
Returns the current session ID.
std::map< uint16_t, CommandPtr > m_pending_commands_map
Definition: Cola2Session.h:148


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