2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include <glib/gstdio.h>
26 #include "../include/libsigrok/libsigrok.h"
29 struct sr_context *srtest_ctx;
31 void srtest_setup(void)
35 ret = sr_init(&srtest_ctx);
36 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
39 void srtest_teardown(void)
43 ret = sr_exit(srtest_ctx);
44 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
47 /* Get a libsigrok driver by name. */
48 struct sr_dev_driver *srtest_driver_get(const char *drivername)
50 struct sr_dev_driver **drivers, *driver = NULL;
53 drivers = sr_driver_list();
54 fail_unless(drivers != NULL, "No drivers found.");
56 for (i = 0; drivers[i]; i++) {
57 if (strcmp(drivers[i]->name, drivername))
61 fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
66 /* Get a libsigrok input format by ID. */
67 struct sr_input_format *srtest_input_get(const char *id)
69 struct sr_input_format **inputs, *input = NULL;
72 inputs = sr_input_list();
73 fail_unless(inputs != NULL, "No input modules found.");
75 for (i = 0; inputs[i]; i++) {
76 if (strcmp(inputs[i]->id, id))
80 fail_unless(input != NULL, "Input module '%s' not found.", id);
85 /* Initialize a libsigrok driver. */
86 void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
90 ret = sr_driver_init(sr_ctx, driver);
91 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
95 /* Initialize all libsigrok drivers. */
96 void srtest_driver_init_all(struct sr_context *sr_ctx)
98 struct sr_dev_driver **drivers, *driver;
101 drivers = sr_driver_list();
102 fail_unless(drivers != NULL, "No drivers found.");
104 for (i = 0; drivers[i]; i++) {
106 ret = sr_driver_init(sr_ctx, driver);
107 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
112 /* Initialize a libsigrok input module. */
113 void srtest_input_init(struct sr_context *sr_ctx, struct sr_input_format *input)
120 in = g_try_malloc0(sizeof(struct sr_input));
121 fail_unless(in != NULL);
126 ret = in->format->init(in, "nonexisting.dat");
127 fail_unless(ret == SR_OK, "Failed to init '%s' input module: %d.",
133 /* Initialize all libsigrok input modules. */
134 void srtest_input_init_all(struct sr_context *sr_ctx)
136 struct sr_input_format **inputs;
139 inputs = sr_input_list();
140 fail_unless(inputs != NULL, "No input modules found.");
142 for (i = 0; inputs[i]; i++)
143 srtest_input_init(sr_ctx, inputs[i]);
146 /* Set the samplerate for the respective driver to the specified value. */
147 void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
150 struct sr_dev_inst *sdi;
153 sdi = g_slist_nth_data(driver->priv, 0);
155 gvar = g_variant_new_uint64(samplerate);
156 ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL);
157 g_variant_unref(gvar);
159 fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
163 /* Get the respective driver's current samplerate. */
164 uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
168 struct sr_dev_inst *sdi;
171 sdi = g_slist_nth_data(driver->priv, 0);
173 ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL);
174 samplerate = g_variant_get_uint64(gvar);
175 g_variant_unref(gvar);
177 fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
183 /* Check whether the respective driver can set/get the correct samplerate. */
184 void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
187 struct sr_dev_driver *driver;
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 ".",
198 void srtest_buf_to_file(const char *filename, const uint8_t *buf, uint64_t len)
201 GError *error = NULL;
204 f = g_fopen(filename, "wb");
205 fail_unless(f != NULL);
207 ret = g_file_set_contents(filename, (const gchar *)buf, len, &error);
208 fail_unless(ret == TRUE);
213 GArray *srtest_get_enabled_logic_channels(const struct sr_dev_inst *sdi)
215 struct sr_channel *ch;
219 channels = g_array_new(FALSE, FALSE, sizeof(int));
220 for (l = sdi->channels; l; l = l->next) {
222 if (ch->type != SR_CHANNEL_LOGIC)
224 if (ch->enabled != TRUE)
226 g_array_append_val(channels, ch->index);