lms_buffer.h
Go to the documentation of this file.
1 /*
2  * lms_buffer.h
3  *
4  * Author: Mike Purvis <mpurvis@clearpathrobotics.com>
5  ***************************************************************************
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the Free Software *
18  * Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307 USA *
20  * *
21  ***************************************************************************/
22 
23 #ifndef LMS1XX_LMS_BUFFER_H_
24 #define LMS1XX_LMS_BUFFER_H_
25 
26 #include "console_bridge/console.h"
27 #include <stdint.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #define LMS_BUFFER_SIZE 50000
32 #define LMS_STX 0x02
33 #define LMS_ETX 0x03
34 
35 class LMSBuffer
36 {
37 public:
39  {
40  }
41 
42  void readFrom(int fd)
43  {
44  int ret = read(fd, buffer_ + total_length_, sizeof(buffer_) - total_length_);
45 
46  if (ret > 0)
47  {
48  total_length_ += ret;
49  logDebug("Read %d bytes from fd, total length is %d.", ret, total_length_);
50  }
51  else
52  {
53 
54  logWarn("Buffer read() returned error.");
55  }
56  }
57 
58  char* getNextBuffer()
59  {
60  if (total_length_ == 0)
61  {
62  // Buffer is empty, no scan data present.
63  logDebug("Empty buffer, nothing to return.");
64  return NULL;
65  }
66 
67  // The objective is to have a message starting at the start of the buffer, so if that's not
68  // the case, then we look for a start-of-message character and shift the buffer back, discarding
69  // any characters in the middle.
70  char* start_of_message = (char*)memchr(buffer_, LMS_STX, total_length_);
71  if (start_of_message == NULL)
72  {
73  // None found, buffer reset.
74  logWarn("No STX found, dropping %d bytes from buffer.", total_length_);
75  total_length_ = 0;
76  }
77  else if (buffer_ != start_of_message)
78  {
79  // Start of message found, ahead of the start of buffer. Therefore shift the buffer back.
80  logWarn("Shifting buffer, dropping %d bytes, %d bytes remain.",
81  (start_of_message - buffer_), total_length_ - (start_of_message - buffer_));
82  shiftBuffer(start_of_message);
83  }
84 
85  // Now look for the end of message character.
87  if (end_of_first_message_ == NULL)
88  {
89  // No end of message found, therefore no message to parse and return.
90  logDebug("No ETX found, nothing to return.");
91  return NULL;
92  }
93 
94  // Null-terminate buffer.
96  return buffer_;
97  }
98 
100  {
102  {
104  end_of_first_message_ = NULL;
105  }
106  }
107 
108 private:
109  void shiftBuffer(char* new_start)
110  {
111  // Shift back anything remaining in the buffer.
112  uint16_t remaining_length = total_length_ - (new_start - buffer_);
113 
114  if (remaining_length > 0)
115  {
116  memmove(buffer_, new_start, remaining_length);
117  }
118  total_length_ = remaining_length;
119  }
120 
122  uint16_t total_length_;
123 
125 };
126 
127 #endif // LMS1XX_LMS_BUFFER_H_
char * getNextBuffer()
Definition: lms_buffer.h:58
void readFrom(int fd)
Definition: lms_buffer.h:42
#define LMS_ETX
Definition: lms_buffer.h:33
uint16_t total_length_
Definition: lms_buffer.h:122
char * end_of_first_message_
Definition: lms_buffer.h:124
void shiftBuffer(char *new_start)
Definition: lms_buffer.h:109
#define LMS_STX
Definition: lms_buffer.h:32
#define LMS_BUFFER_SIZE
Definition: lms_buffer.h:31
char buffer_[LMS_BUFFER_SIZE]
Definition: lms_buffer.h:121
void popLastBuffer()
Definition: lms_buffer.h:99


lms1xx
Author(s): Konrad Banachowicz
autogenerated on Wed Jan 22 2020 03:36:59