SickLDMessage.cc
Go to the documentation of this file.
1 
16 /* Auto-generated header */
18 
19 /* Implementation dependencies */
20 #include <iomanip>
21 #include <iostream>
22 #include <arpa/inet.h>
23 
25 #include <sicktoolbox/SickLDUtility.hh> // for byye-order conversions where necessary
26 
27 /* Associate the namespace */
28 namespace SickToolbox {
29 
35 
36  /* Initialize the object */
37  Clear();
38  }
39 
45  SickLDMessage::SickLDMessage( const uint8_t * const payload_buffer, const unsigned int payload_length ) :
47 
48  /* Build the message object (implicit initialization) */
49  BuildMessage(payload_buffer,payload_length);
50  }
51 
56  SickLDMessage::SickLDMessage( const uint8_t * const message_buffer ) :
58 
59  /* Parse the message into the container (implicit initialization) */
60  ParseMessage(message_buffer);
61  }
62 
68  void SickLDMessage::BuildMessage( const uint8_t * const payload_buffer, const unsigned int payload_length ) {
69 
70  /* Call the parent method
71  * NOTE: The parent method resets the object and assigns _message_length, _payload_length,
72  * _populated and copies the payload into the message buffer at the correct position
73  */
75  ::BuildMessage(payload_buffer,payload_length);
76 
77  /*
78  * Set the message header!
79  */
80  _message_buffer[0] = 0x02; // STX
81  _message_buffer[1] = 'U'; // User
82  _message_buffer[2] = 'S'; // Service
83  _message_buffer[3] = 'P'; // Protocol
84 
85  /* Include the payload length in the header */
86  uint32_t payload_length_32 = host_to_sick_ld_byte_order((uint32_t)_payload_length);
87  memcpy(&_message_buffer[4],&payload_length_32,4);
88 
89  /*
90  * Set the message trailer (just a checksum)!
91  */
92  _message_buffer[_message_length-1] = _computeXOR(&_message_buffer[8],(uint32_t)_payload_length);
93  }
94 
99  void SickLDMessage::ParseMessage( const uint8_t * const message_buffer ) {
100 
101  /* Call the parent method
102  * NOTE: This method resets the object and assigns _populated as true
103  */
105  ::ParseMessage(message_buffer);
106 
107  /* Extract the payload length */
108  uint32_t payload_length_32 = 0;
109  memcpy(&payload_length_32,&message_buffer[4],4);
110  _payload_length = (unsigned int)sick_ld_to_host_byte_order(payload_length_32);
111 
112  /* Compute the total message length */
114 
115  /* Copy the given packet into the buffer */
116  memcpy(_message_buffer,message_buffer,_message_length);
117  }
118 
122  void SickLDMessage::Print( ) const {
123 
124  std::cout.setf(std::ios::hex,std::ios::basefield);
125  std::cout << "Checksum: " << (unsigned int) GetChecksum() << std::endl;
126  std::cout << "Service code: " << (unsigned int) GetServiceCode() << std::endl;
127  std::cout << "Service subcode: " << (unsigned int) GetServiceSubcode() << std::endl;
128  std::cout << std::flush;
129 
130  /* Call parent's print function */
132  }
133 
139  uint8_t SickLDMessage::_computeXOR( const uint8_t * const data, const uint32_t length ) {
140 
141  /* Compute the XOR by summing all of the bytes */
142  uint8_t checksum = 0;
143  for (uint32_t i = 0; i < length; i++) {
144  checksum ^= data[i]; // NOTE: this is equivalent to simply summing all of the bytes
145  }
146 
147  /* done */
148  return checksum;
149  }
150 
152 
153 } /* namespace SickToolbox */
virtual void ParseMessage(const uint8_t *const message_buffer)=0
Parses a sequence of bytes into a Sick message.
Definition: SickMessage.hh:133
#define SICK_LD_MSG_HEADER_LEN
Sick LD message header length in bytes.
void ParseMessage(const uint8_t *const message_buffer)
Parses a sequence of bytes into a SickLDMessage object.
void BuildMessage(const uint8_t *const payload_buffer, const unsigned int payload_length)
Constructs a Sick LD message given parameter values.
uint8_t GetServiceSubcode() const
void BuildMessage(const uint8_t *const payload_buffer, const unsigned int payload_length)
Constructs a Sick message given the parameter values.
Definition: SickMessage.hh:111
uint8_t GetServiceCode() const
SickLDMessage()
A default constructor.
#define SICK_LD_MSG_TRAILER_LEN
Sick LD length of the message trailer.
virtual void Print() const
Print data about this object.
Definition: SickMessage.hh:201
uint16_t sick_ld_to_host_byte_order(uint16_t value)
Converts Sick LD byte order (big-endian) to host byte order (little-endian)
void Print() const
Print the message contents.
uint8_t _computeXOR(const uint8_t *const data, const uint32_t length)
Compute the message checksum (single-byte XOR).
#define SICK_LD_MSG_PAYLOAD_MAX_LEN
Sick LD maximum payload length.
Provides an abstract parent for all Sick messages.
Definition: SickMessage.hh:32
Encapsulates the Sick LIDAR Matlab/C++ toolbox.
Definition: SickLD.cc:44
Defines simple utility functions for working with the Sick LD.
Defines the class SickLDMessage.
uint16_t host_to_sick_ld_byte_order(uint16_t value)
Converts host byte order (little-endian) to Sick LD byte order (big-endian)
uint8_t GetChecksum() const


sicktoolbox
Author(s): Jason Derenick , Thomas Miller
autogenerated on Tue Sep 10 2019 03:37:34