]> sigrok.org Git - libsigrok.git/commitdiff
serial: make failed flush() in open() non-fatal, CP2110 flush() return
authorGerhard Sittig <redacted>
Sat, 1 Aug 2020 12:55:20 +0000 (14:55 +0200)
committerGerhard Sittig <redacted>
Sat, 1 Aug 2020 12:55:20 +0000 (14:55 +0200)
Commit cb828f1b3e00 introduced an unconditional flush() call in the
open() routine's body, and passed its return value in verbatim form to
open() callers. Some of the transports/cables for serial communication
yield the SR_ERR_NA return value. Consider this a non-fatal condition.

Unbreak the CP2110 HID chip's flush() implementation. Don't expect a
return value of 0 from HID write calls, instead expect to see the number
of written bytes for successful calls.

This was tested with ch9325 (UT-D04), cp2110 (UT-D09) and bu86x.

src/serial.c
src/serial_hid_cp2110.c

index fe39effc604f10119dcc822d4b0281557697115a..7f1f1eeaa2651360194231cfc5793fd4ad40141f 100644 (file)
@@ -128,7 +128,18 @@ SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
                        return ret;
        }
 
-       return serial_flush(serial);
+       /*
+        * Flush potentially dangling RX data. Availability of the
+        * flush primitive depends on the transport/cable, absense
+        * is non-fatal.
+        */
+       ret = serial_flush(serial);
+       if (ret == SR_ERR_NA)
+               ret = SR_OK;
+       if (ret != SR_OK)
+               return ret;
+
+       return SR_OK;
 }
 
 /**
index 72a7aa98d3d653fe3e7727b4e224d83f259c9fcc..bdc58664e81db3ce6e967be7a340f02c0242c8a5 100644 (file)
@@ -280,7 +280,7 @@ static int cp2110_flush(struct sr_serial_dev_inst *serial)
        buffer[0] = CP2110_FIFO_PURGE;
        buffer[1] = CP2110_FIFO_PURGE_TX | CP2110_FIFO_PURGE_RX;
        rc = ser_hid_hidapi_set_data(serial, 0, buffer, sizeof(buffer), 0);
-       if (rc != 0)
+       if (rc != sizeof(buffer))
                return SR_ERR;
        return SR_OK;
 }