-
Notifications
You must be signed in to change notification settings - Fork 55
/
pprz_transport.c
203 lines (185 loc) · 5.82 KB
/
pprz_transport.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (C) 2006 Pascal Brisset, Antoine Drouin
* Copyright (C) 2014-2017 Gautier Hattenberger <[email protected]>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
/**
* @file pprzlink/pprz_transport.c
*
* Building and parsing Paparazzi frames.
*
* Pprz frame:
*
* |STX|length|... payload=(length-4) bytes ...|Checksum A|Checksum B|
*
* where checksum is computed over length and payload:
* @code
* ck_A = ck_B = length
* for each byte b in payload
* ck_A += b;
* ck_b += ck_A;
* @endcode
*/
#include <inttypes.h>
#include "pprzlink/pprz_transport.h"
// PPRZ parsing state machine
#define UNINIT 0
#define GOT_STX 1
#define GOT_LENGTH 2
#define GOT_PAYLOAD 3
#define GOT_CRC1 4
static struct pprz_transport * get_pprz_trans(struct pprzlink_msg *msg)
{
return (struct pprz_transport *)(msg->trans->impl);
}
static void accumulate_checksum(struct pprz_transport *trans, const uint8_t byte)
{
trans->ck_a_tx += byte;
trans->ck_b_tx += trans->ck_a_tx;
}
static void put_bytes(struct pprzlink_msg *msg, long fd,
enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
const void *bytes, uint16_t len)
{
const uint8_t *b = (const uint8_t *) bytes;
int i;
for (i = 0; i < len; i++) {
accumulate_checksum(get_pprz_trans(msg), b[i]);
}
msg->dev->put_buffer(msg->dev->periph, fd, b, len);
}
static void put_named_byte(struct pprzlink_msg *msg, long fd,
enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
uint8_t byte, const char *name __attribute__((unused)))
{
accumulate_checksum(get_pprz_trans(msg), byte);
msg->dev->put_byte(msg->dev->periph, fd, byte);
}
static uint8_t size_of(struct pprzlink_msg *msg __attribute__((unused)), uint8_t len)
{
// message length: payload + protocol overhead (STX + len + ck_a + ck_b = 4)
return len + 4;
}
static void start_message(struct pprzlink_msg *msg, long fd, uint8_t payload_len)
{
msg->dev->put_byte(msg->dev->periph, fd, PPRZ_STX);
const uint8_t msg_len = size_of(msg, payload_len);
msg->dev->put_byte(msg->dev->periph, fd, msg_len);
get_pprz_trans(msg)->ck_a_tx = msg_len;
get_pprz_trans(msg)->ck_b_tx = msg_len;
}
static void end_message(struct pprzlink_msg *msg, long fd)
{
msg->dev->put_byte(msg->dev->periph, fd, get_pprz_trans(msg)->ck_a_tx);
msg->dev->put_byte(msg->dev->periph, fd, get_pprz_trans(msg)->ck_b_tx);
msg->dev->send_message(msg->dev->periph, fd);
}
static void overrun(struct pprzlink_msg *msg)
{
msg->dev->nb_ovrn++;
}
static void count_bytes(struct pprzlink_msg *msg, uint8_t bytes)
{
msg->dev->nb_bytes += bytes;
}
static int check_available_space(struct pprzlink_msg *msg, long *fd, uint16_t bytes)
{
return msg->dev->check_free_space(msg->dev->periph, fd, bytes);
}
// Init pprz transport structure
void pprz_transport_init(struct pprz_transport *t)
{
t->status = UNINIT;
t->trans_rx.msg_received = false;
t->trans_tx.size_of = (size_of_t) size_of;
t->trans_tx.check_available_space = (check_available_space_t) check_available_space;
t->trans_tx.put_bytes = (put_bytes_t) put_bytes;
t->trans_tx.put_named_byte = (put_named_byte_t) put_named_byte;
t->trans_tx.start_message = (start_message_t) start_message;
t->trans_tx.end_message = (end_message_t) end_message;
t->trans_tx.overrun = (overrun_t) overrun;
t->trans_tx.count_bytes = (count_bytes_t) count_bytes;
t->trans_tx.impl = (void *)(t);
}
// Parsing function
void parse_pprz(struct pprz_transport *t, uint8_t c)
{
switch (t->status) {
case UNINIT:
if (c == PPRZ_STX) {
t->status++;
}
break;
case GOT_STX:
if (t->trans_rx.msg_received) {
t->trans_rx.ovrn++;
goto error;
}
t->trans_rx.payload_len = c - 4; /* Counting STX, LENGTH and CRC1 and CRC2 */
t->ck_a_rx = t->ck_b_rx = c;
t->status++;
t->payload_idx = 0;
break;
case GOT_LENGTH:
t->trans_rx.payload[t->payload_idx] = c;
t->ck_a_rx += c; t->ck_b_rx += t->ck_a_rx;
t->payload_idx++;
if (t->payload_idx == t->trans_rx.payload_len) {
t->status++;
}
break;
case GOT_PAYLOAD:
if (c != t->ck_a_rx) {
goto error;
}
t->status++;
break;
case GOT_CRC1:
if (c != t->ck_b_rx) {
goto error;
}
t->trans_rx.msg_received = true;
goto restart;
default:
goto error;
}
return;
error:
t->trans_rx.error++;
restart:
t->status = UNINIT;
return;
}
/** Parsing a frame data and copy the payload to the datalink buffer */
void pprz_check_and_parse(struct link_device *dev, struct pprz_transport *trans, uint8_t *buf, bool *msg_available)
{
uint8_t i;
if (dev->char_available(dev->periph)) {
while (dev->char_available(dev->periph) && !trans->trans_rx.msg_received) {
parse_pprz(trans, dev->get_byte(dev->periph));
}
if (trans->trans_rx.msg_received) {
for (i = 0; i < trans->trans_rx.payload_len; i++) {
buf[i] = trans->trans_rx.payload[i];
}
*msg_available = true;
trans->trans_rx.msg_received = false;
}
}
}