2016-05-17 23:26:57 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2016-07-13 10:56:37 -03:00
|
|
|
#include <signal.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-06-11 20:19:56 -03:00
|
|
|
|
|
|
|
#include "ConsoleDevice.h"
|
|
|
|
|
2020-03-12 00:23:01 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2015-06-11 20:19:56 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
ConsoleDevice::ConsoleDevice()
|
2015-06-11 20:19:56 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleDevice::~ConsoleDevice()
|
|
|
|
{
|
2016-07-07 14:56:12 -03:00
|
|
|
close();
|
2015-06-11 20:19:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleDevice::close()
|
|
|
|
{
|
|
|
|
_closed = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleDevice::open()
|
|
|
|
{
|
2020-05-27 23:19:46 -03:00
|
|
|
#if APM_BUILD_TYPE(APM_BUILD_Replay)
|
2020-03-12 00:23:01 -03:00
|
|
|
// we do not want a ConsoleDevice for Replay or any examples. It
|
|
|
|
// screws up the terminal settings and creates all sorts of
|
|
|
|
// weirdnesses in Replay's output.
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2015-06-11 20:19:56 -03:00
|
|
|
_rd_fd = STDIN_FILENO;
|
|
|
|
_wr_fd = STDOUT_FILENO;
|
|
|
|
|
|
|
|
_closed = false;
|
|
|
|
|
2016-07-13 10:56:37 -03:00
|
|
|
if (!_set_signal_handlers()) {
|
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:19:56 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-13 10:56:37 -03:00
|
|
|
bool ConsoleDevice::_set_signal_handlers(void) const
|
|
|
|
{
|
2018-11-26 02:10:25 -04:00
|
|
|
struct sigaction sa {};
|
2016-07-13 10:56:37 -03:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
|
|
|
|
return (sigaction(SIGTTIN, &sa, nullptr) == 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:19:56 -03:00
|
|
|
ssize_t ConsoleDevice::read(uint8_t *buf, uint16_t n)
|
|
|
|
{
|
|
|
|
if (_closed) {
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::read(_rd_fd, buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ConsoleDevice::write(const uint8_t *buf, uint16_t n)
|
|
|
|
{
|
|
|
|
if (_closed) {
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::write(_wr_fd, buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDevice::set_blocking(bool blocking)
|
|
|
|
{
|
|
|
|
int rd_flags;
|
|
|
|
int wr_flags;
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2015-06-11 20:19:56 -03:00
|
|
|
rd_flags = fcntl(_rd_fd, F_GETFL, 0);
|
|
|
|
wr_flags = fcntl(_wr_fd, F_GETFL, 0);
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2015-06-11 20:19:56 -03:00
|
|
|
if (blocking) {
|
|
|
|
rd_flags = rd_flags & ~O_NONBLOCK;
|
|
|
|
wr_flags = wr_flags & ~O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
rd_flags = rd_flags | O_NONBLOCK;
|
|
|
|
wr_flags = wr_flags | O_NONBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(_rd_fd, F_SETFL, rd_flags) < 0) {
|
|
|
|
::fprintf(stderr, "Failed to set Console nonblocking %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(_wr_fd, F_SETFL, wr_flags) < 0) {
|
|
|
|
::fprintf(stderr, "Failed to set Console nonblocking %s\n",strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDevice::set_speed(uint32_t baudrate)
|
|
|
|
{
|
|
|
|
}
|