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>
|
|
|
|
|
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-10-30 02:24:21 -03:00
|
|
|
device_path(nullptr),
|
2016-07-07 14:57:06 -03:00
|
|
|
_packetise(false),
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
open the tty
|
|
|
|
*/
|
2016-05-17 23:26:57 -03:00
|
|
|
void UARTDriver::begin(uint32_t b)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
begin(b, 0, 0);
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2016-05-17 23:26:57 -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) {
|
2015-06-11 20:20:22 -03:00
|
|
|
_device = new 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");
|
|
|
|
_device = new ConsoleDevice();
|
|
|
|
}
|
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();
|
|
|
|
_device->set_blocking(false);
|
|
|
|
}
|
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)) {
|
2016-07-07 14:57:06 -03:00
|
|
|
return AP_HAL::OwnPtr<SerialDevice>(new 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);
|
|
|
|
_packetise = true;
|
|
|
|
if (strcmp(protocol, "udp") == 0) {
|
|
|
|
device = new UDPDevice(_ip, _base_port, bcast, false);
|
|
|
|
} else {
|
|
|
|
if (bcast) {
|
|
|
|
AP_HAL::panic("Can't combine udpin with bcast");
|
|
|
|
}
|
|
|
|
device = new 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);
|
|
|
|
device = new 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
|
|
|
|
*/
|
2016-05-17 23:26:57 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-17 23:26:57 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
enable or disable blocking writes
|
|
|
|
*/
|
2016-05-17 23:26:57 -03:00
|
|
|
void UARTDriver::set_blocking_writes(bool blocking)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
_nonblocking_writes = !blocking;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2016-08-02 10:42:50 -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
|
|
|
}
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
int16_t UARTDriver::read()
|
|
|
|
{
|
2016-07-29 21:40:56 -03:00
|
|
|
if (!_initialised) {
|
2013-09-22 03:01:24 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
|
|
|
|
uint8_t byte;
|
|
|
|
if (!_readbuf.read_byte(&byte)) {
|
2013-09-28 21:13:51 -03:00
|
|
|
return -1;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2016-07-29 21:40:56 -03:00
|
|
|
|
|
|
|
return byte;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Linux implementations of Print virtual methods */
|
2016-05-17 23:26:57 -03:00
|
|
|
size_t UARTDriver::write(uint8_t c)
|
|
|
|
{
|
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
|
|
|
|
2016-07-29 21:40:56 -03:00
|
|
|
while (_writebuf.space() == 0) {
|
2013-09-28 21:13:51 -03:00
|
|
|
if (_nonblocking_writes) {
|
2018-08-06 03:23:24 -03:00
|
|
|
_write_mutex.give();
|
2013-09-28 21:13:51 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
2018-08-06 03:23:24 -03:00
|
|
|
size_t ret = _writebuf.write(&c, 1);
|
|
|
|
_write_mutex.give();
|
|
|
|
return ret;
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write size bytes to the write buffer
|
|
|
|
*/
|
2015-10-20 18:13:25 -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
|
|
|
if (!_nonblocking_writes) {
|
|
|
|
/*
|
|
|
|
use the per-byte delay loop in write() above for blocking writes
|
|
|
|
*/
|
|
|
|
size_t ret = 0;
|
|
|
|
while (size--) {
|
|
|
|
if (write(*buffer++) != 1) break;
|
|
|
|
ret++;
|
|
|
|
}
|
2018-08-06 03:23:24 -03:00
|
|
|
_write_mutex.give();
|
2013-09-28 21:13:51 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
int16_t b = _writebuf.peek(0);
|
2016-05-22 21:10:03 -03:00
|
|
|
if (_packetise && n > 0 &&
|
2016-07-30 08:54:37 -03:00
|
|
|
b != MAVLINK_STX_MAVLINK1 && b != MAVLINK_STX) {
|
2015-12-14 03:26:21 -04:00
|
|
|
/*
|
|
|
|
we have a non-mavlink packet at the start of the
|
|
|
|
buffer. Look ahead for a MAVLink start byte, up to 256 bytes
|
|
|
|
ahead
|
|
|
|
*/
|
|
|
|
uint16_t limit = n>256?256:n;
|
|
|
|
uint16_t i;
|
|
|
|
for (i=0; i<limit; i++) {
|
2016-07-30 08:54:37 -03:00
|
|
|
b = _writebuf.peek(i);
|
2016-05-22 21:10:03 -03:00
|
|
|
if (b == MAVLINK_STX_MAVLINK1 || b == MAVLINK_STX) {
|
2015-12-14 03:26:21 -04:00
|
|
|
n = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we didn't find a MAVLink marker then limit the send size to 256
|
|
|
|
if (i == limit) {
|
|
|
|
n = limit;
|
|
|
|
}
|
|
|
|
}
|
2016-07-30 08:54:37 -03:00
|
|
|
b = _writebuf.peek(0);
|
|
|
|
if (_packetise && n > 0 &&
|
2016-05-22 21:10:03 -03:00
|
|
|
(b == MAVLINK_STX_MAVLINK1 || b == MAVLINK_STX)) {
|
|
|
|
uint8_t min_length = (b == MAVLINK_STX_MAVLINK1)?8:12;
|
2014-10-06 01:10:48 -03:00
|
|
|
// this looks like a MAVLink packet - try to write on
|
|
|
|
// packet boundaries when possible
|
2016-05-22 21:10:03 -03:00
|
|
|
if (n < min_length) {
|
|
|
|
// we need to wait for more data to arrive
|
2014-10-06 01:10:48 -03:00
|
|
|
n = 0;
|
|
|
|
} else {
|
2014-10-08 22:21:50 -03:00
|
|
|
// the length of the packet is the 2nd byte, and mavlink
|
|
|
|
// packets have a 6 byte header plus 2 byte checksum,
|
|
|
|
// giving len+8 bytes
|
2016-07-29 21:40:56 -03:00
|
|
|
int16_t len = _writebuf.peek(1);
|
2016-05-22 21:10:03 -03:00
|
|
|
if (b == MAVLINK_STX) {
|
|
|
|
// check for signed packet with extra 13 bytes
|
2016-07-29 21:40:56 -03:00
|
|
|
int16_t incompat_flags = _writebuf.peek(2);
|
2016-05-22 21:10:03 -03:00
|
|
|
if (incompat_flags & MAVLINK_IFLAG_SIGNED) {
|
|
|
|
min_length += MAVLINK_SIGNATURE_BLOCK_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n < len+min_length) {
|
2014-10-08 22:21:50 -03:00
|
|
|
// we don't have a full packet yet
|
2014-10-06 01:10:48 -03:00
|
|
|
n = 0;
|
2016-05-22 21:10:03 -03:00
|
|
|
} else if (n > len+min_length) {
|
2014-10-08 22:21:50 -03:00
|
|
|
// send just 1 packet at a time (so MAVLink packets
|
|
|
|
// are aligned on UDP boundaries)
|
2016-05-22 21:10:03 -03:00
|
|
|
n = len+min_length;
|
2014-10-06 01:10:48 -03:00
|
|
|
}
|
2016-05-17 23:26:57 -03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|