]> sigrok.org Git - libsigrok.git/blame - tests/lib.c
Build: Include <config.h> first in all source files
[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
6ec6c43b 21#include <config.h>
79bb0e97
UH
22#include <stdio.h>
23#include <string.h>
71185b48
UH
24#include <glib.h>
25#include <glib/gstdio.h>
79bb0e97 26#include <check.h>
4960aeb0 27#include <libsigrok/libsigrok.h>
17794067 28#include "lib.h"
79bb0e97 29
98de0c78
UH
30struct sr_context *srtest_ctx;
31
32void srtest_setup(void)
33{
34 int ret;
35
36 ret = sr_init(&srtest_ctx);
37 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
38}
39
40void srtest_teardown(void)
41{
42 int ret;
43
44 ret = sr_exit(srtest_ctx);
45 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
46}
47
79bb0e97
UH
48/* Get a libsigrok driver by name. */
49struct sr_dev_driver *srtest_driver_get(const char *drivername)
50{
51 struct sr_dev_driver **drivers, *driver = NULL;
52 int i;
53
032da34b 54 drivers = sr_driver_list(srtest_ctx);
79bb0e97
UH
55 fail_unless(drivers != NULL, "No drivers found.");
56
57 for (i = 0; drivers[i]; i++) {
58 if (strcmp(drivers[i]->name, drivername))
59 continue;
60 driver = drivers[i];
61 }
62 fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
63
64 return driver;
65}
66
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
032da34b 83 drivers = sr_driver_list(srtest_ctx);
79bb0e97
UH
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
94/* Set the samplerate for the respective driver to the specified value. */
95void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
96{
97 int ret;
98 struct sr_dev_inst *sdi;
34e4c273 99 GVariant *gvar;
79bb0e97 100
338143ea 101 sdi = g_slist_nth_data(driver->context, 0);
79bb0e97 102
34e4c273 103 gvar = g_variant_new_uint64(samplerate);
57d0a2e1 104 ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL);
34e4c273
UH
105 g_variant_unref(gvar);
106
79bb0e97
UH
107 fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
108 driver->name, ret);
109}
110
111/* Get the respective driver's current samplerate. */
112uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
113{
114 int ret;
34e4c273 115 uint64_t samplerate;
79bb0e97 116 struct sr_dev_inst *sdi;
34e4c273 117 GVariant *gvar;
79bb0e97 118
338143ea 119 sdi = g_slist_nth_data(driver->context, 0);
79bb0e97 120
57d0a2e1 121 ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL);
34e4c273
UH
122 samplerate = g_variant_get_uint64(gvar);
123 g_variant_unref(gvar);
124
79bb0e97
UH
125 fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
126 driver->name, ret);
79bb0e97 127
34e4c273 128 return samplerate;
79bb0e97
UH
129}
130
131/* Check whether the respective driver can set/get the correct samplerate. */
132void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
133 uint64_t samplerate)
134{
135 struct sr_dev_driver *driver;
136 uint64_t s;
137
138 driver = srtest_driver_get(drivername);
139 srtest_driver_init(sr_ctx, driver);;
140 srtest_set_samplerate(driver, samplerate);
141 s = srtest_get_samplerate(driver);
142 fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
143 drivername, s);
144}
71185b48 145
fca75cbb 146GArray *srtest_get_enabled_logic_channels(const struct sr_dev_inst *sdi)
71185b48 147{
ba7dd8bb 148 struct sr_channel *ch;
fca75cbb 149 GArray *channels;
71185b48
UH
150 GSList *l;
151
fca75cbb 152 channels = g_array_new(FALSE, FALSE, sizeof(int));
924866d4 153 for (l = sr_dev_inst_channels_get(sdi); l; l = l->next) {
fca75cbb
UH
154 ch = l->data;
155 if (ch->type != SR_CHANNEL_LOGIC)
71185b48 156 continue;
fca75cbb 157 if (ch->enabled != TRUE)
71185b48 158 continue;
fca75cbb 159 g_array_append_val(channels, ch->index);
71185b48
UH
160 }
161
fca75cbb 162 return channels;
71185b48 163}