-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.cpp
More file actions
51 lines (38 loc) · 861 Bytes
/
filter.cpp
File metadata and controls
51 lines (38 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "stdafx.h"
static char line[256];
static nil parse_line( char ch )
{
}
static u08 msg[256] = {0};
static u08 *pos = msg, checksum = 0;
static u08 &len = msg[2];
static int parse_msg( u08 ch )
{
if( pos == msg && ch != 0xA5 ) return 0;
*pos = ch;
if( pos - msg >= (len + 3) ) {
const bool valid = (checksum == ch);
printf("msg: ");
for(u08 *s = msg; s <= pos; s++)
printf("%02X ", *s);
printf(valid ? "\n" : "(checksum: 0x%02X) invalid\n", checksum);
pos = msg;
len = 0;
checksum = 0;
return 1;
}
pos++;
checksum += ch;
return 1;
}
static char filtered[4096];
const char* filter_rx(const char *data, size_t len)
{
char *pos = filtered;
for(size_t i = 0; i < len; i++) {
const char ch = data[i];
if(!parse_msg((u08) ch))
parse_line(*pos++ = ch);
}
return *pos = '\0', filtered;
}