-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsersyncproto.h
76 lines (58 loc) · 2.16 KB
/
sersyncproto.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
73
74
75
76
/*
* sersyncproto.h
*
* Created on: Jan 13, 2015
* Author: Christian Holl
*/
#ifndef SERSYNCPROTO_H_
#define SERSYNCPROTO_H_
#include <inttypes.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
SERSYNCPROTO_STATE_HEADER,
__SERSYNCPROTO_CHKSUM_START,
SERSYNCPROTO_STATE_CMD,
SERSYNCPROTO_STATE_PAYLOAD,
__SERSYNCPROTO_CHKSUM_END,
SERSYNCPROTO_STATE_CHKSUM,
}sersyncproto_state_t;
typedef struct
{
uint8_t const _cmd_cnt; //!< Amount of cmds available
uint8_t const * const _cmd_array; //!< Array of possible commands
uint8_t const * const _payload_lengths; //!< payload length of each command in cmd_array
uint8_t const _header_len; //!< Length of header
uint8_t const * const _header; //!< Header definition
sersyncproto_state_t _state; //!< Current state of the protocol state machine
uint8_t _length; //!< Variable store the current length of a step (not for user)
uint8_t cmd; //!< When the parser function returns true this contains the cmd
uint8_t _chksum; //!< Variable stores the checksum
uint8_t *payload; //!< Points to the space where the parser should store payload, minsize==max payload size
}sersyncproto_data_t;
#define INIT_SERSYNCPROTO_DATA(DATA, CMD_ARRAY, PAYLOAD_LEN_ARRAY, HEADER_ARRAY, PAYLOAD_BUFFER)\
static sersyncproto_data_t DATA={sizeof(CMD_ARRAY), CMD_ARRAY, PAYLOAD_LEN_ARRAY, sizeof(HEADER_ARRAY), HEADER_ARRAY, SERSYNCPROTO_STATE_HEADER,0,0,0, (uint8_t*)PAYLOAD_BUFFER};
#define SERSYNCPROTO_GET_CUR_CMD(DATA)\
DATA._cmd_array[DATA.cmd]
/**
* Receiving function
* needs to be called for each byte.
* @param cur_byte The current byte
* @param data The data struct containing the definitions
*/
uint8_t sersyncproto_rec(sersyncproto_data_t* data, uint8_t cur_byte);
/**
* Sending function
* @param data The data struct containing the definitions
* @param cmd The command to be issued
* @param payload The payload of the current function
* @param sendbyte The USER_IMPLEMENTATION of a function which sends the byte over the serial interface
*/
uint8_t sersyncproto_send(sersyncproto_data_t* data, uint8_t cmd, uint8_t* payload, void (*sendbyte)(uint8_t) );
#ifdef __cplusplus
}
#endif
#endif /* SERSYNCPROTO_H_ */