From: Gerhard Sittig Date: Wed, 15 Mar 2023 16:24:44 +0000 (+0100) Subject: rdtech-um: concentrate poll period handling in one spot X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c1f4f5894782580e78c47b68b9e2d0da2723e829;p=libsigrok.git rdtech-um: concentrate poll period handling in one spot The handling of "command sent at ..." and the emission of periodic poll requests is non-trivial, and was scattered around different locations. Concentrate all of the time scaling and delta calculation and updating internal state into a single spot, and allow callers to force the first emission when desired (acquisition start needs this). Which eliminates repeated calls into the microseconds getter. Not advancing the last checkpoint, and keep sending requests until successful transmission, remains identical to the previous version of the implementation. In practise transmit errors are rare. --- diff --git a/src/hardware/rdtech-um/api.c b/src/hardware/rdtech-um/api.c index a6c5f697..863ba972 100644 --- a/src/hardware/rdtech-um/api.c +++ b/src/hardware/rdtech-um/api.c @@ -156,7 +156,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) serial_source_add(sdi->session, serial, G_IO_IN, 50, rdtech_um_receive_data, (void *)sdi); - return rdtech_um_poll(sdi); + return rdtech_um_poll(sdi, TRUE); } static struct sr_dev_driver rdtech_um_driver_info = { diff --git a/src/hardware/rdtech-um/protocol.c b/src/hardware/rdtech-um/protocol.c index 6c56e245..e2afc67d 100644 --- a/src/hardware/rdtech-um/protocol.c +++ b/src/hardware/rdtech-um/protocol.c @@ -147,12 +147,21 @@ SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_ins return p; } -SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi) +SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force) { struct dev_context *devc; + int64_t now, elapsed; struct sr_serial_dev_inst *serial; uint8_t request; + /* Check for expired intervals or forced requests. */ + devc = sdi->priv; + now = g_get_monotonic_time() / 1000; + elapsed = now - devc->cmd_sent_at; + if (!force && elapsed < UM_POLL_PERIOD_MS) + return SR_OK; + + /* Send another poll request. Update interval only on success. */ serial = sdi->conn; request = UM_CMD_POLL; if (serial_write_blocking(serial, &request, sizeof(request), @@ -160,9 +169,7 @@ SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi) sr_err("Unable to send poll request."); return SR_ERR; } - - devc = sdi->priv; - devc->cmd_sent_at = g_get_monotonic_time() / 1000; + devc->cmd_sent_at = now; return SR_OK; } @@ -228,7 +235,6 @@ SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data) struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_serial_dev_inst *serial; - int64_t now, elapsed; (void)fd; @@ -247,11 +253,7 @@ SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data) return TRUE; } - now = g_get_monotonic_time() / 1000; - elapsed = now - devc->cmd_sent_at; - - if (elapsed > UM_POLL_PERIOD_MS) - rdtech_um_poll(sdi); + (void)rdtech_um_poll(sdi, FALSE); return TRUE; } diff --git a/src/hardware/rdtech-um/protocol.h b/src/hardware/rdtech-um/protocol.h index 159031a7..dfe1cd75 100644 --- a/src/hardware/rdtech-um/protocol.h +++ b/src/hardware/rdtech-um/protocol.h @@ -51,6 +51,6 @@ struct dev_context { SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial); SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data); -SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi); +SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force); #endif