]> sigrok.org Git - libsigrok.git/blame - src/sw_limits.c
kingst-la2016: style nits, remove not needed include directives
[libsigrok.git] / src / sw_limits.c
CommitLineData
aea4e458
LPC
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 Lars-Peter Clausen <lars@metafoo.de>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
580b94e4
LPC
20/**
21 * @file
22 * Software limits helper functions
580b94e4
LPC
23 */
24
aea4e458
LPC
25#include <config.h>
26#include <stdio.h>
27#include <stdint.h>
28#include <string.h>
29#include <ctype.h>
30#include <libsigrok/libsigrok.h>
31#include "libsigrok-internal.h"
32
3ba944cf
UH
33#define LOG_PREFIX "sw_limits"
34
580b94e4
LPC
35/**
36 * Initialize a software limit instance
37 *
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
40 * allocated.
41 *
42 * @param limits the software limit instance to initialize
43 */
aea4e458
LPC
44SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
45{
17ed72cc 46 memset(limits, 0, sizeof(*limits));
aea4e458
LPC
47}
48
580b94e4
LPC
49/**
50 * Get software limit configuration
51 *
52 * Retrieve the currently configured software limit for the specified key.
53 * Should be called from the drivers config_get() callback.
54 *
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
59 */
d579755a 60SR_PRIV int sr_sw_limits_config_get(const struct sr_sw_limits *limits, uint32_t key,
aea4e458
LPC
61 GVariant **data)
62{
63 switch (key) {
64 case SR_CONF_LIMIT_SAMPLES:
65 *data = g_variant_new_uint64(limits->limit_samples);
66 break;
67785f25
GS
67 case SR_CONF_LIMIT_FRAMES:
68 *data = g_variant_new_uint64(limits->limit_frames);
69 break;
aea4e458
LPC
70 case SR_CONF_LIMIT_MSEC:
71 *data = g_variant_new_uint64(limits->limit_msec / 1000);
72 break;
73 default:
74 return SR_ERR_NA;
75 }
76
77 return SR_OK;
78}
79
580b94e4
LPC
80/**
81 * Set software limit configuration
82 *
83 * Configure software limit for the specified key. Should be called from the
84 * drivers config_set() callback.
85 *
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
90 */
aea4e458
LPC
91SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
92 GVariant *data)
93{
94 switch (key) {
95 case SR_CONF_LIMIT_SAMPLES:
96 limits->limit_samples = g_variant_get_uint64(data);
97 break;
67785f25
GS
98 case SR_CONF_LIMIT_FRAMES:
99 limits->limit_frames = g_variant_get_uint64(data);
100 break;
aea4e458
LPC
101 case SR_CONF_LIMIT_MSEC:
102 limits->limit_msec = g_variant_get_uint64(data) * 1000;
103 break;
104 default:
105 return SR_ERR_NA;
106 }
107
108 return SR_OK;
109}
110
580b94e4
LPC
111/**
112 * Start a new data acquisition session
113 *
114 * Resets the internal accounting for all software limits. Usually should be
115 * called from the drivers acquisition_start() callback.
116 *
117 * @param limits software limits instance
118 */
aea4e458
LPC
119SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
120{
121 limits->samples_read = 0;
67785f25 122 limits->frames_read = 0;
aea4e458
LPC
123 limits->start_time = g_get_monotonic_time();
124}
125
580b94e4
LPC
126/**
127 * Check if any of the configured software limits has been reached
128 *
129 * Usually should be called at the end of the drivers work function after all
130 * processing has been done.
131 *
132 * @param limits software limits instance
133 * @returns TRUE if any of the software limits has been reached and the driver
d9251a2c 134 * should stop data acquisition, otherwise FALSE.
580b94e4 135 */
aea4e458
LPC
136SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
137{
138 if (limits->limit_samples) {
3ba944cf
UH
139 if (limits->samples_read >= limits->limit_samples) {
140 sr_dbg("Requested number of samples (%" PRIu64
141 ") reached.", limits->limit_samples);
aea4e458 142 return TRUE;
3ba944cf 143 }
aea4e458
LPC
144 }
145
67785f25
GS
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);
150 return TRUE;
151 }
152 }
153
17ed72cc 154 if (limits->limit_msec && limits->start_time) {
aea4e458
LPC
155 guint64 now;
156 now = g_get_monotonic_time();
157 if (now > limits->start_time &&
3ba944cf
UH
158 now - limits->start_time > limits->limit_msec) {
159 sr_dbg("Requested sampling time (%" PRIu64
160 "ms) reached.", limits->limit_msec / 1000);
aea4e458 161 return TRUE;
3ba944cf 162 }
aea4e458
LPC
163 }
164
165 return FALSE;
166}
167
5aba93c3
GS
168/**
169 * Get remaining counts until software limits are reached.
170 *
171 * This routine fills in those C language variables which callers
172 * requested, and provides the remaining value until a specified limit
173 * would be reached.
174 *
175 * The @ref sr_sw_limits_config_get() routine is suitable for rare
176 * configuration calls and interfaces nicely with Glib data types. The
177 * @ref sr_sw_limits_check() routine only provides a weak "exceeded"
178 * result. This @ref sr_sw_limits_get_remain() routine is suitable for
179 * additional checks and more eager limits enforcement in (potentially
180 * tight) acquisition code paths. Hardware compression may result in
181 * rather large "overshoots" when checks are done only late.
182 *
183 * @param[in] limits software limit instance
184 * @param[out] samples remaining samples count until the limit is reached
185 * @param[out] frames remaining frames count until the limit is reached
186 * @param[out] msecs remaining milliseconds until the limit is reached
187 *
188 * @return SR_ERR_* upon error, SR_OK otherwise
189 */
190SR_PRIV int sr_sw_limits_get_remain(const struct sr_sw_limits *limits,
191 uint64_t *samples, uint64_t *frames, uint64_t *msecs,
192 gboolean *exceeded)
193{
194
195 if (!limits)
196 return SR_ERR_ARG;
197
198 if (exceeded)
199 *exceeded = FALSE;
200
201 if (samples) do {
202 *samples = 0;
203 if (!limits->limit_samples)
204 break;
205 if (limits->samples_read >= limits->limit_samples) {
206 if (exceeded)
207 *exceeded = TRUE;
208 break;
209 }
210 *samples = limits->limit_samples - limits->samples_read;
211 } while (0);
212
213 if (frames) do {
214 *frames = 0;
215 if (!limits->limit_frames)
216 break;
217 if (limits->frames_read >= limits->limit_frames) {
218 if (exceeded)
219 *exceeded = TRUE;
220 break;
221 }
222 *frames = limits->limit_frames - limits->frames_read;
223 } while (0);
224
225 if (msecs) do {
226 guint64 now, elapsed, remain;
227
228 *msecs = 0;
229 if (!limits->limit_msec)
230 break;
231 if (!limits->start_time)
232 break;
233 now = g_get_monotonic_time();
234 if (now < limits->start_time)
235 break;
236 elapsed = now - limits->start_time;
237 if (elapsed >= limits->limit_msec) {
238 if (exceeded)
239 *exceeded = TRUE;
240 break;
241 }
242 remain = limits->limit_msec - elapsed;
243 *msecs = remain / 1000;
244 } while (0);
245
246 return SR_OK;
247}
248
580b94e4 249/**
67785f25 250 * Update the amount of samples that have been read
580b94e4
LPC
251 *
252 * Update the amount of samples that have been read in the current data
253 * acquisition run. For each invocation @p samples_read will be accumulated and
254 * once the configured sample limit has been reached sr_sw_limits_check() will
255 * return TRUE.
256 *
257 * @param limits software limits instance
258 * @param samples_read the amount of samples that have been read
259 */
aea4e458
LPC
260SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
261 uint64_t samples_read)
262{
263 limits->samples_read += samples_read;
264}
67785f25
GS
265
266/**
267 * Update the amount of frames that have been read
268 *
269 * Update the amount of frames that have been read in the current data
270 * acquisition run. For each invocation @p frames_read will be accumulated and
271 * once the configured frame limit has been reached sr_sw_limits_check() will
272 * return TRUE.
273 *
274 * @param limits software limits instance
275 * @param frames_read the amount of frames that have been read
276 */
277SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
278 uint64_t frames_read)
279{
280 limits->frames_read += frames_read;
281}