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
78  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  return;
100  sick::datastructure::PacketBuffer deployedPacket = m_packet_merger_ptr->getDeployedPacketBuffer();
102 }
103 
105 {
106  if (m_packet_merger_ptr->isEmpty() || m_packet_merger_ptr->isComplete())
107  {
108  m_packet_merger_ptr->setTargetSize(m_tcp_parser_ptr->getExpectedPacketLength(packet));
109  }
110  m_packet_merger_ptr->addTCPPacket(packet);
111  return true;
112 }
113 
115 {
116  if (!m_packet_merger_ptr->isComplete())
117  {
118  m_async_tcp_client_ptr->initiateReceive();
119  return false;
120  }
121  return true;
122 }
123 
124 
126  const sick::datastructure::PacketBuffer& packet)
127 {
128  uint16_t requestID = m_tcp_parser_ptr->getRequestID(packet);
129  CommandPtr pendingCommand;
130  if (findCommand(requestID, pendingCommand))
131  {
132  pendingCommand->processReplyBase(packet.getBuffer());
133  removeCommand(requestID);
134  }
135  return true;
136 }
137 
138 bool Cola2Session::addCommand(const uint16_t& request_id, const CommandPtr& command)
139 {
140  if (m_pending_commands_map.find(request_id) != m_pending_commands_map.end())
141  {
142  return false;
143  }
144  m_pending_commands_map[request_id] = command;
145  return true;
146 }
147 
148 bool Cola2Session::findCommand(const uint16_t& request_id, CommandPtr& command)
149 {
150  if (m_pending_commands_map.find(request_id) == m_pending_commands_map.end())
151  {
152  return false;
153  }
154  command = m_pending_commands_map[request_id];
155  return true;
156 }
157 
158 bool Cola2Session::removeCommand(const uint16_t& request_id)
159 {
160  auto it = m_pending_commands_map.find(request_id);
161  if (it == m_pending_commands_map.end())
162  {
163  return false;
164  }
165  m_pending_commands_map.erase(it);
166  return true;
167 }
168 
170 {
171  if (m_last_request_id == std::numeric_limits<uint16_t>::max())
172  {
173  m_last_request_id = 0;
174  }
175  return ++m_last_request_id;
176 }
177 
178 } // namespace cola2
179 } // 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
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.
const VectorBuffer & getBuffer() const
Getter to return the VectorBuffer saved in the PacketBuffer.
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::vector< uint8_t > VectorBuffer
Typedef for a vector buffer, to sort the incoming packets.
Definition: PacketBuffer.h:71
std::map< uint16_t, CommandPtr > m_pending_commands_map
Definition: Cola2Session.h:148


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Thu May 9 2019 02:41:08