-
Notifications
You must be signed in to change notification settings - Fork 0
/
frames.c
89 lines (73 loc) · 2.07 KB
/
frames.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "frames.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Frame format:
// header - 2 bytes
// data length - 1 byte
// frame id - 1 byte
// '+' character - 1 byte
// data
// '#' character - 1 byte
// checksum - 1 byte
void frames_create(uint8_t* frame,
uint8_t frame_len,
uint8_t* header,
uint8_t* data,
uint8_t frame_id) {
// 7B because 2B header, 1B data length, 1B frame id, 1B "+", 1B "#" and 1B
// checksum
uint8_t data_len = frames_data_len(frame_len);
frame[0] = header[0];
frame[1] = header[1];
frame[2] = data_len;
frame[3] = frame_id;
frame[4] = '+';
memcpy(frame + 5, data, data_len);
frame[frame_len - 2] = '#';
frame[frame_len - 1] = frames_calculate_checksum(frame, frame_len);
}
void frames_header(uint8_t* frame, uint8_t* header) {
header[0] = frame[0];
header[1] = frame[1];
}
void frames_read_data(uint8_t* frame, uint8_t frame_len, uint8_t* data) {
memcpy(data, frame + 5, frames_data_len(frame_len));
}
uint8_t frames_get_id(uint8_t* frame) {
return frame[3];
}
uint8_t frames_len_data(uint8_t* frame) {
return frame[2];
}
bool frames_verify(uint8_t* frame, uint8_t frame_len) {
if ((frame[0] < 'A' || frame[0] > 'Z') &&
(frame[0] < '0' || frame[0] > '9')) {
return false;
}
if ((frame[1] < 'A' || frame[1] > 'Z') &&
(frame[1] < '0' || frame[1] > '9')) {
return false;
}
if (frame[2] != frames_data_len(frame_len)) {
return false;
}
if (frame[4] != '+') {
return false;
}
if (frame[frame_len - 2] != '#') {
return false;
}
uint8_t checksum = frames_calculate_checksum(frame, frame_len);
return checksum == frame[frame_len - 1];
}
uint8_t frames_data_len(uint8_t frame_len) {
return frame_len - 7;
}
uint8_t frames_calculate_checksum(uint8_t* frame, uint8_t frame_len) {
uint8_t checksum = frame[0];
for (uint8_t i = 1; i < frame_len - 1; i++) {
checksum ^= frame[i];
}
return checksum;
}