2012-12-12 18:04:27 -04:00
|
|
|
// -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
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>
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#include "UARTDriver.h"
|
2012-12-17 22:34:13 -04:00
|
|
|
#include "SITL_State.h"
|
|
|
|
|
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 */
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::begin(uint32_t baud, uint16_t rxSpace, uint16_t txSpace)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2015-11-03 16:59:43 -04:00
|
|
|
const char *path = _sitlState->_uart_path[_portNumber];
|
|
|
|
|
|
|
|
if (strcmp(path, "GPS1") == 0) {
|
2012-12-12 18:04:27 -04:00
|
|
|
/* gps */
|
|
|
|
_connected = true;
|
2012-12-17 22:34:13 -04:00
|
|
|
_fd = _sitlState->gps_pipe();
|
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;
|
|
|
|
_fd = _sitlState->gps2_pipe();
|
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
|
|
|
|
uart:/dev/ttyUSB0:57600
|
|
|
|
*/
|
|
|
|
char *saveptr = NULL;
|
|
|
|
char *s = strdup(path);
|
|
|
|
char *devtype = strtok_r(s, ":", &saveptr);
|
|
|
|
char *args1 = strtok_r(NULL, ":", &saveptr);
|
|
|
|
char *args2 = strtok_r(NULL, ":", &saveptr);
|
|
|
|
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) {
|
|
|
|
if (args2 == NULL) {
|
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);
|
|
|
|
_uart_start_connection(args1, baudrate);
|
|
|
|
} 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
|
|
|
|
|
|
|
_set_nonblocking(_fd);
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::end()
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
int16_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-01-10 02:23:32 -04:00
|
|
|
int16_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
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
int16_t UARTDriver::read(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
if (available() <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
uint8_t c;
|
|
|
|
_readbuffer.read(&c, 1);
|
|
|
|
return c;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::flush(void)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
size_t UARTDriver::write(uint8_t c)
|
2012-12-12 18:04:27 -04:00
|
|
|
{
|
2016-01-10 02:17:32 -04:00
|
|
|
if (txspace() <= 0) {
|
2012-12-12 18:04:27 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
_writebuffer.write(&c, 1);
|
|
|
|
return 1;
|
2012-12-12 18:04:27 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
size_t UARTDriver::write(const uint8_t *buffer, size_t size)
|
2013-10-02 04:36:54 -03:00
|
|
|
{
|
2016-01-10 02:17:32 -04:00
|
|
|
if (txspace() <= (ssize_t)size) {
|
|
|
|
size = txspace();
|
2013-10-02 04:36:54 -03:00
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
if (size <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
_writebuffer.write(buffer, size);
|
|
|
|
return size;
|
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;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
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) {
|
|
|
|
memset(&sockaddr,0,sizeof(sockaddr));
|
2012-12-12 18:04:27 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
2015-05-04 21:59:07 -03:00
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
2012-12-12 18:04:27 -04:00
|
|
|
#endif
|
2015-11-03 16:59:43 -04:00
|
|
|
if (port > 1000) {
|
|
|
|
sockaddr.sin_port = htons(port);
|
|
|
|
} else {
|
|
|
|
sockaddr.sin_port = htons(_sitlState->base_port() + port);
|
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
sockaddr.sin_family = AF_INET;
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
_listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (_listen_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %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 */
|
|
|
|
setsockopt(_listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
fprintf(stderr, "bind port %u for %u\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
2015-05-10 02:36:18 -03:00
|
|
|
(unsigned)_portNumber);
|
2012-12-12 18:04:27 -04:00
|
|
|
|
2015-05-10 02:36:18 -03:00
|
|
|
ret = bind(_listen_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
2015-05-04 21:59:07 -03:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "bind failed on port %u - %s\n",
|
|
|
|
(unsigned)ntohs(sockaddr.sin_port),
|
|
|
|
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
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
fprintf(stderr, "Serial port %u on TCP port %u\n", _portNumber,
|
|
|
|
_sitlState->base_port() + _portNumber);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait_for_connection) {
|
|
|
|
fprintf(stdout, "Waiting for connection ....\n");
|
|
|
|
fflush(stdout);
|
|
|
|
_fd = accept(_listen_fd, NULL, NULL);
|
|
|
|
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));
|
|
|
|
_connected = true;
|
|
|
|
}
|
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
|
|
|
|
sockaddr.sin_port = port;
|
|
|
|
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
|
|
|
|
|
|
|
_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (_fd == -1) {
|
|
|
|
fprintf(stderr, "socket failed - %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
_connected = true;
|
|
|
|
}
|
|
|
|
|
2015-11-03 16:59:43 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
start a UART connection for the serial port
|
|
|
|
*/
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_uart_start_connection(const char *path, uint32_t baudrate)
|
2015-11-03 16:59:43 -04:00
|
|
|
{
|
|
|
|
struct termios t {};
|
|
|
|
if (!_connected) {
|
|
|
|
::printf("Opening %s\n", path);
|
|
|
|
_fd = ::open(path, O_RDWR | O_CLOEXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fd == -1) {
|
2015-11-19 23:11:17 -04:00
|
|
|
AP_HAL::panic("Unable to open UART %s", 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;
|
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
|
|
|
|
// set baudrate
|
|
|
|
tcgetattr(_fd, &t);
|
|
|
|
cfsetspeed(&t, baudrate);
|
|
|
|
tcsetattr(_fd, TCSANOW, &t);
|
|
|
|
|
|
|
|
_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)) {
|
|
|
|
_fd = accept(_listen_fd, NULL, NULL);
|
|
|
|
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));
|
|
|
|
fprintf(stdout, "New connection on serial port %u\n", _portNumber);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
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
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (select(fd+1, &fds, NULL, NULL, &tv) == 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void UARTDriver::_timer_tick(void)
|
2016-01-10 02:17:32 -04:00
|
|
|
{
|
2016-01-10 06:26:35 -04:00
|
|
|
if (!_connected) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-10 02:17:32 -04:00
|
|
|
uint32_t navail;
|
|
|
|
ssize_t nwritten;
|
|
|
|
|
|
|
|
const uint8_t *readptr = _writebuffer.readptr(navail);
|
|
|
|
if (readptr && navail > 0) {
|
|
|
|
if (!_use_send_recv) {
|
|
|
|
nwritten = ::write(_fd, readptr, navail);
|
|
|
|
} else {
|
|
|
|
nwritten = send(_fd, readptr, navail, MSG_DONTWAIT);
|
|
|
|
}
|
|
|
|
if (nwritten > 0) {
|
|
|
|
_writebuffer.advance(nwritten);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-10 02:31:50 -04:00
|
|
|
char buf[space];
|
|
|
|
ssize_t nread = 0;
|
2016-01-10 02:17:32 -04:00
|
|
|
if (!_use_send_recv) {
|
|
|
|
int fd = _console?0:_fd;
|
2016-01-10 02:31:50 -04:00
|
|
|
nread = ::read(fd, buf, space);
|
2016-01-10 02:17:32 -04:00
|
|
|
} else {
|
|
|
|
if (_select_check(_fd)) {
|
2016-01-10 02:31:50 -04:00
|
|
|
nread = recv(_fd, buf, space, MSG_DONTWAIT);
|
2016-01-10 02:17:32 -04:00
|
|
|
if (nread <= 0) {
|
|
|
|
// the socket has reached EOF
|
|
|
|
close(_fd);
|
|
|
|
_connected = false;
|
|
|
|
fprintf(stdout, "Closed connection on serial port %u\n", _portNumber);
|
|
|
|
fflush(stdout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nread = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 02:31:50 -04:00
|
|
|
if (nread > 0) {
|
|
|
|
_readbuffer.write((uint8_t *)buf, nread);
|
2016-01-10 02:17:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CONFIG_HAL_BOARD
|
|
|
|
|