ParseTCPPacket.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 
00024 // -- END LICENSE BLOCK ------------------------------------------------
00025 
00026 //----------------------------------------------------------------------
00033 //----------------------------------------------------------------------
00034 
00035 #include <sick_safetyscanners/data_processing/ParseTCPPacket.h>
00036 
00037 #include <sick_safetyscanners/cola2/Command.h>
00038 
00039 namespace sick {
00040 namespace data_processing {
00041 
00042 ParseTCPPacket::ParseTCPPacket()
00043 {
00044   m_reader_ptr = std::make_shared<sick::data_processing::ReadWriteHelper>();
00045 }
00046 
00047 uint32_t ParseTCPPacket::getExpectedPacketLength(const datastructure::PacketBuffer& buffer)
00048 {
00049   const uint8_t* data_ptr(buffer.getBuffer().data());
00050   return readLength(data_ptr) + 8; // for STX and Length which is not included in length datafield
00051 }
00052 
00053 uint16_t ParseTCPPacket::getRequestID(const datastructure::PacketBuffer& buffer) const
00054 {
00055   const uint8_t* data_ptr(buffer.getBuffer().data());
00056   return readRequestID(data_ptr);
00057 }
00058 
00059 
00060 bool ParseTCPPacket::parseTCPSequence(const datastructure::PacketBuffer& buffer,
00061                                       sick::cola2::Command& command) const
00062 {
00063   setCommandValuesFromPacket(buffer, command);
00064 
00065   std::vector<uint8_t> byteVector;
00066   readData(buffer, byteVector);
00067   command.setDataVector(byteVector);
00068 
00069   return true;
00070 }
00071 
00072 void ParseTCPPacket::setCommandValuesFromPacket(const sick::datastructure::PacketBuffer& buffer,
00073                                                 sick::cola2::Command& command) const
00074 {
00075   const uint8_t* data_ptr(buffer.getBuffer().data());
00076   command.setSessionID(readSessionID(data_ptr));
00077   command.setRequestID(readRequestID(data_ptr));
00078   command.setCommandType(readCommandType(data_ptr));
00079   command.setCommandMode(readCommandMode(data_ptr));
00080 }
00081 
00082 uint32_t ParseTCPPacket::readSTx(const uint8_t*& data_ptr) const
00083 {
00084   return m_reader_ptr->readuint32_tBigEndian(data_ptr, 0);
00085 }
00086 
00087 uint32_t ParseTCPPacket::readLength(const uint8_t*& data_ptr) const
00088 {
00089   return m_reader_ptr->readuint32_tBigEndian(data_ptr, 4);
00090 }
00091 
00092 uint8_t ParseTCPPacket::readHubCntr(const uint8_t*& data_ptr) const
00093 {
00094   return m_reader_ptr->readuint8_tBigEndian(data_ptr, 8);
00095 }
00096 uint8_t ParseTCPPacket::readNoC(const uint8_t*& data_ptr) const
00097 {
00098   return m_reader_ptr->readuint8_tBigEndian(data_ptr, 9);
00099 }
00100 uint32_t ParseTCPPacket::readSessionID(const uint8_t*& data_ptr) const
00101 {
00102   return m_reader_ptr->readuint32_tBigEndian(data_ptr, 10);
00103 }
00104 
00105 uint16_t ParseTCPPacket::readRequestID(const uint8_t*& data_ptr) const
00106 {
00107   return m_reader_ptr->readuint16_tBigEndian(data_ptr, 14);
00108 }
00109 
00110 uint8_t ParseTCPPacket::readCommandType(const uint8_t*& data_ptr) const
00111 {
00112   return m_reader_ptr->readuint8_tBigEndian(data_ptr, 16);
00113 }
00114 uint8_t ParseTCPPacket::readCommandMode(const uint8_t*& data_ptr) const
00115 {
00116   return m_reader_ptr->readuint8_tBigEndian(data_ptr, 17);
00117 }
00118 uint16_t ParseTCPPacket::readErrorCode(const uint8_t*& data_ptr) const
00119 {
00120   return m_reader_ptr->readuint16_tBigEndian(data_ptr, 18);
00121 }
00122 
00123 void ParseTCPPacket::readData(const datastructure::PacketBuffer& buffer,
00124                               std::vector<uint8_t>& byteVector) const
00125 {
00126   if (buffer.getLength() < 20)
00127   {
00128     return;
00129   }
00130   else
00131   {
00132     byteVector.insert(byteVector.end(), buffer.getBuffer().begin() + 20, buffer.getBuffer().end());
00133   }
00134 }
00135 
00136 } // namespace data_processing
00137 } // namespace sick


sick_safetyscanners
Author(s): Lennart Puck
autogenerated on Tue May 7 2019 03:27:36