]> sigrok.org Git - libsigrok.git/blob - src/sw_limits.c
sw_limits: add support for maximum frame counts
[libsigrok.git] / src / sw_limits.c
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
20 /**
21  * @file
22  * Software limits helper functions
23  * @internal
24  */
25
26 #include <config.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <libsigrok/libsigrok.h>
32 #include "libsigrok-internal.h"
33
34 #define LOG_PREFIX "sw_limits"
35
36 /**
37  * Initialize a software limit instance
38  *
39  * Must be called before any other operations are performed on a struct
40  * sr_sw_limits and should typically be called after the data structure has been
41  * allocated.
42  *
43  * @param limits the software limit instance to initialize
44  */
45 SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
46 {
47         limits->limit_samples = 0;
48         limits->limit_frames = 0;
49         limits->limit_msec = 0;
50 }
51
52 /**
53  * Get software limit configuration
54  *
55  * Retrieve the currently configured software limit for the specified key.
56  * Should be called from the drivers config_get() callback.
57  *
58  * @param limits software limit instance
59  * @param key config item key
60  * @param data config item data
61  * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
62  */
63 SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
64         GVariant **data)
65 {
66         switch (key) {
67         case SR_CONF_LIMIT_SAMPLES:
68                 *data = g_variant_new_uint64(limits->limit_samples);
69                 break;
70         case SR_CONF_LIMIT_FRAMES:
71                 *data = g_variant_new_uint64(limits->limit_frames);
72                 break;
73         case SR_CONF_LIMIT_MSEC:
74                 *data = g_variant_new_uint64(limits->limit_msec / 1000);
75                 break;
76         default:
77                 return SR_ERR_NA;
78         }
79
80         return SR_OK;
81 }
82
83 /**
84  * Set software limit configuration
85  *
86  * Configure software limit for the specified key. Should be called from the
87  * drivers config_set() callback.
88  *
89  * @param limits software limit instance
90  * @param key config item key
91  * @param data config item data
92  * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
93  */
94 SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
95         GVariant *data)
96 {
97         switch (key) {
98         case SR_CONF_LIMIT_SAMPLES:
99                 limits->limit_samples = g_variant_get_uint64(data);
100                 break;
101         case SR_CONF_LIMIT_FRAMES:
102                 limits->limit_frames = g_variant_get_uint64(data);
103                 break;
104         case SR_CONF_LIMIT_MSEC:
105                 limits->limit_msec = g_variant_get_uint64(data) * 1000;
106                 break;
107         default:
108                 return SR_ERR_NA;
109         }
110
111         return SR_OK;
112 }
113
114 /**
115  * Start a new data acquisition session
116  *
117  * Resets the internal accounting for all software limits. Usually should be
118  * called from the drivers acquisition_start() callback.
119  *
120  * @param limits software limits instance
121  */
122 SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
123 {
124         limits->samples_read = 0;
125         limits->frames_read = 0;
126         limits->start_time = g_get_monotonic_time();
127 }
128
129 /**
130  * Check if any of the configured software limits has been reached
131  *
132  * Usually should be called at the end of the drivers work function after all
133  * processing has been done.
134  *
135  * @param limits software limits instance
136  * @returns TRUE if any of the software limits has been reached and the driver
137  *               should stop data acquisition, otherwise FALSE.
138  */
139 SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
140 {
141         if (limits->limit_samples) {
142                 if (limits->samples_read >= limits->limit_samples) {
143                         sr_dbg("Requested number of samples (%" PRIu64
144                                ") reached.", limits->limit_samples);
145                         return TRUE;
146                 }
147         }
148
149         if (limits->limit_frames) {
150                 if (limits->frames_read >= limits->limit_frames) {
151                         sr_dbg("Requested number of frames (%" PRIu64
152                                ") reached.", limits->limit_frames);
153                         return TRUE;
154                 }
155         }
156
157         if (limits->limit_msec) {
158                 guint64 now;
159                 now = g_get_monotonic_time();
160                 if (now > limits->start_time &&
161                         now - limits->start_time > limits->limit_msec) {
162                         sr_dbg("Requested sampling time (%" PRIu64
163                                "ms) reached.", limits->limit_msec / 1000);
164                         return TRUE;
165                 }
166         }
167
168         return FALSE;
169 }
170
171 /**
172  * Update the amount of samples that have been read
173  *
174  * Update the amount of samples that have been read in the current data
175  * acquisition run. For each invocation @p samples_read will be accumulated and
176  * once the configured sample limit has been reached sr_sw_limits_check() will
177  * return TRUE.
178  *
179  * @param limits software limits instance
180  * @param samples_read the amount of samples that have been read
181  */
182 SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
183         uint64_t samples_read)
184 {
185         limits->samples_read += samples_read;
186 }
187
188 /**
189  * Update the amount of frames that have been read
190  *
191  * Update the amount of frames that have been read in the current data
192  * acquisition run. For each invocation @p frames_read will be accumulated and
193  * once the configured frame limit has been reached sr_sw_limits_check() will
194  * return TRUE.
195  *
196  * @param limits software limits instance
197  * @param frames_read the amount of frames that have been read
198  */
199 SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits,
200         uint64_t frames_read)
201 {
202         limits->frames_read += frames_read;
203 }