forked from sigrokproject/libsigrok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw_limits.c
281 lines (258 loc) · 7.7 KB
/
sw_limits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2016 Lars-Peter Clausen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* Software limits helper functions
*/
#include <config.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
#define LOG_PREFIX "sw_limits"
/**
* Initialize a software limit instance
*
* Must be called before any other operations are performed on a struct
* sr_sw_limits and should typically be called after the data structure has been
* allocated.
*
* @param limits the software limit instance to initialize
*/
SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
{
memset(limits, 0, sizeof(*limits));
}
/**
* Get software limit configuration
*
* Retrieve the currently configured software limit for the specified key.
* Should be called from the drivers config_get() callback.
*
* @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(const struct sr_sw_limits *limits, uint32_t key,
GVariant **data)
{
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
*data = g_variant_new_uint64(limits->limit_samples);
break;
case SR_CONF_LIMIT_FRAMES:
*data = g_variant_new_uint64(limits->limit_frames);
break;
case SR_CONF_LIMIT_MSEC:
*data = g_variant_new_uint64(limits->limit_msec / 1000);
break;
default:
return SR_ERR_NA;
}
return SR_OK;
}
/**
* Set software limit configuration
*
* Configure software limit for the specified key. Should be called from the
* drivers config_set() callback.
*
* @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)
{
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
limits->limit_samples = g_variant_get_uint64(data);
break;
case SR_CONF_LIMIT_FRAMES:
limits->limit_frames = g_variant_get_uint64(data);
break;
case SR_CONF_LIMIT_MSEC:
limits->limit_msec = g_variant_get_uint64(data) * 1000;
break;
default:
return SR_ERR_NA;
}
return SR_OK;
}
/**
* Start a new data acquisition session
*
* Resets the internal accounting for all software limits. Usually should be
* called from the drivers acquisition_start() callback.
*
* @param limits software limits instance
*/
SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
{
limits->samples_read = 0;
limits->frames_read = 0;
limits->start_time = g_get_monotonic_time();
}
/**
* Check if any of the configured software limits has been reached
*
* Usually should be called at the end of the drivers work function after all
* 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.
*/
SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
{
if (limits->limit_samples) {
if (limits->samples_read >= limits->limit_samples) {
sr_dbg("Requested number of samples (%" PRIu64
") reached.", limits->limit_samples);
return TRUE;
}
}
if (limits->limit_frames) {
if (limits->frames_read >= limits->limit_frames) {
sr_dbg("Requested number of frames (%" PRIu64
") reached.", limits->limit_frames);
return TRUE;
}
}
if (limits->limit_msec && limits->start_time) {
guint64 now;
now = g_get_monotonic_time();
if (now > limits->start_time &&
now - limits->start_time > limits->limit_msec) {
sr_dbg("Requested sampling time (%" PRIu64
"ms) reached.", limits->limit_msec / 1000);
return TRUE;
}
}
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
*
* @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
*
* Update the amount of samples that have been read in the current data
* acquisition run. For each invocation @p samples_read will be accumulated and
* once the configured sample limit has been reached sr_sw_limits_check() will
* return TRUE.
*
* @param limits software limits instance
* @param samples_read the amount of samples that have been read
*/
SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
uint64_t samples_read)
{
limits->samples_read += samples_read;
}
/**
* Update the amount of frames that have been read
*
* Update the amount of frames that have been read in the current data
* acquisition run. For each invocation @p frames_read will be accumulated and
* once the configured frame limit has been reached sr_sw_limits_check() will
* return TRUE.
*
* @param limits software limits instance
* @param frames_read the amount of frames that have been read
*/
SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
uint64_t frames_read)
{
limits->frames_read += frames_read;
}