From adb6dc9b44224136ccdd087f29f4796e0bfa54ed Mon Sep 17 00:00:00 2001 From: "Maciej S. Szmigiero" Date: Sat, 31 May 2025 13:08:01 +0200 Subject: [PATCH] port-serial: add flush operation Add a serial port flush operation which can be used to discard either serial port data received but not read, or serial port written but not transmitted, or both types of data. Signed-off-by: Maciej S. Szmigiero --- src/mm-port-serial.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/mm-port-serial.h | 10 ++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/mm-port-serial.c b/src/mm-port-serial.c index f520ff83..89687059 100644 --- a/src/mm-port-serial.c +++ b/src/mm-port-serial.c @@ -1865,6 +1865,49 @@ mm_port_serial_flash (MMPortSerial *self, ctx->flash_id = g_timeout_add (flash_time, (GSourceFunc)flash_do, self); } +/*****************************************************************************/ +/* Flush */ + +gboolean mm_port_serial_flush (MMPortSerial *self, + MMPortSerialFlushType type, + GError **error) +{ + int tcflush_type; + + g_return_val_if_fail (MM_IS_PORT_SERIAL (self), FALSE); + + if (!mm_port_serial_is_open (self)) { + g_set_error_literal (error, + MM_SERIAL_ERROR, + MM_SERIAL_ERROR_NOT_OPEN, + "The serial port is not open."); + return FALSE; + } + + if (type == MM_PORT_SERIAL_FLUSH_RX) { + tcflush_type = TCIFLUSH; + } else if (type == MM_PORT_SERIAL_FLUSH_TX) { + tcflush_type = TCOFLUSH; + } else if (type == MM_PORT_SERIAL_FLUSH_BOTH) { + tcflush_type = TCIOFLUSH; + } else { + g_assert_not_reached (); + return FALSE; + } + + g_assert (self->priv->fd >= 0); + if (tcflush (self->priv->fd, tcflush_type) != 0) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Failed to flush (%d) serial port, errno %d", + tcflush_type, errno); + return FALSE; + } + + return TRUE; +} + /*****************************************************************************/ gboolean diff --git a/src/mm-port-serial.h b/src/mm-port-serial.h index f050bdc3..706d1621 100644 --- a/src/mm-port-serial.h +++ b/src/mm-port-serial.h @@ -48,6 +48,12 @@ typedef enum { MM_PORT_SERIAL_RESPONSE_ERROR, } MMPortSerialResponseType; +typedef enum { + MM_PORT_SERIAL_FLUSH_RX, + MM_PORT_SERIAL_FLUSH_TX, + MM_PORT_SERIAL_FLUSH_BOTH, +} MMPortSerialFlushType; + typedef struct _MMPortSerial MMPortSerial; typedef struct _MMPortSerialClass MMPortSerialClass; typedef struct _MMPortSerialPrivate MMPortSerialPrivate; @@ -141,6 +147,10 @@ gboolean mm_port_serial_flash_finish (MMPortSerial *self, GError **error); void mm_port_serial_flash_cancel (MMPortSerial *self); +gboolean mm_port_serial_flush (MMPortSerial *self, + MMPortSerialFlushType type, + GError **error); + void mm_port_serial_command (MMPortSerial *self, GByteArray *command, guint32 timeout_seconds,