-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes.h
72 lines (62 loc) · 2.01 KB
/
frames.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
71
72
#pragma once
// Library cframes provides useful functions to deal with data frames.
#include <stdbool.h>
#include <stdint.h>
// Creates a new frame.
//
// You must allocate memory yourself.
// The frame starts with 2-byte header and contains (frame_len - 7) bytes data.
// Create also calculates the checksum using frames_calculate_checksum.
// Data length must not overflow 249 (256 - 7).
//
// Example frame: LD\x02\x00+OK#C
extern void frames_create(uint8_t* frame,
uint8_t frame_len,
uint8_t* header,
uint8_t* data,
uint8_t frame_id);
// Places frame's header into header parameter. It is always 2 bytes.
extern void frames_header(uint8_t* frame, uint8_t* header);
// Places frame's data into data parameter.
extern void frames_read_data(uint8_t* frame, uint8_t frame_len, uint8_t* data);
// Get id of frame
extern uint8_t frames_get_id(uint8_t* frame);
// Returns the length of frame's data in bytes.
extern uint8_t frames_len_data(uint8_t* frame);
// Checks whether the frame is valid (i.e of correct format).
//
// The frame must:
//
// - start with 2 byte uppercase ASCII header
//
// - at 3rd position (2nd index): have a length byte that is equal to the length
// of data
//
// - at 4th position (3rd index): have a plus sign ("+")
//
// - at penultimate position: have a hash sign ("#")
//
// - its checksum must be correct
extern bool frames_verify(uint8_t* frame, uint8_t frame_len);
// Returns length of data part of a frame.
// A valid frame always has:
//
// - 2 byte header
//
// - 1 length byte
//
// - 1 frame id byte
//
// - 1 plus sign ("+")
//
// - 1 hash sign ("#")
//
// - 1 checksum byte
//
// That gives 7 non-data bytes.
extern uint8_t frames_data_len(uint8_t frame_len);
// Calculates the simple CRC checksum of frame.
//
// It takes all frame's bytes into account, except the last byte, because
// the last byte is the checksum itself.
extern uint8_t frames_calculate_checksum(uint8_t* frame, uint8_t frame_len);