2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Lars-Peter Clausen <lars@metafoo.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * Software limits helper functions
30 #include <libsigrok/libsigrok.h>
31 #include "libsigrok-internal.h"
33 #define LOG_PREFIX "sw_limits"
36 * Initialize a software limit instance
38 * Must be called before any other operations are performed on a struct
39 * sr_sw_limits and should typically be called after the data structure has been
42 * @param limits the software limit instance to initialize
44 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
46 memset(limits, 0, sizeof(*limits));
50 * Get software limit configuration
52 * Retrieve the currently configured software limit for the specified key.
53 * Should be called from the drivers config_get() callback.
55 * @param limits software limit instance
56 * @param key config item key
57 * @param data config item data
58 * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
60 SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
64 case SR_CONF_LIMIT_SAMPLES:
65 *data = g_variant_new_uint64(limits->limit_samples);
67 case SR_CONF_LIMIT_FRAMES:
68 *data = g_variant_new_uint64(limits->limit_frames);
70 case SR_CONF_LIMIT_MSEC:
71 *data = g_variant_new_uint64(limits->limit_msec / 1000);
81 * Set software limit configuration
83 * Configure software limit for the specified key. Should be called from the
84 * drivers config_set() callback.
86 * @param limits software limit instance
87 * @param key config item key
88 * @param data config item data
89 * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
91 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
95 case SR_CONF_LIMIT_SAMPLES:
96 limits->limit_samples = g_variant_get_uint64(data);
98 case SR_CONF_LIMIT_FRAMES:
99 limits->limit_frames = g_variant_get_uint64(data);
101 case SR_CONF_LIMIT_MSEC:
102 limits->limit_msec = g_variant_get_uint64(data) * 1000;
112 * Start a new data acquisition session
114 * Resets the internal accounting for all software limits. Usually should be
115 * called from the drivers acquisition_start() callback.
117 * @param limits software limits instance
119 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
121 limits->samples_read = 0;
122 limits->frames_read = 0;
123 limits->start_time = g_get_monotonic_time();
127 * Check if any of the configured software limits has been reached
129 * Usually should be called at the end of the drivers work function after all
130 * processing has been done.
132 * @param limits software limits instance
133 * @returns TRUE if any of the software limits has been reached and the driver
134 * should stop data acquisition, otherwise FALSE.
136 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
138 if (limits->limit_samples) {
139 if (limits->samples_read >= limits->limit_samples) {
140 sr_dbg("Requested number of samples (%" PRIu64
141 ") reached.", limits->limit_samples);
146 if (limits->limit_frames) {
147 if (limits->frames_read >= limits->limit_frames) {
148 sr_dbg("Requested number of frames (%" PRIu64
149 ") reached.", limits->limit_frames);
154 if (limits->limit_msec && limits->start_time) {
156 now = g_get_monotonic_time();
157 if (now > limits->start_time &&
158 now - limits->start_time > limits->limit_msec) {
159 sr_dbg("Requested sampling time (%" PRIu64
160 "ms) reached.", limits->limit_msec / 1000);
169 * Update the amount of samples that have been read
171 * Update the amount of samples that have been read in the current data
172 * acquisition run. For each invocation @p samples_read will be accumulated and
173 * once the configured sample limit has been reached sr_sw_limits_check() will
176 * @param limits software limits instance
177 * @param samples_read the amount of samples that have been read
179 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
180 uint64_t samples_read)
182 limits->samples_read += samples_read;
186 * Update the amount of frames that have been read
188 * Update the amount of frames that have been read in the current data
189 * acquisition run. For each invocation @p frames_read will be accumulated and
190 * once the configured frame limit has been reached sr_sw_limits_check() will
193 * @param limits software limits instance
194 * @param frames_read the amount of frames that have been read
196 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
197 uint64_t frames_read)
199 limits->frames_read += frames_read;