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 "../include/libsigrok/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 uint64_t df_packet_counter = 0, sample_counter = 0;
34 static gboolean have_seen_df_end = FALSE;
35 static GArray *logic_channellist = NULL;
36 static int check_to_perform;
37 static uint64_t expected_samples;
38 static uint64_t *expected_samplerate;
40 static void check_all_low(const struct sr_datafeed_logic *logic)
45 for (i = 0; i < logic->length; i++) {
47 if (data[i * logic->unitsize] != 0)
48 fail("Logic data was not all-0x00.");
52 static void check_all_high(const struct sr_datafeed_logic *logic)
57 for (i = 0; i < logic->length; i++) {
59 if (data[i * logic->unitsize] != 0xff)
60 fail("Logic data was not all-0xff.");
64 static void check_hello_world(const struct sr_datafeed_logic *logic)
68 const char *h = "Hello world";
70 for (i = 0; i < logic->length; i++) {
72 b = data[sample_counter + i];
73 if (b != h[sample_counter + i])
74 fail("Logic data was not 'Hello world'.");
78 static void datafeed_in(const struct sr_dev_inst *sdi,
79 const struct sr_datafeed_packet *packet, void *cb_data)
81 const struct sr_datafeed_meta *meta;
82 const struct sr_datafeed_logic *logic;
83 struct sr_config *src;
84 uint64_t samplerate, sample_interval;
90 fail_unless(sdi != NULL);
91 fail_unless(packet != NULL);
93 if (df_packet_counter++ == 0)
94 fail_unless(packet->type == SR_DF_HEADER,
95 "The first packet must be an SR_DF_HEADER.");
98 fail("There must be no packets after an SR_DF_END, but we "
99 "received a packet of type %d.", packet->type);
103 switch (packet->type) {
105 // g_debug("Received SR_DF_HEADER.");
106 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
108 logic_channellist = srtest_get_enabled_logic_channels(sdi);
109 fail_unless(logic_channellist != NULL);
110 fail_unless(logic_channellist->len != 0);
111 // g_debug("Enabled channels: %d.", logic_channellist->len);
114 // g_debug("Received SR_DF_META.");
116 meta = packet->payload;
117 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
119 for (l = meta->config; l; l = l->next) {
121 // g_debug("Got meta key: %d.", src->key);
123 case SR_CONF_SAMPLERATE:
124 samplerate = g_variant_get_uint64(src->data);
125 if (!expected_samplerate)
127 fail_unless(samplerate == *expected_samplerate,
128 "Expected samplerate=%" PRIu64 ", "
129 "got %" PRIu64 "", samplerate,
130 *expected_samplerate);
131 // g_debug("samplerate = %" PRIu64 " Hz.",
134 case SR_CONF_SAMPLE_INTERVAL:
135 sample_interval = g_variant_get_uint64(src->data);
136 (void)sample_interval;
137 // g_debug("sample interval = %" PRIu64 " ms.",
141 /* Unknown metadata is not an error. */
142 g_debug("Got unknown meta key: %d.", src->key);
148 logic = packet->payload;
149 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
151 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
152 // "unitsize %d).", logic->length, logic->unitsize);
154 if (check_to_perform == CHECK_ALL_LOW)
155 check_all_low(logic);
156 else if (check_to_perform == CHECK_ALL_HIGH)
157 check_all_high(logic);
158 else if (check_to_perform == CHECK_HELLO_WORLD)
159 check_hello_world(logic);
161 sample_counter += logic->length / logic->unitsize;
165 // g_debug("Received SR_DF_END.");
166 // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
167 have_seen_df_end = TRUE;
168 if (sample_counter != expected_samples)
169 fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
170 expected_samples, sample_counter);
174 * Note: The binary input format doesn't support SR_DF_TRIGGER
175 * and some other types, those should yield an error.
177 fail("Invalid packet type: %d.", packet->type);
182 static void check_buf(const char *filename, GHashTable *param,
183 const uint8_t *buf, int check, uint64_t samples,
184 uint64_t *samplerate)
188 struct sr_input_format *in_format;
189 struct sr_session *session;
191 /* Initialize global variables for this run. */
192 df_packet_counter = sample_counter = 0;
193 have_seen_df_end = FALSE;
194 logic_channellist = NULL;
195 check_to_perform = check;
196 expected_samples = samples;
197 expected_samplerate = samplerate;
199 in_format = srtest_input_get("binary");
201 in = g_try_malloc0(sizeof(struct sr_input));
202 fail_unless(in != NULL);
204 in->format = in_format;
207 srtest_buf_to_file(filename, buf, samples); /* Create a file. */
209 ret = in->format->init(in, filename);
210 fail_unless(ret == SR_OK, "Input format init error: %d", ret);
212 sr_session_new(&session);
213 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
214 sr_session_dev_add(session, in->sdi);
215 in_format->loadfile(in, filename);
216 sr_session_destroy(session);
218 g_unlink(filename); /* Delete file again. */
221 START_TEST(test_input_binary_all_low)
223 uint64_t i, samplerate;
227 buf = g_try_malloc0(MAX_FILESIZE);
228 fail_unless(buf != NULL);
230 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
231 fail_unless(param != NULL);
232 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
233 samplerate = SR_HZ(1250);
235 /* Check various filesizes, with/without specifying a samplerate. */
236 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
237 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate);
238 for (i = 1; i < MAX_FILESIZE; i *= 3) {
239 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL);
240 check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate);
244 g_hash_table_destroy(param);
249 START_TEST(test_input_binary_all_high)
254 buf = g_try_malloc(MAX_FILESIZE);
255 memset(buf, 0xff, MAX_FILESIZE);
257 check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL);
258 for (i = 1; i < MAX_FILESIZE; i *= 3)
259 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL);
265 START_TEST(test_input_binary_all_high_loop)
269 /* Note: _i is the loop variable from tcase_add_loop_test(). */
271 buf = g_try_malloc((_i * 10) + 1);
272 memset(buf, 0xff, _i * 10);
274 check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL);
280 START_TEST(test_input_binary_hello_world)
286 buf = (uint8_t *)g_strdup("Hello world");
288 param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
289 fail_unless(param != NULL);
290 g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250"));
291 samplerate = SR_HZ(1250);
293 /* Check with and without specifying a samplerate. */
294 check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
295 check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate);
297 g_hash_table_destroy(param);
302 Suite *suite_input_binary(void)
307 s = suite_create("input-binary");
309 tc = tcase_create("basic");
310 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
311 tcase_add_test(tc, test_input_binary_all_low);
312 tcase_add_test(tc, test_input_binary_all_high);
313 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10);
314 tcase_add_test(tc, test_input_binary_hello_world);
315 suite_add_tcase(s, tc);