2013-03-13 08:46:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the libsigrok project.
|
|
|
|
*
|
2014-08-29 19:32:34 +00:00
|
|
|
* Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de>
|
2013-03-13 08:46:08 +00:00
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <check.h>
|
|
|
|
#include <glib/gstdio.h>
|
2014-07-20 10:03:29 +00:00
|
|
|
#include "../include/libsigrok/libsigrok.h"
|
2013-03-13 08:46:08 +00:00
|
|
|
#include "lib.h"
|
|
|
|
|
2015-03-22 22:07:30 +00:00
|
|
|
#define BUFSIZE (1000 * 1000)
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
enum {
|
|
|
|
CHECK_ALL_LOW,
|
|
|
|
CHECK_ALL_HIGH,
|
|
|
|
CHECK_HELLO_WORLD,
|
|
|
|
};
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
static uint64_t df_packet_counter = 0, sample_counter = 0;
|
|
|
|
static gboolean have_seen_df_end = FALSE;
|
2014-03-24 15:11:45 +00:00
|
|
|
static GArray *logic_channellist = NULL;
|
2013-03-13 08:46:08 +00:00
|
|
|
static int check_to_perform;
|
|
|
|
static uint64_t expected_samples;
|
|
|
|
static uint64_t *expected_samplerate;
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
static void check_all_low(const struct sr_datafeed_logic *logic)
|
2013-03-13 08:46:08 +00:00
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
uint8_t *data;
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
for (i = 0; i < logic->length; i++) {
|
2013-03-13 08:46:08 +00:00
|
|
|
data = logic->data;
|
|
|
|
if (data[i * logic->unitsize] != 0)
|
|
|
|
fail("Logic data was not all-0x00.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
static void check_all_high(const struct sr_datafeed_logic *logic)
|
2013-03-13 08:46:08 +00:00
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
uint8_t *data;
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
for (i = 0; i < logic->length; i++) {
|
2013-03-13 08:46:08 +00:00
|
|
|
data = logic->data;
|
|
|
|
if (data[i * logic->unitsize] != 0xff)
|
|
|
|
fail("Logic data was not all-0xff.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
static void check_hello_world(const struct sr_datafeed_logic *logic)
|
2013-03-13 08:46:08 +00:00
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
uint8_t *data, b;
|
|
|
|
const char *h = "Hello world";
|
|
|
|
|
2014-09-10 21:55:15 +00:00
|
|
|
for (i = 0; i < logic->length; i++) {
|
2013-03-13 08:46:08 +00:00
|
|
|
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.");
|
|
|
|
|
2014-03-24 15:11:45 +00:00
|
|
|
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);
|
2013-03-13 08:46:08 +00:00
|
|
|
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)
|
2014-09-10 21:55:15 +00:00
|
|
|
check_all_low(logic);
|
2013-03-13 08:46:08 +00:00
|
|
|
else if (check_to_perform == CHECK_ALL_HIGH)
|
2014-09-10 21:55:15 +00:00
|
|
|
check_all_high(logic);
|
2013-03-13 08:46:08 +00:00
|
|
|
else if (check_to_perform == CHECK_HELLO_WORLD)
|
2014-09-10 21:55:15 +00:00
|
|
|
check_hello_world(logic);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
static void check_buf(GHashTable *options, const uint8_t *buf, int check,
|
|
|
|
uint64_t samples, uint64_t *samplerate)
|
2013-03-13 08:46:08 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sr_input *in;
|
2014-08-29 19:32:34 +00:00
|
|
|
const struct sr_input_module *imod;
|
2014-07-20 10:09:10 +00:00
|
|
|
struct sr_session *session;
|
2014-08-29 19:32:34 +00:00
|
|
|
struct sr_dev_inst *sdi;
|
|
|
|
GString *gbuf;
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
/* Initialize global variables for this run. */
|
|
|
|
df_packet_counter = sample_counter = 0;
|
|
|
|
have_seen_df_end = FALSE;
|
2014-03-24 15:11:45 +00:00
|
|
|
logic_channellist = NULL;
|
2013-03-13 08:46:08 +00:00
|
|
|
check_to_perform = check;
|
|
|
|
expected_samples = samples;
|
|
|
|
expected_samplerate = samplerate;
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
imod = sr_input_find("binary");
|
|
|
|
fail_unless(imod != NULL, "Failed to find input module.");
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
in = sr_input_new(imod, options);
|
|
|
|
fail_unless(in != NULL, "Failed to create input instance.");
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
sdi = sr_input_dev_inst_get(in);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2015-03-26 22:49:35 +00:00
|
|
|
sr_session_new(srtest_ctx, &session);
|
2014-07-20 10:09:10 +00:00
|
|
|
sr_session_datafeed_callback_add(session, datafeed_in, NULL);
|
2014-08-29 19:32:34 +00:00
|
|
|
sr_session_dev_add(session, sdi);
|
|
|
|
|
|
|
|
ret = sr_input_send(in, gbuf);
|
|
|
|
fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
|
2014-10-03 18:11:47 +00:00
|
|
|
sr_input_free(in);
|
2014-08-29 19:32:34 +00:00
|
|
|
|
2014-07-20 10:09:10 +00:00
|
|
|
sr_session_destroy(session);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
g_string_free(gbuf, TRUE);
|
2013-03-13 08:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(test_input_binary_all_low)
|
|
|
|
{
|
|
|
|
uint64_t i, samplerate;
|
2014-08-29 19:32:34 +00:00
|
|
|
GHashTable *options;
|
2013-03-13 08:46:08 +00:00
|
|
|
uint8_t *buf;
|
2014-08-29 19:32:34 +00:00
|
|
|
GVariant *gvar;
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
buf = g_malloc0(BUFSIZE);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
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));
|
2013-03-13 08:46:08 +00:00
|
|
|
samplerate = SR_HZ(1250);
|
|
|
|
|
|
|
|
/* Check various filesizes, with/without specifying a samplerate. */
|
2014-08-29 19:32:34 +00:00
|
|
|
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);
|
2013-03-13 08:46:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
g_hash_table_destroy(options);
|
2013-03-13 08:46:08 +00:00
|
|
|
g_free(buf);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_input_binary_all_high)
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
uint8_t *buf;
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
buf = g_malloc(BUFSIZE);
|
|
|
|
memset(buf, 0xff, BUFSIZE);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
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);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_input_binary_all_high_loop)
|
|
|
|
{
|
|
|
|
uint8_t *buf;
|
2014-08-29 19:32:34 +00:00
|
|
|
uint64_t bufsize;
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
/* Note: _i is the loop variable from tcase_add_loop_test(). */
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
bufsize = (_i * 10);
|
|
|
|
buf = g_malloc(BUFSIZE);
|
|
|
|
memset(buf, 0xff, BUFSIZE);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_input_binary_hello_world)
|
|
|
|
{
|
|
|
|
uint64_t samplerate;
|
|
|
|
uint8_t *buf;
|
2014-08-29 19:32:34 +00:00
|
|
|
GHashTable *options;
|
|
|
|
GVariant *gvar;
|
2013-03-13 08:46:08 +00:00
|
|
|
|
|
|
|
buf = (uint8_t *)g_strdup("Hello world");
|
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
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));
|
2013-03-13 08:46:08 +00:00
|
|
|
samplerate = SR_HZ(1250);
|
|
|
|
|
|
|
|
/* Check with and without specifying a samplerate. */
|
2014-08-29 19:32:34 +00:00
|
|
|
check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
|
|
|
|
check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
|
2013-03-13 08:46:08 +00:00
|
|
|
|
2014-08-29 19:32:34 +00:00
|
|
|
g_hash_table_destroy(options);
|
2013-03-13 08:46:08 +00:00
|
|
|
g_free(buf);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
Suite *suite_input_binary(void)
|
|
|
|
{
|
|
|
|
Suite *s;
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
s = suite_create("input-binary");
|
|
|
|
|
|
|
|
tc = tcase_create("basic");
|
2014-08-11 11:20:50 +00:00
|
|
|
tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
|
2013-03-13 08:46:08 +00:00
|
|
|
tcase_add_test(tc, test_input_binary_all_low);
|
|
|
|
tcase_add_test(tc, test_input_binary_all_high);
|
2014-08-29 19:32:34 +00:00
|
|
|
tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
|
2013-03-13 08:46:08 +00:00
|
|
|
tcase_add_test(tc, test_input_binary_hello_world);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|