PX4 Firmware
PX4 Autopilot Software http://px4.io
test_hott_telemetry.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_hott_telemetry.c
36  * Tests the Graupner HoTT telemetry support.
37  */
38 
39 #include <px4_platform_common/time.h>
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/defines.h>
42 #include <px4_platform_common/log.h>
43 #include <px4_platform_common/posix.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <poll.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <termios.h>
53 #include <unistd.h>
54 
55 #include "tests_main.h"
56 
57 
58 static int open_uart(const char *device)
59 {
60  /* baud rate */
61  int speed = B19200;
62 
63  /* open uart */
64  int uart = open(device, O_RDWR | O_NOCTTY);
65 
66  if (uart < 0) {
67  PX4_ERR("FAIL: Error opening port");
68  return ERROR;
69  }
70 
71  /* Try to set baud rate */
72  struct termios uart_config;
73 
74  /* Fill the struct for the new configuration */
75  tcgetattr(uart, &uart_config);
76 
77  /* Clear ONLCR flag (which appends a CR for every LF) */
78  uart_config.c_oflag &= ~ONLCR;
79 
80  /* Set baud rate */
81  if (cfsetispeed(&uart_config, speed) < 0 || cfsetospeed(&uart_config, speed) < 0) {
82  PX4_ERR("FAIL: Error setting baudrate / termios config for cfsetispeed, cfsetospeed");
83  return ERROR;
84  }
85 
86  if (tcsetattr(uart, TCSANOW, &uart_config) < 0) {
87  PX4_ERR("FAIL: Error setting baudrate / termios config for tcsetattr");
88  return ERROR;
89  }
90 
91  return uart;
92 }
93 
94 int test_hott_telemetry(int argc, char *argv[])
95 {
96  PX4_INFO("HoTT Telemetry Test Requirements:");
97  PX4_INFO("- Radio on and Electric Air. Mod on (telemetry -> sensor select).");
98  PX4_INFO("- Receiver telemetry port must be in telemetry mode.");
99  PX4_INFO("- Connect telemetry wire to /dev/ttyS1 (USART2).");
100  PX4_INFO("Testing...");
101 
102  const char device[] = "/dev/ttyS1";
103  int fd = open_uart(device);
104 
105  if (fd < 0) {
106  close(fd);
107  return ERROR;
108  }
109 
110 #ifdef TIOCSSINGLEWIRE
111  /* Activate single wire mode */
112  ioctl(fd, TIOCSSINGLEWIRE, SER_SINGLEWIRE_ENABLED);
113 #endif
114 
115  char send = 'a';
116  write(fd, &send, 1);
117 
118  /* Since TX and RX are now connected we should be able to read in what we wrote */
119  const int timeout = 1000;
120  struct pollfd fds[] = { { .fd = fd, .events = POLLIN } };
121 
122  if (poll(fds, 1, timeout) == 0) {
123  PX4_ERR("FAIL: Could not read sent data.");
124  return 1;
125  }
126 
127  char receive;
128  read(fd, &receive, 1);
129  PX4_INFO("PASS: Single wire enabled. Sent %x and received %x", send, receive);
130 
131 
132  /* Attempt to read HoTT poll messages from the HoTT receiver */
133  int received_count = 0;
134  int valid_count = 0;
135  const int max_polls = 5;
136  uint8_t byte;
137 
138  for (; received_count < 5; received_count++) {
139  if (poll(fds, 1, timeout) == 0) {
140  PX4_ERR("FAIL: Could not read sent data. Is your HoTT receiver plugged in on %s?", device);
141  return 1;
142 
143  } else {
144  read(fd, &byte, 1);
145 
146  if (byte == 0x80) {
147  valid_count++;
148  }
149 
150  /* Read the device ID being polled */
151  read(fd, &byte, 1);
152  }
153  }
154 
155  if (received_count > 0 && valid_count > 0) {
156  if (received_count == max_polls && valid_count == max_polls) {
157  PX4_INFO("PASS: Received %d out of %d valid byte pairs from the HoTT receiver device.", received_count, max_polls);
158 
159  } else {
160  PX4_WARN("WARN: Received %d out of %d byte pairs of which %d were valid from the HoTT receiver device.", received_count,
161  max_polls, valid_count);
162  }
163 
164  } else {
165  /* Let's work out what went wrong */
166  if (received_count == 0) {
167  PX4_ERR("FAIL: Could not read any polls from HoTT receiver device.");
168  return 1;
169  }
170 
171  if (valid_count == 0) {
172  PX4_ERR("FAIL: Received unexpected values from the HoTT receiver device.");
173  return 1;
174  }
175  }
176 
177 
178  /* Attempt to send a HoTT response messages */
179  uint8_t response[] = {0x7c, 0x8e, 0x00, 0xe0, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, \
180  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
181  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, \
182  0x19, 0x00, 0x00, 0x00, 0x30, 0x75, 0x78, 0x00, 0x00, 0x00, \
183  0x00, 0x00, 0x00, 0x7d, 0x12
184  };
185 
186  px4_usleep(5000);
187 
188  for (unsigned int i = 0; i < sizeof(response); i++) {
189  write(fd, &response[i], 1);
190  px4_usleep(1000);
191  }
192 
193  PX4_INFO("PASS: Response sent to the HoTT receiver device. Voltage should now show 2.5V.");
194 
195 
196 #ifdef TIOCSSINGLEWIRE
197  /* Disable single wire */
198  ioctl(fd, TIOCSSINGLEWIRE, ~SER_SINGLEWIRE_ENABLED);
199 #endif
200 
201  write(fd, &send, 1);
202 
203  /* We should timeout as there will be nothing to read (TX and RX no longer connected) */
204  if (poll(fds, 1, timeout) == 0) {
205  PX4_ERR("FAIL: timeout expected.");
206  return 1;
207  }
208 
209  PX4_INFO("PASS: Single wire disabled.");
210 
211  close(fd);
212  return 0;
213 }
void * send(void *data)
static int open_uart(const char *device)
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
static void read(bootloader_app_shared_t *pshared)
Tests declaration file.
int fd
Definition: dataman.cpp:146
static void write(bootloader_app_shared_t *pshared)
int test_hott_telemetry(int argc, char *argv[])