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 <mail@maciej.szmigiero.name>
This commit is contained in:
Maciej S. Szmigiero
2025-06-14 16:02:23 -05:00
committed by Dan Williams
parent 0787d04a39
commit adb6dc9b44
2 changed files with 53 additions and 0 deletions
+43
View File
@@ -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
+10
View File
@@ -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,