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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <glib/gstdio.h>
24 #include <libsigrok/libsigrok.h>
27 #define BUFSIZE (1000 * 1000)
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 check_all_low(const struct sr_datafeed_logic *logic)
47 for (i = 0; i < logic->length; i++) {
49 if (data[i * logic->unitsize] != 0)
50 fail("Logic data was not all-0x00.");
54 static void check_all_high(const struct sr_datafeed_logic *logic)
59 for (i = 0; i < logic->length; i++) {
61 if (data[i * logic->unitsize] != 0xff)
62 fail("Logic data was not all-0xff.");
66 static void check_hello_world(const struct sr_datafeed_logic *logic)
70 const char *h = "Hello world";
72 for (i = 0; i < logic->length; i++) {
74 b = data[sample_counter + i];
75 if (b != h[sample_counter + i])
76 fail("Logic data was not 'Hello world'.");
80 static void datafeed_in(const struct sr_dev_inst *sdi,
81 const struct sr_datafeed_packet *packet, void *cb_data)
83 const struct sr_datafeed_meta *meta;
84 const struct sr_datafeed_logic *logic;
85 struct sr_config *src;
86 uint64_t samplerate, sample_interval;
92 fail_unless(sdi != NULL);
93 fail_unless(packet != NULL);
95 if (df_packet_counter++ == 0)
96 fail_unless(packet->type == SR_DF_HEADER,
97 "The first packet must be an SR_DF_HEADER.");
100 fail("There must be no packets after an SR_DF_END, but we "
101 "received a packet of type %d.", packet->type);
105 switch (packet->type) {
107 // g_debug("Received SR_DF_HEADER.");
108 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
110 logic_channellist = srtest_get_enabled_logic_channels(sdi);
111 fail_unless(logic_channellist != NULL);
112 fail_unless(logic_channellist->len != 0);
113 // g_debug("Enabled channels: %d.", logic_channellist->len);
116 // g_debug("Received SR_DF_META.");
118 meta = packet->payload;
119 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
121 for (l = meta->config; l; l = l->next) {
123 // g_debug("Got meta key: %d.", src->key);
125 case SR_CONF_SAMPLERATE:
126 samplerate = g_variant_get_uint64(src->data);
127 if (!expected_samplerate)
129 fail_unless(samplerate == *expected_samplerate,
130 "Expected samplerate=%" PRIu64 ", "
131 "got %" PRIu64 "", samplerate,
132 *expected_samplerate);
133 // g_debug("samplerate = %" PRIu64 " Hz.",
136 case SR_CONF_SAMPLE_INTERVAL:
137 sample_interval = g_variant_get_uint64(src->data);
138 (void)sample_interval;
139 // g_debug("sample interval = %" PRIu64 " ms.",
143 /* Unknown metadata is not an error. */
144 g_debug("Got unknown meta key: %d.", src->key);
150 logic = packet->payload;
151 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
153 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
154 // "unitsize %d).", logic->length, logic->unitsize);
156 if (check_to_perform == CHECK_ALL_LOW)
157 check_all_low(logic);
158 else if (check_to_perform == CHECK_ALL_HIGH)
159 check_all_high(logic);
160 else if (check_to_perform == CHECK_HELLO_WORLD)
161 check_hello_world(logic);
163 sample_counter += logic->length / logic->unitsize;
167 // g_debug("Received SR_DF_END.");
168 // fail_unless(p != NULL, "SR_DF_END payload was NULL.");
169 have_seen_df_end = TRUE;
170 if (sample_counter != expected_samples)
171 fail("Expected %" PRIu64 " samples, got %" PRIu64 "",
172 expected_samples, sample_counter);
176 * Note: The binary input format doesn't support SR_DF_TRIGGER
177 * and some other types, those should yield an error.
179 fail("Invalid packet type: %d.", packet->type);
184 static void check_buf(GHashTable *options, const uint8_t *buf, int check,
185 uint64_t samples, uint64_t *samplerate)
189 const struct sr_input_module *imod;
190 struct sr_session *session;
191 struct sr_dev_inst *sdi;
194 /* Initialize global variables for this run. */
195 df_packet_counter = sample_counter = 0;
196 have_seen_df_end = FALSE;
197 logic_channellist = NULL;
198 check_to_perform = check;
199 expected_samples = samples;
200 expected_samplerate = samplerate;
202 gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
204 imod = sr_input_find("binary");
205 fail_unless(imod != NULL, "Failed to find input module.");
207 in = sr_input_new(imod, options);
208 fail_unless(in != NULL, "Failed to create input instance.");
210 sdi = sr_input_dev_inst_get(in);
212 sr_session_new(srtest_ctx, &session);
213 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
214 sr_session_dev_add(session, sdi);
216 ret = sr_input_send(in, gbuf);
217 fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
220 sr_session_destroy(session);
222 g_string_free(gbuf, TRUE);
225 START_TEST(test_input_binary_all_low)
227 uint64_t i, samplerate;
232 buf = g_malloc0(BUFSIZE);
234 gvar = g_variant_new_uint64(1250);
235 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
236 (GDestroyNotify)g_variant_unref);
237 g_hash_table_insert(options, g_strdup("samplerate"),
238 g_variant_ref_sink(gvar));
239 samplerate = SR_HZ(1250);
241 /* Check various filesizes, with/without specifying a samplerate. */
242 check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL);
243 check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate);
244 for (i = 1; i < BUFSIZE; i *= 3) {
245 check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL);
246 check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate);
249 g_hash_table_destroy(options);
254 START_TEST(test_input_binary_all_high)
259 buf = g_malloc(BUFSIZE);
260 memset(buf, 0xff, BUFSIZE);
262 check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL);
263 for (i = 1; i < BUFSIZE; i *= 3)
264 check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL);
270 START_TEST(test_input_binary_all_high_loop)
275 /* Note: _i is the loop variable from tcase_add_loop_test(). */
278 buf = g_malloc(BUFSIZE);
279 memset(buf, 0xff, BUFSIZE);
281 check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
287 START_TEST(test_input_binary_hello_world)
294 buf = (uint8_t *)g_strdup("Hello world");
296 gvar = g_variant_new_uint64(1250);
297 options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
298 (GDestroyNotify)g_variant_unref);
299 g_hash_table_insert(options, g_strdup("samplerate"),
300 g_variant_ref_sink(gvar));
301 samplerate = SR_HZ(1250);
303 /* Check with and without specifying a samplerate. */
304 check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
305 check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
307 g_hash_table_destroy(options);
312 Suite *suite_input_binary(void)
317 s = suite_create("input-binary");
319 tc = tcase_create("basic");
320 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
321 tcase_add_test(tc, test_input_binary_all_low);
322 tcase_add_test(tc, test_input_binary_all_high);
323 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
324 tcase_add_test(tc, test_input_binary_hello_world);
325 suite_add_tcase(s, tc);