2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013-2014 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, see <http://www.gnu.org/licenses/>.
22 #include <glib/gstdio.h>
23 #include <libsigrok/libsigrok.h>
26 #define BUFSIZE (1000 * 1000)
34 static uint64_t df_packet_counter = 0, sample_counter = 0;
35 static gboolean have_seen_df_end = FALSE;
36 static GArray *logic_channellist = NULL;
37 static int check_to_perform;
38 static uint64_t expected_samples;
39 static uint64_t *expected_samplerate;
41 static void check_all_low(const struct sr_datafeed_logic *logic)
46 for (i = 0; i < logic->length; i++) {
48 if (data[i * logic->unitsize] != 0)
49 fail("Logic data was not all-0x00.");
53 static void check_all_high(const struct sr_datafeed_logic *logic)
58 for (i = 0; i < logic->length; i++) {
60 if (data[i * logic->unitsize] != 0xff)
61 fail("Logic data was not all-0xff.");
65 static void check_hello_world(const struct sr_datafeed_logic *logic)
69 const char *h = "Hello world";
71 for (i = 0; i < logic->length; i++) {
73 b = data[sample_counter + i];
74 if (b != h[sample_counter + i])
75 fail("Logic data was not 'Hello world'.");
79 static void datafeed_in(const struct sr_dev_inst *sdi,
80 const struct sr_datafeed_packet *packet, void *cb_data)
82 const struct sr_datafeed_meta *meta;
83 const struct sr_datafeed_logic *logic;
84 struct sr_config *src;
85 uint64_t samplerate, sample_interval;
91 fail_unless(sdi != NULL);
92 fail_unless(packet != NULL);
94 if (df_packet_counter++ == 0)
95 fail_unless(packet->type == SR_DF_HEADER,
96 "The first packet must be an SR_DF_HEADER.");
99 fail("There must be no packets after an SR_DF_END, but we "
100 "received a packet of type %d.", packet->type);
104 switch (packet->type) {
106 // g_debug("Received SR_DF_HEADER.");
107 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
109 logic_channellist = srtest_get_enabled_logic_channels(sdi);
110 fail_unless(logic_channellist != NULL);
111 fail_unless(logic_channellist->len != 0);
112 // g_debug("Enabled channels: %d.", logic_channellist->len);
115 // g_debug("Received SR_DF_META.");
117 meta = packet->payload;
118 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
120 for (l = meta->config; l; l = l->next) {
122 // g_debug("Got meta key: %d.", src->key);
124 case SR_CONF_SAMPLERATE:
125 samplerate = g_variant_get_uint64(src->data);
126 if (!expected_samplerate)
128 fail_unless(samplerate == *expected_samplerate,
129 "Expected samplerate=%" PRIu64 ", "
130 "got %" PRIu64 "", samplerate,
131 *expected_samplerate);
132 // g_debug("samplerate = %" PRIu64 " Hz.",
135 case SR_CONF_SAMPLE_INTERVAL:
136 sample_interval = g_variant_get_uint64(src->data);
137 (void)sample_interval;
138 // g_debug("sample interval = %" PRIu64 " ms.",
142 /* Unknown metadata is not an error. */
143 g_debug("Got unknown meta key: %d.", src->key);
149 logic = packet->payload;
150 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
152 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
153 // "unitsize %d).", logic->length, logic->unitsize);
155 if (check_to_perform == CHECK_ALL_LOW)
156 check_all_low(logic);
157 else if (check_to_perform == CHECK_ALL_HIGH)
158 check_all_high(logic);
159 else if (check_to_perform == CHECK_HELLO_WORLD)
160 check_hello_world(logic);
162 sample_counter += logic->length / logic->unitsize;
166 // g_debug("Received SR_DF_END.");
167 // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
168 have_seen_df_end = TRUE;
169 if (sample_counter != expected_samples)
170 fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
171 expected_samples, sample_counter);
175 * Note: The binary input format doesn't support SR_DF_TRIGGER
176 * and some other types, those should yield an error.
178 fail("Invalid packet type: %d.", packet->type);
183 static void check_buf(GHashTable *options, const uint8_t *buf, int check,
184 uint64_t samples, uint64_t *samplerate)
188 const struct sr_input_module *imod;
189 struct sr_session *session;
190 struct sr_dev_inst *sdi;
193 /* Initialize global variables for this run. */
194 df_packet_counter = sample_counter = 0;
195 have_seen_df_end = FALSE;
196 logic_channellist = NULL;
197 check_to_perform = check;
198 expected_samples = samples;
199 expected_samplerate = samplerate;
201 gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
203 imod = sr_input_find("binary");
204 fail_unless(imod != NULL, "Failed to find input module.");
206 in = sr_input_new(imod, options);
207 fail_unless(in != NULL, "Failed to create input instance.");
209 sdi = sr_input_dev_inst_get(in);
211 sr_session_new(srtest_ctx, &session);
212 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
213 sr_session_dev_add(session, sdi);
215 ret = sr_input_send(in, gbuf);
216 fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
219 sr_session_destroy(session);
221 g_string_free(gbuf, TRUE);
224 START_TEST(test_input_binary_all_low)
226 uint64_t i, samplerate;
231 buf = g_malloc0(BUFSIZE);
233 gvar = g_variant_new_uint64(1250);
234 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
235 (GDestroyNotify)g_variant_unref);
236 g_hash_table_insert(options, g_strdup("samplerate"),
237 g_variant_ref_sink(gvar));
238 samplerate = SR_HZ(1250);
240 /* Check various filesizes, with/without specifying a samplerate. */
241 check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL);
242 check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate);
243 for (i = 1; i < BUFSIZE; i *= 3) {
244 check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL);
245 check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate);
248 g_hash_table_destroy(options);
253 START_TEST(test_input_binary_all_high)
258 buf = g_malloc(BUFSIZE);
259 memset(buf, 0xff, BUFSIZE);
261 check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL);
262 for (i = 1; i < BUFSIZE; i *= 3)
263 check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL);
269 START_TEST(test_input_binary_all_high_loop)
274 /* Note: _i is the loop variable from tcase_add_loop_test(). */
277 buf = g_malloc(BUFSIZE);
278 memset(buf, 0xff, BUFSIZE);
280 check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
286 START_TEST(test_input_binary_hello_world)
293 buf = (uint8_t *)g_strdup("Hello world");
295 gvar = g_variant_new_uint64(1250);
296 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
297 (GDestroyNotify)g_variant_unref);
298 g_hash_table_insert(options, g_strdup("samplerate"),
299 g_variant_ref_sink(gvar));
300 samplerate = SR_HZ(1250);
302 /* Check with and without specifying a samplerate. */
303 check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
304 check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
306 g_hash_table_destroy(options);
311 Suite *suite_input_binary(void)
316 s = suite_create("input-binary");
318 tc = tcase_create("basic");
319 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
320 tcase_add_test(tc, test_input_binary_all_low);
321 tcase_add_test(tc, test_input_binary_all_high);
322 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
323 tcase_add_test(tc, test_input_binary_hello_world);
324 suite_add_tcase(s, tc);