Name of user not set #1005208 has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/79112?usp=email )
Change subject: serial: Fix sp_flush_incoming for serprog TCP connections ......................................................................
serial: Fix sp_flush_incoming for serprog TCP connections
I was working on an esp32 serprog-compatible SPI programmer pet project, and when i implemented the TCP over Wi-Fi for serprog, i discovered that sp_flush_incoming() silently fails if the underlying sp_fd descriptor is a TCP socket.
I added a check for this case - tcflush returns ENOTTY, meaning tcflush is not supported for not terminal objects, in this case a fallback serialport_read_nonblock loop is used.
After that i was able to communicate with my serprog programmer implementation over TCP with 100% success rate.
Signed-off-by: Stanislav Ponomarev me@stasponomarev.com
Change-Id: I9724a2fcd4a41dede2c15f83877efa6c3b0b7fae --- M serial.c 1 file changed, 19 insertions(+), 2 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/12/79112/1
diff --git a/serial.c b/serial.c index 10d739a..f9bd280 100644 --- a/serial.c +++ b/serial.c @@ -377,8 +377,25 @@ #if IS_WINDOWS PurgeComm(sp_fd, PURGE_RXCLEAR); #else - /* FIXME: error handling */ - tcflush(sp_fd, TCIFLUSH); + int ret = tcflush(sp_fd, TCIFLUSH); + if (ret == 0) + return; + + // TCP socket case: sp_fd is not a terminal descriptor - tcflush is not supported + if (errno == ENOTTY) + { + unsigned char c; + + while ((ret = serialport_read_nonblock(&c, 1, 1, NULL)) == 0); + + if (ret > 0) // no more data available + return; + + msg_perr("Could not flush serial port incoming buffer: read has failed"); + return; + } + + msg_perr_strerror("Could not flush serial port incoming buffer: "); #endif return; }