From e7a5945056637ccb9a2aa89e28b3c94f3a056eae Mon Sep 17 00:00:00 2001 From: Staroselskii Georgii Date: Thu, 7 Jul 2016 18:46:12 +0300 Subject: [PATCH] AP_HAL_Linux: make flow_control a property of SerialDevice rather than UARTDriver's Make it possible to change flow control settings for individual devices. --- libraries/AP_HAL_Linux/SerialDevice.h | 7 +++++++ libraries/AP_HAL_Linux/UARTDevice.cpp | 21 +++++++++++++++++++++ libraries/AP_HAL_Linux/UARTDevice.h | 6 ++++++ libraries/AP_HAL_Linux/UARTDriver.cpp | 9 +-------- libraries/AP_HAL_Linux/UARTDriver.h | 11 +++++++++-- 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/libraries/AP_HAL_Linux/SerialDevice.h b/libraries/AP_HAL_Linux/SerialDevice.h index 21e7007725..23117bef5c 100644 --- a/libraries/AP_HAL_Linux/SerialDevice.h +++ b/libraries/AP_HAL_Linux/SerialDevice.h @@ -3,6 +3,8 @@ #include #include +#include "AP_HAL_Linux.h" + class SerialDevice { public: virtual ~SerialDevice() {} @@ -13,4 +15,9 @@ public: virtual ssize_t read(uint8_t *buf, uint16_t n) = 0; virtual void set_blocking(bool blocking) = 0; virtual void set_speed(uint32_t speed) = 0; + virtual AP_HAL::UARTDriver::flow_control get_flow_control(void) { return AP_HAL::UARTDriver::FLOW_CONTROL_ENABLE; } + virtual void set_flow_control(AP_HAL::UARTDriver::flow_control flow_control_setting) + { + /* most devices simply igmore this setting */ + }; }; diff --git a/libraries/AP_HAL_Linux/UARTDevice.cpp b/libraries/AP_HAL_Linux/UARTDevice.cpp index 0af925ec46..20d22f2ee9 100644 --- a/libraries/AP_HAL_Linux/UARTDevice.cpp +++ b/libraries/AP_HAL_Linux/UARTDevice.cpp @@ -109,3 +109,24 @@ void UARTDevice::set_speed(uint32_t baudrate) cfsetspeed(&t, baudrate); tcsetattr(_fd, TCSANOW, &t); } + +void UARTDevice::set_flow_control(AP_HAL::UARTDriver::flow_control flow_control_setting) +{ + struct termios t; + + if (_flow_control == flow_control_setting) { + return; + } + + tcgetattr(_fd, &t); + + if (flow_control_setting != AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE) { + t.c_cflag |= CRTSCTS; + } else { + t.c_cflag &= ~CRTSCTS; + } + + tcsetattr(_fd, TCSANOW, &t); + + _flow_control = flow_control_setting; +} diff --git a/libraries/AP_HAL_Linux/UARTDevice.h b/libraries/AP_HAL_Linux/UARTDevice.h index 53f9482e74..abb1d76dfc 100644 --- a/libraries/AP_HAL_Linux/UARTDevice.h +++ b/libraries/AP_HAL_Linux/UARTDevice.h @@ -14,9 +14,15 @@ public: virtual ssize_t read(uint8_t *buf, uint16_t n) override; virtual void set_blocking(bool blocking) override; virtual void set_speed(uint32_t speed) override; + virtual void set_flow_control(enum AP_HAL::UARTDriver::flow_control flow_control_setting) override; + virtual AP_HAL::UARTDriver::flow_control get_flow_control(void) override + { + return _flow_control; + } private: void _disable_crlf(); + AP_HAL::UARTDriver::flow_control _flow_control = AP_HAL::UARTDriver::flow_control::FLOW_CONTROL_DISABLE; int _fd = -1; const char *_device_path; diff --git a/libraries/AP_HAL_Linux/UARTDriver.cpp b/libraries/AP_HAL_Linux/UARTDriver.cpp index 66a8e5efc0..ba5afac6b0 100644 --- a/libraries/AP_HAL_Linux/UARTDriver.cpp +++ b/libraries/AP_HAL_Linux/UARTDriver.cpp @@ -35,8 +35,7 @@ using namespace Linux; UARTDriver::UARTDriver(bool default_console) : device_path(NULL), - _packetise(false), - _flow_control(FLOW_CONTROL_DISABLE) + _packetise(false) { if (default_console) { _console = true; @@ -74,21 +73,18 @@ void UARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS) case DEVICE_TCP: { _tcp_start_connection(); - _flow_control = FLOW_CONTROL_ENABLE; break; } case DEVICE_UDP: { _udp_start_connection(); - _flow_control = FLOW_CONTROL_ENABLE; break; } case DEVICE_UDPIN: { _udpin_start_connection(); - _flow_control = FLOW_CONTROL_ENABLE; break; } @@ -96,7 +92,6 @@ void UARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS) case DEVICE_QFLIGHT: { _qflight_start_connection(); - _flow_control = FLOW_CONTROL_DISABLE; break; } #endif @@ -275,7 +270,6 @@ bool UARTDriver::_serial_start_connection() _device = new UARTDevice(device_path); _connected = _device->open(); _device->set_blocking(false); - _flow_control = FLOW_CONTROL_DISABLE; return true; } @@ -285,7 +279,6 @@ bool UARTDriver::_qflight_start_connection() { _device = new QFLIGHTDevice(device_path); _connected = _device->open(); - _flow_control = FLOW_CONTROL_DISABLE; return true; } diff --git a/libraries/AP_HAL_Linux/UARTDriver.h b/libraries/AP_HAL_Linux/UARTDriver.h index 0339712184..a7b090ce38 100644 --- a/libraries/AP_HAL_Linux/UARTDriver.h +++ b/libraries/AP_HAL_Linux/UARTDriver.h @@ -36,7 +36,15 @@ public: bool _write_pending_bytes(void); virtual void _timer_tick(void); - enum flow_control get_flow_control(void) { return _flow_control; } + virtual enum flow_control get_flow_control(void) override + { + return _device->get_flow_control(); + } + + virtual void set_flow_control(enum flow_control flow_control_setting) override + { + _device->set_flow_control(flow_control_setting); + } private: AP_HAL::OwnPtr _device; @@ -48,7 +56,6 @@ private: char *_flag; bool _connected; // true if a client has connected bool _packetise; // true if writes should try to be on mavlink boundaries - enum flow_control _flow_control; void _allocate_buffers(uint16_t rxS, uint16_t txS); void _deallocate_buffers();