2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
22 #include <glib/gstdio.h>
23 #include "../libsigrok.h"
26 #define FILENAME "foo.dat"
27 #define MAX_FILESIZE (1 * 1000000)
29 #define CHECK_ALL_LOW 0
30 #define CHECK_ALL_HIGH 1
31 #define CHECK_HELLO_WORLD 2
33 static struct sr_context *sr_ctx;
35 static uint64_t df_packet_counter = 0, sample_counter = 0;
36 static gboolean have_seen_df_end = FALSE;
37 static GArray *logic_channellist = NULL;
38 static int check_to_perform;
39 static uint64_t expected_samples;
40 static uint64_t *expected_samplerate;
42 static void setup(void)
46 ret = sr_init(&sr_ctx);
47 fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret);
50 static void teardown(void)
54 ret = sr_exit(sr_ctx);
55 fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret);
58 static void check_all_low(const struct sr_datafeed_logic *logic)
63 for (i = 0; i < logic->length; i++) {
65 if (data[i * logic->unitsize] != 0)
66 fail("Logic data was not all-0x00.");
70 static void check_all_high(const struct sr_datafeed_logic *logic)
75 for (i = 0; i < logic->length; i++) {
77 if (data[i * logic->unitsize] != 0xff)
78 fail("Logic data was not all-0xff.");
82 static void check_hello_world(const struct sr_datafeed_logic *logic)
86 const char *h = "Hello world";
88 for (i = 0; i < logic->length; i++) {
90 b = data[sample_counter + i];
91 if (b != h[sample_counter + i])
92 fail("Logic data was not 'Hello world'.");
96 static void datafeed_in(const struct sr_dev_inst *sdi,
97 const struct sr_datafeed_packet *packet, void *cb_data)
99 const struct sr_datafeed_meta *meta;
100 const struct sr_datafeed_logic *logic;
101 struct sr_config *src;
102 uint64_t samplerate, sample_interval;
108 fail_unless(sdi != NULL);
109 fail_unless(packet != NULL);
111 if (df_packet_counter++ == 0)
112 fail_unless(packet->type == SR_DF_HEADER,
113 "The first packet must be an SR_DF_HEADER.");
115 if (have_seen_df_end)
116 fail("There must be no packets after an SR_DF_END, but we "
117 "received a packet of type %d.", packet->type);
121 switch (packet->type) {
123 // g_debug("Received SR_DF_HEADER.");
124 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
126 logic_channellist = srtest_get_enabled_logic_channels(sdi);
127 fail_unless(logic_channellist != NULL);
128 fail_unless(logic_channellist->len != 0);
129 // g_debug("Enabled channels: %d.", logic_channellist->len);
132 // g_debug("Received SR_DF_META.");
134 meta = packet->payload;
135 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
137 for (l = meta->config; l; l = l->next) {
139 // g_debug("Got meta key: %d.", src->key);
141 case SR_CONF_SAMPLERATE:
142 samplerate = g_variant_get_uint64(src->data);
143 if (!expected_samplerate)
145 fail_unless(samplerate == *expected_samplerate,
146 "Expected samplerate=%" PRIu64 ", "
147 "got %" PRIu64 "", samplerate,
148 *expected_samplerate);
149 // g_debug("samplerate = %" PRIu64 " Hz.",
152 case SR_CONF_SAMPLE_INTERVAL:
153 sample_interval = g_variant_get_uint64(src->data);
154 (void)sample_interval;
155 // g_debug("sample interval = %" PRIu64 " ms.",
159 /* Unknown metadata is not an error. */
160 g_debug("Got unknown meta key: %d.", src->key);
166 logic = packet->payload;
167 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
169 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
170 // "unitsize %d).", logic->length, logic->unitsize);
172 if (check_to_perform == CHECK_ALL_LOW)
173 check_all_low(logic);
174 else if (check_to_perform == CHECK_ALL_HIGH)
175 check_all_high(logic);
176 else if (check_to_perform == CHECK_HELLO_WORLD)
177 check_hello_world(logic);
179 sample_counter += logic->length / logic->unitsize;
183 // g_debug("Received SR_DF_END.");
184 // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
185 have_seen_df_end = TRUE;
186 if (sample_counter != expected_samples)
187 fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
188 expected_samples, sample_counter);
192 * Note: The binary input format doesn't support SR_DF_TRIGGER
193 * and some other types, those should yield an error.
195 fail("Invalid packet type: %d.", packet->type);
200 static void check_buf(const char *filename, GHashTable *param,
201 const uint8_t *buf, int check, uint64_t samples,
202 uint64_t *samplerate)
206 struct sr_input_format *in_format;
208 /* Initialize global variables for this run. */
209 df_packet_counter = sample_counter = 0;
210 have_seen_df_end = FALSE;
211 logic_channellist = NULL;
212 check_to_perform = check;
213 expected_samples = samples;
214 expected_samplerate = samplerate;
216 in_format = srtest_input_get("binary");
218 in = g_try_malloc0(sizeof(struct sr_input));
219 fail_unless(in != NULL);
221 in->format = in_format;
224 srtest_buf_to_file(filename, buf, samples); /* Create a file. */
226 ret = in->format->init(in, filename);
227 fail_unless(ret == SR_OK, "Input format init error: %d", ret);
230 sr_session_datafeed_callback_add(datafeed_in, NULL);
231 sr_session_dev_add(in->sdi);
232 in_format->loadfile(in, filename);
233 sr_session_destroy();
235 g_unlink(filename); /* Delete file again. */
238 START_TEST(test_input_binary_all_low)
240 uint64_t i, samplerate;
244 buf = g_try_malloc0(MAX_FILESIZE);
245 fail_unless(buf != NULL);
247 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
248 fail_unless(param != NULL);
249 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
250 samplerate = SR_HZ(1250);
252 /* Check various filesizes, with/without specifying a samplerate. */
253 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
254 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate);
255 for (i = 1; i < MAX_FILESIZE; i *= 3) {
256 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL);
257 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate);
261 g_hash_table_destroy(param);
266 START_TEST(test_input_binary_all_high)
271 buf = g_try_malloc(MAX_FILESIZE);
272 memset(buf, 0xff, MAX_FILESIZE);
274 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
275 for (i = 1; i < MAX_FILESIZE; i *= 3)
276 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL);
282 START_TEST(test_input_binary_all_high_loop)
286 /* Note: _i is the loop variable from tcase_add_loop_test(). */
288 buf = g_try_malloc((_i * 10) + 1);
289 memset(buf, 0xff, _i * 10);
291 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL);
297 START_TEST(test_input_binary_hello_world)
303 buf = (uint8_t *)g_strdup("Hello world");
305 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
306 fail_unless(param != NULL);
307 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
308 samplerate = SR_HZ(1250);
310 /* Check with and without specifying a samplerate. */
311 check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
312 check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate);
314 g_hash_table_destroy(param);
319 Suite *suite_input_binary(void)
324 s = suite_create("input-binary");
326 tc = tcase_create("basic");
327 tcase_add_checked_fixture(tc, setup, teardown);
328 tcase_add_test(tc, test_input_binary_all_low);
329 tcase_add_test(tc, test_input_binary_all_high);
330 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10);
331 tcase_add_test(tc, test_input_binary_hello_world);
332 suite_add_tcase(s, tc);