]> sigrok.org Git - libsigrok.git/blame - tests/input_binary.c
Build: Include <config.h> first in all source files
[libsigrok.git] / tests / input_binary.c
CommitLineData
71185b48
UH
1/*
2 * This file is part of the libsigrok project.
3 *
95bc7725 4 * Copyright (C) 2013-2014 Uwe Hermann <uwe@hermann-uwe.de>
71185b48
UH
5 *
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.
10 *
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.
15 *
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
19 */
20
6ec6c43b 21#include <config.h>
71185b48
UH
22#include <check.h>
23#include <glib/gstdio.h>
4960aeb0 24#include <libsigrok/libsigrok.h>
71185b48
UH
25#include "lib.h"
26
1a46cc62 27#define BUFSIZE (1000 * 1000)
71185b48 28
95bc7725
UH
29enum {
30 CHECK_ALL_LOW,
31 CHECK_ALL_HIGH,
32 CHECK_HELLO_WORLD,
33};
71185b48 34
71185b48
UH
35static uint64_t df_packet_counter = 0, sample_counter = 0;
36static gboolean have_seen_df_end = FALSE;
fca75cbb 37static GArray *logic_channellist = NULL;
71185b48
UH
38static int check_to_perform;
39static uint64_t expected_samples;
40static uint64_t *expected_samplerate;
41
25f94dfe 42static void check_all_low(const struct sr_datafeed_logic *logic)
71185b48
UH
43{
44 uint64_t i;
45 uint8_t *data;
46
25f94dfe 47 for (i = 0; i < logic->length; i++) {
71185b48
UH
48 data = logic->data;
49 if (data[i * logic->unitsize] != 0)
50 fail("Logic data was not all-0x00.");
51 }
52}
53
25f94dfe 54static void check_all_high(const struct sr_datafeed_logic *logic)
71185b48
UH
55{
56 uint64_t i;
57 uint8_t *data;
58
25f94dfe 59 for (i = 0; i < logic->length; i++) {
71185b48
UH
60 data = logic->data;
61 if (data[i * logic->unitsize] != 0xff)
62 fail("Logic data was not all-0xff.");
63 }
64}
65
25f94dfe 66static void check_hello_world(const struct sr_datafeed_logic *logic)
71185b48
UH
67{
68 uint64_t i;
69 uint8_t *data, b;
70 const char *h = "Hello world";
71
25f94dfe 72 for (i = 0; i < logic->length; i++) {
71185b48
UH
73 data = logic->data;
74 b = data[sample_counter + i];
75 if (b != h[sample_counter + i])
76 fail("Logic data was not 'Hello world'.");
77 }
78}
79
80static void datafeed_in(const struct sr_dev_inst *sdi,
81 const struct sr_datafeed_packet *packet, void *cb_data)
82{
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;
87 GSList *l;
88 const void *p;
89
90 (void)cb_data;
91
92 fail_unless(sdi != NULL);
93 fail_unless(packet != NULL);
94
95 if (df_packet_counter++ == 0)
96 fail_unless(packet->type == SR_DF_HEADER,
97 "The first packet must be an SR_DF_HEADER.");
98
99 if (have_seen_df_end)
100 fail("There must be no packets after an SR_DF_END, but we "
101 "received a packet of type %d.", packet->type);
102
103 p = packet->payload;
104
105 switch (packet->type) {
106 case SR_DF_HEADER:
107 // g_debug("Received SR_DF_HEADER.");
108 // fail_unless(p != NULL, "SR_DF_HEADER payload was NULL.");
109
fca75cbb
UH
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);
71185b48
UH
114 break;
115 case SR_DF_META:
116 // g_debug("Received SR_DF_META.");
117
118 meta = packet->payload;
119 fail_unless(p != NULL, "SR_DF_META payload was NULL.");
120
121 for (l = meta->config; l; l = l->next) {
122 src = l->data;
123 // g_debug("Got meta key: %d.", src->key);
124 switch (src->key) {
125 case SR_CONF_SAMPLERATE:
126 samplerate = g_variant_get_uint64(src->data);
127 if (!expected_samplerate)
128 break;
129 fail_unless(samplerate == *expected_samplerate,
130 "Expected samplerate=%" PRIu64 ", "
131 "got %" PRIu64 "", samplerate,
132 *expected_samplerate);
133 // g_debug("samplerate = %" PRIu64 " Hz.",
134 // samplerate);
135 break;
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.",
140 // sample_interval);
141 break;
142 default:
143 /* Unknown metadata is not an error. */
144 g_debug("Got unknown meta key: %d.", src->key);
145 break;
146 }
147 }
148 break;
149 case SR_DF_LOGIC:
150 logic = packet->payload;
151 fail_unless(p != NULL, "SR_DF_LOGIC payload was NULL.");
152
153 // g_debug("Received SR_DF_LOGIC (%" PRIu64 " bytes, "
154 // "unitsize %d).", logic->length, logic->unitsize);
155
156 if (check_to_perform == CHECK_ALL_LOW)
25f94dfe 157 check_all_low(logic);
71185b48 158 else if (check_to_perform == CHECK_ALL_HIGH)
25f94dfe 159 check_all_high(logic);
71185b48 160 else if (check_to_perform == CHECK_HELLO_WORLD)
25f94dfe 161 check_hello_world(logic);
71185b48
UH
162
163 sample_counter += logic->length / logic->unitsize;
164
165 break;
166 case SR_DF_END:
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);
173 break;
174 default:
175 /*
176 * Note: The binary input format doesn't support SR_DF_TRIGGER
177 * and some other types, those should yield an error.
178 */
179 fail("Invalid packet type: %d.", packet->type);
180 break;
181 }
182}
183
95bc7725
UH
184static void check_buf(GHashTable *options, const uint8_t *buf, int check,
185 uint64_t samples, uint64_t *samplerate)
71185b48
UH
186{
187 int ret;
188 struct sr_input *in;
95bc7725 189 const struct sr_input_module *imod;
dfa4cd0e 190 struct sr_session *session;
95bc7725
UH
191 struct sr_dev_inst *sdi;
192 GString *gbuf;
71185b48
UH
193
194 /* Initialize global variables for this run. */
195 df_packet_counter = sample_counter = 0;
196 have_seen_df_end = FALSE;
fca75cbb 197 logic_channellist = NULL;
71185b48
UH
198 check_to_perform = check;
199 expected_samples = samples;
200 expected_samplerate = samplerate;
201
95bc7725 202 gbuf = g_string_new_len((gchar *)buf, (gssize)samples);
71185b48 203
95bc7725
UH
204 imod = sr_input_find("binary");
205 fail_unless(imod != NULL, "Failed to find input module.");
71185b48 206
95bc7725
UH
207 in = sr_input_new(imod, options);
208 fail_unless(in != NULL, "Failed to create input instance.");
71185b48 209
95bc7725 210 sdi = sr_input_dev_inst_get(in);
71185b48 211
ca1c21ca 212 sr_session_new(srtest_ctx, &session);
dfa4cd0e 213 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
95bc7725
UH
214 sr_session_dev_add(session, sdi);
215
216 ret = sr_input_send(in, gbuf);
217 fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret);
026d6b2f 218 sr_input_free(in);
95bc7725 219
dfa4cd0e 220 sr_session_destroy(session);
71185b48 221
95bc7725 222 g_string_free(gbuf, TRUE);
71185b48
UH
223}
224
225START_TEST(test_input_binary_all_low)
226{
227 uint64_t i, samplerate;
95bc7725 228 GHashTable *options;
71185b48 229 uint8_t *buf;
95bc7725 230 GVariant *gvar;
71185b48 231
95bc7725 232 buf = g_malloc0(BUFSIZE);
71185b48 233
95bc7725
UH
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));
71185b48
UH
239 samplerate = SR_HZ(1250);
240
241 /* Check various filesizes, with/without specifying a samplerate. */
95bc7725
UH
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);
71185b48
UH
247 }
248
95bc7725 249 g_hash_table_destroy(options);
71185b48
UH
250 g_free(buf);
251}
252END_TEST
253
254START_TEST(test_input_binary_all_high)
255{
256 uint64_t i;
257 uint8_t *buf;
258
95bc7725
UH
259 buf = g_malloc(BUFSIZE);
260 memset(buf, 0xff, BUFSIZE);
71185b48 261
95bc7725
UH
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);
71185b48
UH
265
266 g_free(buf);
267}
268END_TEST
269
270START_TEST(test_input_binary_all_high_loop)
271{
272 uint8_t *buf;
95bc7725 273 uint64_t bufsize;
71185b48
UH
274
275 /* Note: _i is the loop variable from tcase_add_loop_test(). */
276
95bc7725
UH
277 bufsize = (_i * 10);
278 buf = g_malloc(BUFSIZE);
279 memset(buf, 0xff, BUFSIZE);
71185b48 280
95bc7725 281 check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL);
71185b48
UH
282
283 g_free(buf);
284}
285END_TEST
286
287START_TEST(test_input_binary_hello_world)
288{
289 uint64_t samplerate;
290 uint8_t *buf;
95bc7725
UH
291 GHashTable *options;
292 GVariant *gvar;
71185b48
UH
293
294 buf = (uint8_t *)g_strdup("Hello world");
295
95bc7725
UH
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));
71185b48
UH
301 samplerate = SR_HZ(1250);
302
303 /* Check with and without specifying a samplerate. */
95bc7725
UH
304 check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL);
305 check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate);
71185b48 306
95bc7725 307 g_hash_table_destroy(options);
71185b48
UH
308 g_free(buf);
309}
310END_TEST
311
312Suite *suite_input_binary(void)
313{
314 Suite *s;
315 TCase *tc;
316
317 s = suite_create("input-binary");
318
319 tc = tcase_create("basic");
98de0c78 320 tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown);
71185b48
UH
321 tcase_add_test(tc, test_input_binary_all_low);
322 tcase_add_test(tc, test_input_binary_all_high);
95bc7725 323 tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10);
71185b48
UH
324 tcase_add_test(tc, test_input_binary_hello_world);
325 suite_add_tcase(s, tc);
326
327 return s;
328}