Main Page
Classes
Class List
Class Hierarchy
Class Members
All
a
b
c
d
e
f
g
i
l
o
p
q
r
s
t
~
Functions
Variables
a
b
c
d
e
f
o
p
r
s
t
Files
File List
File Members
All
Functions
Enumerations
Enumerator
Macros
include
LMS1xx
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
#define logWarn CONSOLE_BRIDGE_logWarn
36
#define logError CONSOLE_BRIDGE_logError
37
#define logDebug CONSOLE_BRIDGE_logDebug
38
#define logInform CONSOLE_BRIDGE_logInform
39
40
class
LMSBuffer
41
{
42
public
:
43
LMSBuffer
() :
total_length_
(0),
end_of_first_message_
(0)
44
{
45
}
46
47
void
readFrom
(
int
fd)
48
{
49
int
ret = read(fd,
buffer_
+
total_length_
,
sizeof
(
buffer_
) -
total_length_
);
50
51
if
(ret > 0)
52
{
53
total_length_
+= ret;
54
logDebug
(
"Read %d bytes from fd, total length is %d."
, ret,
total_length_
);
55
}
56
else
57
{
58
59
logWarn
(
"Buffer read() returned error."
);
60
}
61
}
62
63
char
*
getNextBuffer
()
64
{
65
if
(
total_length_
== 0)
66
{
67
// Buffer is empty, no scan data present.
68
logDebug
(
"Empty buffer, nothing to return."
);
69
return
NULL;
70
}
71
72
// The objective is to have a message starting at the start of the buffer, so if that's not
73
// the case, then we look for a start-of-message character and shift the buffer back, discarding
74
// any characters in the middle.
75
char
* start_of_message = (
char
*)memchr(
buffer_
,
LMS_STX
,
total_length_
);
76
if
(start_of_message == NULL)
77
{
78
// None found, buffer reset.
79
logWarn
(
"No STX found, dropping %d bytes from buffer."
,
total_length_
);
80
total_length_
= 0;
81
}
82
else
if
(
buffer_
!= start_of_message)
83
{
84
// Start of message found, ahead of the start of buffer. Therefore shift the buffer back.
85
logWarn
(
"Shifting buffer, dropping %d bytes, %d bytes remain."
,
86
(start_of_message -
buffer_
),
total_length_
- (start_of_message -
buffer_
));
87
shiftBuffer
(start_of_message);
88
}
89
90
// Now look for the end of message character.
91
end_of_first_message_
= (
char
*)memchr(
buffer_
,
LMS_ETX
,
total_length_
);
92
if
(
end_of_first_message_
== NULL)
93
{
94
// No end of message found, therefore no message to parse and return.
95
logDebug
(
"No ETX found, nothing to return."
);
96
return
NULL;
97
}
98
99
// Null-terminate buffer.
100
*
end_of_first_message_
= 0;
101
return
buffer_
;
102
}
103
104
void
popLastBuffer
()
105
{
106
if
(
end_of_first_message_
)
107
{
108
shiftBuffer
(
end_of_first_message_
+ 1);
109
end_of_first_message_
= NULL;
110
}
111
}
112
113
private
:
114
void
shiftBuffer
(
char
* new_start)
115
{
116
// Shift back anything remaining in the buffer.
117
uint16_t remaining_length =
total_length_
- (new_start -
buffer_
);
118
119
if
(remaining_length > 0)
120
{
121
memmove(
buffer_
, new_start, remaining_length);
122
}
123
total_length_
= remaining_length;
124
}
125
126
char
buffer_
[
LMS_BUFFER_SIZE
];
127
uint16_t
total_length_
;
128
129
char
*
end_of_first_message_
;
130
};
131
132
#endif // LMS1XX_LMS_BUFFER_H_
LMS_STX
#define LMS_STX
Definition:
lms_buffer.h:32
LMSBuffer::shiftBuffer
void shiftBuffer(char *new_start)
Definition:
lms_buffer.h:114
LMS_BUFFER_SIZE
#define LMS_BUFFER_SIZE
Definition:
lms_buffer.h:31
logDebug
#define logDebug
Definition:
lms_buffer.h:37
LMSBuffer::LMSBuffer
LMSBuffer()
Definition:
lms_buffer.h:43
LMS_ETX
#define LMS_ETX
Definition:
lms_buffer.h:33
logWarn
#define logWarn
Definition:
lms_buffer.h:35
LMSBuffer::end_of_first_message_
char * end_of_first_message_
Definition:
lms_buffer.h:129
LMSBuffer
Definition:
lms_buffer.h:40
LMSBuffer::total_length_
uint16_t total_length_
Definition:
lms_buffer.h:127
LMSBuffer::popLastBuffer
void popLastBuffer()
Definition:
lms_buffer.h:104
LMSBuffer::getNextBuffer
char * getNextBuffer()
Definition:
lms_buffer.h:63
LMSBuffer::buffer_
char buffer_[LMS_BUFFER_SIZE]
Definition:
lms_buffer.h:126
LMSBuffer::readFrom
void readFrom(int fd)
Definition:
lms_buffer.h:47
lms1xx
Author(s): Konrad Banachowicz
autogenerated on Wed Mar 2 2022 00:28:01