]> sigrok.org Git - libsigrok.git/blame - src/sw_limits.c
sw_limits: Add documentation
[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
23 * @internal
24 */
25
aea4e458
LPC
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
580b94e4
LPC
34/**
35 * Initialize a software limit instance
36 *
37 * Must be called before any other operations are performed on a struct
38 * sr_sw_limits and should typically be called after the data structure has been
39 * allocated.
40 *
41 * @param limits the software limit instance to initialize
42 */
aea4e458
LPC
43SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
44{
45 limits->limit_samples = 0;
46 limits->limit_msec = 0;
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 */
aea4e458
LPC
60SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
61 GVariant **data)
62{
63 switch (key) {
64 case SR_CONF_LIMIT_SAMPLES:
65 *data = g_variant_new_uint64(limits->limit_samples);
66 break;
67 case SR_CONF_LIMIT_MSEC:
68 *data = g_variant_new_uint64(limits->limit_msec / 1000);
69 break;
70 default:
71 return SR_ERR_NA;
72 }
73
74 return SR_OK;
75}
76
580b94e4
LPC
77/**
78 * Set software limit configuration
79 *
80 * Configure software limit for the specified key. Should be called from the
81 * drivers config_set() callback.
82 *
83 * @param limits software limit instance
84 * @param key config item key
85 * @param data config item data
86 * @return SR_ERR_NA if @p key is not a supported limit, SR_OK otherwise
87 */
aea4e458
LPC
88SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
89 GVariant *data)
90{
91 switch (key) {
92 case SR_CONF_LIMIT_SAMPLES:
93 limits->limit_samples = g_variant_get_uint64(data);
94 break;
95 case SR_CONF_LIMIT_MSEC:
96 limits->limit_msec = g_variant_get_uint64(data) * 1000;
97 break;
98 default:
99 return SR_ERR_NA;
100 }
101
102 return SR_OK;
103}
104
580b94e4
LPC
105/**
106 * Start a new data acquisition session
107 *
108 * Resets the internal accounting for all software limits. Usually should be
109 * called from the drivers acquisition_start() callback.
110 *
111 * @param limits software limits instance
112 */
aea4e458
LPC
113SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
114{
115 limits->samples_read = 0;
116 limits->start_time = g_get_monotonic_time();
117}
118
580b94e4
LPC
119/**
120 * Check if any of the configured software limits has been reached
121 *
122 * Usually should be called at the end of the drivers work function after all
123 * processing has been done.
124 *
125 * @param limits software limits instance
126 * @returns TRUE if any of the software limits has been reached and the driver
127 * should stop data acquisition, otherwise FALSE.
128 */
aea4e458
LPC
129SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
130{
131 if (limits->limit_samples) {
132 if (limits->samples_read >= limits->limit_samples)
133 return TRUE;
134 }
135
136 if (limits->limit_msec) {
137 guint64 now;
138 now = g_get_monotonic_time();
139 if (now > limits->start_time &&
140 now - limits->start_time > limits->limit_msec)
141 return TRUE;
142 }
143
144 return FALSE;
145}
146
580b94e4
LPC
147/**
148 * Update the amount samples that have been read
149 *
150 * Update the amount of samples that have been read in the current data
151 * acquisition run. For each invocation @p samples_read will be accumulated and
152 * once the configured sample limit has been reached sr_sw_limits_check() will
153 * return TRUE.
154 *
155 * @param limits software limits instance
156 * @param samples_read the amount of samples that have been read
157 */
aea4e458
LPC
158SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
159 uint64_t samples_read)
160{
161 limits->samples_read += samples_read;
162}