2014-10-06 01:10:48 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: -*- nil -*-
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2014-07-06 23:03:26 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
2013-09-28 03:28:36 -03:00
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
#include "UARTDriver.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2013-09-28 21:13:51 -03:00
|
|
|
#include <poll.h>
|
|
|
|
#include <assert.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
#include <sys/ioctl.h>
|
2014-06-11 03:34:07 -03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-08-15 19:50:47 -03:00
|
|
|
#include <AP_HAL/utility/RingBuffer.h>
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2015-06-11 15:30:36 -03:00
|
|
|
#include "UARTDevice.h"
|
2015-06-11 18:31:50 -03:00
|
|
|
#include "UDPDevice.h"
|
2015-06-11 20:20:22 -03:00
|
|
|
#include "ConsoleDevice.h"
|
2015-07-08 14:27:21 -03:00
|
|
|
#include "TCPServerDevice.h"
|
2015-12-13 23:19:54 -04:00
|
|
|
#include "UARTQFlight.h"
|
2015-06-11 15:30:36 -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) :
|
2013-09-22 03:01:24 -03:00
|
|
|
device_path(NULL),
|
2014-10-08 22:21:50 -03:00
|
|
|
_packetise(false),
|
|
|
|
_flow_control(FLOW_CONTROL_DISABLE)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-30 23:49:58 -03:00
|
|
|
if (default_console) {
|
2015-06-11 20:20:22 -03:00
|
|
|
_device = new ConsoleDevice();
|
|
|
|
_device->open();
|
2013-09-30 23:49:58 -03:00
|
|
|
_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
|
|
|
|
*/
|
2015-10-20 18:13:25 -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
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
2013-09-28 21:13:51 -03:00
|
|
|
{
|
2013-09-30 23:49:58 -03:00
|
|
|
if (device_path == NULL && _console) {
|
2015-06-11 20:20:22 -03:00
|
|
|
_device = new ConsoleDevice();
|
|
|
|
_device->open();
|
|
|
|
_device->set_blocking(false);
|
2013-09-30 23:49:58 -03:00
|
|
|
} else if (!_initialised) {
|
2013-09-28 21:13:51 -03:00
|
|
|
if (device_path == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-19 13:15:30 -03:00
|
|
|
|
|
|
|
switch (_parseDevicePath(device_path)) {
|
2014-06-11 03:34:07 -03:00
|
|
|
case DEVICE_TCP:
|
2014-10-06 01:10:48 -03:00
|
|
|
{
|
2015-06-19 13:15:30 -03:00
|
|
|
_tcp_start_connection();
|
2015-07-29 01:03:30 -03:00
|
|
|
_flow_control = FLOW_CONTROL_ENABLE;
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
2015-06-19 13:15:30 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
case DEVICE_UDP:
|
|
|
|
{
|
|
|
|
_udp_start_connection();
|
2014-10-08 22:21:50 -03:00
|
|
|
_flow_control = FLOW_CONTROL_ENABLE;
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
2015-06-19 13:15:30 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
2015-12-13 23:19:54 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_QFLIGHT
|
|
|
|
case DEVICE_QFLIGHT:
|
|
|
|
{
|
|
|
|
_qflight_start_connection();
|
|
|
|
_flow_control = FLOW_CONTROL_DISABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-19 13:15:30 -03:00
|
|
|
case DEVICE_SERIAL:
|
2014-10-06 01:10:48 -03:00
|
|
|
{
|
2015-06-11 08:57:15 -03:00
|
|
|
if (!_serial_start_connection()) {
|
2015-06-11 15:30:36 -03:00
|
|
|
break; /* Whatever it might mean */
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Notify that the option is not valid and select standart input and output
|
2015-07-16 14:05:55 -03:00
|
|
|
::printf("Argument is not valid. Fallback to console.\n");
|
|
|
|
::printf("Launch with --help to see an example.\n");
|
2014-10-06 01:10:48 -03:00
|
|
|
|
2015-06-11 20:20:22 -03:00
|
|
|
_device = new ConsoleDevice();
|
|
|
|
_device->open();
|
|
|
|
_device->set_blocking(false);
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = false;
|
|
|
|
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
|
|
|
|
2015-06-11 08:53:52 -03:00
|
|
|
_allocate_buffers(rxS, txS);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
/*
|
|
|
|
allocate the read buffer
|
|
|
|
*/
|
|
|
|
if (rxS != 0 && rxS != _readbuf_size) {
|
|
|
|
_readbuf_size = rxS;
|
|
|
|
if (_readbuf != NULL) {
|
|
|
|
free(_readbuf);
|
|
|
|
}
|
|
|
|
_readbuf = (uint8_t *)malloc(_readbuf_size);
|
|
|
|
_readbuf_head = 0;
|
|
|
|
_readbuf_tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
allocate the write buffer
|
|
|
|
*/
|
|
|
|
if (txS != 0 && txS != _writebuf_size) {
|
|
|
|
_writebuf_size = txS;
|
|
|
|
if (_writebuf != NULL) {
|
|
|
|
free(_writebuf);
|
|
|
|
}
|
2014-08-18 20:27:45 -03:00
|
|
|
_writebuf = (uint8_t *)malloc(_writebuf_size);
|
2013-09-28 21:13:51 -03:00
|
|
|
_writebuf_head = 0;
|
|
|
|
_writebuf_tail = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_writebuf_size != 0 && _readbuf_size != 0) {
|
|
|
|
_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
|
|
|
{
|
|
|
|
if (_readbuf) {
|
|
|
|
free(_readbuf);
|
|
|
|
_readbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_writebuf) {
|
|
|
|
free(_writebuf);
|
|
|
|
_writebuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_readbuf_size = _writebuf_size = 0;
|
|
|
|
_writebuf_head = 0;
|
|
|
|
_writebuf_tail = 0;
|
|
|
|
_readbuf_head = 0;
|
|
|
|
_readbuf_tail = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::device_type 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)) {
|
|
|
|
return DEVICE_SERIAL;
|
2015-12-13 23:19:54 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_QFLIGHT
|
|
|
|
} else if (strncmp(arg, "qflight:", 8) == 0) {
|
|
|
|
return DEVICE_QFLIGHT;
|
|
|
|
#endif
|
2015-07-16 14:05:55 -03:00
|
|
|
} else if (strncmp(arg, "tcp:", 4) != 0 &&
|
|
|
|
strncmp(arg, "udp:", 4) != 0) {
|
|
|
|
return DEVICE_UNKNOWN;
|
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
char *devstr = strdup(arg);
|
|
|
|
if (devstr == NULL) {
|
|
|
|
return DEVICE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-07-16 14:05:55 -03:00
|
|
|
char *saveptr = NULL;
|
|
|
|
char *protocol, *ip, *port, *flag;
|
|
|
|
|
|
|
|
protocol = strtok_r(devstr, ":", &saveptr);
|
|
|
|
ip = strtok_r(NULL, ":", &saveptr);
|
|
|
|
port = strtok_r(NULL, ":", &saveptr);
|
|
|
|
flag = strtok_r(NULL, ":", &saveptr);
|
|
|
|
|
|
|
|
device_type type = DEVICE_UNKNOWN;
|
|
|
|
|
|
|
|
if (ip == NULL || port == NULL) {
|
|
|
|
fprintf(stderr, "IP or port is set incorrectly.\n");
|
|
|
|
type = DEVICE_UNKNOWN;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_ip) {
|
|
|
|
free(_ip);
|
2014-10-06 01:10:48 -03:00
|
|
|
_ip = NULL;
|
2015-07-16 14:05:55 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_flag) {
|
|
|
|
free(_flag);
|
2014-10-06 01:10:48 -03:00
|
|
|
_flag = NULL;
|
|
|
|
}
|
2015-07-16 14:05:55 -03:00
|
|
|
|
|
|
|
_base_port = (uint16_t) atoi(port);
|
|
|
|
_ip = strdup(ip);
|
|
|
|
|
|
|
|
/* Optional flag for TCP */
|
|
|
|
if (flag != NULL) {
|
|
|
|
_flag = strdup(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(protocol, "udp") == 0) {
|
|
|
|
type = DEVICE_UDP;
|
|
|
|
} else {
|
|
|
|
type = DEVICE_TCP;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
2014-10-06 01:10:48 -03:00
|
|
|
free(devstr);
|
2015-07-16 14:05:55 -03:00
|
|
|
return type;
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
|
|
|
|
2015-07-16 14:05:55 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool UARTDriver::_serial_start_connection()
|
2015-06-11 08:57:15 -03:00
|
|
|
{
|
2015-06-11 15:30:36 -03:00
|
|
|
_device = new UARTDevice(device_path);
|
2015-07-28 20:55:47 -03:00
|
|
|
_connected = _device->open();
|
2015-06-11 15:30:36 -03:00
|
|
|
_device->set_blocking(false);
|
2015-06-11 08:57:15 -03:00
|
|
|
_flow_control = FLOW_CONTROL_DISABLE;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-13 23:19:54 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_QFLIGHT
|
|
|
|
bool UARTDriver::_qflight_start_connection()
|
|
|
|
{
|
|
|
|
_device = new QFLIGHTDevice(device_path);
|
|
|
|
_connected = _device->open();
|
|
|
|
_flow_control = FLOW_CONTROL_DISABLE;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-06 01:10:48 -03:00
|
|
|
/*
|
|
|
|
start a UDP connection for the serial port
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_udp_start_connection(void)
|
2014-10-06 01:10:48 -03:00
|
|
|
{
|
2015-07-29 03:46:53 -03:00
|
|
|
bool bcast = (_flag && strcmp(_flag, "bcast") == 0);
|
|
|
|
_device = new UDPDevice(_ip, _base_port, bcast);
|
2015-07-28 20:55:47 -03:00
|
|
|
_connected = _device->open();
|
2015-06-11 18:31:50 -03:00
|
|
|
_device->set_blocking(false);
|
2014-10-06 01:10:48 -03:00
|
|
|
|
2015-06-11 18:31:50 -03:00
|
|
|
/* try to write on MAVLink packet boundaries if possible */
|
2014-10-06 01:10:48 -03:00
|
|
|
_packetise = true;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_tcp_start_connection(void)
|
2015-06-19 13:15:30 -03:00
|
|
|
{
|
2015-07-28 20:37:57 -03:00
|
|
|
bool wait = (_flag && strcmp(_flag, "wait") == 0);
|
|
|
|
_device = new TCPServerDevice(_ip, _base_port, wait);
|
2015-06-19 13:15:30 -03:00
|
|
|
|
2015-07-28 20:55:47 -03:00
|
|
|
_connected = _device->open();
|
2015-06-19 13:15:30 -03:00
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
/*
|
|
|
|
shutdown a UART
|
|
|
|
*/
|
2015-10-20 18:13:25 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-20 18:13:25 -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
|
|
|
|
*/
|
2015-10-20 18:13:25 -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
|
|
|
|
*/
|
2015-10-20 18:13:25 -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?
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
bool UARTDriver::tx_pending()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
return !BUF_EMPTY(_writebuf);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
return the number of bytes available to be read
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
int16_t UARTDriver::available()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
if (!_initialised) {
|
2013-09-22 03:01:24 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
uint16_t _tail;
|
|
|
|
return BUF_AVAILABLE(_readbuf);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
how many bytes are available in the output buffer?
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
int16_t UARTDriver::txspace()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint16_t _head;
|
|
|
|
return BUF_SPACE(_writebuf);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
int16_t UARTDriver::read()
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
uint8_t c;
|
|
|
|
if (!_initialised || _readbuf == NULL) {
|
2013-09-22 03:01:24 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
if (BUF_EMPTY(_readbuf)) {
|
|
|
|
return -1;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
c = _readbuf[_readbuf_head];
|
|
|
|
BUF_ADVANCEHEAD(_readbuf, 1);
|
|
|
|
return c;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Linux implementations of Print virtual methods */
|
2015-10-20 18:13:25 -03:00
|
|
|
size_t UARTDriver::write(uint8_t c)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
if (!_initialised) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint16_t _head;
|
|
|
|
|
|
|
|
while (BUF_SPACE(_writebuf) == 0) {
|
|
|
|
if (_nonblocking_writes) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
_writebuf[_writebuf_tail] = c;
|
|
|
|
BUF_ADVANCETAIL(_writebuf, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t _head, space;
|
|
|
|
space = BUF_SPACE(_writebuf);
|
|
|
|
if (space == 0) {
|
2013-09-22 03:01:24 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
if (size > space) {
|
|
|
|
size = space;
|
|
|
|
}
|
|
|
|
if (_writebuf_tail < _head) {
|
|
|
|
// perform as single memcpy
|
|
|
|
assert(_writebuf_tail+size <= _writebuf_size);
|
|
|
|
memcpy(&_writebuf[_writebuf_tail], buffer, size);
|
|
|
|
BUF_ADVANCETAIL(_writebuf, size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform as two memcpy calls
|
|
|
|
uint16_t n = _writebuf_size - _writebuf_tail;
|
|
|
|
if (n > size) n = size;
|
|
|
|
assert(_writebuf_tail+n <= _writebuf_size);
|
|
|
|
memcpy(&_writebuf[_writebuf_tail], buffer, n);
|
|
|
|
BUF_ADVANCETAIL(_writebuf, n);
|
|
|
|
buffer += n;
|
|
|
|
n = size - n;
|
|
|
|
if (n > 0) {
|
|
|
|
assert(_writebuf_tail+n <= _writebuf_size);
|
|
|
|
memcpy(&_writebuf[_writebuf_tail], buffer, n);
|
|
|
|
BUF_ADVANCETAIL(_writebuf, n);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
{
|
|
|
|
int ret = 0;
|
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;
|
|
|
|
}
|
2015-06-11 15:30:36 -03:00
|
|
|
|
|
|
|
ret = _device->write(buf, n);
|
2013-09-28 21:13:51 -03:00
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
BUF_ADVANCEHEAD(_writebuf, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
{
|
|
|
|
int ret;
|
2015-06-11 15:30:36 -03:00
|
|
|
|
|
|
|
ret = _device->read(buf, n);
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
if (ret > 0) {
|
|
|
|
BUF_ADVANCETAIL(_readbuf, ret);
|
2015-06-11 15:30:36 -03:00
|
|
|
}
|
2015-02-12 06:45:08 -04:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
{
|
|
|
|
uint16_t n;
|
|
|
|
|
|
|
|
// write any pending bytes
|
|
|
|
uint16_t _tail;
|
2015-07-29 01:03:30 -03:00
|
|
|
uint16_t available_bytes = BUF_AVAILABLE(_writebuf);
|
|
|
|
n = available_bytes;
|
2015-12-14 03:26:21 -04:00
|
|
|
if (_packetise && n > 0 && _writebuf[_writebuf_head] != 254) {
|
|
|
|
/*
|
|
|
|
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++) {
|
|
|
|
if (_writebuf[(_writebuf_head + i) % _writebuf_size] == 254) {
|
|
|
|
n = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we didn't find a MAVLink marker then limit the send size to 256
|
|
|
|
if (i == limit) {
|
|
|
|
n = limit;
|
|
|
|
}
|
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
if (_packetise && n > 0 && _writebuf[_writebuf_head] == 254) {
|
|
|
|
// this looks like a MAVLink packet - try to write on
|
|
|
|
// packet boundaries when possible
|
|
|
|
if (n < 8) {
|
|
|
|
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
|
2014-10-06 01:10:48 -03:00
|
|
|
uint16_t ofs = (_writebuf_head + 1) % _writebuf_size;
|
|
|
|
uint8_t len = _writebuf[ofs];
|
|
|
|
if (n < len+8) {
|
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;
|
|
|
|
} else if (n > len+8) {
|
2014-10-08 22:21:50 -03:00
|
|
|
// send just 1 packet at a time (so MAVLink packets
|
|
|
|
// are aligned on UDP boundaries)
|
2014-10-06 01:10:48 -03:00
|
|
|
n = len+8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
if (n > 0) {
|
2014-10-08 22:21:50 -03:00
|
|
|
uint16_t n1 = _writebuf_size - _writebuf_head;
|
|
|
|
if (n1 >= n) {
|
2013-09-28 21:13:51 -03:00
|
|
|
// do as a single write
|
|
|
|
_write_fd(&_writebuf[_writebuf_head], n);
|
|
|
|
} else {
|
|
|
|
// split into two writes
|
2014-10-08 22:21:50 -03:00
|
|
|
if (_packetise) {
|
|
|
|
// keep as a single UDP packet
|
|
|
|
uint8_t tmpbuf[n];
|
|
|
|
memcpy(tmpbuf, &_writebuf[_writebuf_head], n1);
|
|
|
|
if (n > n1) {
|
|
|
|
memcpy(&tmpbuf[n1], &_writebuf[0], n-n1);
|
|
|
|
}
|
|
|
|
_write_fd(tmpbuf, n);
|
|
|
|
} else {
|
|
|
|
int ret = _write_fd(&_writebuf[_writebuf_head], n1);
|
|
|
|
if (ret == n1 && n > n1) {
|
|
|
|
_write_fd(&_writebuf[_writebuf_head], n - n1);
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 01:03:30 -03:00
|
|
|
return BUF_AVAILABLE(_writebuf) != available_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
overhead in the main task enormously.
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void UARTDriver::_timer_tick(void)
|
2015-07-29 01:03:30 -03:00
|
|
|
{
|
|
|
|
uint16_t n;
|
|
|
|
|
|
|
|
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
|
|
|
|
uint16_t _head;
|
|
|
|
n = BUF_SPACE(_readbuf);
|
|
|
|
if (n > 0) {
|
2014-10-08 22:27:32 -03:00
|
|
|
uint16_t n1 = _readbuf_size - _readbuf_tail;
|
|
|
|
if (n1 >= n) {
|
2013-09-28 21:13:51 -03:00
|
|
|
// one read will do
|
|
|
|
assert(_readbuf_tail+n <= _readbuf_size);
|
|
|
|
_read_fd(&_readbuf[_readbuf_tail], n);
|
|
|
|
} else {
|
|
|
|
assert(_readbuf_tail+n1 <= _readbuf_size);
|
|
|
|
int ret = _read_fd(&_readbuf[_readbuf_tail], n1);
|
2014-08-18 20:27:45 -03:00
|
|
|
if (ret == n1 && n > n1) {
|
2013-09-28 21:13:51 -03:00
|
|
|
assert(_readbuf_tail+(n-n1) <= _readbuf_size);
|
|
|
|
_read_fd(&_readbuf[_readbuf_tail], n - n1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_in_timer = false;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2013-09-28 03:28:36 -03:00
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD
|