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