]> sigrok.org Git - libsigrok.git/commitdiff
beaglelogic: Add beaglelogic_tcp_drain function
authorKumar Abhishek <redacted>
Sat, 23 Sep 2017 05:39:14 +0000 (11:09 +0530)
committerUwe Hermann <redacted>
Tue, 26 Sep 2017 16:14:46 +0000 (18:14 +0200)
The function drains off all the remaining data in the receive socket
and is triggered before starting a capture and after a capture is
completed. In the absence of this function, there is a possibility of
data corruption and also the NodeJS TCP server throws an error if the
buffer is not completely read out before the socket is closed.

Signed-off-by: Kumar Abhishek <redacted>
src/hardware/beaglelogic/api.c
src/hardware/beaglelogic/beaglelogic.h
src/hardware/beaglelogic/beaglelogic_tcp.c
src/hardware/beaglelogic/protocol.c

index 2d21f46d06e4afba451d4c40c3861850aab557e3..b933965dab0470882dae4224088ea45ccbca8450 100644 (file)
@@ -358,9 +358,11 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
        /* Execute a stop on BeagleLogic */
        devc->beaglelogic->stop(devc);
 
-       /* lseek to offset 0, flushes the cache */
+       /* Flush the cache */
        if (devc->beaglelogic == &beaglelogic_native_ops)
                lseek(devc->fd, 0, SEEK_SET);
+       else
+               beaglelogic_tcp_drain(devc);
 
        /* Remove session source and send EOT packet */
        sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
index 3ee0fd693f3e0665f2aa09468839811821660425..8dba337caba58346faacacc99cef407a84208f96 100644 (file)
@@ -127,5 +127,6 @@ SR_PRIV extern const struct beaglelogic_ops beaglelogic_native_ops;
 SR_PRIV extern const struct beaglelogic_ops beaglelogic_tcp_ops;
 
 SR_PRIV int beaglelogic_tcp_detect(struct dev_context *devc);
+SR_PRIV int beaglelogic_tcp_drain(struct dev_context *devc);
 
 #endif
index 8baf4eef055702dd23bc3e43b9fd8c2c89eaf534..2c303c976c1bef216e2f31cac7642a91eb3fe4f6 100644 (file)
@@ -132,6 +132,32 @@ static int beaglelogic_tcp_read_data(struct dev_context *devc, char *buf,
        return len;
 }
 
+SR_PRIV int beaglelogic_tcp_drain(struct dev_context *devc) {
+       char *buf = g_malloc(1024);
+       fd_set rset;
+       int ret, len = 0;
+       struct timeval tv;
+
+       FD_ZERO(&rset);
+       FD_SET(devc->socket, &rset);
+
+       /* 25ms timeout */
+       tv.tv_sec = 0;
+       tv.tv_usec = 25 * 1000;
+
+       do {
+               ret = select(devc->socket + 1, &rset, NULL, NULL, &tv);
+               if (ret > 0) {
+                       len += beaglelogic_tcp_read_data(devc, buf, 1024);
+               }
+       } while (ret > 0);
+
+       sr_spew("Drained %d bytes of data.", len);
+
+       g_free(buf);
+       return SR_OK;
+}
+
 static int beaglelogic_tcp_get_string(struct dev_context *devc, const char *cmd,
                char **tcp_resp) {
        GString *response = g_string_sized_new(1024);
@@ -314,6 +340,7 @@ static int beaglelogic_get_lasterror(struct dev_context *devc) {
 }
 
 static int beaglelogic_start(struct dev_context *devc) {
+       beaglelogic_tcp_drain(devc);
        return beaglelogic_tcp_send_cmd(devc, "get");
 }
 
index 2787b9bac9ef56f7c043ab2974a085d45fc63819..8e955e1662a3922fb76c2fca08c6dec0ace10f35 100644 (file)
@@ -194,6 +194,10 @@ SR_PRIV int beaglelogic_tcp_receive_data(int fd, int revents, void *cb_data)
                /* Send EOA Packet, stop polling */
                std_session_send_df_end(sdi);
                devc->beaglelogic->stop(devc);
+
+               /* Drain the receive buffer */
+               beaglelogic_tcp_drain(devc);
+
                sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
        }