Skip to content

Commit dc362a1

Browse files
e220: write waits without the shared Poll(); test comment
SerialPortUart::Write called SerialPort::Poll(true, ...) from the TX thread while the RX thread waits in Poll(false, ...), but Poll() is meant for one thread at a time (shared stop eventfd: Stop was delayed by the read slice). Write() now retries TryWrite() every 1 ms until its 1 s deadline; a closed or lost port still ends it at once. Tests: a full output buffer does not disturb the RX wait, and completes when the peer drains it. Also state in a comment that ParityFromConfigIsAppliedWhenReopening covers the driver API, not the JSON path (the config parser accepts 8N1 only).
1 parent deb6b73 commit dc362a1

4 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎core/radio/e220/serial_port_uart.cpp‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
*/
66
#include "core/radio/e220/serial_port_uart.hpp"
77

8+
#include <thread> // NOLINT
9+
810
namespace srp {
911
namespace core {
1012
namespace radio {
1113
namespace e220 {
1214
namespace {
1315
constexpr std::uint32_t kWriteTimeoutMs = 1000;
16+
constexpr std::chrono::milliseconds kWriteRetryPause{1}; // about one byte at 9600 baud
1417

1518
/// Speed in bit/s for a termios constant; 0 = not an E220 UART speed.
1619
std::uint32_t BaudBps(speed_t speed) {
@@ -71,16 +74,12 @@ core::ErrorCode SerialPortUart::Write(const std::vector<uint8_t>& data) {
7174
if (r.status != serial::IoStatus::kTimeout) {
7275
return core::ErrorCode::kConnectionError;
7376
}
74-
// Output buffer full: wait for room until the write deadline.
75-
const auto now = std::chrono::steady_clock::now(); // NOLINT
76-
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now);
77-
if (left.count() <= 0) {
77+
// Output buffer full: retry until the write deadline. Not SerialPort::Poll(): the RX thread sits in
78+
// Poll() (WaitForData) and Poll() is for one thread at a time (shared stop eventfd).
79+
if (std::chrono::steady_clock::now() >= deadline) { // NOLINT
7880
return core::ErrorCode::kError;
7981
}
80-
const auto p = port_.Poll(true, static_cast<std::uint32_t>(left.count()), {});
81-
if (p.status == serial::IoStatus::kClosed || p.status == serial::IoStatus::kError) {
82-
return core::ErrorCode::kConnectionError;
83-
}
82+
std::this_thread::sleep_for(kWriteRetryPause);
8483
}
8584
return core::ErrorCode::kOk;
8685
}

‎core/radio/e220/serial_port_uart.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*
99
* Limits of SerialPort: 8N1 only (Open() with another parity returns false) and the port is opened
1010
* exclusively (TIOCEXCL + flock), so a second user of the same device fails to open it.
11+
*
12+
* Threads: WaitForData() (RX thread) uses SerialPort::Poll(), which is for one thread at a time, so
13+
* Write() (TX thread) never calls Poll(): when the output buffer is full it retries TryWrite() every
14+
* 1 ms until its 1 s deadline (kError), a lost or closed port ends it at once (kConnectionError).
1115
*/
1216
#ifndef CORE_RADIO_E220_SERIAL_PORT_UART_HPP_
1317
#define CORE_RADIO_E220_SERIAL_PORT_UART_HPP_

‎core/radio/e220/ut/test_e220_driver.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ TEST_F(E220Test, Baud9600DoesNotReopenTheUart) {
203203
EXPECT_EQ(module_->uart_opens.size(), 1U);
204204
}
205205

206+
// Covers the driver API (E220Config.parity -> IUartDriver::Open), not the JSON path: the config parser
207+
// accepts 8N1 only, so no model produces a non-default parity yet.
206208
TEST_F(E220Test, ParityFromConfigIsAppliedWhenReopening) {
207209
E220Config cfg;
208210
cfg.parity = UartParity::k8E1;

‎core/radio/e220/ut/test_e220_io.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include <termios.h>
1212
#include <unistd.h>
1313

14+
#include <atomic>
1415
#include <chrono> // NOLINT
1516
#include <cstdlib>
1617
#include <fstream>
18+
#include <stop_token> // NOLINT
1719
#include <string>
1820
#include <thread> // NOLINT
1921
#include <tuple>
@@ -108,6 +110,61 @@ TEST(SerialPortUart, WaitForDataTimesOutAndStopTokenWakesIt) {
108110
EXPECT_LT(std::chrono::steady_clock::now() - t0, milliseconds(1000));
109111
}
110112

113+
// TX thread writes into a full output buffer while the RX thread waits for data: the write must not
114+
// share SerialPort::Poll() with the wait (one thread at a time), so Stop reaches the RX wait at once.
115+
TEST(SerialPortUart, WriteToAFullBufferDoesNotDisturbTheReadWait) {
116+
Pty pty;
117+
SerialPortUart u;
118+
ASSERT_TRUE(u.Open(pty.path, uart::UartConfig::Mavlink(57600)));
119+
std::stop_source stop;
120+
std::atomic<bool> rx_done{false};
121+
std::thread rx([&] {
122+
u.WaitForData(milliseconds(10000), stop.get_token());
123+
rx_done = true;
124+
});
125+
// Nobody reads the master side: the pty buffer fills and Write() gives up at its 1 s deadline.
126+
const auto t0 = std::chrono::steady_clock::now();
127+
EXPECT_EQ(u.Write(std::vector<std::uint8_t>(512 * 1024, 0x55)), core::ErrorCode::kError);
128+
const auto write_time = std::chrono::steady_clock::now() - t0;
129+
EXPECT_GE(write_time, milliseconds(900));
130+
EXPECT_LT(write_time, milliseconds(3000));
131+
EXPECT_FALSE(rx_done); // the wait was not woken by the blocked Write()
132+
const auto t1 = std::chrono::steady_clock::now();
133+
stop.request_stop();
134+
rx.join();
135+
EXPECT_TRUE(rx_done);
136+
EXPECT_LT(std::chrono::steady_clock::now() - t1, milliseconds(200));
137+
}
138+
139+
TEST(SerialPortUart, WriteToAFullBufferCompletesWhenThePeerDrains) {
140+
Pty pty;
141+
SerialPortUart u;
142+
ASSERT_TRUE(u.Open(pty.path, uart::UartConfig::Mavlink(57600)));
143+
ASSERT_EQ(::fcntl(pty.master, F_SETFL, O_NONBLOCK), 0);
144+
const std::size_t total = 256 * 1024; // more than the pty buffer: Write() has to wait for room
145+
std::atomic<std::size_t> drained{0};
146+
std::atomic<bool> done{false};
147+
std::thread peer([&] {
148+
std::uint8_t buf[4096];
149+
while (!done) {
150+
const ssize_t n = ::read(pty.master, buf, sizeof(buf));
151+
if (n > 0) {
152+
drained += static_cast<std::size_t>(n);
153+
} else {
154+
std::this_thread::sleep_for(milliseconds(2));
155+
}
156+
}
157+
});
158+
EXPECT_EQ(u.Write(std::vector<std::uint8_t>(total, 0x55)), core::ErrorCode::kOk);
159+
const auto limit = std::chrono::steady_clock::now() + milliseconds(2000);
160+
while (drained < total && std::chrono::steady_clock::now() < limit) {
161+
std::this_thread::sleep_for(milliseconds(5));
162+
}
163+
done = true;
164+
peer.join();
165+
EXPECT_EQ(drained, total);
166+
}
167+
111168
TEST(SerialPortUart, ClosedPortAndLostDeviceAreReportedAsErrors) {
112169
SerialPortUart u;
113170
EXPECT_FALSE(u.Read(8).has_value());

0 commit comments

Comments
 (0)