2013-09-22 03:01:24 -03:00
|
|
|
#include "UARTDriver.h"
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <assert.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2014-06-11 03:34:07 -03:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-11 03:34:07 -03:00
|
|
|
#include <string.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2015-06-11 20:20:22 -03:00
|
|
|
#include "ConsoleDevice.h"
|
2015-07-08 14:27:21 -03:00
|
|
|
#include "TCPServerDevice.h"
|
2016-05-17 23:26:57 -03:00
|
|
|
#include "UARTDevice.h"
|
|
|
|
#include "UDPDevice.h"
|
2015-06-11 15:30:36 -03:00
|
|
|
|
2016-05-22 21:10:03 -03:00
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2021-09-17 20:48:29 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2018-12-05 01:58:26 -04:00
|
|
|
#include <AP_HAL/utility/packetise.h>
|
2021-09-17 20:48:29 -03:00
|
|
|
#endif
|
2016-05-22 21:10:03 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
using namespace Linux;
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::UARTDriver(bool default_console) :
|
2016-07-07 14:57:06 -03:00
|
|
|
_device{new ConsoleDevice()}
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-30 23:49:58 -03:00
|
|
|
if (default_console) {
|
|
|
|
_console = true;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set the tty device to use for this UART
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::set_device_path(const char *path)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
|
|
|
device_path = path;
|
|
|
|
}
|
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
void UARTDriver::_begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
2016-07-08 10:16:11 -03:00
|
|
|
if (!_initialised) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (device_path == nullptr && _console) {
|
2024-05-26 22:24:12 -03:00
|
|
|
_device = NEW_NOTHROW ConsoleDevice();
|
2016-07-08 10:16:11 -03:00
|
|
|
} else {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (device_path == nullptr) {
|
2016-07-08 10:16:11 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_device = _parseDevicePath(device_path);
|
|
|
|
|
|
|
|
if (!_device.get()) {
|
|
|
|
::fprintf(stderr, "Argument is not valid. Fallback to console.\n"
|
|
|
|
"Launch with --help to see an example.\n");
|
2024-05-26 22:24:12 -03:00
|
|
|
_device = NEW_NOTHROW ConsoleDevice();
|
2016-07-08 10:16:11 -03:00
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2016-07-07 14:57:06 -03:00
|
|
|
if (!_connected) {
|
|
|
|
_connected = _device->open();
|
2020-03-12 00:23:01 -03:00
|
|
|
if (_connected) {
|
|
|
|
_device->set_blocking(false);
|
|
|
|
}
|
2016-07-07 14:57:06 -03:00
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = false;
|
2016-07-07 14:57:06 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
while (_in_timer) hal.scheduler->delay(1);
|
|
|
|
|
2015-06-11 15:30:36 -03:00
|
|
|
_device->set_speed(b);
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2018-07-11 20:36:45 -03:00
|
|
|
bool clear_buffers = false;
|
|
|
|
if (b != 0) {
|
|
|
|
if (_baudrate != b && hal.console != this) {
|
|
|
|
clear_buffers = true;
|
|
|
|
}
|
|
|
|
_baudrate = b;
|
|
|
|
}
|
2018-05-15 21:47:42 -03:00
|
|
|
|
2015-06-11 08:53:52 -03:00
|
|
|
_allocate_buffers(rxS, txS);
|
2018-07-11 20:36:45 -03:00
|
|
|
|
|
|
|
if (clear_buffers) {
|
|
|
|
_readbuf.clear();
|
|
|
|
_writebuf.clear();
|
|
|
|
}
|
2015-06-11 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_allocate_buffers(uint16_t rxS, uint16_t txS)
|
2015-06-11 08:53:52 -03:00
|
|
|
{
|
|
|
|
/* we have enough memory to have a larger transmit buffer for
|
|
|
|
* all ports. This means we don't get delays while waiting to
|
|
|
|
* write GPS config packets
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rxS < 8192) {
|
|
|
|
rxS = 8192;
|
|
|
|
}
|
2015-07-29 01:03:30 -03:00
|
|
|
if (txS < 32000) {
|
|
|
|
txS = 32000;
|
2015-06-11 08:53:52 -03:00
|
|
|
}
|
|
|
|
|
2016-07-29 21:40:56 -03:00
|
|
|
if (_writebuf.set_size(txS) && _readbuf.set_size(rxS)) {
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = true;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_deallocate_buffers()
|
2015-06-11 15:35:48 -03:00
|
|
|
{
|
2016-07-29 21:40:56 -03:00
|
|
|
_readbuf.set_size(0);
|
|
|
|
_writebuf.set_size(0);
|
2015-06-11 15:35:48 -03:00
|
|
|
}
|
|
|
|
|
2014-06-11 03:34:07 -03:00
|
|
|
/*
|
|
|
|
Device path accepts the following syntaxes:
|
|
|
|
- /dev/ttyO1
|
2014-10-06 01:10:48 -03:00
|
|
|
- tcp:*:1243:wait
|
|
|
|
- udp:192.168.2.15:1243
|
2014-06-11 03:34:07 -03:00
|
|
|
*/
|
2016-07-07 14:57:06 -03:00
|
|
|
AP_HAL::OwnPtr<SerialDevice> UARTDriver::_parseDevicePath(const char *arg)
|
2014-06-11 03:34:07 -03:00
|
|
|
{
|
2014-10-06 01:10:48 -03:00
|
|
|
struct stat st;
|
2014-06-11 03:34:07 -03:00
|
|
|
|
2015-07-16 14:05:55 -03:00
|
|
|
if (stat(arg, &st) == 0 && S_ISCHR(st.st_mode)) {
|
2024-05-26 22:24:12 -03:00
|
|
|
return AP_HAL::OwnPtr<SerialDevice>(NEW_NOTHROW UARTDevice(arg));
|
2016-05-17 23:26:57 -03:00
|
|
|
} else if (strncmp(arg, "tcp:", 4) != 0 &&
|
2016-06-28 07:58:31 -03:00
|
|
|
strncmp(arg, "udp:", 4) != 0 &&
|
|
|
|
strncmp(arg, "udpin:", 6)) {
|
2016-07-07 14:57:06 -03:00
|
|
|
return nullptr;
|
2015-07-16 14:05:55 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
char *devstr = strdup(arg);
|
2016-07-07 14:57:06 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (devstr == nullptr) {
|
2016-07-07 14:57:06 -03:00
|
|
|
return nullptr;
|
2014-10-06 01:10:48 -03:00
|
|
|
}
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
char *saveptr = nullptr;
|
2015-07-16 14:05:55 -03:00
|
|
|
char *protocol, *ip, *port, *flag;
|
|
|
|
|
|
|
|
protocol = strtok_r(devstr, ":", &saveptr);
|
2016-10-30 02:24:21 -03:00
|
|
|
ip = strtok_r(nullptr, ":", &saveptr);
|
|
|
|
port = strtok_r(nullptr, ":", &saveptr);
|
|
|
|
flag = strtok_r(nullptr, ":", &saveptr);
|
2015-07-16 14:05:55 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (ip == nullptr || port == nullptr) {
|
2016-07-07 14:57:06 -03:00
|
|
|
free(devstr);
|
|
|
|
return nullptr;
|
2015-07-16 14:05:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_ip) {
|
|
|
|
free(_ip);
|
2016-10-30 02:24:21 -03:00
|
|
|
_ip = nullptr;
|
2015-07-16 14:05:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_flag) {
|
|
|
|
free(_flag);
|
2016-10-30 02:24:21 -03:00
|
|
|
_flag = nullptr;
|
2014-10-06 01:10:48 -03:00
|
|
|
}
|
2015-07-16 14:05:55 -03:00
|
|
|
|
|
|
|
_base_port = (uint16_t) atoi(port);
|
|
|
|
_ip = strdup(ip);
|
|
|
|
|
|
|
|
/* Optional flag for TCP */
|
2016-10-30 02:24:21 -03:00
|
|
|
if (flag != nullptr) {
|
2015-07-16 14:05:55 -03:00
|
|
|
_flag = strdup(flag);
|
|
|
|
}
|
|
|
|
|
2016-07-07 14:57:06 -03:00
|
|
|
AP_HAL::OwnPtr<SerialDevice> device = nullptr;
|
2015-07-16 14:05:55 -03:00
|
|
|
|
2016-07-07 14:57:06 -03:00
|
|
|
if (strcmp(protocol, "udp") == 0 || strcmp(protocol, "udpin") == 0) {
|
|
|
|
bool bcast = (_flag && strcmp(_flag, "bcast") == 0);
|
2021-09-17 20:48:29 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2016-07-07 14:57:06 -03:00
|
|
|
_packetise = true;
|
2021-09-17 20:48:29 -03:00
|
|
|
#endif
|
2016-07-07 14:57:06 -03:00
|
|
|
if (strcmp(protocol, "udp") == 0) {
|
2024-05-26 22:24:12 -03:00
|
|
|
device = NEW_NOTHROW UDPDevice(_ip, _base_port, bcast, false);
|
2016-07-07 14:57:06 -03:00
|
|
|
} else {
|
|
|
|
if (bcast) {
|
|
|
|
AP_HAL::panic("Can't combine udpin with bcast");
|
|
|
|
}
|
2024-05-26 22:24:12 -03:00
|
|
|
device = NEW_NOTHROW UDPDevice(_ip, _base_port, false, true);
|
2016-06-28 07:58:31 -03:00
|
|
|
|
2016-07-07 14:57:06 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool wait = (_flag && strcmp(_flag, "wait") == 0);
|
2024-05-26 22:24:12 -03:00
|
|
|
device = NEW_NOTHROW TCPServerDevice(_ip, _base_port, wait);
|
2016-06-28 08:00:44 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
2016-07-07 14:57:06 -03:00
|
|
|
free(devstr);
|
|
|
|
return device;
|
2015-06-19 13:15:30 -03:00
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
/*
|
|
|
|
shutdown a UART
|
|
|
|
*/
|
2023-07-07 05:46:52 -03:00
|
|
|
void UARTDriver::_end()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = false;
|
2014-06-11 03:34:07 -03:00
|
|
|
_connected = false;
|
2015-06-11 18:31:50 -03:00
|
|
|
|
|
|
|
while (_in_timer) {
|
|
|
|
hal.scheduler->delay(1);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2015-06-11 15:35:48 -03:00
|
|
|
|
2015-06-11 18:31:50 -03:00
|
|
|
_device->close();
|
2015-06-11 15:35:48 -03:00
|
|
|
_deallocate_buffers();
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
void UARTDriver::_flush()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
|
|
|
// we are not doing any buffering, so flush is a no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if the UART is initialised
|
|
|
|
*/
|
2016-05-17 23:26:57 -03:00
|
|
|
bool UARTDriver::is_initialized()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
return _initialised;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
do we have any bytes pending transmission?
|
|
|
|
*/
|
2016-05-17 23:26:57 -03:00
|
|
|
bool UARTDriver::tx_pending()
|
|
|
|
{
|
2016-07-29 21:40:56 -03:00
|
|
|
return (_writebuf.available() > 0);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return the number of bytes available to be read
|
|
|
|
*/
|
2023-07-07 05:46:52 -03:00
|
|
|
uint32_t UARTDriver::_available()
|
2016-05-17 23:26:57 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
if (!_initialised) {
|
2013-09-22 03:01:24 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
return _readbuf.available();
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
how many bytes are available in the output buffer?
|
|
|
|
*/
|
2016-08-02 10:42:50 -03:00
|
|
|
uint32_t UARTDriver::txspace()
|
2016-05-17 23:26:57 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
return _writebuf.space();
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
ssize_t UARTDriver::_read(uint8_t *buffer, uint16_t count)
|
2016-05-17 23:26:57 -03:00
|
|
|
{
|
2016-07-29 21:40:56 -03:00
|
|
|
if (!_initialised) {
|
2023-07-07 05:46:52 -03:00
|
|
|
return 0;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
return _readbuf.read(buffer, count);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
bool UARTDriver::_discard_input()
|
2020-05-22 21:22:46 -03:00
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-03 01:34:37 -03:00
|
|
|
_readbuf.clear();
|
2020-05-22 21:22:46 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
/*
|
|
|
|
write size bytes to the write buffer
|
|
|
|
*/
|
2023-07-07 05:46:52 -03:00
|
|
|
size_t UARTDriver::_write(const uint8_t *buffer, size_t size)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-06 03:23:24 -03:00
|
|
|
if (!_write_mutex.take_nonblocking()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
|
2018-08-06 03:23:24 -03:00
|
|
|
size_t ret = _writebuf.write(buffer, size);
|
|
|
|
_write_mutex.give();
|
|
|
|
return ret;
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
try writing n bytes, handling an unresponsive port
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
int UARTDriver::_write_fd(const uint8_t *buf, uint16_t n)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
2015-07-28 20:55:47 -03:00
|
|
|
/*
|
|
|
|
allow for delayed connection. This allows ArduPilot to start
|
|
|
|
before a network interface is available.
|
|
|
|
*/
|
|
|
|
if (!_connected) {
|
|
|
|
_connected = _device->open();
|
|
|
|
}
|
|
|
|
if (!_connected) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2016-07-29 21:40:56 -03:00
|
|
|
return _device->write(buf, n);
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
try reading n bytes, handling an unresponsive port
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
int UARTDriver::_read_fd(uint8_t *buf, uint16_t n)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
2016-07-29 21:40:56 -03:00
|
|
|
return _device->read(buf, n);
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2015-07-29 01:03:30 -03:00
|
|
|
try to push out one lump of pending bytes
|
|
|
|
return true if progress is made
|
2013-09-28 21:13:51 -03:00
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
bool UARTDriver::_write_pending_bytes(void)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
|
|
|
// write any pending bytes
|
2016-07-29 21:40:56 -03:00
|
|
|
uint32_t available_bytes = _writebuf.available();
|
2016-07-30 08:54:37 -03:00
|
|
|
uint16_t n = available_bytes;
|
2018-12-05 01:58:26 -04:00
|
|
|
|
2021-09-17 20:48:29 -03:00
|
|
|
#if HAL_GCS_ENABLED
|
2018-12-05 01:58:26 -04:00
|
|
|
if (_packetise && n > 0) {
|
|
|
|
// send on MAVLink packet boundaries if possible
|
|
|
|
n = mavlink_packetise(_writebuf, n);
|
2014-10-06 01:10:48 -03:00
|
|
|
}
|
2021-09-17 20:48:29 -03:00
|
|
|
#endif
|
2014-10-06 01:10:48 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
if (n > 0) {
|
2016-07-29 21:40:56 -03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (_packetise) {
|
|
|
|
// keep as a single UDP packet
|
|
|
|
uint8_t tmpbuf[n];
|
|
|
|
_writebuf.peekbytes(tmpbuf, n);
|
|
|
|
ret = _write_fd(tmpbuf, n);
|
|
|
|
if (ret > 0)
|
|
|
|
_writebuf.advance(ret);
|
2013-09-28 21:13:51 -03:00
|
|
|
} else {
|
2016-07-29 21:40:56 -03:00
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
const auto n_vec = _writebuf.peekiovec(vec, n);
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
ret = _write_fd(vec[i].data, (uint16_t)vec[i].len);
|
2016-10-05 13:17:14 -03:00
|
|
|
if (ret < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_writebuf.advance(ret);
|
|
|
|
|
|
|
|
/* We wrote less than we asked for, stop */
|
|
|
|
if ((unsigned)ret != vec[i].len) {
|
|
|
|
break;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:40:56 -03:00
|
|
|
return _writebuf.available() != available_bytes;
|
2015-07-29 01:03:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
push any pending bytes to/from the serial port. This is called at
|
|
|
|
1kHz in the timer thread. Doing it this way reduces the system call
|
2016-05-17 23:26:57 -03:00
|
|
|
overhead in the main task enormously.
|
2015-07-29 01:03:30 -03:00
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_timer_tick(void)
|
2015-07-29 01:03:30 -03:00
|
|
|
{
|
|
|
|
if (!_initialised) return;
|
|
|
|
|
|
|
|
_in_timer = true;
|
|
|
|
|
|
|
|
uint8_t num_send = 10;
|
|
|
|
while (num_send != 0 && _write_pending_bytes()) {
|
|
|
|
num_send--;
|
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
// try to fill the read buffer
|
2016-07-29 21:40:56 -03:00
|
|
|
int ret;
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
|
|
|
|
const auto n_vec = _readbuf.reserve(vec, _readbuf.space());
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
ret = _read_fd(vec[i].data, vec[i].len);
|
2016-10-05 13:17:14 -03:00
|
|
|
if (ret < 0) {
|
2016-07-29 21:40:56 -03:00
|
|
|
break;
|
2016-10-05 13:17:14 -03:00
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
_readbuf.commit((unsigned)ret);
|
2016-10-05 13:17:14 -03:00
|
|
|
|
2018-05-15 21:47:42 -03:00
|
|
|
// update receive timestamp
|
|
|
|
_receive_timestamp[_receive_timestamp_idx^1] = AP_HAL::micros64();
|
|
|
|
_receive_timestamp_idx ^= 1;
|
|
|
|
|
2016-10-05 13:17:14 -03:00
|
|
|
/* stop reading as we read less than we asked for */
|
|
|
|
if ((unsigned)ret < vec[i].len) {
|
2016-07-29 21:40:56 -03:00
|
|
|
break;
|
2016-10-05 13:17:14 -03:00
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer = false;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2018-05-15 21:47:42 -03:00
|
|
|
|
2019-04-11 11:26:35 -03:00
|
|
|
void UARTDriver::configure_parity(uint8_t v) {
|
|
|
|
_device->set_parity(v);
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:47:42 -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:47:42 -03:00
|
|
|
{
|
|
|
|
uint64_t last_receive_us = _receive_timestamp[_receive_timestamp_idx];
|
|
|
|
if (_baudrate > 0) {
|
|
|
|
// assume 10 bits per byte.
|
2018-05-16 18:01:14 -03:00
|
|
|
uint32_t transport_time_us = (1000000UL * 10UL / _baudrate) * (nbytes+available());
|
2018-05-15 21:47:42 -03:00
|
|
|
last_receive_us -= transport_time_us;
|
|
|
|
}
|
|
|
|
return last_receive_us;
|
|
|
|
}
|
2023-10-24 19:42:14 -03:00
|
|
|
|
|
|
|
uint32_t UARTDriver::bw_in_bytes_per_second() const
|
|
|
|
{
|
|
|
|
// if connected, assume at least a 10/100Mbps connection
|
|
|
|
const uint32_t bitrate = (_connected && _ip != nullptr) ? 10E6 : _baudrate;
|
|
|
|
return bitrate/10; // convert bits to bytes minus overhead
|
2023-10-30 21:03:25 -03:00
|
|
|
}
|