blob: f06bebb6884c4fe649f66353a3ef19df63eb0732 (
plain) (
blame)
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
|
#ifndef FRAMER_H
#define FRAMER_H
#include <inttypes.h>
#define MAX_PACKET_SIZE 64
#define MAX_FRAME_SIZE MAX_PACKET_SIZE + 3
class Framer {
private:
enum LinkState {
WAITING,
RECEIVING,
ESCAPING
};
static const uint8_t ESCAPE = 0x02;
static const uint8_t START = 0x03;
static const uint8_t STOP = 0x10;
LinkState state; //current state
uint8_t frameBuffer[MAX_FRAME_SIZE]; //data of current frame
uint8_t staged; //previous byte read, not defined when position == -1
int16_t position; //position of next byte to be read into frame buffer, can also be -1 if no byte has yet been staged
uint8_t checksum;
inline void reset(); //reset current frame
inline void accept(uint8_t byte); //reads a data byte (not a command byte) and takes an appropraiet action by modifying internal state
inline static bool isCommandByte(uint8_t byte) {return (byte == START || byte == STOP || byte == ESCAPE);}
protected:
Framer();
void put(uint8_t byte);
void send(uint16_t length, const uint8_t * const data);
/* Called when a byte is to be sent.
* @param byte the byte to be sent */
virtual void onSendByte(uint8_t byte) = 0;
/* Called when a valid frame is received. */
virtual void onFrame(uint16_t length, uint8_t *data) = 0;
};
#endif /* FRAMER_H */
|