Line data Source code
1 : #include <errno.h> 2 : #include <fcntl.h> 3 : #include <stdio.h> 4 : #include <unistd.h> 5 : #include <signal.h> 6 : 7 : #include <AP_HAL/AP_HAL.h> 8 : 9 : #include "ConsoleDevice.h" 10 : 11 : #include <AP_Vehicle/AP_Vehicle_Type.h> 12 : 13 480 : ConsoleDevice::ConsoleDevice() 14 : { 15 480 : } 16 : 17 960 : ConsoleDevice::~ConsoleDevice() 18 : { 19 480 : close(); 20 960 : } 21 : 22 480 : bool ConsoleDevice::close() 23 : { 24 480 : _closed = true; 25 : 26 480 : return true; 27 : } 28 : 29 0 : bool ConsoleDevice::open() 30 : { 31 : #if APM_BUILD_TYPE(APM_BUILD_Replay) 32 : // we do not want a ConsoleDevice for Replay or any examples. It 33 : // screws up the terminal settings and creates all sorts of 34 : // weirdnesses in Replay's output. 35 : return false; 36 : #endif 37 : 38 0 : _rd_fd = STDIN_FILENO; 39 0 : _wr_fd = STDOUT_FILENO; 40 : 41 0 : _closed = false; 42 : 43 0 : if (!_set_signal_handlers()) { 44 0 : close(); 45 0 : return false; 46 : } 47 : 48 0 : return true; 49 : } 50 : 51 0 : bool ConsoleDevice::_set_signal_handlers(void) const 52 : { 53 0 : struct sigaction sa {}; 54 0 : sigemptyset(&sa.sa_mask); 55 0 : sa.sa_handler = SIG_IGN; 56 : 57 0 : return (sigaction(SIGTTIN, &sa, nullptr) == 0); 58 : 59 : } 60 : 61 0 : ssize_t ConsoleDevice::read(uint8_t *buf, uint16_t n) 62 : { 63 0 : if (_closed) { 64 0 : return -EAGAIN; 65 : } 66 : 67 0 : return ::read(_rd_fd, buf, n); 68 : } 69 : 70 0 : ssize_t ConsoleDevice::write(const uint8_t *buf, uint16_t n) 71 : { 72 0 : if (_closed) { 73 0 : return -EAGAIN; 74 : } 75 : 76 0 : return ::write(_wr_fd, buf, n); 77 : } 78 : 79 0 : void ConsoleDevice::set_blocking(bool blocking) 80 : { 81 : int rd_flags; 82 : int wr_flags; 83 : 84 0 : rd_flags = fcntl(_rd_fd, F_GETFL, 0); 85 0 : wr_flags = fcntl(_wr_fd, F_GETFL, 0); 86 : 87 0 : if (blocking) { 88 0 : rd_flags = rd_flags & ~O_NONBLOCK; 89 0 : wr_flags = wr_flags & ~O_NONBLOCK; 90 : } else { 91 0 : rd_flags = rd_flags | O_NONBLOCK; 92 0 : wr_flags = wr_flags | O_NONBLOCK; 93 : } 94 : 95 0 : if (fcntl(_rd_fd, F_SETFL, rd_flags) < 0) { 96 0 : ::fprintf(stderr, "Failed to set Console nonblocking %s\n", strerror(errno)); 97 : } 98 : 99 0 : if (fcntl(_wr_fd, F_SETFL, wr_flags) < 0) { 100 0 : ::fprintf(stderr, "Failed to set Console nonblocking %s\n",strerror(errno)); 101 : } 102 : 103 0 : } 104 : 105 0 : void ConsoleDevice::set_speed(uint32_t baudrate) 106 : { 107 0 : }