Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
syslink.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
29 #include "syslink.h"
30 
31 const char *syslink_stx = "\xbc\xcf";
32 
34 {
35  state->state = SYSLINK_STATE_START;
36  state->index = 0;
37 }
38 
40 {
41 
42  switch (state->state) {
44  if (c == syslink_stx[state->index]) {
45  state->index++;
46  } else {
47  state->index = 0;
48  }
49 
50  if (syslink_stx[state->index] == '\x00') {
51  state->state = SYSLINK_STATE_TYPE;
52  }
53 
54  break;
55 
56  case SYSLINK_STATE_TYPE:
57  msg->type = c;
58  state->state = SYSLINK_STATE_LENGTH;
59  break;
60 
62  msg->length = c;
63 
64  if (c > SYSLINK_MAX_DATA_LEN) { // Too long
65  state->state = SYSLINK_STATE_START;
66  } else {
67  state->state = c > 0 ? SYSLINK_STATE_DATA : SYSLINK_STATE_CKSUM;
68  }
69 
70  state->index = 0;
71  break;
72 
73  case SYSLINK_STATE_DATA:
74  msg->data[state->index++] = c;
75 
76  if (state->index >= msg->length) {
77  state->state = SYSLINK_STATE_CKSUM;
78  state->index = 0;
80  }
81 
82  break;
83 
85  if (c != msg->cksum[state->index]) {
86  // fail checksum
87  state->state = SYSLINK_STATE_START;
88  state->index = 0;
89  break;
90  }
91 
92  state->index++;
93 
94  if (state->index >= (int)sizeof(msg->cksum)) {
95  state->state = SYSLINK_STATE_START;
96  state->index = 0;
97  return true; // message is correct, return true
98  }
99 
100  break;
101  }
102 
103  return false;
104 
105 }
106 
107 /*
108  Computes Fletcher 8bit checksum per RFC1146
109 A := A + D[i]
110 B := B + A
111 */
113 {
114  uint8_t a = 0, b = 0;
115  uint8_t *Di = (uint8_t *)msg, *end = Di + (2 + msg->length) * sizeof(uint8_t);
116 
117  while (Di < end) {
118  a = a + *Di;
119  b = b + a;
120  ++Di;
121  }
122 
123  msg->cksum[0] = a;
124  msg->cksum[1] = b;
125 }
struct State state
Definition: state.c:36
uint8_t msg[10]
Buffer used for general comunication over SPI (out buffer)
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
float b
Definition: wedgebug.c:202