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.
This commit is contained in:
Gerhard Sittig 2018-06-03 12:17:09 +02:00 committed by Uwe Hermann
parent ae4c1fb637
commit 8c3df6e5cd
3 changed files with 31 additions and 0 deletions

View File

@ -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_close(struct sr_serial_dev_inst *serial);
SR_PRIV int serial_flush(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 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, SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
const void *buf, size_t count, unsigned int timeout_ms); const void *buf, size_t count, unsigned int timeout_ms);
SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial, 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); uint16_t vendor_id, uint16_t product_id);
SR_PRIV int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial, SR_PRIV int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial,
int *baud, int *bits); int *baud, int *bits);
SR_PRIV size_t sr_ser_libsp_get_rx_avail(struct sr_serial_dev_inst *serial);
#endif #endif
/*--- ezusb.c ---------------------------------------------------------------*/ /*--- ezusb.c ---------------------------------------------------------------*/

View File

@ -152,6 +152,21 @@ SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial)
return sr_ser_libsp_drain(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, static int _serial_write(struct sr_serial_dev_inst *serial,
const void *buf, size_t count, const void *buf, size_t count,
int nonblocking, unsigned int timeout_ms) int nonblocking, unsigned int timeout_ms)

View File

@ -473,3 +473,17 @@ SR_PRIV int sr_ser_libsp_get_frame_format(struct sr_serial_dev_inst *serial,
return ret; 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;
}