]> sigrok.org Git - libsigrok.git/blame - tests/lib.c
rigol-ds1xx2: Unbreak listing of _VDIV and _COUPLING
[libsigrok.git] / tests / lib.c
CommitLineData
79bb0e97
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdio.h>
22#include <string.h>
71185b48
UH
23#include <glib.h>
24#include <glib/gstdio.h>
79bb0e97
UH
25#include <check.h>
26#include "../libsigrok.h"
27
28/* Get a libsigrok driver by name. */
29struct sr_dev_driver *srtest_driver_get(const char *drivername)
30{
31 struct sr_dev_driver **drivers, *driver = NULL;
32 int i;
33
34 drivers = sr_driver_list();
35 fail_unless(drivers != NULL, "No drivers found.");
36
37 for (i = 0; drivers[i]; i++) {
38 if (strcmp(drivers[i]->name, drivername))
39 continue;
40 driver = drivers[i];
41 }
42 fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
43
44 return driver;
45}
46
71185b48
UH
47/* Get a libsigrok input format by ID. */
48struct sr_input_format *srtest_input_get(const char *id)
49{
50 struct sr_input_format **inputs, *input = NULL;
51 int i;
52
53 inputs = sr_input_list();
54 fail_unless(inputs != NULL, "No input modules found.");
55
56 for (i = 0; inputs[i]; i++) {
57 if (strcmp(inputs[i]->id, id))
58 continue;
59 input = inputs[i];
60 }
61 fail_unless(input != NULL, "Input module '%s' not found.", id);
62
63 return input;
64}
65
66/* Get a libsigrok output format by ID. */
67struct sr_output_format *srtest_output_get(const char *id)
68{
69 struct sr_output_format **outputs, *output = NULL;
70 int i;
71
72 outputs = sr_output_list();
73 fail_unless(outputs != NULL, "No output modules found.");
74
75 for (i = 0; outputs[i]; i++) {
76 if (strcmp(outputs[i]->id, id))
77 continue;
78 output = outputs[i];
79 }
80 fail_unless(output != NULL, "Output module '%s' not found.", id);
81
82 return output;
83}
84
79bb0e97
UH
85/* Initialize a libsigrok driver. */
86void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
87{
88 int ret;
89
90 ret = sr_driver_init(sr_ctx, driver);
91 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
92 driver->name, ret);
93}
94
95/* Initialize all libsigrok drivers. */
96void srtest_driver_init_all(struct sr_context *sr_ctx)
97{
98 struct sr_dev_driver **drivers, *driver;
99 int i, ret;
100
101 drivers = sr_driver_list();
102 fail_unless(drivers != NULL, "No drivers found.");
103
104 for (i = 0; drivers[i]; i++) {
105 driver = drivers[i];
106 ret = sr_driver_init(sr_ctx, driver);
107 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
108 driver->name, ret);
109 }
110}
111
71185b48
UH
112/* Initialize a libsigrok input module. */
113void srtest_input_init(struct sr_context *sr_ctx, struct sr_input_format *input)
114{
115 int ret;
116 struct sr_input *in;
117
118 (void)sr_ctx;
119
120 in = g_try_malloc0(sizeof(struct sr_input));
121 fail_unless(in != NULL);
122
123 in->format = input;
124 in->param = NULL;
125
126 ret = in->format->init(in, "nonexisting.dat");
127 fail_unless(ret == SR_OK, "Failed to init '%s' input module: %d.",
128 input->id, ret);
129
130 g_free(in);
131}
132
133/* Initialize all libsigrok input modules. */
134void srtest_input_init_all(struct sr_context *sr_ctx)
135{
136 struct sr_input_format **inputs;
137 int i;
138
139 inputs = sr_input_list();
140 fail_unless(inputs != NULL, "No input modules found.");
141
142 for (i = 0; inputs[i]; i++)
143 srtest_input_init(sr_ctx, inputs[i]);
144}
145
79bb0e97
UH
146/* Set the samplerate for the respective driver to the specified value. */
147void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
148{
149 int ret;
150 struct sr_dev_inst *sdi;
34e4c273 151 GVariant *gvar;
79bb0e97
UH
152
153 sdi = g_slist_nth_data(driver->priv, 0);
154
34e4c273
UH
155 gvar = g_variant_new_uint64(samplerate);
156 ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi);
157 g_variant_unref(gvar);
158
79bb0e97
UH
159 fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
160 driver->name, ret);
161}
162
163/* Get the respective driver's current samplerate. */
164uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
165{
166 int ret;
34e4c273 167 uint64_t samplerate;
79bb0e97 168 struct sr_dev_inst *sdi;
34e4c273 169 GVariant *gvar;
79bb0e97
UH
170
171 sdi = g_slist_nth_data(driver->priv, 0);
172
34e4c273
UH
173 ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi);
174 samplerate = g_variant_get_uint64(gvar);
175 g_variant_unref(gvar);
176
79bb0e97
UH
177 fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
178 driver->name, ret);
79bb0e97 179
34e4c273 180 return samplerate;
79bb0e97
UH
181}
182
183/* Check whether the respective driver can set/get the correct samplerate. */
184void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
185 uint64_t samplerate)
186{
187 struct sr_dev_driver *driver;
188 uint64_t s;
189
190 driver = srtest_driver_get(drivername);
191 srtest_driver_init(sr_ctx, driver);;
192 srtest_set_samplerate(driver, samplerate);
193 s = srtest_get_samplerate(driver);
194 fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
195 drivername, s);
196}
71185b48
UH
197
198void srtest_buf_to_file(const char *filename, const uint8_t *buf, uint64_t len)
199{
200 FILE *f;
201 GError *error;
202 gboolean ret;
203
204 f = g_fopen(filename, "wb");
205 fail_unless(f != NULL);
206
207 ret = g_file_set_contents(filename, (const gchar *)buf, len, &error);
208 fail_unless(ret == TRUE);
209
210 fclose(f);
211}
212
213GArray *srtest_get_enabled_logic_probes(const struct sr_dev_inst *sdi)
214{
215 struct sr_probe *probe;
216 GArray *probes;
217 GSList *l;
218
219 probes = g_array_new(FALSE, FALSE, sizeof(int));
220 for (l = sdi->probes; l; l = l->next) {
221 probe = l->data;
222 if (probe->type != SR_PROBE_LOGIC)
223 continue;
224 if (probe->enabled != TRUE)
225 continue;
226 g_array_append_val(probes, probe->index);
227 }
228
229 return probes;
230}