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  boost::mutex::scoped_lock lock(m_command_map_mutex);
144 
145  if (m_pending_commands_map.find(request_id) != m_pending_commands_map.end())
146  {
147  return false;
148  }
149  m_pending_commands_map[request_id] = command;
150  return true;
151 }
152 
153 bool Cola2Session::findCommand(const uint16_t& request_id, CommandPtr& command)
154 {
155  boost::mutex::scoped_lock lock(m_command_map_mutex);
156 
157  if (m_pending_commands_map.find(request_id) == m_pending_commands_map.end())
158  {
159  return false;
160  }
161  command = m_pending_commands_map[request_id];
162  return true;
163 }
164 
165 bool Cola2Session::removeCommand(const uint16_t& request_id)
166 {
167  boost::mutex::scoped_lock lock(m_command_map_mutex);
168 
169  auto it = m_pending_commands_map.find(request_id);
170  if (it == m_pending_commands_map.end())
171  {
172  return false;
173  }
174  m_pending_commands_map.erase(it);
175  return true;
176 }
177 
179 {
180  if (m_last_request_id == std::numeric_limits<uint16_t>::max())
181  {
182  m_last_request_id = 0;
183  }
184  return ++m_last_request_id;
185 }
186 
187 } // namespace cola2
188 } // namespace sick
sick::cola2::Cola2Session::CommandPtr
std::shared_ptr< sick::cola2::Command > CommandPtr
Typedef for a pointer containing a command to be executed.
Definition: Cola2Session.h:78
sick::cola2::Cola2Session::addPacketToMerger
bool addPacketToMerger(const sick::datastructure::PacketBuffer &packet)
Definition: Cola2Session.cpp:107
sick::cola2::Cola2Session::m_last_request_id
uint16_t m_last_request_id
Definition: Cola2Session.h:154
sick
Definition: ApplicationNameVariableCommand.h:43
sick::cola2::Cola2Session::Cola2Session
Cola2Session(const std::shared_ptr< communication::AsyncTCPClient > &async_tcp_client)
Constructor of the cola2 session.
Definition: Cola2Session.cpp:40
command
ROSLIB_DECL std::string command(const std::string &cmd)
sick::cola2::Cola2Session::m_command_map_mutex
boost::mutex m_command_map_mutex
Definition: Cola2Session.h:150
sick::cola2::Cola2Session::close
bool close()
Closes a session with the sensor. Executes the close session command.
Definition: Cola2Session.cpp:56
sick::cola2::Cola2Session::m_tcp_parser_ptr
std::shared_ptr< sick::data_processing::ParseTCPPacket > m_tcp_parser_ptr
Definition: Cola2Session.h:146
sick::cola2::Cola2Session::doDisconnect
void doDisconnect()
Triggers the disconnection of the tcp socket.
Definition: Cola2Session.cpp:62
sick::cola2::Cola2Session::findCommand
bool findCommand(const uint16_t &request_id, CommandPtr &command)
Definition: Cola2Session.cpp:153
sick::cola2::Cola2Session::sendTelegramAndListenForAnswer
bool sendTelegramAndListenForAnswer(const CommandPtr &command)
Definition: Cola2Session.cpp:74
sick::cola2::Cola2Session::executeCommand
bool executeCommand(const CommandPtr &command)
Executes the command passed to the function.
Definition: Cola2Session.cpp:67
sick::cola2::Cola2Session::open
bool open()
Opens a session with the sensor. Executes the create session command.
Definition: Cola2Session.cpp:50
sick::datastructure::PacketBuffer::getBuffer
std::shared_ptr< std::vector< uint8_t > const > getBuffer() const
Getter to return a copy of the data saved in the PacketBuffer.
Definition: PacketBuffer.cpp:53
Cola2Session.h
sick::cola2::Cola2Session::m_async_tcp_client_ptr
std::shared_ptr< sick::communication::AsyncTCPClient > m_async_tcp_client_ptr
Definition: Cola2Session.h:143
sick::cola2::Cola2Session::m_session_id
uint32_t m_session_id
Definition: Cola2Session.h:153
sick::cola2::Cola2Session::setSessionID
void setSessionID(const uint32_t &session_id)
Sets the current session ID.
Definition: Cola2Session.cpp:90
sick::cola2::Cola2Session::getNextRequestID
uint16_t getNextRequestID()
Returns the next request ID. The request ID is used to match the return packages of the sensor to the...
Definition: Cola2Session.cpp:178
sick::cola2::Cola2Session::removeCommand
bool removeCommand(const uint16_t &request_id)
Definition: Cola2Session.cpp:165
sick::cola2::Cola2Session::getSessionID
uint32_t getSessionID() const
Returns the current session ID.
Definition: Cola2Session.cpp:85
sick::datastructure::PacketBuffer
A packetbuffer for the raw data from the sensor.
Definition: PacketBuffer.h:61
sick::cola2::Cola2Session::m_pending_commands_map
std::map< uint16_t, CommandPtr > m_pending_commands_map
Definition: Cola2Session.h:148
sick::cola2::Cola2Session::addCommand
bool addCommand(const uint16_t &request_id, const CommandPtr &command)
Definition: Cola2Session.cpp:141
sick::cola2::Cola2Session::startProcessingAndRemovePendingCommandAfterwards
bool startProcessingAndRemovePendingCommandAfterwards(const sick::datastructure::PacketBuffer &packet)
Definition: Cola2Session.cpp:128
sick::cola2::Cola2Session::m_packet_merger_ptr
std::shared_ptr< sick::data_processing::TCPPacketMerger > m_packet_merger_ptr
Definition: Cola2Session.h:145
sick::cola2::Cola2Session::checkIfPacketIsCompleteAndOtherwiseListenForMorePackets
bool checkIfPacketIsCompleteAndOtherwiseListenForMorePackets()
Definition: Cola2Session.cpp:117
sick::cola2::Cola2Session::processPacket
void processPacket(const sick::datastructure::PacketBuffer &packet)
Definition: Cola2Session.cpp:95


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Fri Jun 21 2024 02:40:51