]> sigrok.org Git - libsigrok.git/blame - tests/lib.c
sr_session_new(): Return SR_ERR_ARG upon invalid argument.
[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 25#include <check.h>
6592c369 26#include "../include/libsigrok/libsigrok.h"
17794067 27#include "lib.h"
79bb0e97
UH
28
29/* Get a libsigrok driver by name. */
30struct sr_dev_driver *srtest_driver_get(const char *drivername)
31{
32 struct sr_dev_driver **drivers, *driver = NULL;
33 int i;
34
35 drivers = sr_driver_list();
36 fail_unless(drivers != NULL, "No drivers found.");
37
38 for (i = 0; drivers[i]; i++) {
39 if (strcmp(drivers[i]->name, drivername))
40 continue;
41 driver = drivers[i];
42 }
43 fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
44
45 return driver;
46}
47
71185b48
UH
48/* Get a libsigrok input format by ID. */
49struct sr_input_format *srtest_input_get(const char *id)
50{
51 struct sr_input_format **inputs, *input = NULL;
52 int i;
53
54 inputs = sr_input_list();
55 fail_unless(inputs != NULL, "No input modules found.");
56
57 for (i = 0; inputs[i]; i++) {
58 if (strcmp(inputs[i]->id, id))
59 continue;
60 input = inputs[i];
61 }
62 fail_unless(input != NULL, "Input module '%s' not found.", id);
63
64 return input;
65}
66
79bb0e97
UH
67/* Initialize a libsigrok driver. */
68void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
69{
70 int ret;
71
72 ret = sr_driver_init(sr_ctx, driver);
73 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
74 driver->name, ret);
75}
76
77/* Initialize all libsigrok drivers. */
78void srtest_driver_init_all(struct sr_context *sr_ctx)
79{
80 struct sr_dev_driver **drivers, *driver;
81 int i, ret;
82
83 drivers = sr_driver_list();
84 fail_unless(drivers != NULL, "No drivers found.");
85
86 for (i = 0; drivers[i]; i++) {
87 driver = drivers[i];
88 ret = sr_driver_init(sr_ctx, driver);
89 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
90 driver->name, ret);
91 }
92}
93
71185b48
UH
94/* Initialize a libsigrok input module. */
95void srtest_input_init(struct sr_context *sr_ctx, struct sr_input_format *input)
96{
97 int ret;
98 struct sr_input *in;
99
100 (void)sr_ctx;
101
102 in = g_try_malloc0(sizeof(struct sr_input));
103 fail_unless(in != NULL);
104
105 in->format = input;
106 in->param = NULL;
107
108 ret = in->format->init(in, "nonexisting.dat");
109 fail_unless(ret == SR_OK, "Failed to init '%s' input module: %d.",
110 input->id, ret);
111
112 g_free(in);
113}
114
115/* Initialize all libsigrok input modules. */
116void srtest_input_init_all(struct sr_context *sr_ctx)
117{
118 struct sr_input_format **inputs;
119 int i;
120
121 inputs = sr_input_list();
122 fail_unless(inputs != NULL, "No input modules found.");
123
124 for (i = 0; inputs[i]; i++)
125 srtest_input_init(sr_ctx, inputs[i]);
126}
127
79bb0e97
UH
128/* Set the samplerate for the respective driver to the specified value. */
129void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
130{
131 int ret;
132 struct sr_dev_inst *sdi;
34e4c273 133 GVariant *gvar;
79bb0e97
UH
134
135 sdi = g_slist_nth_data(driver->priv, 0);
136
34e4c273 137 gvar = g_variant_new_uint64(samplerate);
57d0a2e1 138 ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL);
34e4c273
UH
139 g_variant_unref(gvar);
140
79bb0e97
UH
141 fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
142 driver->name, ret);
143}
144
145/* Get the respective driver's current samplerate. */
146uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
147{
148 int ret;
34e4c273 149 uint64_t samplerate;
79bb0e97 150 struct sr_dev_inst *sdi;
34e4c273 151 GVariant *gvar;
79bb0e97
UH
152
153 sdi = g_slist_nth_data(driver->priv, 0);
154
57d0a2e1 155 ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL);
34e4c273
UH
156 samplerate = g_variant_get_uint64(gvar);
157 g_variant_unref(gvar);
158
79bb0e97
UH
159 fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
160 driver->name, ret);
79bb0e97 161
34e4c273 162 return samplerate;
79bb0e97
UH
163}
164
165/* Check whether the respective driver can set/get the correct samplerate. */
166void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
167 uint64_t samplerate)
168{
169 struct sr_dev_driver *driver;
170 uint64_t s;
171
172 driver = srtest_driver_get(drivername);
173 srtest_driver_init(sr_ctx, driver);;
174 srtest_set_samplerate(driver, samplerate);
175 s = srtest_get_samplerate(driver);
176 fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
177 drivername, s);
178}
71185b48
UH
179
180void srtest_buf_to_file(const char *filename, const uint8_t *buf, uint64_t len)
181{
182 FILE *f;
c3e2b08d 183 GError *error = NULL;
71185b48
UH
184 gboolean ret;
185
186 f = g_fopen(filename, "wb");
187 fail_unless(f != NULL);
188
189 ret = g_file_set_contents(filename, (const gchar *)buf, len, &error);
190 fail_unless(ret == TRUE);
191
192 fclose(f);
193}
194
fca75cbb 195GArray *srtest_get_enabled_logic_channels(const struct sr_dev_inst *sdi)
71185b48 196{
ba7dd8bb 197 struct sr_channel *ch;
fca75cbb 198 GArray *channels;
71185b48
UH
199 GSList *l;
200
fca75cbb
UH
201 channels = g_array_new(FALSE, FALSE, sizeof(int));
202 for (l = sdi->channels; l; l = l->next) {
203 ch = l->data;
204 if (ch->type != SR_CHANNEL_LOGIC)
71185b48 205 continue;
fca75cbb 206 if (ch->enabled != TRUE)
71185b48 207 continue;
fca75cbb 208 g_array_append_val(channels, ch->index);
71185b48
UH
209 }
210
fca75cbb 211 return channels;
71185b48 212}