-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.h
70 lines (53 loc) · 1.19 KB
/
frame.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef FRAME_H
#define FRAME_H
#include <iostream>
#include <string>
#include "hexdump.hpp"
#include "crc.h"
class DeFramer {
public:
virtual bool findframe(const char c) {
return false;
}
};
#define BSB_STARTOFFRAME 0xdc
class DeFramerBSB : public DeFramer {
std::string buffer;
public:
bool findframe(const char c) {
// FIXME - Config issue?
buffer+=c^0xff;
if (buffer.at(0) != (char) BSB_STARTOFFRAME) {
buffer.erase(0,1);
return false;
}
if (!complete()) {
return false;
}
uint16_t crc=CRC::poly1021((const uint8_t *) buffer.c_str(), framelength());
if (crc != 0) {
std::cout << "CRC16 error - expected 0 - got " << crc << " dropping" << std::endl;
hex::dump(buffer.c_str(), framelength(), std::cout);
buffer.erase(0,framelength());
return false;
}
return true;
}
std::string frame(void ) {
std::string frame=buffer.substr(0,framelength());
buffer.erase(0, framelength());
return frame;
}
private:
int framelength() {
return buffer.at(3);
}
bool complete() {
if (buffer.length() < 4)
return false;
if (buffer.length() >= framelength())
return true;
return false;
}
};
#endif