]>
Commit | Line | Data |
---|---|---|
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 |
44 | SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits) |
45 | { | |
46 | limits->limit_samples = 0; | |
67785f25 | 47 | limits->limit_frames = 0; |
aea4e458 LPC |
48 | limits->limit_msec = 0; |
49 | } | |
50 | ||
580b94e4 LPC |
51 | /** |
52 | * Get software limit configuration | |
53 | * | |
54 | * Retrieve the currently configured software limit for the specified key. | |
55 | * Should be called from the drivers config_get() callback. | |
56 | * | |
57 | * @param limits software limit instance | |
58 | * @param key config item key | |
59 | * @param data config item data | |
60 | * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise | |
61 | */ | |
aea4e458 LPC |
62 | SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key, |
63 | GVariant **data) | |
64 | { | |
65 | switch (key) { | |
66 | case SR_CONF_LIMIT_SAMPLES: | |
67 | *data = g_variant_new_uint64(limits->limit_samples); | |
68 | break; | |
67785f25 GS |
69 | case SR_CONF_LIMIT_FRAMES: |
70 | *data = g_variant_new_uint64(limits->limit_frames); | |
71 | break; | |
aea4e458 LPC |
72 | case SR_CONF_LIMIT_MSEC: |
73 | *data = g_variant_new_uint64(limits->limit_msec / 1000); | |
74 | break; | |
75 | default: | |
76 | return SR_ERR_NA; | |
77 | } | |
78 | ||
79 | return SR_OK; | |
80 | } | |
81 | ||
580b94e4 LPC |
82 | /** |
83 | * Set software limit configuration | |
84 | * | |
85 | * Configure software limit for the specified key. Should be called from the | |
86 | * drivers config_set() callback. | |
87 | * | |
88 | * @param limits software limit instance | |
89 | * @param key config item key | |
90 | * @param data config item data | |
91 | * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise | |
92 | */ | |
aea4e458 LPC |
93 | SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key, |
94 | GVariant *data) | |
95 | { | |
96 | switch (key) { | |
97 | case SR_CONF_LIMIT_SAMPLES: | |
98 | limits->limit_samples = g_variant_get_uint64(data); | |
99 | break; | |
67785f25 GS |
100 | case SR_CONF_LIMIT_FRAMES: |
101 | limits->limit_frames = g_variant_get_uint64(data); | |
102 | break; | |
aea4e458 LPC |
103 | case SR_CONF_LIMIT_MSEC: |
104 | limits->limit_msec = g_variant_get_uint64(data) * 1000; | |
105 | break; | |
106 | default: | |
107 | return SR_ERR_NA; | |
108 | } | |
109 | ||
110 | return SR_OK; | |
111 | } | |
112 | ||
580b94e4 LPC |
113 | /** |
114 | * Start a new data acquisition session | |
115 | * | |
116 | * Resets the internal accounting for all software limits. Usually should be | |
117 | * called from the drivers acquisition_start() callback. | |
118 | * | |
119 | * @param limits software limits instance | |
120 | */ | |
aea4e458 LPC |
121 | SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits) |
122 | { | |
123 | limits->samples_read = 0; | |
67785f25 | 124 | limits->frames_read = 0; |
aea4e458 LPC |
125 | limits->start_time = g_get_monotonic_time(); |
126 | } | |
127 | ||
580b94e4 LPC |
128 | /** |
129 | * Check if any of the configured software limits has been reached | |
130 | * | |
131 | * Usually should be called at the end of the drivers work function after all | |
132 | * processing has been done. | |
133 | * | |
134 | * @param limits software limits instance | |
135 | * @returns TRUE if any of the software limits has been reached and the driver | |
d9251a2c | 136 | * should stop data acquisition, otherwise FALSE. |
580b94e4 | 137 | */ |
aea4e458 LPC |
138 | SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits) |
139 | { | |
140 | if (limits->limit_samples) { | |
3ba944cf UH |
141 | if (limits->samples_read >= limits->limit_samples) { |
142 | sr_dbg("Requested number of samples (%" PRIu64 | |
143 | ") reached.", limits->limit_samples); | |
aea4e458 | 144 | return TRUE; |
3ba944cf | 145 | } |
aea4e458 LPC |
146 | } |
147 | ||
67785f25 GS |
148 | if (limits->limit_frames) { |
149 | if (limits->frames_read >= limits->limit_frames) { | |
150 | sr_dbg("Requested number of frames (%" PRIu64 | |
151 | ") reached.", limits->limit_frames); | |
152 | return TRUE; | |
153 | } | |
154 | } | |
155 | ||
aea4e458 LPC |
156 | if (limits->limit_msec) { |
157 | guint64 now; | |
158 | now = g_get_monotonic_time(); | |
159 | if (now > limits->start_time && | |
3ba944cf UH |
160 | now - limits->start_time > limits->limit_msec) { |
161 | sr_dbg("Requested sampling time (%" PRIu64 | |
162 | "ms) reached.", limits->limit_msec / 1000); | |
aea4e458 | 163 | return TRUE; |
3ba944cf | 164 | } |
aea4e458 LPC |
165 | } |
166 | ||
167 | return FALSE; | |
168 | } | |
169 | ||
580b94e4 | 170 | /** |
67785f25 | 171 | * Update the amount of samples that have been read |
580b94e4 LPC |
172 | * |
173 | * Update the amount of samples that have been read in the current data | |
174 | * acquisition run. For each invocation @p samples_read will be accumulated and | |
175 | * once the configured sample limit has been reached sr_sw_limits_check() will | |
176 | * return TRUE. | |
177 | * | |
178 | * @param limits software limits instance | |
179 | * @param samples_read the amount of samples that have been read | |
180 | */ | |
aea4e458 LPC |
181 | SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits, |
182 | uint64_t samples_read) | |
183 | { | |
184 | limits->samples_read += samples_read; | |
185 | } | |
67785f25 GS |
186 | |
187 | /** | |
188 | * Update the amount of frames that have been read | |
189 | * | |
190 | * Update the amount of frames that have been read in the current data | |
191 | * acquisition run. For each invocation @p frames_read will be accumulated and | |
192 | * once the configured frame limit has been reached sr_sw_limits_check() will | |
193 | * return TRUE. | |
194 | * | |
195 | * @param limits software limits instance | |
196 | * @param frames_read the amount of frames that have been read | |
197 | */ | |
198 | SR_PRIV void sr_sw_limits_update_frames_read(struct sr_sw_limits *limits, | |
199 | uint64_t frames_read) | |
200 | { | |
201 | limits->frames_read += frames_read; | |
202 | } |