From: Kumar Abhishek Date: Sat, 23 Sep 2017 05:39:14 +0000 (+0530) Subject: beaglelogic: Add beaglelogic_tcp_drain function X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=f7d7ee82dd1d88af807d4d8376d04a39baa53490;p=libsigrok.git beaglelogic: Add beaglelogic_tcp_drain function 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 --- diff --git a/src/hardware/beaglelogic/api.c b/src/hardware/beaglelogic/api.c index 2d21f46d..b933965d 100644 --- a/src/hardware/beaglelogic/api.c +++ b/src/hardware/beaglelogic/api.c @@ -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); diff --git a/src/hardware/beaglelogic/beaglelogic.h b/src/hardware/beaglelogic/beaglelogic.h index 3ee0fd69..8dba337c 100644 --- a/src/hardware/beaglelogic/beaglelogic.h +++ b/src/hardware/beaglelogic/beaglelogic.h @@ -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 diff --git a/src/hardware/beaglelogic/beaglelogic_tcp.c b/src/hardware/beaglelogic/beaglelogic_tcp.c index 8baf4eef..2c303c97 100644 --- a/src/hardware/beaglelogic/beaglelogic_tcp.c +++ b/src/hardware/beaglelogic/beaglelogic_tcp.c @@ -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"); } diff --git a/src/hardware/beaglelogic/protocol.c b/src/hardware/beaglelogic/protocol.c index 2787b9ba..8e955e16 100644 --- a/src/hardware/beaglelogic/protocol.c +++ b/src/hardware/beaglelogic/protocol.c @@ -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); }