]> sigrok.org Git - libsigrok.git/blobdiff - src/sw_limits.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / sw_limits.c
index 6e1c4c141018392b6067f5d661f4fdd72adcdf6d..41052d0f8f54ad6b43e74cad09c08f2b42782077 100644 (file)
 /**
  * @file
  * Software limits helper functions
- * @internal
  */
 
-#include <config.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
+#include "config.h"
+
 #include <ctype.h>
 #include <libsigrok/libsigrok.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "sw_limits"
@@ -44,9 +45,7 @@
  */
 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
 {
-       limits->limit_samples = 0;
-       limits->limit_frames = 0;
-       limits->limit_msec = 0;
+       memset(limits, 0, sizeof(*limits));
 }
 
 /**
@@ -58,10 +57,11 @@ SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
  * @param limits software limit instance
  * @param key config item key
  * @param data config item data
+ *
  * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
  */
-SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
-       GVariant **data)
+SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits,
+       uint32_t key, GVariant **data)
 {
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
@@ -89,10 +89,11 @@ SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
  * @param limits software limit instance
  * @param key config item key
  * @param data config item data
+ *
  * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
  */
-SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
-       GVariant *data)
+SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits,
+       uint32_t key, GVariant *data)
 {
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
@@ -133,6 +134,7 @@ SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
  * processing has been done.
  *
  * @param limits software limits instance
+ *
  * @returns TRUE if any of the software limits has been reached and the driver
  *               should stop data acquisition, otherwise FALSE.
  */
@@ -154,7 +156,7 @@ SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
                }
        }
 
-       if (limits->limit_msec) {
+       if (limits->limit_msec && limits->start_time) {
                guint64 now;
                now = g_get_monotonic_time();
                if (now > limits->start_time &&
@@ -168,6 +170,88 @@ SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
        return FALSE;
 }
 
+/**
+ * Get remaining counts until software limits are reached.
+ *
+ * This routine fills in those C language variables which callers
+ * requested, and provides the remaining value until a specified limit
+ * would be reached.
+ *
+ * The @ref sr_sw_limits_config_get() routine is suitable for rare
+ * configuration calls and interfaces nicely with Glib data types. The
+ * @ref sr_sw_limits_check() routine only provides a weak "exceeded"
+ * result. This @ref sr_sw_limits_get_remain() routine is suitable for
+ * additional checks and more eager limits enforcement in (potentially
+ * tight) acquisition code paths. Hardware compression may result in
+ * rather large "overshoots" when checks are done only late.
+ *
+ * @param[in] limits software limit instance
+ * @param[out] samples remaining samples count until the limit is reached
+ * @param[out] frames remaining frames count until the limit is reached
+ * @param[out] msecs remaining milliseconds until the limit is reached
+ * @param[out] exceeded whether configured limits were reached before
+ *
+ * @return SR_ERR_* upon error, SR_OK otherwise
+ */
+SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
+       uint64_t *samples, uint64_t *frames, uint64_t *msecs,
+       gboolean *exceeded)
+{
+
+       if (!limits)
+               return SR_ERR_ARG;
+
+       if (exceeded)
+               *exceeded = FALSE;
+
+       if (samples) do {
+               *samples = 0;
+               if (!limits->limit_samples)
+                       break;
+               if (limits->samples_read >= limits->limit_samples) {
+                       if (exceeded)
+                               *exceeded = TRUE;
+                       break;
+               }
+               *samples = limits->limit_samples - limits->samples_read;
+       } while (0);
+
+       if (frames) do {
+               *frames = 0;
+               if (!limits->limit_frames)
+                       break;
+               if (limits->frames_read >= limits->limit_frames) {
+                       if (exceeded)
+                               *exceeded = TRUE;
+                       break;
+               }
+               *frames = limits->limit_frames - limits->frames_read;
+       } while (0);
+
+       if (msecs) do {
+               guint64 now, elapsed, remain;
+
+               *msecs = 0;
+               if (!limits->limit_msec)
+                       break;
+               if (!limits->start_time)
+                       break;
+               now = g_get_monotonic_time();
+               if (now < limits->start_time)
+                       break;
+               elapsed = now - limits->start_time;
+               if (elapsed >= limits->limit_msec) {
+                       if (exceeded)
+                               *exceeded = TRUE;
+                       break;
+               }
+               remain = limits->limit_msec - elapsed;
+               *msecs = remain / 1000;
+       } while (0);
+
+       return SR_OK;
+}
+
 /**
  * Update the amount of samples that have been read
  *