Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pprz_transport.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Pascal Brisset, Antoine Drouin
3  * Copyright (C) 2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
41 #ifndef PPRZ_TRANSPORT_H
42 #define PPRZ_TRANSPORT_H
43 
44 #include <inttypes.h>
45 #include "std.h"
46 #ifndef PPRZ_DATALINK_EXPORT
49 #else /* PPRZ_DATALINK_EXPORT defined */
50 #include "datalink.h"
51 #include "transport.h"
52 #endif
53 
54 /* PPRZ Transport
55  */
56 
57 #define STX 0x99
58 
59 // PPRZ parsing state machine
60 #define UNINIT 0
61 #define GOT_STX 1
62 #define GOT_LENGTH 2
63 #define GOT_PAYLOAD 3
64 #define GOT_CRC1 4
65 
67  // generic reception interface
69  // specific pprz transport_rx variables
73  // generic transmission interface
75  // specific pprz transport_tx variables
77 };
78 
79 extern struct pprz_transport pprz_tp;
80 
81 // Init function
82 extern void pprz_transport_init(struct pprz_transport *t);
83 
84 static inline void parse_pprz(struct pprz_transport *t, uint8_t c)
85 {
86  switch (t->status) {
87  case UNINIT:
88  if (c == STX) {
89  t->status++;
90  }
91  break;
92  case GOT_STX:
93  if (t->trans_rx.msg_received) {
94  t->trans_rx.ovrn++;
95  goto error;
96  }
97  t->trans_rx.payload_len = c - 4; /* Counting STX, LENGTH and CRC1 and CRC2 */
98  t->ck_a_rx = t->ck_b_rx = c;
99  t->status++;
100  t->payload_idx = 0;
101  break;
102  case GOT_LENGTH:
103  t->trans_rx.payload[t->payload_idx] = c;
104  t->ck_a_rx += c; t->ck_b_rx += t->ck_a_rx;
105  t->payload_idx++;
106  if (t->payload_idx == t->trans_rx.payload_len) {
107  t->status++;
108  }
109  break;
110  case GOT_PAYLOAD:
111  if (c != t->ck_a_rx) {
112  goto error;
113  }
114  t->status++;
115  break;
116  case GOT_CRC1:
117  if (c != t->ck_b_rx) {
118  goto error;
119  }
121  goto restart;
122  default:
123  goto error;
124  }
125  return;
126 error:
127  t->trans_rx.error++;
128 restart:
129  t->status = UNINIT;
130  return;
131 }
132 
133 static inline void pprz_parse_payload(struct pprz_transport *t)
134 {
135  uint8_t i;
136  for (i = 0; i < t->trans_rx.payload_len; i++) {
137  dl_buffer[i] = t->trans_rx.payload[i];
138  }
140 }
141 
142 
143 #define PprzCheckAndParse(_dev, _trans) pprz_check_and_parse(&(_dev).device, &(_trans))
144 
145 static inline void pprz_check_and_parse(struct link_device *dev, struct pprz_transport *trans)
146 {
147  if (dev->char_available(dev->periph)) {
148  while (dev->char_available(dev->periph) && !trans->trans_rx.msg_received) {
149  parse_pprz(trans, dev->get_byte(dev->periph));
150  }
151  if (trans->trans_rx.msg_received) {
152  pprz_parse_payload(trans);
153  trans->trans_rx.msg_received = FALSE;
154  }
155  }
156 }
157 
158 #endif /* PPRZ_TRANSPORT_H */
159 
volatile bool_t msg_received
message received flag
Definition: transport.h:42
Generic reception transport header.
Definition: transport.h:39
Generic transmission transport header.
Definition: transport.h:89
generic transport header
#define GOT_PAYLOAD
uint8_t payload[TRANSPORT_PAYLOAD_LEN]
payload buffer
Definition: transport.h:40
static void parse_pprz(struct pprz_transport *t, uint8_t c)
volatile uint8_t payload_len
payload buffer length
Definition: transport.h:41
#define GOT_STX
uint8_t payload_idx
#define FALSE
Definition: std.h:5
bool_t dl_msg_available
Definition: main_demo5.c:61
static void pprz_check_and_parse(struct link_device *dev, struct pprz_transport *trans)
#define TRUE
Definition: std.h:4
struct transport_tx trans_tx
#define UNINIT
void pprz_transport_init(struct pprz_transport *t)
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
unsigned char uint8_t
Definition: types.h:14
#define STX
#define GOT_CRC1
struct transport_rx trans_rx
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:64
#define GOT_LENGTH
static void pprz_parse_payload(struct pprz_transport *t)
uint8_t error
overrun and error flags
Definition: transport.h:43
uint8_t ovrn
Definition: transport.h:43
struct pprz_transport pprz_tp