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