]> sigrok.org Git - libsigrok.git/commitdiff
serial: introduce "has receive data" query
authorGerhard Sittig <redacted>
Sun, 3 Jun 2018 10:17:09 +0000 (12:17 +0200)
committerUwe Hermann <redacted>
Sun, 2 Jun 2019 18:39:02 +0000 (20:39 +0200)
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.

src/libsigrok-internal.h
src/serial.c
src/serial_libsp.c

index 2b62260f4b33d9a1cd1b9d0cd6f19c4a101be832..a6497c958ac86b725c753f8f182aec562d53bd9d 100644 (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_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 ---------------------------------------------------------------*/
index 3b1f4264ce714eee277d2bcabf7f17fe401e7585..ef1563829eb13d54cd8492fbc853fa1415a1ec8e 100644 (file)
@@ -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)
index 0ceb8b0d8ff291def4983180d3290e972a21e772..69a9cc1ef7fbea2a6d8620d9a6a1ee516f9850b0 100644 (file)
@@ -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;
+}