Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 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> |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 23 | #include <glib.h> |
| 24 | #include <glib/gstdio.h> |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 25 | #include <check.h> |
| 26 | #include "../libsigrok.h" |
Uwe Hermann | 1779406 | 2014-01-17 20:41:56 +0100 | [diff] [blame] | 27 | #include "lib.h" |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 28 | |
| 29 | /* Get a libsigrok driver by name. */ |
| 30 | struct 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 | |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 48 | /* Get a libsigrok input format by ID. */ |
| 49 | struct 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 | |
| 67 | /* Get a libsigrok output format by ID. */ |
| 68 | struct sr_output_format *srtest_output_get(const char *id) |
| 69 | { |
| 70 | struct sr_output_format **outputs, *output = NULL; |
| 71 | int i; |
| 72 | |
| 73 | outputs = sr_output_list(); |
| 74 | fail_unless(outputs != NULL, "No output modules found."); |
| 75 | |
| 76 | for (i = 0; outputs[i]; i++) { |
| 77 | if (strcmp(outputs[i]->id, id)) |
| 78 | continue; |
| 79 | output = outputs[i]; |
| 80 | } |
| 81 | fail_unless(output != NULL, "Output module '%s' not found.", id); |
| 82 | |
| 83 | return output; |
| 84 | } |
| 85 | |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 86 | /* Initialize a libsigrok driver. */ |
| 87 | void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver) |
| 88 | { |
| 89 | int ret; |
| 90 | |
| 91 | ret = sr_driver_init(sr_ctx, driver); |
| 92 | fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.", |
| 93 | driver->name, ret); |
| 94 | } |
| 95 | |
| 96 | /* Initialize all libsigrok drivers. */ |
| 97 | void srtest_driver_init_all(struct sr_context *sr_ctx) |
| 98 | { |
| 99 | struct sr_dev_driver **drivers, *driver; |
| 100 | int i, ret; |
| 101 | |
| 102 | drivers = sr_driver_list(); |
| 103 | fail_unless(drivers != NULL, "No drivers found."); |
| 104 | |
| 105 | for (i = 0; drivers[i]; i++) { |
| 106 | driver = drivers[i]; |
| 107 | ret = sr_driver_init(sr_ctx, driver); |
| 108 | fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.", |
| 109 | driver->name, ret); |
| 110 | } |
| 111 | } |
| 112 | |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 113 | /* Initialize a libsigrok input module. */ |
| 114 | void srtest_input_init(struct sr_context *sr_ctx, struct sr_input_format *input) |
| 115 | { |
| 116 | int ret; |
| 117 | struct sr_input *in; |
| 118 | |
| 119 | (void)sr_ctx; |
| 120 | |
| 121 | in = g_try_malloc0(sizeof(struct sr_input)); |
| 122 | fail_unless(in != NULL); |
| 123 | |
| 124 | in->format = input; |
| 125 | in->param = NULL; |
| 126 | |
| 127 | ret = in->format->init(in, "nonexisting.dat"); |
| 128 | fail_unless(ret == SR_OK, "Failed to init '%s' input module: %d.", |
| 129 | input->id, ret); |
| 130 | |
| 131 | g_free(in); |
| 132 | } |
| 133 | |
| 134 | /* Initialize all libsigrok input modules. */ |
| 135 | void srtest_input_init_all(struct sr_context *sr_ctx) |
| 136 | { |
| 137 | struct sr_input_format **inputs; |
| 138 | int i; |
| 139 | |
| 140 | inputs = sr_input_list(); |
| 141 | fail_unless(inputs != NULL, "No input modules found."); |
| 142 | |
| 143 | for (i = 0; inputs[i]; i++) |
| 144 | srtest_input_init(sr_ctx, inputs[i]); |
| 145 | } |
| 146 | |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 147 | /* Set the samplerate for the respective driver to the specified value. */ |
| 148 | void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate) |
| 149 | { |
| 150 | int ret; |
| 151 | struct sr_dev_inst *sdi; |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 152 | GVariant *gvar; |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 153 | |
| 154 | sdi = g_slist_nth_data(driver->priv, 0); |
| 155 | |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 156 | gvar = g_variant_new_uint64(samplerate); |
Uwe Hermann | 57d0a2e | 2013-11-10 23:22:18 +0100 | [diff] [blame] | 157 | ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL); |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 158 | g_variant_unref(gvar); |
| 159 | |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 160 | fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.", |
| 161 | driver->name, ret); |
| 162 | } |
| 163 | |
| 164 | /* Get the respective driver's current samplerate. */ |
| 165 | uint64_t srtest_get_samplerate(struct sr_dev_driver *driver) |
| 166 | { |
| 167 | int ret; |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 168 | uint64_t samplerate; |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 169 | struct sr_dev_inst *sdi; |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 170 | GVariant *gvar; |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 171 | |
| 172 | sdi = g_slist_nth_data(driver->priv, 0); |
| 173 | |
Uwe Hermann | 57d0a2e | 2013-11-10 23:22:18 +0100 | [diff] [blame] | 174 | ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL); |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 175 | samplerate = g_variant_get_uint64(gvar); |
| 176 | g_variant_unref(gvar); |
| 177 | |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 178 | fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.", |
| 179 | driver->name, ret); |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 180 | |
Uwe Hermann | 34e4c27 | 2013-04-12 17:59:38 +0200 | [diff] [blame] | 181 | return samplerate; |
Uwe Hermann | 79bb0e9 | 2013-03-07 09:37:42 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Check whether the respective driver can set/get the correct samplerate. */ |
| 185 | void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername, |
| 186 | uint64_t samplerate) |
| 187 | { |
| 188 | struct sr_dev_driver *driver; |
| 189 | uint64_t s; |
| 190 | |
| 191 | driver = srtest_driver_get(drivername); |
| 192 | srtest_driver_init(sr_ctx, driver);; |
| 193 | srtest_set_samplerate(driver, samplerate); |
| 194 | s = srtest_get_samplerate(driver); |
| 195 | fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".", |
| 196 | drivername, s); |
| 197 | } |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 198 | |
| 199 | void srtest_buf_to_file(const char *filename, const uint8_t *buf, uint64_t len) |
| 200 | { |
| 201 | FILE *f; |
Marvin Schmidt | c3e2b08 | 2013-11-26 08:50:52 +0100 | [diff] [blame] | 202 | GError *error = NULL; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 203 | gboolean ret; |
| 204 | |
| 205 | f = g_fopen(filename, "wb"); |
| 206 | fail_unless(f != NULL); |
| 207 | |
| 208 | ret = g_file_set_contents(filename, (const gchar *)buf, len, &error); |
| 209 | fail_unless(ret == TRUE); |
| 210 | |
| 211 | fclose(f); |
| 212 | } |
| 213 | |
| 214 | GArray *srtest_get_enabled_logic_probes(const struct sr_dev_inst *sdi) |
| 215 | { |
Uwe Hermann | ba7dd8b | 2014-03-24 21:34:20 +0100 | [diff] [blame] | 216 | struct sr_channel *ch; |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 217 | GArray *probes; |
| 218 | GSList *l; |
| 219 | |
| 220 | probes = g_array_new(FALSE, FALSE, sizeof(int)); |
| 221 | for (l = sdi->probes; l; l = l->next) { |
| 222 | probe = l->data; |
Uwe Hermann | 3f239f0 | 2014-03-24 22:39:42 +0100 | [diff] [blame] | 223 | if (probe->type != SR_CHANNEL_LOGIC) |
Uwe Hermann | 71185b4 | 2013-03-13 09:46:08 +0100 | [diff] [blame] | 224 | continue; |
| 225 | if (probe->enabled != TRUE) |
| 226 | continue; |
| 227 | g_array_append_val(probes, probe->index); |
| 228 | } |
| 229 | |
| 230 | return probes; |
| 231 | } |