Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
36 #ifndef PPRZ_TRANSPORT_H
37 #define PPRZ_TRANSPORT_H
38 
39 #include <inttypes.h>
40 #include "std.h"
43 
44 /* PPRZ Transport
45  * downlink macros
46  */
47 extern uint8_t ck_a, ck_b;
48 
49 #define STX 0x99
50 
52 #define PprzTransportSizeOf(_dev, _payload) (_payload+4)
53 
54 #define PprzTransportCheckFreeSpace(_dev, _x) TransportLink(_dev, CheckFreeSpace(_x))
55 #define PprzTransportPut1Byte(_dev, _x) TransportLink(_dev, Transmit(_x))
56 #define PprzTransportSendMessage(_dev) TransportLink(_dev, SendMessage())
57 
58 #define PprzTransportHeader(_dev, payload_len) { \
59  PprzTransportPut1Byte(_dev, STX); \
60  uint8_t msg_len = PprzTransportSizeOf(_dev, payload_len); \
61  PprzTransportPut1Byte(_dev, msg_len); \
62  ck_a = msg_len; ck_b = msg_len; \
63 }
64 
65 #define PprzTransportTrailer(_dev) { \
66  PprzTransportPut1Byte(_dev, ck_a); \
67  PprzTransportPut1Byte(_dev, ck_b); \
68  PprzTransportSendMessage(_dev); \
69 }
70 
71 #define PprzTransportPutUint8(_dev, _byte) { \
72  ck_a += _byte; \
73  ck_b += ck_a; \
74  PprzTransportPut1Byte(_dev, _byte); \
75  }
76 
77 #define PprzTransportPutNamedUint8(_dev, _name, _byte) PprzTransportPutUint8(_dev, _byte)
78 
79 #define PprzTransportPut1ByteByAddr(_dev, _byte) { \
80  uint8_t _x = *(_byte); \
81  PprzTransportPutUint8(_dev, _x); \
82  }
83 
84 #define PprzTransportPut2ByteByAddr(_dev, _byte) { \
85  PprzTransportPut1ByteByAddr(_dev, _byte); \
86  PprzTransportPut1ByteByAddr(_dev, (const uint8_t*)_byte+1); \
87  }
88 
89 #define PprzTransportPut4ByteByAddr(_dev, _byte) { \
90  PprzTransportPut2ByteByAddr(_dev, _byte); \
91  PprzTransportPut2ByteByAddr(_dev, (const uint8_t*)_byte+2); \
92  }
93 
94 #ifdef __IEEE_BIG_ENDIAN /* From machine/ieeefp.h */
95 #define PprzTransportPutDoubleByAddr(_dev, _byte) { \
96  PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
97  PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
98  }
99 #else
100 #define PprzTransportPutDoubleByAddr(_dev, _byte) { \
101  PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
102  PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
103  }
104 #endif
105 
106 
107 #define PprzTransportPutInt8ByAddr(_dev, _x) PprzTransportPut1ByteByAddr(_dev, _x)
108 #define PprzTransportPutUint8ByAddr(_dev, _x) PprzTransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
109 #define PprzTransportPutInt16ByAddr(_dev, _x) PprzTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
110 #define PprzTransportPutUint16ByAddr(_dev, _x) PprzTransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
111 #define PprzTransportPutInt32ByAddr(_dev, _x) PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
112 #define PprzTransportPutUint32ByAddr(_dev, _x) PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
113 #define PprzTransportPutFloatByAddr(_dev, _x) PprzTransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
114 
115 #define PprzTransportPutArray(_dev, _put, _n, _x) { \
116  uint8_t _i; \
117  PprzTransportPutUint8(_dev, _n); \
118  for(_i = 0; _i < _n; _i++) { \
119  _put(_dev, &_x[_i]); \
120  } \
121 }
122 
123 #define PprzTransportPutFloatArray(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutFloatByAddr, _n, _x)
124 #define PprzTransportPutDoubleArray(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutDoubleByAddr, _n, _x)
125 
126 #define PprzTransportPutInt16Array(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutInt16ByAddr, _n, _x)
127 #define PprzTransportPutUint16Array(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutUint16ByAddr, _n, _x)
128 
129 #define PprzTransportPutInt32Array(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutInt32ByAddr, _n, _x)
130 #define PprzTransportPutUint32Array(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutUint32ByAddr, _n, _x)
131 
132 #define PprzTransportPutUint8Array(_dev, _n, _x) PprzTransportPutArray(_dev, PprzTransportPutUint8ByAddr, _n, _x)
133 
134 
137 // PPRZ parsing state machine
138 #define UNINIT 0
139 #define GOT_STX 1
140 #define GOT_LENGTH 2
141 #define GOT_PAYLOAD 3
142 #define GOT_CRC1 4
143 
145  // generic interface
146  struct transport trans;
147  // specific pprz transport variables
151 };
152 
153 extern struct pprz_transport pprz_tp;
154 
155 static inline void parse_pprz(struct pprz_transport * t, uint8_t c ) {
156  switch (t->status) {
157  case UNINIT:
158  if (c == STX)
159  t->status++;
160  break;
161  case GOT_STX:
162  if (t->trans.msg_received) {
163  t->trans.ovrn++;
164  goto error;
165  }
166  t->trans.payload_len = c-4; /* Counting STX, LENGTH and CRC1 and CRC2 */
167  t->ck_a = t->ck_b = c;
168  t->status++;
169  t->payload_idx = 0;
170  break;
171  case GOT_LENGTH:
172  t->trans.payload[t->payload_idx] = c;
173  t->ck_a += c; t->ck_b += t->ck_a;
174  t->payload_idx++;
175  if (t->payload_idx == t->trans.payload_len)
176  t->status++;
177  break;
178  case GOT_PAYLOAD:
179  if (c != t->ck_a)
180  goto error;
181  t->status++;
182  break;
183  case GOT_CRC1:
184  if (c != t->ck_b)
185  goto error;
186  t->trans.msg_received = TRUE;
187  goto restart;
188  default:
189  goto error;
190  }
191  return;
192  error:
193  t->trans.error++;
194  restart:
195  t->status = UNINIT;
196  return;
197 }
198 
199 static inline void pprz_parse_payload(struct pprz_transport * t) {
200  uint8_t i;
201  for(i = 0; i < t->trans.payload_len; i++)
202  dl_buffer[i] = t->trans.payload[i];
204 }
205 
206 
207 #define PprzBuffer(_dev) TransportLink(_dev,ChAvailable())
208 #define ReadPprzBuffer(_dev,_trans) { while (TransportLink(_dev,ChAvailable())&&!(_trans.trans.msg_received)) parse_pprz(&(_trans),TransportLink(_dev,Getch())); }
209 #define PprzCheckAndParse(_dev,_trans) { \
210  if (PprzBuffer(_dev)) { \
211  ReadPprzBuffer(_dev,_trans); \
212  if (_trans.trans.msg_received) { \
213  pprz_parse_payload(&(_trans)); \
214  _trans.trans.msg_received = FALSE; \
215  } \
216  } \
217 }
218 
219 
220 #endif /* PPRZ_TRANSPORT_H */
221 
generic transport header
#define GOT_PAYLOAD
static void parse_pprz(struct pprz_transport *t, uint8_t c)
Generic transport header.
Definition: transport.h:39
struct transport trans
#define GOT_STX
bool_t dl_msg_available
Definition: main_demo5.c:56
uint8_t ck_a
volatile uint8_t payload_len
Definition: transport.h:43
uint8_t payload[TRANSPORT_PAYLOAD_LEN]
Definition: transport.h:41
#define UNINIT
Receiving pprz messages.
#define TRUE
Definition: imu_chimu.h:144
unsigned char uint8_t
Definition: types.h:14
#define STX
#define GOT_CRC1
volatile bool_t msg_received
Definition: transport.h:45
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:59
uint8_t ck_b
uint8_t ovrn
Definition: transport.h:47
#define GOT_LENGTH
static void pprz_parse_payload(struct pprz_transport *t)
uint8_t error
Definition: transport.h:47
static struct point c
Definition: discsurvey.c:39
struct pprz_transport pprz_tp