2014-10-06 01:10:48 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: -*- nil -*-
|
|
|
|
|
2013-09-28 03:28:36 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
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-01-06 17:09:03 -04:00
|
|
|
#include "../AP_HAL/utility/RingBuffer.h"
|
2013-09-22 03:01:24 -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;
|
|
|
|
|
2013-09-30 23:49:58 -03:00
|
|
|
LinuxUARTDriver::LinuxUARTDriver(bool default_console) :
|
2013-09-22 03:01:24 -03:00
|
|
|
device_path(NULL),
|
2013-09-30 23:49:58 -03:00
|
|
|
_rd_fd(-1),
|
2014-10-06 01:10:48 -03:00
|
|
|
_wr_fd(-1),
|
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) {
|
|
|
|
_rd_fd = 0;
|
|
|
|
_wr_fd = 1;
|
|
|
|
_console = true;
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set the tty device to use for this UART
|
|
|
|
*/
|
2015-06-28 15:57:19 -03:00
|
|
|
void LinuxUARTDriver::set_device_path(const char *path)
|
2013-09-22 03:01:24 -03:00
|
|
|
{
|
|
|
|
device_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
open the tty
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::begin(uint32_t b)
|
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
begin(b, 0, 0);
|
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
void LinuxUARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
|
|
|
{
|
2013-09-30 23:49:58 -03:00
|
|
|
if (device_path == NULL && _console) {
|
|
|
|
_rd_fd = 0;
|
|
|
|
_wr_fd = 1;
|
2013-10-01 00:05:04 -03:00
|
|
|
fcntl(_rd_fd, F_SETFL, fcntl(_rd_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
fcntl(_wr_fd, F_SETFL, fcntl(_wr_fd, F_GETFL, 0) | O_NONBLOCK);
|
2013-09-30 23:49:58 -03:00
|
|
|
} else if (!_initialised) {
|
2013-09-28 21:13:51 -03:00
|
|
|
if (device_path == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-11 03:34:07 -03:00
|
|
|
|
|
|
|
switch (_parseDevicePath(device_path)){
|
|
|
|
case DEVICE_TCP:
|
2014-10-06 01:10:48 -03:00
|
|
|
{
|
|
|
|
_connected = false;
|
|
|
|
if (_flag != NULL){
|
|
|
|
if (!strcmp(_flag, "wait")){
|
|
|
|
_tcp_start_connection(true);
|
2014-06-11 03:34:07 -03:00
|
|
|
} else {
|
|
|
|
_tcp_start_connection(false);
|
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
} else {
|
|
|
|
_tcp_start_connection(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_connected) {
|
2015-02-12 06:45:08 -04:00
|
|
|
::printf("LinuxUARTDriver TCP connection not established\n");
|
2014-10-06 01:10:48 -03:00
|
|
|
exit(1);
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
2014-10-08 22:21:50 -03:00
|
|
|
_flow_control = FLOW_CONTROL_ENABLE;
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DEVICE_SERIAL:
|
|
|
|
{
|
|
|
|
_rd_fd = open(device_path, O_RDWR);
|
|
|
|
_wr_fd = _rd_fd;
|
|
|
|
if (_rd_fd == -1) {
|
2014-10-08 22:21:50 -03:00
|
|
|
::fprintf(stdout, "Failed to open UART device %s - %s\n",
|
|
|
|
device_path, strerror(errno));
|
2014-10-06 01:10:48 -03:00
|
|
|
return;
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
// always run the file descriptor non-blocking, and deal with
|
|
|
|
// blocking IO in the higher level calls
|
|
|
|
fcntl(_rd_fd, F_SETFL, fcntl(_rd_fd, F_GETFL, 0) | O_NONBLOCK);
|
2014-10-08 22:21:50 -03:00
|
|
|
|
|
|
|
// TODO: add proper flow control support
|
|
|
|
_flow_control = FLOW_CONTROL_DISABLE;
|
2014-10-06 01:10:48 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Notify that the option is not valid and select standart input and output
|
2014-10-08 22:21:50 -03:00
|
|
|
::printf("LinuxUARTDriver parsing failed, using default\n");
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
_rd_fd = 0;
|
|
|
|
_wr_fd = 1;
|
|
|
|
fcntl(_rd_fd, F_SETFL, fcntl(_rd_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
fcntl(_wr_fd, F_SETFL, fcntl(_wr_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
break;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
2014-10-06 01:10:48 -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 < 1024) {
|
|
|
|
rxS = 8192;
|
|
|
|
}
|
|
|
|
if (txS < 8192) {
|
|
|
|
txS = 8192;
|
|
|
|
}
|
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = false;
|
|
|
|
while (_in_timer) hal.scheduler->delay(1);
|
|
|
|
|
2013-09-30 23:49:58 -03:00
|
|
|
if (b != 0 && _rd_fd == _wr_fd) {
|
2013-09-28 21:13:51 -03:00
|
|
|
// set the baud rate
|
2013-09-22 03:01:24 -03:00
|
|
|
struct termios t;
|
2014-07-07 00:36:20 -03:00
|
|
|
memset(&t, 0, sizeof(t));
|
2013-09-30 23:49:58 -03:00
|
|
|
tcgetattr(_rd_fd, &t);
|
2013-09-22 03:01:24 -03:00
|
|
|
cfsetspeed(&t, b);
|
|
|
|
// disable LF -> CR/LF
|
2014-11-07 07:13:29 -04:00
|
|
|
t.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL | IXON | IXOFF);
|
2014-08-18 09:20:55 -03:00
|
|
|
t.c_oflag &= ~(OPOST | ONLCR);
|
|
|
|
t.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE);
|
2014-11-07 07:13:29 -04:00
|
|
|
t.c_cc[VMIN] = 0;
|
2013-09-30 23:49:58 -03:00
|
|
|
tcsetattr(_rd_fd, TCSANOW, &t);
|
2013-09-22 03:01:24 -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
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2014-10-06 01:10:48 -03:00
|
|
|
LinuxUARTDriver::device_type LinuxUARTDriver::_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
|
|
|
_flag = NULL; // init flag
|
|
|
|
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
char *devstr = strdup(arg);
|
|
|
|
if (devstr == NULL) {
|
|
|
|
return DEVICE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(devstr, &st) == 0 && S_ISCHR(st.st_mode)) {
|
|
|
|
free(devstr);
|
|
|
|
return DEVICE_SERIAL;
|
|
|
|
} else if (strncmp(devstr, "tcp:", 4) == 0 ||
|
|
|
|
strncmp(devstr, "udp:", 4) == 0) {
|
|
|
|
char *saveptr = NULL;
|
|
|
|
// Parse the string
|
2014-06-11 03:34:07 -03:00
|
|
|
char *protocol, *ip, *port, *flag;
|
2014-10-06 01:10:48 -03:00
|
|
|
protocol = strtok_r(devstr, ":", &saveptr);
|
|
|
|
ip = strtok_r(NULL, ":", &saveptr);
|
|
|
|
port = strtok_r(NULL, ":", &saveptr);
|
|
|
|
flag = strtok_r(NULL, ":", &saveptr);
|
2014-06-11 03:34:07 -03:00
|
|
|
|
|
|
|
_base_port = (uint16_t) atoi(port);
|
2014-10-06 01:10:48 -03:00
|
|
|
if (_ip) free(_ip);
|
|
|
|
_ip = NULL;
|
|
|
|
if (ip) {
|
|
|
|
_ip = strdup(ip);
|
|
|
|
}
|
|
|
|
if (_flag) free(_flag);
|
|
|
|
_flag = NULL;
|
|
|
|
if (flag) {
|
|
|
|
_flag = strdup(flag);
|
|
|
|
}
|
|
|
|
if (strcmp(protocol, "udp") == 0) {
|
|
|
|
free(devstr);
|
|
|
|
return DEVICE_UDP;
|
|
|
|
}
|
|
|
|
free(devstr);
|
2014-06-11 03:34:07 -03:00
|
|
|
return DEVICE_TCP;
|
2014-10-06 01:10:48 -03:00
|
|
|
}
|
|
|
|
free(devstr);
|
|
|
|
return DEVICE_UNKNOWN;
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
start a TCP connection for the serial port. If wait_for_connection
|
|
|
|
is true then block until a client connects
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::_tcp_start_connection(bool wait_for_connection)
|
|
|
|
{
|
|
|
|
int one=1;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
int ret;
|
|
|
|
int listen_fd = -1; // socket we are listening on
|
|
|
|
int net_fd = -1; // network file descriptor, will be linked to wr_fd and rd_fd
|
2014-06-20 09:29:20 -03:00
|
|
|
uint8_t portNumber = 0; // connecto to _base_port + portNumber
|
2014-06-11 03:34:07 -03:00
|
|
|
|
|
|
|
if (net_fd != -1) {
|
|
|
|
close(net_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen_fd == -1) {
|
|
|
|
memset(&sockaddr,0,sizeof(sockaddr));
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
|
|
|
#endif
|
|
|
|
sockaddr.sin_port = htons(_base_port + portNumber);
|
|
|
|
sockaddr.sin_family = AF_INET;
|
2014-10-06 01:10:48 -03:00
|
|
|
if (strcmp(_ip, "*") == 0) {
|
|
|
|
// Bind to all interfaces
|
|
|
|
sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
} else {
|
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(_ip);
|
|
|
|
}
|
2014-06-11 03:34:07 -03:00
|
|
|
|
|
|
|
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (listen_fd == -1) {
|
2014-10-08 22:21:50 -03:00
|
|
|
::printf("socket failed - %s\n", strerror(errno));
|
2014-06-11 03:34:07 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we want to be able to re-use ports quickly */
|
|
|
|
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
if (!wait_for_connection) {
|
2014-06-11 03:34:07 -03:00
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
ret = connect(listen_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
2014-06-11 03:34:07 -03:00
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
if (ret < 0) {
|
|
|
|
::printf("connect failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-06-11 03:34:07 -03:00
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
fcntl(listen_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK);
|
2014-06-11 03:34:07 -03:00
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
_rd_fd = listen_fd;
|
|
|
|
|
|
|
|
_connected = true;
|
|
|
|
|
|
|
|
::printf("Serial port %u on TCP port %u\n", portNumber,
|
|
|
|
_base_port + portNumber);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
ret = bind(listen_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
|
|
|
::printf("bind failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(listen_fd, 5);
|
|
|
|
if (ret == -1) {
|
|
|
|
::printf("listen failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
::printf("Serial port %u on TCP port %u\n", portNumber,
|
|
|
|
_base_port + portNumber);
|
|
|
|
::fflush(stdout);
|
|
|
|
|
|
|
|
::printf("Waiting for connection ....\n");
|
|
|
|
::fflush(stdout);
|
|
|
|
net_fd = accept(listen_fd, NULL, NULL);
|
|
|
|
if (net_fd == -1) {
|
|
|
|
::printf("accept() error - %s", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
setsockopt(net_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
setsockopt(net_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2014-06-20 09:19:03 -03:00
|
|
|
|
2015-02-12 06:45:08 -04:00
|
|
|
// always run the file descriptor non-blocking, and deal with |
|
|
|
|
// blocking IO in the higher level calls
|
|
|
|
fcntl(net_fd, F_SETFL, fcntl(net_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
|
|
|
|
_connected = true;
|
|
|
|
_rd_fd = net_fd;
|
|
|
|
_wr_fd = net_fd;
|
|
|
|
}
|
2014-06-20 09:19:03 -03:00
|
|
|
|
2014-06-11 03:34:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 01:10:48 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
start a UDP connection for the serial port
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::_udp_start_connection(void)
|
|
|
|
{
|
|
|
|
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(_base_port);
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(_ip);
|
|
|
|
|
|
|
|
_rd_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (_rd_fd == -1) {
|
2014-10-08 22:21:50 -03:00
|
|
|
::printf("socket failed - %s\n", strerror(errno));
|
2014-10-06 01:10:48 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = connect(_rd_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
2014-10-08 22:21:50 -03:00
|
|
|
::printf("connect failed to %s:%u - %s\n",
|
|
|
|
_ip, (unsigned)_base_port,
|
|
|
|
strerror(errno));
|
2014-10-06 01:10:48 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// always run the file descriptor non-blocking, and deal with |
|
|
|
|
// blocking IO in the higher level calls
|
|
|
|
fcntl(_rd_fd, F_SETFL, fcntl(_rd_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
_wr_fd = _rd_fd;
|
|
|
|
|
|
|
|
// try to write on MAVLink packet boundaries if possible
|
|
|
|
_packetise = true;
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:01:24 -03:00
|
|
|
/*
|
|
|
|
shutdown a UART
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::end()
|
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
_initialised = false;
|
2014-06-11 03:34:07 -03:00
|
|
|
_connected = false;
|
2013-09-28 21:13:51 -03:00
|
|
|
while (_in_timer) hal.scheduler->delay(1);
|
2013-09-30 23:49:58 -03:00
|
|
|
if (_rd_fd == _wr_fd && _rd_fd != -1) {
|
|
|
|
close(_rd_fd);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
2013-09-30 23:49:58 -03:00
|
|
|
_rd_fd = -1;
|
|
|
|
_wr_fd = -1;
|
2013-09-28 21:13:51 -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;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LinuxUARTDriver::flush()
|
|
|
|
{
|
|
|
|
// we are not doing any buffering, so flush is a no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
return true if the UART is initialised
|
|
|
|
*/
|
|
|
|
bool LinuxUARTDriver::is_initialized()
|
|
|
|
{
|
2013-09-28 21:13:51 -03:00
|
|
|
return _initialised;
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
enable or disable blocking writes
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::set_blocking_writes(bool blocking)
|
|
|
|
{
|
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?
|
|
|
|
*/
|
|
|
|
bool LinuxUARTDriver::tx_pending()
|
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
int16_t LinuxUARTDriver::available()
|
|
|
|
{
|
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?
|
|
|
|
*/
|
|
|
|
int16_t LinuxUARTDriver::txspace()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int16_t LinuxUARTDriver::read()
|
|
|
|
{
|
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 */
|
|
|
|
size_t LinuxUARTDriver::write(uint8_t c)
|
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
size_t LinuxUARTDriver::write(const uint8_t *buffer, size_t size)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
int LinuxUARTDriver::_write_fd(const uint8_t *buf, uint16_t n)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
struct pollfd fds;
|
2013-09-30 23:49:58 -03:00
|
|
|
fds.fd = _wr_fd;
|
2013-09-28 21:13:51 -03:00
|
|
|
fds.events = POLLOUT;
|
|
|
|
fds.revents = 0;
|
|
|
|
|
|
|
|
if (poll(&fds, 1, 0) == 1) {
|
2013-09-30 23:49:58 -03:00
|
|
|
ret = ::write(_wr_fd, buf, n);
|
2013-09-22 03:01:24 -03:00
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
int LinuxUARTDriver::_read_fd(uint8_t *buf, uint16_t n)
|
|
|
|
{
|
|
|
|
int ret;
|
2013-09-30 23:49:58 -03:00
|
|
|
ret = ::read(_rd_fd, buf, n);
|
2013-09-28 21:13:51 -03:00
|
|
|
if (ret > 0) {
|
|
|
|
BUF_ADVANCETAIL(_readbuf, ret);
|
2015-02-12 06:45:08 -04:00
|
|
|
} else {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
/* Ignore EAGAIN that resulted from non-blocking read */
|
|
|
|
break;
|
|
|
|
case EPIPE:
|
|
|
|
/* Ignore EPIPE that resulted from peer shutdown */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
::fprintf(stdout, "read failed - %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2013-09-28 21:13:51 -03:00
|
|
|
}
|
2015-02-12 06:45:08 -04:00
|
|
|
|
2013-09-28 21:13:51 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void LinuxUARTDriver::_timer_tick(void)
|
|
|
|
{
|
|
|
|
uint16_t n;
|
|
|
|
|
|
|
|
if (!_initialised) return;
|
|
|
|
|
|
|
|
_in_timer = true;
|
|
|
|
|
|
|
|
// write any pending bytes
|
|
|
|
uint16_t _tail;
|
|
|
|
n = BUF_AVAILABLE(_writebuf);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|