]> sigrok.org Git - libsigrok.git/blob - tests/lib.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / tests / lib.c
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <check.h>
26 #include <libsigrok/libsigrok.h>
27 #include "lib.h"
28
29 struct sr_context *srtest_ctx;
30
31 void srtest_setup(void)
32 {
33         int ret;
34
35         ret = sr_init(&srtest_ctx);
36         fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
37 }
38
39 void srtest_teardown(void)
40 {
41         int ret;
42
43         ret = sr_exit(srtest_ctx);
44         fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
45 }
46
47 /* Get a libsigrok driver by name. */
48 struct sr_dev_driver *srtest_driver_get(const char *drivername)
49 {
50         struct sr_dev_driver **drivers, *driver = NULL;
51         int i;
52
53         drivers = sr_driver_list(srtest_ctx);
54         fail_unless(drivers != NULL, "No drivers found.");
55
56         for (i = 0; drivers[i]; i++) {
57                 if (strcmp(drivers[i]->name, drivername))
58                         continue;
59                 driver = drivers[i];
60         }
61         fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
62
63         return driver;
64 }
65
66 /* Initialize a libsigrok driver. */
67 void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
68 {
69         int ret;
70
71         ret = sr_driver_init(sr_ctx, driver);
72         fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
73                     driver->name, ret);
74 }
75
76 /* Initialize all libsigrok drivers. */
77 void srtest_driver_init_all(struct sr_context *sr_ctx)
78 {
79         struct sr_dev_driver **drivers, *driver;
80         int i, ret;
81
82         drivers = sr_driver_list(srtest_ctx);
83         fail_unless(drivers != NULL, "No drivers found.");
84
85         for (i = 0; drivers[i]; i++) {
86                 driver = drivers[i];
87                 ret = sr_driver_init(sr_ctx, driver);
88                 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
89                             driver->name, ret);
90         }
91 }
92
93 /* Set the samplerate for the respective driver to the specified value. */
94 void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
95 {
96         int ret;
97         struct sr_dev_inst *sdi;
98         GVariant *gvar;
99
100         sdi = g_slist_nth_data(driver->context, 0);
101
102         gvar = g_variant_new_uint64(samplerate);
103         ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL);
104         g_variant_unref(gvar);
105
106         fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
107                     driver->name, ret);
108 }
109
110 /* Get the respective driver's current samplerate. */
111 uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
112 {
113         int ret;
114         uint64_t samplerate;
115         struct sr_dev_inst *sdi;
116         GVariant *gvar;
117
118         sdi = g_slist_nth_data(driver->context, 0);
119
120         ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL);
121         samplerate = g_variant_get_uint64(gvar);
122         g_variant_unref(gvar);
123
124         fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
125                     driver->name, ret);
126
127         return samplerate;
128 }
129
130 /* Check whether the respective driver can set/get the correct samplerate. */
131 void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
132                              uint64_t samplerate)
133 {
134         struct sr_dev_driver *driver;
135         uint64_t s;
136
137         driver = srtest_driver_get(drivername);
138         srtest_driver_init(sr_ctx, driver);;
139         srtest_set_samplerate(driver, samplerate);
140         s = srtest_get_samplerate(driver);
141         fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
142                     drivername, s);
143 }
144
145 GArray *srtest_get_enabled_logic_channels(const struct sr_dev_inst *sdi)
146 {
147         struct sr_channel *ch;
148         GArray *channels;
149         GSList *l;
150
151         channels = g_array_new(FALSE, FALSE, sizeof(int));
152         for (l = sr_dev_inst_channels_get(sdi); l; l = l->next) {
153                 ch = l->data;
154                 if (ch->type != SR_CHANNEL_LOGIC)
155                         continue;
156                 if (ch->enabled != TRUE)
157                         continue;
158                 g_array_append_val(channels, ch->index);
159         }
160
161         return channels;
162 }