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
ivy_transport.c
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 
30 #include "std.h"
34 
35 #include <stdio.h>
36 #include <Ivy/ivy.h>
37 
39 
40 static void put_bytes(struct ivy_transport *trans, struct link_device *dev __attribute__((unused)),
41  enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
42  uint8_t len, const void *bytes)
43 {
44  const uint8_t *b = (const uint8_t *) bytes;
45 
46  // Start delimiter "quote" for char arrays (strings)
47  if (format == DL_FORMAT_ARRAY && type == DL_TYPE_CHAR) {
48  trans->ivy_p += sprintf(trans->ivy_p, "\"");
49  }
50 
51  int i = 0;
52  while (i < len) {
53  // print data with correct type
54  switch (type) {
55  case DL_TYPE_CHAR:
56  trans->ivy_p += sprintf(trans->ivy_p, "%c", (char)(*((char *)(b + i))));
57  i++;
58  break;
59  case DL_TYPE_UINT8:
60  trans->ivy_p += sprintf(trans->ivy_p, "%u", b[i]);
61  i++;
62  break;
63  case DL_TYPE_UINT16:
64  trans->ivy_p += sprintf(trans->ivy_p, "%u", (uint16_t)(*((uint16_t *)(b + i))));
65  i += 2;
66  break;
67  case DL_TYPE_UINT32:
68  case DL_TYPE_TIMESTAMP:
69  trans->ivy_p += sprintf(trans->ivy_p, "%u", (uint32_t)(*((uint32_t *)(b + i))));
70  i += 4;
71  break;
72  case DL_TYPE_UINT64:
73 #if __WORDSIZE == 64
74  trans->ivy_p += sprintf(trans->ivy_p, "%lu", (uint64_t)(*((uint64_t *)(b + i))));
75 #else
76  trans->ivy_p += sprintf(trans->ivy_p, "%llu", (uint64_t)(*((uint64_t *)(b + i))));
77 #endif
78  i += 8;
79  break;
80  case DL_TYPE_INT8:
81  trans->ivy_p += sprintf(trans->ivy_p, "%d", (int8_t)(*((int8_t *)(b + i))));
82  i++;
83  break;
84  case DL_TYPE_INT16:
85  trans->ivy_p += sprintf(trans->ivy_p, "%d", (int16_t)(*((int16_t *)(b + i))));
86  i += 2;
87  break;
88  case DL_TYPE_INT32:
89  trans->ivy_p += sprintf(trans->ivy_p, "%d", (int32_t)(*((int32_t *)(b + i))));
90  i += 4;
91  break;
92  case DL_TYPE_INT64:
93 #if __WORDSIZE == 64
94  trans->ivy_p += sprintf(trans->ivy_p, "%ld", (uint64_t)(*((uint64_t *)(b + i))));
95 #else
96  trans->ivy_p += sprintf(trans->ivy_p, "%lld", (uint64_t)(*((uint64_t *)(b + i))));
97 #endif
98  i += 8;
99  break;
100  case DL_TYPE_FLOAT:
101  trans->ivy_p += sprintf(trans->ivy_p, "%f", (float)(*((float *)(b + i))));
102  i += 4;
103  break;
104  case DL_TYPE_DOUBLE:
105  trans->ivy_p += sprintf(trans->ivy_p, "%f", (double)(*((double *)(b + i))));
106  i += 8;
107  break;
109  default:
110  // Don't print array length but increment index
111  i++;
112  break;
113  }
114  // Coma delimiter for array, no delimiter for char array (string), space otherwise
115  if (format == DL_FORMAT_ARRAY) {
116  if (type != DL_TYPE_CHAR) {
117  trans->ivy_p += sprintf(trans->ivy_p, ",");
118  }
119  } else {
120  trans->ivy_p += sprintf(trans->ivy_p, " ");
121  }
122  }
123 
124  // space end delimiter for arrays, additionally un-quote char arrays (strings)
125  if (format == DL_FORMAT_ARRAY) {
126  if (type == DL_TYPE_CHAR) {
127  trans->ivy_p += sprintf(trans->ivy_p, "\" ");
128  } else {
129  trans->ivy_p += sprintf(trans->ivy_p, " ");
130  }
131  }
132 }
133 
134 static void put_named_byte(struct ivy_transport *trans, struct link_device *dev __attribute__((unused)),
135  enum TransportDataType type __attribute__((unused)), enum TransportDataFormat format __attribute__((unused)),
136  uint8_t byte __attribute__((unused)), const char *name __attribute__((unused)))
137 {
138  trans->ivy_p += sprintf(trans->ivy_p, "%s ", name);
139 }
140 
141 static uint8_t size_of(struct ivy_transport *trans __attribute__((unused)), uint8_t len)
142 {
143  return len;
144 }
145 
146 static void start_message(struct ivy_transport *trans, struct link_device *dev __attribute__((unused)),
147  uint8_t payload_len __attribute__((unused)))
148 {
149  trans->ivy_p = trans->ivy_buf;
150 }
151 
152 static void end_message(struct ivy_transport *trans, struct link_device *dev)
153 {
154  *(--trans->ivy_p) = '\0';
155  if (trans->ivy_dl_enabled) {
156  IvySendMsg("%s", trans->ivy_buf);
157  dev->nb_msgs++;
158  }
159 }
160 
161 static void overrun(struct ivy_transport *trans __attribute__((unused)),
162  struct link_device *dev)
163 {
164  dev->nb_ovrn++;
165 }
166 
167 static void count_bytes(struct ivy_transport *trans __attribute__((unused)),
168  struct link_device *dev, uint8_t bytes)
169 {
170  dev->nb_bytes += bytes;
171 }
172 
173 static int check_available_space(struct ivy_transport *trans __attribute__((unused)),
174  struct link_device *dev __attribute__((unused)), uint8_t bytes __attribute__((unused)))
175 {
176  return TRUE;
177 }
178 
179 static int check_free_space(struct ivy_transport *p __attribute__((unused)), uint8_t len __attribute__((unused))) { return TRUE; }
180 static void transmit(struct ivy_transport *p __attribute__((unused)), uint8_t byte __attribute__((unused))) {}
181 static void send_message(struct ivy_transport *p __attribute__((unused))) {}
182 static int null_function(struct ivy_transport *p __attribute__((unused))) { return 0; }
183 
185 {
188 
197  ivy_tp.trans_tx.impl = (void *)(&ivy_tp);
203  ivy_tp.device.periph = (void *)(&ivy_tp);
204 }
unsigned short uint16_t
Definition: types.h:16
static void count_bytes(struct ivy_transport *trans, struct link_device *dev, uint8_t bytes)
TransportDataFormat
Data format (scalar or array)
Definition: transport.h:66
static void put_named_byte(struct ivy_transport *trans, struct link_device *dev, enum TransportDataType type, enum TransportDataFormat format, uint8_t byte, const char *name)
end_message_t end_message
transport trailer
Definition: transport.h:95
static int null_function(struct ivy_transport *p)
generic transport header
struct link_device device
Definition: ivy_transport.h:44
uint8_t(* size_of_t)(void *, uint8_t)
Function pointers definition.
Definition: transport.h:76
void(* end_message_t)(void *, struct link_device *)
Definition: transport.h:83
struct ivy_transport ivy_tp
Definition: ivy_transport.c:38
size_of_t size_of
get size of payload with transport header and trailer
Definition: transport.h:90
overrun_t overrun
overrun
Definition: transport.h:96
void(* overrun_t)(void *, struct link_device *)
Definition: transport.h:84
TransportDataType
Data type.
Definition: transport.h:48
void ivy_transport_init(void)
count_bytes_t count_bytes
count bytes to send
Definition: transport.h:97
struct transport_tx trans_tx
Definition: ivy_transport.h:42
static void send_message(struct ivy_transport *p)
#define TRUE
Definition: std.h:4
unsigned long long uint64_t
Definition: types.h:20
char ivy_buf[256]
Definition: ivy_transport.h:38
static int check_available_space(struct ivy_transport *trans, struct link_device *dev, uint8_t bytes)
unsigned long uint32_t
Definition: types.h:18
void(* start_message_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:82
static uint8_t size_of(struct ivy_transport *trans, uint8_t len)
signed short int16_t
Definition: types.h:17
void(* put_named_byte_t)(void *, struct link_device *, enum TransportDataType, enum TransportDataFormat, uint8_t, const char *)
Definition: transport.h:80
start_message_t start_message
transport header
Definition: transport.h:94
put_named_byte_t put_named_byte
send a single byte or its name
Definition: transport.h:93
static void end_message(struct ivy_transport *trans, struct link_device *dev)
signed long int32_t
Definition: types.h:19
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
int(* check_available_space_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:77
void(* put_bytes_t)(void *, struct link_device *, enum TransportDataType, enum TransportDataFormat, uint8_t, const void *)
Definition: transport.h:78
unsigned char uint8_t
Definition: types.h:14
void * impl
pointer to parent implementation
Definition: transport.h:98
static void start_message(struct ivy_transport *trans, struct link_device *dev, uint8_t payload_len)
static void overrun(struct ivy_transport *trans, struct link_device *dev)
check_available_space_t check_available_space
check if transmit buffer is not full
Definition: transport.h:91
static float p[2][2]
put_bytes_t put_bytes
send bytes
Definition: transport.h:92
static void put_bytes(struct ivy_transport *trans, struct link_device *dev, enum TransportDataType type, enum TransportDataFormat format, uint8_t len, const void *bytes)
Definition: ivy_transport.c:40
signed char int8_t
Definition: types.h:15
void(* count_bytes_t)(void *, struct link_device *, uint8_t)
Definition: transport.h:85
static void transmit(struct ivy_transport *p, uint8_t byte)
Building Paparazzi frames over IVY.
static int check_free_space(struct ivy_transport *p, uint8_t len)