From: Gerhard Sittig Date: Sun, 3 Jun 2018 10:17:09 +0000 (+0200) Subject: serial: introduce "has receive data" query X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=8c3df6e5cd3d23bf01807e6296f7b7a9f8475902;p=libsigrok.git serial: introduce "has receive data" query Add a serial_has_receive_data() routine to the serial layer's API which returns the number of (known to be) available RX data bytes. Implement support in the libserialport specific code. --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 2b62260f..a6497c95 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -1079,6 +1079,7 @@ SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags); SR_PRIV int serial_close(struct sr_serial_dev_inst *serial); SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial); SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial); +SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial); SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial, const void *buf, size_t count, unsigned int timeout_ms); SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, @@ -1132,6 +1133,7 @@ SR_PRIV GSList *sr_ser_libsp_find_usb(GSList *list, sr_ser_find_append_t append, uint16_t vendor_id, uint16_t product_id); SR_PRIV int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial, int *baud, int *bits); +SR_PRIV size_t sr_ser_libsp_get_rx_avail(struct sr_serial_dev_inst *serial); #endif /*--- ezusb.c ---------------------------------------------------------------*/ diff --git a/src/serial.c b/src/serial.c index 3b1f4264..ef156382 100644 --- a/src/serial.c +++ b/src/serial.c @@ -152,6 +152,21 @@ SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial) return sr_ser_libsp_drain(serial); } +/** + * Check for available receive data. + * + * @param[in] serial Previously opened serial port instance. + * + * @returns The number of (known) available RX data bytes. + * + * Returns 0 if no receive data is available, or if the amount of + * available receive data cannot get determined. + */ +SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial) +{ + return sr_ser_libsp_get_rx_avail(serial); +} + static int _serial_write(struct sr_serial_dev_inst *serial, const void *buf, size_t count, int nonblocking, unsigned int timeout_ms) diff --git a/src/serial_libsp.c b/src/serial_libsp.c index 0ceb8b0d..69a9cc1e 100644 --- a/src/serial_libsp.c +++ b/src/serial_libsp.c @@ -473,3 +473,17 @@ SR_PRIV int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial, return ret; } + +SR_PRIV size_t sr_ser_libsp_get_rx_avail(struct sr_serial_dev_inst *serial) +{ + int rc; + + if (!serial) + return 0; + + rc = sp_input_waiting(serial->sp_data); + if (rc < 0) + return 0; + + return rc; +}