1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <check.h>
#include <glib/gstdio.h>
#include <libsigrok/libsigrok.h>
#include "lib.h"
#define BUFSIZE (1000 * 1000)
enum {
CHECK_ALL_LOW,
CHECK_ALL_HIGH,
CHECK_HELLO_WORLD,
};
static uint64_t df_packet_counter = 0, sample_counter = 0;
static gboolean have_seen_df_end = FALSE;
static GArray *logic_channellist = NULL;
static int check_to_perform;
static uint64_t expected_samples;
static uint64_t *expected_samplerate;
static void check_all_low(const struct sr_datafeed_logic *logic)
{
uint64_t i;
uint8_t *data;
for (i = 0; i < logic->length; i++) {
data = logic->data;
if (data[i * logic->unitsize] != 0)
fail("Logic data was not all-0x00.");
}
}
static void check_all_high(const struct sr_datafeed_logic *logic)
{
uint64_t i;
uint8_t *data;
for (i = 0; i < logic->length; i++) {
data = logic->data;
if (data[i * logic->unitsize] != 0xff)
fail("Logic data was not all-0xff.");
}
}
static void check_hello_world(const struct sr_datafeed_logic *logic)
{
uint64_t i;
uint8_t *data, b;
const char *h = "Hello world";
for (i = 0; i < logic->length; i++) {
data = logic->data;
b = data[sample_counter + i];
if (b != h[sample_counter + i])
fail("Logic data was not 'Hello world'.");
}
}
static void datafeed_in(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *packet, void *cb_data)
{
const struct sr_datafeed_meta *meta;
const struct sr_datafeed_logic *logic;
struct sr_config *src;
uint64_t samplerate, sample_interval;
GSList *l;
const void *p;
(void)cb_data;
fail_unless(sdi != NULL);
fail_unless(packet != NULL);
if (df_packet_counter++ == 0)
fail_unless(packet->type == SR_DF_HEADER,
"The first packet must be an SR_DF_HEADER.");
if (have_seen_df_end)
fail("There must be no packets after an SR_DF_END, but we "
"received a packet of type %d.", packet->type);
p = packet->payload;
switch (packet->type) {
case SR_DF_HEADER:
// g_debug("Received SR_DF_HEADER.");
// fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
logic_channellist = srtest_get_enabled_logic_channels(sdi);
fail_unless(logic_channellist != NULL);
fail_unless(logic_channellist->len != 0);
// g_debug("Enabled channels: %d.", logic_channellist->len);
break;
case SR_DF_META:
// g_debug("Received SR_DF_META.");
meta = packet->payload;
fail_unless(p != NULL, "SR_DF_META payload was NULL.");
for (l = meta->config; l; l = l->next) {
src = l->data;
// g_debug("Got meta key: %d.", src->key);
switch (src->key) {
case SR_CONF_SAMPLERATE:
samplerate = g_variant_get_uint64(src->data);
if (!expected_samplerate)
break;
fail_unless(samplerate == *expected_samplerate,
"Expected samplerate=%" PRIu64 ", "
"got %" PRIu64 "", samplerate,
*expected_samplerate);
// g_debug("samplerate = %" PRIu64 " Hz.",
// samplerate);
break;
case SR_CONF_SAMPLE_INTERVAL:
sample_interval = g_variant_get_uint64(src->data);
(void)sample_interval;
// g_debug("sample interval = %" PRIu64 " ms.",
// sample_interval);
break;
default:
/* Unknown metadata is not an error. */
g_debug("Got unknown meta key: %d.", src->key);
break;
}
}
break;
case SR_DF_LOGIC:
logic = packet->payload;
fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
// g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
// "unitsize %d).", logic->length, logic->unitsize);
if (check_to_perform == CHECK_ALL_LOW)
check_all_low(logic);
else if (check_to_perform == CHECK_ALL_HIGH)
check_all_high(logic);
else if (check_to_perform == CHECK_HELLO_WORLD)
check_hello_world(logic);
sample_counter += logic->length / logic->unitsize;
break;
case SR_DF_END:
// g_debug("Received SR_DF_END.");
// fail_unless(p != NULL, "SR_DF_END payload was NULL.");
have_seen_df_end = TRUE;
if (sample_counter != expected_samples)
fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
expected_samples, sample_counter);
break;
default:
/*
* Note: The binary input format doesn't support SR_DF_TRIGGER
* and some other types, those should yield an error.
*/
fail("Invalid packet type: %d.", packet->type);
break;
}
}
static void check_buf(GHashTable *options, const uint8_t *buf, int check,
uint64_t samples, uint64_t *samplerate)
{
int ret;
struct sr_input *in;
const struct sr_input_module *imod;
struct sr_session *session;
struct sr_dev_inst *sdi;
GString *gbuf;
/* Initialize global variables for this run. */
df_packet_counter = sample_counter = 0;
have_seen_df_end = FALSE;
logic_channellist = NULL;
check_to_perform = check;
expected_samples = samples;
expected_samplerate = samplerate;
gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
imod = sr_input_find("binary");
fail_unless(imod != NULL, "Failed to find input module.");
in = sr_input_new(imod, options);
fail_unless(in != NULL, "Failed to create input instance.");
sdi = sr_input_dev_inst_get(in);
sr_session_new(srtest_ctx, &session);
sr_session_datafeed_callback_add(session, datafeed_in, NULL);
sr_session_dev_add(session, sdi);
ret = sr_input_send(in, gbuf);
fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
sr_input_free(in);
sr_session_destroy(session);
g_string_free(gbuf, TRUE);
}
START_TEST(test_input_binary_all_low)
{
uint64_t i, samplerate;
GHashTable *options;
uint8_t *buf;
GVariant *gvar;
buf = g_malloc0(BUFSIZE);
gvar = g_variant_new_uint64(1250);
options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)g_variant_unref);
g_hash_table_insert(options, g_strdup("samplerate"),
g_variant_ref_sink(gvar));
samplerate = SR_HZ(1250);
/* Check various filesizes, with/without specifying a samplerate. */
check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL);
check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate);
for (i = 1; i < BUFSIZE; i *= 3) {
check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL);
check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate);
}
g_hash_table_destroy(options);
g_free(buf);
}
END_TEST
START_TEST(test_input_binary_all_high)
{
uint64_t i;
uint8_t *buf;
buf = g_malloc(BUFSIZE);
memset(buf, 0xff, BUFSIZE);
check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL);
for (i = 1; i < BUFSIZE; i *= 3)
check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL);
g_free(buf);
}
END_TEST
START_TEST(test_input_binary_all_high_loop)
{
uint8_t *buf;
uint64_t bufsize;
/* Note: _i is the loop variable from tcase_add_loop_test(). */
bufsize = (_i * 10);
buf = g_malloc(BUFSIZE);
memset(buf, 0xff, BUFSIZE);
check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
g_free(buf);
}
END_TEST
START_TEST(test_input_binary_hello_world)
{
uint64_t samplerate;
uint8_t *buf;
GHashTable *options;
GVariant *gvar;
buf = (uint8_t *)g_strdup("Hello world");
gvar = g_variant_new_uint64(1250);
options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)g_variant_unref);
g_hash_table_insert(options, g_strdup("samplerate"),
g_variant_ref_sink(gvar));
samplerate = SR_HZ(1250);
/* Check with and without specifying a samplerate. */
check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
g_hash_table_destroy(options);
g_free(buf);
}
END_TEST
Suite *suite_input_binary(void)
{
Suite *s;
TCase *tc;
s = suite_create("input-binary");
tc = tcase_create("basic");
tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
tcase_add_test(tc, test_input_binary_all_low);
tcase_add_test(tc, test_input_binary_all_high);
tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
tcase_add_test(tc, test_input_binary_hello_world);
suite_add_tcase(s, tc);
return s;
}
|