-
Notifications
You must be signed in to change notification settings - Fork 2
/
Packet.c
73 lines (70 loc) · 1.63 KB
/
Packet.c
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
71
72
73
#include "Packet.h"
#include "Command.h"
#include "crc8.h"
#define BUF_SIZE 32
uint8_t opcode = 0;
uint8_t serialbuf[BUF_SIZE + 1] = {0};
uint8_t buffer_current = 0;
uint8_t buffer_length = 0;
CommandAction_t cmd_state = OP_READY;
void EasyCon_serial_task(uint8_t byte)
{
switch(cmd_state)
{
case OP_READY:
opcode = byte;
if(!cmd_checkop(opcode)) {
cmd_senderrE(ERR_OP);
goto error;
}
cmd_state = DATA_LEN_RECV;
memset(serialbuf, 0, BUF_SIZE);
break;
case DATA_LEN_RECV:
if(byte >= BUF_SIZE) {
cmd_senderrE(ERR_LEN);
goto error;
}
buffer_length = byte;
// if data length is 0 then skipp DATA_RECV
if(buffer_length == 0) {
cmd_state = DATA_END;
} else {
cmd_state = DATA_RECV;
buffer_current = 0;
}
break;
case DATA_RECV:
serialbuf[buffer_current++] = byte;
if(buffer_current == buffer_length) {
cmd_state = DATA_END;
return;
}
break;
case DATA_END:
// crc check
uint8_t crcdata = byte;
int8_t crcsum = 0;
// default crc should be 0 if buffer_length is 0
if(buffer_length != 0) {
crcsum = crc8(serialbuf, buffer_length);
}
if(crcsum != crcdata) {
cmd_senderr(ERR_CRC, crcdata, crcsum);
goto error;
} else {
// all good, dispatch op commands
cmd_dispatch(opcode, serialbuf, buffer_length);
cmd_state = OP_READY;
return;
}
break;
default:
// unknown state error
cmd_senderrE(ERR_UKN);
error:
cmd_state = OP_READY;
break;
}
// loop
}