2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
//
|
|
|
|
// Copyright (c) 2010 Michael Smith. All rights reserved.
|
|
|
|
//
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
2015-05-04 21:59:07 -03:00
|
|
|
#include <sys/types.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2015-05-01 06:52:07 -03:00
|
|
|
#include <sys/select.h>
|
2015-11-03 16:59:43 -04:00
|
|
|
#include <termios.h>
|
2017-11-22 18:21:50 -04:00
|
|
|
#include <sys/time.h>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include "UARTDriver.h"
|
2012-12-17 22:34:13 -04:00
|
|
|
#include "SITL_State.h"
|
2021-09-17 20:48:38 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2018-12-05 01:58:37 -04:00
|
|
|
#include <AP_HAL/utility/packetise.h>
|
2021-09-17 20:48:38 -03:00
|
|
|
#endif
|
2012-12-17 22:34:13 -04:00
|
|
|
|
2022-10-27 22:38:07 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2020-02-20 22:41:38 -04:00
|
|
|
#include <AP_Filesystem/AP_Filesystem.h>
|
2022-10-27 22:38:07 -03:00
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
bool UARTDriver::_console;
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
/* UARTDriver method implementations */
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
void UARTDriver::_begin(uint32_t baud, uint16_t rxSpace, uint16_t txSpace)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2023-12-10 23:55:21 -04:00
|
|
|
if (_portNumber >= ARRAY_SIZE(_sitlState->_serial_path)) {
|
|
|
|
AP_HAL::panic("port number out of range; you may need to extend _sitlState->_serial_path");
|
2018-06-29 04:51:14 -03:00
|
|
|
}
|
|
|
|
|
2023-12-10 23:55:21 -04:00
|
|
|
const char *path = _sitlState->_serial_path[_portNumber];
|
2015-11-03 16:59:43 -04:00
|
|
|
|
2019-01-24 20:26:38 -04:00
|
|
|
if (baud != 0) {
|
|
|
|
_uart_baudrate = baud;
|
|
|
|
}
|
2018-05-15 21:52:08 -03:00
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
if (strcmp(path, "GPS1") == 0) {
|
2012-12-12 18:04:27 -04:00
|
|
|
/* gps */
|
|
|
|
_connected = true;
|
2021-10-26 09:31:22 -03:00
|
|
|
_sim_serial_device = _sitlState->create_serial_sim("gps:1", "");
|
2015-11-03 16:59:43 -04:00
|
|
|
} else if (strcmp(path, "GPS2") == 0) {
|
|
|
|
/* 2nd gps */
|
2013-12-21 07:26:30 -04:00
|
|
|
_connected = true;
|
2021-10-26 09:31:22 -03:00
|
|
|
_sim_serial_device = _sitlState->create_serial_sim("gps:2", "");
|
2015-11-03 16:59:43 -04:00
|
|
|
} else {
|
|
|
|
/* parse type:args:flags string for path.
|
|
|
|
For example:
|
|
|
|
tcp:5760:wait // tcp listen on port 5760
|
|
|
|
tcp:0:wait // tcp listen on use base_port + 0
|
|
|
|
tcpclient:192.168.2.15:5762
|
2018-12-05 03:32:43 -04:00
|
|
|
udpclient:127.0.0.1
|
|
|
|
udpclient:127.0.0.1:14550
|
|
|
|
mcast:
|
|
|
|
mcast:239.255.145.50:14550
|
2015-11-03 16:59:43 -04:00
|
|
|
uart:/dev/ttyUSB0:57600
|
2017-08-10 07:23:11 -03:00
|
|
|
sim:ParticleSensor_SDS021:
|
2020-02-20 22:41:38 -04:00
|
|
|
file:/tmp/my-device-capture.BIN
|
2023-01-12 19:52:08 -04:00
|
|
|
logic_async_csv:/tmp/logic_async.csv:
|
2015-11-03 16:59:43 -04:00
|
|
|
*/
|
2016-10-30 02:24:21 -03:00
|
|
|
char *saveptr = nullptr;
|
2015-11-03 16:59:43 -04:00
|
|
|
char *s = strdup(path);
|
|
|
|
char *devtype = strtok_r(s, ":", &saveptr);
|
2016-10-30 02:24:21 -03:00
|
|
|
char *args1 = strtok_r(nullptr, ":", &saveptr);
|
|
|
|
char *args2 = strtok_r(nullptr, ":", &saveptr);
|
2022-10-03 13:52:29 -03:00
|
|
|
#if APM_BUILD_COPTER_OR_HELI || APM_BUILD_TYPE(APM_BUILD_ArduPlane)
|
2023-12-10 23:55:21 -04:00
|
|
|
if (_portNumber == 1 && AP::sitl()->adsb_plane_count >= 0) {
|
2021-11-09 02:23:34 -04:00
|
|
|
// this is ordinarily port 5762. The ADSB simulation assumed
|
|
|
|
// this port, so if enabled we assume we'll be doing ADSB...
|
|
|
|
// add sanity check here that we're doing mavlink on this port?
|
2023-12-10 23:55:21 -04:00
|
|
|
::printf("SIM-ADSB connection on SERIAL%u\n", _portNumber);
|
2021-11-09 02:23:34 -04:00
|
|
|
_connected = true;
|
|
|
|
_sim_serial_device = _sitlState->create_serial_sim("adsb", nullptr);
|
|
|
|
} else
|
|
|
|
#endif
|
2015-11-03 16:59:43 -04:00
|
|
|
if (strcmp(devtype, "tcp") == 0) {
|
|
|
|
uint16_t port = atoi(args1);
|
|
|
|
bool wait = (args2 && strcmp(args2, "wait") == 0);
|
|
|
|
_tcp_start_connection(port, wait);
|
|
|
|
} else if (strcmp(devtype, "tcpclient") == 0) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (args2 == nullptr) {
|
2015-11-19 23:11:17 -04:00
|
|
|
AP_HAL::panic("Invalid tcp client path: %s", path);
|
2015-11-03 16:59:43 -04:00
|
|
|
}
|
|
|
|
uint16_t port = atoi(args2);
|
|
|
|
_tcp_start_client(args1, port);
|
|
|
|
} else if (strcmp(devtype, "uart") == 0) {
|
2016-02-09 18:37:37 -04:00
|
|
|
uint32_t baudrate = args2? atoi(args2) : baud;
|
2015-11-03 16:59:43 -04:00
|
|
|
::printf("UART connection %s:%u\n", args1, baudrate);
|
2016-08-22 04:11:48 -03:00
|
|
|
_uart_path = strdup(args1);
|
|
|
|
_uart_baudrate = baudrate;
|
|
|
|
_uart_start_connection();
|
2017-08-10 07:23:11 -03:00
|
|
|
} else if (strcmp(devtype, "sim") == 0) {
|
|
|
|
if (!_connected) {
|
2023-12-10 23:55:21 -04:00
|
|
|
::printf("SIM connection %s:%s on SERIAL%u\n", args1, args2, _portNumber);
|
2017-08-10 07:23:11 -03:00
|
|
|
_connected = true;
|
2021-10-26 09:31:22 -03:00
|
|
|
_sim_serial_device = _sitlState->create_serial_sim(args1, args2);
|
2017-08-10 07:23:11 -03:00
|
|
|
}
|
2018-12-05 01:32:49 -04:00
|
|
|
} else if (strcmp(devtype, "udpclient") == 0) {
|
2018-12-05 03:32:43 -04:00
|
|
|
// udp client connection
|
2018-12-05 01:32:49 -04:00
|
|
|
const char *ip = args1;
|
|
|
|
uint16_t port = args2?atoi(args2):14550;
|
2018-12-05 03:32:43 -04:00
|
|
|
if (!_connected) {
|
|
|
|
::printf("UDP connection %s:%u\n", ip, port);
|
|
|
|
_udp_start_client(ip, port);
|
|
|
|
}
|
|
|
|
} else if (strcmp(devtype, "mcast") == 0) {
|
|
|
|
// udp multicast connection
|
|
|
|
const char *ip = args1 && *args1?args1:mcast_ip_default;
|
|
|
|
uint16_t port = args2?atoi(args2):mcast_port_default;
|
|
|
|
if (!_connected) {
|
|
|
|
::printf("UDP multicast connection %s:%u\n", ip, port);
|
|
|
|
_udp_start_multicast(ip, port);
|
|
|
|
}
|
2020-12-28 10:33:56 -04:00
|
|
|
} else if (strcmp(devtype,"none") == 0) {
|
|
|
|
// skipping port
|
|
|
|
::printf("Skipping port %s\n", args1);
|
2020-02-20 22:41:38 -04:00
|
|
|
} else if (strcmp(devtype, "file") == 0) {
|
|
|
|
if (_connected) {
|
|
|
|
AP::FS().close(_fd);
|
|
|
|
}
|
|
|
|
::printf("FILE connection %s\n", args1);
|
|
|
|
_fd = AP::FS().open(args1, O_RDONLY);
|
|
|
|
if (_fd == -1) {
|
|
|
|
AP_HAL::panic("Failed to open (%s): %m", args1);
|
|
|
|
}
|
|
|
|
_connected = true;
|
2023-01-12 19:52:08 -04:00
|
|
|
} else if (strcmp(devtype, "logic_async_csv") == 0) {
|
|
|
|
if (_connected) {
|
|
|
|
AP::FS().close(_fd);
|
|
|
|
}
|
|
|
|
::printf("logic_async_csv connection %s\n", args1);
|
|
|
|
_fd = AP::FS().open(args1, O_RDONLY);
|
|
|
|
if (_fd == -1) {
|
|
|
|
AP_HAL::panic("Failed to open (%s): %m", args1);
|
|
|
|
}
|
|
|
|
_connected = true;
|
|
|
|
logic_async_csv.active = true;
|
2015-11-03 16:59:43 -04:00
|
|
|
} else {
|
2015-11-19 23:11:17 -04:00
|
|
|
AP_HAL::panic("Invalid device path: %s", path);
|
2015-11-03 16:59:43 -04:00
|
|
|
}
|
|
|
|
free(s);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
|
2022-09-26 22:20:16 -03:00
|
|
|
if (_sim_serial_device != nullptr) {
|
|
|
|
_sim_serial_device->set_autopilot_baud(baud);
|
|
|
|
}
|
|
|
|
|
2018-06-20 06:01:08 -03:00
|
|
|
if (hal.console != this) { // don't clear USB buffers (allows early startup messages to escape)
|
|
|
|
_readbuffer.clear();
|
|
|
|
_writebuffer.clear();
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:17:32 -04:00
|
|
|
_set_nonblocking(_fd);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
void UARTDriver::_end()
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
uint32_t UARTDriver::_available(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
_check_connection();
|
|
|
|
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2016-01-10 02:17:32 -04:00
|
|
|
return _readbuffer.available();
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-08-02 10:42:50 -03:00
|
|
|
uint32_t UARTDriver::txspace(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2016-01-10 02:17:32 -04:00
|
|
|
_check_connection();
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _writebuffer.space();
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
ssize_t UARTDriver::_read(uint8_t *buffer, uint16_t count)
|
2021-07-08 04:10:23 -03:00
|
|
|
{
|
|
|
|
return _readbuffer.read(buffer, count);
|
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
bool UARTDriver::_discard_input(void)
|
2020-05-22 21:23:04 -03:00
|
|
|
{
|
2020-06-03 01:35:01 -03:00
|
|
|
_readbuffer.clear();
|
2020-05-22 21:23:04 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
void UARTDriver::_flush(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2021-02-19 09:46:38 -04:00
|
|
|
// flush the write buffer - but don't fail and don't
|
|
|
|
// infinitely-loop. This is not a good definition of "flush", but
|
|
|
|
// it was judged that we had to return from this function even if
|
|
|
|
// we hadn't actually done our job.
|
|
|
|
uint32_t start_ms = AP_HAL::millis();
|
|
|
|
while (AP_HAL::millis() - start_ms < 1000) {
|
|
|
|
if (_writebuffer.available() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_timer_tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that the outbound TCP queue is also empty...
|
|
|
|
start_ms = AP_HAL::millis();
|
|
|
|
while (AP_HAL::millis() - start_ms < 1000) {
|
|
|
|
if (((HALSITL::UARTDriver*)hal.serial(0))->get_system_outqueue_length() == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(1000);
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2023-06-26 06:52:07 -03:00
|
|
|
size_t UARTDriver::_write(const uint8_t *buffer, size_t size)
|
2013-10-02 04:36:54 -03:00
|
|
|
{
|
2023-07-13 03:14:27 -03:00
|
|
|
const auto _txspace = txspace();
|
|
|
|
if (_txspace < size) {
|
|
|
|
size = _txspace;
|
2013-10-02 04:36:54 -03:00
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
if (size <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-17 23:05:41 -03:00
|
|
|
|
2023-01-15 00:55:54 -04:00
|
|
|
/*
|
|
|
|
simulate byte loss at the link layer
|
|
|
|
*/
|
2023-07-13 04:13:51 -03:00
|
|
|
uint8_t lost_byte = 0;
|
2023-01-15 00:55:54 -04:00
|
|
|
#if !defined(HAL_BUILD_AP_PERIPH)
|
|
|
|
SITL::SIM *_sitl = AP::sitl();
|
|
|
|
|
|
|
|
if (_sitl && _sitl->uart_byte_loss_pct > 0) {
|
|
|
|
if (fabsf(rand_float()) < _sitl->uart_byte_loss_pct.get() * 0.01 * size) {
|
2023-07-13 04:13:51 -03:00
|
|
|
lost_byte = 1;
|
2023-01-15 00:55:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAL_BUILD_AP_PERIPH
|
2023-07-17 23:05:41 -03:00
|
|
|
|
|
|
|
|
|
|
|
const size_t ret = _writebuffer.write(buffer, size - lost_byte) + lost_byte;
|
|
|
|
if (_unbuffered_writes) {
|
|
|
|
handle_writing_from_writebuffer_to_device();
|
2017-11-22 18:21:50 -04:00
|
|
|
}
|
2023-07-17 23:05:41 -03:00
|
|
|
return ret;
|
2013-10-02 04:36:54 -03:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:17:32 -04:00
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
/*
|
|
|
|
start a TCP connection for the serial port. If wait_for_connection
|
|
|
|
is true then block until a client connects
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_tcp_start_connection(uint16_t port, bool wait_for_connection)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
int one=1;
|
|
|
|
int ret;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_connected) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-17 22:34:13 -04:00
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
_use_send_recv = true;
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_console) {
|
|
|
|
// hack for console access
|
|
|
|
_connected = true;
|
2015-11-03 16:59:43 -04:00
|
|
|
_use_send_recv = false;
|
2015-05-04 21:59:07 -03:00
|
|
|
_listen_fd = -1;
|
|
|
|
_fd = 1;
|
|
|
|
return;
|
|
|
|
}
|
2012-12-13 18:57:01 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_fd != -1) {
|
|
|
|
close(_fd);
|
|
|
|
}
|
2012-12-17 22:34:13 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_listen_fd == -1) {
|
2020-08-05 02:15:43 -03:00
|
|
|
memset(&_listen_sockaddr,0,sizeof(_listen_sockaddr));
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
2020-08-05 02:15:43 -03:00
|
|
|
_listen_sockaddr.sin_len = sizeof(_listen_sockaddr);
|
2012-12-12 18:04:27 -04:00
|
|
|
#endif
|
2015-11-03 16:59:43 -04:00
|
|
|
if (port > 1000) {
|
2020-08-05 02:15:43 -03:00
|
|
|
_listen_sockaddr.sin_port = htons(port);
|
2015-11-03 16:59:43 -04:00
|
|
|
} else {
|
2020-08-05 02:15:43 -03:00
|
|
|
_listen_sockaddr.sin_port = htons(_sitlState->base_port() + port);
|
2015-11-03 16:59:43 -04:00
|
|
|
}
|
2020-08-05 02:15:43 -03:00
|
|
|
_listen_sockaddr.sin_family = AF_INET;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2018-06-07 08:09:42 -03:00
|
|
|
_listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_listen_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-06-07 08:09:42 -03:00
|
|
|
ret = fcntl(_listen_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-17 22:34:13 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
/* we want to be able to re-use ports quickly */
|
2018-03-31 06:03:47 -03:00
|
|
|
if (setsockopt(_listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
|
|
|
|
fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2023-12-10 23:55:21 -04:00
|
|
|
fprintf(stderr, "bind port %u for SERIAL%u\n",
|
2020-08-05 02:15:43 -03:00
|
|
|
(unsigned)ntohs(_listen_sockaddr.sin_port),
|
2015-05-10 02:36:18 -03:00
|
|
|
(unsigned)_portNumber);
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2020-08-05 02:15:43 -03:00
|
|
|
ret = bind(_listen_fd, (struct sockaddr *)&_listen_sockaddr, sizeof(_listen_sockaddr));
|
2015-05-04 21:59:07 -03:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "bind failed on port %u - %s\n",
|
2020-08-05 02:15:43 -03:00
|
|
|
(unsigned)ntohs(_listen_sockaddr.sin_port),
|
2015-05-04 21:59:07 -03:00
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
ret = listen(_listen_fd, 5);
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "listen failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
2012-12-17 22:34:13 -04:00
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2023-12-10 23:55:21 -04:00
|
|
|
fprintf(stderr, "SERIAL%u on TCP port %u\n", _portNumber,
|
2020-08-05 02:15:43 -03:00
|
|
|
(unsigned)ntohs(_listen_sockaddr.sin_port));
|
2015-05-04 21:59:07 -03:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_for_connection) {
|
|
|
|
fprintf(stdout, "Waiting for connection ....\n");
|
|
|
|
fflush(stdout);
|
2016-10-30 02:24:21 -03:00
|
|
|
_fd = accept(_listen_fd, nullptr, nullptr);
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_fd == -1) {
|
|
|
|
fprintf(stderr, "accept() error - %s", strerror(errno));
|
|
|
|
exit(1);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2018-06-08 00:37:24 -03:00
|
|
|
fcntl(_fd, F_SETFD, FD_CLOEXEC);
|
2015-05-04 21:59:07 -03:00
|
|
|
_connected = true;
|
2020-08-05 02:15:43 -03:00
|
|
|
fprintf(stdout, "Connection on serial port %u\n", (unsigned)ntohs(_listen_sockaddr.sin_port));
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2015-05-10 02:36:18 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
start a TCP client connection for the serial port.
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_tcp_start_client(const char *address, uint16_t port)
|
2015-05-10 02:36:18 -03:00
|
|
|
{
|
|
|
|
int one=1;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (_connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
_use_send_recv = true;
|
|
|
|
|
2015-05-10 02:36:18 -03:00
|
|
|
if (_fd != -1) {
|
|
|
|
close(_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sockaddr,0,sizeof(sockaddr));
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
|
|
|
#endif
|
2016-05-30 03:42:28 -03:00
|
|
|
sockaddr.sin_port = htons(port);
|
2015-05-10 02:36:18 -03:00
|
|
|
sockaddr.sin_family = AF_INET;
|
2015-11-03 16:59:43 -04:00
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(address);
|
2015-05-10 02:36:18 -03:00
|
|
|
|
2018-06-07 08:09:42 -03:00
|
|
|
_fd = socket(AF_INET, SOCK_STREAM, 0);
|
2015-05-10 02:36:18 -03:00
|
|
|
if (_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-06-07 08:09:42 -03:00
|
|
|
ret = fcntl(_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-05-10 02:36:18 -03:00
|
|
|
|
|
|
|
/* we want to be able to re-use ports quickly */
|
|
|
|
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
|
|
|
|
ret = connect(_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "connect failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2019-03-12 23:54:51 -03:00
|
|
|
fcntl(_fd, F_SETFD, FD_CLOEXEC);
|
2015-05-10 02:36:18 -03:00
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
|
2018-12-05 01:32:49 -04:00
|
|
|
/*
|
|
|
|
start a UDP client connection for the serial port.
|
|
|
|
*/
|
|
|
|
void UARTDriver::_udp_start_client(const char *address, uint16_t port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (_connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_use_send_recv = true;
|
|
|
|
|
|
|
|
if (_fd != -1) {
|
|
|
|
close(_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sockaddr,0,sizeof(sockaddr));
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
|
|
|
#endif
|
|
|
|
sockaddr.sin_port = htons(port);
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(address);
|
|
|
|
|
|
|
|
_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ret = fcntl(_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to setup for broadcast, this may fail if insufficient privileges
|
|
|
|
int one = 1;
|
|
|
|
setsockopt(_fd,SOL_SOCKET,SO_BROADCAST,(char *)&one,sizeof(one));
|
|
|
|
|
|
|
|
ret = connect(_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "udp connect failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
_is_udp = true;
|
2021-09-17 20:48:38 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2018-12-05 01:58:37 -04:00
|
|
|
_packetise = true;
|
2021-09-17 20:48:38 -03:00
|
|
|
#endif
|
2018-12-05 01:32:49 -04:00
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 03:32:43 -04:00
|
|
|
/*
|
|
|
|
start a UDP multicast connection
|
|
|
|
*/
|
|
|
|
void UARTDriver::_udp_start_multicast(const char *address, uint16_t port)
|
|
|
|
{
|
|
|
|
if (_connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// establish the listening port
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&sockaddr,0,sizeof(sockaddr));
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
|
|
|
#endif
|
|
|
|
sockaddr.sin_port = htons(port);
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(address);
|
|
|
|
|
|
|
|
_mc_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (_mc_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ret = fcntl(_mc_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int one = 1;
|
|
|
|
if (setsockopt(_mc_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
|
|
|
|
fprintf(stderr, "setsockopt failed: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// close on exec, to allow reboot
|
|
|
|
fcntl(_mc_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
2023-06-21 02:22:32 -03:00
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
|
|
|
|
/*
|
|
|
|
on cygwin you need to bind to INADDR_ANY then use the multicast
|
|
|
|
IP_ADD_MEMBERSHIP to get on the right address
|
|
|
|
*/
|
|
|
|
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
#endif
|
|
|
|
|
2018-12-05 03:32:43 -04:00
|
|
|
ret = bind(_mc_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "multicast bind failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ip_mreq mreq {};
|
|
|
|
mreq.imr_multiaddr.s_addr = inet_addr(address);
|
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
ret = setsockopt(_mc_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
|
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "multicast membership add failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now start the outgoing connection as an ordinary UDP connection
|
|
|
|
_udp_start_client(address, port);
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:32:49 -04:00
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
/*
|
|
|
|
start a UART connection for the serial port
|
|
|
|
*/
|
2016-08-22 04:11:48 -03:00
|
|
|
void UARTDriver::_uart_start_connection(void)
|
2015-11-03 16:59:43 -04:00
|
|
|
{
|
|
|
|
struct termios t {};
|
|
|
|
if (!_connected) {
|
2016-08-22 04:11:48 -03:00
|
|
|
_fd = ::open(_uart_path, O_RDWR | O_CLOEXEC);
|
|
|
|
if (_fd == -1) {
|
2020-08-18 04:24:22 -03:00
|
|
|
static uint32_t last_error_print_ms;
|
|
|
|
if (AP_HAL::millis() - last_error_print_ms > 5000) {
|
|
|
|
::printf("Failed to open (%s): %s\n", _uart_path, strerror(errno));
|
|
|
|
last_error_print_ms = AP_HAL::millis();
|
|
|
|
}
|
2016-08-22 04:11:48 -03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-23 10:31:23 -03:00
|
|
|
// use much smaller buffer sizes on real UARTs
|
|
|
|
_writebuffer.set_size(1024);
|
|
|
|
_readbuffer.set_size(512);
|
2016-08-22 04:11:48 -03:00
|
|
|
::printf("Opened %s\n", _uart_path);
|
2015-11-03 16:59:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_fd == -1) {
|
2016-08-22 04:11:48 -03:00
|
|
|
AP_HAL::panic("Unable to open UART %s", _uart_path);
|
2015-11-03 16:59:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set non-blocking
|
|
|
|
int flags = fcntl(_fd, F_GETFL, 0);
|
|
|
|
flags = flags | O_NONBLOCK;
|
|
|
|
fcntl(_fd, F_SETFL, flags);
|
|
|
|
|
|
|
|
// disable LF -> CR/LF
|
|
|
|
tcgetattr(_fd, &t);
|
|
|
|
t.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL | IXON | IXOFF);
|
|
|
|
t.c_oflag &= ~(OPOST | ONLCR);
|
|
|
|
t.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
|
|
|
|
t.c_cc[VMIN] = 0;
|
2016-05-16 03:51:09 -03:00
|
|
|
if (_sitlState->use_rtscts()) {
|
|
|
|
t.c_cflag |= CRTSCTS;
|
|
|
|
}
|
2015-11-03 16:59:43 -04:00
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
|
|
|
|
// set baudrate
|
2020-12-08 23:59:31 -04:00
|
|
|
if (_uart_baudrate != 0) {
|
|
|
|
set_speed(_uart_baudrate);
|
|
|
|
}
|
2015-11-03 16:59:43 -04:00
|
|
|
|
|
|
|
_connected = true;
|
|
|
|
_use_send_recv = false;
|
|
|
|
}
|
|
|
|
|
2012-12-12 18:04:27 -04:00
|
|
|
/*
|
|
|
|
see if a new connection is coming in
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_check_connection(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_connected) {
|
|
|
|
// we only want 1 connection at a time
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_select_check(_listen_fd)) {
|
2016-10-30 02:24:21 -03:00
|
|
|
_fd = accept(_listen_fd, nullptr, nullptr);
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_fd != -1) {
|
|
|
|
int one = 1;
|
|
|
|
_connected = true;
|
|
|
|
setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
|
|
|
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
2019-03-12 23:54:51 -03:00
|
|
|
fcntl(_fd, F_SETFD, FD_CLOEXEC);
|
2023-12-10 23:55:21 -04:00
|
|
|
fprintf(stdout, "New connection on SERIAL%u\n", _portNumber);
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
|
|
|
}
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
use select() to see if something is pending
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
bool UARTDriver::_select_check(int fd)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2016-06-30 07:45:03 -03:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-08 17:48:25 -04:00
|
|
|
#if !APM_BUILD_TYPE(APM_BUILD_Replay)
|
2015-05-04 21:59:07 -03:00
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
// zero time means immediate return from select()
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (select(fd+1, &fds, nullptr, nullptr, &tv) == 1) {
|
2015-05-04 21:59:07 -03:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-08 17:48:25 -04:00
|
|
|
#endif
|
2015-05-04 21:59:07 -03:00
|
|
|
return false;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_set_nonblocking(int fd)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
unsigned v = fcntl(fd, F_GETFL, 0);
|
|
|
|
fcntl(fd, F_SETFL, v | O_NONBLOCK);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2017-11-22 18:21:50 -04:00
|
|
|
bool UARTDriver::set_unbuffered_writes(bool on) {
|
|
|
|
if (_fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_unbuffered_writes = on;
|
|
|
|
|
|
|
|
// this has no effect
|
|
|
|
unsigned v = fcntl(_fd, F_GETFL, 0);
|
|
|
|
v &= ~O_NONBLOCK;
|
2018-01-19 16:50:19 -04:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
fcntl(_fd, F_SETFL | F_NOCACHE, v | O_SYNC);
|
|
|
|
#else
|
2017-11-22 18:21:50 -04:00
|
|
|
fcntl(_fd, F_SETFL, v | O_DIRECT | O_SYNC);
|
2018-01-19 16:50:19 -04:00
|
|
|
#endif
|
2017-11-22 18:21:50 -04:00
|
|
|
return _unbuffered_writes;
|
|
|
|
}
|
|
|
|
|
2016-08-22 04:11:48 -03:00
|
|
|
void UARTDriver::_check_reconnect(void)
|
|
|
|
{
|
|
|
|
if (!_uart_path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_uart_start_connection();
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:52:08 -04:00
|
|
|
uint16_t UARTDriver::read_from_async_csv(uint8_t *buffer, uint16_t space)
|
|
|
|
{
|
|
|
|
if (_fd == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const uint32_t micros = AP_HAL::micros();
|
|
|
|
if (micros < 5000000) {
|
|
|
|
// don't inject for the first several seconds
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t i;
|
|
|
|
for (i=0; i<space; i++) {
|
|
|
|
if (logic_async_csv.loaded) {
|
|
|
|
const uint32_t emit_timestamp_us = micros - logic_async_csv.first_emit_micros_us;
|
|
|
|
const uint32_t data_timestamp_us = logic_async_csv.loaded_data.timestamp_us - logic_async_csv.first_timestamp_us;
|
|
|
|
if (data_timestamp_us > emit_timestamp_us) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
buffer[i] = logic_async_csv.loaded_data.b;
|
|
|
|
logic_async_csv.loaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!logic_async_csv.loaded) {
|
|
|
|
uint8_t c;
|
|
|
|
const ssize_t nread = ::read(_fd, &c, 1);
|
|
|
|
if (nread == 0) {
|
|
|
|
// EOF
|
|
|
|
close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// feed data into CSV Reader, handle new state:
|
|
|
|
const auto retcode = logic_async_csv.csvreader.feed(c);
|
|
|
|
switch (retcode) {
|
|
|
|
case AP_CSVReader::RetCode::OK:
|
|
|
|
continue;
|
|
|
|
case AP_CSVReader::RetCode::ERROR:
|
|
|
|
AP_HAL::panic("Malformed CSV?");
|
|
|
|
case AP_CSVReader::RetCode::TERM_DONE:
|
|
|
|
case AP_CSVReader::RetCode::VECTOR_DONE:
|
|
|
|
switch (logic_async_csv.terms_seen) {
|
|
|
|
case 0: // start_time
|
|
|
|
if (!logic_async_csv.done_first_line) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
logic_async_csv.loaded_data.timestamp_us = atof((char*)logic_async_csv.term) * 1000000; // seconds to microseconds
|
|
|
|
break;
|
|
|
|
case 1: // data
|
|
|
|
if (!logic_async_csv.done_first_line) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
logic_async_csv.loaded_data.b = (char_to_hex(logic_async_csv.term[2]) << 4) | char_to_hex(logic_async_csv.term[3]);
|
|
|
|
break;
|
|
|
|
case 2: // error
|
|
|
|
case 3: // framing error
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
AP_HAL::panic("Too many terms in CSV, want (name,type,start_time,duration,data");
|
|
|
|
}
|
|
|
|
logic_async_csv.terms_seen++;
|
|
|
|
if (retcode != AP_CSVReader::RetCode::VECTOR_DONE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we've handled the last term, now handle the vector:
|
|
|
|
if (logic_async_csv.terms_seen != 4) {
|
|
|
|
AP_HAL::panic("Incorrect number off terms in CSV, want (Time [s],Value,Parity Error,Framing Error)");
|
|
|
|
}
|
|
|
|
logic_async_csv.terms_seen = 0;
|
|
|
|
if (!logic_async_csv.done_first_line) {
|
|
|
|
// skip the headers
|
|
|
|
logic_async_csv.done_first_line = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (logic_async_csv.first_timestamp_us == 0) {
|
|
|
|
logic_async_csv.first_timestamp_us = logic_async_csv.loaded_data.timestamp_us;
|
|
|
|
logic_async_csv.first_emit_micros_us = micros;
|
|
|
|
}
|
|
|
|
logic_async_csv.loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:04:09 -03:00
|
|
|
void UARTDriver::handle_writing_from_writebuffer_to_device()
|
2016-01-10 02:17:32 -04:00
|
|
|
{
|
2016-01-10 06:26:35 -04:00
|
|
|
if (!_connected) {
|
2016-08-22 04:11:48 -03:00
|
|
|
_check_reconnect();
|
2016-01-10 06:26:35 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
ssize_t nwritten;
|
2019-01-24 20:26:38 -04:00
|
|
|
uint32_t max_bytes = 10000;
|
2020-09-12 16:04:38 -03:00
|
|
|
#if !defined(HAL_BUILD_AP_PERIPH)
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *_sitl = AP::sitl();
|
2019-01-24 20:26:38 -04:00
|
|
|
if (_sitl && _sitl->telem_baudlimit_enable) {
|
|
|
|
// limit byte rate to configured baudrate
|
|
|
|
uint32_t now = AP_HAL::micros();
|
2023-07-17 23:04:09 -03:00
|
|
|
float dt = 1.0e-6 * (now - last_write_tick_us);
|
2019-01-24 20:26:38 -04:00
|
|
|
max_bytes = _uart_baudrate * dt / 10;
|
|
|
|
if (max_bytes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-17 23:04:09 -03:00
|
|
|
last_write_tick_us = now;
|
2019-01-24 20:26:38 -04:00
|
|
|
}
|
2020-09-12 16:04:38 -03:00
|
|
|
#endif
|
2018-12-05 01:58:37 -04:00
|
|
|
if (_packetise) {
|
|
|
|
uint16_t n = _writebuffer.available();
|
2019-01-24 20:26:38 -04:00
|
|
|
n = MIN(n, max_bytes);
|
2021-09-17 20:48:38 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2018-12-05 01:58:37 -04:00
|
|
|
if (n > 0) {
|
|
|
|
n = mavlink_packetise(_writebuffer, n);
|
|
|
|
}
|
2021-09-17 20:48:38 -03:00
|
|
|
#endif
|
2018-12-05 01:58:37 -04:00
|
|
|
if (n > 0) {
|
|
|
|
// keep as a single UDP packet
|
|
|
|
uint8_t tmpbuf[n];
|
|
|
|
_writebuffer.peekbytes(tmpbuf, n);
|
|
|
|
ssize_t ret = send(_fd, tmpbuf, n, MSG_DONTWAIT);
|
|
|
|
if (ret > 0) {
|
|
|
|
_writebuffer.advance(ret);
|
2016-08-22 04:11:48 -03:00
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
2018-12-05 01:58:37 -04:00
|
|
|
} else {
|
|
|
|
uint32_t navail;
|
|
|
|
const uint8_t *readptr = _writebuffer.readptr(navail);
|
|
|
|
if (readptr && navail > 0) {
|
2019-01-24 20:26:38 -04:00
|
|
|
navail = MIN(navail, max_bytes);
|
2021-10-26 09:31:22 -03:00
|
|
|
if (_sim_serial_device != nullptr) {
|
|
|
|
nwritten = _sim_serial_device->write_to_device((const char*)readptr, navail);
|
|
|
|
} else if (!_use_send_recv) {
|
|
|
|
nwritten = ::write(_fd, readptr, navail);
|
2018-12-05 01:58:37 -04:00
|
|
|
if (nwritten == -1 && errno != EAGAIN && _uart_path) {
|
|
|
|
close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
_connected = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nwritten = send(_fd, readptr, navail, MSG_DONTWAIT);
|
|
|
|
}
|
|
|
|
if (nwritten > 0) {
|
|
|
|
_writebuffer.advance(nwritten);
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
|
|
|
}
|
2023-07-17 23:04:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UARTDriver::handle_reading_from_device_to_readbuffer()
|
|
|
|
{
|
|
|
|
if (!_connected) {
|
|
|
|
_check_reconnect();
|
|
|
|
return;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
|
2016-01-10 02:31:50 -04:00
|
|
|
uint32_t space = _readbuffer.space();
|
|
|
|
if (space == 0) {
|
2016-01-10 02:17:32 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-07-17 23:04:09 -03:00
|
|
|
|
|
|
|
uint32_t max_bytes = 10000;
|
|
|
|
#if !defined(HAL_BUILD_AP_PERIPH)
|
|
|
|
SITL::SIM *_sitl = AP::sitl();
|
|
|
|
if (_sitl && _sitl->telem_baudlimit_enable) {
|
|
|
|
// limit byte rate to configured baudrate
|
|
|
|
uint32_t now = AP_HAL::micros();
|
|
|
|
float dt = 1.0e-6 * (now - last_read_tick_us);
|
|
|
|
max_bytes = _uart_baudrate * dt / 10;
|
|
|
|
if (max_bytes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_read_tick_us = now;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-24 20:26:38 -04:00
|
|
|
space = MIN(space, max_bytes);
|
2023-07-17 23:04:09 -03:00
|
|
|
|
2016-01-10 02:31:50 -04:00
|
|
|
char buf[space];
|
|
|
|
ssize_t nread = 0;
|
2018-12-05 03:32:43 -04:00
|
|
|
if (_mc_fd >= 0) {
|
|
|
|
if (_select_check(_mc_fd)) {
|
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t fromlen = sizeof(from);
|
|
|
|
nread = recvfrom(_mc_fd, buf, space, MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
|
|
|
|
uint16_t port = ntohs(from.sin_port);
|
|
|
|
if (_mc_myport == 0) {
|
|
|
|
// get our own address, so we can recognise packets from ourself
|
|
|
|
struct sockaddr_in myaddr;
|
|
|
|
socklen_t myaddrlen;
|
|
|
|
if (getsockname(_fd, (struct sockaddr *)&myaddr, &myaddrlen) == 0) {
|
|
|
|
_mc_myport = ntohs(myaddr.sin_port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_mc_myport == port) {
|
|
|
|
// assume this is a packet from ourselves. This is not
|
|
|
|
// entirely accurate, as it could be a packet from
|
|
|
|
// another machine that has assigned the same port,
|
|
|
|
// unfortunately we don't have a better way to detect
|
|
|
|
// packets from ourselves
|
|
|
|
nread = 0;
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 09:31:22 -03:00
|
|
|
} else if (_sim_serial_device != nullptr) {
|
|
|
|
nread = _sim_serial_device->read_from_device(buf, space);
|
2023-01-12 19:52:08 -04:00
|
|
|
} else if (logic_async_csv.active) {
|
|
|
|
nread = read_from_async_csv((uint8_t*)buf, space);
|
2018-12-05 03:32:43 -04:00
|
|
|
} else if (!_use_send_recv) {
|
|
|
|
if (!_select_check(_fd)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
int fd = _console?0:_fd;
|
2016-01-10 02:31:50 -04:00
|
|
|
nread = ::read(fd, buf, space);
|
2016-08-22 04:11:48 -03:00
|
|
|
if (nread == -1 && errno != EAGAIN && _uart_path) {
|
|
|
|
close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
_connected = false;
|
|
|
|
}
|
2018-12-05 03:32:43 -04:00
|
|
|
} else if (_select_check(_fd)) {
|
|
|
|
nread = recv(_fd, buf, space, MSG_DONTWAIT);
|
|
|
|
if (nread <= 0 && !_is_udp) {
|
|
|
|
// the socket has reached EOF
|
|
|
|
close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
_connected = false;
|
2023-12-10 23:55:21 -04:00
|
|
|
fprintf(stdout, "Closed connection on SERIAL%u\n", _portNumber);
|
2018-12-05 03:32:43 -04:00
|
|
|
fflush(stdout);
|
2023-01-23 23:31:52 -04:00
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
|
|
|
|
if (_portNumber == 0) {
|
|
|
|
// exit on cygwin port 0 is almost certainly closing the
|
|
|
|
// connection in MissionPlanner SITL. We want to exit or
|
|
|
|
// we leave a stray process which confuses restart
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-05 03:32:43 -04:00
|
|
|
return;
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-01-10 02:31:50 -04:00
|
|
|
if (nread > 0) {
|
|
|
|
_readbuffer.write((uint8_t *)buf, nread);
|
2018-05-15 21:52:08 -03:00
|
|
|
_receive_timestamp = AP_HAL::micros64();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 23:04:09 -03:00
|
|
|
void UARTDriver::_timer_tick(void)
|
|
|
|
{
|
|
|
|
handle_writing_from_writebuffer_to_device();
|
|
|
|
handle_reading_from_device_to_readbuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-15 21:52:08 -03:00
|
|
|
/*
|
|
|
|
return timestamp estimate in microseconds for when the start of
|
|
|
|
a nbytes packet arrived on the uart. This should be treated as a
|
|
|
|
time constraint, not an exact time. It is guaranteed that the
|
|
|
|
packet did not start being received after this time, but it
|
|
|
|
could have been in a system buffer before the returned time.
|
|
|
|
|
|
|
|
This takes account of the baudrate of the link. For transports
|
|
|
|
that have no baudrate (such as USB) the time estimate may be
|
|
|
|
less accurate.
|
|
|
|
|
|
|
|
A return value of zero means the HAL does not support this API
|
|
|
|
*/
|
2018-05-16 18:01:14 -03:00
|
|
|
uint64_t UARTDriver::receive_time_constraint_us(uint16_t nbytes)
|
2018-05-15 21:52:08 -03:00
|
|
|
{
|
|
|
|
uint64_t last_receive_us = _receive_timestamp;
|
|
|
|
if (_uart_baudrate > 0) {
|
|
|
|
// assume 10 bits per byte.
|
2018-05-16 18:01:14 -03:00
|
|
|
uint32_t transport_time_us = (1000000UL * 10UL / _uart_baudrate) * (nbytes+available());
|
2018-05-15 21:52:08 -03:00
|
|
|
last_receive_us -= transport_time_us;
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
2018-05-15 21:52:08 -03:00
|
|
|
return last_receive_us;
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
|
|
|
|
2020-04-02 21:47:06 -03:00
|
|
|
ssize_t UARTDriver::get_system_outqueue_length() const
|
|
|
|
{
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
|
|
|
|
return 0;
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
int size;
|
|
|
|
if (ioctl(_fd, TIOCOUTQ, &size) == -1) {
|
|
|
|
// ::fprintf(stderr, "ioctl TIOCOUTQ failed: %m\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-24 19:32:42 -03:00
|
|
|
uint32_t UARTDriver::bw_in_bytes_per_second() const
|
|
|
|
{
|
|
|
|
// if connected, assume at least a 10/100Mbps connection
|
2023-12-10 23:55:21 -04:00
|
|
|
const uint32_t bitrate = _connected ? 10E6 : _uart_baudrate;
|
2023-10-24 19:32:42 -03:00
|
|
|
return bitrate/10; // convert bits to bytes minus overhead
|
|
|
|
};
|
2016-01-10 02:17:32 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|