-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageDecoding.c
69 lines (64 loc) · 2.38 KB
/
MessageDecoding.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
/**
* @file MessageDecoding.c
* @author paul
* @date 01.11.22
* @brief Implementation of decoding logic for protobuf messages send via a serial interface.
* @ingroup Messages
*/
#include "MessageDecoding.h"
#include "MessageDefs.h"
void message_decoding_init(message_decoding_data_t *decoding_data, uint8_t message_id) {
decoding_data->decodingState = DECODING_INITIAL;
decoding_data->len = 0;
decoding_data->id = message_id;
}
bool message_decoding_decode(message_decoding_data_t *decoding_data, uint8_t data, const pb_msgdesc_t *fields,
void *message) {
switch (decoding_data->decodingState) {
case DECODING_INITIAL:
if (data == END_BYTE) {
decoding_data->decodingState = DECODING_END_FOUND;
}
break;
case DECODING_END_FOUND:
if (data == START_BYTE) {
decoding_data->decodingState = DECODING_FIRST_FOUND;
if (decoding_data->len > 0) {
decoding_data->len -= 1;
pb_istream_t istream = pb_istream_from_buffer(decoding_data->buf, decoding_data->len);
pb_decode(&istream, fields, message);
decoding_data->len = 0;
return true;
}
} else {
if (decoding_data->len == 0) {
decoding_data->decodingState = DECODING_INITIAL;
} else {
decoding_data->decodingState = DECODING_IN_DATA;
decoding_data->buf[decoding_data->len] = data;
decoding_data->len += 1;
}
}
break;
case DECODING_FIRST_FOUND:
if (data == decoding_data->id) {
decoding_data->decodingState = DECODING_IN_DATA;
} else {
decoding_data->decodingState = DECODING_IN_WRONG_DATA;
}
break;
case DECODING_IN_DATA:
if (data == END_BYTE) {
decoding_data->decodingState = DECODING_END_FOUND;
}
decoding_data->buf[decoding_data->len] = data;
decoding_data->len += 1;
break;
case DECODING_IN_WRONG_DATA:
if (data == END_BYTE) {
decoding_data->decodingState = DECODING_END_FOUND;
}
break;
}
return false;
}