SickMessage.hh
Go to the documentation of this file.
1 
16 #ifndef SICK_MESSAGE
17 #define SICK_MESSAGE
18 
19 /* Dependencies */
20 #include <arpa/inet.h>
21 #include <iomanip>
22 #include <iostream>
23 
24 /* Associate the namespace */
25 namespace SickToolbox {
26 
31  template < unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
32  class SickMessage {
33 
34  public:
35 
37  static const unsigned int MESSAGE_HEADER_LENGTH = MSG_HEADER_LENGTH;
38  static const unsigned int MESSAGE_TRAILER_LENGTH = MSG_TRAILER_LENGTH;
39  static const unsigned int MESSAGE_PAYLOAD_MAX_LENGTH = MSG_PAYLOAD_MAX_LENGTH;
40  static const unsigned int MESSAGE_MAX_LENGTH = MESSAGE_HEADER_LENGTH + MESSAGE_PAYLOAD_MAX_LENGTH + MESSAGE_TRAILER_LENGTH;
41 
43  SickMessage( );
44 
46  void BuildMessage( const uint8_t * const payload_buffer, const unsigned int payload_length );
47 
49  virtual void ParseMessage( const uint8_t * const message_buffer ) = 0;
50 
52  void GetMessage( uint8_t * const message_buffer ) const;
53 
55  unsigned int GetMessageLength( ) const { return _message_length; }
56 
58  void GetPayload( uint8_t * const payload_buffer ) const;
59 
61  void GetPayloadAsCStr( char * const payload_str ) const;
62 
64  void GetPayloadSubregion( uint8_t * const payload_sub_buffer, const unsigned int start_idx,
65  const unsigned int stop_idx ) const;
66 
68  unsigned int GetPayloadLength( ) const { return _payload_length; }
69 
71  bool IsPopulated( ) const { return _populated; };
72 
74  virtual void Clear( );
75 
77  virtual void Print( ) const;
78 
80  virtual ~SickMessage( );
81 
82  protected:
83 
85  unsigned int _payload_length;
86 
88  unsigned int _message_length;
89 
92 
94  bool _populated;
95 
96  };
97 
98 
102  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
104 
110  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
111  void SickMessage< MSG_HEADER_LENGTH, MSG_PAYLOAD_MAX_LENGTH, MSG_TRAILER_LENGTH >::BuildMessage( const uint8_t * const payload_buffer, const unsigned int payload_length ) {
112 
113  /* Clear the object */
114  Clear();
115 
116  /* Assign the payload and message lengths */
117  _payload_length = payload_length;
119 
120  /* Copy the payload into the message buffer */
121  memcpy(&_message_buffer[MESSAGE_HEADER_LENGTH],payload_buffer,_payload_length);
122 
123  /* Mark the object container as being populated */
124  _populated = true;
125 
126  }
127 
132  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
134 
135  /* Clear the message container/object */
136  Clear();
137 
138  /* Mark the object as populated */
139  _populated = true;
140  }
141 
146  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
148  memcpy(message_buffer,_message_buffer,_message_length);
149  }
150 
155  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
157  memcpy(payload_buffer,&_message_buffer[MESSAGE_HEADER_LENGTH],_payload_length);
158  }
159 
161  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
163  memcpy(payload_buffer,&_message_buffer[MESSAGE_HEADER_LENGTH],_payload_length);
164  payload_buffer[_payload_length] = '\0';
165  }
166 
173  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
175  const unsigned int start_idx,
176  const unsigned int stop_idx ) const {
177  /* Extract the subregion */
178  memcpy(payload_sub_buffer,&_message_buffer[MESSAGE_HEADER_LENGTH+start_idx],stop_idx+1);
179  }
180 
184  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
186 
187  /* Reset the parent integer variables */
189 
190  /* Clear the message buffer */
192 
193  /* Set the flag indicating this message object/container is empty */
194  _populated = false;
195  }
196 
200  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
202 
203  std::cout << "Payload length: " << GetPayloadLength() << std::endl;
204  std::cout << "Message length: " << GetMessageLength() << std::endl;
205  std::cout << std::flush;
206 
207  std::cout << "Message (hex):" << std::endl;
208  std::cout.setf(std::ios::hex,std::ios::basefield);
209  for (unsigned int i = 0; i < _message_length; i++) {
210  std::cout << (int)_message_buffer[i] << " ";
211  }
212  std::cout << std::endl << std::flush;
213 
214  std::cout << "Message (ASCII):" << std::endl;
215  std::cout.setf(std::ios::dec,std::ios::basefield);
216  for (unsigned int i = 0; i < _message_length; i++) {
217  std::cout << _message_buffer[i] << " ";
218  }
219  std::cout << std::endl << std::flush;
220  }
221 
225  template< unsigned int MSG_HEADER_LENGTH, unsigned int MSG_PAYLOAD_MAX_LENGTH, unsigned int MSG_TRAILER_LENGTH >
227 
228 } /* namespace SickToolbox */
229 
230 #endif /* SICK_MESSAGE */
virtual void ParseMessage(const uint8_t *const message_buffer)=0
Parses a sequence of bytes into a Sick message.
Definition: SickMessage.hh:133
unsigned int GetPayloadLength() const
Definition: SickMessage.hh:68
virtual ~SickMessage()
A destructor.
Definition: SickMessage.hh:226
unsigned int _payload_length
Definition: SickMessage.hh:85
static const unsigned int MESSAGE_TRAILER_LENGTH
Definition: SickMessage.hh:38
unsigned int _message_length
Definition: SickMessage.hh:88
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
static const unsigned int MESSAGE_PAYLOAD_MAX_LENGTH
Definition: SickMessage.hh:39
SickMessage()
A default constructor.
Definition: SickMessage.hh:103
static const unsigned int MESSAGE_MAX_LENGTH
Definition: SickMessage.hh:40
uint8_t _message_buffer[MESSAGE_MAX_LENGTH]
Definition: SickMessage.hh:91
virtual void Print() const
Print data about this object.
Definition: SickMessage.hh:201
unsigned int GetMessageLength() const
Definition: SickMessage.hh:55
void GetPayload(uint8_t *const payload_buffer) const
Get the payload contents as a sequence of well-formed bytes.
Definition: SickMessage.hh:156
virtual void Clear()
Reset all internal fields and buffers.
Definition: SickMessage.hh:185
void GetPayloadSubregion(uint8_t *const payload_sub_buffer, const unsigned int start_idx, const unsigned int stop_idx) const
Get a specified sub-region of the payload buffer.
Definition: SickMessage.hh:174
void GetPayloadAsCStr(char *const payload_str) const
Definition: SickMessage.hh:162
bool IsPopulated() const
Definition: SickMessage.hh:71
Provides an abstract parent for all Sick messages.
Definition: SickMessage.hh:32
Encapsulates the Sick LIDAR Matlab/C++ toolbox.
Definition: SickLD.cc:44
void GetMessage(uint8_t *const message_buffer) const
Get the message as a sequence of well-formed bytes.
Definition: SickMessage.hh:147
static const unsigned int MESSAGE_HEADER_LENGTH
Definition: SickMessage.hh:37


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